Skip to content

Commit

Permalink
Fix dynamic lag speed typo and cleanup file
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerS1066 committed Jan 3, 2022
1 parent f9ffa34 commit d719822
Showing 1 changed file with 79 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.countercraft.movecraft.processing.CachedMovecraftWorld;
import net.countercraft.movecraft.processing.MovecraftWorld;
import net.countercraft.movecraft.processing.WorldManager;
import net.countercraft.movecraft.processing.tasks.detection.DetectionTask;
import net.countercraft.movecraft.util.Counter;
import net.countercraft.movecraft.util.Tags;
import net.countercraft.movecraft.util.TimingData;
Expand All @@ -35,7 +34,6 @@
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -47,32 +45,44 @@
import static net.countercraft.movecraft.util.SignUtils.getFacing;

public abstract class BaseCraft implements Craft {
@NotNull protected final CraftType type;
@NotNull protected HitBox hitBox;
@NotNull protected final MutableHitBox collapsedHitBox;
@NotNull protected MutableHitBox fluidLocations;
@NotNull protected final Counter<Material> materials;
@NotNull protected World w;
@NotNull private final AtomicBoolean processing = new AtomicBoolean();
@NotNull
protected final CraftType type;
@NotNull
protected final MutableHitBox collapsedHitBox;
@NotNull
protected final Counter<Material> materials;
@NotNull
private final AtomicBoolean processing = new AtomicBoolean();
private final long origPilotTime;
@NotNull
private final Map<Location, BlockData> phaseBlocks = new HashMap<>();
@NotNull
protected HitBox hitBox;
@NotNull
protected MutableHitBox fluidLocations;
@NotNull
protected World w;
@NotNull TimingData stats = new TimingData();
private boolean cruising;
private boolean sinking;
private boolean disabled;
private CruiseDirection cruiseDirection;
private long lastCruiseUpdate;
private long lastBlockCheck;
private long lastRotateTime=0;
private final long origPilotTime;
private long lastRotateTime = 0;
private long lastTeleportTime;
private int lastDX, lastDY, lastDZ;
private int currentGear = 1;
private double burningFuel;
private int origBlockCount;
@Nullable private Player notificationPlayer;
@NotNull private Audience audience;
@NotNull private final Map<Location, BlockData> phaseBlocks = new HashMap<>();
@NotNull private String name = "";
@NotNull TimingData stats = new TimingData();
@NotNull private MovecraftLocation lastTranslation = new MovecraftLocation(0,0,0);
@Nullable
private Player notificationPlayer;
@NotNull
private Audience audience;
@NotNull
private String name = "";
@NotNull
private MovecraftLocation lastTranslation = new MovecraftLocation(0, 0, 0);

public BaseCraft(@NotNull CraftType type, @NotNull World world) {
this.type = type;
Expand Down Expand Up @@ -102,7 +112,7 @@ public HitBox getHitBox() {
return hitBox;
}

public void setHitBox(@NotNull HitBox hitBox){
public void setHitBox(@NotNull HitBox hitBox) {
this.hitBox = hitBox;
}

Expand All @@ -112,13 +122,13 @@ public CraftType getType() {
}

@NotNull
public MovecraftWorld getMovecraftWorld(){
public MovecraftWorld getMovecraftWorld() {
return CachedMovecraftWorld.of(w);
}

@NotNull
public World getWorld() {
if(WorldManager.INSTANCE.isRunning() && !Bukkit.isPrimaryThread()){
if (WorldManager.INSTANCE.isRunning() && !Bukkit.isPrimaryThread()) {
var exception = new Throwable("Invoking most methods on worlds while the world manager is running WILL cause deadlock.");
Bukkit.getLogger().log(Level.SEVERE, exception, exception::getMessage);
}
Expand All @@ -137,11 +147,11 @@ public void translate(int dx, int dy, int dz) {
@Override
public void translate(@NotNull World world, int dx, int dy, int dz) {
var v = type.getObjectProperty(CraftType.DISABLE_TELEPORT_TO_WORLDS);
if(!(v instanceof Collection<?>))
if (!(v instanceof Collection<?>))
throw new IllegalStateException("DISABLE_TELEPORT_TO_WORLDS must be of type Collection");
var disableTeleportToWorlds = ((Collection<?>) v);
disableTeleportToWorlds.forEach(i -> {
if(!(i instanceof String))
if (!(i instanceof String))
throw new IllegalStateException("Values in DISABLE_TELEPORT_TO_WORLDS must be of type String");
});

Expand Down Expand Up @@ -171,7 +181,7 @@ public void translate(@NotNull World world, int dx, int dy, int dz) {

@Override
public void rotate(MovecraftRotation rotation, MovecraftLocation originPoint) {
if(getLastRotateTime()+1e9>System.nanoTime()){
if (getLastRotateTime() + 1e9 > System.nanoTime()) {
getAudience().sendMessage(I18nSupport.getInternationalisedComponent("Rotation - Turning Too Quickly"));
return;
}
Expand All @@ -186,6 +196,7 @@ public void rotate(MovecraftRotation rotation, MovecraftLocation originPoint, bo

/**
* Gets the crafts that have made contact with this craft
*
* @return a set of crafts on contact with this craft
*/
@NotNull
Expand All @@ -197,7 +208,7 @@ public Set<Craft> getContacts() {
MovecraftLocation tcenter = contact.getHitBox().getMidPoint();
int distsquared = ccenter.distanceSquared(tcenter);
double detectionMultiplier;
if(tcenter.getY() > 65) // TODO: fix the water line
if (tcenter.getY() > 65) // TODO: fix the water line
detectionMultiplier = (double) contact.getType().getPerWorldProperty(CraftType.PER_WORLD_DETECTION_MULTIPLIER, contact.getWorld());
else
detectionMultiplier = (double) contact.getType().getPerWorldProperty(CraftType.PER_WORLD_UNDERWATER_DETECTION_MULTIPLIER, contact.getWorld());
Expand All @@ -222,26 +233,26 @@ public void resetSigns(@NotNull Sign clicked) {
if (sign.equals(clicked)) {
continue;
}
if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Cruise: ON")){
if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Cruise: ON")) {
sign.setLine(0, "Cruise: OFF");
}
else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Cruise: OFF")
&& ChatColor.stripColor(clicked.getLine(0)).equalsIgnoreCase("Cruise: ON")
&& getFacing(sign) == getFacing(clicked)) {
sign.setLine(0,"Cruise: ON");
sign.setLine(0, "Cruise: ON");
}
else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Ascend: ON")){
else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Ascend: ON")) {
sign.setLine(0, "Ascend: OFF");
}
else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Ascend: OFF")
&& ChatColor.stripColor(clicked.getLine(0)).equalsIgnoreCase("Ascend: ON")){
&& ChatColor.stripColor(clicked.getLine(0)).equalsIgnoreCase("Ascend: ON")) {
sign.setLine(0, "Ascend: ON");
}
else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Descend: ON")){
else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Descend: ON")) {
sign.setLine(0, "Descend: OFF");
}
else if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Descend: OFF")
&& ChatColor.stripColor(clicked.getLine(0)).equalsIgnoreCase("Descend: ON")){
&& ChatColor.stripColor(clicked.getLine(0)).equalsIgnoreCase("Descend: ON")) {
sign.setLine(0, "Descend: ON");
}
sign.update();
Expand All @@ -268,7 +279,7 @@ public boolean getSinking() {
public void sink() {
CraftSinkEvent event = new CraftSinkEvent(this);
Bukkit.getServer().getPluginManager().callEvent(event);
if(event.isCancelled()){
if (event.isCancelled()) {
return;
}
sinking = true;
Expand All @@ -295,13 +306,13 @@ public void setCruiseDirection(CruiseDirection cruiseDirection) {
}

@Override
public void setLastCruiseUpdate(long update) {
this.lastCruiseUpdate = update;
public long getLastCruiseUpdate() {
return lastCruiseUpdate;
}

@Override
public long getLastCruiseUpdate() {
return lastCruiseUpdate;
public void setLastCruiseUpdate(long update) {
this.lastCruiseUpdate = update;
}

@Override
Expand All @@ -315,13 +326,14 @@ public void setLastBlockCheck(long update) {
}

@Override
public void setLastTranslation(@NotNull MovecraftLocation lastTranslation){
this.lastTranslation = lastTranslation;
@NotNull
public MovecraftLocation getLastTranslation() {
return this.lastTranslation;
}

@Override @NotNull
public MovecraftLocation getLastTranslation(){
return this.lastTranslation;
@Override
public void setLastTranslation(@NotNull MovecraftLocation lastTranslation) {
this.lastTranslation = lastTranslation;
}

@Override
Expand All @@ -344,7 +356,8 @@ public void setOrigBlockCount(int origBlockCount) {
this.origBlockCount = origBlockCount;
}

@Nullable @Deprecated
@Nullable
@Deprecated
public Player getNotificationPlayer() {
return notificationPlayer;
}
Expand All @@ -365,55 +378,59 @@ public double getMeanCruiseTime() {
}

@Override
public void addCruiseTime(float cruiseTime){
public void addCruiseTime(float cruiseTime) {
stats.accept(cruiseTime);
}

@Override
public int getTickCooldown() {
if(sinking)
if (sinking)
return type.getIntProperty(CraftType.SINK_RATE_TICKS);

// Counter<Material> counter = new Counter<>();
// Map<Material, Integer> counter = new HashMap<>();
if(materials.isEmpty()){
for(MovecraftLocation location : hitBox){
if (materials.isEmpty()) {
for (MovecraftLocation location : hitBox) {
materials.add(location.toBukkit(w).getBlock().getType());
}
}

int chestPenalty = 0;
for(Material m : Tags.CHESTS) {
for (Material m : Tags.CHESTS) {
chestPenalty += materials.get(m);
}
chestPenalty *= type.getDoubleProperty(CraftType.CHEST_PENALTY);
if(!cruising)
if (!cruising)
return ((int) type.getPerWorldProperty(CraftType.PER_WORLD_TICK_COOLDOWN, w) + chestPenalty) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1);

// Ascent or Descent
if(cruiseDirection == CruiseDirection.UP || cruiseDirection == CruiseDirection.DOWN) {
if (cruiseDirection == CruiseDirection.UP || cruiseDirection == CruiseDirection.DOWN)
return ((int) type.getPerWorldProperty(CraftType.PER_WORLD_VERT_CRUISE_TICK_COOLDOWN, w) + chestPenalty) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1);
}

// Dynamic Fly Block Speed
int cruiseTickCooldown = (int) type.getPerWorldProperty(CraftType.PER_WORLD_CRUISE_TICK_COOLDOWN, w);
if(type.getDoubleProperty(CraftType.DYNAMIC_FLY_BLOCK_SPEED_FACTOR) != 0){
if (type.getDoubleProperty(CraftType.DYNAMIC_FLY_BLOCK_SPEED_FACTOR) != 0) {
EnumSet<Material> flyBlockMaterials = type.getMaterialSetProperty(CraftType.DYNAMIC_FLY_BLOCK);
double count = 0;
for(Material m : flyBlockMaterials) {
for (Material m : flyBlockMaterials) {
count += materials.get(m);
}
double ratio = count / hitBox.size();
return Math.max((int)Math.round((20.0 * ((int) type.getPerWorldProperty(CraftType.PER_WORLD_CRUISE_SKIP_BLOCKS, w) + 1)) / ((type.getDoubleProperty(CraftType.DYNAMIC_FLY_BLOCK_SPEED_FACTOR) * 1.5) * (ratio - .5) + (20.0 / cruiseTickCooldown) + 1)) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1), 1);
double speed = (type.getDoubleProperty(CraftType.DYNAMIC_FLY_BLOCK_SPEED_FACTOR) * 1.5)
* (ratio - 0.5)
+ (20.0 / cruiseTickCooldown) + 1;
return Math.max((int) Math.round((20.0 * ((int) type.getPerWorldProperty(CraftType.PER_WORLD_CRUISE_SKIP_BLOCKS, w) + 1)) / speed) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1), 1);
}

if(type.getDoubleProperty(CraftType.DYNAMIC_FLY_BLOCK_SPEED_FACTOR) == 0.0 || type.getDoubleProperty(CraftType.DYNAMIC_LAG_POWER_FACTOR) == 0.0 || Math.abs(type.getDoubleProperty(CraftType.DYNAMIC_LAG_POWER_FACTOR)) > 1.0)
if (type.getDoubleProperty(CraftType.DYNAMIC_LAG_SPEED_FACTOR) == 0.0
|| type.getDoubleProperty(CraftType.DYNAMIC_LAG_POWER_FACTOR) == 0.0
|| Math.abs(type.getDoubleProperty(CraftType.DYNAMIC_LAG_POWER_FACTOR)) > 1.0)
return (cruiseTickCooldown + chestPenalty) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1);
if(stats.getCount() == 0)
if (stats.getCount() == 0)
return (int) Math.round(20.0 * ((cruiseTickCooldown + 1.0) / type.getDoubleProperty(CraftType.DYNAMIC_LAG_MIN_SPEED)) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1));

int cruiseSkipBlocks = (int) type.getPerWorldProperty(CraftType.PER_WORLD_CRUISE_SKIP_BLOCKS, w);
if(Settings.Debug) {
if (Settings.Debug) {
Bukkit.getLogger().info("Skip: " + cruiseSkipBlocks);
Bukkit.getLogger().info("Tick: " + cruiseTickCooldown);
Bukkit.getLogger().info("SpeedFactor: " + type.getDoubleProperty(CraftType.DYNAMIC_LAG_SPEED_FACTOR));
Expand All @@ -426,17 +443,18 @@ public int getTickCooldown() {
double speed = 20.0 * (cruiseSkipBlocks + 1.0) / (float) cruiseTickCooldown;
speed -= type.getDoubleProperty(CraftType.DYNAMIC_LAG_SPEED_FACTOR) * Math.pow(getMeanCruiseTime() * 1000.0, type.getDoubleProperty(CraftType.DYNAMIC_LAG_POWER_FACTOR));
speed = Math.max(type.getDoubleProperty(CraftType.DYNAMIC_LAG_MIN_SPEED), speed);
return (int)Math.round((20.0 * (cruiseSkipBlocks + 1.0)) / speed) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1);
return (int) Math.round((20.0 * (cruiseSkipBlocks + 1.0)) / speed) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1);
//In theory, the chest penalty is not needed for a DynamicLag craft.
}

/**
* gets the speed of a craft in blocks per second.
*
* @return the speed of the craft
*/
@Override
public double getSpeed() {
if(cruiseDirection == CruiseDirection.UP || cruiseDirection == CruiseDirection.DOWN) {
if (cruiseDirection == CruiseDirection.UP || cruiseDirection == CruiseDirection.DOWN) {
return 20 * ((int) type.getPerWorldProperty(CraftType.PER_WORLD_VERT_CRUISE_SKIP_BLOCKS, w) + 1) / (double) getTickCooldown();
}
else {
Expand All @@ -455,7 +473,7 @@ public void setLastRotateTime(long lastRotateTime) {
}

@Override
public int getWaterLine(){
public int getWaterLine() {
//TODO: Remove this temporary system in favor of passthrough blocks
// Find the waterline from the surrounding terrain or from the static level in the craft type
int waterLine = 0;
Expand Down Expand Up @@ -509,7 +527,7 @@ public int getWaterLine(){
}

@Override
public @NotNull Map<Location, BlockData> getPhaseBlocks(){
public @NotNull Map<Location, BlockData> getPhaseBlocks() {
return phaseBlocks;
}

Expand Down Expand Up @@ -563,12 +581,12 @@ public void setCurrentGear(int currentGear) {

@Override
@NotNull
public Audience getAudience(){
public Audience getAudience() {
return audience;
}

@Override
public void setAudience(@NotNull Audience audience){
public void setAudience(@NotNull Audience audience) {
this.audience = audience;
}
}

0 comments on commit d719822

Please sign in to comment.