-
Notifications
You must be signed in to change notification settings - Fork 660
Random spawn #2375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Random spawn #2375
Changes from 11 commits
f8d8844
fdc8d84
080408a
93ee430
3ad66f7
da2fec8
1452ec9
7eb62af
16b1f70
65013df
3dcb5cb
4cc3336
823ffb4
ec20f48
ffb024c
38c40b7
4ec4126
e57aac8
dc5f4a4
69a0d9f
5f036e4
f86967f
9d8e350
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
evanpelle marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { Game, PlayerType } from "../../game/Game"; | ||
| import { TileRef } from "../../game/GameMap"; | ||
| import { PseudoRandom } from "../../PseudoRandom"; | ||
| import { GameID } from "../../Schemas"; | ||
| import { simpleHash } from "../../Util"; | ||
| import { SpawnExecution } from "../SpawnExecution"; | ||
|
|
||
| export class PlayerSpawner { | ||
| private random: PseudoRandom; | ||
| private players: SpawnExecution[] = []; | ||
|
|
||
| constructor( | ||
| private gm: Game, | ||
| gameID: GameID, | ||
| ) { | ||
| this.random = new PseudoRandom(simpleHash(gameID)); | ||
| } | ||
|
|
||
| private randTile(): TileRef { | ||
| const x = this.random.nextInt(0, this.gm.width()); | ||
| const y = this.random.nextInt(0, this.gm.height()); | ||
|
|
||
| return this.gm.ref(x, y); | ||
| } | ||
|
|
||
| randomSpawnLand(): TileRef | null { | ||
| let tries = 0; | ||
|
|
||
| while (tries < 100) { | ||
|
||
| tries++; | ||
|
|
||
| const tile = this.randTile(); | ||
|
|
||
| if ( | ||
| !this.gm.isLand(tile) || | ||
| this.gm.hasOwner(tile) || | ||
| this.gm.isBorder(tile) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| let tooCloseToOtherPlayer = false; | ||
| for (const spawn of this.players) { | ||
| if (this.gm.manhattanDist(spawn.tile, tile) < 50) { | ||
| tooCloseToOtherPlayer = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (tooCloseToOtherPlayer) { | ||
| continue; | ||
| } | ||
|
|
||
| return tile; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| spawnPlayers(): SpawnExecution[] { | ||
| for (const player of this.gm.allPlayers()) { | ||
| if (player.type() !== PlayerType.Human) { | ||
| continue; | ||
| } | ||
|
|
||
| const spawnLand = this.randomSpawnLand(); | ||
|
|
||
| if (spawnLand === null) { | ||
| // TODO: this should normally not happen, additional logic may be needed, if this occurs | ||
| console.warn(`cannot spawn ${player.id}`); | ||
| continue; | ||
| } | ||
|
|
||
| this.players.push(new SpawnExecution(player.info(), spawnLand)); | ||
| } | ||
|
|
||
| return this.players; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -644,6 +644,9 @@ export class GameView implements GameMap { | |
| inSpawnPhase(): boolean { | ||
| return this.ticks() <= this._config.numSpawnPhaseTurns(); | ||
| } | ||
| isRandomSpawn(): boolean { | ||
| return this._config.randomSpawn(); | ||
| } | ||
|
||
| config(): Config { | ||
| return this._config; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
little worried about this if the machine is slow and it takes longer than 1 second to load the game.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored logic there to mitigate this issue.