From 9fdb886134f49e11b6b13f98b2bc3b040db2b758 Mon Sep 17 00:00:00 2001 From: JMcB17 Date: Sun, 16 May 2021 06:57:24 +0100 Subject: [PATCH] Better fix for virus spawning, no infinite loop (#1570) * 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 --- CHANGELOG.md | 3 +++ package.json | 2 +- src/Server.js | 32 ++++++++++++++++++++++++++------ src/gamemodes/Experimental.js | 3 +-- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a1ed9cf2..1471baa26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/package.json b/package.json index 7daa45599..d36e893c4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Server.js b/src/Server.js index c01b63598..ccafc2c8d 100755 --- a/src/Server.js +++ b/src/Server.js @@ -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; @@ -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) { @@ -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++) { @@ -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); } diff --git a/src/gamemodes/Experimental.js b/src/gamemodes/Experimental.js index ddd0c697a..5b8a4de68 100644 --- a/src/gamemodes/Experimental.js +++ b/src/gamemodes/Experimental.js @@ -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) {