Skip to content

Commit

Permalink
Implement one block gap jumps and prepare for towering and digging st…
Browse files Browse the repository at this point in the history
…raight down
  • Loading branch information
AlexProgrammerDE committed Oct 17, 2023
1 parent d22c563 commit d8a0421
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Costs {
public static final double STRAIGHT = 1;
public static final double DIAGONAL = 1.4142135623730951;
public static final double JUMP = 0.3;
public static final double ONE_GAP_JUMP = STRAIGHT + JUMP;
public static final double FALL_1 = 0.1;
public static final double FALL_2 = 0.2;
public static final double FALL_3 = 0.3;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* ServerWrecker
*
* Copyright (C) 2023 ServerWrecker
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
package net.pistonmaster.serverwrecker.pathfinding.execution;

import com.github.steveice10.mc.protocol.data.game.entity.RotationOrigin;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import net.pistonmaster.serverwrecker.protocol.BotConnection;
import net.pistonmaster.serverwrecker.util.BlockTypeHelper;
import org.cloudburstmc.math.vector.Vector3d;

@ToString
@RequiredArgsConstructor
public class GapJumpAction implements WorldAction {
private final Vector3d position;
private boolean didLook = false;
private int noJumpTicks = 0;
private boolean didJump = false;

@Override
public boolean isCompleted(BotConnection connection) {
var movementManager = connection.sessionDataManager().getBotMovementManager();
var botPosition = movementManager.getPlayerPos();
var levelState = connection.sessionDataManager().getCurrentLevel();
if (levelState == null) {
return false;
}

var blockMeta = levelState.getBlockStateAt(position.toInt());
var insideBlock = blockMeta.isPresent() && !BlockTypeHelper.isEmpty(blockMeta.get());

if (insideBlock) {
// We are inside a block, so being close is good enough
var distance = botPosition.distance(position);
return distance <= 1;
} else if (botPosition.getY() != position.getY()) {
// We want to be on the same Y level
return false;
} else {
var distance = botPosition.distance(position);
return distance <= 0.3;
}
}

@Override
public void tick(BotConnection connection) {
var movementManager = connection.sessionDataManager().getBotMovementManager();
var botPosition = movementManager.getPlayerPos();

var previousYaw = movementManager.getYaw();
movementManager.lookAt(RotationOrigin.EYES, position);
movementManager.setPitch(0);
var newYaw = movementManager.getYaw();

var yawDifference = Math.abs(previousYaw - newYaw);

// We should only set the yaw once to the server to prevent the bot looking weird due to inaccuracy
if (didLook && yawDifference > 5) {
movementManager.setLastSentYaw(movementManager.getYaw());
} else {
didLook = true;
}

// Don't let the bot look up or down (makes it look weird)
movementManager.getControlState().resetAll();
movementManager.getControlState().setForward(true);

if (shouldJump()) {
movementManager.getControlState().setJumping(true);
}
}

private boolean shouldJump() {
if (noJumpTicks < 1) {
noJumpTicks++;
return false;
} else {
noJumpTicks = 0;
return true;
}
}

@Override
public int getAllowedTicks() {
// 5-seconds max to walk to a block
return 5 * 20;
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
package net.pistonmaster.serverwrecker.pathfinding.graph.actions.movement;
package net.pistonmaster.serverwrecker.pathfinding.graph.actions;

import com.github.steveice10.mc.protocol.data.game.entity.object.Direction;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
Expand All @@ -30,8 +30,7 @@
import net.pistonmaster.serverwrecker.pathfinding.execution.BlockPlaceAction;
import net.pistonmaster.serverwrecker.pathfinding.execution.MovementAction;
import net.pistonmaster.serverwrecker.pathfinding.execution.WorldAction;
import net.pistonmaster.serverwrecker.pathfinding.graph.actions.GraphAction;
import net.pistonmaster.serverwrecker.pathfinding.graph.actions.GraphInstructions;
import net.pistonmaster.serverwrecker.pathfinding.graph.actions.movement.*;
import net.pistonmaster.serverwrecker.protocol.bot.BotActionManager;
import net.pistonmaster.serverwrecker.util.VectorHelper;
import org.cloudburstmc.math.vector.Vector3i;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* ServerWrecker
*
* Copyright (C) 2023 ServerWrecker
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
package net.pistonmaster.serverwrecker.pathfinding.graph.actions.parkour;

import org.cloudburstmc.math.vector.Vector3i;

public enum ParkourDirection {
NORTH,
SOUTH,
EAST,
WEST;

public static final ParkourDirection[] VALUES = values();

@SuppressWarnings("DuplicatedCode")
public Vector3i offset(Vector3i vector) {
return switch (this) {
case NORTH -> vector.add(0, 0, -1);
case SOUTH -> vector.add(0, 0, 1);
case EAST -> vector.add(1, 0, 0);
case WEST -> vector.add(-1, 0, 0);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* ServerWrecker
*
* Copyright (C) 2023 ServerWrecker
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
package net.pistonmaster.serverwrecker.pathfinding.graph.actions.parkour;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import lombok.Getter;
import lombok.Setter;
import net.pistonmaster.serverwrecker.pathfinding.BotEntityState;
import net.pistonmaster.serverwrecker.pathfinding.Costs;
import net.pistonmaster.serverwrecker.pathfinding.execution.GapJumpAction;
import net.pistonmaster.serverwrecker.pathfinding.graph.actions.GraphAction;
import net.pistonmaster.serverwrecker.pathfinding.graph.actions.GraphInstructions;
import net.pistonmaster.serverwrecker.util.VectorHelper;
import org.cloudburstmc.math.vector.Vector3i;

import java.util.List;

public class ParkourMovement implements GraphAction {
private static final Vector3i FEET_POSITION_RELATIVE_BLOCK = Vector3i.ZERO;
private final ParkourDirection direction;
private final Vector3i targetFeetBlock;
@Setter
@Getter
private boolean isImpossible = false;

public ParkourMovement(ParkourDirection direction) {
this.direction = direction;
this.targetFeetBlock = direction.offset(direction.offset(FEET_POSITION_RELATIVE_BLOCK));
}

private ParkourMovement(ParkourMovement other) {
this.direction = other.direction;
this.targetFeetBlock = other.targetFeetBlock;
this.isImpossible = other.isImpossible;
}

public List<Vector3i> listRequiredFreeBlocks() {
List<Vector3i> requiredFreeBlocks = new ObjectArrayList<>();

// Make head block free (maybe head block is a slab)
requiredFreeBlocks.add(FEET_POSITION_RELATIVE_BLOCK.add(0, 1, 0));

// Make block above the head block free for jump
requiredFreeBlocks.add(FEET_POSITION_RELATIVE_BLOCK.add(0, 2, 0));

var oneFurther = direction.offset(FEET_POSITION_RELATIVE_BLOCK);

// The gap to jump over
requiredFreeBlocks.add(oneFurther.sub(0, 1, 0));

// Room for jumping
requiredFreeBlocks.add(oneFurther);
requiredFreeBlocks.add(oneFurther.add(0, 1, 0));
requiredFreeBlocks.add(oneFurther.add(0, 2, 0));

var twoFurther = direction.offset(oneFurther);

// Room for jumping
requiredFreeBlocks.add(twoFurther);
requiredFreeBlocks.add(twoFurther.add(0, 1, 0));
requiredFreeBlocks.add(twoFurther.add(0, 2, 0));

return requiredFreeBlocks;
}

public Vector3i requiredSolidBlock() {
// Floor block
return targetFeetBlock.sub(0, 1, 0);
}

@Override
public boolean isImpossibleToComplete() {
return isImpossible;
}

@Override
public GraphInstructions getInstructions(BotEntityState previousEntityState) {
var absoluteTargetFeetBlock = previousEntityState.positionBlock().add(targetFeetBlock);
var targetFeetDoublePosition = VectorHelper.middleOfBlockNormalize(absoluteTargetFeetBlock.toDouble());

return new GraphInstructions(new BotEntityState(
targetFeetDoublePosition,
absoluteTargetFeetBlock,
previousEntityState.levelState(),
previousEntityState.inventory()
), Costs.ONE_GAP_JUMP, List.of(new GapJumpAction(targetFeetDoublePosition)));
}

@Override
public ParkourMovement copy(BotEntityState previousEntityState) {
return new ParkourMovement(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* ServerWrecker
*
* Copyright (C) 2023 ServerWrecker
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
package net.pistonmaster.serverwrecker.pathfinding.graph.actions.updown;

public enum UpDownDirection {
UP,
DOWN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* ServerWrecker
*
* Copyright (C) 2023 ServerWrecker
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
package net.pistonmaster.serverwrecker.pathfinding.graph.actions.updown;

public class UpDownMovement {
}

0 comments on commit d8a0421

Please sign in to comment.