Skip to content

Commit

Permalink
Few performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Oct 17, 2023
1 parent 8c37395 commit 4e005cb
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class Costs {
// Multiply calculated ticks using this number to get a good relative heuristic
public static final double TICKS_PER_BLOCK = 5;
// We don't want a bot that tries to break blocks instead of walking around them
public static final double BREAK_BLOCK_ADDITION = 10;
public static final double PLACE_BLOCK = 10;
public static final double BREAK_BLOCK_ADDITION = 2;
public static final double PLACE_BLOCK = 5;
// Sliding around a corner is roughly like walking two blocks
public static final double CORNER_SLIDE = 2 - DIAGONAL;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public GraphInstructions[] getActions(BotEntityState node) {
var value = SUBSCRIPTION_VALUES[i];

BlockStateMeta blockState = null;
Vector3i absolutePositionBlock = null;

// We cache only this, but not solid because solid will only occur a single time
var calculatedFree = false;
Expand All @@ -102,9 +103,9 @@ public GraphInstructions[] getActions(BotEntityState node) {
continue;
}

// Absolute position of where the feet of the player right now are
var absolutePositionBlock = node.positionBlock().add(key);
if (blockState == null) {
// Lazy calculation to avoid unnecessary calls
absolutePositionBlock = node.positionBlock().add(key);
blockState = node.levelState()
.getBlockStateAt(absolutePositionBlock)
.orElseThrow(OutOfLevelException::new);
Expand All @@ -121,6 +122,10 @@ public GraphInstructions[] getActions(BotEntityState node) {
}

if (isFree) {
if (movement.isAllowBlockActions()) {
movement.getNoNeedToBreak()[subscriber.blockArrayIndex] = true;
}

continue;
}

Expand All @@ -146,6 +151,11 @@ public GraphInstructions[] getActions(BotEntityState node) {
}
}
case MOVEMENT_BREAK_SAFETY_CHECK -> {
// There is no need to break this block, so there is no need for safety checks
if (movement.getNoNeedToBreak()[subscriber.blockArrayIndex]) {
continue;
}

// The block was already marked as unsafe
if (movement.getUnsafeToBreak()[subscriber.blockArrayIndex]) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public final class PlayerMovement implements GraphAction {
private final MovementMiningCost[] blockBreakCosts;
@Getter
private final boolean[] unsafeToBreak;
@Getter
private final boolean[] noNeedToBreak;
@Setter
@Getter
private BotActionManager.BlockPlaceData blockPlaceData;
Expand Down Expand Up @@ -90,9 +92,11 @@ public PlayerMovement(MovementDirection direction, MovementSide side, MovementMo
if (allowBlockActions) {
blockBreakCosts = new MovementMiningCost[freeCapacity()];
unsafeToBreak = new boolean[freeCapacity()];
noNeedToBreak = new boolean[freeCapacity()];
} else {
blockBreakCosts = null;
unsafeToBreak = null;
noNeedToBreak = null;
}
}

Expand All @@ -108,6 +112,7 @@ private PlayerMovement(PlayerMovement other) {
this.allowBlockActions = other.allowBlockActions;
this.blockBreakCosts = other.blockBreakCosts == null ? null : new MovementMiningCost[other.blockBreakCosts.length];
this.unsafeToBreak = other.unsafeToBreak == null ? null : new boolean[other.unsafeToBreak.length];
this.noNeedToBreak = other.noNeedToBreak == null ? null : new boolean[other.noNeedToBreak.length];
this.blockPlaceData = other.blockPlaceData;
this.requiresAgainstBlock = other.requiresAgainstBlock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,9 @@ public boolean isInFluid() {
return false;
}

var blockType = level.getBlockTypeAt(blockPos);
return blockType.map(BlockType::isFluid).orElse(false);
return level.getBlockTypeAt(blockPos)
.map(BlockType::isFluid)
.orElse(false);
}

public void jump() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.pistonmaster.serverwrecker.data.BlockType;
import net.pistonmaster.serverwrecker.data.ResourceData;
import net.pistonmaster.serverwrecker.protocol.bot.block.BlockStateMeta;
import net.pistonmaster.serverwrecker.protocol.bot.model.ChunkKey;
Expand Down Expand Up @@ -120,10 +119,6 @@ public Optional<BlockStateMeta> getBlockStateAt(Vector3i block) {
.getBlockStateForStateId(chunkData.getBlock(block)));
}

public Optional<BlockType> getBlockTypeAt(Vector3i block) {
return getBlockStateAt(block).map(BlockStateMeta::blockType);
}

public ChunkHolder immutableCopy() {
return new ChunkHolder(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public Optional<BlockStateMeta> getBlockStateAt(Vector3i block) {
}

public Optional<BlockType> getBlockTypeAt(Vector3i block) {
return chunks.getBlockTypeAt(block);
return getBlockStateAt(block).map(BlockStateMeta::blockType);
}

public boolean isOutOfWorld(Vector3i block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class VectorHelper {
public static final Hash.Strategy<Vector3i> VECTOR3I_HASH_STRATEGY = new Hash.Strategy<>() {
@Override
public int hashCode(Vector3i o) {
return (o.getX() * 211 + o.getY()) * 97 + o.getZ();
return o.hashCode();
}

@Override
Expand Down

0 comments on commit 4e005cb

Please sign in to comment.