Files
OpenFrontIO/tests/BotBehavior.test.ts
FloPinguin dfe33a05e9 Improved the nation alliance request logic 🤝 Massive upgrade to singleplayer fun (#2606)
## Response to alliance requests

Previously the way nations responded to alliance requests was quite
simple / boring / exploitable. Basically you couldn't ally them if you
had a bad relation with them, or if you had too many alliances.
Otherwise they would just take it.

Now there is a **complete decision tree which is based on the
difficulty**. The nations should also feel more human now.

For example, just like humans, nations will now consider to take an
alliance even if you have a bad relation with them (If you are a
threat).

Also, nations no longer check if YOU have too many alliances. Now they
do what humans do: Check if THEY have too many alliances (they want to
be able to attack somebody).

Another big change is the default case: Previously it was just `return
true`. Now it's `return isAlliancePartnerSimilarlyStrong`. So they do
what humans do: Take a quick look at their troop count before allying
them.

## Sending alliance requests

Previously alliance requests were sent randomly. Quite boring.

Now we use the same decision tree as for responding.

## Alliance extension requests

They also use the same decision tree.

## Tests

Tested it a lot in singleplayer.
I have planned to add unit tests for all the nation/bot stuff in the
upcoming cleanup phase.

## 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:

FloPinguin
2025-12-14 15:18:07 -08:00

161 lines
4.1 KiB
TypeScript

import { BotBehavior } from "../src/core/execution/utils/BotBehavior";
import { Game, Player, PlayerInfo, PlayerType } from "../src/core/game/Game";
import { PseudoRandom } from "../src/core/PseudoRandom";
import { setup } from "./util/Setup";
describe("BotBehavior Attack Behavior", () => {
let game: Game;
let bot: Player;
let human: Player;
let botBehavior: BotBehavior;
// Helper function for basic test setup
async function setupTestEnvironment() {
const testGame = await setup("big_plains", {
infiniteGold: true,
instantBuild: true,
infiniteTroops: true,
});
// Add players
const botInfo = new PlayerInfo(
"bot_test",
PlayerType.Bot,
null,
"bot_test",
);
const humanInfo = new PlayerInfo(
"human_test",
PlayerType.Human,
null,
"human_test",
);
testGame.addPlayer(botInfo);
testGame.addPlayer(humanInfo);
const testBot = testGame.player("bot_test");
const testHuman = testGame.player("human_test");
// Assign territories
let landTileCount = 0;
testGame.map().forEachTile((tile) => {
if (!testGame.map().isLand(tile)) return;
(landTileCount++ % 2 === 0 ? testBot : testHuman).conquer(tile);
});
// Add troops
testBot.addTroops(5000);
testHuman.addTroops(5000);
// Skip spawn phase
while (testGame.inSpawnPhase()) {
testGame.executeNextTick();
}
const behavior = new BotBehavior(
new PseudoRandom(42),
testGame,
testBot,
0.5,
0.5,
0.2,
);
return { testGame, testBot, testHuman, behavior };
}
// Helper functions for tile assignment
function assignAlternatingLandTiles(
game: Game,
players: Player[],
totalTiles: number,
) {
let assigned = 0;
game.map().forEachTile((tile) => {
if (assigned >= totalTiles) return;
if (!game.map().isLand(tile)) return;
const player = players[assigned % players.length];
player.conquer(tile);
assigned++;
});
}
beforeEach(async () => {
const env = await setupTestEnvironment();
game = env.testGame;
bot = env.testBot;
human = env.testHuman;
botBehavior = env.behavior;
});
test("bot cannot attack allied player", () => {
// Form alliance (bot creates request to human)
const allianceRequest = bot.createAllianceRequest(human);
allianceRequest?.accept();
expect(bot.isAlliedWith(human)).toBe(true);
// Count attacks before attempting attack
const attacksBefore = bot.outgoingAttacks().length;
// Attempt attack (should be blocked)
botBehavior.sendAttack(human);
// Execute a few ticks to process the attacks
for (let i = 0; i < 5; i++) {
game.executeNextTick();
}
expect(bot.isAlliedWith(human)).toBe(true);
expect(human.incomingAttacks()).toHaveLength(0);
// Should be same number of attacks (no new attack created)
expect(bot.outgoingAttacks()).toHaveLength(attacksBefore);
});
test("nation cannot attack allied player", () => {
// Create nation
const nationInfo = new PlayerInfo(
"nation_test",
PlayerType.FakeHuman,
null,
"nation_test",
);
game.addPlayer(nationInfo);
const nation = game.player("nation_test");
// Use helper for tile assignment
assignAlternatingLandTiles(game, [bot, human, nation], 21); // 21 to ensure each gets 7 tiles
nation.addTroops(1000);
const nationBehavior = new BotBehavior(
new PseudoRandom(42),
game,
nation,
0.5,
0.5,
0.2,
);
// Alliance between nation and human
const allianceRequest = nation.createAllianceRequest(human);
allianceRequest?.accept();
expect(nation.isAlliedWith(human)).toBe(true);
const attacksBefore = nation.outgoingAttacks().length;
nation.addTroops(50_000);
// Nation tries to attack ally (should be blocked)
nationBehavior.sendAttack(human);
// Execute a few ticks to process the attacks
for (let i = 0; i < 5; i++) {
game.executeNextTick();
}
expect(nation.isAlliedWith(human)).toBe(true);
expect(nation.outgoingAttacks()).toHaveLength(attacksBefore);
});
});