feat: NPC 세금 처리 로직 및 관련 핸들러 추가, 대형 테스트 맵 수정
This commit is contained in:
@@ -387,9 +387,11 @@ export class GeneralAI {
|
||||
}
|
||||
|
||||
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 =
|
||||
joinYearMonth(this.world.currentYear, this.world.currentMonth) -
|
||||
joinYearMonth(this.scenarioMeta?.startYear ?? this.startYear, 1);
|
||||
joinYearMonth(this.world.currentYear, this.world.currentMonth) - joinYearMonth(initYear, initMonth);
|
||||
if (relYearMonth > 1) {
|
||||
const establish = generalActionHandlers['건국']?.(this);
|
||||
if (establish) {
|
||||
|
||||
@@ -298,7 +298,7 @@ export const do징병 = (ai: GeneralAI) => {
|
||||
if (!city || !nation || !ai.unitSet || !ai.map) {
|
||||
return null;
|
||||
}
|
||||
if ([0, 1].includes(ai.dipState)) {
|
||||
if ([0, 1].includes(ai.dipState) && ai.general.npcState < 2) {
|
||||
return null;
|
||||
}
|
||||
if (!(ai.genType & t통솔장)) {
|
||||
|
||||
@@ -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
|
||||
): 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: {
|
||||
reservedTurns: InMemoryReservedTurnStore;
|
||||
scenarioConfig: ScenarioConfig;
|
||||
|
||||
+33
-33
@@ -7,7 +7,7 @@ export const LARGE_TEST_MAP: MapDefinition = {
|
||||
{
|
||||
id: 1,
|
||||
name: '소성A',
|
||||
level: 4,
|
||||
level: 5,
|
||||
region: 1,
|
||||
position: { x: 10, y: 10 },
|
||||
connections: [2, 3],
|
||||
@@ -20,18 +20,18 @@ export const LARGE_TEST_MAP: MapDefinition = {
|
||||
wall: 1000,
|
||||
},
|
||||
initial: {
|
||||
population: 10000,
|
||||
agriculture: 1000,
|
||||
commerce: 1000,
|
||||
security: 1000,
|
||||
defence: 500,
|
||||
wall: 500,
|
||||
population: 6000,
|
||||
agriculture: 600,
|
||||
commerce: 600,
|
||||
security: 600,
|
||||
defence: 300,
|
||||
wall: 300,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: '중성B',
|
||||
level: 5,
|
||||
level: 6,
|
||||
region: 1,
|
||||
position: { x: 20, y: 10 },
|
||||
connections: [1, 4],
|
||||
@@ -44,18 +44,18 @@ export const LARGE_TEST_MAP: MapDefinition = {
|
||||
wall: 1100,
|
||||
},
|
||||
initial: {
|
||||
population: 12000,
|
||||
agriculture: 1100,
|
||||
commerce: 1100,
|
||||
security: 1100,
|
||||
defence: 550,
|
||||
wall: 550,
|
||||
population: 7200,
|
||||
agriculture: 660,
|
||||
commerce: 660,
|
||||
security: 660,
|
||||
defence: 330,
|
||||
wall: 330,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: '대성C',
|
||||
level: 6,
|
||||
level: 7,
|
||||
region: 2,
|
||||
position: { x: 10, y: 20 },
|
||||
connections: [1, 5],
|
||||
@@ -79,7 +79,7 @@ export const LARGE_TEST_MAP: MapDefinition = {
|
||||
{
|
||||
id: 4,
|
||||
name: '특성D',
|
||||
level: 7,
|
||||
level: 8,
|
||||
region: 2,
|
||||
position: { x: 30, y: 10 },
|
||||
connections: [2, 5],
|
||||
@@ -103,7 +103,7 @@ export const LARGE_TEST_MAP: MapDefinition = {
|
||||
{
|
||||
id: 5,
|
||||
name: '소성E',
|
||||
level: 4,
|
||||
level: 5,
|
||||
region: 3,
|
||||
position: { x: 20, y: 20 },
|
||||
connections: [3, 4, 6],
|
||||
@@ -116,18 +116,18 @@ export const LARGE_TEST_MAP: MapDefinition = {
|
||||
wall: 1000,
|
||||
},
|
||||
initial: {
|
||||
population: 10000,
|
||||
agriculture: 1000,
|
||||
commerce: 1000,
|
||||
security: 1000,
|
||||
defence: 500,
|
||||
wall: 500,
|
||||
population: 6000,
|
||||
agriculture: 600,
|
||||
commerce: 600,
|
||||
security: 600,
|
||||
defence: 300,
|
||||
wall: 300,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
name: '중성F',
|
||||
level: 5,
|
||||
level: 6,
|
||||
region: 3,
|
||||
position: { x: 30, y: 20 },
|
||||
connections: [5, 7],
|
||||
@@ -140,18 +140,18 @@ export const LARGE_TEST_MAP: MapDefinition = {
|
||||
wall: 1100,
|
||||
},
|
||||
initial: {
|
||||
population: 12000,
|
||||
agriculture: 1100,
|
||||
commerce: 1100,
|
||||
security: 1100,
|
||||
defence: 550,
|
||||
wall: 550,
|
||||
population: 7200,
|
||||
agriculture: 660,
|
||||
commerce: 660,
|
||||
security: 660,
|
||||
defence: 330,
|
||||
wall: 330,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 7,
|
||||
name: '대성G',
|
||||
level: 6,
|
||||
level: 7,
|
||||
region: 4,
|
||||
position: { x: 40, y: 20 },
|
||||
connections: [6, 8],
|
||||
@@ -175,7 +175,7 @@ export const LARGE_TEST_MAP: MapDefinition = {
|
||||
{
|
||||
id: 8,
|
||||
name: '특성H',
|
||||
level: 7,
|
||||
level: 8,
|
||||
region: 4,
|
||||
position: { x: 50, y: 20 },
|
||||
connections: [7, 9],
|
||||
@@ -199,7 +199,7 @@ export const LARGE_TEST_MAP: MapDefinition = {
|
||||
{
|
||||
id: 9,
|
||||
name: '소성I',
|
||||
level: 4,
|
||||
level: 5,
|
||||
region: 5,
|
||||
position: { x: 60, y: 20 },
|
||||
connections: [8],
|
||||
|
||||
@@ -6,6 +6,8 @@ import { InMemoryReservedTurnStore } from '../src/turn/reservedTurnStore.js';
|
||||
import { createReservedTurnHandler } from '../src/turn/reservedTurnHandler.js';
|
||||
import { InMemoryTurnProcessor } from '../src/turn/inMemoryTurnProcessor.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';
|
||||
|
||||
const mockDate = new Date('0179-08-01T00:00:00Z');
|
||||
@@ -108,7 +110,7 @@ describe('NPC 대형 시뮬레이션', () => {
|
||||
generals.length + 1,
|
||||
cityId,
|
||||
{ leadership: 75, strength: 10, intelligence: 75 },
|
||||
3
|
||||
2
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -176,7 +178,7 @@ describe('NPC 대형 시뮬레이션', () => {
|
||||
currentMonth: 8,
|
||||
tickSeconds: 600,
|
||||
lastTurnTime: mockDate,
|
||||
meta: { seed: 1 },
|
||||
meta: { seed: 1, initYear: 179, initMonth: 8 },
|
||||
};
|
||||
|
||||
const schedule: TurnSchedule = {
|
||||
@@ -207,10 +209,16 @@ describe('NPC 대형 시뮬레이션', () => {
|
||||
nationTraits: new Map(),
|
||||
});
|
||||
|
||||
const npcTaxHandler = createNpcTaxHandler({
|
||||
getWorld: () => wrapper.world,
|
||||
});
|
||||
|
||||
const calendarHandler = composeCalendarHandlers(incomeHandler, npcTaxHandler);
|
||||
|
||||
const world = new InMemoryTurnWorld(state, snapshot, {
|
||||
schedule,
|
||||
generalTurnHandler: handler,
|
||||
calendarHandler: incomeHandler,
|
||||
calendarHandler,
|
||||
});
|
||||
wrapper.world = world;
|
||||
|
||||
@@ -266,7 +274,7 @@ describe('NPC 대형 시뮬레이션', () => {
|
||||
const generals = world.listGenerals();
|
||||
for (const nation of nations) {
|
||||
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;
|
||||
}
|
||||
|
||||
const npcNationPolicy =
|
||||
general.npcState >= 2
|
||||
? {
|
||||
values: {
|
||||
minNPCRecruitCityPopulation: 0,
|
||||
},
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const newNation: Nation = {
|
||||
id: newNationId,
|
||||
name: nationName,
|
||||
@@ -79,6 +88,7 @@ export class ActionDefinition<
|
||||
surlimit: 72,
|
||||
secretlimit: 3,
|
||||
gennum: 1,
|
||||
...(npcNationPolicy ? { npc_nation_policy: npcNationPolicy } : {}),
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user