have buildings take time to construct

This commit is contained in:
Evan
2025-02-08 13:53:58 -08:00
parent 0487509c03
commit 1594a45dac
13 changed files with 216 additions and 82 deletions
+122
View File
@@ -0,0 +1,122 @@
import { consolex } from "../Consolex";
import {
Cell,
Execution,
Game,
Player,
Unit,
PlayerID,
UnitType,
Tick,
} from "../game/Game";
import { TileRef } from "../game/GameMap";
import { CityExecution } from "./CityExecution";
import { DefensePostExecution } from "./DefensePostExecution";
import { MirvExecution } from "./MIRVExecution";
import { MissileSiloExecution } from "./MissileSiloExecution";
import { NukeExecution } from "./NukeExecution";
import { PortExecution } from "./PortExecution";
import { WarshipExecution } from "./WarshipExecution";
export class ConstructionExecution implements Execution {
private player: Player;
private construction: Unit;
private active: boolean = true;
private mg: Game;
private ticksUntilComplete: Tick;
constructor(
private ownerId: PlayerID,
private tile: TileRef,
private constructionType: UnitType
) {}
init(mg: Game, ticks: number): void {
this.mg = mg;
this.player = mg.player(this.ownerId);
}
tick(ticks: number): void {
if (this.construction == null) {
const info = this.mg.unitInfo(this.constructionType);
if (info.constructionDuration == null) {
this.completeConstruction();
this.active = false;
return;
}
const spawnTile = this.player.canBuild(this.constructionType, this.tile);
if (spawnTile == false) {
consolex.warn(`cannot build ${UnitType.Construction}`);
this.active = false;
return;
}
this.construction = this.player.buildUnit(
UnitType.Construction,
0,
spawnTile
);
this.construction.setConstructionType(this.constructionType);
this.ticksUntilComplete = info.constructionDuration;
return;
}
if (!this.construction.isActive()) {
this.active = false;
return;
}
if (this.ticksUntilComplete == 0) {
this.player = this.construction.owner();
this.construction.delete(false);
this.completeConstruction();
this.active = false;
return;
}
this.ticksUntilComplete--;
}
private completeConstruction() {
const player = this.player;
switch (this.constructionType) {
case UnitType.AtomBomb:
case UnitType.HydrogenBomb:
this.mg.addExecution(
new NukeExecution(this.constructionType, player.id(), this.tile)
);
break;
case UnitType.MIRV:
this.mg.addExecution(new MirvExecution(player.id(), this.tile));
break;
case UnitType.Warship:
this.mg.addExecution(new WarshipExecution(player.id(), this.tile));
break;
case UnitType.Port:
this.mg.addExecution(new PortExecution(player.id(), this.tile));
break;
case UnitType.MissileSilo:
this.mg.addExecution(new MissileSiloExecution(player.id(), this.tile));
break;
case UnitType.DefensePost:
this.mg.addExecution(new DefensePostExecution(player.id(), this.tile));
break;
case UnitType.City:
this.mg.addExecution(new CityExecution(player.id(), this.tile));
break;
default:
throw Error(`unit type ${this.constructionType} not supported`);
}
}
owner(): Player {
return null;
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}
+6 -41
View File
@@ -38,6 +38,7 @@ import { DefensePostExecution } from "./DefensePostExecution";
import { CityExecution } from "./CityExecution";
import { TileRef } from "../game/GameMap";
import { MirvExecution } from "./MIRVExecution";
import { ConstructionExecution } from "./ConstructionExecution";
export class Executor {
// private random = new PseudoRandom(999)
@@ -106,47 +107,11 @@ export class Executor {
case "troop_ratio":
return new SetTargetTroopRatioExecution(intent.player, intent.ratio);
case "build_unit":
switch (intent.unit) {
case UnitType.AtomBomb:
case UnitType.HydrogenBomb:
return new NukeExecution(
intent.unit,
intent.player,
this.mg.ref(intent.x, intent.y)
);
case UnitType.MIRV:
return new MirvExecution(
intent.player,
this.mg.ref(intent.x, intent.y)
);
case UnitType.Warship:
return new WarshipExecution(
intent.player,
this.mg.ref(intent.x, intent.y)
);
case UnitType.Port:
return new PortExecution(
intent.player,
this.mg.ref(intent.x, intent.y)
);
case UnitType.MissileSilo:
return new MissileSiloExecution(
intent.player,
this.mg.ref(intent.x, intent.y)
);
case UnitType.DefensePost:
return new DefensePostExecution(
intent.player,
this.mg.ref(intent.x, intent.y)
);
case UnitType.City:
return new CityExecution(
intent.player,
this.mg.ref(intent.x, intent.y)
);
default:
throw Error(`unit type ${intent.unit} not supported`);
}
return new ConstructionExecution(
intent.player,
this.mg.ref(intent.x, intent.y),
intent.unit
);
default:
throw new Error(`intent type ${intent} not found`);
}
+14 -4
View File
@@ -29,6 +29,7 @@ import { AllianceRequestReplyExecution } from "./alliance/AllianceRequestReplyEx
import { closestTwoTiles } from "./Util";
import { calculateBoundingBox, simpleHash } from "../Util";
import { andFN, manhattanDistFN, TileRef } from "../game/GameMap";
import { ConstructionExecution } from "./ConstructionExecution";
export class FakeHumanExecution implements Execution {
private firstMove = true;
@@ -304,14 +305,16 @@ export class FakeHumanExecution implements Execution {
);
if (oceanTiles.length > 0) {
const buildTile = this.random.randElement(oceanTiles);
this.mg.addExecution(new PortExecution(this.player.id(), buildTile));
this.mg.addExecution(
new ConstructionExecution(this.player.id(), buildTile, UnitType.Port)
);
}
return;
}
this.maybeSpawnStructure(
UnitType.City,
2,
(t) => new CityExecution(this.player.id(), t)
(t) => new ConstructionExecution(this.player.id(), t, UnitType.City)
);
if (this.maybeSpawnWarship()) {
return;
@@ -319,7 +322,8 @@ export class FakeHumanExecution implements Execution {
this.maybeSpawnStructure(
UnitType.MissileSilo,
1,
(t) => new MissileSiloExecution(this.player.id(), t)
(t) =>
new ConstructionExecution(this.player.id(), t, UnitType.MissileSilo)
);
}
@@ -369,7 +373,13 @@ export class FakeHumanExecution implements Execution {
consolex.warn("cannot spawn destroyer");
return false;
}
this.mg.addExecution(new WarshipExecution(this.player.id(), targetTile));
this.mg.addExecution(
new ConstructionExecution(
this.player.id(),
targetTile,
UnitType.Warship
)
);
return true;
}
return false;