Simple Upgradable Structures (Cities, Ports, SAMs and Silos) (#1012)

## Description:

https://github.com/openfrontio/OpenFrontIO/issues/776

I've implemented upgradable structures for cities and ports.

As of right now this is just meant as a QOL change for structure
stacking that currently happens and no gameplay changes are intended.

Structure upgrades cost the same as making a new structure of that type
and function the same as making a new structure of that type.

I'm putting up a draft PR for this now since adding support for SAMs and
Silos will take more time to handle the cooldowns and I want to make
sure I'm on the right track for getting this merged.

I also still need to add bot behavior for this and re-enable min
distance for structures.

I didn't see translations for the UnitInfoModal so I've left that out
for now.

I've tested locally in a single player game so far but will document and
test more thoroughly before merging.


![image](https://github.com/user-attachments/assets/321a17cf-26a5-4152-aae1-6b6a691638bb)


![image](https://github.com/user-attachments/assets/8cfdabe6-f0a1-435a-a5a3-05b442427c2f)


## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

## Please put your Discord username so you can be contacted if a bug or
regression is found:

# Poutine

---------

Co-authored-by: Scott Anderson <scottanderson@users.noreply.github.com>
This commit is contained in:
Ethienne Graveline
2025-06-10 23:04:17 -04:00
committed by GitHub
co-authored by Scott Anderson
parent 9c3b828fc8
commit 9b2c6cc1f6
22 changed files with 334 additions and 46 deletions
+13 -1
View File
@@ -34,7 +34,8 @@ export type Intent =
| EmbargoIntent
| QuickChatIntent
| MoveWarshipIntent
| MarkDisconnectedIntent;
| MarkDisconnectedIntent
| UpgradeStructureIntent;
export type AttackIntent = z.infer<typeof AttackIntentSchema>;
export type CancelAttackIntent = z.infer<typeof CancelAttackIntentSchema>;
@@ -55,6 +56,9 @@ export type TargetTroopRatioIntent = z.infer<
typeof TargetTroopRatioIntentSchema
>;
export type BuildUnitIntent = z.infer<typeof BuildUnitIntentSchema>;
export type UpgradeStructureIntent = z.infer<
typeof UpgradeStructureIntentSchema
>;
export type MoveWarshipIntent = z.infer<typeof MoveWarshipIntentSchema>;
export type QuickChatIntent = z.infer<typeof QuickChatIntentSchema>;
export type MarkDisconnectedIntent = z.infer<
@@ -178,6 +182,7 @@ const BaseIntentSchema = z.object({
"emoji",
"troop_ratio",
"build_unit",
"upgrade_structure",
"embargo",
"move_warship",
]),
@@ -266,6 +271,12 @@ export const BuildUnitIntentSchema = BaseIntentSchema.extend({
y: z.number(),
});
export const UpgradeStructureIntentSchema = BaseIntentSchema.extend({
type: z.literal("upgrade_structure"),
unit: z.nativeEnum(UnitType),
unitId: z.number(),
});
export const CancelAttackIntentSchema = BaseIntentSchema.extend({
type: z.literal("cancel_attack"),
attackID: z.string(),
@@ -316,6 +327,7 @@ const IntentSchema = z.union([
DonateTroopIntentSchema,
TargetTroopRatioIntentSchema,
BuildUnitIntentSchema,
UpgradeStructureIntentSchema,
EmbargoIntentSchema,
MoveWarshipIntentSchema,
QuickChatIntentSchema,
+1
View File
@@ -83,6 +83,7 @@ export const OTHER_INDEX_BUILT = 0; // Structures and warships built
export const OTHER_INDEX_DESTROY = 1; // Structures and warships destroyed
export const OTHER_INDEX_CAPTURE = 2; // Structures captured
export const OTHER_INDEX_LOST = 3; // Structures/warships destroyed/captured by others
export const OTHER_INDEX_UPGRADE = 4; // Structures upgraded
const BigIntStringSchema = z.preprocess((val) => {
if (typeof val === "string" && /^\d+$/.test(val)) return BigInt(val);
+11 -3
View File
@@ -347,6 +347,7 @@ export class DefaultConfig implements Config {
),
territoryBound: true,
constructionDuration: this.instantBuild() ? 0 : 2 * 10,
upgradable: true,
};
case UnitType.AtomBomb:
return {
@@ -390,6 +391,7 @@ export class DefaultConfig implements Config {
: 1_000_000n,
territoryBound: true,
constructionDuration: this.instantBuild() ? 0 : 10 * 10,
upgradable: true,
};
case UnitType.DefensePost:
return {
@@ -406,6 +408,7 @@ export class DefaultConfig implements Config {
),
territoryBound: true,
constructionDuration: this.instantBuild() ? 0 : 5 * 10,
upgradable: true,
};
case UnitType.SAMLauncher:
return {
@@ -422,6 +425,7 @@ export class DefaultConfig implements Config {
),
territoryBound: true,
constructionDuration: this.instantBuild() ? 0 : 30 * 10,
upgradable: true,
};
case UnitType.City:
return {
@@ -439,6 +443,7 @@ export class DefaultConfig implements Config {
),
territoryBound: true,
constructionDuration: this.instantBuild() ? 0 : 2 * 10,
upgradable: true,
};
case UnitType.Construction:
return {
@@ -666,7 +671,11 @@ export class DefaultConfig implements Config {
player.type() === PlayerType.Human && this.infiniteTroops()
? 1_000_000_000
: 2 * (Math.pow(player.numTilesOwned(), 0.6) * 1000 + 50000) +
player.units(UnitType.City).length * this.cityPopulationIncrease();
player
.units(UnitType.City)
.map((city) => city.level())
.reduce((a, b) => a + b, 0) *
this.cityPopulationIncrease();
if (player.type() === PlayerType.Bot) {
return maxPop / 2;
@@ -761,8 +770,7 @@ export class DefaultConfig implements Config {
}
structureMinDist(): number {
// TODO: Increase this to ~15 once upgradable structures are implemented.
return 1;
return 15;
}
shellLifetime(): number {
+3
View File
@@ -24,6 +24,7 @@ import { SetTargetTroopRatioExecution } from "./SetTargetTroopRatioExecution";
import { SpawnExecution } from "./SpawnExecution";
import { TargetPlayerExecution } from "./TargetPlayerExecution";
import { TransportShipExecution } from "./TransportShipExecution";
import { UpgradeStructureExecution } from "./UpgradeStructureExecution";
export class Executor {
// private random = new PseudoRandom(999)
@@ -114,6 +115,8 @@ export class Executor {
this.mg.ref(intent.x, intent.y),
intent.unit,
);
case "upgrade_structure":
return new UpgradeStructureExecution(player, intent.unitId);
case "quick_chat":
return new QuickChatExecution(
player,
+11 -1
View File
@@ -34,10 +34,20 @@ export class MissileSiloExecution implements Execution {
}
}
const cooldown = this.silo.ticksLeftInCooldown();
const frontTime = this.silo.ticksLeftInCooldown();
if (frontTime === undefined) {
return;
}
const cooldown =
this.mg.config().SiloCooldown() - (this.mg.ticks() - frontTime);
if (typeof cooldown === "number" && cooldown >= 0) {
this.silo.touch();
}
if (cooldown <= 0) {
this.silo.reloadMissile();
}
}
isActive(): boolean {
+15 -5
View File
@@ -155,11 +155,6 @@ export class SAMLauncherExecution implements Execution {
target = this.getSingleTarget();
}
const cooldown = this.sam.ticksLeftInCooldown();
if (typeof cooldown === "number" && cooldown >= 0) {
this.sam.touch();
}
const isSingleTarget = target && !target.targetedBySAM();
if (
(isSingleTarget || mirvWarheadTargets.length > 0) &&
@@ -204,6 +199,21 @@ export class SAMLauncherExecution implements Execution {
}
}
}
const frontTime = this.sam.ticksLeftInCooldown();
if (frontTime === undefined) {
return;
}
const cooldown =
this.mg.config().SAMCooldown() - (this.mg.ticks() - frontTime);
if (typeof cooldown === "number" && cooldown >= 0) {
this.sam.touch();
}
if (cooldown <= 0) {
this.sam.reloadMissile();
}
}
isActive(): boolean {
@@ -0,0 +1,44 @@
import { Execution, Game, Player, Unit } from "../game/Game";
export class UpgradeStructureExecution implements Execution {
private structure: Unit | undefined;
private cost: bigint;
constructor(
private player: Player,
private unitId: number,
) {}
init(mg: Game, ticks: number): void {
this.structure = this.player
.units()
.find((unit) => unit.id() === this.unitId);
if (this.structure === undefined) {
console.warn(`structure is undefined`);
return;
}
if (!this.structure.info().upgradable) {
console.warn(`unit type ${this.structure} cannot be upgraded`);
return;
}
this.cost = this.structure.info().cost(this.player);
if (this.player.gold() < this.cost) {
return;
}
this.player.upgradeUnit(this.structure);
return;
}
tick(ticks: number): void {
return;
}
isActive(): boolean {
return false;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}
+8 -1
View File
@@ -130,6 +130,7 @@ export interface UnitInfo {
maxHealth?: number;
damage?: number;
constructionDuration?: number;
upgradable?: boolean;
}
export enum UnitType {
@@ -385,8 +386,9 @@ export interface Unit {
// SAMs & Missile Silos
launch(): void;
ticksLeftInCooldown(): Tick | undefined;
reloadMissile(): void;
isInCooldown(): boolean;
ticksLeftInCooldown(): Tick | undefined;
// Trade Ships
setSafeFromPirates(): void; // Only for trade ships
@@ -396,6 +398,10 @@ export interface Unit {
constructionType(): UnitType | null;
setConstructionType(type: UnitType): void;
// Upgradable Structures
level(): number;
increaseLevel(): void;
// Warships
setPatrolTile(tile: TileRef): void;
patrolTile(): TileRef | undefined;
@@ -471,6 +477,7 @@ export interface Player {
spawnTile: TileRef,
params: UnitParams<T>,
): Unit;
upgradeUnit(unit: Unit): void;
captureUnit(unit: Unit): void;
+3 -1
View File
@@ -80,7 +80,9 @@ export interface UnitUpdate {
targetTile?: TileRef; // Only for nukes
health?: number;
constructionType?: UnitType;
ticksLeftInCooldown?: Tick;
missileTimerQueue: number[];
readyMissileCount: number;
level: number;
}
export interface AttackUpdate {
+6 -4
View File
@@ -112,11 +112,13 @@ export class UnitView {
return this.data.targetTile;
}
ticksLeftInCooldown(): Tick | undefined {
return this.data.ticksLeftInCooldown;
return this.data.missileTimerQueue?.[0];
}
isCooldown(): boolean {
if (this.data.ticksLeftInCooldown === undefined) return false;
return this.data.ticksLeftInCooldown > 0;
isInCooldown(): boolean {
return this.data.readyMissileCount === 0;
}
level(): number {
return this.data.level;
}
}
+6
View File
@@ -754,6 +754,12 @@ export class PlayerImpl implements Player {
return b;
}
upgradeUnit(unit: Unit) {
const cost = this.mg.unitInfo(unit.type()).cost(this);
this.removeGold(cost);
unit.increaseLevel();
}
public buildableUnits(tile: TileRef): BuildableUnit[] {
const validTiles = this.validStructureSpawnTiles(tile);
return Object.values(UnitType).map((u) => {
+3
View File
@@ -85,6 +85,9 @@ export interface Stats {
// Player captures a unit of type
unitCapture(player: Player, type: OtherUnitType): void;
// Player upgrades a unit of type
unitUpgrade(player: Player, type: OtherUnitType): void;
// Player destroys a unit of type
unitDestroy(player: Player, type: OtherUnitType): void;
+5
View File
@@ -20,6 +20,7 @@ import {
OTHER_INDEX_CAPTURE,
OTHER_INDEX_DESTROY,
OTHER_INDEX_LOST,
OTHER_INDEX_UPGRADE,
OtherUnitType,
PlayerStats,
unitTypeToBombUnit,
@@ -234,6 +235,10 @@ export class StatsImpl implements Stats {
this._addOtherUnit(player, type, OTHER_INDEX_CAPTURE, 1);
}
unitUpgrade(player: Player, type: OtherUnitType): void {
this._addOtherUnit(player, type, OTHER_INDEX_UPGRADE, 1);
}
unitDestroy(player: Player, type: OtherUnitType): void {
this._addOtherUnit(player, type, OTHER_INDEX_DESTROY, 1);
}
+28 -19
View File
@@ -26,8 +26,10 @@ export class UnitImpl implements Unit {
private _constructionType: UnitType | undefined;
private _lastOwner: PlayerImpl | null = null;
private _troops: number;
private _cooldownStartTick: Tick | null = null;
private _missileTimerQueue: number[] = [];
private _readyMissileCount: number = 1;
private _patrolTile: TileRef | undefined;
private _level: number = 1;
constructor(
private _type: UnitType,
private mg: GameImpl,
@@ -104,7 +106,9 @@ export class UnitImpl implements Unit {
constructionType: this._constructionType,
targetUnitId: this._targetUnit?.id() ?? undefined,
targetTile: this.targetTile() ?? undefined,
ticksLeftInCooldown: this.ticksLeftInCooldown() ?? undefined,
missileTimerQueue: this._missileTimerQueue,
readyMissileCount: this._readyMissileCount,
level: this.level(),
};
}
@@ -267,30 +271,23 @@ export class UnitImpl implements Unit {
}
launch(): void {
this._cooldownStartTick = this.mg.ticks();
this._missileTimerQueue.push(this.mg.ticks());
this._readyMissileCount--;
this.mg.addUpdate(this.toUpdate());
}
ticksLeftInCooldown(): Tick | undefined {
let cooldownDuration = 0;
if (this.type() === UnitType.SAMLauncher) {
cooldownDuration = this.mg.config().SAMCooldown();
} else if (this.type() === UnitType.MissileSilo) {
cooldownDuration = this.mg.config().SiloCooldown();
} else {
return undefined;
}
if (!this._cooldownStartTick) {
return undefined;
}
return cooldownDuration - (this.mg.ticks() - this._cooldownStartTick);
return this._missileTimerQueue[0];
}
isInCooldown(): boolean {
const ticksLeft = this.ticksLeftInCooldown();
return ticksLeft !== undefined && ticksLeft > 0;
return this._readyMissileCount === 0;
}
reloadMissile(): void {
this._missileTimerQueue.shift();
this._readyMissileCount++;
this.mg.addUpdate(this.toUpdate());
}
setTargetTile(targetTile: TileRef | undefined) {
@@ -335,4 +332,16 @@ export class UnitImpl implements Unit {
this.mg.config().safeFromPiratesCooldownMax()
);
}
level(): number {
return this._level;
}
increaseLevel(): void {
this._level++;
if ([UnitType.MissileSilo, UnitType.SAMLauncher].includes(this.type())) {
this._readyMissileCount++;
}
this.mg.addUpdate(this.toUpdate());
}
}