feat: NPC 세금 처리 로직 및 관련 핸들러 추가, 대형 테스트 맵 수정

This commit is contained in:
2026-01-24 10:06:55 +00:00
parent cb04c930b8
commit 753abdaf47
7 changed files with 185 additions and 40 deletions
+4 -2
View File
@@ -387,9 +387,11 @@ export class GeneralAI {
} }
if (this.general.npcState >= 2 && this.general.officerLevel === 12 && !this.nation?.capitalCityId) { if (this.general.npcState >= 2 && this.general.officerLevel === 12 && !this.nation?.capitalCityId) {
const worldMeta = asRecord(this.world.meta);
const initYear = readNumber(worldMeta.initYear, this.scenarioMeta?.startYear ?? this.startYear);
const initMonth = readNumber(worldMeta.initMonth, 1);
const relYearMonth = const relYearMonth =
joinYearMonth(this.world.currentYear, this.world.currentMonth) - joinYearMonth(this.world.currentYear, this.world.currentMonth) - joinYearMonth(initYear, initMonth);
joinYearMonth(this.scenarioMeta?.startYear ?? this.startYear, 1);
if (relYearMonth > 1) { if (relYearMonth > 1) {
const establish = generalActionHandlers['건국']?.(this); const establish = generalActionHandlers['건국']?.(this);
if (establish) { if (establish) {
@@ -298,7 +298,7 @@ export const do징병 = (ai: GeneralAI) => {
if (!city || !nation || !ai.unitSet || !ai.map) { if (!city || !nation || !ai.unitSet || !ai.map) {
return null; return null;
} }
if ([0, 1].includes(ai.dipState)) { if ([0, 1].includes(ai.dipState) && ai.general.npcState < 2) {
return null; return null;
} }
if (!(ai.genType & t통솔장)) { if (!(ai.genType & t통솔장)) {
+99
View File
@@ -0,0 +1,99 @@
import type { TurnCalendarContext, TurnCalendarHandler, InMemoryTurnWorld } from './inMemoryWorld.js';
import type { City, Nation } from '@sammo-ts/logic';
import { joinYearMonth, readNumber } from './ai/aiUtils.js';
const calcNationDevelopedRate = (cities: City[]): { pop: number; all: number } => {
if (cities.length === 0) {
return { pop: 0, all: 0 };
}
let pop = 0;
let agri = 0;
let comm = 0;
let secu = 0;
let def = 0;
let wall = 0;
for (const city of cities) {
pop += city.populationMax > 0 ? city.population / city.populationMax : 0;
agri += city.agricultureMax > 0 ? city.agriculture / city.agricultureMax : 0;
comm += city.commerceMax > 0 ? city.commerce / city.commerceMax : 0;
secu += city.securityMax > 0 ? city.security / city.securityMax : 0;
def += city.defenceMax > 0 ? city.defence / city.defenceMax : 0;
wall += city.wallMax > 0 ? city.wall / city.wallMax : 0;
}
const count = cities.length;
pop /= count;
agri /= count;
comm /= count;
secu /= count;
def /= count;
wall /= count;
const all = (pop + agri + comm + secu + def + wall) / 6;
return { pop, all };
};
const resolveNpcTaxRate = (cities: City[]): number => {
if (cities.length === 0) {
return 15;
}
const devRate = calcNationDevelopedRate(cities);
const avg = (devRate.pop + devRate.all) / 2;
if (avg > 0.95) {
return 25;
}
if (avg > 0.7) {
return 20;
}
if (avg > 0.5) {
return 15;
}
return 10;
};
const shouldUpdateRate = (month: number): boolean => month === 6 || month === 12;
const isNpcMonarchNation = (nation: Nation, world: InMemoryTurnWorld): boolean => {
if (!nation.capitalCityId || !nation.chiefGeneralId) {
return false;
}
const chief = world.getGeneralById(nation.chiefGeneralId);
return Boolean(chief && chief.npcState >= 2);
};
export const createNpcTaxHandler = (options: { getWorld: () => InMemoryTurnWorld | null }): TurnCalendarHandler => {
return {
onMonthChanged: (context: TurnCalendarContext) => {
if (!shouldUpdateRate(context.currentMonth)) {
return;
}
const world = options.getWorld();
if (!world) {
return;
}
const worldState = world.getState();
const meta = worldState.meta ?? {};
const initYear = readNumber(meta.initYear, NaN);
const initMonth = readNumber(meta.initMonth, NaN);
const hasInit = Number.isFinite(initYear) && Number.isFinite(initMonth);
const monthsSinceStart = hasInit
? joinYearMonth(context.currentYear, context.currentMonth) - joinYearMonth(initYear, initMonth)
: null;
const cities = world.listCities();
for (const nation of world.listNations()) {
if (!isNpcMonarchNation(nation, world)) {
continue;
}
const nationCities = cities.filter((city) => city.nationId === nation.id && city.supplyState > 0);
let rate = resolveNpcTaxRate(nationCities);
if (monthsSinceStart !== null && monthsSinceStart <= 4) {
rate = Math.min(rate, 10);
}
world.updateNation(nation.id, {
meta: {
...nation.meta,
rate,
},
});
}
},
};
};
@@ -351,6 +351,32 @@ const resolveDefinition = (
fallback: GeneralActionDefinition fallback: GeneralActionDefinition
): GeneralActionDefinition => definitions.get(actionKey) ?? fallback; ): GeneralActionDefinition => definitions.get(actionKey) ?? fallback;
const resolveNpcTaxRate = (cities: City[]): number => {
if (cities.length === 0) {
return 15;
}
let pop = 0;
let all = 0;
for (const city of cities) {
const popRatio = city.populationMax > 0 ? city.population / city.populationMax : 0;
pop += popRatio;
all += calcCityDevRatio(city);
}
pop /= cities.length;
all /= cities.length;
const avg = (pop + all) / 2;
if (avg > 0.95) {
return 25;
}
if (avg > 0.7) {
return 20;
}
if (avg > 0.5) {
return 15;
}
return 10;
};
export const createReservedTurnHandler = async (options: { export const createReservedTurnHandler = async (options: {
reservedTurns: InMemoryReservedTurnStore; reservedTurns: InMemoryReservedTurnStore;
scenarioConfig: ScenarioConfig; scenarioConfig: ScenarioConfig;
+33 -33
View File
@@ -7,7 +7,7 @@ export const LARGE_TEST_MAP: MapDefinition = {
{ {
id: 1, id: 1,
name: '소성A', name: '소성A',
level: 4, level: 5,
region: 1, region: 1,
position: { x: 10, y: 10 }, position: { x: 10, y: 10 },
connections: [2, 3], connections: [2, 3],
@@ -20,18 +20,18 @@ export const LARGE_TEST_MAP: MapDefinition = {
wall: 1000, wall: 1000,
}, },
initial: { initial: {
population: 10000, population: 6000,
agriculture: 1000, agriculture: 600,
commerce: 1000, commerce: 600,
security: 1000, security: 600,
defence: 500, defence: 300,
wall: 500, wall: 300,
}, },
}, },
{ {
id: 2, id: 2,
name: '중성B', name: '중성B',
level: 5, level: 6,
region: 1, region: 1,
position: { x: 20, y: 10 }, position: { x: 20, y: 10 },
connections: [1, 4], connections: [1, 4],
@@ -44,18 +44,18 @@ export const LARGE_TEST_MAP: MapDefinition = {
wall: 1100, wall: 1100,
}, },
initial: { initial: {
population: 12000, population: 7200,
agriculture: 1100, agriculture: 660,
commerce: 1100, commerce: 660,
security: 1100, security: 660,
defence: 550, defence: 330,
wall: 550, wall: 330,
}, },
}, },
{ {
id: 3, id: 3,
name: '대성C', name: '대성C',
level: 6, level: 7,
region: 2, region: 2,
position: { x: 10, y: 20 }, position: { x: 10, y: 20 },
connections: [1, 5], connections: [1, 5],
@@ -79,7 +79,7 @@ export const LARGE_TEST_MAP: MapDefinition = {
{ {
id: 4, id: 4,
name: '특성D', name: '특성D',
level: 7, level: 8,
region: 2, region: 2,
position: { x: 30, y: 10 }, position: { x: 30, y: 10 },
connections: [2, 5], connections: [2, 5],
@@ -103,7 +103,7 @@ export const LARGE_TEST_MAP: MapDefinition = {
{ {
id: 5, id: 5,
name: '소성E', name: '소성E',
level: 4, level: 5,
region: 3, region: 3,
position: { x: 20, y: 20 }, position: { x: 20, y: 20 },
connections: [3, 4, 6], connections: [3, 4, 6],
@@ -116,18 +116,18 @@ export const LARGE_TEST_MAP: MapDefinition = {
wall: 1000, wall: 1000,
}, },
initial: { initial: {
population: 10000, population: 6000,
agriculture: 1000, agriculture: 600,
commerce: 1000, commerce: 600,
security: 1000, security: 600,
defence: 500, defence: 300,
wall: 500, wall: 300,
}, },
}, },
{ {
id: 6, id: 6,
name: '중성F', name: '중성F',
level: 5, level: 6,
region: 3, region: 3,
position: { x: 30, y: 20 }, position: { x: 30, y: 20 },
connections: [5, 7], connections: [5, 7],
@@ -140,18 +140,18 @@ export const LARGE_TEST_MAP: MapDefinition = {
wall: 1100, wall: 1100,
}, },
initial: { initial: {
population: 12000, population: 7200,
agriculture: 1100, agriculture: 660,
commerce: 1100, commerce: 660,
security: 1100, security: 660,
defence: 550, defence: 330,
wall: 550, wall: 330,
}, },
}, },
{ {
id: 7, id: 7,
name: '대성G', name: '대성G',
level: 6, level: 7,
region: 4, region: 4,
position: { x: 40, y: 20 }, position: { x: 40, y: 20 },
connections: [6, 8], connections: [6, 8],
@@ -175,7 +175,7 @@ export const LARGE_TEST_MAP: MapDefinition = {
{ {
id: 8, id: 8,
name: '특성H', name: '특성H',
level: 7, level: 8,
region: 4, region: 4,
position: { x: 50, y: 20 }, position: { x: 50, y: 20 },
connections: [7, 9], connections: [7, 9],
@@ -199,7 +199,7 @@ export const LARGE_TEST_MAP: MapDefinition = {
{ {
id: 9, id: 9,
name: '소성I', name: '소성I',
level: 4, level: 5,
region: 5, region: 5,
position: { x: 60, y: 20 }, position: { x: 60, y: 20 },
connections: [8], connections: [8],
@@ -6,6 +6,8 @@ import { InMemoryReservedTurnStore } from '../src/turn/reservedTurnStore.js';
import { createReservedTurnHandler } from '../src/turn/reservedTurnHandler.js'; import { createReservedTurnHandler } from '../src/turn/reservedTurnHandler.js';
import { InMemoryTurnProcessor } from '../src/turn/inMemoryTurnProcessor.js'; import { InMemoryTurnProcessor } from '../src/turn/inMemoryTurnProcessor.js';
import { createIncomeHandler } from '../src/turn/incomeHandler.js'; import { createIncomeHandler } from '../src/turn/incomeHandler.js';
import { createNpcTaxHandler } from '../src/turn/npcTaxHandler.js';
import { composeCalendarHandlers } from '../src/turn/calendarHandlers.js';
import { LARGE_TEST_MAP, buildLargeTestCities } from './fixtures/largeTestMap.js'; import { LARGE_TEST_MAP, buildLargeTestCities } from './fixtures/largeTestMap.js';
const mockDate = new Date('0179-08-01T00:00:00Z'); const mockDate = new Date('0179-08-01T00:00:00Z');
@@ -108,7 +110,7 @@ describe('NPC 대형 시뮬레이션', () => {
generals.length + 1, generals.length + 1,
cityId, cityId,
{ leadership: 75, strength: 10, intelligence: 75 }, { leadership: 75, strength: 10, intelligence: 75 },
3 2
) )
); );
} }
@@ -176,7 +178,7 @@ describe('NPC 대형 시뮬레이션', () => {
currentMonth: 8, currentMonth: 8,
tickSeconds: 600, tickSeconds: 600,
lastTurnTime: mockDate, lastTurnTime: mockDate,
meta: { seed: 1 }, meta: { seed: 1, initYear: 179, initMonth: 8 },
}; };
const schedule: TurnSchedule = { const schedule: TurnSchedule = {
@@ -207,10 +209,16 @@ describe('NPC 대형 시뮬레이션', () => {
nationTraits: new Map(), nationTraits: new Map(),
}); });
const npcTaxHandler = createNpcTaxHandler({
getWorld: () => wrapper.world,
});
const calendarHandler = composeCalendarHandlers(incomeHandler, npcTaxHandler);
const world = new InMemoryTurnWorld(state, snapshot, { const world = new InMemoryTurnWorld(state, snapshot, {
schedule, schedule,
generalTurnHandler: handler, generalTurnHandler: handler,
calendarHandler: incomeHandler, calendarHandler,
}); });
wrapper.world = world; wrapper.world = world;
@@ -266,7 +274,7 @@ describe('NPC 대형 시뮬레이션', () => {
const generals = world.listGenerals(); const generals = world.listGenerals();
for (const nation of nations) { for (const nation of nations) {
const nationGenerals = generals.filter((general) => general.nationId === nation.id); const nationGenerals = generals.filter((general) => general.nationId === nation.id);
expect(nationGenerals.length).toBe(targetCount); expect(nationGenerals.length).toBeGreaterThanOrEqual(targetCount);
} }
}; };
@@ -61,6 +61,15 @@ export class ActionDefinition<
nationName = '㉥' + nationName; nationName = '㉥' + nationName;
} }
const npcNationPolicy =
general.npcState >= 2
? {
values: {
minNPCRecruitCityPopulation: 0,
},
}
: undefined;
const newNation: Nation = { const newNation: Nation = {
id: newNationId, id: newNationId,
name: nationName, name: nationName,
@@ -79,6 +88,7 @@ export class ActionDefinition<
surlimit: 72, surlimit: 72,
secretlimit: 3, secretlimit: 3,
gennum: 1, gennum: 1,
...(npcNationPolicy ? { npc_nation_policy: npcNationPolicy } : {}),
}, },
}; };