Skip to content

Commit

Permalink
Remove target block checking from spin head and rod wiggle goals. Upd…
Browse files Browse the repository at this point in the history
…ated searching for button and pressing button goals to be more reliable at finding and pressing.
  • Loading branch information
MrJoshuaT committed Nov 3, 2021
1 parent b822e2e commit 8c51b20
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,17 @@ protected void initGoals() {
}

var priority = 0;

this.goalSelector.add(++priority, new SwimGoal(this));
this.goalSelector.add(++priority, new EscapeWaterGoal(this));
this.goalSelector.add(++priority, new EscapeDangerGoal(this, 0.5D));
this.goalSelector.add(++priority, new IronGolemWanderAroundGoal(this, 0.25D));
this.goalSelector.add(++priority, new SearchForButtonsGoal(this));
this.goalSelector.add(++priority, new PressButtonGoal(this));
this.goalSelector.add(++priority, new LookAroundGoal(this));
this.goalSelector.add(++priority, new IronGolemWanderAroundGoal(this, 0.25D));
this.goalSelector.add(++priority, new SpinHeadGoal(this));
this.goalSelector.add(++priority, new RodWiggleGoal(this));
this.goalSelector.add(++priority, new LookAtEntityGoal(this, PlayerEntity.class, 6.0F));
this.goalSelector.add(++priority, new LookAtEntityGoal(this, IronGolemEntity.class, 10.0F));

this.targetSelector.add(1, new SearchForButtonsGoal(this));
this.goalSelector.add(++priority, new LookAroundGoal(this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,34 @@

public class PressButtonGoal extends Goal {
private final CopperGolemEntity entity;
public float lastTick = 0;
private int ticks = 0;

public PressButtonGoal(CopperGolemEntity entity) {
this.entity = entity;
}

@Override
public boolean canStart() { return this.entity.getBlockTarget() != null && this.entity.getRandom().nextFloat() > 0.6f; }
public boolean canStart() { return this.entity.getBlockTarget() != null && this.entity.getRandom().nextFloat() < 0.01f; }

public boolean shouldContinue() { return this.entity.getBlockTarget() != null; }
@Override
public boolean shouldContinue() {
return this.entity.getBlockTarget() != null;
}

public void start() {
var target = this.entity.getBlockTarget();
entity.getNavigation().startMovingTo(target.getX(), target.getY(), target.getZ(), 0.4D);
}

public void tick() {
if (this.lastTick > 0) {
--this.lastTick;
this.ticks++;
if (this.ticks > 40 && !entity.getNavigation().isFollowingPath()) {
entity.clearBlockTarget();
return;
}

var target = entity.getBlockTarget();
if (target == null || !canClickTarget(target) || lastTick >= 20)
if (target == null || !canClickTarget(target))
return;

try {
Expand All @@ -50,10 +55,10 @@ public void tick() {
this.entity.setBendOverTicks(-10); // bend backwards
}
button.onUse(state, entity.world, target, null, Hand.MAIN_HAND, null);
this.lastTick = 20 * 5;
this.entity.setButtonTicksLeft(10); // 10 = 1 rotation, as max deviation is set to 10 in model logic

entity.clearBlockTarget();
ticks++;
}
catch (Exception e) {
ModInit.LOGGER.error("Failed to press targeted button", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public RodWiggleGoal(CopperGolemEntity entity) {

@Override
public boolean canStart() {
if (entity.getBlockTarget() != null)
return false;
return entity.getRandom().nextFloat() > 0.995F;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public SpinHeadGoal(CopperGolemEntity entity) {

@Override
public boolean canStart() {
if (entity.getBlockTarget() != null)
return false;
return entity.getRandom().nextFloat() > 0.995F;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import net.minecraft.block.Block;
import net.minecraft.entity.ai.goal.Goal;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;

import org.jetbrains.annotations.Nullable;

Expand All @@ -15,25 +14,21 @@

public class SearchForButtonsGoal extends Goal {
private final CopperGolemEntity entity;
public float lastTick = 0;

public SearchForButtonsGoal(CopperGolemEntity entity) {
this.entity = entity;
}

@Override
public boolean canStart() { return true; }

public void start() {
this.searchForButtons();
public boolean canStart() {
return this.entity.getBlockTarget() == null && this.entity.getRandom().nextFloat() < 0.1f;
}

public void tick() {
if (this.lastTick > 0)
--this.lastTick;
@Override
public boolean shouldContinue() { return this.entity.getBlockTarget() == null; }

if (this.lastTick <= 0)
searchForButtons();
public void tick() {
searchForButtons();
}

private void searchForButtons() {
Expand All @@ -47,15 +42,11 @@ private void searchForButtons() {
return;

entity.setBlockTarget(pos);

// Set tick cooldown
this.lastTick = 20 * (10 * MathHelper.clamp(entity.getRandom().nextFloat(), 0.5f, 1f));
}

@Nullable
private BlockPos getRandomNavigableBlockPos(List<BlockPos> buttons) {
while (buttons.size() > 0)
{
while (buttons.size() > 0) {
var index = entity.getRandom().nextInt(buttons.size());
var pos = buttons.get(index);
var path = this.entity.getNavigation().findPathTo(pos, 1);
Expand All @@ -74,11 +65,9 @@ private List<BlockPos> getButtons() {
BlockPos pos = this.entity.getBlockPos();
BlockPos start = pos.add(-10, -10, -10);
BlockPos end = pos.add(10, 10, 10);
for (BlockPos blockpos$mutableblockpos : BlockPos.iterate(start, end))
{
for (BlockPos blockpos$mutableblockpos : BlockPos.iterate(start, end)) {
Block block = this.entity.world.getBlockState(blockpos$mutableblockpos).getBlock();
if (isValidBlock(block))
{
if (isValidBlock(block)) {
blocks.add(blockpos$mutableblockpos.toImmutable());
}
}
Expand Down

0 comments on commit 8c51b20

Please sign in to comment.