From 429e5f8ed91e6b70dd779763ad5908837142c64b Mon Sep 17 00:00:00 2001 From: Pilow Date: Fri, 17 May 2024 20:28:01 +0700 Subject: [PATCH] Fix map with a lot of obstacles --- src/main/java/com/leekwars/generator/maps/Map.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/leekwars/generator/maps/Map.java b/src/main/java/com/leekwars/generator/maps/Map.java index 1ff6ad8..2616939 100644 --- a/src/main/java/com/leekwars/generator/maps/Map.java +++ b/src/main/java/com/leekwars/generator/maps/Map.java @@ -152,7 +152,9 @@ public static Map generateMap(State state, int context, int width, int height, i } } } - map.setEntity(l, c); + if (c != null) { + map.setEntity(l, c); + } } } @@ -160,7 +162,7 @@ public static Map generateMap(State state, int context, int width, int height, i } else { - while (!valid && nb < 63) { + while (!valid && nb++ < 63) { map = new Map(width, height); map.state = state; @@ -206,6 +208,7 @@ public static Map generateMap(State state, int context, int width, int height, i c = map.getRandomCell(state, t == 0 ? 1 : 4); } } + if (c == null) continue; map.setEntity(l, c); leeks.add(l); @@ -230,7 +233,6 @@ public static Map generateMap(State state, int context, int width, int height, i } } } - nb++; } } @@ -404,20 +406,24 @@ public void clear() { public Cell getRandomCell(State state) { Cell retour = null; + int nb = 0; while (retour == null || !retour.available(this)) { retour = getCell(state.getRandom().getInt(0, nb_cells)); + if (nb++ > 64) break; } return retour; } public Cell getRandomCell(State state, int part) { Cell retour = null; + int nb = 0; while (retour == null || !retour.available(this)) { int y = state.getRandom().getInt(0, height - 1); int x = state.getRandom().getInt(0, width / 4); int cellid = y * (width * 2 - 1); cellid += (part - 1) * width / 4 + x; retour = getCell(cellid); + if (nb++ > 64) break; } return retour; }