<commit_before>package sx.lambda.voxel.tasks;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;
import groovy.transform.CompileStatic;
import sx.lambda.voxel.VoxelGameClient;
import sx.lambda.voxel.api.BuiltInBlockIds;
import sx.lambda.voxel.block.Block;
import sx.lambda.voxel.entity.LivingEntity;
import sx.lambda.voxel.entity.player.Player;
import sx.lambda.voxel.world.IWorld;
import sx.lambda.voxel.world.chunk.IChunk;

@CompileStatic
public class MovementHandler implements RepeatedTask {
    public MovementHandler(VoxelGameClient game) {
        this.game = game;
    }

    @Override
    public String getIdentifier() {
        return "Movement Handler";
    }

    @Override
    public void run() {
        try {
            long lastMoveCheckMS = System.currentTimeMillis();
            while (!game.isDone()) {
                if (game.getWorld() == null || game.getPlayer() == null) {
                    Thread.sleep(1000);
                    lastMoveCheckMS = System.currentTimeMillis();
                } else {
                    Player player = game.getPlayer();
                    IWorld world = game.getWorld();
                    long moveDiffMS = System.currentTimeMillis() - lastMoveCheckMS;
                    float movementMultiplier = moveDiffMS * 0.0043f;
                    final boolean threeDMove = false;
                    Vector3 lastPosition = player.getPosition().cpy();
                    if (game.getCurrentScreen().equals(game.getHud())) {
                        float deltaX = 0;
                        float deltaY = 0;
                        float deltaZ = 0;
                        if (Gdx.input.isKeyPressed(Input.Keys.W)) {// Forward TODO Config - Make keys configurable
                            float yaw = player.getRotation().getYaw();
                            float pitch = player.getRotation().getPitch();
                            if (threeDMove) {
                                deltaX += MathUtils.cosDeg(pitch) * MathUtils.sinDeg(yaw) * movementMultiplier;
                                deltaY += MathUtils.sinDeg(pitch) * movementMultiplier;
                                deltaZ += -MathUtils.cosDeg(pitch) * MathUtils.cosDeg(yaw) * movementMultiplier;
                            } else {
                                deltaX += MathUtils.sinDeg(yaw) * movementMultiplier;
                                deltaZ += -MathUtils.cosDeg(yaw) * movementMultiplier;
                            }
                        } else if (Gdx.input.isKeyPressed(Input.Keys.S)) {
                            float yaw = player.getRotation().getYaw();
                            float pitch = player.getRotation().getPitch();
                            if (threeDMove) {
                                deltaX += -MathUtils.cosDeg(pitch) * MathUtils.sinDeg(yaw) * movementMultiplier;
                                deltaY += -MathUtils.sinDeg(pitch) * movementMultiplier;
                                deltaZ += MathUtils.cosDeg(pitch) * MathUtils.cosDeg(yaw) * movementMultiplier;
                            } else {
                                deltaX += -MathUtils.sinDeg(yaw) * movementMultiplier;
                                deltaZ += MathUtils.cosDeg(yaw) * movementMultiplier;
                                deltaY += 0;
                            }
                        } else if (Gdx.input.isKeyPressed(Input.Keys.A)) {//Strafe left
                            float yaw = player.getRotation().getYaw();

                            deltaX += MathUtils.sinDeg(yaw - 90) * movementMultiplier;
                            deltaZ += -MathUtils.cosDeg(yaw - 90) * movementMultiplier;
                        } else if (Gdx.input.isKeyPressed(Input.Keys.D)) {//Strafe right
                            float yaw = player.getRotation().getYaw();

                            deltaX += MathUtils.sinDeg(yaw + 90) * movementMultiplier;
                            deltaZ += -MathUtils.cosDeg(yaw + 90) * movementMultiplier;
                        }

                        if (!checkDeltaCollision(player, 0, 0, deltaZ)) {
                            player.getPosition().offset(0, 0, deltaZ);
                        }
                        if (!checkDeltaCollision(player, deltaX, 0, 0)) {
                            player.getPosition().offset(deltaX, 0, 0);
                        }
                        if (!checkDeltaCollision(player, 0, deltaY, 0)) {
                            player.getPosition().offset(0, deltaY, 0);
                        }
                    }

                    int playerX = MathUtils.floor(player.getPosition().getX());
                    int playerZ = MathUtils.floor(player.getPosition().getZ());
                    IChunk playerChunk = world.getChunk(playerX, playerZ);
                    if (playerChunk != null) {
                        int cx = playerX & (game.getWorld().getChunkSize() - 1);
                        int cz = playerZ & (game.getWorld().getChunkSize() - 1);
                        int feetBlockY = MathUtils.floor(player.getPosition().getY() - 0.02f);
                        Block blockAtPlayer = playerChunk.getBlock(cx, feetBlockY, cz);
                        if (blockAtPlayer != null && blockAtPlayer.isSolid() &&
                                blockAtPlayer.calculateBoundingBox(playerChunk, cx, feetBlockY, cz)
                                        .contains(player.getPosition().cpy().add(0, -0.02f, 0))) {
                            player.setOnGround(true);
                        } else {
                            player.setOnGround(false);
                        }
                    } else {
                        player.setOnGround(true);
                    }

                    int blockInFeet = player.getBlockInFeet(world);
                    boolean inWater = blockInFeet == BuiltInBlockIds.WATER_ID || blockInFeet == BuiltInBlockIds.WATER_FLOW_ID
                            || blockInFeet == BuiltInBlockIds.LAVA_STILL_ID || blockInFeet == BuiltInBlockIds.LAVA_FLOW_ID;
                    if (inWater) {
                        if (!Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
                            player.setYVelocity(-3f); // TODO use the same value as mc
                        } else {
                            player.setYVelocity(3f); // TODO use the same value as mc
                        }<commit_msg>Reallow strafing and moving at the same time. Normalize the resultant delta vector.<commit_after>package sx.lambda.voxel.tasks;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;
import groovy.transform.CompileStatic;
import sx.lambda.voxel.VoxelGameClient;
import sx.lambda.voxel.api.BuiltInBlockIds;
import sx.lambda.voxel.block.Block;
import sx.lambda.voxel.entity.LivingEntity;
import sx.lambda.voxel.entity.player.Player;
import sx.lambda.voxel.world.IWorld;
import sx.lambda.voxel.world.chunk.IChunk;

@CompileStatic
public class MovementHandler implements RepeatedTask {
    public MovementHandler(VoxelGameClient game) {
        this.game = game;
    }

    @Override
    public String getIdentifier() {
        return "Movement Handler";
    }

    @Override
    public void run() {
        try {
            long lastMoveCheckMS = System.currentTimeMillis();
            while (!game.isDone()) {
                if (game.getWorld() == null || game.getPlayer() == null) {
                    Thread.sleep(1000);
                    lastMoveCheckMS = System.currentTimeMillis();
                } else {
                    Player player = game.getPlayer();
                    IWorld world = game.getWorld();
                    long moveDiffMS = System.currentTimeMillis() - lastMoveCheckMS;
                    float movementMultiplier = moveDiffMS * 0.0043f;
                    final boolean threeDMove = false;
                    Vector3 lastPosition = player.getPosition().cpy();
                    if (game.getCurrentScreen().equals(game.getHud())) {
                        float deltaX = 0;
                        float deltaY = 0;
                        float deltaZ = 0;
                        if (Gdx.input.isKeyPressed(Input.Keys.W)) {// Forward TODO Config - Make keys configurable
                            float yaw = player.getRotation().getYaw();
                            float pitch = player.getRotation().getPitch();
                            if (threeDMove) {
                                deltaX += MathUtils.cosDeg(pitch) * MathUtils.sinDeg(yaw) * movementMultiplier;
                                deltaY += MathUtils.sinDeg(pitch) * movementMultiplier;
                                deltaZ += -MathUtils.cosDeg(pitch) * MathUtils.cosDeg(yaw) * movementMultiplier;
                            } else {
                                deltaX += MathUtils.sinDeg(yaw) * movementMultiplier;
                                deltaZ += -MathUtils.cosDeg(yaw) * movementMultiplier;
                            }
                        }
                        if (Gdx.input.isKeyPressed(Input.Keys.S)) {
                            float yaw = player.getRotation().getYaw();
                            float pitch = player.getRotation().getPitch();
                            if (threeDMove) {
                                deltaX += -MathUtils.cosDeg(pitch) * MathUtils.sinDeg(yaw) * movementMultiplier;
                                deltaY += -MathUtils.sinDeg(pitch) * movementMultiplier;
                                deltaZ += MathUtils.cosDeg(pitch) * MathUtils.cosDeg(yaw) * movementMultiplier;
                            } else {
                                deltaX += -MathUtils.sinDeg(yaw) * movementMultiplier;
                                deltaZ += MathUtils.cosDeg(yaw) * movementMultiplier;
                                deltaY += 0;
                            }
                        }
                        if (Gdx.input.isKeyPressed(Input.Keys.A)) {//Strafe left
                            float yaw = player.getRotation().getYaw();

                            deltaX += MathUtils.sinDeg(yaw - 90) * movementMultiplier;
                            deltaZ += -MathUtils.cosDeg(yaw - 90) * movementMultiplier;
                        }
                        if (Gdx.input.isKeyPressed(Input.Keys.D)) {//Strafe right
                            float yaw = player.getRotation().getYaw();

                            deltaX += MathUtils.sinDeg(yaw + 90) * movementMultiplier;
                            deltaZ += -MathUtils.cosDeg(yaw + 90) * movementMultiplier;
                        }

                        Vector3 deltaVec = new Vector3(deltaX, deltaY, deltaZ);
                        deltaVec.nor();
                        deltaVec.set(deltaVec.x*movementMultiplier, deltaVec.y*movementMultiplier, deltaVec.z*movementMultiplier);

                        if (!checkDeltaCollision(player, 0, 0, deltaVec.z)) {
                            player.getPosition().offset(0, 0, deltaVec.z);
                        }
                        if (!checkDeltaCollision(player, deltaVec.x, 0, 0)) {
                            player.getPosition().offset(deltaVec.x, 0, 0);
                        }
                        if (!checkDeltaCollision(player, 0, deltaVec.y, 0)) {
                            player.getPosition().offset(0, deltaVec.y, 0);
                        }
                    }

                    int playerX = MathUtils.floor(player.getPosition().getX());
                    int playerZ = MathUtils.floor(player.getPosition().getZ());
                    IChunk playerChunk = world.getChunk(playerX, playerZ);
                    if (playerChunk != null) {
                        int cx = playerX & (game.getWorld().getChunkSize() - 1);
                        int cz = playerZ & (game.getWorld().getChunkSize() - 1);
                        int feetBlockY = MathUtils.floor(player.getPosition().getY() - 0.02f);
                        Block blockAtPlayer = playerChunk.getBlock(cx, feetBlockY, cz);
                        if (blockAtPlayer != null && blockAtPlayer.isSolid() &&
                                blockAtPlayer.calculateBoundingBox(playerChunk, cx, feetBlockY, cz)
                                        .contains(player.getPosition().cpy().add(0, -0.02f, 0))) {
                            player.setOnGround(true);
                        } else {
                            player.setOnGround(false);
                        }
                    } else {
                        player.setOnGround(true);
                    }

                    int blockInFeet = player.getBlockInFeet(world);
                    boolean inWater = blockInFeet == BuiltInBlockIds.WATER_ID || blockInFeet == BuiltInBlockIds.WATER_FLOW_ID
                            || blockInFeet == BuiltInBlockIds.LAVA_STILL_ID || blockInFeet == BuiltInBlockIds.LAVA_FLOW_ID;
                    if (inWater) {
                        if (!Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
                            player.setYVelocity(-3f); // TODO use the same value as mc
                        } else {
                            player.setYVelocity(3f); // TODO use the same value as mc
                        }
{'fraction_non_alphanumeric': 0.0989718174421185, 'fraction_numerical': 0.007101116946519713, 'mean_word_length': 1.3314364545611312, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3865578', 'n_tokens_mistral': 3671, 'n_tokens_neox': 3425, 'n_words': 619}