From 53b8718a06925c00680b1c212f5cc223ceac3219 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sun, 31 Jul 2022 10:59:25 +0200 Subject: [PATCH] Added creative movement. --- .../nl/andrewl/aos_core/model/Player.java | 31 ++ .../nl/andrewl/aos2_server/PlayerManager.java | 2 +- .../logic/CreativeMovementController.java | 58 ++++ .../logic/NormalMovementController.java | 257 +++++++++++++++ .../logic/PlayerActionManager.java | 302 ++---------------- .../logic/PlayerMovementController.java | 10 + .../aos2_server/model/ServerPlayer.java | 1 - server/src/main/resources/redfort.wld | Bin 1665332 -> 1665332 bytes 8 files changed, 380 insertions(+), 281 deletions(-) create mode 100644 server/src/main/java/nl/andrewl/aos2_server/logic/CreativeMovementController.java create mode 100644 server/src/main/java/nl/andrewl/aos2_server/logic/NormalMovementController.java create mode 100644 server/src/main/java/nl/andrewl/aos2_server/logic/PlayerMovementController.java diff --git a/core/src/main/java/nl/andrewl/aos_core/model/Player.java b/core/src/main/java/nl/andrewl/aos_core/model/Player.java index 23bbe99..f5cb440 100644 --- a/core/src/main/java/nl/andrewl/aos_core/model/Player.java +++ b/core/src/main/java/nl/andrewl/aos_core/model/Player.java @@ -2,6 +2,7 @@ package nl.andrewl.aos_core.model; import nl.andrewl.aos_core.Directions; import nl.andrewl.aos_core.MathUtils; +import nl.andrewl.aos_core.model.world.World; import org.joml.*; import org.joml.Math; @@ -194,6 +195,36 @@ public class Player { .translate(-0.35f, getEyeHeight() - 0.4f, 0.35f); } + /** + * Gets the list of all spaces occupied by a player's position, in the + * horizontal XZ plane. This can be between 1 and 4 spaces, depending on + * if the player's position is overlapping with a few blocks. + * @param pos The position. + * @return The list of 2d positions occupied. + */ + private List getHorizontalSpaceOccupied(Vector3f pos) { + // Get the list of 2d x,z coordinates that we overlap with. + List points = new ArrayList<>(4); // Due to the size of radius, there can only be a max of 4 blocks. + int minX = (int) Math.floor(pos.x - RADIUS); + int minZ = (int) Math.floor(pos.z - RADIUS); + int maxX = (int) Math.floor(pos.x + RADIUS); + int maxZ = (int) Math.floor(pos.z + RADIUS); + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { + points.add(new Vector2i(x, z)); + } + } + return points; + } + + public boolean isGrounded(World world) { + // Player must be flat on the top of a block. + if (Math.floor(position.y) != position.y) return false; + // Check to see if there's a block under any of the spaces the player is over. + return getHorizontalSpaceOccupied(position).stream() + .anyMatch(point -> world.getBlockAt(point.x, position.y - 0.1f, point.y) != 0); + } + public List getBlockSpaceOccupied() { float playerBodyMinZ = position.z - RADIUS; float playerBodyMaxZ = position.z + RADIUS; diff --git a/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java b/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java index 662df22..f36b7b9 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java +++ b/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java @@ -47,7 +47,7 @@ public class PlayerManager { joinMessage = username + " joined the game."; } player.setPosition(getBestSpawnPoint(player)); - player.setMode(PlayerMode.NORMAL); + player.setMode(PlayerMode.CREATIVE); // Tell all other players that this one has joined. broadcastTcpMessageToAllBut(new PlayerJoinMessage( player.getId(), player.getUsername(), player.getTeam() == null ? -1 : player.getTeam().getId(), diff --git a/server/src/main/java/nl/andrewl/aos2_server/logic/CreativeMovementController.java b/server/src/main/java/nl/andrewl/aos2_server/logic/CreativeMovementController.java new file mode 100644 index 0000000..b9d0254 --- /dev/null +++ b/server/src/main/java/nl/andrewl/aos2_server/logic/CreativeMovementController.java @@ -0,0 +1,58 @@ +package nl.andrewl.aos2_server.logic; + +import nl.andrewl.aos2_server.Server; +import nl.andrewl.aos2_server.config.ServerConfig; +import nl.andrewl.aos2_server.model.ServerPlayer; +import nl.andrewl.aos_core.model.world.World; +import org.joml.Vector3f; + +public class CreativeMovementController implements PlayerMovementController { + @Override + public boolean tickMovement(float dt, ServerPlayer player, PlayerInputTracker input, Server server, World world, ServerConfig.PhysicsConfig config) { + boolean updated = tickVelocity(player, input, config); + if (player.getVelocity().lengthSquared() > 0) { + Vector3f movement = new Vector3f(player.getVelocity()).mul(dt); + player.getPosition().add(movement); + updated = true; + } + return updated; + } + + private boolean tickVelocity(ServerPlayer player, PlayerInputTracker input, ServerConfig.PhysicsConfig config) { + boolean updated = false; + var velocity = player.getVelocity(); + var orientation = player.getOrientation(); + Vector3f acceleration = new Vector3f(0); + if (input.forward()) acceleration.z -= 1; + if (input.backward()) acceleration.z += 1; + if (input.left()) acceleration.x -= 1; + if (input.right()) acceleration.x += 1; + if (input.jumping()) acceleration.y += 1; + if (input.crouching()) acceleration.y -= 1; + if (acceleration.lengthSquared() > 0) { + acceleration.normalize() + .rotateY(orientation.x) + .mul(config.movementAcceleration); + velocity.add(acceleration); + + float maxSpeed = config.walkingSpeed; + if (input.sprinting()) { + maxSpeed = config.sprintingSpeed; + } + maxSpeed *= 4; + if (velocity.length() > maxSpeed) { + velocity.normalize(maxSpeed); + } + updated = true; + } else if (velocity.lengthSquared() > 0) { + float decel = Math.min(velocity.length(), config.movementDeceleration); + Vector3f deceleration = new Vector3f(velocity).negate().normalize().mul(decel); + velocity.add(deceleration); + if (Math.abs(velocity.x) < 0.1f) velocity.x = 0; + if (Math.abs(velocity.y) < 0.1f) velocity.y = 0; + if (Math.abs(velocity.z) < 0.1f) velocity.z = 0; + updated = true; + } + return updated; + } +} diff --git a/server/src/main/java/nl/andrewl/aos2_server/logic/NormalMovementController.java b/server/src/main/java/nl/andrewl/aos2_server/logic/NormalMovementController.java new file mode 100644 index 0000000..74a9dd9 --- /dev/null +++ b/server/src/main/java/nl/andrewl/aos2_server/logic/NormalMovementController.java @@ -0,0 +1,257 @@ +package nl.andrewl.aos2_server.logic; + +import nl.andrewl.aos2_server.Server; +import nl.andrewl.aos2_server.config.ServerConfig; +import nl.andrewl.aos2_server.model.ServerPlayer; +import nl.andrewl.aos_core.model.item.GunItemStack; +import nl.andrewl.aos_core.model.world.World; +import nl.andrewl.aos_core.net.client.ClientHealthMessage; +import nl.andrewl.aos_core.net.client.SoundMessage; +import org.joml.Math; +import org.joml.Vector3f; + +import java.util.concurrent.ThreadLocalRandom; + +import static nl.andrewl.aos_core.model.Player.RADIUS; + +public class NormalMovementController implements PlayerMovementController { + @Override + public boolean tickMovement(float dt, ServerPlayer player, PlayerInputTracker input, Server server, World world, ServerConfig.PhysicsConfig config) { + boolean updated; + var velocity = player.getVelocity(); + var position = player.getPosition(); + boolean grounded = player.isGrounded(world); + updated = tickHorizontalVelocity(player, input, config, grounded); + + if (grounded) { + if (input.jumping()) { + velocity.y = config.jumpVerticalSpeed * (input.sprinting() ? 1.25f : 1f); + updated = true; + } + } else { + velocity.y -= config.gravity * dt * 2; // Apply double-gravity to players to make the game feel faster. + updated = true; + } + + // Apply updated velocity to the player. + if (velocity.lengthSquared() > 0) { + Vector3f movement = new Vector3f(velocity).mul(dt); + // Check for collisions if we try to move according to what the player wants. + checkBlockCollisions(player, movement, server, world); + position.add(movement); + updated = true; + } + + // Finally, check to see if the player is outside the world, and kill them if so. + if ( + player.getPosition().x < world.getMinX() - 5 || player.getPosition().x > world.getMaxX() + 6 || + player.getPosition().z < world.getMinZ() - 5 || player.getPosition().z > world.getMaxZ() + 6 || + player.getPosition().y < world.getMinY() - 50 || player.getPosition().y > world.getMaxY() + 500 + ) { + server.getPlayerManager().playerKilled(player, null); + } + + return updated; + } + + private boolean tickHorizontalVelocity(ServerPlayer player, PlayerInputTracker input, ServerConfig.PhysicsConfig config, boolean grounded) { + boolean updated = false; + var velocity = player.getVelocity(); + var orientation = player.getOrientation(); + Vector3f horizontalVelocity = new Vector3f( + velocity.x == velocity.x ? velocity.x : 0f, + 0, + velocity.z == velocity.z ? velocity.z : 0f + ); + Vector3f acceleration = new Vector3f(0); + if (input.forward()) acceleration.z -= 1; + if (input.backward()) acceleration.z += 1; + if (input.left()) acceleration.x -= 1; + if (input.right()) acceleration.x += 1; + if (acceleration.lengthSquared() > 0) { + acceleration.normalize(); + acceleration.rotateAxis(orientation.x, 0, 1, 0); + float accelerationMagnitude = config.movementAcceleration; + if (!grounded) accelerationMagnitude *= 0.25f; + acceleration.mul(accelerationMagnitude); + horizontalVelocity.add(acceleration); + float maxSpeed; + if (input.crouching()) { + maxSpeed = config.crouchingSpeed; + } else if (input.sprinting()) { + maxSpeed = config.sprintingSpeed; + } else { + maxSpeed = config.walkingSpeed; + } + // If scoping, then force limit to crouching speed. + if (input.interacting() && player.getInventory().getSelectedItemStack() instanceof GunItemStack) { + maxSpeed = config.crouchingSpeed; + } + if (horizontalVelocity.length() > maxSpeed) { + horizontalVelocity.normalize(maxSpeed); + } + updated = true; + } else if (horizontalVelocity.lengthSquared() > 0) { + float baseDecel = config.movementDeceleration; + if (!grounded) baseDecel *= 0.25f; + float decelerationMagnitude = Math.min(horizontalVelocity.length(), baseDecel); + Vector3f deceleration = new Vector3f(horizontalVelocity).negate().normalize().mul(decelerationMagnitude); + horizontalVelocity.add(deceleration); + if (horizontalVelocity.length() < 0.1f) { + horizontalVelocity.set(0); + } + updated = true; + } + + // Update the player's velocity with what we've computed. + velocity.x = horizontalVelocity.x; + velocity.z = horizontalVelocity.z; + return updated; + } + + private void checkBlockCollisions(ServerPlayer player, Vector3f movement, Server server, World world) { + var position = player.getPosition(); + var velocity = player.getVelocity(); + final Vector3f nextTickPosition = new Vector3f(position).add(movement); + float height = player.getCurrentHeight(); + float delta = 0.00001f; + final Vector3f stepSize = new Vector3f(movement).normalize(1.0f); + // The number of steps we'll make towards the next tick position. + int stepCount = (int) Math.ceil(movement.length()); + if (stepCount == 0) return; // No movement, so exit. + final Vector3f nextPos = new Vector3f(position); + final Vector3f lastPos = new Vector3f(position); + for (int i = 0; i < stepCount; i++) { + lastPos.set(nextPos); + nextPos.add(stepSize); + // If we shot past the next tick position, clamp it to that. + if (new Vector3f(nextPos).sub(position).length() > movement.length()) { + nextPos.set(nextTickPosition); + } + + // Check if we collide with anything at this new position. + + float playerBodyPrevMinZ = lastPos.z - RADIUS; + float playerBodyPrevMaxZ = lastPos.z + RADIUS; + float playerBodyPrevMinX = lastPos.x - RADIUS; + float playerBodyPrevMaxX = lastPos.x + RADIUS; + float playerBodyPrevMinY = lastPos.y; + float playerBodyPrevMaxY = lastPos.y + height; + + float playerBodyMinZ = nextPos.z - RADIUS; + float playerBodyMaxZ = nextPos.z + RADIUS; + float playerBodyMinX = nextPos.x - RADIUS; + float playerBodyMaxX = nextPos.x + RADIUS; + float playerBodyMinY = nextPos.y; + float playerBodyMaxY = nextPos.y + height; + + // Compute the bounds of all blocks the player is intersecting with. + int minX = (int) Math.floor(playerBodyMinX); + int minZ = (int) Math.floor(playerBodyMinZ); + int minY = (int) Math.floor(playerBodyMinY); + int maxX = (int) Math.floor(playerBodyMaxX); + int maxZ = (int) Math.floor(playerBodyMaxZ); + int maxY = (int) Math.floor(playerBodyMaxY); + + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { + for (int y = minY; y <= maxY; y++) { + byte block = world.getBlockAt(x, y, z); + if (block <= 0) continue; // We're not colliding with this block. + float blockMinY = (float) y; + float blockMaxY = (float) y + 1; + float blockMinX = (float) x; + float blockMaxX = (float) x + 1; + float blockMinZ = (float) z; + float blockMaxZ = (float) z + 1; + + /* + To determine if the player is moving into the -Z side of a block: + - The player's max z position went from < blockMinZ to >= blockMinZ. + - The block to the -Z direction is air. + */ + boolean collidingWithNegativeZ = playerBodyPrevMaxZ < blockMinZ && playerBodyMaxZ >= blockMinZ && world.getBlockAt(x, y, z - 1) <= 0; + if (collidingWithNegativeZ) { + position.z = blockMinZ - RADIUS - delta; + velocity.z = 0; + movement.z = 0; + } + + /* + To determine if the player is moving into the +Z side of a block: + - The player's min z position went from >= blockMaxZ to < blockMaxZ. + - The block to the +Z direction is air. + */ + boolean collidingWithPositiveZ = playerBodyPrevMinZ >= blockMaxZ && playerBodyMinZ < blockMaxZ && world.getBlockAt(x, y, z + 1) <= 0; + if (collidingWithPositiveZ) { + position.z = blockMaxZ + RADIUS + delta; + velocity.z = 0; + movement.z = 0; + } + + /* + To determine if the player is moving into the -X side of a block: + - The player's max x position went from < blockMinX to >= blockMinX + - The block to the -X direction is air. + */ + boolean collidingWithNegativeX = playerBodyPrevMaxX < blockMinX && playerBodyMaxX >= blockMinX && world.getBlockAt(x - 1, y, z) <= 0; + if (collidingWithNegativeX) { + position.x = blockMinX - RADIUS - delta; + velocity.x = 0; + movement.x = 0; + } + + /* + To determine if the player is moving into the +X side of a block: + - The player's min x position went from >= blockMaxX to < blockMaxX. + - The block to the +X direction is air. + */ + boolean collidingWithPositiveX = playerBodyPrevMinX >= blockMaxX && playerBodyMinX < blockMaxX && world.getBlockAt(x + 1, y, z) <= 0; + if (collidingWithPositiveX) { + position.x = blockMaxX + RADIUS + delta; + velocity.x = 0; + movement.x = 0; + } + + /* + To determine if the player is moving down onto a block: + - The player's min y position went from >= blockMaxY to < blockMaxY + - The block above the current one is air. + */ + boolean collidingWithFloor = playerBodyPrevMinY >= blockMaxY && playerBodyMinY < blockMaxY && world.getBlockAt(x, y + 1, z) <= 0; + if (collidingWithFloor) { + // This is a special case! We need to check for fall damage. + if (velocity.y < -20) { + float damage = velocity.y / 50f; + player.setHealth(player.getHealth() + damage); + if (player.getHealth() <= 0) { + server.getPlayerManager().playerKilled(player, player); + } else { + var handler = server.getPlayerManager().getHandler(player.getId()); + handler.sendDatagramPacket(new ClientHealthMessage(player.getHealth())); + int soundVariant = ThreadLocalRandom.current().nextInt(1, 4); + handler.sendDatagramPacket(new SoundMessage("hurt_" + soundVariant, 1, player.getPosition())); + } + } + position.y = blockMaxY; + velocity.y = 0; + movement.y = 0; + } + + /* + To determine if the player is moving up into a block: + - The player's y position went from below blockMinY to >= blockMinY + - The block below the current one is air. + */ + boolean collidingWithCeiling = playerBodyPrevMaxY < blockMinY && playerBodyMaxY >= blockMinY && world.getBlockAt(x, y - 1, z) <= 0; + if (collidingWithCeiling) { + position.y = blockMinY - height - delta; + velocity.y = 0; + movement.y = 0; + } + } + } + } + } + } +} 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 e683f3a..764818e 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 @@ -1,8 +1,8 @@ package nl.andrewl.aos2_server.logic; import nl.andrewl.aos2_server.Server; -import nl.andrewl.aos2_server.config.ServerConfig; import nl.andrewl.aos2_server.model.ServerPlayer; +import nl.andrewl.aos_core.model.PlayerMode; import nl.andrewl.aos_core.model.item.BlockItemStack; import nl.andrewl.aos_core.model.item.Gun; import nl.andrewl.aos_core.model.item.GunItemStack; @@ -14,22 +14,19 @@ import nl.andrewl.aos_core.model.world.World; import nl.andrewl.aos_core.net.client.*; import nl.andrewl.aos_core.net.world.ChunkUpdateMessage; import org.joml.Math; -import org.joml.Vector2i; import org.joml.Vector3f; import org.joml.Vector3i; -import java.util.ArrayList; -import java.util.List; import java.util.concurrent.ThreadLocalRandom; -import static nl.andrewl.aos2_server.model.ServerPlayer.RADIUS; - /** * Component that manages a server player's current actions and movement. */ public class PlayerActionManager { private final ServerPlayer player; private final PlayerInputTracker input; + private final PlayerMovementController normalMovementController = new NormalMovementController(); + private final CreativeMovementController creativeMovementController = new CreativeMovementController(); private long lastBlockRemovedAt = 0; private long lastBlockPlacedAt = 0; @@ -91,7 +88,7 @@ public class PlayerActionManager { lastResupplyAt = now; } - if (server.getConfig().actions.healthRegenPerSecond != 0 && player.getHealth() < 1) { + if (player.getMode() == PlayerMode.NORMAL && server.getConfig().actions.healthRegenPerSecond != 0 && player.getHealth() < 1) { player.setHealth(player.getHealth() + server.getConfig().actions.healthRegenPerSecond * dt); server.getPlayerManager().getHandler(player).sendDatagramPacket(new ClientHealthMessage(player.getHealth())); } @@ -101,7 +98,11 @@ public class PlayerActionManager { updated = true; } - tickMovement(dt, server, world, server.getConfig().physics); + updated = switch (player.getMode()) { + case NORMAL -> normalMovementController.tickMovement(dt, player, input, server, world, server.getConfig().physics); + case CREATIVE -> creativeMovementController.tickMovement(dt, player, input, server, world, server.getConfig().physics); + case SPECTATOR -> false; + } || updated; input.reset(); // Reset our input state after processing this tick's player input. } @@ -141,12 +142,16 @@ public class PlayerActionManager { stack.getAmount() < stack.getType().getMaxAmount() && now - lastBlockRemovedAt > server.getConfig().actions.blockBreakCooldown * 1000 ) { - var hit = world.getLookingAtPos(player.getEyePosition(), player.getViewVector(), server.getConfig().actions.blockBreakReach); + float reach = server.getConfig().actions.blockBreakReach; + if (player.getMode() == PlayerMode.CREATIVE) reach *= 10; + var hit = world.getLookingAtPos(player.getEyePosition(), player.getViewVector(), reach); if (hit != null && !server.getTeamManager().isProtected(hit.pos())) { world.setBlockAt(hit.pos().x, hit.pos().y, hit.pos().z, (byte) 0); lastBlockRemovedAt = now; - stack.incrementAmount(); - server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(player.getInventory())); + if (player.getMode() == PlayerMode.NORMAL) { + stack.incrementAmount(); + server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(player.getInventory())); + } server.getPlayerManager().broadcastUdpMessage(ChunkUpdateMessage.fromWorld(hit.pos(), world)); server.getPlayerManager().broadcastUdpMessage(new SoundMessage("block_break_1", 1, player.getPosition())); } @@ -157,7 +162,9 @@ public class PlayerActionManager { stack.getAmount() > 0 && now - lastBlockPlacedAt > server.getConfig().actions.blockPlaceCooldown * 1000 ) { - var hit = world.getLookingAtPos(player.getEyePosition(), player.getViewVector(), server.getConfig().actions.blockPlaceReach); + float reach = server.getConfig().actions.blockPlaceReach; + if (player.getMode() == PlayerMode.CREATIVE) reach *= 10; + var hit = world.getLookingAtPos(player.getEyePosition(), player.getViewVector(), reach); if (hit != null && !server.getTeamManager().isProtected(hit.pos())) { Vector3i placePos = new Vector3i(hit.pos()); placePos.add(hit.norm()); @@ -167,8 +174,10 @@ public class PlayerActionManager { 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(); - server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(player.getInventory())); + if (player.getMode() == PlayerMode.NORMAL) { + stack.decrementAmount(); + server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(player.getInventory())); + } server.getPlayerManager().broadcastUdpMessage(ChunkUpdateMessage.fromWorld(placePos, world)); server.getPlayerManager().broadcastUdpMessage(new SoundMessage("block_place_1", 1, player.getPosition())); } @@ -176,271 +185,6 @@ public class PlayerActionManager { } } - private void tickMovement(float dt, Server server, World world, ServerConfig.PhysicsConfig config) { - var velocity = player.getVelocity(); - var position = player.getPosition(); - boolean grounded = isGrounded(world); - tickHorizontalVelocity(config, grounded); - - if (grounded) { - if (input.jumping()) { - velocity.y = config.jumpVerticalSpeed * (input.sprinting() ? 1.25f : 1f); - updated = true; - } - } else { - velocity.y -= config.gravity * dt * 2; // Apply double-gravity to players to make the game feel faster. - updated = true; - } - - // Apply updated velocity to the player. - if (velocity.lengthSquared() > 0) { - Vector3f movement = new Vector3f(velocity).mul(dt); - // Check for collisions if we try to move according to what the player wants. - checkBlockCollisions(movement, server, world); - position.add(movement); - updated = true; - } - - // Finally, check to see if the player is outside the world, and kill them if so. - if ( - player.getPosition().x < world.getMinX() - 5 || player.getPosition().x > world.getMaxX() + 6 || - player.getPosition().z < world.getMinZ() - 5 || player.getPosition().z > world.getMaxZ() + 6 || - player.getPosition().y < world.getMinY() - 50 || player.getPosition().y > world.getMaxY() + 500 - ) { - server.getPlayerManager().playerKilled(player, null); - } - } - - private void tickHorizontalVelocity(ServerConfig.PhysicsConfig config, boolean grounded) { - var velocity = player.getVelocity(); - var orientation = player.getOrientation(); - Vector3f horizontalVelocity = new Vector3f( - velocity.x == velocity.x ? velocity.x : 0f, - 0, - velocity.z == velocity.z ? velocity.z : 0f - ); - Vector3f acceleration = new Vector3f(0); - if (input.forward()) acceleration.z -= 1; - if (input.backward()) acceleration.z += 1; - if (input.left()) acceleration.x -= 1; - if (input.right()) acceleration.x += 1; - if (acceleration.lengthSquared() > 0) { - acceleration.normalize(); - acceleration.rotateAxis(orientation.x, 0, 1, 0); - float accelerationMagnitude = config.movementAcceleration; - if (!grounded) accelerationMagnitude *= 0.25f; - acceleration.mul(accelerationMagnitude); - horizontalVelocity.add(acceleration); - float maxSpeed; - if (input.crouching()) { - maxSpeed = config.crouchingSpeed; - } else if (input.sprinting()) { - maxSpeed = config.sprintingSpeed; - } else { - maxSpeed = config.walkingSpeed; - } - // If scoping, then force limit to crouching speed. - if (isScopeEnabled()) maxSpeed = config.crouchingSpeed; - if (horizontalVelocity.length() > maxSpeed) { - horizontalVelocity.normalize(maxSpeed); - } - updated = true; - } else if (horizontalVelocity.lengthSquared() > 0) { - float baseDecel = config.movementDeceleration; - if (!grounded) baseDecel *= 0.25f; - float decelerationMagnitude = Math.min(horizontalVelocity.length(), baseDecel); - Vector3f deceleration = new Vector3f(horizontalVelocity).negate().normalize().mul(decelerationMagnitude); - horizontalVelocity.add(deceleration); - if (horizontalVelocity.length() < 0.1f) { - horizontalVelocity.set(0); - } - updated = true; - } - - // Update the player's velocity with what we've computed. - velocity.x = horizontalVelocity.x; - velocity.z = horizontalVelocity.z; - } - - private boolean isGrounded(World world) { - var position = player.getPosition(); - // Player must be flat on the top of a block. - if (Math.floor(position.y) != position.y) return false; - // Check to see if there's a block under any of the spaces the player is over. - return getHorizontalSpaceOccupied(position).stream() - .anyMatch(point -> world.getBlockAt(point.x, position.y - 0.1f, point.y) != 0); - } - - /** - * Gets the list of all spaces occupied by a player's position, in the - * horizontal XZ plane. This can be between 1 and 4 spaces, depending on - * if the player's position is overlapping with a few blocks. - * @param pos The position. - * @return The list of 2d positions occupied. - */ - private List getHorizontalSpaceOccupied(Vector3f pos) { - // Get the list of 2d x,z coordinates that we overlap with. - List points = new ArrayList<>(4); // Due to the size of radius, there can only be a max of 4 blocks. - int minX = (int) Math.floor(pos.x - RADIUS); - int minZ = (int) Math.floor(pos.z - RADIUS); - int maxX = (int) Math.floor(pos.x + RADIUS); - int maxZ = (int) Math.floor(pos.z + RADIUS); - for (int x = minX; x <= maxX; x++) { - for (int z = minZ; z <= maxZ; z++) { - points.add(new Vector2i(x, z)); - } - } - return points; - } - - private void checkBlockCollisions(Vector3f movement, Server server, World world) { - var position = player.getPosition(); - var velocity = player.getVelocity(); - final Vector3f nextTickPosition = new Vector3f(position).add(movement); - float height = player.getCurrentHeight(); - float delta = 0.00001f; - final Vector3f stepSize = new Vector3f(movement).normalize(1.0f); - // The number of steps we'll make towards the next tick position. - int stepCount = (int) Math.ceil(movement.length()); - if (stepCount == 0) return; // No movement, so exit. - final Vector3f nextPos = new Vector3f(position); - final Vector3f lastPos = new Vector3f(position); - for (int i = 0; i < stepCount; i++) { - lastPos.set(nextPos); - nextPos.add(stepSize); - // If we shot past the next tick position, clamp it to that. - if (new Vector3f(nextPos).sub(position).length() > movement.length()) { - nextPos.set(nextTickPosition); - } - - // Check if we collide with anything at this new position. - - float playerBodyPrevMinZ = lastPos.z - RADIUS; - float playerBodyPrevMaxZ = lastPos.z + RADIUS; - float playerBodyPrevMinX = lastPos.x - RADIUS; - float playerBodyPrevMaxX = lastPos.x + RADIUS; - float playerBodyPrevMinY = lastPos.y; - float playerBodyPrevMaxY = lastPos.y + height; - - float playerBodyMinZ = nextPos.z - RADIUS; - float playerBodyMaxZ = nextPos.z + RADIUS; - float playerBodyMinX = nextPos.x - RADIUS; - float playerBodyMaxX = nextPos.x + RADIUS; - float playerBodyMinY = nextPos.y; - float playerBodyMaxY = nextPos.y + height; - - // Compute the bounds of all blocks the player is intersecting with. - int minX = (int) Math.floor(playerBodyMinX); - int minZ = (int) Math.floor(playerBodyMinZ); - int minY = (int) Math.floor(playerBodyMinY); - int maxX = (int) Math.floor(playerBodyMaxX); - int maxZ = (int) Math.floor(playerBodyMaxZ); - int maxY = (int) Math.floor(playerBodyMaxY); - - for (int x = minX; x <= maxX; x++) { - for (int z = minZ; z <= maxZ; z++) { - for (int y = minY; y <= maxY; y++) { - byte block = world.getBlockAt(x, y, z); - if (block <= 0) continue; // We're not colliding with this block. - float blockMinY = (float) y; - float blockMaxY = (float) y + 1; - float blockMinX = (float) x; - float blockMaxX = (float) x + 1; - float blockMinZ = (float) z; - float blockMaxZ = (float) z + 1; - - /* - To determine if the player is moving into the -Z side of a block: - - The player's max z position went from < blockMinZ to >= blockMinZ. - - The block to the -Z direction is air. - */ - boolean collidingWithNegativeZ = playerBodyPrevMaxZ < blockMinZ && playerBodyMaxZ >= blockMinZ && world.getBlockAt(x, y, z - 1) <= 0; - if (collidingWithNegativeZ) { - position.z = blockMinZ - RADIUS - delta; - velocity.z = 0; - movement.z = 0; - } - - /* - To determine if the player is moving into the +Z side of a block: - - The player's min z position went from >= blockMaxZ to < blockMaxZ. - - The block to the +Z direction is air. - */ - boolean collidingWithPositiveZ = playerBodyPrevMinZ >= blockMaxZ && playerBodyMinZ < blockMaxZ && world.getBlockAt(x, y, z + 1) <= 0; - if (collidingWithPositiveZ) { - position.z = blockMaxZ + RADIUS + delta; - velocity.z = 0; - movement.z = 0; - } - - /* - To determine if the player is moving into the -X side of a block: - - The player's max x position went from < blockMinX to >= blockMinX - - The block to the -X direction is air. - */ - boolean collidingWithNegativeX = playerBodyPrevMaxX < blockMinX && playerBodyMaxX >= blockMinX && world.getBlockAt(x - 1, y, z) <= 0; - if (collidingWithNegativeX) { - position.x = blockMinX - RADIUS - delta; - velocity.x = 0; - movement.x = 0; - } - - /* - To determine if the player is moving into the +X side of a block: - - The player's min x position went from >= blockMaxX to < blockMaxX. - - The block to the +X direction is air. - */ - boolean collidingWithPositiveX = playerBodyPrevMinX >= blockMaxX && playerBodyMinX < blockMaxX && world.getBlockAt(x + 1, y, z) <= 0; - if (collidingWithPositiveX) { - position.x = blockMaxX + RADIUS + delta; - velocity.x = 0; - movement.x = 0; - } - - /* - To determine if the player is moving down onto a block: - - The player's min y position went from >= blockMaxY to < blockMaxY - - The block above the current one is air. - */ - boolean collidingWithFloor = playerBodyPrevMinY >= blockMaxY && playerBodyMinY < blockMaxY && world.getBlockAt(x, y + 1, z) <= 0; - if (collidingWithFloor) { - // This is a special case! We need to check for fall damage. - if (velocity.y < -20) { - float damage = velocity.y / 50f; - player.setHealth(player.getHealth() + damage); - if (player.getHealth() <= 0) { - server.getPlayerManager().playerKilled(player, player); - } else { - var handler = server.getPlayerManager().getHandler(player.getId()); - handler.sendDatagramPacket(new ClientHealthMessage(player.getHealth())); - int soundVariant = ThreadLocalRandom.current().nextInt(1, 4); - handler.sendDatagramPacket(new SoundMessage("hurt_" + soundVariant, 1, player.getPosition())); - } - } - position.y = blockMaxY; - velocity.y = 0; - movement.y = 0; - } - - /* - To determine if the player is moving up into a block: - - The player's y position went from below blockMinY to >= blockMinY - - The block below the current one is air. - */ - boolean collidingWithCeiling = playerBodyPrevMaxY < blockMinY && playerBodyMaxY >= blockMinY && world.getBlockAt(x, y - 1, z) <= 0; - if (collidingWithCeiling) { - position.y = blockMinY - height - delta; - velocity.y = 0; - movement.y = 0; - } - - updated = true; - } - } - } - } - } - private void shootGun(long now, Server server, Gun gun, GunItemStack g) { server.getProjectileManager().spawnBullets(player, gun); g.setBulletCount(g.getBulletCount() - 1); diff --git a/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerMovementController.java b/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerMovementController.java new file mode 100644 index 0000000..96fa86c --- /dev/null +++ b/server/src/main/java/nl/andrewl/aos2_server/logic/PlayerMovementController.java @@ -0,0 +1,10 @@ +package nl.andrewl.aos2_server.logic; + +import nl.andrewl.aos2_server.Server; +import nl.andrewl.aos2_server.config.ServerConfig; +import nl.andrewl.aos2_server.model.ServerPlayer; +import nl.andrewl.aos_core.model.world.World; + +public interface PlayerMovementController { + boolean tickMovement(float dt, ServerPlayer player, PlayerInputTracker input, Server server, World world, ServerConfig.PhysicsConfig config); +} diff --git a/server/src/main/java/nl/andrewl/aos2_server/model/ServerPlayer.java b/server/src/main/java/nl/andrewl/aos2_server/model/ServerPlayer.java index 88cc82a..02126d8 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/model/ServerPlayer.java +++ b/server/src/main/java/nl/andrewl/aos2_server/model/ServerPlayer.java @@ -2,7 +2,6 @@ package nl.andrewl.aos2_server.model; import nl.andrewl.aos2_server.logic.PlayerActionManager; import nl.andrewl.aos_core.model.Player; -import nl.andrewl.aos_core.model.PlayerMode; import nl.andrewl.aos_core.model.item.BlockItemStack; import nl.andrewl.aos_core.model.item.GunItemStack; import nl.andrewl.aos_core.model.item.Inventory; diff --git a/server/src/main/resources/redfort.wld b/server/src/main/resources/redfort.wld index 073b6b1e5f55eca1282cda889a687bccd475fde7..0a724839a6a488259726add4b4f27659b9fdc84b 100644 GIT binary patch delta 7015 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