From 7325cfc7e614f6bd7a65173a447ccb4262c34e33 Mon Sep 17 00:00:00 2001 From: Evan Date: Fri, 24 Jan 2025 09:09:31 -0800 Subject: [PATCH] leaderboard shows percentage non-fallout tiles owned --- src/client/graphics/layers/Leaderboard.ts | 4 +++- src/core/configuration/DevConfig.ts | 22 ++++++++---------- src/core/game/GameImpl.ts | 12 ++-------- src/core/game/GameMap.ts | 27 ++++++++++++++++++++--- src/core/game/GameView.ts | 1 + 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/client/graphics/layers/Leaderboard.ts b/src/client/graphics/layers/Leaderboard.ts index 95ffa5f19..64545f33a 100644 --- a/src/client/graphics/layers/Leaderboard.ts +++ b/src/client/graphics/layers/Leaderboard.ts @@ -53,12 +53,14 @@ export class Leaderboard extends LitElement implements Layer { const sorted = this.game.playerViews() .sort((a, b) => b.numTilesOwned() - a.numTilesOwned()) + const numTilesWithoutFallout = this.game.numLandTiles() - this.game.numTilesWithFallout() + this.players = sorted .slice(0, 5) .map((player, index) => ({ name: player.displayName(), position: index + 1, - score: formatPercentage(player.numTilesOwned() / this.game.numLandTiles()), + score: formatPercentage(player.numTilesOwned() / numTilesWithoutFallout), gold: renderNumber(player.gold()), isMyPlayer: player == myPlayer, player: player diff --git a/src/core/configuration/DevConfig.ts b/src/core/configuration/DevConfig.ts index 803d3b6a0..07995f0bd 100644 --- a/src/core/configuration/DevConfig.ts +++ b/src/core/configuration/DevConfig.ts @@ -31,26 +31,22 @@ export class DevConfig extends DefaultConfig { } // percentageTilesOwnedToWin(): number { - // return 1 + // return 10 // } // populationIncreaseRate(player: Player): number { // return this.maxPopulation(player) // } - - - // tradeShipSpawnRate(): number { // return 10 + // boatMaxDistance(): number { + // return 5000 // } - boatMaxDistance(): number { - return 5000 - } - numBots(): number { - return 0 - } - spawnNPCs(): boolean { - return false - } + // numBots(): number { + // return 0 + // } + // spawnNPCs(): boolean { + // return false + // } } diff --git a/src/core/game/GameImpl.ts b/src/core/game/GameImpl.ts index 266d595be..936f6137b 100644 --- a/src/core/game/GameImpl.ts +++ b/src/core/game/GameImpl.ts @@ -43,8 +43,6 @@ export class GameImpl implements Game { private updates: GameUpdates = createGameUpdatesMap() - private _numTilesWithFallout = 0 - constructor( private _map: GameMap, private miniGameMap: GameMap, @@ -62,9 +60,6 @@ export class GameImpl implements Game { )) } - numTilesWithFallout(): number { - return this._numTilesWithFallout - } owner(ref: TileRef): Player | TerraNullius { return this.playerBySmallID(this.ownerID(ref)) @@ -99,7 +94,6 @@ export class GameImpl implements Game { if (this._map.hasFallout(tile)) { return } - this._numTilesWithFallout++ this._map.setFallout(tile, value) this.addUpdate({ type: GameUpdateType.Tile, @@ -341,10 +335,7 @@ export class GameImpl implements Game { owner._tiles.add(tile) owner._lastTileChange = this._ticks this.updateBorders(tile) - if (this._map.hasFallout(tile)) { - this._numTilesWithFallout-- - this._map.setFallout(tile, false) - } + this._map.setFallout(tile, false) this.addUpdate({ type: GameUpdateType.Tile, update: this.toTileUpdate(tile) @@ -511,6 +502,7 @@ export class GameImpl implements Game { bfs(tile: TileRef, filter: (gm: GameMap, tile: TileRef) => boolean): Set { return this._map.bfs(tile, filter) } toTileUpdate(tile: TileRef): bigint { return this._map.toTileUpdate(tile) } updateTile(tu: TileUpdate): TileRef { return this._map.updateTile(tu) } + numTilesWithFallout(): number { return this._map.numTilesWithFallout() } } // Or a more dynamic approach that will catch new enum values: diff --git a/src/core/game/GameMap.ts b/src/core/game/GameMap.ts index f2653bb3d..9ee1a9ffd 100644 --- a/src/core/game/GameMap.ts +++ b/src/core/game/GameMap.ts @@ -43,9 +43,13 @@ export interface GameMap { toTileUpdate(tile: TileRef): bigint updateTile(tu: TileUpdate): TileRef + + numTilesWithFallout(): number } export class GameMapImpl implements GameMap { + private _numTilesWithFallout = 0 + private readonly terrain: Uint8Array; // Immutable terrain data private readonly state: Uint16Array; // Mutable game state private readonly width_: number; @@ -74,6 +78,9 @@ export class GameMapImpl implements GameMap { this.terrain = terrainData; this.state = new Uint16Array(width * height); } + numTilesWithFallout(): number { + return this._numTilesWithFallout + } ref(x: number, y: number): TileRef { if (!this.isValidCoord(x, y)) { @@ -145,10 +152,17 @@ export class GameMapImpl implements GameMap { } setFallout(ref: TileRef, value: boolean): void { + const existingFallout = this.hasFallout(ref) if (value) { - this.state[ref] |= 1 << GameMapImpl.FALLOUT_BIT; + if (!existingFallout) { + this._numTilesWithFallout++ + this.state[ref] |= 1 << GameMapImpl.FALLOUT_BIT; + } } else { - this.state[ref] &= ~(1 << GameMapImpl.FALLOUT_BIT); + if (existingFallout) { + this._numTilesWithFallout-- + this.state[ref] &= ~(1 << GameMapImpl.FALLOUT_BIT); + } } } @@ -253,8 +267,15 @@ export class GameMapImpl implements GameMap { const tileRef = Number(tu >> 16n); const state = Number(tu & 0xFFFFn); - // Update the state for this tile + const existingFallout = this.hasFallout(tileRef) this.state[tileRef] = state; + const newFallout = this.hasFallout(tileRef) + if (existingFallout && !newFallout) { + this._numTilesWithFallout-- + } + if (!existingFallout && newFallout) { + this._numTilesWithFallout++ + } return tileRef; } diff --git a/src/core/game/GameView.ts b/src/core/game/GameView.ts index 11e5de1f3..f4d72495f 100644 --- a/src/core/game/GameView.ts +++ b/src/core/game/GameView.ts @@ -299,4 +299,5 @@ export class GameView implements GameMap { bfs(tile: TileRef, filter: (gm: GameMap, tile: TileRef) => boolean): Set { return this._map.bfs(tile, filter) } toTileUpdate(tile: TileRef): bigint { return this._map.toTileUpdate(tile) } updateTile(tu: TileUpdate): TileRef { return this._map.updateTile(tu) } + numTilesWithFallout(): number { return this._map.numTilesWithFallout() } }