Skip to content

Commit

Permalink
Merge pull request #25 from codergautam/adtest
Browse files Browse the repository at this point in the history
Fix AI player spawning logic
  • Loading branch information
codergautam authored Jan 21, 2024
2 parents c2c9eda + 848634f commit 13d6612
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
9 changes: 8 additions & 1 deletion server/src/game/GameMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class GameMap {
this.entityTimers = new Set();
this.coinsCount = map.coinsCount !== undefined ? map.coinsCount : 100;
this.chestsCount = map.chestCount !== undefined ? map.chestsCount : 50;
this.aiPlayersCount = map.aiPlayersCount !== undefined ? map.aiPlayersCount : 10;
this.aiPlayersCount = map.aiPlayersCount !== undefined ? map.aiPlayersCount : 10; // contrary to the name, we want to make sure theres no more than this many AI players, goal is to maintain the game at this many players when low traffic
}

initialize() {
Expand All @@ -71,6 +71,7 @@ class GameMap {
spawnZone: this.shape,
});
}
console.log('spawning', this.aiPlayersCount, 'AI bots');
for (let i = 0; i < this.aiPlayersCount; i++) {
this.spawnPlayerBot();
}
Expand All @@ -88,6 +89,12 @@ class GameMap {
this.addEntity(spawner.definition);
}
}

if(this.game.players.size < this.aiPlayersCount) {
for (let i = 0; i < this.aiPlayersCount - this.game.players.size; i++) {
this.spawnPlayerBot();
}
}
}

spawnPlayerBot() {
Expand Down
6 changes: 5 additions & 1 deletion server/src/game/entities/PlayerBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ class PlayerAI extends Player {
remove(reason) {
super.remove(reason);

this.game.map.spawnPlayerBot();
// only spawn if total player count is less than game.aiPlayerCount
// if(this.game.players.size < this.game.aiPlayerCount) {
// console.log('respawning AI bot');
// this.game.map.spawnPlayerBot();
// }
}
}

Expand Down
11 changes: 11 additions & 0 deletions server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ function start() {
game.initialize();
const server = new Server(game);
server.initialize(app);
app.get('/serverinfo', (res) => {
setCorsHeaders(res);
res.writeHeader('Content-Type', 'application/json');
res.writeStatus('200 OK');
res.end(JSON.stringify({
tps: game.tps,
entityCnt: game.entities.size,
playerCnt: game.players.size,
realPlayersCnt: [...game.players.values()].filter(p => !p.isBot).length,
}));
});

// Gameloop
const frameTime = 1000 / config.tickRate;
Expand Down

0 comments on commit 13d6612

Please sign in to comment.