Skip to content

Commit

Permalink
autobattles moved out to separate js file
Browse files Browse the repository at this point in the history
  • Loading branch information
Areso committed Jan 7, 2023
1 parent e3b5833 commit 560b1a1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 83 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ <h3 id="lblTabInn">Welcome to Inn!</h3>
<script async src="js/tab_building.js"></script>
<!--JS LIBRARY FOR MODAL DIALOGUES-->
<script async src="js/lib_dialogue.js"></script>
<!--JS AUTOBATTLES-->
<script async src="js/mech_autobattle.js"></script>
<!--ADVENTURE GENERATOR MAP-->
<script async src="js/mech_advmap_gen.js"></script>
<!--ADVENTURE RENDERER MAP-->
Expand Down
85 changes: 2 additions & 83 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
}
game.isAutoBattle = true;
while(game.isAutoBattle) {
game.calcAttackPhase("AdvMap");
if (typeof calcAttackPhase === "function") { calcAttackPhase("AdvMap") };
}
//suppose we win the battle
//removing the monster from removable objects (render array)
Expand Down Expand Up @@ -809,86 +809,6 @@
default: return "Unknown unit name";
}
},
calcAutoBattle: function(attackerStack, attackerHeroAttack, defenderStack, defenderHeroDefence, campaignType) {
console.log('--Attack start --');
var startRoundStr = locObj.autobattle_journal_log_start.txt;
var endRoundStr = locObj.autobattle_journal_log_end.txt;
if (campaignType==="AutoCampaign") {
postBattleLog(startRoundStr);
}
for (var attackerKey in attackerStack.units) {
if (attackerStack.units.hasOwnProperty(attackerKey)) {
console.log('Attacker: ', attackerStack.units[attackerKey]);
for (var defenderKey in defenderStack.units) {
if (defenderStack.units.hasOwnProperty(defenderKey)) {
var target = defenderStack.units[defenderKey];
var dmg = game.calcDmg(attackerStack.units[attackerKey], attackerHeroAttack, defenderStack.units[defenderKey], defenderHeroDefence);
console.log('DMG before round: ', dmg);
dmg = Math.round(dmg);
console.log(dmg);
var attackerStr = locObj.autobattle_journal_log_dmg.txt.replace("%arg1", game.unitNameLoc(attackerStack.units[attackerKey].name)).replace("%arg2", dmg).replace("%arg3", game.unitNameLoc(target.name));
if (dmg >= target.stackHealth) {
console.log(`${target.name} is dying`);
delete defenderStack.units[defenderKey];
game.myhero[target.name] = 0;
console.log("%c DEFENDER UNITS LIST: ", "background: purple; color: #ededed", defenderStack.units);
attackerStr += ' ' + locObj.autobattle_journal_log_destroyed.txt.replace("%arg1", game.unitNameLoc(target.name));
if (campaignType==="AutoCampaign") {
postBattleLog(attackerStr);
}
if (isEmptyObj(defenderStack.units)) {
game.isAutoBattle = false;
game.checkWinner(campaignType);
if (campaignType === "AutoCampaign") {
game.attacker = 0;
}
}
} else {
var defenderLooses = parseInt(dmg / defenderStack.units[defenderKey].health);
defenderStack.units[defenderKey].count -= defenderLooses;
game.myhero[defenderKey] -= defenderLooses;
target.stackHealth -= dmg;
if (defenderLooses > 0) {
attackerStr += ' ' + locObj.autobattle_journal_log_dead.txt.replace("%arg1", defenderLooses).replace("%arg2", game.unitNameLoc(target.name));
}
if (campaignType==="AutoCampaign") {
postBattleLog(attackerStr);
}
}
}
}
}
}
if (campaignType==="AutoCampaign") {
postBattleLog(endRoundStr);
}
console.log('-- Attack turn ended --');
console.log('-----------------------');
},
calcAttackPhase: function (campaignType) {
var attackVal = 0;
var defenceVal = 0;
if (game.attacker === 1) {
if (game.myhero.atk) {
attackVal = game.myhero.atk;
}
if (game.enemyHero && game.enemyHero.def) {
defenceVal = game.enemyHero.def
}
console.log("%cENEMY ARMY FROM MAP: ", 'color: #333; background: #fc3; padding: 6px;', game.enemyHeroArmy);
game.calcAutoBattle(game.myheroArmy, attackVal, game.enemyHeroArmy, defenceVal, campaignType);
game.attacker = 2;
} else if (game.attacker === 2) {
if (game.enemyHero && game.enemyHero.atk) {
attackVal = game.enemyHero.atk;
}
if (game.myhero.def) {
defenceVal = game.myhero.def
}
game.calcAutoBattle(game.enemyHeroArmy, attackVal, game.myheroArmy, defenceVal, campaignType);
game.attacker = 1;
}
},
heroDismiss: function () {
if (game.heroExists()===true){
if (game.isHeroHaveTroops()) {
Expand Down Expand Up @@ -1106,9 +1026,8 @@
if (rnd <= 12) {
game.isAutoBattle = true;
createBattleAccordion();

while (game.isAutoBattle) {
game.calcAttackPhase("AutoCampaign");
if (typeof calcAttackPhase === "function") { calcAttackPhase("AutoCampaign") };
}
} else {
postJournalLog(localeStrings[175]);
Expand Down
81 changes: 81 additions & 0 deletions js/mech_autobattle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
function calcAttackPhase (campaignType) {
var attackVal = 0;
var defenceVal = 0;
if (game.attacker === 1) {
if (game.myhero.atk) {
attackVal = game.myhero.atk;
}
if (game.enemyHero && game.enemyHero.def) {
defenceVal = game.enemyHero.def
}
console.log("%cENEMY ARMY FROM MAP: ", 'color: #333; background: #fc3; padding: 6px;', game.enemyHeroArmy);
calcAutoBattle(game.myheroArmy, attackVal, game.enemyHeroArmy, defenceVal, campaignType);
game.attacker = 2;
}
if (game.attacker === 2) {
if (game.enemyHero && game.enemyHero.atk) {
attackVal = game.enemyHero.atk;
}
if (game.myhero.def) {
defenceVal = game.myhero.def
}
calcAutoBattle(game.enemyHeroArmy, attackVal, game.myheroArmy, defenceVal, campaignType);
game.attacker = 1;
}
}
function calcAutoBattle (attackerStack, attackerHeroAttack, defenderStack, defenderHeroDefence, campaignType) {
console.log('--Attack start --');
var startRoundStr = locObj.autobattle_journal_log_start.txt;
var endRoundStr = locObj.autobattle_journal_log_end.txt;
if (campaignType==="AutoCampaign") {
postBattleLog(startRoundStr);
}
for (var attackerKey in attackerStack.units) {
if (attackerStack.units.hasOwnProperty(attackerKey)) {
console.log('Attacker: ', attackerStack.units[attackerKey]);
for (var defenderKey in defenderStack.units) {
if (defenderStack.units.hasOwnProperty(defenderKey)) {
var target = defenderStack.units[defenderKey];
var dmg = game.calcDmg(attackerStack.units[attackerKey], attackerHeroAttack, defenderStack.units[defenderKey], defenderHeroDefence);
console.log('DMG before round: ', dmg);
dmg = Math.round(dmg);
console.log(dmg);
var attackerStr = locObj.autobattle_journal_log_dmg.txt.replace("%arg1", game.unitNameLoc(attackerStack.units[attackerKey].name)).replace("%arg2", dmg).replace("%arg3", game.unitNameLoc(target.name));
if (dmg >= target.stackHealth) {
console.log(`${target.name} is dying`);
delete defenderStack.units[defenderKey];
game.myhero[target.name] = 0;
console.log("%c DEFENDER UNITS LIST: ", "background: purple; color: #ededed", defenderStack.units);
attackerStr += ' ' + locObj.autobattle_journal_log_destroyed.txt.replace("%arg1", game.unitNameLoc(target.name));
if (campaignType==="AutoCampaign") {
postBattleLog(attackerStr);
}
if (isEmptyObj(defenderStack.units)) {
game.isAutoBattle = false;
game.checkWinner(campaignType);
if (campaignType === "AutoCampaign") {
game.attacker = 0;
}
}
} else {
var defenderLooses = parseInt(dmg / defenderStack.units[defenderKey].health);
defenderStack.units[defenderKey].count -= defenderLooses;
game.myhero[defenderKey] -= defenderLooses;
target.stackHealth -= dmg;
if (defenderLooses > 0) {
attackerStr += ' ' + locObj.autobattle_journal_log_dead.txt.replace("%arg1", defenderLooses).replace("%arg2", game.unitNameLoc(target.name));
}
if (campaignType==="AutoCampaign") {
postBattleLog(attackerStr);
}
}
}
}
}
}
if (campaignType==="AutoCampaign") {
postBattleLog(endRoundStr);
}
console.log('-- Attack turn ended --');
console.log('-----------------------');
}

0 comments on commit 560b1a1

Please sign in to comment.