fix(core): prevent bots from invading/attacking themselves (#3865) (#4014)

Resolves #4094

## Description:

In Free-For-All (FFA) mode where teams default to 0, player isOnSameTeam
checks returned false for oneself, allowing players to attack
themselves. Consequently, if a bot conquered the targeted tile between
queueing a transport ship action and its actual initialization, the
target became itself, causing the bot to execute a self-invasion.

This fix adds a reflexive check in PlayerImpl.ts's isFriendly method to
always treat oneself as friendly. It also adds a safety guard in
TransportShipExecution.ts's init method to abort ship execution if the
target has shifted to the attacker.

## Please complete the following:

- [X] I have added screenshots for all UI updates
- [X] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [X] I have added relevant tests to the test directory
- [X] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

barfires
This commit is contained in:
Berk
2026-05-31 20:05:51 -07:00
committed by GitHub
parent b38f8ed1f8
commit f3ba95574c
4 changed files with 83 additions and 0 deletions
+48
View File
@@ -239,3 +239,51 @@ describe("Donate Gold to a non ally", () => {
expect(recipient.gold() >= recipientGoldBefore).toBe(true);
});
});
describe("Self donation prevention", () => {
it("Should evaluate isFriendly(this) to true but disallow donating to self", async () => {
const game = await setup("ocean_and_land", {
infiniteGold: false,
infiniteTroops: false,
donateGold: true,
donateTroops: true,
});
const gameID: GameID = "game_id";
// Create a player with team=0/null (default/FFA)
const playerInfo = new PlayerInfo(
"player_self",
PlayerType.Human,
null,
"self_id",
);
game.addPlayer(playerInfo);
const player = game.player(playerInfo.id);
const spawnA = game.ref(0, 10);
game.addExecution(new SpawnExecution(gameID, playerInfo, spawnA));
game.executeNextTick();
// Assert player.isFriendly(player) === true
expect(player.isFriendly(player)).toBe(true);
// Assert canDonateGold and canDonateTroops return false for self
expect(player.canDonateGold(player)).toBe(false);
expect(player.canDonateTroops(player)).toBe(false);
// Try executing DonateGoldExecution and DonateTroopsExecution on self
player.addGold(1000n);
player.addTroops(1000);
const goldBefore = player.gold();
const troopsBefore = player.troops();
game.addExecution(new DonateGoldExecution(player, player.id(), 500));
game.addExecution(new DonateTroopsExecution(player, player.id(), 500));
game.executeNextTick();
// Verify no changes occurred to gold or troops (execution failed/aborted)
expect(player.gold()).toBeGreaterThanOrEqual(goldBefore);
expect(player.troops()).toBeGreaterThanOrEqual(troopsBefore);
});
});