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
58 changes: 42 additions & 16 deletions src/core/configuration/DefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export abstract class DefaultServerConfig implements ServerConfig {
export class DefaultConfig implements Config {
private pastelTheme: PastelTheme = new PastelTheme();
private pastelThemeDark: PastelThemeDark = new PastelThemeDark();
private unitInfoCache = new Map<UnitType, UnitInfo>();
constructor(
private _serverConfig: ServerConfig,
private _gameConfig: GameConfig,
Expand Down Expand Up @@ -323,30 +324,40 @@ export class DefaultConfig implements Config {
}

unitInfo(type: UnitType): UnitInfo {
const cached = this.unitInfoCache.get(type);
if (cached !== undefined) {
return cached;
}

let info: UnitInfo;
switch (type) {
case UnitType.TransportShip:
return {
info = {
cost: () => 0n,
};
break;
case UnitType.Warship:
return {
info = {
cost: this.costWrapper(
(numUnits: number) => Math.min(1_000_000, (numUnits + 1) * 250_000),
UnitType.Warship,
),
maxHealth: 1000,
};
break;
case UnitType.Shell:
return {
info = {
cost: () => 0n,
damage: 250,
};
break;
case UnitType.SAMMissile:
return {
info = {
cost: () => 0n,
};
break;
case UnitType.Port:
return {
info = {
cost: this.costWrapper(
(numUnits: number) =>
Math.min(1_000_000, Math.pow(2, numUnits) * 125_000),
Expand All @@ -356,47 +367,55 @@ export class DefaultConfig implements Config {
constructionDuration: this.instantBuild() ? 0 : 2 * 10,
upgradable: true,
};
break;
case UnitType.AtomBomb:
return {
info = {
cost: this.costWrapper(() => 750_000, UnitType.AtomBomb),
};
break;
case UnitType.HydrogenBomb:
return {
info = {
cost: this.costWrapper(() => 5_000_000, UnitType.HydrogenBomb),
};
break;
case UnitType.MIRV:
return {
info = {
cost: (game: Game, player: Player) => {
if (player.type() === PlayerType.Human && this.infiniteGold()) {
return 0n;
}
return 25_000_000n + game.stats().numMirvsLaunched() * 15_000_000n;
},
};
break;
case UnitType.MIRVWarhead:
return {
info = {
cost: () => 0n,
};
break;
case UnitType.TradeShip:
return {
info = {
cost: () => 0n,
};
break;
case UnitType.MissileSilo:
return {
info = {
cost: this.costWrapper(() => 1_000_000, UnitType.MissileSilo),
constructionDuration: this.instantBuild() ? 0 : 10 * 10,
upgradable: true,
};
break;
case UnitType.DefensePost:
return {
info = {
cost: this.costWrapper(
(numUnits: number) => Math.min(250_000, (numUnits + 1) * 50_000),
UnitType.DefensePost,
),
constructionDuration: this.instantBuild() ? 0 : 5 * 10,
};
break;
case UnitType.SAMLauncher:
return {
info = {
cost: this.costWrapper(
(numUnits: number) =>
Math.min(3_000_000, (numUnits + 1) * 1_500_000),
Expand All @@ -405,8 +424,9 @@ export class DefaultConfig implements Config {
constructionDuration: this.instantBuild() ? 0 : 30 * 10,
upgradable: true,
};
break;
case UnitType.City:
return {
info = {
cost: this.costWrapper(
(numUnits: number) =>
Math.min(1_000_000, Math.pow(2, numUnits) * 125_000),
Expand All @@ -415,8 +435,9 @@ export class DefaultConfig implements Config {
constructionDuration: this.instantBuild() ? 0 : 2 * 10,
upgradable: true,
};
break;
case UnitType.Factory:
return {
info = {
cost: this.costWrapper(
(numUnits: number) =>
Math.min(1_000_000, Math.pow(2, numUnits) * 125_000),
Expand All @@ -426,13 +447,18 @@ export class DefaultConfig implements Config {
constructionDuration: this.instantBuild() ? 0 : 2 * 10,
upgradable: true,
};
break;
case UnitType.Train:
return {
info = {
cost: () => 0n,
};
break;
default:
assertNever(type);
}

this.unitInfoCache.set(type, info);
return info;
}

private costWrapper(
Expand Down
57 changes: 33 additions & 24 deletions src/core/execution/NukeExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,28 +252,31 @@ export class NukeExecution implements Execution {
throw new Error("Not initialized");
}

const magnitude = this.mg.config().nukeMagnitudes(this.nuke.type());
const mg = this.mg;
const config = mg.config();

const magnitude = config.nukeMagnitudes(this.nuke.type());
const toDestroy = this.tilesToDestroy();

// Retrieve all impacted players and the number of tiles
const tilesPerPlayers = new Map<Player, number>();
for (const tile of toDestroy) {
const owner = this.mg.owner(tile);
const owner = mg.owner(tile);
if (owner.isPlayer()) {
owner.relinquish(tile);
const numTiles = tilesPerPlayers.get(owner);
tilesPerPlayers.set(owner, numTiles === undefined ? 1 : numTiles + 1);
tilesPerPlayers.set(owner, (tilesPerPlayers.get(owner) ?? 0) + 1);
}
if (this.mg.isLand(tile)) {
this.mg.setFallout(tile, true);

if (mg.isLand(tile)) {
mg.setFallout(tile, true);
}
}

// Then compute the explosion effect on each player
for (const [player, numImpactedTiles] of tilesPerPlayers) {
const config = this.mg.config();
const tilesBeforeNuke = player.numTilesOwned() + numImpactedTiles;
const transportShips = player.units(UnitType.TransportShip);
const outgoingAttacks = player.outgoingAttacks();
const maxTroops = config.maxTroops(player);
// nukeDeathFactor could compute the complete fallout in a single call instead
for (let i = 0; i < numImpactedTiles; i++) {
Expand All @@ -287,39 +290,45 @@ export class NukeExecution implements Execution {
maxTroops,
),
);
player.outgoingAttacks().forEach((attack) => {
for (const attack of outgoingAttacks) {
const attackTroops = attack.troops();
const deaths = config.nukeDeathFactor(
this.nukeType,
attack.troops(),
attackTroops,
numTilesLeft,
maxTroops,
);
attack.setTroops(attack.troops() - deaths);
});
transportShips.forEach((unit) => {
attack.setTroops(attackTroops - deaths);
}
for (const unit of transportShips) {
const unitTroops = unit.troops();
const deaths = config.nukeDeathFactor(
this.nukeType,
unit.troops(),
unitTroops,
numTilesLeft,
maxTroops,
);
unit.setTroops(unit.troops() - deaths);
});
unit.setTroops(unitTroops - deaths);
}
}
}

const outer2 = magnitude.outer * magnitude.outer;
for (const unit of this.mg.units()) {
const dst = this.dst;
const destroyer = this.player;
for (const unit of mg.units()) {
const type = unit.type();
if (
unit.type() !== UnitType.AtomBomb &&
unit.type() !== UnitType.HydrogenBomb &&
unit.type() !== UnitType.MIRVWarhead &&
unit.type() !== UnitType.MIRV &&
unit.type() !== UnitType.SAMMissile
type === UnitType.AtomBomb ||
type === UnitType.HydrogenBomb ||
type === UnitType.MIRVWarhead ||
type === UnitType.MIRV ||
type === UnitType.SAMMissile
) {
if (this.mg.euclideanDistSquared(this.dst, unit.tile()) < outer2) {
unit.delete(true, this.player);
}
continue;
}
if (mg.euclideanDistSquared(dst, unit.tile()) < outer2) {
unit.delete(true, destroyer);
}
}

Expand Down
Loading
Loading