mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-19 10:24:47 +01:00
Create files from Grasscutter-Quests
these files are NOT directly compatible with Grasscutter, and require additional modifications to the codebase to work.
This commit is contained in:
118
src/main/java/emu/grasscutter/utils/GridPosition.java
Normal file
118
src/main/java/emu/grasscutter/utils/GridPosition.java
Normal file
@@ -0,0 +1,118 @@
|
||||
package emu.grasscutter.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
public class GridPosition implements Serializable {
|
||||
private static final long serialVersionUID = -2001232300615923575L;
|
||||
|
||||
@Getter @Setter private int x;
|
||||
|
||||
@Getter @Setter private int z;
|
||||
|
||||
@Getter @Setter private int width;
|
||||
|
||||
public GridPosition() {}
|
||||
|
||||
public GridPosition(int x, int y, int width) {
|
||||
set(x, y, width);
|
||||
}
|
||||
|
||||
public GridPosition(GridPosition pos) {
|
||||
this.set(pos);
|
||||
}
|
||||
|
||||
public GridPosition(Position pos, int width) {
|
||||
this.set((int)(pos.getX() / width), (int)(pos.getZ() / width), width);
|
||||
}
|
||||
|
||||
public GridPosition(List<Integer> xzwidth) {
|
||||
this.width = xzwidth.get(2);
|
||||
this.z = xzwidth.get(1);
|
||||
this.x = xzwidth.get(0);
|
||||
}
|
||||
|
||||
public GridPosition(String str) throws IOException {
|
||||
String[] listOfParams = str.replace(" ", "").replace("(", "").replace(")", "").split(",");
|
||||
if(listOfParams.length != 3)
|
||||
throw new IOException("invalid size on GridPosition definition - ");
|
||||
try {
|
||||
this.x = Integer.parseInt(listOfParams[0]);
|
||||
this.z = Integer.parseInt(listOfParams[1]);
|
||||
this.width = Integer.parseInt(listOfParams[2]);
|
||||
} catch(NumberFormatException ignored) {
|
||||
throw new IOException("invalid number on GridPosition definition - ");
|
||||
}
|
||||
}
|
||||
|
||||
public GridPosition set(int x, int z) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public GridPosition set(int x, int z, int width) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
this.width = width;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Deep copy
|
||||
public GridPosition set(GridPosition pos) {
|
||||
return this.set(pos.getX(), pos.getZ(), pos.getWidth());
|
||||
}
|
||||
|
||||
public GridPosition addClone(int x, int z) {
|
||||
GridPosition pos = clone();
|
||||
pos.x += x;
|
||||
pos.z += z;
|
||||
return pos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GridPosition clone() {
|
||||
return new GridPosition(x, z, width);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(" + this.getX() + ", " + this.getZ() + ", " + this.getWidth() + ")";
|
||||
}
|
||||
|
||||
public int[] toIntArray() {
|
||||
return new int[]{ x, z, width };
|
||||
}
|
||||
|
||||
public int[] toXZIntArray() {
|
||||
return new int[]{ x, z };
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
int result = (int) (x ^ (x >>> 32));
|
||||
result = 31 * result + (int) (z ^ (z >>> 32));
|
||||
result = 31 * result + (int) (width ^ (width >>> 32));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null)
|
||||
return false;
|
||||
if (getClass() != o.getClass())
|
||||
return false;
|
||||
GridPosition pos = (GridPosition) o;
|
||||
// field comparison
|
||||
return pos.x == x && pos.z == z && pos.width == width;
|
||||
}
|
||||
}
|
||||
66
src/main/java/emu/grasscutter/utils/KahnsSort.java
Normal file
66
src/main/java/emu/grasscutter/utils/KahnsSort.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package emu.grasscutter.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
|
||||
public class KahnsSort {
|
||||
public static class Node {
|
||||
int source, dest; //Dest is a value, and source too
|
||||
|
||||
public Node(int source, int dest) {
|
||||
this.source = source;
|
||||
this.dest = dest;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Graph {
|
||||
Map<Integer, List<Integer>> mainList;
|
||||
Map<Integer, Integer> degreeList;
|
||||
|
||||
List<Integer> nodeList;
|
||||
|
||||
public Graph(List<Node> nodes, List<Integer> nodeList) {
|
||||
mainList = new HashMap<>();
|
||||
this.nodeList = nodeList;
|
||||
|
||||
for(int i = 0; i < nodeList.size(); i++) mainList.put(nodeList.get(i), new ArrayList<>());
|
||||
|
||||
degreeList = new HashMap<>();
|
||||
for(int i = 0; i < nodeList.size(); i++) degreeList.put(nodeList.get(i), 0);
|
||||
|
||||
for(Node node : nodes) {
|
||||
mainList.get(node.source).add(node.dest);
|
||||
degreeList.replace(node.dest, degreeList.get(node.dest) + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Integer> doSort(Graph graph) {
|
||||
List<Integer> orderedList = new ArrayList<>();
|
||||
Map<Integer, Integer> degreeList = graph.degreeList;
|
||||
|
||||
Stack<Integer> zeroStack = new Stack<>();
|
||||
degreeList.forEach((key, value) -> {
|
||||
if(value == 0) zeroStack.add(key);
|
||||
});
|
||||
|
||||
while(!zeroStack.isEmpty()) {
|
||||
int element = zeroStack.pop();
|
||||
|
||||
//If the list is empty then this node
|
||||
if(!graph.mainList.get(element).isEmpty()) orderedList.add(element);
|
||||
for(int topElement : graph.mainList.get(element)) {
|
||||
degreeList.replace(topElement, degreeList.get(topElement) - 1);
|
||||
|
||||
if(degreeList.get(topElement) == 0) zeroStack.add(topElement);
|
||||
}
|
||||
}
|
||||
|
||||
if(degreeList.values().stream().filter(value -> value != 0).count() != 0) return null; //Loop found
|
||||
|
||||
return orderedList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user