Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 89 additions & 16 deletions src/combat/AI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,26 @@ const HARASS_INTERVAL_MS: Record<AIDifficulty, number> = {
}

const DEFENSE_TARGET: Record<AIDifficulty, number> = {
easy: 1,
medium: 3,
easy: 2,
medium: 5,
hard: 6,
smart_hard: 8, // heavier base defenses but spread (Rule-of-3 enforced by Rhizome)
}

// AA turret target count per difficulty (patriot_missile / flak_cannon)
const AA_DEFENSE_TARGET: Record<AIDifficulty, number> = {
easy: 0,
medium: 2,
hard: 3,
smart_hard: 4, // heavier base defenses but spread (Rule-of-3 enforced by Rhizome)
smart_hard: 4,
}

// Credit threshold above which AI will accelerate tech building construction
const TECH_CREDIT_THRESHOLD: Record<AIDifficulty, number> = {
easy: 3000,
medium: 2000,
hard: 1500,
smart_hard: 1200,
}

const MAX_ARMY: Record<AIDifficulty, number> = {
Expand Down Expand Up @@ -837,6 +853,13 @@ export class AI {
if (!this.hasBuildingPlacedOrConstructing('barracks') && this.tryBuildBuilding('barracks')) return
if (!this.hasBuildingPlacedOrConstructing('war_factory') && this.tryBuildBuilding('war_factory')) return
break
case 'tech': {
// Tech advancement: push toward mid-tech or battle_lab
const midTechId = this.side === 'alliance' ? 'air_force_command' : 'radar_tower'
if (!this.hasBuildingPlacedOrConstructing(midTechId) && this.tryBuildBuilding(midTechId)) return
if (!this.hasBuildingPlacedOrConstructing('battle_lab') && this.tryBuildBuilding('battle_lab')) return
break
}
case 'expansion':
// Fall through to normal tech tree for expansion decisions
break
Expand Down Expand Up @@ -927,11 +950,14 @@ export class AI {
return
}

// 11. Mid-game tech building
// 11. Mid-game tech building — push aggressively; hard/smart_hard build as soon as war_factory is up
const midTechId = this.side === 'alliance' ? 'air_force_command' : 'radar_tower'
if (!this.hasBuildingPlacedOrConstructing(midTechId)) {
const tanksNeeded = this.difficulty === 'easy' ? 3 : 1
if (this.countUnitsByCategory('vehicle') >= tanksNeeded) {
// easy: wait for 3 vehicles; medium: 1 vehicle; hard/smart_hard: no vehicle gate (credit-driven)
const tanksNeeded = this.difficulty === 'easy' ? 3 : this.difficulty === 'medium' ? 1 : 0
const credits = this.economy.getCredits(this.playerId)
const canTechRush = this.difficulty !== 'easy' && credits >= TECH_CREDIT_THRESHOLD[this.difficulty]
if (canTechRush || this.countUnitsByCategory('vehicle') >= tanksNeeded) {
this.tryBuildBuilding(midTechId)
}
return
Expand All @@ -943,9 +969,12 @@ export class AI {
return
}

// 13. Battle Lab
// 13. Battle Lab — all difficulties push toward it; easy waits for smaller army
if (!this.hasBuildingPlacedOrConstructing('battle_lab')) {
if (this.difficulty !== 'easy' || this.getCombatUnits().length >= 12) {
const armyGate = this.difficulty === 'easy' ? 6 : 0 // easy needs small army first; others build immediately
const credits = this.economy.getCredits(this.playerId)
const creditGate = this.difficulty === 'easy' ? 0 : TECH_CREDIT_THRESHOLD[this.difficulty]
if (this.getCombatUnits().length >= armyGate || credits >= creditGate) {
this.tryBuildBuilding('battle_lab')
}
return
Expand All @@ -957,8 +986,8 @@ export class AI {
this.tryBuildBuilding(this.side === 'alliance' ? 'air_force_command' : 'radar_tower', true)
}

// 10. Medium/Hard: build superweapons aggressively once battle lab is up
if (this.difficulty !== 'easy' && this.hasActiveBuilding('battle_lab')) {
// 14b. All difficulties: build superweapons once battle lab is up
if (this.hasActiveBuilding('battle_lab')) {
for (const swId of AI.SW_IDS) {
const swDef = BUILDING_DEFS[swId]
if (!swDef) continue
Expand Down Expand Up @@ -998,24 +1027,47 @@ export class AI {
private buildDefenses(): void {
if (!this.hasActiveBuilding('war_factory')) return

const target = DEFENSE_TARGET[this.difficulty] + (this.enemyIsRushing ? 1 : 0)
// Scale total defense target with game phase: base + 1 for mid, +2 for late
const phaseBonus = this.phase === 'late' ? 2 : this.phase === 'mid' ? 1 : 0
const rushBonus = this.enemyIsRushing ? 1 : 0
const target = DEFENSE_TARGET[this.difficulty] + phaseBonus + rushBonus

// Early defenses (cheap, available after barracks)
// Tier 1: cheap early defenses (available after barracks)
const earlyDefId = this.side === 'alliance' ? 'pillbox' : 'sentry_gun'
const earlyCount = this.countBuildings(earlyDefId)
// Build up to min(2, target) early defenses first as quick perimeter
if (earlyCount < Math.min(2, target)) {
this.tryBuildBuilding(earlyDefId, true)
return
}

// Late defenses (require mid-tech building)
// Mid-tech required for advanced defenses
const midTechId = this.side === 'alliance' ? 'air_force_command' : 'radar_tower'
if (!this.hasActiveBuilding(midTechId)) return

// Tier 2: powerful late defenses (prism_tower / tesla_coil)
const lateDefId = this.side === 'alliance' ? 'prism_tower' : 'tesla_coil'
const lateCount = this.countBuildings(lateDefId)
if (lateCount + earlyCount < target) {
this.tryBuildBuilding(lateDefId, true)
return
}

// Tier 3: AA defenses — protect base from air threats
const aaTarget = AA_DEFENSE_TARGET[this.difficulty]
if (aaTarget > 0) {
const aaDefId = this.side === 'alliance' ? 'patriot_missile' : 'flak_cannon'
const aaCount = this.countBuildings(aaDefId)
if (aaCount < aaTarget) {
this.tryBuildBuilding(aaDefId, true)
return
}
}

// Tier 4: extra early-tier coverage once all advanced defenses are placed
// Pad with more pillboxes/sentry guns for outer perimeter in mid/late game
if (this.phase !== 'early' && earlyCount < target) {
this.tryBuildBuilding(earlyDefId, true)
}
}

Expand Down Expand Up @@ -1199,11 +1251,32 @@ export class AI {
if (this.canProduceUnit('v3_launcher')) pool.push('v3_launcher')
}

// ── Late-tier heavy units ──
// ── Mid-tier boost: once mid-tech is up, shift weight toward advanced units ──
const hasMidTech = this.side === 'alliance'
? this.hasActiveBuilding('air_force_command')
: this.hasActiveBuilding('radar_tower')
if (hasMidTech && this.phase !== 'early') {
// Reduce basic infantry weight (only 1 slot instead of 2) — tanks and specials preferred
if (pool.length >= 2 && pool[0] === basicInf && pool[1] === basicInf) {
pool.splice(1, 1) // remove one basic infantry slot
}
// Extra tank weight in mid-phase
if (this.canProduceUnit(mainTank)) pool.push(mainTank)
}

// ── Late-tier heavy units — strongly prefer apex predators when battle_lab is up ──
if (this.phase === 'late') {
if (this.side === 'collective') {
if (this.canProduceUnit('apocalypse_tank')) pool.push('apocalypse_tank', 'apocalypse_tank')
if (this.canProduceUnit('kirov')) pool.push('kirov')
if (this.canProduceUnit('apocalypse_tank')) pool.push('apocalypse_tank', 'apocalypse_tank', 'apocalypse_tank')
if (this.canProduceUnit('kirov')) pool.push('kirov', 'kirov')
} else {
// Alliance: boost black_eagle and prism_tank in late phase
if (this.canProduceUnit('black_eagle')) pool.push('black_eagle', 'black_eagle')
if (this.canProduceUnit('prism_tank')) pool.push('prism_tank', 'prism_tank')
}
// In late phase, further reduce basic infantry — invest in apex units
if (pool.length >= 2 && pool[0] === basicInf && pool[1] === basicInf) {
pool.splice(0, 2) // remove both basic infantry slots; late game = high-tier army
}
}

Expand Down
41 changes: 35 additions & 6 deletions src/combat/Rhizome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ function mockEntityManager(
}

/** Creates a minimal mock Economy */
function mockEconomy() {
return {} as unknown as import('../economy/Economy').Economy
function mockEconomy(credits = 0) {
return { getCredits: () => credits } as unknown as import('../economy/Economy').Economy
}

/** Creates a minimal GameState with a fog-of-war map */
Expand Down Expand Up @@ -195,11 +195,12 @@ describe('RHIZOME_PARAMS', () => {
expect(RHIZOME_PARAMS.PRESSURE_ENEMY_RATIO).toBe(1.5)
})

it('organ priority order is power→refinery→production→expansion', () => {
it('organ priority order is power→refinery→production→tech→expansion', () => {
expect(RHIZOME_PARAMS.ORGAN_PRIORITY[0]).toBe('power')
expect(RHIZOME_PARAMS.ORGAN_PRIORITY[1]).toBe('refinery')
expect(RHIZOME_PARAMS.ORGAN_PRIORITY[2]).toBe('production')
expect(RHIZOME_PARAMS.ORGAN_PRIORITY[3]).toBe('expansion')
expect(RHIZOME_PARAMS.ORGAN_PRIORITY[3]).toBe('tech')
expect(RHIZOME_PARAMS.ORGAN_PRIORITY[4]).toBe('expansion')
})
})

Expand Down Expand Up @@ -245,7 +246,7 @@ describe('Rhizome.getOrganPriority', () => {
expect(r.getOrganPriority(mockGameState())).toBe('production')
})

it('returns "expansion" when all organ targets are met', () => {
it('returns "tech" when production targets are met, no tech buildings, and credits allow', () => {
const buildings = [
mockBuilding({ defId: 'power_plant', category: 'power', providespower: 200, state: 'active' }),
mockBuilding({ id: 'b2', defId: 'ore_refinery', category: 'production', state: 'active' }),
Expand All @@ -254,7 +255,35 @@ describe('Rhizome.getOrganPriority', () => {
mockBuilding({ id: 'b5', defId: 'war_factory', category: 'production', state: 'active' }),
]
const em = mockEntityManager(buildings, [])
const r = new Rhizome(1, em, mockEconomy())
const r = new Rhizome(1, em, mockEconomy(RHIZOME_PARAMS.TECH_CREDIT_MIN))
expect(r.getOrganPriority(mockGameState())).toBe('tech')
})

it('returns "expansion" when all organ targets are met and tech building exists', () => {
const buildings = [
mockBuilding({ defId: 'power_plant', category: 'power', providespower: 200, state: 'active' }),
mockBuilding({ id: 'b2', defId: 'ore_refinery', category: 'production', state: 'active' }),
mockBuilding({ id: 'b3', defId: 'ore_refinery', category: 'production', state: 'active' }),
mockBuilding({ id: 'b4', defId: 'barracks', category: 'production', state: 'active' }),
mockBuilding({ id: 'b5', defId: 'war_factory', category: 'production', state: 'active' }),
mockBuilding({ id: 'b6', defId: 'air_force_command', category: 'tech', state: 'active' }),
]
const em = mockEntityManager(buildings, [])
const r = new Rhizome(1, em, mockEconomy(5000))
expect(r.getOrganPriority(mockGameState())).toBe('expansion')
})

it('returns "expansion" when all organ targets are met and credits below tech threshold', () => {
const buildings = [
mockBuilding({ defId: 'power_plant', category: 'power', providespower: 200, state: 'active' }),
mockBuilding({ id: 'b2', defId: 'ore_refinery', category: 'production', state: 'active' }),
mockBuilding({ id: 'b3', defId: 'ore_refinery', category: 'production', state: 'active' }),
mockBuilding({ id: 'b4', defId: 'barracks', category: 'production', state: 'active' }),
mockBuilding({ id: 'b5', defId: 'war_factory', category: 'production', state: 'active' }),
]
const em = mockEntityManager(buildings, [])
// 0 credits — below TECH_CREDIT_MIN — so tech is not triggered
const r = new Rhizome(1, em, mockEconomy(0))
expect(r.getOrganPriority(mockGameState())).toBe('expansion')
})
})
Expand Down
21 changes: 18 additions & 3 deletions src/combat/Rhizome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const RHIZOME_PARAMS = {

// ── Metabolic Loop ─────────────────────────────────────────────────────────

/** Ordered build priority: power → refinery → production → expansion */
ORGAN_PRIORITY: ['power', 'refinery', 'production', 'expansion'] as const,
/** Ordered build priority: power → refinery → production → tech → expansion */
ORGAN_PRIORITY: ['power', 'refinery', 'production', 'tech', 'expansion'] as const,

/**
* Rule-of-3: minimum tile distance between buildings of the same def.
Expand All @@ -41,6 +41,12 @@ export const RHIZOME_PARAMS = {
/** Target count of production buildings before switching to expansion. */
PRODUCTION_COUNT_TARGET: 2,

/**
* Minimum credits before Rhizome will allocate to tech building construction.
* Prevents starving income by building expensive tech too early.
*/
TECH_CREDIT_MIN: 1200,

// ── Cellular Unit Logic (NC Density) ───────────────────────────────────────

/** How often (ms) to re-evaluate NC density states for all units. */
Expand Down Expand Up @@ -248,7 +254,16 @@ export class Rhizome {
return 'production'
}

// 4. Expand tech/economy/territory
// 4. Tech advancement: if no tech buildings exist and economy allows, push toward tech ceiling
const techBuildings = active.filter(b => b.def.category === 'tech')
if (techBuildings.length === 0) {
const credits = this.economy.getCredits(this.playerId)
if (credits >= RHIZOME_PARAMS.TECH_CREDIT_MIN) {
return 'tech'
}
}

// 5. Expand tech/economy/territory
return 'expansion'
}

Expand Down