-
Notifications
You must be signed in to change notification settings - Fork 0
/
WarLightBrainController.java
434 lines (352 loc) · 12 KB
/
WarLightBrainController.java
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package feelsgoodman;
import java.util.ArrayList;
import edu.warbot.agents.MovableWarAgent;
import edu.warbot.agents.enums.WarAgentType;
import edu.warbot.agents.percepts.WarAgentPercept;
import edu.warbot.brains.WarBrain;
import edu.warbot.brains.brains.WarLightBrain;
import edu.warbot.communications.WarMessage;
public abstract class WarLightBrainController extends WarLightBrain {
WTask ctask;
public int _timer;
public double _dist;
public boolean _patrouille;
public boolean _relais;
public WarLightBrainController() {
super();
this.ctask = idleTask;
this._timer = 5;
this._dist = 0;
this._patrouille = true;
this._relais = false;
}
static WTask idleTask = new WTask(){
String exec(WarBrain bc){
WarLightBrainController me = (WarLightBrainController) bc;
if(me._timer > 0){
for(WarMessage m : me.getMessages()){
if(m.getMessage().equals("help me now")){
me.ctask = defTask;
me.setHeading(Double.parseDouble(m.getContent()[0]));
return ACTION_MOVE;
}
}
}
else{
//Si pas de message de la base
for(WarMessage m : me.getMessages()) {
if(m.getMessage().equals("fin de patrouille"));
me._patrouille = false;
me.ctask = huntTask;
return ACTION_MOVE;
}
me.ctask = patrouilleTask;
return ACTION_MOVE;
}
me._timer--;
return ACTION_IDLE;
}
};
static WTask patrouilleTask = new WTask() {
String exec(WarBrain bc) {
WarLightBrainController me = (WarLightBrainController) bc;
for(WarMessage m : me.getMessages()) {
if(m.getMessage().equals("Patrouille done")) {
me.ctask = huntTask;
}
}
for (WarAgentPercept wp : me.getPerceptsEnemies()) {
if (wp.getType().equals(WarAgentType.WarLight) || wp.getType().equals(WarAgentType.WarEngineer) || wp.getType().equals(WarAgentType.WarExplorer)) {
return me.attack(wp);
}
}
ArrayList<WarAgentPercept> foodPercepts = (ArrayList<WarAgentPercept>) me
.getPercepts();
// Si il y a des percepts
if (foodPercepts != null && foodPercepts.size() > 0) {
//Nourriture la plus proche normalement
WarAgentPercept foodP = null;
for (WarAgentPercept war : foodPercepts) {
if (war.getType() == WarAgentType.WarFood && foodP == null)
foodP = war;
}
if (foodP != null) {
me.ctask = protectTask;
me.broadcastMessageToAgentType(WarAgentType.WarExplorer, "I get food !", "");
return ACTION_MOVE;
}
}
WarMessage mess = me.getMessageAboutFood();
if(mess != null) {
me.setHeading(me.getMessageAboutFood().getAngle());
return (MovableWarAgent.ACTION_MOVE);
}
return ACTION_MOVE;
}
};
static WTask protectTask = new WTask() {
String exec(WarBrain bc) {
WarLightBrainController me = (WarLightBrainController) bc;
if(!me._relais) me.broadcastMessageToAgentType(WarAgentType.WarEngineer, "spot here", "");
for(WarMessage m : me.getMessages()) {
if(m.getMessage().equals("I come to work")) {
me._relais = true;
}
}
for (WarAgentPercept wp : me.getPerceptsEnemies()) {
if (wp.getType().equals(WarAgentType.WarLight) || wp.getType().equals(WarAgentType.WarEngineer) || wp.getType().equals(WarAgentType.WarExplorer)) {
return me.attack(wp);
}
}
for(WarMessage m : me.getMessages()){
if(m.getMessage().equals("Patrouille done"))
me.ctask = huntTask;
}
me.setHeading(me.getHeading() + 10);
return ACTION_MOVE;
}
};
static WTask defTask = new WTask(){
String exec(WarBrain bc){
WarLightBrainController me = (WarLightBrainController) bc;
for(WarMessage m : me.getMessages()){
if(m.getMessage().equals("I am safe")){
me.ctask = huntTask;
me.setHeading(me.getHeading() + 180);
return ACTION_MOVE;
}
if(m.getMessage().equals("danger here")) {
me.setHeading(me.getIndirectPositionOfAgentWithMessage(m).getAngle());
}
}
for (WarAgentPercept wp : me.getPerceptsEnemies()) {
if (wp.getType().equals(WarAgentType.WarRocketLauncher)) {
System.out.println("bullet?");
return me.attack(wp);
}
}
if (me.isBlocked())
me.setRandomHeading();
return ACTION_MOVE;
}
};
static WTask godefTask = new WTask(){
String exec(WarBrain bc){
WarLightBrainController me = (WarLightBrainController) bc;
for(WarMessage m : me.getMessages()){
if(m.getMessage().equals("I am safe")){
me.ctask = huntTask;
me.setHeading(me.getHeading() + 180);
return ACTION_MOVE;
}
if(m.getMessage().equals("danger here")) {
me.setDebugString("coucou " + me.getIndirectPositionOfAgentWithMessage(m).getAngle());
me.setHeading(me.getIndirectPositionOfAgentWithMessage(m).getAngle());
if(me.getIndirectPositionOfAgentWithMessage(m).getDistance() < 100) {
me.broadcastMessageToAgentType(WarAgentType.WarBase, "j'y suis", "");
me.ctask = defTask;
}
}
}
if (me.isBlocked())
me.setRandomHeading();
return ACTION_MOVE;
}
};
static WTask huntTask = new WTask(){
String exec(WarBrain bc){
WarLightBrainController me = (WarLightBrainController) bc;
for (WarAgentPercept wp : me.getPerceptsEnemies()) {
if(wp.getType().equals(WarAgentType.WarBase)){
String ang = "";
ang += wp.getAngle();
String dis = "";
dis += wp.getDistance();
String[] content = new String[]{ang, dis};
me.broadcastMessageToAll("Base found !", content);
}
if (!wp.getType().equals(WarAgentType.WarBase) && !wp.getType().equals(WarAgentType.WarFood) && !wp.getType().equals(WarAgentType.WarRocket)) {
if(wp.getType().equals(WarAgentType.WarHeavy) || wp.getType().equals(WarAgentType.WarTurret)){
String ang = "";
ang += wp.getAngle();
String dis = "";
dis += wp.getDistance();
String[] content = new String[]{ang, dis};
if(wp.getType().equals(WarAgentType.WarTurret)){
me.broadcastMessageToAll("turret enemi here", content);
}
else me.broadcastMessageToAll("heavy enemi here", content);
me.setHeading(me.getHeading() - 60);
}
else{
return me.attack(wp);
}
}
}
for(WarMessage m : me.getMessages()){
if(m.getMessage().equals("help me now") && m.getDistance() < 200){
me.reply(m, "I am coming now", "");
me._dist = m.getDistance();
me.ctask = godefTask;
me.setHeading(m.getAngle());
}
else if(m.getMessage().equals("help me now") && m.getDistance() < 300){
me.reply(m, "I am coming", "");
me._dist = m.getDistance();
me.ctask = godefTask;
me.setHeading(m.getAngle());
}
else if(m.getMessage().equals("rocket enemi here") && m.getDistance() < 200) {
me.setHeading(m.getAngle());
}
else if(m.getMessage().equals("Attack!")) {
System.out.println("attack!!!!!");
me.ctask = finalAssaultTask;
}
}
if (me.isBlocked())
me.setRandomHeading();
return ACTION_MOVE;
}
};
static WTask finalAssaultTask = new WTask() {
String exec(WarBrain bc) {
WarLightBrainController me = (WarLightBrainController) bc;
for (WarAgentPercept wp : me.getPerceptsEnemies()) {
if(wp.getType().equals(WarAgentType.WarBase)){
String ang = "";
ang += wp.getAngle();
String dis = "";
dis += wp.getDistance();
String[] content = new String[]{dis, ang};
me.broadcastMessageToAll("Base found !", content);
ArrayList<WarAgentPercept> alliesPercepts = (ArrayList<WarAgentPercept>) me.getPerceptsAllies();
/*if(!alliesPercepts.isEmpty()){
for(WarAgentPercept wpAllie : alliesPercepts){
if(me.getHeading() == wpAllie.getAngle()){
System.out.println("ne tire pas");
return ACTION_IDLE;
}
}
}*/
if (me.isReloaded())
return ACTION_FIRE;
else if (me.isReloading()) {
me.setHeading(me.getHeading() + 60);
return ACTION_IDLE;
}
else
return ACTION_RELOAD;
}
if (!wp.getType().equals(WarAgentType.WarBase) && !wp.getType().equals(WarAgentType.WarFood) && !wp.getType().equals(WarAgentType.WarRocket)) {
if(wp.getType().equals(WarAgentType.WarHeavy)){
String ang = "";
ang += wp.getAngle();
String dis = "";
dis += wp.getDistance();
String[] content = new String[]{ang, dis};
if(wp.getType().equals(WarAgentType.WarHeavy)){
me.broadcastMessageToAll("heavy enemi here", content);
}
me.setHeading(me.getHeading() - 60);
}
else{
return me.attack(wp);
}
}
}
for(WarMessage m : me.getMessages()) {
if(m.getMessage().equals("Base found !")) {
me.setHeading(m.getAngle() + 10);
}
}
if (me.isBlocked())
me.setRandomHeading();
return ACTION_MOVE;
}
};
public String attack(WarAgentPercept enemi){
ArrayList<WarAgentPercept> alliesPercepts = (ArrayList<WarAgentPercept>) getPerceptsAllies();
if(!alliesPercepts.isEmpty()){
for(WarAgentPercept wp : alliesPercepts){
if(getHeading() == wp.getAngle()){
System.out.println("ne tire pas");
return ACTION_IDLE;
}
}
}
//Combien de tick avant de toucher l'enemi
int timing = (int) enemi.getDistance()/10;
if(enemi.getDistance()%10 > 0) timing++;
//Determine la distance parcourue par l'enemi avant que le tire touche
double distancePrevision = 0;
if(enemi.getType().equals(WarAgentType.WarLight)){
distancePrevision = 1.8 * timing;
}
else if(enemi.getType().equals(WarAgentType.WarHeavy)){
distancePrevision = 0.8 * timing;
}
else if(enemi.getType().equals(WarAgentType.WarRocketLauncher)){
distancePrevision = 1.0 * timing;
}
else if(enemi.getType().equals(WarAgentType.WarKamikaze)){
distancePrevision = 1.0 * timing;
}
else if(enemi.getType().equals(WarAgentType.WarExplorer)){
distancePrevision = 2.0 * timing;
}
else if(enemi.getType().equals(WarAgentType.WarEngineer)){
distancePrevision = 1.0 * timing;
}
//Viser là où sera l'enemi
Vector2 v = new Vector2();
Double[] d = v.getDist(enemi.getDistance(), enemi.getAngle(), distancePrevision, enemi.getHeading());
this.setHeading(d[1]);
if (this.isReloaded())
return ACTION_FIRE;
else if (this.isReloading())
return ACTION_MOVE;
else
return ACTION_RELOAD;
}
private WarMessage getMessageAboutFood() {
if(!this.getMessages().isEmpty()){
WarMessage message = this.getMessages().get(0);
for( WarMessage wm : this.getMessages()){
if(wm.getMessage().equals("I get food !") && wm.getDistance() <= message.getDistance()){
message = wm;
}
}
if(message.getMessage().equals("I get food !")){
return message;
}
}
return null;
}
@Override
public String action() {
String toReturn = ctask.exec(this);
if(toReturn == null){
for (WarAgentPercept wp : getPerceptsEnemies()) {
if (!wp.getType().equals(WarAgentType.WarBase) && !wp.getType().equals(WarAgentType.WarFood)) {
setHeading(wp.getAngle());
this.setDebugString("Attaque");
if (isReloaded())
return ACTION_FIRE;
else if (isReloading())
return ACTION_IDLE;
else
return ACTION_RELOAD;
}
}
if (isBlocked())
setRandomHeading();
return ACTION_MOVE;
}
else{
if (isBlocked())
setRandomHeading();
return toReturn;
}
}
}