From 4aba806610039873341b5500102db604e188fdae Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Fri, 29 Jul 2022 23:30:09 +0200 Subject: [PATCH] Fixed block placement bug, and reloading bug. --- .../andrewl/aos_core/model/item/Inventory.java | 7 +++++++ .../nl/andrewl/aos_core/model/world/World.java | 6 ++++++ .../andrewl/aos2_server/ProjectileManager.java | 2 +- .../main/java/nl/andrewl/aos2_server/Server.java | 4 ++++ .../aos2_server/logic/PlayerActionManager.java | 16 +++++++++++++--- .../andrewl/aos2_server/logic/WorldUpdater.java | 5 +++-- 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java b/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java index 90ea7ff..e5533f4 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/item/Inventory.java @@ -47,6 +47,13 @@ public class Inventory { return Optional.empty(); } + public int getIndex(ItemStack stack) { + for (int i = 0; i < itemStacks.size(); i++) { + if (itemStacks.get(i).equals(stack)) return i; + } + return -1; + } + public byte getSelectedBlockValue() { for (var stack : itemStacks) { if (stack instanceof BlockItemStack b) { diff --git a/core/src/main/java/nl/andrewl/aos_core/model/world/World.java b/core/src/main/java/nl/andrewl/aos_core/model/world/World.java index 2691e15..89ea601 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/world/World.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/world/World.java @@ -142,6 +142,12 @@ public class World { return chunkMap.values().stream().mapToInt(c -> c.getPosition().z * Chunk.SIZE + Chunk.SIZE - 1).max().orElse(0); } + public boolean containsPoint(Vector3i pos) { + return pos.x >= getMinX() && pos.x < getMaxX() && + pos.y >= getMinY() && pos.y < getMaxY() && + pos.z >= getMinZ() && pos.z < getMaxZ(); + } + /** * Clears all data from the world. */ diff --git a/server/src/main/java/nl/andrewl/aos2_server/ProjectileManager.java b/server/src/main/java/nl/andrewl/aos2_server/ProjectileManager.java index c982118..7b95d6f 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/ProjectileManager.java +++ b/server/src/main/java/nl/andrewl/aos2_server/ProjectileManager.java @@ -77,7 +77,7 @@ public class ProjectileManager { direction.z += rand.nextGaussian(0, perturbationFactor); Vector3f vel = new Vector3f(direction).normalize() - .mul(200 * MOVEMENT_FACTOR) + .mul(300 * MOVEMENT_FACTOR) .add(player.getVelocity()); ServerProjectile bullet = new ServerProjectile(id, new Vector3f(pos), vel, Projectile.Type.BULLET, player, gun); diff --git a/server/src/main/java/nl/andrewl/aos2_server/Server.java b/server/src/main/java/nl/andrewl/aos2_server/Server.java index 3bcdffb..9743afb 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/Server.java +++ b/server/src/main/java/nl/andrewl/aos2_server/Server.java @@ -26,6 +26,10 @@ import java.nio.file.Path; import java.util.List; import java.util.concurrent.ForkJoinPool; +/** + * The central server, which mainly contains all the different managers and + * other components that make up the server's state and logic. + */ public class Server implements Runnable { private final ServerSocket serverSocket; private final DatagramSocket datagramSocket; diff --git a/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerActionManager.java b/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerActionManager.java index c5b5764..18010e6 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerActionManager.java +++ b/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerActionManager.java @@ -40,6 +40,7 @@ public class PlayerActionManager { private boolean gunNeedsReCock = false; private boolean gunReloading = false; private long gunReloadingStartedAt = 0; + private GunItemStack reloadingItemStack = null; private boolean updated = false; @@ -149,17 +150,23 @@ public class PlayerActionManager { g.setClipCount(g.getClipCount() - 1); gunReloadingStartedAt = now; gunReloading = true; + reloadingItemStack = g; server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(player.getInventory())); server.getPlayerManager().broadcastUdpMessage(new SoundMessage("reload", 1, player.getPosition(), player.getVelocity())); } if (// Check to see if reloading is done. gunReloading && + reloadingItemStack != null && now - gunReloadingStartedAt > gun.getReloadTime() * 1000 ) { - g.setBulletCount(gun.getMaxBulletCount()); + reloadingItemStack.setBulletCount(gun.getMaxBulletCount()); + int idx = player.getInventory().getIndex(reloadingItemStack); + if (idx != -1) { + server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(idx, reloadingItemStack)); + } gunReloading = false; - server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(player.getInventory())); + reloadingItemStack = null; } // Check to see if the player released the trigger, for non-automatic weapons. @@ -195,7 +202,10 @@ public class PlayerActionManager { if (hit != null && !server.getTeamManager().isProtected(hit.pos())) { Vector3i placePos = new Vector3i(hit.pos()); placePos.add(hit.norm()); - if (!isSpaceOccupied(placePos)) { // Ensure that we can't place blocks in space we're occupying. + boolean canPlace = server.getPlayerManager().getPlayers().stream() + .noneMatch(p -> p.isSpaceOccupied(placePos)) && + world.containsPoint(placePos); + if (canPlace) { // Ensure that we can't place blocks in space we're occupying. world.setBlockAt(placePos.x, placePos.y, placePos.z, stack.getSelectedValue()); lastBlockPlacedAt = now; stack.decrementAmount(); diff --git a/server/src/main/java/nl/andrewl/aos2_server/logic/WorldUpdater.java b/server/src/main/java/nl/andrewl/aos2_server/logic/WorldUpdater.java index abd5839..5f4089a 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/logic/WorldUpdater.java +++ b/server/src/main/java/nl/andrewl/aos2_server/logic/WorldUpdater.java @@ -26,8 +26,9 @@ public class WorldUpdater implements Runnable { @Override public void run() { - final long msPerTick = (long) (Math.floor(1.0 / ticksPerSecond) * 1_000); - final long nsPerTick = (long) Math.floor((1.0 / ticksPerSecond) * 1_000_000_000.0); + double secondsPerTick = 1.0 / ticksPerSecond; + final long msPerTick = (long) Math.floor(secondsPerTick * 1_000); + final long nsPerTick = (long) Math.floor(secondsPerTick * 1_000_000_000); System.out.printf("Running world updater at %d ms/tick, or %d ns/tick.%n", msPerTick, nsPerTick); running = true; while (running) {