Skip to content

Commit 1d75d28

Browse files
committed
added attackMove, added navdistance constant for movement
1 parent e1af913 commit 1d75d28

File tree

12 files changed

+257
-73
lines changed

12 files changed

+257
-73
lines changed

builder/version.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#Thu, 14 Nov 2013 18:19:16 +0100
1+
#Fri, 15 Nov 2013 15:02:57 +0100
22

33
version.number=6
4-
version.build=20
5-
version.timestamp=201311141819
4+
version.build=24
5+
version.timestamp=201311151502

src/java/de/ntcomputer/minecraft/controllablemobs/api/ControllableMobActions.java

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* You can retrieve an instance by using {@link ControllableMob#getActions()}
1414
*
1515
* @author Cybran
16-
* @version v5
16+
* @version v6
1717
*/
1818
public interface ControllableMobActions {
1919
/**
@@ -28,7 +28,7 @@ public interface ControllableMobActions {
2828
/**
2929
* Orders the entity to move to the given location, optionally adding the action to the queue.
3030
* This action is block accurate, meaning, that the entity will stop once it reached the block the given location is pointing at.
31-
* When the actions execution is started, all other movements or attacks caused by the AI are stopped and overridden.
31+
* When the action's execution is started, all other movements or attacks caused by the AI are stopped and overridden.
3232
* If a target is assigned to the entity, it will not be lost and dealing with it will be resumed when this movement is finished.
3333
*
3434
* @param loc the location the entity will move to.
@@ -37,6 +37,61 @@ public interface ControllableMobActions {
3737
*/
3838
public ControllableMobAction moveTo(Location loc, boolean queue);
3939

40+
/**
41+
* Orders the entity to move to the given location. Uses the {@link ControllableMobActions#getDefaultQueuingFlag() default queuing flag}.
42+
* This action is block accurate, meaning, that the entity will stop once it reached the block the given location is pointing at.
43+
* When the action's execution is started, all other movements caused by the AI are stopped and overridden.
44+
* If the entity has a target locked on, it will interrupt the movement, deal with this target, and then continue moving to the destination.
45+
*
46+
* @see #attackMoveTo(Location, boolean, double, double)
47+
* @param loc the location the entity will move to.
48+
* @return {@link ControllableMobAction}
49+
*/
50+
public ControllableMobAction attackMoveTo(Location loc);
51+
52+
/**
53+
* Orders the entity to move to the given location, optionally adding the action to the queue.
54+
* This action is block accurate, meaning, that the entity will stop once it reached the block the given location is pointing at.
55+
* When the action's execution is started, all other movements caused by the AI are stopped and overridden.
56+
* If the entity has a target locked on, it will interrupt the movement, deal with this target, and then continue moving to the destination.
57+
*
58+
* @see #attackMoveTo(Location, boolean, double, double)
59+
* @param loc the location the entity will move to.
60+
* @param queue whether this action should be added to the queue (true) or executed directly (false).
61+
* @return {@link ControllableMobAction}
62+
*/
63+
public ControllableMobAction attackMoveTo(Location loc, boolean queue);
64+
65+
/**
66+
* Orders the entity to move to the given location, optionally adding the action to the queue.
67+
* This action is block accurate, meaning, that the entity will stop once it reached the block the given location is pointing at.
68+
* When the action's execution is started, all other movements caused by the AI are stopped and overridden.
69+
* If the entity has a target locked on, it will interrupt the movement, deal with this target, and then continue moving to the destination.
70+
*
71+
* @see #attackMoveTo(Location, boolean, double, double)
72+
* @param loc the location the entity will move to.
73+
* @param queue whether this action should be added to the queue (true) or executed directly (false).
74+
* @param maximumDistractionDistance the maximum distance this entity will try to follow targets in order to attack them. If reached, the entity will turn back and continue moving to the destination. Default is 16.0 blocks.
75+
* @return {@link ControllableMobAction}
76+
* @throws IllegalArgumentException when maximumDistractionDistance is zero or negative
77+
*/
78+
public ControllableMobAction attackMoveTo(Location loc, boolean queue, double maximumDistractionDistance) throws IllegalArgumentException;
79+
80+
/**
81+
* Orders the entity to move to the given location, optionally adding the action to the queue.
82+
* This action is block accurate, meaning, that the entity will stop once it reached the block the given location is pointing at.
83+
* When the action's execution is started, all other movements caused by the AI are stopped and overridden.
84+
* If the entity has a target locked on, it will interrupt the movement, deal with this target, and then continue moving to the destination.
85+
*
86+
* @param loc the location the entity will move to.
87+
* @param queue whether this action should be added to the queue (true) or executed directly (false).
88+
* @param maximumDistractionDistance the maximum distance this entity will try to follow targets in order to attack them. If reached, the entity will turn back and continue moving to the destination. Default is 16.0 blocks.
89+
* @param movementSpeedMultiplicator 1.0 for default speed, 2.0 for doubling the movement speed, and so on. Default is 1.0
90+
* @return {@link ControllableMobAction}
91+
* @throws IllegalArgumentException when maximumDistractionDistance or movementSpeedMultiplicator is zero or negative
92+
*/
93+
public ControllableMobAction attackMoveTo(Location loc, boolean queue, double maximumDistractionDistance, double movementSpeedMultiplicator) throws IllegalArgumentException;
94+
4095
/**
4196
* Orders the entity to look at the given location.
4297
* Uses the {@link ControllableMobActions#getDefaultQueuingFlag() default queuing flag}.

src/java/de/ntcomputer/minecraft/controllablemobs/api/actions/ActionType.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Can be obtained by calling {@link ControllableMobAction#getType()}
66
*
77
* @author Cybran
8-
* @version v2
8+
* @version v6
99
*
1010
*/
1111
public enum ActionType {
@@ -21,6 +21,11 @@ public enum ActionType {
2121
*/
2222
MOVE(false, false),
2323

24+
/**
25+
* @see de.ntcomputer.minecraft.controllablemobs.api.ControllableMobActions#attackMoveTo(org.bukkit.Location, boolean, double, double)
26+
*/
27+
ATTACKMOVE(false, false),
28+
2429
/**
2530
* @see de.ntcomputer.minecraft.controllablemobs.api.ControllableMobActions#target(org.bukkit.entity.LivingEntity, boolean)
2631
*/

src/java/de/ntcomputer/minecraft/controllablemobs/implementation/CraftControllableMobActions.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import de.ntcomputer.minecraft.controllablemobs.api.ControllableMobActions;
88
import de.ntcomputer.minecraft.controllablemobs.api.actions.ActionType;
99
import de.ntcomputer.minecraft.controllablemobs.api.actions.ControllableMobAction;
10+
import de.ntcomputer.minecraft.controllablemobs.implementation.actions.ControllableMobActionAttackMove;
1011
import de.ntcomputer.minecraft.controllablemobs.implementation.actions.ControllableMobActionBase;
1112
import de.ntcomputer.minecraft.controllablemobs.implementation.actions.ControllableMobActionCallback;
1213
import de.ntcomputer.minecraft.controllablemobs.implementation.actions.ControllableMobActionDie;
@@ -49,7 +50,7 @@ public ControllableMobAction moveTo(Location loc) {
4950

5051
@Override
5152
public ControllableMobAction moveTo(Location loc, boolean queue) {
52-
return this.addAction(new ControllableMobActionMove(this.actionManager, loc), queue);
53+
return this.addAction(new ControllableMobActionMove(this.actionManager, loc, 1.0D), queue);
5354
}
5455

5556
@Override
@@ -178,4 +179,26 @@ public void setDefaultQueuingFlag(boolean defaultQueue) {
178179
this.defaultQueue = defaultQueue;
179180
}
180181

182+
@Override
183+
public ControllableMobAction attackMoveTo(Location loc) {
184+
return this.attackMoveTo(loc, defaultQueue);
185+
}
186+
187+
@Override
188+
public ControllableMobAction attackMoveTo(Location loc, boolean queue) {
189+
return this.attackMoveTo(loc, queue, 16.0D);
190+
}
191+
192+
@Override
193+
public ControllableMobAction attackMoveTo(Location loc, boolean queue, double maximumDistractionDistance) throws IllegalArgumentException {
194+
return this.attackMoveTo(loc, queue, maximumDistractionDistance, 1.0D);
195+
}
196+
197+
@Override
198+
public ControllableMobAction attackMoveTo(Location loc, boolean queue, double maximumDistractionDistance, double movementSpeedMultiplicator) throws IllegalArgumentException {
199+
if(maximumDistractionDistance <= 0) throw new IllegalArgumentException("maximumDistractionDistance must be greater than 0.0");
200+
if(movementSpeedMultiplicator <= 0) throw new IllegalArgumentException("movementSpeedMultiplicator must be greater than 0.0");
201+
return this.addAction(new ControllableMobActionAttackMove(this.actionManager, loc, movementSpeedMultiplicator, maximumDistractionDistance), queue);
202+
}
203+
181204
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package de.ntcomputer.minecraft.controllablemobs.implementation.actions;
2+
3+
import net.minecraft.server.v1_6_R3.World;
4+
5+
import org.bukkit.Location;
6+
import org.bukkit.craftbukkit.v1_6_R3.CraftWorld;
7+
8+
import de.ntcomputer.minecraft.controllablemobs.api.actions.ActionType;
9+
10+
public class ControllableMobActionAbstractMove extends ControllableMobActionBase {
11+
public final int x;
12+
public final int y;
13+
public final int z;
14+
public final World world;
15+
public final double movementSpeedMultiplicator;
16+
private final boolean isValid;
17+
18+
public ControllableMobActionAbstractMove(ControllableMobActionManager manager, ActionType type, Location to, double movementSpeedMultiplicator) {
19+
super(manager, type);
20+
if(to==null) {
21+
this.x = 0;
22+
this.y = 0;
23+
this.z = 0;
24+
this.world = null;
25+
this.movementSpeedMultiplicator = 0;
26+
this.isValid = false;
27+
} else {
28+
this.x = to.getBlockX();
29+
this.y = to.getBlockY();
30+
this.z = to.getBlockZ();
31+
this.world = ((CraftWorld) to.getWorld()).getHandle();
32+
this.movementSpeedMultiplicator = Math.abs(movementSpeedMultiplicator);
33+
this.isValid = true;
34+
}
35+
}
36+
37+
@Override
38+
public boolean isValid() {
39+
return super.isValid() && this.isValid;
40+
}
41+
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.ntcomputer.minecraft.controllablemobs.implementation.actions;
2+
3+
import org.bukkit.Location;
4+
5+
import de.ntcomputer.minecraft.controllablemobs.api.actions.ActionType;
6+
7+
public class ControllableMobActionAttackMove extends ControllableMobActionAbstractMove {
8+
public final double maxDistraction;
9+
public double lastDistanceSquared = Double.MAX_VALUE;
10+
11+
public ControllableMobActionAttackMove(ControllableMobActionManager manager, Location to, double movementSpeedMultiplicator, double maxDistraction) {
12+
super(manager, ActionType.ATTACKMOVE, to, movementSpeedMultiplicator);
13+
if(this.isValid()) {
14+
this.maxDistraction = 0;
15+
} else {
16+
this.maxDistraction = Math.abs(maxDistraction);
17+
}
18+
}
19+
20+
}
Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,13 @@
11
package de.ntcomputer.minecraft.controllablemobs.implementation.actions;
22

3-
import net.minecraft.server.v1_6_R3.World;
4-
53
import org.bukkit.Location;
6-
import org.bukkit.craftbukkit.v1_6_R3.CraftWorld;
74

85
import de.ntcomputer.minecraft.controllablemobs.api.actions.ActionType;
96

10-
public class ControllableMobActionMove extends ControllableMobActionBase {
11-
public final int x;
12-
public final int y;
13-
public final int z;
14-
public final World world;
15-
private final boolean isValid;
7+
public class ControllableMobActionMove extends ControllableMobActionAbstractMove {
168

17-
public ControllableMobActionMove(final ControllableMobActionManager manager, final Location to) {
18-
super(manager, ActionType.MOVE);
19-
if(to==null) {
20-
this.x = 0;
21-
this.y = 0;
22-
this.z = 0;
23-
this.world = null;
24-
this.isValid = false;
25-
} else {
26-
this.x = to.getBlockX();
27-
this.y = to.getBlockY();
28-
this.z = to.getBlockZ();
29-
this.world = ((CraftWorld) to.getWorld()).getHandle();
30-
this.isValid = true;
31-
}
32-
}
33-
34-
@Override
35-
public boolean isValid() {
36-
return super.isValid() && this.isValid;
9+
public ControllableMobActionMove(ControllableMobActionManager manager, Location to, double movementSpeedMultiplicator) {
10+
super(manager, ActionType.MOVE, to, movementSpeedMultiplicator);
3711
}
3812

3913
}

src/java/de/ntcomputer/minecraft/controllablemobs/implementation/ai/AIGoalController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.bukkit.entity.LivingEntity;
44

55
import de.ntcomputer.minecraft.controllablemobs.implementation.CraftControllableMob;
6+
import de.ntcomputer.minecraft.controllablemobs.implementation.ai.behaviors.PathfinderGoalActionAttackMove;
67
import de.ntcomputer.minecraft.controllablemobs.implementation.ai.behaviors.PathfinderGoalActionFollow;
78
import de.ntcomputer.minecraft.controllablemobs.implementation.ai.behaviors.PathfinderGoalActionJump;
89
import de.ntcomputer.minecraft.controllablemobs.implementation.ai.behaviors.PathfinderGoalActionLook;
@@ -18,7 +19,8 @@ public AIGoalController(CraftControllableMob<E> mob) {
1819

1920
@Override
2021
protected void createActionGoals() {
21-
this.addActionGoal(-2, new PathfinderGoalActionMove(mob));
22+
this.addActionGoal(-3, new PathfinderGoalActionMove(mob));
23+
this.addActionGoal(-2, new PathfinderGoalActionAttackMove(mob));
2224
this.addActionGoal(-1, new PathfinderGoalActionFollow(mob));
2325
this.addActionGoal(0, new PathfinderGoalActionWait(mob));
2426
this.addActionGoal(Integer.MAX_VALUE, new PathfinderGoalActionJump(mob));
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package de.ntcomputer.minecraft.controllablemobs.implementation.ai.behaviors;
2+
3+
import net.minecraft.server.v1_6_R3.PathEntity;
4+
import de.ntcomputer.minecraft.controllablemobs.api.actions.ActionType;
5+
import de.ntcomputer.minecraft.controllablemobs.implementation.CraftControllableMob;
6+
import de.ntcomputer.minecraft.controllablemobs.implementation.actions.ControllableMobActionAbstractMove;
7+
import de.ntcomputer.minecraft.controllablemobs.implementation.nativeinterfaces.NativeInterfaces;
8+
9+
public class PathfinderGoalActionAbstractMove<A extends ControllableMobActionAbstractMove> extends PathfinderGoalActionDelayed<A> {
10+
private PathEntity path;
11+
12+
public PathfinderGoalActionAbstractMove(CraftControllableMob<?> mob, ActionType type) {
13+
super(mob, type);
14+
this.setMutexBits(3);
15+
}
16+
17+
@Override
18+
protected boolean canStartAction() {
19+
if(action.world!=this.mob.nmsEntity.world) return false;
20+
this.mob.adjustMaximumNavigationDistance(NativeInterfaces.ENTITY.METHOD_GETDISTANCETOLOCATION.invoke(this.mob.nmsEntity, action.x, action.y, action.z) + 50.0D);
21+
final PathEntity path = NativeInterfaces.NAVIGATION.METHOD_CREATEPATHTOLOCATION.invoke(this.mob.nmsEntity.getNavigation(), action.x, action.y, action.z);
22+
if(path==null) return false;
23+
this.path = path;
24+
return true;
25+
}
26+
27+
@Override
28+
protected void onStartAction() {
29+
NativeInterfaces.NAVIGATION.METHOD_MOVEALONGPATH.invoke(this.mob.nmsEntity.getNavigation(), this.path, 1.0);
30+
}
31+
32+
@Override
33+
protected boolean canContinueAction() {
34+
return !NativeInterfaces.NAVIGATION.METHOD_ISNOTMOVING.invoke(this.mob.nmsEntity.getNavigation());
35+
}
36+
37+
@Override
38+
protected void onEndAction() {
39+
NativeInterfaces.NAVIGATION.METHOD_STOP.invoke(this.mob.nmsEntity.getNavigation());
40+
}
41+
42+
@Override
43+
protected int getDelayTicks() {
44+
return 1;
45+
}
46+
47+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package de.ntcomputer.minecraft.controllablemobs.implementation.ai.behaviors;
2+
3+
import de.ntcomputer.minecraft.controllablemobs.api.actions.ActionType;
4+
import de.ntcomputer.minecraft.controllablemobs.implementation.CraftControllableMob;
5+
import de.ntcomputer.minecraft.controllablemobs.implementation.actions.ControllableMobActionAttackMove;
6+
import de.ntcomputer.minecraft.controllablemobs.implementation.nativeinterfaces.NativeInterfaces;
7+
8+
public class PathfinderGoalActionAttackMove extends PathfinderGoalActionAbstractMove<ControllableMobActionAttackMove> {
9+
10+
public PathfinderGoalActionAttackMove(CraftControllableMob<?> mob) {
11+
super(mob, ActionType.ATTACKMOVE);
12+
}
13+
14+
@Override
15+
protected boolean isActionBlocked() {
16+
if(this.mob.nmsEntity.getGoalTarget()==null) return false;
17+
double distanceSquared = NativeInterfaces.ENTITY.METHOD_GETDISTANCETOLOCATIONSQUARED.invoke(this.mob.nmsEntity, action.x, action.y, action.z);
18+
if(distanceSquared > this.action.lastDistanceSquared) return false;
19+
this.action.lastDistanceSquared = distanceSquared;
20+
return true;
21+
}
22+
23+
@Override
24+
protected boolean isActionRequired() {
25+
double distanceSquared = NativeInterfaces.ENTITY.METHOD_GETDISTANCETOLOCATIONSQUARED.invoke(this.mob.nmsEntity, action.x, action.y, action.z);
26+
if(this.mob.nmsEntity.getGoalTarget()!=null) {
27+
if(distanceSquared < this.action.lastDistanceSquared) {
28+
this.action.lastDistanceSquared = distanceSquared;
29+
return false;
30+
}
31+
if(Math.sqrt(distanceSquared) < Math.sqrt(this.action.lastDistanceSquared) + this.action.maxDistraction) {
32+
return false;
33+
}
34+
}
35+
return true;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)