From a3be77dff8a0bb322a8f6d8b2c5943070ac0da6c Mon Sep 17 00:00:00 2001 From: evanpelle Date: Sat, 10 Aug 2024 10:17:19 -0700 Subject: [PATCH] working --- src/client/ClientGame.ts | 3 +- src/core/Game.ts | 2 ++ src/core/GameImpl.ts | 26 ++++++++++++++ src/core/execution/AttackExecution.ts | 51 +++++++++++++++------------ 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/client/ClientGame.ts b/src/client/ClientGame.ts index 4547b09b0..535e906ce 100644 --- a/src/client/ClientGame.ts +++ b/src/client/ClientGame.ts @@ -7,7 +7,6 @@ import {Settings} from "../core/Settings"; import {GameRenderer} from "./GameRenderer"; import {InputHandler, MouseUpEvent, ZoomEvent, DragEvent, MouseDownEvent} from "./InputHandler" import {ClientIntentMessageSchema, ClientJoinMessageSchema, ClientMessageSchema, ServerMessage, ServerMessageSchema, ServerSyncMessage, Turn} from "../core/Schemas"; -import {AttackIntent, Intent, SpawnIntent} from "../core/Schemas"; @@ -96,7 +95,7 @@ export class ClientGame { this.renderer.initialize() this.input.initialize() - // this.executor.spawnBots(500) + this.executor.spawnBots(500) setInterval(() => this.tick(), 10); diff --git a/src/core/Game.ts b/src/core/Game.ts index b94210a80..e6b8c5888 100644 --- a/src/core/Game.ts +++ b/src/core/Game.ts @@ -85,6 +85,8 @@ export interface MutableBoat extends Boat { export interface TerraNullius { ownsTile(cell: Cell): boolean isPlayer(): false + borderTilesWith(other: Player): ReadonlySet + sharesBorderWith(other: Player): boolean } export interface Player { diff --git a/src/core/GameImpl.ts b/src/core/GameImpl.ts index feb31089c..52b6c7701 100644 --- a/src/core/GameImpl.ts +++ b/src/core/GameImpl.ts @@ -67,6 +67,17 @@ export class BoatImpl implements MutableBoat { } } +class Border { + borderWith: Map> = new Map() + + sharesBorderWith(other: Player | TerraNullius): boolean { + if (!this.borderWith.has(other)) { + return false + } + return this.borderWith.get(other).size > 0 + } +} + export class PlayerImpl implements MutablePlayer { public _boats: BoatImpl[] = [] @@ -192,6 +203,21 @@ class TerraNulliusImpl implements TerraNullius { } isPlayer(): false {return false as const} + borderTilesWith(other: Player): ReadonlySet { + const border = new Set() + for (const enemyBorder of other.borderTilesWith(this)) { + for (const neighbor of enemyBorder.neighbors()) { + if (neighbor.terrain() == TerrainTypes.Land && neighbor.owner() == this) { + border.add(neighbor) + } + } + } + return border + } + + sharesBorderWith(other: Player): boolean { + return other.sharesBorderWith(this) + } } export class TerrainMapImpl implements TerrainMap { diff --git a/src/core/execution/AttackExecution.ts b/src/core/execution/AttackExecution.ts index 700143e34..062d2149e 100644 --- a/src/core/execution/AttackExecution.ts +++ b/src/core/execution/AttackExecution.ts @@ -72,14 +72,11 @@ export class AttackExecution implements Execution { } private calculateToConquer() { - const border = this.owner().borderTilesWith(this.target) - const enemyBorder: Set = new Set() - for (const b of border) { - b.neighbors() - .filter(t => t.terrain() == TerrainTypes.Land) - .filter(t => t.owner() == this.target) - .forEach(t => enemyBorder.add(t)) - } + // console.profile('calc_to_conquer') + + const enemyBorder = this.target.borderTilesWith(this._owner) + + // let closestTile: Tile; // let closestDist: number = Number.POSITIVE_INFINITY; @@ -108,27 +105,37 @@ export class AttackExecution implements Execution { // } this.toConquer.clear() - let tiles = Array.from(enemyBorder) if (this.targetCell != null) { + let tiles = Array.from(enemyBorder) tiles = tiles.slice().sort((a, b) => manhattanDist(a.cell(), this.targetCell) - manhattanDist(b.cell(), this.targetCell)) - } + for (let i = 0; i < tiles.length; i++) { + const numOwnedByMe = tiles[i].neighbors() + .filter(t => t.terrain() == TerrainTypes.Land) + .filter(t => t.owner() == this._owner) + .length - - for (let i = 0; i < tiles.length; i++) { - const numOwnedByMe = tiles[i].neighbors() - .filter(t => t.terrain() == TerrainTypes.Land) - .filter(t => t.owner() == this._owner) - .length - - let distModifer = 0 - if (this.targetCell != null) { - distModifer = i / tiles.length * 2 + let distModifer = 0 + if (this.targetCell != null) { + distModifer = i / tiles.length * 2 + } + this.toConquer.add(new TileContainer(tiles[i], distModifer - numOwnedByMe + this.random.nextInt(0, 2))) + // this.toConquer.add(new TileContainer(tiles[i], i)) + } + } else { + for (const tile of enemyBorder) { + const numOwnedByMe = tile.neighbors() + .filter(t => t.terrain() == TerrainTypes.Land) + .filter(t => t.owner() == this._owner) + .length + + this.toConquer.add(new TileContainer(tile, - numOwnedByMe + this.random.nextInt(0, 2))) } - this.toConquer.add(new TileContainer(tiles[i], distModifer - numOwnedByMe + this.random.nextInt(0, 2))) - // this.toConquer.add(new TileContainer(tiles[i], i)) } + // console.profileEnd('calc_to_conquer') + + } owner(): MutablePlayer {