Skip to content

Commit

Permalink
gps foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Nov 2, 2023
1 parent 55c1c9a commit d16a85b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp-brotli:4.10.0'

implementation 'com.github.ben-manes.caffeine:caffeine:3.1.5'
implementation 'com.github.jafarlihi:eemit:0.1.0'
//#endregion

//#region Implementations for 'test'
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/io/github/emcw/EMCMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.emcw.map.api.Players;
import io.github.emcw.map.api.Residents;
import io.github.emcw.map.api.Towns;
import io.github.emcw.map.api.GPS;

import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -14,20 +15,22 @@
import java.util.concurrent.TimeUnit;

public class EMCMap {
@Getter
final String mapName;
@Getter final String mapName;

@Setter(AccessLevel.PRIVATE) public Towns Towns = null;
@Setter(AccessLevel.PRIVATE) public Nations Nations = null;
@Setter(AccessLevel.PRIVATE) public Players Players = null;
@Setter(AccessLevel.PRIVATE) public Residents Residents = null;
@Setter(AccessLevel.PRIVATE) public GPS GPS = null;

final CacheOptions lazyOpts = new CacheOptions(2, TimeUnit.SECONDS, CacheStrategy.LAZY);
final CacheOptions timedOpts = new CacheOptions(3, TimeUnit.MINUTES, CacheStrategy.TIME_BASED);

public EMCMap(String mapName) {
this.mapName = mapName;
initCaches();

setGPS(new GPS(this));
}

public EMCMap(String mapName, CacheOptions mapDataCache, CacheOptions playerDataCache) {
Expand All @@ -43,6 +46,8 @@ public EMCMap(String mapName, CacheOptions mapDataCache, CacheOptions playerData
setPlayers(new Players(this, playerDataCache));

if (prefill) prefill();

setGPS(new GPS(this));
}

private void initCaches() {
Expand Down
82 changes: 81 additions & 1 deletion src/main/java/io/github/emcw/map/api/GPS.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,90 @@
package io.github.emcw.map.api;

import io.github.emcw.EMCMap;
import io.github.emcw.map.entities.Location;
import io.github.emcw.map.entities.Nation;

import com.github.jafarlihi.eemit.EventEmitter;
import io.github.emcw.map.entities.Town;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static io.github.emcw.utils.Funcs.collectEntities;
import static io.github.emcw.utils.GsonUtil.streamValues;

record Route(Nation nation, Integer distance, String direction) {}
record RouteType(boolean avoidPvp, boolean avoidPublic) {
public static final RouteType SAFEST = new RouteType(true, true);
public static final RouteType FASTEST = new RouteType(false, false);
public static final RouteType AVOID_PUBLIC = new RouteType(false, true);
public static final RouteType AVOID_PVP = new RouteType(true, false);
}

public class GPS extends EventEmitter<Object> {
private final EMCMap parent;

public class GPS {
private final Location lastLoc = null;
private final Location emittedHidden = null;

public GPS(EMCMap parent) {
this.parent = parent;
}

public GPS track() {
return this;
}

public Route safestRoute(Location loc) {
return findRoute(loc, RouteType.SAFEST);
}

public Route fastestRoute(Location loc) {
return findRoute(loc, RouteType.FASTEST);
}

public Route findRoute(Location loc, RouteType route) {
if (loc == null || !loc.valid()) {
new IllegalArgumentException(
"Cannot find route! Inputted location is invalid:\n" + loc
).printStackTrace();
}

Map<String, Town> towns = this.parent.Towns.all();
Map<String, Nation> nations = this.parent.Nations.all();

Map<String, Nation> filtered = collectEntities(streamValues(nations).filter(nation -> {
Town capital = nation.getCapital();
if (towns.containsKey(capital.getName())) return false;

Town.Flags flags = capital.getFlags();

boolean PVP = route.avoidPvp() && flags.PVP;
boolean capitalIsPublic = route.avoidPublic() && !flags.PUBLIC;

return !PVP && !capitalIsPublic;
}));

return new Route(null, 0, "");
}

static String cardinalDirection(Location origin, Location destination) {
int deltaX = origin.getX() - destination.getX();
int deltaZ = origin.getZ() - destination.getZ();

double angle = Math.atan2(deltaZ, deltaX) * 180 / Math.PI;

// Determine the cardinal direction
if (angle >= -45 && angle < 45)
return "east";

if (angle >= 45 && angle < 135)
return "north";

if (angle >= 135 || angle < -135)
return "west";

return "south";
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/github/emcw/map/entities/Town.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ void init(JsonObject obj) {
outline = getColour(keyAsStr(obj, "outline"));
}

static class Flags {
final Boolean PVP, EXPLOSIONS, FIRE, CAPITAL, MOBS, PUBLIC;
public static class Flags {
public final Boolean PVP, EXPLOSIONS, FIRE, CAPITAL, MOBS, PUBLIC;

Flags(JsonObject obj) {
PVP = keyAsBool(obj, "pvp");
Expand Down

0 comments on commit d16a85b

Please sign in to comment.