Skip to content

Commit

Permalink
Merge branch 'master' into integration
Browse files Browse the repository at this point in the history
  • Loading branch information
cam72cam committed Sep 13, 2023
2 parents 003279b + f1458c8 commit bec7f28
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@
import cam72cam.mod.net.Packet;
import cam72cam.mod.net.PacketDirection;
import cam72cam.mod.serialization.TagField;
import cam72cam.mod.world.World;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Comparator;
import java.util.Objects;
import java.util.UUID;

public class ClientPartDragging {
private EntityRollingStock stock = null;
private Control<?> component = null;
private EntityRollingStock interactingStock = null;
private Control<?> interactingControl = null;
private Float lastValue = null;

private EntityRollingStock targetStock = null;
private Interactable<?> targetInteractable = null;

public static void register() {
ClientPartDragging dragger = new ClientPartDragging();
Mouse.registerDragHandler(dragger::capture);
Expand All @@ -31,47 +35,16 @@ public static void register() {
}

private boolean capture(Player.Hand hand) {

if (hand == Player.Hand.SECONDARY && MinecraftClient.isReady() && !MinecraftClient.getPlayer().isCrouching()) {
this.stock = null;
Player player = MinecraftClient.getPlayer();
Vec3d look = player.getLookVector();
Vec3d start = player.getPositionEyes();

MinecraftClient.getPlayer().getWorld().getEntities(EntityRollingStock.class).stream()
.filter(stock ->
stock.getPosition().distanceTo(player.getPositionEyes()) < stock.getDefinition().getLength(stock.gauge)
)
.flatMap(stock ->
stock.getDefinition().getModel().getInteractable().stream().map(c -> Pair.of(stock, c))
).map(p -> {
double padding = 0.2 * p.getLeft().gauge.scale();
Double min = null;
Vec3d center = p.getRight().center(p.getLeft());
IBoundingBox bb = p.getRight().getBoundingBox(p.getLeft()).grow(new Vec3d(padding, padding, padding));
for (double i = 0; i < 3; i += 0.1 * p.getLeft().gauge.scale()) {
Vec3d cast = start.add(look.scale(i));
if (bb.contains(cast)) {
double dist = cast.distanceTo(center);
min = min == null ? dist : Math.min(dist, min);
}
}
return min != null ? Pair.of(min, p) : null;
}).filter(Objects::nonNull)
.min(Comparator.comparingDouble(Pair::getLeft))
.map(Pair::getRight)
.ifPresent(found -> {
Interactable<?> interactable = found.getRight();
if (interactable instanceof Control) {
this.stock = found.getLeft();
this.component = (Control<?>) interactable;
new DragPacket(stock, component, true, 0, false).sendToServer();
}
if (interactable instanceof Seat) {
new SeatPacket((EntityRidableRollingStock) found.getLeft(), (Seat)interactable).sendToServer();
}
});
return stock == null;
if (hand == Player.Hand.SECONDARY && MinecraftClient.isReady() && !MinecraftClient.getPlayer().isCrouching() && targetInteractable != null) {
if (targetInteractable instanceof Control) {
this.interactingStock = targetStock;
this.interactingControl = (Control<?>) targetInteractable;
new DragPacket(interactingStock, interactingControl, true, 0, false).sendToServer();
}
if (targetInteractable instanceof Seat) {
new SeatPacket((EntityRidableRollingStock) interactingStock, (Seat<?>)targetInteractable).sendToServer();
}
return false;
}
return true;
}
Expand Down Expand Up @@ -139,30 +112,69 @@ protected void handle() {
}

private void tick() {
if (stock != null && MinecraftClient.isReady()) {
if (!MinecraftClient.isReady()) {
return;
}

if (interactingStock != null) {
interactingControl.lookedAt = interactingStock.getWorld().getTicks();
if (Mouse.getDrag() == null) {
component.stopClientDragging();
new DragPacket(stock, component, false, 0, true).sendToServer();
stock = null;
component = null;
interactingControl.stopClientDragging();
new DragPacket(interactingStock, interactingControl, false, 0, true).sendToServer();
interactingStock = null;
interactingControl = null;
lastValue = null;
return;
}

if (component.toggle) {
if (interactingControl.toggle) {
return;
}

float newValue = component.clientMovementDelta(MinecraftClient.getPlayer(), stock) + stock.getControlPosition(component);
float newValue = interactingControl.clientMovementDelta(MinecraftClient.getPlayer(), interactingStock) + interactingStock.getControlPosition(interactingControl);
if (lastValue != null && Math.abs(lastValue - newValue) < 0.001) {
return;
}

// Server will override this, but for now it allows the client to see the active changes.
stock.setControlPosition(component, newValue);
interactingStock.setControlPosition(interactingControl, newValue);

new DragPacket(stock, component, false, newValue, false).sendToServer();
new DragPacket(interactingStock, interactingControl, false, newValue, false).sendToServer();
lastValue = newValue;
} else {
targetStock = null;
targetInteractable = null;

Player player = MinecraftClient.getPlayer();
Vec3d look = player.getLookVector();
Vec3d start = player.getPositionEyes();
World world = player.getWorld();

Double min = null;

for (EntityRollingStock stock : world.getEntities(EntityRollingStock.class)) {
if (stock.getPosition().distanceToSquared(player.getPosition()) > stock.getDefinition().getLength(stock.gauge) * stock.getDefinition().getLength(stock.gauge)) {
continue;
}

double padding = 0.05 * stock.gauge.scale();
for (Interactable<?> interactable : stock.getDefinition().getModel().getInteractable()) {
IBoundingBox bb = interactable.getBoundingBox(stock).grow(new Vec3d(padding, padding, padding));
for (double i = 0.125; i < 3; i += 0.05 * stock.gauge.scale()) {
Vec3d cast = start.add(look.scale(i));
if (bb.contains(cast)) {
interactable.lookedAt = MinecraftClient.getPlayer().getWorld().getTicks();
// This is a weird check, but it seems to work
double dist = i * interactable.center(stock).distanceTo(cast);
if (min == null || min > dist) {
min = dist;
targetStock = stock;
targetInteractable = interactable;
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cam72cam.immersiverailroading.model.part;

import cam72cam.immersiverailroading.ConfigGraphics;
import cam72cam.immersiverailroading.ConfigSound;
import cam72cam.immersiverailroading.entity.EntityMoveableRollingStock;
import cam72cam.immersiverailroading.entity.EntityRollingStock;
import cam72cam.immersiverailroading.library.ModelComponentType;
Expand All @@ -18,6 +17,7 @@
import cam72cam.mod.model.obj.OBJGroup;
import cam72cam.mod.render.GlobalRender;
import cam72cam.mod.render.opengl.RenderState;
import cam72cam.mod.text.TextColor;
import cam72cam.mod.util.Axis;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.text.WordUtils;
Expand Down Expand Up @@ -212,31 +212,9 @@ public void postRender(T stock, RenderState state) {
return;
}

Player player = MinecraftClient.getPlayer();

boolean overrideRange = stock.getControlPressed(this);
if (!overrideRange) {
if (transform(center, stock).distanceTo(player.getPositionEyes().add(stock.getVelocity())) > 4) {
return;
}

IBoundingBox bb = IBoundingBox.from(
transform(part.min, stock),
transform(part.max, stock)
).grow(new Vec3d(0.05, 0.05, 0.05));
// The added velocity is due to a bug where the player may tick before or after the stock.
// Ideally we'd be able to fix this in UMC and have all UMC entities tick after the main entities
// or at least expose a "tick order" function as crappy as that would be...

boolean inRange = false;
Vec3d delta = bb.max().subtract(bb.min());
double step = Math.max(0.01, Math.min(delta.x, Math.min(delta.y, delta.z)) / 2);
for (double i = 0; i < 2; i += step) {
inRange = inRange || bb.contains(player.getPositionEyes().add(player.getLookVector().scale(i)).add(stock.getVelocity()));
}
if (!inRange) {
return;
}
boolean isPressed = stock.getControlPressed(this);
if (!isPressed && lookedAt != stock.getWorld().getTicks()) {
return;
}

Matrix4 m = this.state.getGroupMatrix(stock, modelId);
Expand Down Expand Up @@ -277,7 +255,11 @@ public void postRender(T stock, RenderState state) {
labelstate = String.format(" (%d%%)", (int)(percent * 100));
}
}
GlobalRender.drawText((label != null ? label : formatLabel(part.type)) + labelstate, state, pos, 0.2f, 180 - stock.getRotationYaw() - 90);
String str = (label != null ? label : formatLabel(part.type)) + labelstate;
if (isPressed) {
str = TextColor.BOLD.wrap(str);
}
GlobalRender.drawText(str, state, pos, 0.2f, 180 - stock.getRotationYaw() - 90);
}

public float getValue(EntityMoveableRollingStock stock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

public class Interactable<T extends EntityMoveableRollingStock> {
public final ModelComponent part;
public long lookedAt;

public Interactable(ModelComponent part) {
this.part = part;
Expand Down

0 comments on commit bec7f28

Please sign in to comment.