Skip to content

Commit

Permalink
F*CK I HATE JAVA
Browse files Browse the repository at this point in the history
well its not that bad but WHY does Arraylist::add at index SHIFT THE WHOLE ARRAY AHHH. Anyway now the splash planes wont show D:
  • Loading branch information
Goby56 committed Aug 23, 2024
1 parent 40db6c7 commit 4169c8e
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 177 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/goby56/wakes/config/WakesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class WakesConfig {
"mobs", EffectSpawningRule.ONLY_SIMULATION,
"items", EffectSpawningRule.ONLY_SIMULATION
));
public boolean wakesInRunningWater = false;

// Behaviour
public float wavePropagationFactor = 0.95f;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/goby56/wakes/config/YACLIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ public static Screen createScreen(Screen parent) {
.option(effectSpawningRuleOption("other_players", EffectSpawningRule.ONLY_SIMULATION))
.option(effectSpawningRuleOption("mobs", EffectSpawningRule.ONLY_SIMULATION))
.option(effectSpawningRuleOption("items", EffectSpawningRule.ONLY_SIMULATION))
.option(booleanOption("wakes_in_running_water", false)
.binding(false, () -> config.wakesInRunningWater, val -> config.wakesInRunningWater = val)
.build())
.option(booleanOption("spawn_particles", false)
.binding(true, () -> config.spawnParticles, val -> config.spawnParticles = val)
.build())
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/goby56/wakes/debug/DebugCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static int spawnWakeNode(CommandContext<FabricClientCommandSource> cmdCtx
if (!cmdCtx.getSource().getWorld().getFluidState(new BlockPos((int) pos.x, (int) Math.floor(pos.y), (int) pos.z)).isIn(FluidTags.WATER)) return 0;
WakeNode node = new WakeNode(result.getPos(), 100);
node.floodLevel = cmdCtx.getArgument("flood_level", Integer.class);
node.spawner = "Command";
WakeHandler.getInstance().insert(node);
return 1;
}
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/goby56/wakes/debug/WakeDebugRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,15 @@ public class WakeDebugRenderer implements WorldRenderEvents.DebugRender {
@Override
public void beforeDebugRender(WorldRenderContext context) {
WakeHandler wakeHandler = WakeHandler.getInstance();
Map<Integer, Integer> nodeHeights = new HashMap<>();
Map<Integer, Integer> brickHeights = new HashMap<>();
if (WakesClient.CONFIG_INSTANCE.drawDebugBoxes) {
for (var node : wakeHandler.getVisible(context.frustum(), WakeNode.class)) {
nodeHeights.merge((int) node.height, 1, Integer::sum);
Box box = new Box(node.x, node.height - 0.1f, node.z, node.x + 1, node.height - 0.2f, node.z + 1);
DebugRenderer.drawBox(context.matrixStack(), context.consumers(),
box.offset(context.camera().getPos().negate()),
node.toBox().offset(context.camera().getPos().negate()),
1, 0, 1, 0.5f);
}
for (var brick : wakeHandler.getVisible(context.frustum(), Brick.class)) {
brickHeights.merge((int) brick.pos.y, 1, Integer::sum);
Vec3d pos = brick.pos;
Box box = new Box(pos.x, pos.y - 0.2f, pos.z, pos.x + brick.dim, pos.y - 0.3f, pos.z + brick.dim);
Box box = new Box(pos.x, pos.y - (1 - WakeNode.WATER_OFFSET), pos.z, pos.x + brick.dim, pos.y, pos.z + brick.dim);
var col = Color.getHSBColor(new Random(pos.hashCode()).nextFloat(), 1f, 1f).getRGBColorComponents(null);
DebugRenderer.drawBox(context.matrixStack(), context.consumers(),
box.offset(context.camera().getPos().negate()),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/goby56/wakes/duck/ProducesWake.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

public interface ProducesWake {
boolean onWaterSurface();
float producingHeight();
void setProducingHeight(float h);
Integer producingWaterLevel();
void setProducingHeight(int y);
Vec3d getPrevPos();
void setPrevPos(Vec3d pos);
Vec3d getNumericalVelocity();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/goby56/wakes/mixin/LilyPadFallMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void onLandedUpon(World world, BlockState state, BlockPos pos, Entity ent
EffectSpawningRule rule = WakesUtils.getEffectRuleFromSource(entity);
ProducesWake wakeProducer = (ProducesWake) entity;
if (rule.simulateWakes) {
wakeProducer.setProducingHeight(pos.getY() + world.getFluidState(pos).getHeight());
wakeProducer.setProducingHeight(pos.getY());
WakesUtils.placeFallSplash(entity);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/goby56/wakes/mixin/WakeSpawnerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class WakeSpawnerMixin implements ProducesWake {
@Unique private Vec3d numericalVelocity = Vec3d.ZERO;
@Unique private double horizontalNumericalVelocity = 0;
@Unique private double verticalNumericalVelocity = 0;
@Unique private Float producingWaterLevel = null;
@Unique private Integer producingWaterLevel = null;
@Unique private SplashPlaneParticle splashPlane;
@Unique private boolean hasRecentlyTeleported = false;

Expand Down Expand Up @@ -68,13 +68,13 @@ public void setPrevPos(Vec3d pos) {
}

@Override
public float producingHeight() {
public Integer producingWaterLevel() {
return this.producingWaterLevel;
}

@Override
public void setProducingHeight(float h) {
this.producingWaterLevel = h;
public void setProducingHeight(int y) {
this.producingWaterLevel = y;
}

@Override
Expand Down Expand Up @@ -114,7 +114,7 @@ private void tick(CallbackInfo info) {

if (this.onWaterSurface && !this.hasRecentlyTeleported) {
if (this.producingWaterLevel == null)
this.producingWaterLevel = WakesUtils.getWaterLevel(this.world, thisEntity);
this.producingWaterLevel = (int) Math.floor(WakesUtils.getWaterLevel(this.world, thisEntity));

Vec3d currPos = new Vec3d(thisEntity.getX(), this.producingWaterLevel, thisEntity.getZ());

Expand All @@ -138,7 +138,7 @@ private void onSwimmingStart(CallbackInfo ci) {
EffectSpawningRule rule = WakesUtils.getEffectRuleFromSource(thisEntity);
if (rule.simulateWakes) {
if (this.producingWaterLevel == null)
this.producingWaterLevel = WakesUtils.getWaterLevel(this.world, thisEntity);
this.producingWaterLevel = (int) Math.floor(WakesUtils.getWaterLevel(this.world, thisEntity));
WakesUtils.placeFallSplash(((Entity) (Object) this));
}
// TODO ADD WAKE WHEN GETTING OUT OF WATER
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.goby56.wakes.particle.custom;

import com.goby56.wakes.WakesClient;
import com.goby56.wakes.duck.ProducesWake;
import com.goby56.wakes.particle.WithOwnerParticleType;
import com.goby56.wakes.simulation.WakeNode;
Expand Down Expand Up @@ -45,7 +44,7 @@ public void tick() {

if (this.owner instanceof ProducesWake wake) {
SplashPlaneParticle splashPlane = wake.getSplashPlane();
if (splashPlane == null) {
if (splashPlane == null || wake.producingWaterLevel() == null) {
this.markDead();
return;
}
Expand All @@ -56,7 +55,7 @@ public void tick() {

Vec3d particleOffset = new Vec3d(-splashPlane.direction.z, 0f, splashPlane.direction.x).multiply(this.offset * this.owner.getWidth() / 2f);
Vec3d pos = splashPlane.getPos().add(particleOffset).add(splashPlane.direction.multiply(-0.2f));
this.setPos(pos.x, wake.producingHeight(), pos.z);
this.setPos(pos.x, wake.producingWaterLevel(), pos.z);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import net.minecraft.util.math.*;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Random;

public class SplashPlaneParticle extends Particle {
Entity owner;
float yaw;
Expand Down Expand Up @@ -81,7 +78,7 @@ private void aliveTick(ProducesWake wakeProducer) {
this.direction = Vec3d.fromPolar(0, -this.yaw);
Vec3d planeOffset = direction.multiply(this.owner.getWidth() + WakesClient.CONFIG_INSTANCE.splashPlaneOffset);
Vec3d planePos = this.owner.getPos().add(planeOffset);
this.setPos(planePos.x, wakeProducer.producingHeight(), planePos.z);
this.setPos(planePos.x, wakeProducer.producingWaterLevel(), planePos.z);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/goby56/wakes/render/WakeTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.goby56.wakes.render.enums.RenderType;
import com.goby56.wakes.simulation.Brick;
import com.goby56.wakes.simulation.QuadTree;
import com.goby56.wakes.simulation.WakeNode;
import com.mojang.blaze3d.platform.GlConst;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.TextureUtil;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void render(Matrix4f matrix, Camera camera, Brick brick) {

BufferBuilder buffer = Tessellator.getInstance().begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL);

Vector3f pos = brick.pos.add(camera.getPos().negate()).toVector3f();
Vector3f pos = brick.pos.add(camera.getPos().negate()).toVector3f().add(0, WakeNode.WATER_OFFSET, 0);
int light = LightmapTextureManager.MAX_LIGHT_COORDINATE;
buffer.vertex(matrix, pos.x, pos.y, pos.z)
.color(1f, 1f, 1f, 1f)
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/goby56/wakes/simulation/Age.java

This file was deleted.

1 change: 0 additions & 1 deletion src/main/java/com/goby56/wakes/simulation/Brick.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public WakeNode get(int x, int z) {
}

public void insert(WakeNode node) {
if (Math.abs(node.height - this.pos.y) > 1f) System.out.printf("INSERTING %s IN WRONG BRICK (%f)\n", node, this.pos.y);
int x = Math.floorMod(node.x, dim), z = Math.floorMod(node.z, dim);
if (nodes[z][x] != null) {
nodes[z][x].revive(node);
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/com/goby56/wakes/simulation/Position.java

This file was deleted.

3 changes: 1 addition & 2 deletions src/main/java/com/goby56/wakes/simulation/QuadTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class QuadTree {
private final DecentralizedBounds bounds;
private final int depth;
private Brick brick;
private final float yLevel;
public final float yLevel;

public QuadTree(float y) {
this(ROOT_X, y, ROOT_Z, ROOT_WIDTH, 0, null);
Expand Down Expand Up @@ -97,7 +97,6 @@ public <T> void query(Frustum frustum, ArrayList<T> output, Class<T> type) {
return;
}
if (hasLeaf() && brick.occupied > 0) {
// TODO ADD VISIBLE NODES CHECK
if (type.equals(Brick.class)) {
output.add(type.cast(brick));
}
Expand Down
48 changes: 28 additions & 20 deletions src/main/java/com/goby56/wakes/simulation/WakeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class WakeHandler {
private static WakeHandler INSTANCE;
public World world;

private final ArrayList<QuadTree> trees;
private final ArrayList<Queue<WakeNode>> toBeInserted;
private QuadTree[] trees;
private QueueSet<WakeNode>[] toBeInserted;
public boolean resolutionResetScheduled = false;
private final int minY;
private final int maxY;
Expand All @@ -27,11 +27,10 @@ private WakeHandler(ClientWorld world) {
this.minY = world.getBottomY();
this.maxY = world.getTopY();
int worldHeight = this.maxY - this.minY;
this.trees = new ArrayList<>(worldHeight);
this.toBeInserted = new ArrayList<>(worldHeight);
this.trees = new QuadTree[worldHeight];
this.toBeInserted = new QueueSet[worldHeight];
for (int i = 0; i < worldHeight; i++) {
this.trees.add(null);
this.toBeInserted.add(new QueueSet<>());
toBeInserted[i] = new QueueSet<>();
}
}

Expand All @@ -47,16 +46,15 @@ public static WakeHandler getInstance() {

public void tick() {
for (int i = 0; i < this.maxY - this.minY; i++) {
Queue<WakeNode> pendingNodes = this.toBeInserted[i];
if (this.resolutionResetScheduled) {
this.toBeInserted.get(i).clear();
if (pendingNodes != null) pendingNodes.clear();
continue;
}
QuadTree tree = this.trees.get(i);
QuadTree tree = this.trees[i];
if (tree != null) {
tree.tick();

long tInsertion = System.nanoTime();
Queue<WakeNode> pendingNodes = this.toBeInserted.get(i);
while (pendingNodes.peek() != null) {
tree.insert(pendingNodes.poll());
}
Expand All @@ -70,31 +68,40 @@ public void tick() {

public void insert(WakeNode node) {
if (this.resolutionResetScheduled) return;
int i = this.getArrayIndex((int) node.height);
int i = this.getArrayIndex(node.y);
if (i < 0) return;

if (this.trees.get(i) == null) {
this.trees.add(i, new QuadTree(node.height));
if (this.trees[i] == null) {
this.trees[i] = new QuadTree(node.y);
}

this.toBeInserted.get(i).add(node);
if (node.validPos()) {
this.toBeInserted[i].add(node);
}
}

public <T> ArrayList<T> getVisible(Frustum frustum, Class<T> type) {
ArrayList<T> visibleQuads = new ArrayList<>();
for (int i = 0; i < this.maxY - this.minY; i++) {
if (this.trees.get(i) != null) {
this.trees.get(i).query(frustum, visibleQuads, type);
if (this.trees[i] != null) {
this.trees[i].query(frustum, visibleQuads, type);
}
}
return visibleQuads;
}

private int getArrayIndex(int height) {
if (height < this.minY || height > this.maxY) {
private int getArrayIndex(int y) {
if (y < this.minY || y > this.maxY) {
return -1;
}
return height - this.minY;
return y - this.minY;
}

private int getYLevel(int i) {
if (i < 0 || i > this.maxY - this.minY) {
throw new IndexOutOfBoundsException();
}
return i + this.minY;
}

public static void scheduleResolutionChange(Resolution newRes) {
Expand All @@ -114,10 +121,11 @@ private void changeResolution() {

private void reset() {
for (int i = 0; i < this.maxY - this.minY; i++) {
QuadTree tree = this.trees.get(i);
QuadTree tree = this.trees[i];
if (tree != null) {
tree.prune();
}
toBeInserted[i].clear();
}
}

Expand Down
Loading

0 comments on commit 4169c8e

Please sign in to comment.