-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player051BasicAI.oz
630 lines (560 loc) · 17.8 KB
/
Player051BasicAI.oz
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
functor
import
Input
System
OS
export
portPlayer:StartPlayer
define
fun{GenerateInitialState}
state(idPlayer:_ lives:_ isAlive:_ currentPosition:_ counterMine:_ counterMissile:_ counterDrone:_ counterSonar: _ path:_ isUnderSurface:_ listMine:_ nbrMinePlaced:_ closestEnemyPos:_)
end
%Get a random element From Result, where N is Max index to choose
fun{GetRandomElem Result N} I in
I = ({OS.rand} mod N) + 1
{GetElementInList Result I}
end
%Get the I th element of L
fun{GetElementInList L I}
case L of H|T then
if I == 1 then
H
else
{GetElementInList T I-1}
end
[] nil then
nil
end
end
fun{FindPointInList L I}
case L of nil then
no
[] H|T then
if I == H then
yes
else
{FindPointInList T I}
end
end
end
fun{CanMoveTo X Y Map} List Point in
if (X == 0 orelse X > (Input.nRow +1) orelse Y == 0 orelse Y > (Input.nColumn +1 )) then
no
else
List={GetElementInList Map X}
Point={GetElementInList List Y}
case Point of 1 then
no
[] 0 then
yes
else
no
end
end
end
fun{InitPos Map} P Value in
P = pt(x:({OS.rand}mod 10)+1 y:({OS.rand}mod 10)+1)
if({CanMoveTo P.x P.y Map} == no) then
Value = {InitPos Map}
else
Value = P
end
Value
end
fun{GenerateDirection State Map}
P
Direction
in
P = State.currentPosition
%WORK but bloked when he arrive on the possition
if State.closestEnemyPos \= nil then
if (State.currentPosition.x >= State.closestEnemyPos.x ) andthen ({CanMoveTo P.x-1 P.y Map} == yes andthen {FindPointInList State.path pt(x:P.x-1 y:P.y)} == no) then
Direction = north
elseif (State.currentPosition.y =< State.closestEnemyPos.y ) andthen ({CanMoveTo P.x P.y+1 Map} == yes andthen {FindPointInList State.path pt(x:P.x y:P.y+1)} == no) then
Direction = east
elseif (State.currentPosition.x =< State.closestEnemyPos.x ) andthen ({CanMoveTo P.x+1 P.y Map} == yes andthen {FindPointInList State.path pt(x:P.x+1 y:P.y)} == no) then
Direction = south
elseif (State.currentPosition.y >= State.closestEnemyPos.y ) andthen ({CanMoveTo P.x P.y-1 Map} == yes andthen {FindPointInList State.path pt(x:P.x y:P.y-1)} == no) then
Direction = west
else
Direction = surface
end
else
if ({CanMoveTo P.x-1 P.y Map} == yes andthen {FindPointInList State.path pt(x:P.x-1 y:P.y)} == no) then
Direction = north
elseif ({CanMoveTo P.x P.y+1 Map} == yes andthen {FindPointInList State.path pt(x:P.x y:P.y+1)} == no) then
Direction = east
elseif ({CanMoveTo P.x+1 P.y Map} == yes andthen {FindPointInList State.path pt(x:P.x+1 y:P.y)} == no) then
Direction = south
elseif ({CanMoveTo P.x P.y-1 Map} == yes andthen {FindPointInList State.path pt(x:P.x y:P.y-1)} == no) then
Direction = west
else
Direction = surface
end
end
Direction
end
fun {GetNewPosition Direction CurrentPosition}
Position
in
case Direction of north then
Position = pt(x:CurrentPosition.x-1 y:CurrentPosition.y)
[] east then
Position = pt(x:CurrentPosition.x y:CurrentPosition.y+1)
[] south then
Position = pt(x:CurrentPosition.x+1 y:CurrentPosition.y)
[] west then
Position = pt(x:CurrentPosition.x y:CurrentPosition.y-1)
[] surface then
Position = CurrentPosition
end %case
Position
end %fun
%Create a new state based on the previous one
%Param Value = the attribute to update with the value of Result
fun{StateModification State Value Result} NewState PositionMine in
NewState = {GenerateInitialState}
if Value == 'initPath' then
NewState.currentPosition = Result
NewState.path = Result|nil
else
if Value == 'position' then
NewState.currentPosition = Result
NewState.path = Result|State.path
else
NewState.currentPosition = State.currentPosition
NewState.path = State.path
end
end
if Value == 'updateLife' then
NewState.lives = Result
else
NewState.lives = State.lives
end
if NewState.lives > 0 then
NewState.isAlive = true
NewState.idPlayer = State.idPlayer
else
NewState.isAlive = false
NewState.idPlayer = nil
%{System.showInfo '[DEAD] '#State.idPlayer.name#' at position : '#State.currentPosition.x#'-'#State.currentPosition.y}
end
if Value == 'chargeItem' then
if Result == mine then
NewState.counterMine = State.counterMine+1
else
NewState.counterMine = State.counterMine
end
if Result == missile then
NewState.counterMissile = State.counterMissile+1
else
NewState.counterMissile = State.counterMissile
end
if Result == drone then
NewState.counterDrone = State.counterDrone+1
else
NewState.counterDrone = State.counterDrone
end
if Result == sonar then
NewState.counterSonar = State.counterSonar+1
else
NewState.counterSonar = State.counterSonar
end
else
if Value == 'removeItem' then
if Result == mine then
NewState.counterMine = State.counterMine-Input.mine
else
NewState.counterMine = State.counterMine
end
if Result == missile then
NewState.counterMissile = State.counterMissile-Input.missile
else
NewState.counterMissile = State.counterMissile
end
if Result == drone then
NewState.counterDrone = State.counterDrone-Input.drone
else
NewState.counterDrone = State.counterDrone
end
if Result == sonar then
NewState.counterSonar = State.counterSonar-Input.sonar
else
NewState.counterSonar = State.counterSonar
end
else
NewState.counterMine = State.counterMine
NewState.counterMissile = State.counterMissile
NewState.counterSonar = State.counterSonar
NewState.counterDrone = State.counterDrone
end
end
if Value == 'fireMine' then
NewState.listMine = Result|State.listMine
NewState.nbrMinePlaced = State.nbrMinePlaced + 1
else
if Value == 'blowMine' then
NewState.nbrMinePlaced = State.nbrMinePlaced - 1
NewState.listMine = Result
else
NewState.nbrMinePlaced = State.nbrMinePlaced
NewState.listMine = State.listMine
end
end
if Value == 'dive' then
NewState.isUnderSurface = Result
else
NewState.isUnderSurface = State.isUnderSurface
end
if Value == 'changeEnemy' then
NewState.closestEnemyPos = Result
else
NewState.closestEnemyPos = State.closestEnemyPos
end
NewState
end
%TODO change the list when postion sonar in state received
fun{GenerateItem State} List Value Charged in
if State.closestEnemyPos \= nil then
% 50% missile and 25% mine 25% sonar
List = [missile missile mine sonar]
Value = {GetRandomElem List 3}
else
% 30% sonar, missile, mine and 10% drone
List = [mine mine mine missile missile missile sonar sonar sonar drone]
Value = {GetRandomElem List 10}
%List = [sonar sonar sonar]
%Value = {GetRandomElem List 3}
end
case Value of mine then
if(State.counterMine+1 >= Input.mine) then Charged = true end
[] missile then
if(State.counterMissile+1 >= Input.missile) then Charged = true end
[] sonar then
if(State.counterSonar+1 >= Input.sonar) then Charged = true end
[] drone then
if(State.counterDrone+1 >= Input.drone) then Charged = true end
else
Charged = false
end
if {IsDet Charged} == false then
Charged = false
end
info(item:Value isCharged:Charged)
end
fun{GetItemReady State} P D in
if State.closestEnemyPos \= nil then
D = {DistanceFrom State.closestEnemyPos State.currentPosition}
if D >= Input.minDistanceMissile andthen D =< Input.maxDistanceMissile andthen State.counterMissile >= Input.missile then
% Missile can touch the point & it's ready
info(item:missile val:missile(State.closestEnemyPos) firedOnPos:true)
elseif (State.counterMine >= Input.mine) then
%Place a mine if ready
P={PositionToFire State.currentPosition 0 Input.minDistanceMine Input.maxDistanceMine nil}
info(item:mine val:mine(P) firedOnPos:false)
else
info(item:nil val:nil firedOnPos:false)
end
else
if (State.counterSonar >= Input.sonar) then
info(item:sonar val:sonar firedOnPos:false)
elseif(State.counterMissile >= Input.missile) then
P={PositionToFire State.currentPosition 0 Input.minDistanceMissile Input.maxDistanceMissile nil}
info(item:missile val:missile(P) firedOnPos:false)
elseif (State.counterMine >= Input.mine) then
P={PositionToFire State.currentPosition 0 Input.minDistanceMine Input.maxDistanceMine nil}
info(item:mine val:mine(P) firedOnPos:false)
elseif (State.counterDrone >= Input.drone) then
if ({OS.rand} mod 2) == 1 then
%row x
info(item:drone val:drone(row:({OS.rand} mod Input.nRow +1)) firedOnPos:false)
else
%column y
info(item:drone val:drone(column:({OS.rand} mod Input.nColumn +1)) firedOnPos:false)
end
else
info(item:nil val:nil firedOnPos:false)
end%end check counter
end%end else 1st if
%info(item:nil val:nil)
end
%TODO distance the closed position of an enemy in state
fun{PositionToFire CurrentP N Min Max Path} ReturnValue List Direction P in
List = [north south east west]
Direction = {GetRandomElem List 4}
P = {GetNewPosition Direction CurrentP}
if ({FindPointInList Path P} == yes orelse {CanMoveTo P.x P.y Input.map} == no) then
ReturnValue = {PositionToFire CurrentP N Min Max Path}
else
if N < Min then
ReturnValue = {PositionToFire P N+1 Max Min P|Path}
elseif N >= Min andthen N < Max then
if ({OS.rand} mod 2) == 1 then
ReturnValue = P
else
ReturnValue = {PositionToFire P N+1 Max Min P|Path}
end
else
ReturnValue = P
end
end
ReturnValue
end%fun
proc{PrintPath L}
case L
of nil then
skip
[] H|T then
{System.showInfo '[DEBUG] Path : pt(x:'#H.x#' y:'#H.y#')'}
end
skip
end
fun{RemoveElementFrom L E}
case L
of nil then
nil
[] H|T then
if H == E then
T
else
H|{RemoveElementFrom T E}
end
end
end
fun{DistanceFrom P1 P2}
{Abs P1.x - P2.x} + {Abs P1.y - P2.y}
end
StartPlayer
TreatStream
Dive
in
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fun{StartPlayer Color ID}
Stream
Port
State
in
{NewPort Stream Port}
thread
State = {GenerateInitialState}
State.idPlayer = id(id:ID color:Color name:'Player'#ID#'-IA')
State.lives = Input.maxDamage
{TreatStream Stream State}
end
Port
end %fun
%TODO
proc{TreatStream Stream State} % has as many parameters as you want
case Stream
of nil then skip
[] initPosition(?ID ?Position)|T then NewState in
%{System.showInfo 'initPosition'}
ID = State.idPlayer
State.currentPosition = {InitPos Input.map}
State.counterMine = 0
State.counterDrone = 0
State.counterSonar = 0
State.counterMissile = 0
State.isUnderSurface = false
Position = State.currentPosition
State.listMine = nil|nil
State.nbrMinePlaced = 1
State.closestEnemyPos = nil
NewState = {StateModification State 'initPath' Position}
{TreatStream T NewState}
[]move(?ID ?Position ?Direction)|T then NewState Pos in
%{System.showInfo 'move()'}
ID = State.idPlayer
Direction = {GenerateDirection State Input.map}
Pos = {GetNewPosition Direction State.currentPosition}
if Direction == surface then
NewState = {StateModification State 'initPath' Pos}
else
NewState = {StateModification State 'position' Pos}
end
Position = NewState.currentPosition
{TreatStream T NewState}
[] dive|T then NewState in
% {System.showInfo 'dive()'}
Dive = true
NewState = {StateModification State 'dive' true}
{TreatStream T NewState}
[] chargeItem(?ID ?KindItem)|T then NewItemTuple NewState in
%{System.showInfo 'chargeItem()'}
ID = State.idPlayer
NewItemTuple = {GenerateItem State}
NewState = {StateModification State 'chargeItem' NewItemTuple.item}
if(NewItemTuple.isCharged) then
KindItem = NewItemTuple.item
end
%{System.showInfo ' | Charged : Mine : '#NewState.counterMine#' Missile : '#NewState.counterMissile#' Drone : '#NewState.counterDrone#' Sonar'#NewState.counterSonar}
{TreatStream T NewState}
[] fireItem(?ID ?FireItem)|T then ItemReady NewState NewState2 NewState3 Position in
%{System.showInfo 'fireItem()'}
ID = State.idPlayer
ItemReady = {GetItemReady State}
if ItemReady.val \= nil then
FireItem = ItemReady.val
end
if ItemReady.item == mine then
NewState2 = {StateModification State 'fireMine' ItemReady.val}
else
NewState2 = {StateModification State nil nil}
end
NewState = {StateModification NewState2 'removeItem' ItemReady.item}
if ItemReady.firedOnPos then
NewState3 = {StateModification NewState2 'changeEnemy' nil}
else
NewState3 = {StateModification NewState2 nil nil}
end
{TreatStream T NewState3}
[]fireMine(?ID ?Mine)|T then NewState CurrentM L in
%{System.showInfo 'fireMine()'}
CurrentM = {GetRandomElem State.listMine State.nbrMinePlaced}
ID = State.idPlayer
if CurrentM \= nil then
L = {RemoveElementFrom State.listMine CurrentM}
NewState = {StateModification State 'blowMine' L}
Mine = CurrentM
else
NewState = {StateModification State nil nil}
end
{TreatStream T NewState}
[]isSurface(?ID ?Answer)|T then NewState in
ID = State.idPlayer
Answer = State.underSurface
{TreatStream T State}
[]sayMove(ID Direction)|T then NewState in
if ID \= nil andthen State.idPlayer \= nil then
{System.showInfo '[RADIO#'#State.idPlayer.id#'] '#ID.name#' has moved to '#Direction}
end
{TreatStream T State}
[]saySurface(ID)|T then NewState in
if ID \= nil andthen State.idPlayer \= nil then
{System.showInfo '[RADIO#'#State.idPlayer.id#'] '#ID.name#' has made surface'}
end
{TreatStream T State}
[]sayCharge(ID KindItem)|T then NewState in
if ID \= nil andthen State.idPlayer \= nil then
{System.showInfo '[RADIO#'#State.idPlayer.id#'] '#ID.name#' has Charged a '#KindItem}
end
{TreatStream T State}
[]sayMinePlaced(ID)|T then NewState in
if ID \= nil andthen State.idPlayer \= nil then
{System.showInfo '[RADIO#'#State.idPlayer.id#'] '#ID.name#' has planted a mine'}
end
{TreatStream T State}
%TODO Check correct
[]sayMissileExplode(ID Position ?Message)|T then NewState Distance Damage LifeLeft in
if ID \= nil andthen State.idPlayer \= nil then
{System.showInfo '[RADIO#'#State.idPlayer.id#'] '#ID.name#' launched a missile at the coordonate '#Position.x#'-'#Position.y}
end
Distance = {DistanceFrom Position State.currentPosition}
if Distance == 0 then
Damage = 2
elseif Distance == 1 then
Damage = 1
else
Damage = 0
end
LifeLeft = (State.lives - Damage)
if LifeLeft > 0 then
Message = sayDamageTaken(State.idPlayer Damage LifeLeft)
else %submarine Destroyed
Message = sayDeath(State.idPlayer)
end
NewState = {StateModification State updateLife LifeLeft}
{TreatStream T NewState}
[]sayMineExplode(ID Position ?Message)|T then NewState Distance Damage LifeLeft in
if ID \= nil andthen State.idPlayer \= nil then
{System.showInfo '[RADIO#'#State.idPlayer.id#'] '#ID.name#' blow a mine at the coordonate '#Position.x#'-'#Position.y}
end
Distance = {DistanceFrom Position State.currentPosition}
if Distance == 0 then
Damage = 2
elseif Distance == 1 then
Damage = 1
else
Damage = 0
end
LifeLeft = (State.lives - Damage)
if LifeLeft > 0 then
Message = sayDamageTaken(State.idPlayer Damage LifeLeft)
else %submarine Destroyed
Message = sayDeath(State.idPlayer)
end
NewState = {StateModification State updateLife LifeLeft}
{TreatStream T NewState}
[]sayPassingDrone(Drone ?ID ?Answer)|T then NewState in
ID = State.idPlayer
case Drone
of drone(row:X) then
if X == State.currentPosition.x then
Answer = true
else
Answer = false
end
[] drone(column:Y) then
if Y == State.currentPosition.y then
Answer = true
else
Answer = false
end
else
Answer = false
end
{TreatStream T State}
[]sayAnswerDrone(Drone ID Answer)|T then NewState in
if ID \= nil andthen State.idPlayer \= nil then
if Answer then
case Drone
of drone(row:X) then
{System.showInfo '[RADIO] '#ID.name#' is on the row '#X}
[] drone(column:Y) then
{System.showInfo '[RADIO] '#ID.name#' is on the column '#Y}
else
{System.showInfo 'Problem Drone'}
end
else
{System.showInfo '[RADIO] '#ID.name#' isn\'t on the range of the drone'}
end
end
{TreatStream T State}
%TODO
[]sayPassingSonar(?ID ?Answer)|T then NewState P in
if ({OS.rand } mod 2) == 0 then
%X correct
P = pt(x:_ y:_)
P.x = State.currentPosition.x
P.y= ({OS.rand} mod Input.nColumn)+1
else
%Y correct
P = pt(x:_ y:_)
P.y = State.currentPosition.y
P.x= ({OS.rand} mod Input.nRow)+1
end
ID = State.idPlayer
Answer = P
{TreatStream T State}
[]sayAnswerSonar(ID Answer)|T then NewState in
if ID \= nil andthen State.idPlayer \= nil then
{System.showInfo '[RADIO#'#State.idPlayer.id#'] '#ID.name #' has been detected at position '#Answer.x #'-'#Answer.y#' by the sonar'}
NewState = {StateModification State changeEnemy Answer}
end
{TreatStream T NewState}
[]sayDeath(ID)|T then NewState in
if ID \= nil andthen State.idPlayer \= nil then
{System.showInfo '[RADIO#'#State.idPlayer.id#'] '#ID.name#' is dead'}
end
{TreatStream T State}
[]sayDamageTaken(ID Damage LifeLeft)|T then NewState in
if ID \= nil andthen State.idPlayer \= nil then
{System.showInfo '[RADIO#'#State.idPlayer.id#'] '#ID.name#' has taken ' #Damage#' damage(s) [LIFELEFT = '#LifeLeft#']'}
end
{TreatStream T State}
[] _|T then NewState in
NewState = {StateModification State nil nil}
{TreatStream T NewState}
end %case
end %proc
end