Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracked Locations on Crafts #616

Merged
merged 14 commits into from
Jul 28, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.countercraft.movecraft.Movecraft;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.MovecraftRotation;
import net.countercraft.movecraft.TrackedLocation;
import net.countercraft.movecraft.async.AsyncTask;
import net.countercraft.movecraft.config.Settings;
import net.countercraft.movecraft.craft.Craft;
Expand Down Expand Up @@ -51,6 +52,7 @@
import org.bukkit.inventory.InventoryView;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import static net.countercraft.movecraft.util.MathUtils.withinWorldBorder;
Expand Down Expand Up @@ -167,6 +169,12 @@ protected void execute() {
parentCraft.getFluidLocations().addAll(newFluidList);
}

//Rotates the craft's tracked locations.
for (Set<TrackedLocation> locations : craft.getTrackedLocations().values()) {
for (TrackedLocation location : locations) {
location.rotate(rotation, originPoint);
}
}

updates.add(new CraftRotateCommand(getCraft(),originPoint, rotation));
//rotate entities in the craft
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.countercraft.movecraft.Movecraft;
import net.countercraft.movecraft.MovecraftChunk;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.TrackedLocation;
import net.countercraft.movecraft.async.AsyncTask;
import net.countercraft.movecraft.config.Settings;
import net.countercraft.movecraft.craft.ChunkManager;
Expand Down Expand Up @@ -58,8 +59,8 @@
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.countercraft.movecraft.Movecraft;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.MovecraftRotation;
import net.countercraft.movecraft.TrackedLocation;
import net.countercraft.movecraft.async.rotation.RotationTask;
import net.countercraft.movecraft.async.translation.TranslationTask;
import net.countercraft.movecraft.config.Settings;
Expand All @@ -25,6 +26,7 @@
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
Expand Down Expand Up @@ -78,6 +80,7 @@ public abstract class BaseCraft implements Craft {
private String name = "";
@NotNull
private MovecraftLocation lastTranslation = new MovecraftLocation(0, 0, 0);
private Map<NamespacedKey, Set<TrackedLocation>> trackedLocations = new HashMap<>();

public BaseCraft(@NotNull CraftType type, @NotNull World world) {
this.type = type;
Expand Down Expand Up @@ -598,4 +601,7 @@ public void setTotalFuel(double fuel) {
public double getTotalFuel() {
return totalFuel;
}

@Override
public Map<NamespacedKey, Set<TrackedLocation>> getTrackedLocations() {return trackedLocations;}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package net.countercraft.movecraft;

import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.util.MathUtils;
import org.jetbrains.annotations.NotNull;

public class TrackedLocation {
private MovecraftLocation offSet;
private final Craft craft;

/**
* Creates a new TrackedLocation instance which tracks a location about a craft's midpoint.
* @param craft The craft that's that tied to the location.
* @param location The absolute position to track. This location will be stored as a relative
* location to the craft's central hitbox.
*/
public TrackedLocation(@NotNull Craft craft, @NotNull MovecraftLocation location) {
this.craft = craft;
MovecraftLocation midPoint = craft.getHitBox().getMidPoint();
offSet = new MovecraftLocation(
goodroach marked this conversation as resolved.
Show resolved Hide resolved
location.getX() - midPoint.getX(),
location.getY() - midPoint.getY(),
location.getZ() - midPoint.getZ()
);
}

/**
* Rotates the stored location about any point.
* @param rotation A clockwise or counter-clockwise direction to rotate.
* @param pivot An absolute position at which to rotate the trackedLocation.
*/
public void rotate(MovecraftRotation rotation, MovecraftLocation pivot) {
//This is the position vector relative to the tracked location to the pivot.
MovecraftLocation offSetToPivot = new MovecraftLocation(
getAbsoluteLocation().getX() - pivot.getX(),
getAbsoluteLocation().getY() - pivot.getY(),
getAbsoluteLocation().getZ() - pivot.getZ()
);
//This is the position vector relative to the midpoint to the pivot.
MovecraftLocation midPoint = craft.getHitBox().getMidPoint();
MovecraftLocation midPointToPivot = new MovecraftLocation(
midPoint.getX() - pivot.getX(),
midPoint.getY() - pivot.getY(),
midPoint.getZ() - pivot.getZ()
);
//Rotates both vectors
MovecraftLocation rotatedOffSetToPivot = MathUtils.rotateVec(rotation, offSetToPivot);
MovecraftLocation rotatedMidPointToPivot = MathUtils.rotateVec(rotation, midPointToPivot);
//Subtracts rotatedOffSetToPivot - rotatedMidPointToPivot to get the new offset to the midpoint.
offSet = new MovecraftLocation(
rotatedOffSetToPivot.getX() - rotatedMidPointToPivot.getX(),
rotatedOffSetToPivot.getY() - rotatedMidPointToPivot.getY(),
rotatedOffSetToPivot.getZ() - rotatedMidPointToPivot.getZ()
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, if a turret were to rotate in the craft, would the craft be able to track that as well? Or is that not worth going for right now?

}

/**
* Gets the stored location.
* @return Returns the location.
*/
public MovecraftLocation getAbsoluteLocation() {
MovecraftLocation midPoint = craft.getHitBox().getMidPoint();
return new MovecraftLocation(
offSet.getX() + midPoint.getX(),
offSet.getY() + midPoint.getY(),
offSet.getZ() + midPoint.getZ()
);
goodroach marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.countercraft.movecraft.CruiseDirection;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.MovecraftRotation;
import net.countercraft.movecraft.TrackedLocation;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.processing.MovecraftWorld;
import net.countercraft.movecraft.util.Counter;
Expand All @@ -28,12 +29,11 @@
import net.kyori.adventure.audience.Audience;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Sign;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -263,4 +263,6 @@ default void setLastDZ(int dZ){}
double getTotalFuel ();

void setTotalFuel (double fuel);

Map<NamespacedKey, Set<TrackedLocation>> getTrackedLocations();
}