From 4eb61a8f48567a6c3adf819697f90e9893c9ccde Mon Sep 17 00:00:00 2001 From: punchready Date: Sat, 24 Jul 2021 22:29:57 +0200 Subject: [PATCH] fix: random neighbour field technically doesnt allow coordinates outside the map --- .../marvelous/gamelibrary/gamelogic/GameLogic.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java index 70cf6e3..9f01b60 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -710,18 +710,23 @@ public class GameLogic { return options; } + ArrayList allOptions = new ArrayList<>(); for(IntVector2 dir: IntVector2.CardinalDirections) { IntVector2 pos = start.copy().add(dir); if(pos.getX() < 0 || pos.getX() >= state.mapSize.getX() || pos.getY() < 0 || pos.getY() >= state.mapSize.getY()) { continue; } - if(state.entities.findByPosition(pos).size() == 0) { + allOptions.add(pos); + if(state.entities.findByPosition(pos).isEmpty()) { options.add(pos); } } - if(options.size() == 0) { - return getFreeNeighbour(state, start.copy().add(IntVector2.CardinalDirections[rand.nextInt(IntVector2.CardinalDirections.length)])); + if(options.isEmpty()) { + if(allOptions.isEmpty()) { + return allOptions; + } + return getFreeNeighbour(state, start.copy().add(allOptions.get(rand.nextInt(allOptions.size())))); }else { return options; }