Apply changes from #63 (Anime-Game-Servers/Grasscutter-Quests)

This commit is contained in:
KingRainbow44
2023-04-23 22:51:08 -04:00
parent d608831594
commit c9d6225194
20 changed files with 893 additions and 460 deletions

View File

@@ -1,48 +1,63 @@
package emu.grasscutter.data.server;
import com.github.davidmoten.rtreemulti.RTree;
import com.github.davidmoten.rtreemulti.geometry.Geometry;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.scripts.SceneIndexManager;
import emu.grasscutter.utils.GridPosition;
import emu.grasscutter.utils.Position;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class Grid {
public Map<String, Set<Integer>> grid;
public Map<GridPosition, Set<Integer>> gridMap = new LinkedHashMap<>();
public transient RTree<Map.Entry<GridPosition, Set<Integer>>, Geometry> gridOptimized = null;
private transient Set<Integer> nearbyGroups = new HashSet<>(100);
/** Loads the correct grid map. */
public void load() {
this.grid.forEach((position, groups) -> this.gridMap.put(new GridPosition(position), groups));
public Map<GridPosition, Set<Integer>> grid = new LinkedHashMap<>();
/**
* Creates an optimized cache of the grid.
*/
private void optimize() {
if (this.gridOptimized == null) {
var gridValues = new ArrayList<Map.Entry<GridPosition, Set<Integer>>>();
this.grid.forEach((k, v) -> gridValues.add(new AbstractMap.SimpleEntry<>(k, v)));
this.gridOptimized = SceneIndexManager.buildIndex(2, gridValues, entry -> entry.getKey().toPoint());
}
}
/**
* @return The correctly loaded grid map.
*/
public Map<GridPosition, Set<Integer>> getGrid() {
return this.gridMap;
return this.grid;
}
public Set<Integer> getNearbyGroups(int vision_level, Position position) {
this.optimize(); // Check to see if the grid is optimized.
int width = Grasscutter.getConfig().server.game.visionOptions[vision_level].gridWidth;
int vision_range = Grasscutter.getConfig().server.game.visionOptions[vision_level].visionRange;
int vision_range_grid = vision_range / width;
GridPosition pos = new GridPosition(position, width);
Set<Integer> nearbyGroups = new HashSet<>();
// construct a nearby pisition list, add 1 more because a player can be in an edge case, this
this.nearbyGroups.clear();
// construct a nearby position list, add 1 more because a player can be in an edge case, this
// should not affect much the loading
for (int x = 0; x < vision_range_grid + 1; x++) {
for (int z = 0; z < vision_range_grid + 1; z++) {
nearbyGroups.addAll(gridMap.getOrDefault(pos.addClone(x, z), new HashSet<>()));
nearbyGroups.addAll(gridMap.getOrDefault(pos.addClone(-x, z), new HashSet<>()));
nearbyGroups.addAll(gridMap.getOrDefault(pos.addClone(x, -z), new HashSet<>()));
nearbyGroups.addAll(gridMap.getOrDefault(pos.addClone(-x, -z), new HashSet<>()));
}
}
// var nearbyGroups = new HashSet<Integer>();
// for (int x = 0; x < vision_range_grid + 1; x++) {
// for (int z = 0; z < vision_range_grid + 1; z++) {
// nearbyGroups.addAll(gridMap.getOrDefault(pos.addClone(x, z), new HashSet<>()));
// nearbyGroups.addAll(gridMap.getOrDefault(pos.addClone(-x, z), new HashSet<>()));
// nearbyGroups.addAll(gridMap.getOrDefault(pos.addClone(x, -z), new HashSet<>()));
// nearbyGroups.addAll(gridMap.getOrDefault(pos.addClone(-x, -z), new HashSet<>()));
// }
// }
return nearbyGroups;
// Construct a list of nearby groups.
SceneIndexManager.queryNeighbors(gridOptimized, pos.toDoubleArray(), vision_range_grid + 1)
.forEach(e -> nearbyGroups.addAll(e.getValue()));
return this.nearbyGroups;
}
}