Respect allied pirate supply during pirate events

This commit is contained in:
coyotte508 2026-10-13 05:03:17 +02:00
commit 4d8939c5dd
5 changed files with 51 additions and 5 deletions

View file

@ -130,13 +130,19 @@ fn remove_pirate_ships(builder: IncidentBuilder) -> IncidentBuilder {
}
let pirates = get_pirates_player(game);
let pirate_ships = pirates
let pirate_ships: Vec<_> = pirates
.units
.iter()
.filter(|u| u.is_ship())
.map(|u| u.id)
.collect();
let needs_removal = 2_u8.saturating_sub(pirates.available_units().get(&UnitType::Ship));
// Allied ships are reserved in held_units and cannot be recycled.
let needs_removal = 2_u8
.saturating_sub(pirates.available_units().get(&UnitType::Ship))
.min(pirate_ships.len() as u8);
if needs_removal == 0 {
return None;
}
Some(UnitsRequest::new(
pirates.index,
@ -169,6 +175,12 @@ fn place_pirate_ship(builder: IncidentBuilder, priority: i32, blockade: bool) ->
IncidentTarget::ActivePlayer,
priority,
move |game, p, _i| {
if get_pirates_player(game).available_units().get(&UnitType::Ship) == 0 {
if blockade {
p.log(game, "Pirate event has no effect: Carthage controls all four Pirate Ships");
}
return None;
}
let sea_spaces = pirate_spawn_positions(game, p.index, blockade);
if sea_spaces.is_empty() && blockade {

View file

@ -331,3 +331,5 @@ Viewer 0.1.105 / engine 0.4.80 allows reclaiming influenced buildings from anoth
Viewer 0.1.106 simplifies cultural influence confirmation: editable source/target fields, payment choices with combined costs, no duplicate action and empty-range rows, and a pinned roll summary/button. The influence panel has room for its choices on mobile while longer content scrolls independently. Desktop/mobile map selection and confirmation checks pass, including visible roll-button bounds. Engine remains 0.4.80.
Viewer 0.1.107 simplifies move/attack previews: coordinates appear once in the route, payment choices are labelled and selected visibly, defenders have a compact heading, and terrain details expand on demand. The final move/attack button includes the selected cost and stays outside the scrolling content. Desktop/mobile Roads attack checks verify payment selection does not submit prematurely and confirmation stays visible. Engine remains 0.4.80.
Viewer 0.1.108 / engine 0.4.81 fixes pirate events when Carthage controls pirate ships. Removal only recycles neutral ships; placement checks remaining supply before each ship. All four allied ships suppress placement, and three allies permit only one new pirate. Tests cover zero to four allied ships with both empty and full remaining neutral supply, including supply released by Hegemony.

View file

@ -1,12 +1,12 @@
{
"name": "@boardgamers/clash-web-client",
"version": "0.1.107",
"version": "0.1.108",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@boardgamers/clash-web-client",
"version": "0.1.107",
"version": "0.1.108",
"dependencies": {
"@boardgamers/protocol": "0.9.0",
"lucide-svelte": "^0.577.0",

View file

@ -1,6 +1,6 @@
{
"name": "@boardgamers/clash-web-client",
"version": "0.1.107",
"version": "0.1.108",
"private": true,
"type": "module",
"scripts": {

View file

@ -0,0 +1,32 @@
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {createRequire} from 'node:module';
const engine=createRequire(import.meta.url)('../.engine/server.js');
const npcs=JSON.parse(await engine.init(2,[],{},'pirate-supply',{})).players.slice(2);
for (const held of [0,1,2,3,4]) for (const neutral of new Set([0,4-held])) test(`Pirate event respects ${held} allied ships with ${neutral} neutral ships`,()=>{
const g=JSON.parse(readFileSync(new URL('../../server/tests/test_games/incidents/pirates_spawn.json',import.meta.url),'utf8'));
for(const npc of npcs) if(!g.players.some((p:any)=>p.civilization===npc.civilization)) g.players.push({...npc,id:g.players.length});
const p=g.players[0], pirates=g.players.find((p:any)=>p.civilization==='Pirates');
p.civilization='Carthage';p.advances.push('Fishing','Navigation');
p.units.push({id:30,unit_type:'Settler',position:'D1'});
for(let i=0;i<held;i++)p.units.push({id:40+i,unit_type:'Ship',position:'D1',pirate:true});
p.next_unit_id=50;
pirates.units=Array.from({length:neutral},(_,i)=>({id:i,unit_type:'Ship',position:'D2'}));
pirates.held_units={ships:held};pirates.next_unit_id=20;
let state=engine.tryMove(JSON.stringify(g),JSON.stringify({Response:{SelectAdvance:'Storage'}}),0);
for(let i=0;i<8;i++) {
const current=JSON.parse(state), npc=current.players.find((p:any)=>p.civilization==='Pirates');
assert.ok((npc.units??[]).length+(npc.held_units?.ships??0)<=4,'never exceed the physical supply');
const req=current.events?.at(-1)?.handler?.request;
if(req?.SelectUnits) {
const amount=Math.min(2,neutral);
state=engine.tryMove(state,JSON.stringify({Response:{SelectUnits:req.SelectUnits.choices.slice(0,amount)}}),0);
} else if(req?.SelectPositions && current.events.at(-1).handler.request.SelectPositions.description?.includes('Pirate')) {
state=engine.tryMove(state,JSON.stringify({Response:{SelectPositions:[req.SelectPositions.choices[0]]}}),0);
} else break;
}
const final=JSON.parse(state), npc=final.players.find((p:any)=>p.civilization==='Pirates');
assert.equal((npc.units??[]).filter((u:any)=>u.id>=20).length,Math.min(2,4-held));
assert.equal(final.players[0].units.filter((u:any)=>u.pirate).length,held,'allies are untouched');
});