Created
December 29, 2015 21:18
-
-
Save SharpCoder/ab135c5c044f5a24f149 to your computer and use it in GitHub Desktop.
combat system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ships = [[shipName, qty]] | |
ShipManager.prototype.combat = function (user1, user1Ships, user2, user2Ships, mission, success, supressMessages, supressFleet1Death, supressFleet2Death) { | |
supressMessages = false || supressMessages; | |
// Calcualte the total attack power for each fleet. | |
var gameContext = this.gameContext, | |
fleet1Flat = [], | |
fleet2Flat = []; | |
// Populate the lists. | |
for (var i = 0; i < user1Ships.length; i++) { | |
if (!user1Ships[i]) continue; | |
for (var r = 0; r < user1Ships[i][1]; r++) { | |
fleet1Flat.push(gameContext.ShipManager.find(user1Ships[i][0])); | |
} | |
} | |
for (var i = 0; i < user2Ships.length; i++) { | |
if (!user2Ships[i]) continue; | |
for (var r = 0; r < user2Ships[i][1]; r++) { | |
fleet2Flat.push(gameContext.ShipManager.find(user2Ships[i][0])); | |
} | |
} | |
// Dice | |
function d(num) { | |
return Math.round(Math.random() * num); | |
} | |
// Keep track of how many ships were destroyed by either side. | |
var oFleet1Destroyed = {}; | |
var oFleet2Destroyed = {}; | |
var fleet1Destroyed = 0, | |
fleet2Destroyed = 0; | |
do { | |
// Randomize the fleets. | |
fleet1Flat.sort(function (a, b) { return Math.random(); }); | |
fleet2Flat.sort(function (a, b) { return Math.random(); }); | |
// Iterate over the list of the smaller fleet. | |
for (var i = 0; i < Math.min(fleet1Flat.length, fleet2Flat.length); i++) { | |
var ship1 = fleet1Flat[i]; | |
var ship2 = fleet2Flat[i]; | |
// Do the dice roll. | |
if (d(ship1.attack + ship1.attackBonus) > d(ship2.attack + ship2.attackBonus)) { | |
// Ship1 wins, kill ship 2. | |
fleet2Flat.splice(i--, 1); | |
fleet2Destroyed++; | |
if (oFleet2Destroyed.hasOwnProperty(ship2.name)) | |
oFleet2Destroyed[ship2.name]++; | |
else | |
oFleet2Destroyed[ship2.name] = 1; | |
} else { | |
// Ship2 wins, kill ship 1. | |
fleet1Flat.splice(i--, 1); | |
fleet1Destroyed++; | |
if (oFleet1Destroyed.hasOwnProperty(ship1.name)) | |
oFleet1Destroyed[ship1.name]++; | |
else | |
oFleet1Destroyed[ship1.name] = 1; | |
} | |
} | |
} while(fleet1Flat.length > 0 && fleet2Flat.length > 0); | |
// Create a delegate. | |
function complete() { | |
var fleet = []; | |
var winnerShipsDestroyed = 0; | |
var loserShipsDestroyed = 0; | |
var winner = ''; | |
var loser = ''; | |
if (fleet1Flat.length > 0) { | |
// First user won the combat. | |
fleet = fleet1Flat; | |
winner = user1; | |
loser = user2; | |
winnerShipsDestroyed = fleet1Destroyed; | |
loserShipsDestroyed = fleet2Destroyed; | |
} else { | |
// Second user won the combat. | |
fleet = fleet2Flat; | |
winner = user2; | |
loser = user1; | |
winnerShipsDestroyed = fleet2Destroyed; | |
loserShipsDestroyed = fleet1Destroyed; | |
} | |
// Send the notifications. | |
if (!supressMessages) { | |
if (winnerShipsDestroyed > 0) { | |
gameContext.UserManager.addNotification(winner, "blue", "You have lost " + winnerShipsDestroyed + " ships during a space battle with " + loser); | |
} else { | |
gameContext.UserManager.addNotification(winner, "blue", "All ships managed to survive during the space battle with " + loser); | |
} | |
gameContext.UserManager.addNotification(loser, "red", "You have lost all " + loserShipsDestroyed + " ships during the space battle with " + winner); | |
} | |
// Iterate over the fleet, reconstruct the list. | |
var result = [], | |
obj = {}; | |
for (var i = 0; i < fleet.length; i++) { | |
obj[fleet[i].name] = (obj[fleet[i].name] || 0) + 1; | |
} | |
// Iterate over the object. Rebuild. | |
for (var prop in obj) { | |
result.push([prop, parseFloat(obj[prop])]); | |
} | |
// Return the result. | |
if (success) success.call(this, winner, result); | |
} | |
var expected = 0; | |
var destroyed = 0; | |
// Calculate expected. | |
if (!supressFleet1Death) expected += fleet1Destroyed; | |
if (!supressFleet2Death) expected += fleet2Destroyed; | |
function attemptDelegate(qty){ | |
gameContext.lock(mission.toString(), 5000, function (done) { | |
destroyed += qty; | |
if (destroyed >= expected) { | |
done(); | |
complete(); | |
} else { | |
done(); | |
} | |
}); | |
} | |
// Destroy the ships. | |
for (var prop in oFleet2Destroyed) { | |
if (!supressFleet2Death) { | |
gameContext.ShipManager.killShip(prop, true, mission, user2, attemptDelegate.bind(this, oFleet2Destroyed[prop]), oFleet2Destroyed[prop]); | |
} | |
} | |
for (var prop in oFleet1Destroyed) { | |
if (!supressFleet1Death) { | |
gameContext.ShipManager.killShip(prop, true, mission, user1, attemptDelegate.bind(this, oFleet1Destroyed[prop]), oFleet1Destroyed[prop]); | |
} | |
} | |
// Odd condition in which no ships were lost (technically). | |
// which is possible through the supression flags. | |
if (expected == 0) attemptDelegate(0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment