-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAIController.java
More file actions
261 lines (217 loc) · 9.05 KB
/
AIController.java
File metadata and controls
261 lines (217 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package de.ntcomputer.minecraft.controllablemobs.implementation.ai;
import de.ntcomputer.minecraft.controllablemobs.api.ai.AIState;
import de.ntcomputer.minecraft.controllablemobs.api.ai.AIType;
import de.ntcomputer.minecraft.controllablemobs.api.ai.behaviors.AIBehavior;
import de.ntcomputer.minecraft.controllablemobs.implementation.CraftControllableMob;
import de.ntcomputer.minecraft.controllablemobs.implementation.ai.behaviors.PathfinderGoalAIMonitor;
import de.ntcomputer.minecraft.controllablemobs.implementation.ai.behaviors.PathfinderGoalWrapper;
import de.ntcomputer.minecraft.controllablemobs.implementation.nativeinterfaces.NativeInterfaces;
import de.ntcomputer.minecraft.controllablemobs.implementation.nativeinterfaces.primitives.NativeFieldObject;
import net.minecraft.server.v1_7_R1.EntityInsentient;
import net.minecraft.server.v1_7_R1.PathfinderGoal;
import net.minecraft.server.v1_7_R1.PathfinderGoalSelector;
import org.bukkit.craftbukkit.v1_7_R1.util.UnsafeList;
import org.bukkit.entity.LivingEntity;
import java.util.*;
public abstract class AIController<E extends LivingEntity> implements Comparator<Object> {
private List<PathfinderGoal> actionGoals;
private List<CraftAIPart<E,?>> attachedParts;
private List<CraftAIPart<E,?>> defaultParts;
private PathfinderGoalAIMonitor monitor;
CraftControllableMob<E> mob;
int lastBehaviorPriority = 1;
public PathfinderGoalSelector selector;
public Map<PathfinderGoal,CraftAIPart<E,?>> goalPartMap;
public AIController(CraftControllableMob<E> mob, NativeFieldObject<EntityInsentient,PathfinderGoalSelector> selectorField) {
this.mob = mob;
this.selector = selectorField.get(mob.nmsEntity);
// create custom parts list
this.attachedParts = new ArrayList<CraftAIPart<E,?>>();
this.goalPartMap = new HashMap<PathfinderGoal,CraftAIPart<E,?>>();
// copy default items
this.defaultParts = new ArrayList<CraftAIPart<E,?>>();
HashSet<PathfinderGoal> activeDefaultGoals = new HashSet<PathfinderGoal>();
List<Object> defaultItems = NativeInterfaces.PATHFINDERGOALSELECTOR.FIELD_GOALITEMS.get(this.selector);
CraftAIPart<E,?> part;
for(Object item: defaultItems) {
part = new CraftAIPart<E,AIBehavior<? super E>>(this, NativeInterfaces.PATHFINDERGOALSELECTORITEM.FIELD_PRIORITY.get(item), NativeInterfaces.PATHFINDERGOALSELECTORITEM.FIELD_GOAL.get(item));
if(NativeInterfaces.PATHFINDERGOALSELECTOR.FIELD_ACTIVEGOALITEMS.get(selector).contains(item)) {
part.setState(AIState.ACTIVE);
activeDefaultGoals.add(part.goal);
}
this.defaultParts.add(part);
this.attachedParts.add(part);
this.goalPartMap.put(part.goal, part);
}
// create action goals and monitor and sort with default goals
this.actionGoals = new ArrayList<PathfinderGoal>();
this.createActionGoals();
this.monitor = new PathfinderGoalAIMonitor(this,activeDefaultGoals);
this.addActionGoal(0, monitor);
this.sortGoals();
}
// action goal methods
protected abstract void createActionGoals();
protected void addActionGoal(final int priority, final PathfinderGoalWrapper actionGoal) {
this.actionGoals.add(actionGoal);
this.addGoal(priority, actionGoal);
}
// internal goal modifications
private void addGoal(int priority, PathfinderGoal goal) {
NativeInterfaces.PATHFINDERGOALSELECTOR.METHOD_ADDPATHFINDERGOAL.invoke(this.selector, priority, goal);
}
private void removeGoal(PathfinderGoal goal) {
PathfinderGoal searchGoal;
// shutdown and remove active item
Iterator<Object> iterator = NativeInterfaces.PATHFINDERGOALSELECTOR.FIELD_ACTIVEGOALITEMS.get(this.selector).iterator();
while(iterator.hasNext()) {
searchGoal = NativeInterfaces.PATHFINDERGOALSELECTORITEM.FIELD_GOAL.get(iterator.next());
if(searchGoal==goal) {
NativeInterfaces.PATHFINDERGOAL.METHOD_ONEND.invoke(searchGoal);
iterator.remove();
break;
}
}
((UnsafeList.Itr) iterator).valid = false;
// remove item
iterator = NativeInterfaces.PATHFINDERGOALSELECTOR.FIELD_GOALITEMS.get(this.selector).iterator();
while(iterator.hasNext()) {
searchGoal = NativeInterfaces.PATHFINDERGOALSELECTORITEM.FIELD_GOAL.get(iterator.next());
if(searchGoal==goal) {
iterator.remove();
break;
}
}
((UnsafeList.Itr) iterator).valid = false;
}
private void clearGoals() {
PathfinderGoal searchGoal;
// shutdown and remove active non-action items
Iterator<Object> iterator = NativeInterfaces.PATHFINDERGOALSELECTOR.FIELD_ACTIVEGOALITEMS.get(this.selector).iterator();
while(iterator.hasNext()) {
searchGoal = NativeInterfaces.PATHFINDERGOALSELECTORITEM.FIELD_GOAL.get(iterator.next());
if(!this.actionGoals.contains(searchGoal)) {
NativeInterfaces.PATHFINDERGOAL.METHOD_ONEND.invoke(searchGoal);
iterator.remove();
}
}
((UnsafeList.Itr) iterator).valid = false;
// remove all non-action items
iterator = NativeInterfaces.PATHFINDERGOALSELECTOR.FIELD_GOALITEMS.get(this.selector).iterator();
while(iterator.hasNext()) {
searchGoal = NativeInterfaces.PATHFINDERGOALSELECTORITEM.FIELD_GOAL.get(iterator.next());
if(!this.actionGoals.contains(searchGoal) ) iterator.remove();
}
((UnsafeList.Itr) iterator).valid = false;
}
private void sortGoals() {
Collections.sort(NativeInterfaces.PATHFINDERGOALSELECTOR.FIELD_GOALITEMS.get(this.selector), this);
}
@Override
public int compare(Object goalitem1, Object goalitem2) {
return NativeInterfaces.PATHFINDERGOALSELECTORITEM.FIELD_PRIORITY.get(goalitem1) <= NativeInterfaces.PATHFINDERGOALSELECTORITEM.FIELD_PRIORITY.get(goalitem2) ? -1 : 1;
}
// external AI access
<B extends AIBehavior<? super E>> CraftAIPart<E,B> add(B behavior) {
CraftAIPart<E,B> part = new CraftAIPart<E,B>(this, behavior);
this.attachAndSort(part);
return part;
}
private void attach(CraftAIPart<E,?> part) {
AIComponentHandlers.handleAdd(mob, part.goal);
this.addGoal(part.priority, part.goal);
this.attachedParts.add(part);
this.goalPartMap.put(part.goal, part);
part.setState(AIState.INACTIVE);
}
void attachAndSort(CraftAIPart<E,?> part) throws IllegalStateException {
this.disposedCall();
this.attach(part);
this.sortGoals();
}
void unattach(CraftAIPart<E,?> part) {
this.removeGoal(part.goal);
this.attachedParts.remove(part);
this.goalPartMap.remove(part.goal);
part.setState(AIState.UNATTACHED);
AIComponentHandlers.handleRemoved(mob, part.goal);
}
void remove(Set<AIType> typeSet, boolean remove) {
List<CraftAIPart<E,?>> toRemove = new LinkedList<CraftAIPart<E,?>>();
for(CraftAIPart<E,?> searchPart: this.attachedParts) {
if(! (typeSet.contains(searchPart.getType()) ^ remove)) toRemove.add(searchPart);
}
for(CraftAIPart<E,?> removePart: toRemove) {
this.unattach(removePart);
}
}
boolean contains(AIType type) {
for(CraftAIPart<E,?> searchPart: this.attachedParts) {
if(searchPart.getType()==type) return true;
}
return false;
}
void get(List<? super CraftAIPart<E,?>> list, Set<AIType> types) {
if(types==null) {
list.addAll(this.attachedParts);
} else {
for(CraftAIPart<E,?> part: this.attachedParts) {
if(types.contains(part.getType())) list.add(part);
}
}
}
void clear() {
this.clearGoals();
PathfinderGoal[] goals = new PathfinderGoal[this.attachedParts.size()];
int i = 0;
for(CraftAIPart<E,?> part: this.attachedParts) {
goals[i++] = part.goal;
part.setState(AIState.UNATTACHED);
}
this.attachedParts.clear();
this.goalPartMap.clear();
this.monitor.reset();
for(PathfinderGoal goal: goals) {
AIComponentHandlers.handleRemoved(mob, goal);
}
}
void reset() {
this.clear();
for(CraftAIPart<E,?> defaultPart: this.defaultParts) {
this.attach(defaultPart);
}
}
// disposing
private void disposedCall() throws IllegalStateException {
if(this.selector==null) throw new IllegalStateException("[ControllableMobsAPI] you must not modify AI parts after the ControllableMob has been released");
}
void dispose() {
// shutdown and remove active items
final List<Object> activeItems = NativeInterfaces.PATHFINDERGOALSELECTOR.FIELD_ACTIVEGOALITEMS.get(this.selector);
for(Object item: activeItems) {
NativeInterfaces.PATHFINDERGOAL.METHOD_ONEND.invoke(NativeInterfaces.PATHFINDERGOALSELECTORITEM.FIELD_GOAL.get(item));
}
activeItems.clear();
// remove all items
NativeInterfaces.PATHFINDERGOALSELECTOR.FIELD_GOALITEMS.get(this.selector).clear();
// restore default items
for(CraftAIPart<E,?> defaultPart: this.defaultParts) {
this.attach(defaultPart);
}
// set all AI parts to unattached
for(CraftAIPart<E,?> part: this.attachedParts) {
part.setState(AIState.UNATTACHED);
}
this.attachedParts.clear();
this.goalPartMap.clear();
this.defaultParts.clear();
// nullify
this.monitor = null;
this.attachedParts = null;
this.goalPartMap = null;
this.defaultParts = null;
this.actionGoals = null;
this.selector = null;
this.mob = null;
}
}