Skip to content

Commit

Permalink
Move area tracking into a client player class
Browse files Browse the repository at this point in the history
  • Loading branch information
platz1de committed Oct 6, 2024
1 parent 137cb3e commit 82e90d0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/game/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {gameModeFromId} from "./mode/GameModeRegistry";
import {initGameData} from "./GameData";
import {GameTickPacket} from "../network/protocol/packet/game/GameTickPacket";
import {hideAllUIElements, showUIElement} from "../ui/UIManager";
import {ClientPlayer} from "./player/ClientPlayer";

/**
* Start a new game with the given map.
Expand All @@ -40,7 +41,7 @@ export function startGame(map: GameMap, mode: GameMode, seed: number, players: {
playerNameRenderingManager.reset(500);
attackActionHandler.init(500);
spawnManager.init(500);
playerManager.init(players.map((p, i) => new Player(i, p.name, HSLColor.fromRGB(0, 200, 200))), clientId, 500);
playerManager.init(players.map((p, i) => new (i === clientId ? ClientPlayer : Player)(i, p.name, HSLColor.fromRGB(0, 200, 200))), clientId, 500);

random.reset(seed);
}
Expand Down
28 changes: 1 addition & 27 deletions src/game/TerritoryManager.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import {clientPlayer, playerManager} from "./player/PlayerManager";
import {playerManager} from "./player/PlayerManager";
import {territoryRenderingManager} from "../renderer/manager/TerritoryRenderingManager";
import {playerNameRenderingManager} from "../renderer/manager/PlayerNameRenderingManager";
import {areaCalculator} from "../map/area/AreaCalculator";
import {onNeighbors} from "../util/MathUtil";
import {gameMap} from "./GameData";
import {TerritoryTransaction} from "./transaction/TerritoryTransaction";

class TerritoryManager {
tileOwners: Uint16Array;
readonly OWNER_NONE = 65535;
playerIndex: Uint16Array;

/**
* Resets the territory manager.
Expand All @@ -21,8 +18,6 @@ class TerritoryManager {
for (let i = 0; i < this.tileOwners.length; i++) {
this.tileOwners[i] = gameMap.getTile(i).isSolid ? this.OWNER_NONE : this.OWNER_NONE - 1;
}

this.playerIndex = new Uint16Array(areaCalculator.preprocessMap());
}

/**
Expand Down Expand Up @@ -102,22 +97,8 @@ class TerritoryManager {
this.tileOwners[tile] = owner;
if (previousOwner !== this.OWNER_NONE) {
playerManager.getPlayer(previousOwner).removeTile(tile, transaction);
if (previousOwner === clientPlayer.id) {
onNeighbors(tile, (neighbor) => {
if (this.isWater(neighbor)) {
this.playerIndex[areaCalculator.areaIndex[neighbor]]--;
}
});
}
}
playerManager.getPlayer(owner).addTile(tile, transaction);
if (owner === clientPlayer.id) {
onNeighbors(tile, (neighbor) => {
if (this.isWater(neighbor)) {
this.playerIndex[areaCalculator.areaIndex[neighbor]]++;
}
});
}
}

/**
Expand All @@ -133,13 +114,6 @@ class TerritoryManager {
playerManager.getPlayer(owner).removeTile(tile, transaction);
//TODO: integrate this with the transaction system
territoryRenderingManager.clear(tile);
if (owner === clientPlayer.id) {
onNeighbors(tile, (neighbor) => {
if (this.isWater(neighbor)) {
this.playerIndex[areaCalculator.areaIndex[neighbor]]--;
}
});
}
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions src/game/player/ClientPlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {Player} from "./Player";
import {TerritoryTransaction} from "../transaction/TerritoryTransaction";
import {onNeighbors} from "../../util/MathUtil";
import {HSLColor} from "../../util/HSLColor";
import {areaCalculator} from "../../map/area/AreaCalculator";
import {territoryManager} from "../TerritoryManager";

let areaIndex: Uint16Array;

export class ClientPlayer extends Player {
constructor(id: number, name: string, baseColor: HSLColor) {
super(id, name, baseColor);

areaIndex = new Uint16Array(areaCalculator.preprocessMap());
}

addTile(tile: number, transaction: TerritoryTransaction) {
super.addTile(tile, transaction);

onNeighbors(tile, neighbor => {
if (territoryManager.isWater(neighbor)) {
areaIndex[areaCalculator.areaIndex[neighbor]]++;
}
});
}

removeTile(tile: number, transaction: TerritoryTransaction) {
super.removeTile(tile, transaction);

onNeighbors(tile, neighbor => {
if (territoryManager.isWater(neighbor)) {
areaIndex[areaCalculator.areaIndex[neighbor]]--;
}
});
}
}

/**
* Check if a player has territory neighboring a specific area.
* This is used to determine whether an area needs to be checked for potential attacks or boat placements.
* @param area The area to check
*/
export function hasTerritoryNear(area: number): boolean {
return areaIndex[area] > 0;
}
5 changes: 3 additions & 2 deletions src/map/area/BoatPathfinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {clientPlayer} from "../../game/player/PlayerManager";
import {gameMap} from "../../game/GameData";
import {UnsupportedDataException} from "../../util/Exceptions";
import {checkLineOfSight} from "../../util/VoxelRayTrace";
import {hasTerritoryNear} from "../../game/player/ClientPlayer";

/**
* Pathfinding for boats.
Expand Down Expand Up @@ -170,7 +171,7 @@ function findStartsInArea(start: number) {
export function findStartingPoint(target: number): number | null {
let inSameArea = false;
onNeighborWater(target, tile => {
if (territoryManager.playerIndex[areaCalculator.areaIndex[tile]] > 0) {
if (hasTerritoryNear(areaCalculator.areaIndex[tile])) {
inSameArea = true;
}
});
Expand All @@ -193,7 +194,7 @@ export function findStartingPoint(target: number): number | null {
if (cost >= foundCost) {
break;
}
if (territoryManager.playerIndex[node.canonicalAreaId as number] > 0) {
if (hasTerritoryNear(node.canonicalAreaId as number)) {
const [tile, distance] = findPlayerTile(node.x + node.y * gameMap.width);
if (distance < foundCost) {
found = tile;
Expand Down

0 comments on commit 82e90d0

Please sign in to comment.