Files
OpenFrontIO/tests/NationAllianceBehavior.test.ts
T
FloPinguin 86d1ac6c62 Nations now counter warship infestations 🚢 (#2658)
## Description:

Relevant for singleplayer and HumansVsNations: 
Humans sometimes try to flood the entire ocean with warships. The goal
is to dominate the trade and to block transport ships.

The already existing `trackTransportShipsAndRetaliate` and
`trackTradeShipsAndRetaliate` methods can't stop these large scale
infestations, the nations are completely helpless.

The new `counterWarshipInfestation` method checks if a nation is one of
the top 3 richest players (Enough money for warships) and if any enemy
(or enemy team) has accumulated more than 10 (for teams total 15)
warships, then builds a counter-warship targeting that threat.

This feature only activates on Hard or Impossible difficulty.

Thats how it can look, nations send out a warship every couple of
seconds, until the infestation threat is gone:

<img width="779" height="670" alt="Screenshot 2025-12-20 160600"
src="https://github.com/user-attachments/assets/25040077-e7db-4720-aea4-7c230afe05ea"
/>

## 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-24 10:07:44 -08:00

176 lines
4.7 KiB
TypeScript

import { NationAllianceBehavior } from "../src/core/execution/nation/NationAllianceBehavior";
import {
AllianceRequest,
Game,
Player,
PlayerInfo,
PlayerType,
Tick,
} from "../src/core/game/Game";
import { PseudoRandom } from "../src/core/PseudoRandom";
import { setup } from "./util/Setup";
let game: Game;
let player: Player;
let requestor: Player;
let allianceBehavior: NationAllianceBehavior;
describe("AllianceBehavior.handleAllianceRequests", () => {
beforeEach(async () => {
game = await setup("big_plains", {
infiniteGold: true,
instantBuild: true,
});
const playerInfo = new PlayerInfo(
"player_id",
PlayerType.Bot,
null,
"player_id",
);
const requestorInfo = new PlayerInfo(
"requestor_id",
PlayerType.Human,
null,
"requestor_id",
);
game.addPlayer(playerInfo);
game.addPlayer(requestorInfo);
player = game.player("player_id");
requestor = game.player("requestor_id");
// Use a fixed random seed for deterministic behavior
const random = new PseudoRandom(46);
allianceBehavior = new NationAllianceBehavior(random, game, player);
});
function setupAllianceRequest({
isTraitor = false,
relationDelta = 2,
numTilesPlayer = 10,
numTilesRequestor = 10,
alliancesCount = 0,
} = {}) {
if (isTraitor) requestor.markTraitor();
player.updateRelation(requestor, relationDelta);
requestor.updateRelation(player, relationDelta);
game.map().forEachTile((tile) => {
if (game.map().isLand(tile)) {
if (numTilesPlayer > 0) {
player.conquer(tile);
numTilesPlayer--;
} else if (numTilesRequestor > 0) {
requestor.conquer(tile);
numTilesRequestor--;
}
}
});
jest.spyOn(player, "alliances").mockReturnValue(new Array(alliancesCount));
const mockRequest = {
requestor: () => requestor,
recipient: () => player,
createdAt: () => 0 as unknown as Tick,
accept: jest.fn(),
reject: jest.fn(),
} as unknown as AllianceRequest;
jest
.spyOn(player, "incomingAllianceRequests")
.mockReturnValue([mockRequest]);
return mockRequest;
}
test("should accept alliance when all conditions are met", () => {
const request = setupAllianceRequest({});
allianceBehavior.handleAllianceRequests();
expect(request.accept).toHaveBeenCalled();
expect(request.reject).not.toHaveBeenCalled();
});
test("should reject alliance if requestor is a traitor", () => {
const request = setupAllianceRequest({ isTraitor: true });
allianceBehavior.handleAllianceRequests();
expect(request.accept).not.toHaveBeenCalled();
expect(request.reject).toHaveBeenCalled();
});
test("should reject alliance if relation is hostile", () => {
const request = setupAllianceRequest({ relationDelta: -2 });
allianceBehavior.handleAllianceRequests();
expect(request.accept).not.toHaveBeenCalled();
expect(request.reject).toHaveBeenCalled();
});
test("should accept alliance if requestor is much larger (> 3 times size of recipient)", () => {
const request = setupAllianceRequest({
numTilesRequestor: 40,
});
allianceBehavior.handleAllianceRequests();
expect(request.accept).toHaveBeenCalled();
expect(request.reject).not.toHaveBeenCalled();
});
test("should reject alliance if player has too many alliances", () => {
const request = setupAllianceRequest({ alliancesCount: 10 });
allianceBehavior.handleAllianceRequests();
expect(request.accept).not.toHaveBeenCalled();
expect(request.reject).toHaveBeenCalled();
});
});
describe("AllianceBehavior.handleAllianceExtensionRequests", () => {
let mockGame: any;
let mockPlayer: any;
let mockAlliance: any;
let mockHuman: any;
let mockRandom: any;
let allianceBehavior: NationAllianceBehavior;
beforeEach(() => {
mockGame = { addExecution: jest.fn() };
mockHuman = { id: jest.fn(() => "human_id") };
mockAlliance = {
onlyOneAgreedToExtend: jest.fn(() => true),
other: jest.fn(() => mockHuman),
};
mockRandom = { chance: jest.fn() };
mockPlayer = {
alliances: jest.fn(() => [mockAlliance]),
relation: jest.fn(),
id: jest.fn(() => "bot_id"),
type: jest.fn(() => PlayerType.Nation),
};
allianceBehavior = new NationAllianceBehavior(
mockRandom,
mockGame,
mockPlayer,
);
});
it("should NOT request extension if onlyOneAgreedToExtend is false (no expiration yet or both already agreed)", () => {
mockAlliance.onlyOneAgreedToExtend.mockReturnValue(false);
allianceBehavior.handleAllianceExtensionRequests();
expect(mockGame.addExecution).not.toHaveBeenCalled();
});
});