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
+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());
}
}