fix: circumvent late update issues by manually locking positions

This commit is contained in:
punchready 2021-07-25 00:41:22 +02:00
parent 9b8e3a7be3
commit 191817100d
1 changed files with 18 additions and 4 deletions

View File

@ -325,12 +325,15 @@ public class GameLogic {
List<StoneType> stones = Arrays.asList(target.inventory.getStonesAsArray());
Collections.shuffle(stones); // required by documents
ArrayList<IntVector2> used = new ArrayList<>();
for(StoneType stone: stones) {
ArrayList<IntVector2> options = getFreeNeighbour(state, target.getPosition());
ArrayList<IntVector2> options = getFreeNeighbour(state, target.getPosition(), used);
IntVector2 picked = options.get(rand.nextInt(options.size()));
used.add(picked);
result.add(new EventBuilder(EventType.SpawnEntityEvent)
.withEntity(new InfinityStone(
new EntityID(EntityType.InfinityStones, stone.getID()),
options.get(rand.nextInt(options.size())),
picked,
stone
))
.buildEntityEvent());
@ -704,6 +707,17 @@ public class GameLogic {
* @return A list of free neighbour field options
*/
public static ArrayList<IntVector2> getFreeNeighbour(GameState state, IntVector2 start) {
return getFreeNeighbour(state, start, new ArrayList<>(0));
}
/**
* Finds free neighbour options from a starting field.
* @param state The game state to work on
* @param start The starting position
* @param blocked A list of positions to treat as not-free
* @return A list of free neighbour field options
*/
public static ArrayList<IntVector2> getFreeNeighbour(GameState state, IntVector2 start, List<IntVector2> blocked) {
ArrayList<IntVector2> options = new ArrayList<>();
if(start.getX() < 0 || start.getX() >= state.mapSize.getX() || start.getY() < 0 || start.getY() >= state.mapSize.getY()) {
@ -717,7 +731,7 @@ public class GameLogic {
continue;
}
allOptions.add(pos);
if(state.entities.findByPosition(pos).isEmpty()) {
if(!blocked.contains(pos) && state.entities.findByPosition(pos).isEmpty()) {
options.add(pos);
}
}
@ -726,7 +740,7 @@ public class GameLogic {
if(allOptions.isEmpty()) {
return allOptions;
}
return getFreeNeighbour(state, allOptions.get(rand.nextInt(allOptions.size())));
return getFreeNeighbour(state, allOptions.get(rand.nextInt(allOptions.size())), blocked);
}else {
return options;
}