forked from mellendo/Halite2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyBot.py
More file actions
1408 lines (1200 loc) · 51.1 KB
/
MyBot.py
File metadata and controls
1408 lines (1200 loc) · 51.1 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
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
"""
import hlt, time
import logging
import random
import collections
import math
import copy
############################################################################
DOLOG = hlt.constants.DOLOG
random.seed()
numFirstPlanetChosen = 0
retreatCornerShips = dict()
############################################################################
def addOppDocksToObstLists() :
for enemyShip in oppDocks :
if not enemyShip.isMobile() :
obstLists.growObsts.append(enemyShip)
obstLists.allEntities.append(enemyShip)
obstLists.sitters.append(enemyShip)
############################################################################
def randomBool() :
return random.getrandbits(1)
############################################################################
class ObstLists() :
def __init__(self) :
pass
############################################################################
BB = 3
def getBC(z) :
return int(z) >> BB
def getKey(e) :
return (getBC(e.x),getBC(e.y))
def getInvKey(key) :
return hlt.entity.Position(key[0] << BB,key[1] << BB)
class MapBlocks() :
def __init__(self) :
self.blocks = collections.defaultdict(list)
def updateBlocks(self) :
self.blocks.clear()
for player in game_map.all_players():
for ship in player.all_ships():
self.blocks[getKey(ship)].append(ship)
def getCloseInBlocks(self,e,dist) :
lowX = getBC(e.x - dist)
upX = getBC(e.x + dist) + 1
lowY = getBC(e.y - dist)
upY = getBC(e.y + dist) + 1
eX = getBC(e.x)
eY = getBC(e.y)
close = []
sqrDist = dist ** 2
for x in range(lowX,upX) :
for y in range(lowY,upY) :
key = (x,y)
if True or key in self.blocks : #HACK
if x == eX or y == eY or \
x < eX and y < eY and \
e.squareDist(getInvKey((x+1,y+1))) < sqrDist or \
x < eX and y > eY and \
e.squareDist(getInvKey((x+1,y))) < sqrDist or \
x > eX and y < eY and \
e.squareDist(getInvKey((x,y+1))) < sqrDist or \
x > eX and y > eY and \
e.squareDist(getInvKey((x,y))) < sqrDist :
if DOLOG :
logging.info("addKey: {} {} {}".format(\
dist,e,getInvKey(key)))
for be in self.blocks[key] :
if e.squareDist(be) < sqrDist :
close.append(be)
if DOLOG :
logging.info("addToClose: {} {}".format(\
e,be))
if DOLOG :
logging.info("closeToE: {} close {}".format(e,close))
return close
############################################################################
Attacker = hlt.entity.Attacker
Defender = hlt.entity.Defender
Explorer = hlt.entity.Explorer
def getShipRoles() :
for player in game_map.all_players():
pid = player.id
for ship in player.all_ships():
minDist = 99999
minPlanet = None
for planet in game_map.all_planets():
#if not planet.owner : continue
d = ship.calculate_distance_between(planet) - planet.radius
if d < minDist :
minDist = d
minPlanet = planet
if minPlanet.owner :
if minPlanet.owner.id != pid :
ship.role = Attacker
else :
ship.role = Defender
else :
ship.role = Explorer
ship.dist2Planet = minDist
ship.nearestPlanet = minPlanet
############################################################################
def getMyOppHNP(totalShipHealthNProd) :
myHNP = 0
oppHNP = 0
for hnpID in totalShipHealthNProd :
if hnpID == mePlayer.id :
myHNP += totalShipHealthNProd[hnpID]
else :
oppHNP += totalShipHealthNProd[hnpID]
return myHNP, oppHNP
def logShips(game_map, ships) :
for ship in ships :
minDist = 99999
for planet in game_map.all_planets():
d = ship.calculate_distance_between(planet) - planet.radius
if d < minDist :
minDist = d
minPlanet = planet
logging.info("shipMinDist: {} ship: {} planet {}".format(minDist, ship, minPlanet))
def logGameState(game_map) :
logging.info("turnNumber: {} ".format(turnNumber))
myShipNum = len(mePlayer.all_ships())
oppShipNum = len(game_map.oppShips())
shipDiff = myShipNum - oppShipNum
logging.info("shipDiff: {} my: {} opp: {}".format(shipDiff, myShipNum, oppShipNum))
logging.info("hnpDiff: {} my: {} opp: {}".format(\
myHNP - oppHNP, myHNP, oppHNP))
if myShipNum + oppShipNum <= 30 :
logging.info("myShips:")
logShips(game_map, mePlayer.all_ships())
logging.info("oppShips:")
logShips(game_map, game_map.oppShips())
oppShipsPos = list()
myIdDests = list()
def recordShipInfo(game_map) :
myIdDests.clear()
for ship in mePlayer.all_ships() :
myIdDests.append([ship.id, ship.dest()])
oppShipsPos.clear()
for ship in game_map.oppShips() :
oppShipsPos.append(ship.pos())
logging.info("added {} for {}".format(ship.pos(),ship))
def checkForLostShips(game_map) :
logging.info("myIdDests {}".format(myIdDests))
closeSqrDist = (hlt.constants.MAX_SPEED + 5 + 1 + .1) ** 2
for idDest in myIdDests :
if not mePlayer.get_ship(idDest[0]) :
dest = idDest[1]
isClose = False
for oShip in oppShipsPos :
logging.info("sqrDist {} closeSqrDist {} oShip {} dest {}".format(oShip.squareDist(dest), closeSqrDist, oShip, dest))
if oShip.squareDist(dest) < closeSqrDist :
isClose = True
break
if not isClose :
logging.info("ERROR: idDest {} mystery gone".format(idDest))
############################################################################
def getDistAdd(ship, entity) :
if not entity.isShip() :
return 0
oShip = entity
if not oShip.isMobile() :
return -44
#return -21
if ship.isAttacker() :
if oShip.isDefender() and ship.dist2Planet > oShip.dist2Planet :
return -31
return -17
if ship.isExplorer() :
if oShip.isAttacker() :
return -11
return -31
# ship is defender
if oShip.isDefender() and ship.dist2Planet > oShip.dist2Planet :
return -17
if oShip.isAttacker() and ship.dist2Planet > oShip.dist2Planet :
return -31 + min(20,0.5*(ship.dist2Planet - oShip.dist2Planet))
return -31
oShip = entity
if not oShip.isMobile() :
return -34
#return -21
if ship.isAttacker() :
if oShip.isDefender() and ship.dist2Planet > oShip.dist2Planet :
return -21
return -14
if ship.isExplorer() :
if oShip.isAttacker() :
return 7
return -21
# ship is defender
if oShip.isDefender() and ship.dist2Planet > oShip.dist2Planet :
return 7
if oShip.isAttacker() and ship.dist2Planet > oShip.dist2Planet :
return -14
return -21
############################################################################
def nearestFirst(obj, entities) :
dis = []
for i in range(len(entities)) :
dis.append([obj.calcDist(entities[i]),i])
dis = sorted(dis)
return [ entities[dis[i][1]] for i in range(len(entities)) ]
############################################################################
def doClumpAttack(point, closeShips) :
myCloseShips = []
for s in closeShips :
isMine = s.owner.id == myID
if s.isMobile() :
if not isMine :
getTargetInfo(s.id).capacity -= 1
elif not isCommanded(s.id) and not getShipInfo(s).isAssigned and \
not s.id in escapeShips :
myCloseShips.append(s)
clump = createClump(point)
for s in nearestFirst(point, myCloseShips) :
if time.time() - startTime > 1.4 :
break
if DOLOG :
logging.info("nearestFirst: {} {}".format(point,s))
navigate_command = clump.navToClump(s)
if navigate_command :
addNavCmd(navigate_command)
getShipInfo(s).isAssigned = True
if len(clump.targets) == 1 :
dest = s.dest()
if DOLOG :
logging.info("resetCenter {} to {}".format(\
clump.center,dest))
clump.center.x = dest.x
clump.center.y = dest.y
############################################################################
def getClosestMyDocked(e) :
minDist = 99999
minPlanet = None
for p in game_map.all_planets():
if p.owner == None or p.owner.id != mePlayer.id :
continue
d = e.calculate_distance_between(p) - p.radius
if d < minDist :
minDist = d
minPlanet = p
if minPlanet :
closestDocked = e.getClosest(minPlanet.all_docked_ships())
return closestDocked
return None
############################################################################
commandedShipIDs = set()
def isCommanded(sid) : return sid in commandedShipIDs
def addNavCmd(navigate_command) :
sid = navigate_command.split()[1]
if not sid in commandedShipIDs :
command_queue.append(navigate_command)
commandedShipIDs.add(sid)
############################################################################
shipID2siIndx = dict()
def getShipInfo(s) :
return SIs[shipID2siIndx[s.id]]
shipID2tiIndx = dict()
def getTargetInfo(sid) :
return TIs[shipID2tiIndx[sid]] if sid in shipID2tiIndx else \
TargetInfo(hlt.entity.Position(0,0),0)
############################################################################
def newShip(oShip, xy) :
nShip = hlt.entity.Ship(None, None, xy[0], xy[1], None, None, None, None, None, None, None)
nShip.copyAttrs(oShip)
return nShip
############################################################################
def isMoving(navigate_command) :
if not navigate_command : return False
return navigate_command.split()[2] != 0
############################################################################
class ShipInfo():
def __init__(self, ship) :
self.ship = ship
self.isAssigned = False
############################################################################
class TargetInfo():
def __init__(self, entity, capacity, dm=1, da=0, dr=False) :
self.entity = entity
self.capacity = capacity
self.distMult = dm
self.distAdd = da
self.doRandomize = dr
self.targets = list()
self.isEscape = False
############################################################################
def equalPos(p1,p2) :
return p1.x == p2.x and p1.y == p2.y
############################################################################
class Clump :
def __init__(self, center) :
self.center = copy.deepcopy(center)
self.center.radius = 5
self.targets = []
def navToClump(self, ship) :
target = ship.getClumpTarget(self.center, self.targets, game_map)
navigate_command = ship.navigate(\
target, game_map, obstLists, doFollow=True)
if navigate_command :
dest = ship.dest()
closestTarget = dest.getClosest(self.targets + [target])
if dest.calculate_distance_between(closestTarget) < 2.5 :
self.targets.append(dest)
if DOLOG:
logging.info(\
"navToClump: ship {} target {} dest {} dist {} c {}".format(\
ship, target, dest, dest.calcDist(target), self))
return navigate_command
def __repr__(self):
return "Clump: {} nt: {}".format(self.center, len(self.targets))
clumps = []
def createClump(center) :
clump = Clump(center)
clumps.append(clump)
if DOLOG :
logging.info("createclump: {} clumps {}".format(clump,clumps))
return clump
def getClosestClump(fromShip, toShip) :
centers = [ clump.center for clump in clumps ]
obsts = game_map.obstacles_between(fromShip, toShip, centers)
if centers :
closestCenter = fromShip.getClosest(centers)
for clump in clumps :
if equalPos(closestCenter, clump.center) :
if fromShip.calcDist(clump.center) > \
9.5 + math.sqrt(len(clump.targets)) :
return None
else :
return clump
return None
def doClump() :
return not mildEarlyAttack and \
mePlayer.num_ships() - 4.0 * numOppShips < 0
############################################################################
class AdvInfo() :
def __init__(self, midPoint, distToMid=1.5) :
self.closeShips = mbs.getCloseInBlocks(midPoint, 7 + 5 - distToMid)
self.healthAdv = 0
self.shipAdv = 0
self.numCommandableShips = 0
for s in self.closeShips :
isMine = s.owner.id == myID
if DOLOG : logging.info("isMine {} myID {} s.owner {} s {}".format(\
isMine, myID, s.owner.id,s))
if s.isMobile() :
mult = 1 if isMine else -1
self.healthAdv += mult * s.health
self.shipAdv += mult
self.numCommandableShips += 1
if DOLOG :
logging.info(
"adv: s {} h {} mp {} s {} oS {} cs {}".format(\
self.shipAdv, self.healthAdv, midPoint, ship, oShip, self.closeShips))
def lotsShips(self) :
return self.numCommandableShips >= 3 and self.shipAdv >= 0
def goodForAttacker(self) :
return self.shipAdv > 0 or self.healthAdv > 0 and self.shipAdv >= 0 or \
self.lotsShips()
def goodForDefender(self) :
return self.shipAdv > 0 or self.healthAdv >= 0 and self.shipAdv == 0 or \
self.lotsShips()
############################################################################
def safeToDock(ship, planet) :
closeShips = mbs.getCloseInBlocks(ship, 40)
myNum = 0
oppNum = 0
for s in nearestFirst(ship, closeShips) :
if s.id != ship.id and not s.id in isDocking and s.isMobile():
if s.owner.id == myID :
myNum += 1
else :
oppNum += 1
if myNum - oppNum < 0 :
return False
return True # myNum >= oppNum * 1.1
def myNumSitting() :
return len(isDocking) + len(player2Docks[myID])
############################################################################
def doAorD(ship, target, oShip, isDefense) :
target = ship.truncTarget(target)
midPoint = oShip.closest_point_to(target, min_distance=1.5)
advInfo = AdvInfo(midPoint)
if isDefense and advInfo.goodForDefender() or \
not isDefense and advInfo.goodForAttacker() :
if advInfo.numCommandableShips > 1 and \
(not probingPlanet or advInfo.numCommandableShips != advInfo.shipAdv) :
doClumpAttack(target, advInfo.closeShips)
return True, None
else :
navigate_command = ship.navigate(target, game_map, \
obstLists, doFollow=doFollow)
return True, navigate_command
return False, None
############################################################################
def doDefend(ship, oShip, closestDocked, oShipDist) :
target = closestDocked.closest_point_to(oShip, min_distance=oShipDist)
return doAorD(ship, target, oShip, True)
############################################################################
def doAttack(ship, target, oShip) :
return doAorD(ship, target, oShip, False)
############################################################################
def doSideAttack(ship, target1, oShip, randMult) :
return False,None
dx1 = target1.x - ship.x
dy1 = target1.y - ship.y
vecPerp = (randMult * dy1, randMult * dx1 * -1)
vec25 = (.25 * dx1 + .75 * vecPerp[0], .25 * dy1 + .75 * vecPerp[1])
# do 2 * vec25 to make sure at least distance 7
target25 = hlt.entity.Position(ship.x + 2 * vec25[0], ship.y + 2 * vec25[1])
return doAttack(ship, target25, oShip)
############################################################################
def probeDoDefend(ship, oShip) :
#origOShip = oShip
#goShip = guessOShip(oShip)
closestDocked = getClosestMyDocked(oShip)
if not closestDocked : return None
dist2docked = oShip.calculate_distance_between(closestDocked)
oShip = ship.target4avoidFirstObst(oShip, obstLists.sitters, game_map)
if dist2docked < 15 :
target = oShip.closest_point_to(closestDocked, min_distance=2)
navigate_command = ship.navigate(target, game_map, \
obstLists, doFollow=doFollow)
else :
good, navigate_command = doDefend(ship, oShip, closestDocked, 3)
if not good :
good, navigate_command = doDefend(ship, oShip, closestDocked, 9)
if not good :
good, navigate_command = doDefend(ship, oShip, closestDocked, 13)
if not good :
target = oShip.closest_point_to(closestDocked, min_distance=2)
navigate_command = ship.navigate(target, game_map, \
obstLists, doFollow=doFollow)
return navigate_command
############################################################################
def probeDoAttack(ship, entity) :
# TODO head of the pack should become a pillager
entity = ship.target4avoidFirstObst(entity, obstLists.sitters, game_map)
good = False
dist2entity = ship.calculate_distance_between(entity) - entity.radius
if dist2entity < 9 :
target = ship.closest_point_to(entity, min_distance=2)
target1 = target
good, navigate_command = doAttack(ship, target, entity)
if not good and dist2entity < 8 and entity.isShip() and not entity.isMobile() :
mid = hlt.entity.getMidPos(ship, entity)
closeShips = mbs.getCloseInBlocks(mid, dist2entity / 2)
if not game_map.obstacles_between(ship, entity, closeShips) :
navigate_command = ship.navigate(entity, game_map, \
obstLists, goHard=True, doFollow=doFollow)
return navigate_command
if not good and dist2entity > 8 :
target = entity.closest_point_to(ship, min_distance=7.01 - 0.5)
target1 = target
good, navigate_command = doAttack(ship, target, entity)
if not good and dist2entity > 4 :
target = entity.closest_point_to(ship, min_distance=3.01 - 0.5)
good, navigate_command = doAttack(ship, target, entity)
if not good :
# TODO attacker code
# TODO try 12 away from nearest attacker toward entity, then away
randMult = -1 if randomBool() else 1
good, navigate_command = \
doSideAttack(ship, target1, entity, randMult)
if not good :
good, navigate_command = \
doSideAttack(ship, target1, entity, -1 * randMult)
if not good :
target = entity.closest_point_to(ship, min_distance=-3.01 - 0.5)
good, navigate_command = doAttack(ship, target, entity)
if not good :
target = entity.closest_point_to(ship, min_distance=-7.01 - 0.5)
good, navigate_command = doAttack(ship, target, entity)
if not good :
navigate_command = ship.navigate(target, game_map, \
obstLists, doFollow=doFollow)
return navigate_command
############################################################################
shipOldXY = dict()
def guessOShip(oShip) :
if oShip.id in shipOldXY :
oldXY = shipOldXY[oShip.id]
oShip = newShip(oShip, [ 2 * oShip.x - oldXY[0], 2 * oShip.y - oldXY[1] ])
return oShip
def getMegaRunawayTarget() :
p0 = middlePlanets[0]
maxd = 0
for p in middlePlanets :
maxd = max(maxd, abs(p.x - p0.x))
maxd = max(maxd, abs(p.y - p0.y))
additive = max(9,p0.radius + 1.5)
rad = maxd/2 + additive
mcorners = []
for x in [width/2 - rad, width/2 + rad ] :
for y in [height/2 - rad, height/2 + rad ] :
mcorners.append(hlt.entity.Position(x,y))
ncorners = []
for mc in nearestFirst(myAvg, mcorners) :
if myAvg.calcDist(mc) > 7 :
ncorners.append(mc)
if len(ncorners) == 2 :
break
tncorners = [ship.truncTarget(nc) for nc in ncorners]
cornerNearOpp = nearestFirst(nearestOppAvg, tncorners)
if DOLOG :
logging.info("EarlyRunTarget: t: {} tnc: {} n: {} m: {} mya: {} noa: {}".format( \
cornerNearOpp[-1],tncorners,ncorners,mcorners,myAvg,nearestOppAvg))
return cornerNearOpp[-1]
def moveEarlyMegaShip(oShip, game_map, myAvg, command_queue) :
mePlayer = game_map.get_me()
earlyMegaShip = hlt.entity.MegaShip(mePlayer.all_ships(), myAvg)
# TODO distance depends on isMobile()
if oShip.isMobile() :
randDist = random.uniform(3,7)
target = ship.closest_point_to(oShip, min_distance = randDist + earlyMegaShip.radius)
dist = ship.calculate_distance_between(oShip)
if dist < 23 :
if numActualOpps == 1 and \
myHNP >= totalShipHealthNProd[nearestOppID] :
target = getMegaRunawayTarget()
elif dist < 19 :
target.randomize()
dist = target.calculate_distance_between(oShip)
if dist < 0.6 + 0.1 + earlyMegaShip.radius : # don't crash
target = ship.closest_point_to(oShip, min_distance = \
randDist + earlyMegaShip.radius)
else :
target = ship.closest_point_to(oShip, min_distance = 2 + (earlyMegaShip.radius - .5))
obstLists = ObstLists()
obstLists.growObsts = list(game_map.all_planets())
obstLists.allEntities = list(game_map.all_planets())
obstLists.sitters = list(game_map.all_planets())
navigate_command = earlyMegaShip.navigate(target, game_map, obstLists)
if navigate_command :
command_queue += navigate_command
############################################################################
didEarlyMegaShip = False
def doEarlyAttackManeuvers(oShip, game_map, myAvg, command_queue, obstLists) :
global didEarlyMegaShip
if oShip.isMobile() :
if not didEarlyMegaShip :
# find ship closest to myAvg and see if MegaShip is complete
minDist = 99999
maxDist = 0
avgShip = None
for ship in mePlayer.all_ships():
dist = ship.calculate_distance_between(myAvg)
if dist < minDist :
minDist = dist
avgShip = ship
if dist > maxDist :
maxDist = dist
if maxDist < 2 * avgShip.radius + .5 :
didEarlyMegaShip = True
if DOLOG :
logging.info("MegaShipping: {} {} ships: {}".format( \
didEarlyMegaShip, maxDist, mePlayer.all_ships()))
if not didEarlyMegaShip :
# move other ships toward avgShip
for ship in mePlayer.all_ships():
if avgShip != ship :
navigate_command = ship.navigate(ship.closestIntegralPoint(avgShip),
game_map, obstLists)
if isMoving(navigate_command) :
addNavCmd(navigate_command)
if not command_queue :
didEarlyMegaShip = True
moveEarlyMegaShip(oShip, game_map, myAvg, command_queue)
else :
if not didEarlyMegaShip :
return False
else :
moveEarlyMegaShip(oShip, game_map, myAvg, command_queue)
if DOLOG : logging.info("MegaShippingCommand: {}".format(command_queue))
return True
############################################################################
RetreatingState = 0
ClockwiseState = 1
CounterClockwiseState = 2
id2retreatState = collections.defaultdict(int)
ONTOP = 0
ONLEFT = 1
ONBOTTOM = 2
ONRIGHT = 3
def getSidePos(e) :
DRs = [ [ e.y , ONTOP ],
[ e.x , ONLEFT ],
[ height - e.y , ONBOTTOM ],
[ width - e.x , ONRIGHT ] ]
return sorted(DRs)[0][1]
def getRetreatPoint() :
mPos = getSidePos(myAvg)
if mPos == ONTOP :
rp = hlt.entity.Position(myAvg.x,2)
if mPos == ONLEFT :
rp = hlt.entity.Position(2, myAvg.y)
if mPos == ONBOTTOM :
rp = hlt.entity.Position(myAvg.x,height - 2)
if mPos == ONRIGHT :
rp = hlt.entity.Position(width - 2, myAvg.y)
return rp
def getRevolvingTarget(ship, shipState, pos) :
DTN = .5
if shipState == CounterClockwiseState :
if pos == ONTOP :
target = hlt.entity.Position(ship.x - 7, 2.2)
if target.x < DTN :
return getRevolvingTarget(ship, shipState, ONLEFT)
if pos == ONLEFT :
target = hlt.entity.Position(2.2, ship.y + 7)
if target.y > height - DTN :
return getRevolvingTarget(ship, shipState, ONBOTTOM)
if pos == ONBOTTOM :
target = hlt.entity.Position(ship.x + 7, height - 2.2)
if target.x > width - DTN :
return getRevolvingTarget(ship, shipState, ONRIGHT)
if pos == ONRIGHT :
target = hlt.entity.Position(width - 2.2, ship.y - 7)
if target.y < DTN :
return getRevolvingTarget(ship, shipState, ONTOP)
else :
if pos == ONTOP :
target = hlt.entity.Position(ship.x + 7, 3.4)
if target.x > width - DTN :
return getRevolvingTarget(ship, shipState, ONRIGHT)
if pos == ONLEFT :
target = hlt.entity.Position(3.4, ship.y - 7)
if target.y < DTN :
return getRevolvingTarget(ship, shipState, ONTOP)
if pos == ONBOTTOM :
target = hlt.entity.Position(ship.x - 7, height - 3.4)
if target.x < DTN :
return getRevolvingTarget(ship, shipState, ONLEFT)
if pos == ONRIGHT :
target = hlt.entity.Position(width - 3.4, ship.y + 7)
if target.y > height - DTN :
return getRevolvingTarget(ship, shipState, ONBOTTOM)
return target
############################################################################
def getClosestDist(target,enemyShips) :
closest = target.getClosest(enemyShips)
return 99999 if closest == None else target.calcDist(closest)
def retreatNavigate(ship, target, doClump=True) :
target = ship.target4avoidFirstObst(target, obstLists.sitters, game_map)
target = ship.truncTarget(target)
enemyShips = []
for s in mbs.getCloseInBlocks(ship, 41) :
if s.owner.id != myID and s.isMobile() : enemyShips.append(s)
dist = getClosestDist(target, enemyShips)
if DOLOG: logging.info("retreatNav {} dist {} {}".format(\
ship, dist, enemyShips))
if dist < 15 :
numAngles = 32
targets = [ship]
for i in range(numAngles) :
target = ship.getTarget(i * 360 / numAngles, 7.01)
if ship.targetIsOutsideMap(target, gm) :
continue
if gm.obstacles_between(ship, target, gm.all_planets()) :
continue
targets.append(target)
DTs = []
for i in range(len(targets)) :
DTs.append([getClosestDist(targets[i], enemyShips),i])
dt = sorted(DTs)[-1]
dist = dt[0]
target = targets[ dt[1] ]
if DOLOG: logging.info("retreatNav dist {} t {} DTs {} {}".format(\
dist, target, DTs, targets))
if doClump :
doClumpAttack(target, mbs.getCloseInBlocks(target, 8))
return None
navigate_command = ship.navigate(target, game_map, \
obstLists, doFollow=True)
return navigate_command
############################################################################
doRetreat = False
retreatPoint = None
def checkDoMassRetreat(SIs) :
if numActualOpps == 1 :
return
global doRetreat
if not doRetreat :
if DOLOG: logging.info("myNShips {} mvAvgNS {}".format(\
mePlayer.num_ships(), mvAvgNumShips[myID]))
if mePlayer.num_ships() >= mvAvgNumShips[myID] or \
len(player2histShips[myID]) < 10 :
return
#if lowerID != None : # a player with less hist ships and some docked
#if True :
#if mePlayer.num_ships() < 10 and len(player2histShips[myID]) > 30 :
#doRetreat = True
#sortedHist = list()
#for player in gm.all_players() :
#sortedHist.append([len(player2histShips[player.id]),player.id])
#sortedHist = sorted(sortedHist)
#for i in range(len(sortedHist)) :
#if myID == sortedHist[i][1] :
#mySHIdx = i
#break
#if DOLOG: logging.info("mySHIdx {} sortedHist {}".format(\
#mySHIdx, sortedHist))
#if mySHIdx == len(sortedHist) - 1 :
#return
#upperPlayer = gm.get_player(sortedHist[mySHIdx + 1][1])
#divisor = 2
#if mePlayer.num_ships() < upperPlayer.num_ships() / divisor :
#doRetreat = True
sortedNS = list()
for player in gm.all_players() :
if len(player2Docks[player.id]) > 0 :
sortedNS.append([player.num_ships(),player.id])
sortedNS = sorted(sortedNS)
mySNSIdx = None
for i in range(len(sortedNS)) :
if myID == sortedNS[i][1] :
mySNSIdx = i
break
if DOLOG: logging.info("mySNSIdx {} sortedNS {}".format(\
mySNSIdx, sortedNS))
if mySNSIdx == None : # we haven't docked yet.
return
if mySNSIdx == len(sortedNS) - 1 : # We are the best
return
if len(sortedNS) == 1 : # only us with docked ships
return
upperPlayer = gm.get_player(sortedNS[mySNSIdx + 1][1])
if mePlayer.num_ships() < upperPlayer.num_ships() / (2 + mySNSIdx) or \
mePlayer.num_ships() < myMaxNumShips / 2 and \
len(player2Mobiles[myID]) <= 7 :
doRetreat = True
if not doRetreat :
return
global retreatPoint
if retreatPoint == None :
retreatPoint = getRetreatPoint()
escapeShips.clear()
retreatCornerShips.clear()
if len(retreatCornerShips) == 0 :
mobiles = set(player2Mobiles[myID])
for cp in nearestFirst(myAvg, cornerPoints) :
if len(mobiles) > 0 :
mobile = cp.getClosest(
[mePlayer.get_ship(mid) for mid in mobiles])
retreatCornerShips[mobile.id] = cp
mobiles.discard(mobile.id)
if DOLOG: logging.info("retreatPoint {} ".format(retreatPoint ))
addOppDocksToObstLists()
DSIs = []
for i in range(len(SIs)) :
si = SIs[i]
DSIs.append([getClosestDist(si.ship, cornerPoints),i])
for dsi in sorted(DSIs) :
si = SIs[dsi[1]]
if time.time() - startTime > 1.4 :
break
ship = si.ship
if ship.id in escapeShips or si.isAssigned :
continue
navigate_command = None
shipState = id2retreatState[ship.id]
pos = getSidePos(ship)
if shipState == RetreatingState :
target = retreatPoint
if ship.calculate_distance_between(target) < 7 :
if pos == ONLEFT and ship.y > target.y or \
pos == ONTOP and ship.x < target.x or \
pos == ONRIGHT and ship.y < target.y or \
pos == ONBOTTOM and ship.x > target.x :
shipState = CounterClockwiseState
else :
shipState = ClockwiseState
id2retreatState[ship.id] = shipState
if shipState != RetreatingState :
target = getRevolvingTarget(ship, shipState, pos)
if ship.id in retreatCornerShips :
target = retreatCornerShips[ship.id]
navigate_command = retreatNavigate(ship, target, doClump = False)
if DOLOG: logging.info("retreatPt {} target {} nc {} ship {}".format(\
retreatPoint, target, navigate_command, ship))
if navigate_command:
addNavCmd(navigate_command)
si.isAssigned = True
############################################################################
game = hlt.Game('mellendo59')
hasDocked = False
firstPlanetID = None
doFirstPlanetID = None
command_queue = []
TIs = []
DIs = []
SIs = []
escapePoints = []
doEscapePoint = True
escapeShips = set()
turnNumber = -1
oppDockedAt = None
player2histShips = collections.defaultdict(set)
player2histDocks = collections.defaultdict(set)
mvAvgNumShips = dict()
myMaxNumShips = 0
circleRadius = 10
circleAngle = 0
numCirclePoints = int(random.uniform(2,10))
#numCirclePoints = 4
circleSpin = random.uniform(6,10)
#circleSpin = 2
circleDoRandomize = False
mbs = MapBlocks()
probingPlanet = False
isDocking = set()
wait33 = None
# MAIN LOOP
while True:
# Update the map for the new turn and get the latest version
game_map = game.update_map()
gm = game_map
startTime = time.time()
mbs.updateBlocks()
width = game_map.width
height = game_map.height
cornerPoints = [hlt.entity.Position(1,1), \
hlt.entity.Position(width-1,1), \
hlt.entity.Position(1,height-1), \
hlt.entity.Position(width-1,height-1) ]
turnNumber += 1
mePlayer = game_map.get_me()
myID = mePlayer.id
isDocking.clear()
totalShipHealthNProd = game_map.getTotalShipHealthNProdDict()
myHNP, oppHNP = getMyOppHNP(totalShipHealthNProd)
numStrongOpps = game_map.numStrongOpps()
numActualOpps = game_map.numActualOpps()
getShipRoles()
if DOLOG :
logGameState(game_map)
checkForLostShips(game_map)
############################################################################
for player in game_map.all_players():
pid = player.id
if pid in mvAvgNumShips :
mvAvgNumShips[pid] += .14 * (player.num_ships() - mvAvgNumShips[pid])