-
Notifications
You must be signed in to change notification settings - Fork 669
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
Merged
Merged
Random spawn #2375
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f8d8844
feat: random spawn
mepoohsta fdc8d84
Merge branch 'main' into random-spawn
mepoohsta 080408a
fix: padding
mepoohsta 93ee430
fix: revert lang changes
mepoohsta 3ad66f7
fix: issue with tooCloseToOtherPlayer
mepoohsta da2fec8
fix: revert missed langs
mepoohsta 1452ec9
fix: typo
mepoohsta 7eb62af
feat: move PlayerSpawner to utils
mepoohsta 16b1f70
feat: added auto GoToPlayerEvent on random spawn
mepoohsta 65013df
feat: change random spawn translation
mepoohsta 3dcb5cb
feat: add splash screen if auto allocation was failed
mepoohsta 4cc3336
Merge branch 'main' into random-spawn
mepoohsta 823ffb4
feat: add tests
mepoohsta ec20f48
fix: tests
mepoohsta ffb024c
feat: set max spawn retries same as BotSpawner
mepoohsta 38c40b7
fix: adjust tests
mepoohsta 4ec4126
fix: move isRandomSpawn to config
mepoohsta e57aac8
fix: config
mepoohsta dc5f4a4
feat: rewrite goToPlayer logic
mepoohsta 69a0d9f
feat: clearTimeout on end game
mepoohsta 5f036e4
Merge branch 'main' into random-spawn
mepoohsta f86967f
fix: typo
mepoohsta 9d8e350
Merge branch 'main' into random-spawn
evanpelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| 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 nornally not happen, | ||
| // but if it does maybe need to add some splash screen or add additional logic | ||
| console.warn(`cannot spawn ${player.id}`); | ||
| continue; | ||
| } | ||
|
|
||
| this.players.push(new SpawnExecution(player.info(), spawnLand)); | ||
| } | ||
|
|
||
| return this.players; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.