-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
451 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,6 @@ | |
<artifactId>dotenv-java</artifactId> | ||
<version>3.0.0</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
|
455 changes: 227 additions & 228 deletions
455
src/main/java/cz/trailsthroughshadows/algorithm/Dungeon.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/cz/trailsthroughshadows/algorithm/location/Navigation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cz.trailsthroughshadows.algorithm.location; | ||
|
||
import cz.trailsthroughshadows.algorithm.util.Vec3; | ||
import cz.trailsthroughshadows.api.table.schematic.hex.Hex; | ||
import cz.trailsthroughshadows.api.table.schematic.part.Part; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.*; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class Navigation { | ||
|
||
private final List<Part> parts; | ||
|
||
public Navigation(Part... parts) { | ||
this.parts = Arrays.asList(parts); | ||
} | ||
|
||
public Part getPart(Hex hex) { | ||
return parts.stream() | ||
.filter(part -> part.getId() == hex.getKey().getIdPart()) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
public int getDistance(Hex hex1, Hex hex2) { | ||
Vec3<Integer> vec = new Vec3<>(hex1.getQ() - hex2.getQ(), hex1.getR() - hex2.getR(), hex1.getS() - hex2.getS()); | ||
return (Math.abs(vec.x()) + Math.abs(vec.y()) + Math.abs(vec.z())) / 2; | ||
} | ||
|
||
public List<Hex> getNeighbors(Hex hex) { | ||
return getNeighbors(hex, 1); | ||
} | ||
|
||
public List<Hex> getNeighbors(Hex hex, int range) { | ||
return getPart(hex).getHexes().stream() | ||
.filter(neighbor -> hex != neighbor && getDistance(hex, neighbor) <= range) | ||
.toList(); | ||
} | ||
|
||
public List<Hex> getPath(Hex hex1, Hex hex2) { | ||
Map<Hex, Integer> distances = new HashMap<>(); | ||
Queue<Hex> queue = new LinkedList<>(); | ||
List<Hex> path = new ArrayList<>(); | ||
|
||
distances.put(hex1, 0); | ||
queue.add(hex1); | ||
|
||
// calculate distance from hex1 | ||
while (!queue.isEmpty()) { | ||
Hex current = queue.poll(); | ||
int distance = distances.get(current); | ||
|
||
List<Hex> neighbors = getNeighbors(current); | ||
for (Hex neighbor : neighbors) { | ||
if (distances.containsKey(neighbor)) | ||
continue; | ||
|
||
distances.put(neighbor, distance + 1); | ||
queue.add(neighbor); | ||
} | ||
} | ||
|
||
// hex2 is not reachable from hex1 | ||
if (!distances.containsKey(hex2)) | ||
return null; | ||
|
||
// create path to hex2 | ||
Hex currentHex = hex2; | ||
int currentDistance = distances.get(hex2); | ||
|
||
while (!currentHex.equals(hex1)) { | ||
path.add(currentHex); | ||
|
||
List<Hex> neighbors = getNeighbors(currentHex); | ||
for (Hex neighbor : neighbors) { | ||
int neighborDistance = distances.get(neighbor); | ||
|
||
if (neighborDistance < currentDistance) { | ||
currentHex = neighbor; | ||
currentDistance = neighborDistance; | ||
break; | ||
} | ||
} | ||
} | ||
path.add(hex1); | ||
|
||
Collections.reverse(path); | ||
return path; | ||
} | ||
} |
7 changes: 3 additions & 4 deletions
7
...roughshadows/algorithm/util/ListUtil.java → ...lsthroughshadows/algorithm/util/List.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/cz/trailsthroughshadows/algorithm/util/Vec3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package cz.trailsthroughshadows.algorithm.util; | ||
|
||
public record Vec3<T>(T x, T y, T z) { | ||
} |
4 changes: 0 additions & 4 deletions
4
src/main/java/cz/trailsthroughshadows/algorithm/utils/Vec3.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/cz/trailsthroughshadows/api/rest/endpoints/ValidationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package cz.trailsthroughshadows.api.rest.endpoints; | ||
|
||
import cz.trailsthroughshadows.algorithm.location.Navigation; | ||
import cz.trailsthroughshadows.api.table.schematic.hex.Hex; | ||
import cz.trailsthroughshadows.api.table.schematic.part.Part; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
@Slf4j | ||
public class ValidationService { | ||
|
||
// TODO: Implement Part validation @Bačkorče | ||
// Validations?: | ||
// 1. Part must have at least 5 hexes | ||
// 2. Part must have at most 50 hexes | ||
// 3. Part is maximum 8 hexes wide and 8 hexes tall | ||
// 4. All hexes must be connected | ||
public List<String> validatePart(Part part) { | ||
List<String> errors = new ArrayList<>(); | ||
|
||
int minHexes = 5; | ||
int maxHexes = 50; | ||
int maxHexesWide = 8; | ||
|
||
// min 5 hexes | ||
if (part.getHexes().size() < minHexes) { | ||
errors.add("Part must have at least %d hexes!".formatted(minHexes)); | ||
} | ||
|
||
// max 50 hexes | ||
if (part.getHexes().size() > maxHexes) { | ||
errors.add("Part must have at most 50 hexes!"); | ||
} | ||
|
||
// max 8 hexes wide | ||
int diffQ = part.getHexes().stream().mapToInt(Hex::getQ).max().getAsInt() - part.getHexes().stream().mapToInt(Hex::getQ).min().getAsInt(); | ||
int diffR = part.getHexes().stream().mapToInt(Hex::getR).max().getAsInt() - part.getHexes().stream().mapToInt(Hex::getR).min().getAsInt(); | ||
int diffS = part.getHexes().stream().mapToInt(Hex::getS).max().getAsInt() - part.getHexes().stream().mapToInt(Hex::getS).min().getAsInt(); | ||
if (diffQ > maxHexesWide || diffR > maxHexesWide || diffS > maxHexesWide) { | ||
errors.add("Part must not be wider than %d hexes!".formatted(maxHexesWide)); | ||
} | ||
|
||
// no hexes can be on the same position | ||
int duplicates = 0; | ||
for (Hex hex1 : part.getHexes()) { | ||
for (Hex hex2 : part.getHexes()) { | ||
if (hex1 == hex2) | ||
continue; | ||
|
||
if (hex1.getQ() == hex2.getQ() && hex1.getR() == hex2.getR() && hex1.getS() == hex2.getS()) { | ||
duplicates++; | ||
break; | ||
} | ||
} | ||
} | ||
if (duplicates > 0) | ||
errors.add("Part must not have duplicate hexes!"); | ||
|
||
// every hex has to have correct coordinates | ||
for (Hex hex : part.getHexes()) { | ||
if (hex.getQ() + hex.getR() + hex.getS() != 0) { | ||
errors.add("Every hex has to have correct coordinates!"); | ||
break; | ||
} | ||
} | ||
|
||
// must include center hex | ||
Optional<Hex> centerHex = part.getHexes().stream().filter(hex -> hex.getQ() == 0 && hex.getR() == 0 && hex.getS() == 0).findFirst(); | ||
if (centerHex.isEmpty()) { | ||
errors.add("Part must include a center hex!"); | ||
return errors; | ||
} | ||
|
||
// all hexes must be connected | ||
Navigation navigation = new Navigation(part); | ||
|
||
for (Hex hex : part.getHexes()) { | ||
if (hex == centerHex.get()) | ||
continue; | ||
|
||
if (navigation.getPath(centerHex.get(), hex) == null) { | ||
errors.add("All hexes must be connected!"); | ||
break; | ||
} | ||
} | ||
|
||
return errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.