Skip to content

Commit

Permalink
Better fix for virus spawning, no infinite loop (#1570)
Browse files Browse the repository at this point in the history
* Fix safe spawning for viruses, mother cells, and players

* Bump version number, add to changelog

* Fix duplicated code

* Fix potential infinite loop

* Fix leftover curlybrace syntax error
  • Loading branch information
irismessage authored May 16, 2021
1 parent 3b1213b commit 9fdb886
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
#### 1.6.3
* Fixed safe spawning of virus, mother, and player cells

#### 1.6.2
* Optimised food and virus cell spawning and removal model (no longer use a spawning interval).
* Removed unused gamemodes.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "MultiOgarII",
"version": "1.6.2",
"version": "1.6.3",
"description": "Open source multi-protocol Ogar server",
"author": "Megabyte918 (https://github.com/Megabyte918)",
"homepage": "https://github.com/Megabyte918/MultiOgarII",
Expand Down
32 changes: 26 additions & 6 deletions src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Server {

// Startup
this.run = true;
this.version = '1.6.2';
this.version = '1.6.3';
this.httpServer = null;
this.lastNodeId = 1;
this.lastPlayerId = 1;
Expand Down Expand Up @@ -663,6 +663,22 @@ class Server {
return new Vec2(this.border.minx + this.border.width * Math.random(),
this.border.miny + this.border.height * Math.random());
}
safeSpawn(cell) {
const maxAttempts = 10;

var spawnSuccess = false;
var attempts = 0;
while (!spawnSuccess && attempts < maxAttempts) {
if (!this.willCollide(cell)) {
spawnSuccess = true;
}
else {
cell.position = this.randomPos();
attempts++;
}
}
this.addNode(cell);
}
spawnFood() {
var cell = new Entity.Food(this, null, this.randomPos(), this.config.foodMinSize);
if (this.config.foodMassGrow) {
Expand All @@ -674,8 +690,8 @@ class Server {
}
spawnVirus() {
var virus = new Entity.Virus(this, null, this.randomPos(), this.config.virusMinSize);
if (!this.willCollide(virus))
this.addNode(virus);
this.safeSpawn(virus);

}
spawnCells(virusCount, foodCount) {
for (var i = 0; i < foodCount; i++) {
Expand Down Expand Up @@ -704,9 +720,13 @@ class Server {
}
// Spawn player safely (do not check minions)
var cell = new Entity.PlayerCell(this, player, pos, size);
if (this.willCollide(cell) && !player.isMi)
pos = this.randomPos(); // Not safe => retry
this.addNode(cell);
if (!player.isMi) {
this.addNode(cell);
}
else {
this.safeSpawn(cell);
}

// Set initial mouse coords
player.mouse.assign(pos);
}
Expand Down
3 changes: 1 addition & 2 deletions src/gamemodes/Experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class Experimental extends FFA {
}
// Spawn if no cells are colliding
var mother = new Entity.MotherCell(server, null, server.randomPos(), 149);
if (!server.willCollide(mother))
server.addNode(mother);
server.safeSpawn(mother);
}
// Override
onServerInit(server) {
Expand Down

0 comments on commit 9fdb886

Please sign in to comment.