Skip to content

Commit

Permalink
fix: battle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
trevelyan committed Jan 23, 2024
1 parent b02c7dd commit 664558c
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 80 deletions.
186 changes: 146 additions & 40 deletions mods/his/his.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions mods/his/lib/ui/overlays/movement.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ module.exports = MovementOverlayTemplate = (obj, his_self) => {

if (!obj.to) { obj.to = "Unknown"; }


let html = `<div class="movement-overlay">
<div class="help">
<div class="movement-from">${obj.from}</div> to <div class="movement-to">${obj.to}</div></div>
<div class="movement-table"><div class="movement-source">`;
for (let i = 0; i < obj.unmoved_units.length; i++) {

console.log("UNMOVED: " + JSON.stringify(obj.unmoved_units[i]));

let ucss = "";
let uclass = "";
let utype = obj.unmoved_units[i].type;
Expand Down
31 changes: 17 additions & 14 deletions mods/his/src/his-allies.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,35 @@
return f;
}

areAllies(faction1, faction2) {
areAllies(faction1, faction2, count_minor_activated_factions=1) {
try { if (this.game.state.diplomacy[faction1][faction2].allies == 1) { return 1; } } catch (err) {}
try { if (this.game.state.diplomacy[faction2][faction1].allies == 1) { return 1; } } catch (err) {}
try { if (this.game.state.activated_powers[faction1].includes(faction2)) { return 1; } } catch (err) {}
try { if (this.game.state.activated_powers[faction2].includes(faction1)) { return 1; } } catch (err) {}
if (this.isMinorPower(faction1) || this.isMinorPower(faction2)) {
let f1cp = this.returnControllingPower(faction1);
let f2cp = this.returnControllingPower(faction2);
try { if (this.game.state.diplomacy[f2cp][f1cp].allies == 1) { return 1; } } catch (err) {}
try { if (this.game.state.diplomacy[f1cp][f2cp].allies == 1) { return 1; } } catch (err) {}
try { if (this.game.state.diplomacy[f2cp][f1cp].allies == 1) { return 1; } } catch (err) {}
if (count_minor_activated_factions) {
if (this.isMinorPower(faction1) || this.isMinorPower(faction2)) {
let f1cp = this.returnControllingPower(faction1);
let f2cp = this.returnControllingPower(faction2);
try { if (this.game.state.diplomacy[f2cp][f1cp].allies == 1) { return 1; } } catch (err) {}
try { if (this.game.state.diplomacy[f1cp][f2cp].allies == 1) { return 1; } } catch (err) {}
try { if (this.game.state.diplomacy[f2cp][f1cp].allies == 1) { return 1; } } catch (err) {}
}
}
return 0;
}

areEnemies(faction1, faction2) {
areEnemies(faction1, faction2, count_minor_activated_factions=1) {
try { if (this.game.state.diplomacy[faction1][faction2].enemies == 1) { return 1; } } catch (err) {}
try { if (this.game.state.diplomacy[faction2][faction1].enemies == 1) { return 1; } } catch (err) {}
try { if (this.game.state.activated_powers[faction1].includes(faction2)) { return 0; } } catch (err) {}
try { if (this.game.state.activated_powers[faction2].includes(faction1)) { return 0; } } catch (err) {}
if (this.isMinorPower(faction1) || this.isMinorPower(faction2)) {
let f1cp = this.returnControllingPower(faction1);
let f2cp = this.returnControllingPower(faction2);
try { if (this.game.state.diplomacy[f1cp][f2cp].enemies == 1) { return 1; } } catch (err) {}
try { if (this.game.state.diplomacy[f2cp][f1cp].enemies == 1) { return 1; } } catch (err) {}
if (count_minor_activated_factions) {
if (this.isMinorPower(faction1) || this.isMinorPower(faction2)) {
let f1cp = this.returnControllingPower(faction1);
let f2cp = this.returnControllingPower(faction2);
try { if (this.game.state.diplomacy[f1cp][f2cp].enemies == 1) { return 1; } } catch (err) {}
try { if (this.game.state.diplomacy[f2cp][f1cp].enemies == 1) { return 1; } } catch (err) {}
}
}
return 0;
}
Expand Down Expand Up @@ -98,7 +102,6 @@
}
}


if (amp == 1) {
if (this.isMinorPower(faction1)) {
if (!this.isMinorPower(faction2)) {
Expand Down
13 changes: 10 additions & 3 deletions mods/his/src/his-cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -1304,12 +1304,14 @@ if (space.key === "bordeaux") {
onEvent : function(his_self, faction) {

let controlling_power = "papacy";
let victim_power = "protestant";

//
// prior to League formation
//
if (his_self.game.state.events.schmalkaldic_league != 1) {
controlling_power = "protestant";
victim_power = "papacy";
his_self.setEnemies("papacy","hapsburg");
}

Expand All @@ -1324,7 +1326,7 @@ if (space.key === "bordeaux") {
// controlling power gets 1 card
//
his_self.game.queue.push(`DEAL\t1\t${controlling_player}\t1`);
his_self.game.queue.push("spanish_invasion_land\t"+controlling_player+"\t"+controlling_power);
his_self.game.queue.push("spanish_invasion_land\t"+controlling_player+"\t"+controlling_power+"\t"+victim_power);

return 1;
},
Expand All @@ -1336,14 +1338,15 @@ if (space.key === "bordeaux") {

let controlling_player = parseInt(mv[1]);
let controlling_power = mv[2];
let victim_power = mv[3];

if (his_self.game.player === controlling_player) {

//
// 2P card, so french get activated under protestant control
// 2P card, so spanish get activated under protestant control
//
his_self.addMove("set_activated_powers\t"+controlling_power+"\thapsburg");
his_self.addMove("declare_war\t"+controlling_power+"\thapsburg");
his_self.addMove("declare_war\t"+victim_power+"\thapsburg");

his_self.playerSelectSpaceWithFilter(

Expand All @@ -1356,6 +1359,8 @@ if (space.key === "bordeaux") {

function(spacekey) {

his_self.updateStatus("acknowledge...");

//
// move Duke of Alva, add regulars
//
Expand Down Expand Up @@ -1420,6 +1425,7 @@ if (space.key === "bordeaux") {
},

function(spacekey) {
his_self.updateStatus("acknowledge...");
his_self.addMove("build\tland\thapsburg\t"+"squadron"+"\t"+spacekey);
his_self.endTurn();
},
Expand All @@ -1431,6 +1437,7 @@ if (space.key === "bordeaux") {
}

if (action === "mercenaries") {
his_self.updateStatus("acknowledge...");
his_self.addMove("build\tland\thapsburg\t"+"mercenary"+"\t"+land_spacekey);
his_self.addMove("build\tland\thapsburg\t"+"mercenary"+"\t"+land_spacekey);
his_self.endTurn();
Expand Down
4 changes: 2 additions & 2 deletions mods/his/src/his-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@
let box = '#' + factions[i] + "_" + factions[ii];
obj = document.querySelector(box);
if (obj) {
if (this.areAllies(factions[i], factions[ii])) {
if (this.areAllies(factions[i], factions[ii], 0)) {
obj.innerHTML = '<img src="/his/img/Allied.svg" />';
obj.style.display = "block";
} else {
if (this.areEnemies(factions[i], factions[ii])) {
if (this.areEnemies(factions[i], factions[ii], 0)) {
obj.innerHTML = '<img src="/his/img/AtWar.svg" />';
obj.style.display = "block";
} else {
Expand Down
51 changes: 40 additions & 11 deletions mods/his/src/his-gameloop.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,8 @@ if (this.game.state.scenario != "is_testing") {
this.game.spaces['graz'].type = 'key';
this.game.spaces['graz'].occupier = 'protestant';

this.setEnemies("papacy","hapsburg");
this.setActivatedPower("protestant", "hapsburg");

this.controlSpace("france", "genoa");
this.addRegular("france", "genoa", 3);
Expand Down Expand Up @@ -1590,7 +1592,7 @@ console.log("NO-ONE BUT US, ADD ALLY CHECK!");

for (let zz = 0; zz < this.game.state.activated_powers[io[i]].length; zz++) {
let ap = this.game.state.activated_powers[io[i]][zz];
if (ap != attacker) {
if (ap !== attacker && !io.includes(ap) && io[i] != attacker) {
let units_in_space = this.returnFactionLandUnitsInSpace(ap, spacekey);
if (units_in_space > 0) {
for (let zz = 0; zz < neighbours.length; zz++) {
Expand Down Expand Up @@ -4322,9 +4324,39 @@ console.log("we have made it this far!");
}
}
}
this.game.queue.push("post_field_battle_player_evaluate_fortification\t"+his_self.game.state.field_battle.attacker_faction+"\t"+his_self.returnPlayerOfFaction(his_self.game.state.field_battle.defender_faction)+"\t"+his_self.game.state.field_battle.defender_faction+"\t"+space.key);

let defender_player = this.returnPlayerCommandingFaction(his_self.game.state.field_battle.defender_faction);
let attacker_player = this.returnPlayerCommandingFaction(his_self.game.state.field_battle.attacker_faction);

// if the defender is the active player AND controls the space, they can fortify
if (defender_player == this.game.state.active_player && this.isSpaceControlled(his_self.game.state.field_battle.spacekey, his_self.game.state.field_battle.defender_faction)) {
this.game.queue.push("post_field_battle_player_evaluate_fortification\t"+his_self.game.state.field_battle.attacker_faction+"\t"+his_self.returnPlayerOfFaction(his_self.game.state.field_battle.defender_faction)+"\t"+his_self.game.state.field_battle.defender_faction+"\t"+space.key);
// otherwise they need to retreat
} else {
for (let f in his_self.game.state.field_battle.faction_map) {
let can_faction_retreat = 0;
if (his_self.game.state.field_battle.faction_map[f] === his_self.game.state.field_battle.defender_faction) {
for (let z = 0; z < space.neighbours.length; z++) {
let fluis = this.canFactionRetreatToSpace(f, space.neighbours[z], "");
if (fluis > 0) {
can_faction_retreat = 1;
}
}
if (can_faction_retreat == 1) {
this.game.queue.push("purge_units_and_capture_leaders\t"+f+"\t"+his_self.game.state.field_battle.attacker_faction+"\t"+space.key);
if (his_self.game.state.field_battle.defender_land_units_remaining > 0) {
this.game.queue.push("post_field_battle_player_evaluate_retreat\t"+f+"\t"+space.key);
}
}
if (can_faction_retreat == 0) {
this.game.queue.push("purge_units_and_capture_leaders\t"+f+"\t"+his_self.game.state.field_battle.attacker_faction+"\t"+space.key);
}
}
}
}
}


//
// redisplay
//
Expand All @@ -4336,7 +4368,6 @@ console.log("we have made it this far!");
his_self.field_battle_overlay.renderPostFieldBattle(his_self.game.state.field_battle);
his_self.field_battle_overlay.pullHudOverOverlay();


return 1;

}
Expand Down Expand Up @@ -5017,8 +5048,6 @@ console.log("spacekey: " + spacekey);
his_self.game.state.assault.attacker_hits_first = 0;
his_self.game.state.assault.defender_hits_first = 0;

console.log("ASSAULT: " + JSON.stringify(his_self.game.state.assault));

his_self.game.queue.push(`assault_continue\t${mv[1]}\t${mv[2]}`);

let ap = {};
Expand Down Expand Up @@ -5586,9 +5615,9 @@ console.log("naval space units: " + JSON.stringify(space.units));
// fortification has already happened. if the loser is the attacker, they have to retreat
//
if (this.game.player == this.returnPlayerOfFaction(loser)) {
let winning_faction = attacker_faction;
let is_attacker_loser = false;
if (loser === attacker_faction) { is_attacker_loser = true; }
this.playerEvaluateRetreatOpportunity(attacker_faction, spacekey, this.game.state.attacker_comes_from_this_spacekey, defender_faction, is_attacker_loser);
this.playerEvaluateRetreatOpportunity(loser, winning_faction, attacker_faction, spacekey, this.game.state.attacker_comes_from_this_spacekey);
} else {
this.updateStatus(this.returnFactionName(loser) + " considering post-battle retreat");
}
Expand Down Expand Up @@ -6403,7 +6432,7 @@ defender_hits - attacker_hits;
} else {
this.updateLog("Protestants translate New Testament (french)");
this.game.state.translations['new']['french']++;
if (this.game.state.translations['full']['french'] == 6) {
if (this.game.state.translations['new']['french'] == 6) {
// protestant gets 1 roll bonus at start
his_self.game.queue.push("hide_overlay\ttheses");
his_self.game.queue.push("remove_translation_bonus");
Expand All @@ -6419,7 +6448,7 @@ defender_hits - attacker_hits;
}
}
if (zone === "english") {
if (this.game.state.translations['new']['english'] >= 6) {
if (this.game.state.translations['new']['english'] >= 6) {
this.updateLog("Protestants translate Old Testament (english)");
this.game.state.translations['full']['english']++;
if (this.game.state.translations['full']['english'] == 10) {
Expand All @@ -6439,7 +6468,7 @@ defender_hits - attacker_hits;
} else {
this.updateLog("Protestants translate New Testament (english)");
this.game.state.translations['new']['english']++;
if (this.game.state.translations['full']['english'] == 6) {
if (this.game.state.translations['new']['english'] == 6) {
// protestant gets 1 roll bonus at start
his_self.game.queue.push("hide_overlay\ttheses");
his_self.game.queue.push("remove_translation_bonus");
Expand Down Expand Up @@ -7629,7 +7658,7 @@ console.log("RESHUFFLE: " + JSON.stringify(reshuffle_cards));
});
} else {
this.hideOverlays();
this.updateStatusAndListCards("Opponent Turn", () => {});
this.updateStatusAndListCards("Opponent Turn", his_self.game.deck[0].fhand[0], () => {});
}

return 0;
Expand Down
Loading

0 comments on commit 664558c

Please sign in to comment.