-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyBot4.py
756 lines (664 loc) · 28.2 KB
/
MyBot4.py
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
#!/usr/bin/env python
#
"""
// The DoTurn function is where your code goes. The PlanetWars object contains
// the state of the game, including information about all planets and fleets
// that currently exist. Inside this function, you issue orders using the
// pw.IssueOrder() function. For example, to send 10 ships from planet 3 to
// planet 8, you would say pw.IssueOrder(3, 8, 10).
//
// There is already a basic strategy in place here. You can use it as a
// starting point, or you can throw it out entirely and replace it with your
// own. Check out the tutorials and articles on the contest website at
// http://www.ai-contest.com/resources.
"""
from PlanetWars import PlanetWars
from sys import stdout
import random
import sys
messageHistory = []
allies_nicks = []
next_targets = []
# -------------------------------------------------
# TODO:
# - 'co-op' and 'calculate targets' sections refactoring
# - fleets iteration optimization
# - encapsulate f.write
# -------------------------------------------------
def DoTurn(pw, group_ids, nickname, f):
commandGame = False
for plan in pw.MyPlanets():
f.write('planet ' + str(plan.PlanetID() ) + '\n')
f.write('planet ' + str(plan ) + '\n')
# send planet position
if not messageHistory:
if len(pw.MyPlanets()) > 0:
mes = pw.MyPlanets()[0].PlanetID()
pw.SendMessage(nickname,mes)
# else:
# mes = random.randint(-214783648, 2147483647)
# write message log
tmp = []
for mes in pw.Messages():
tmp.append((mes.Nickname(), mes.Number()))
messageHistory.append(tmp)
# # find out and remember party nickname
if not allies_nicks and messageHistory:
tmp = messageHistory[-1]
for msh in tmp:
f.write('=====msh[1] text: ' + str(msh[1] ) + '\n')
for pln in pw.Planets():
# f.write('pln.Owner(): ' + str( pln.Owner() ) + '\n')
if pln.PlanetID() == int(msh[1]) and \
pln.Owner() in group_ids:
f.write('=====if' + str(pln.PlanetID() ) + '\n')
for gip in group_ids:
if pln.Owner() == int(gip):
allies_nicks.append(( msh[0], pln.Owner() ))
f.write('=====planet.Owner()' + str(pln.Owner() ) + '\n')
f.write('last mes: ' + str(messageHistory[-1]) + '\n')
f.write('allies_nicks ' + str(allies_nicks) + '\n')
# Ship count
mySize = 0
alliesSize = 0
enemySize = 0
# Ships growth
myGrowth = 0
alliesGrowth = 0
enemyGrowth = 0
# Planets
myPlanets = []
enemyPlanets = []
alliesPlanets = []
neutralPlanets = pw.NeutralPlanets()
# Fleets
myFleets = []
partyFleets = []
alliesFleets = []
enemyFleets = []
# Group fleets
for fleet in pw.Fleets():
if fleet.Owner() == 1 or \
fleet.Owner() in group_ids:
partyFleets.append(fleet)
# My fleets
for fleet in pw.MyFleets():
myFleets.append(fleet)
# Allies fleets
for fleet in pw.EnemyFleets():
if fleet.Owner() in group_ids:
alliesFleets.append(fleet)
# Enemy fleets
for fleet in pw.EnemyFleets():
if fleet.Owner() not in group_ids:
enemyFleets.append(fleet)
# My power
for p in pw.MyPlanets():
myPlanets.append(p)
mySize += p.NumShips()
myGrowth += p.GrowthRate()
# Allies power
for p in pw.EnemyPlanets():
if p.Owner() in group_ids:
alliesPlanets.append(p)
alliesSize += p.NumShips()
alliesGrowth += p.GrowthRate()
# Enemies power
for p in pw.EnemyPlanets():
if p.Owner() not in group_ids:
enemyPlanets.append(p)
enemySize += p.NumShips()
enemyGrowth += p.GrowthRate()
# Neutral planets without allies fleets in space
for fleet in alliesFleets:
if fleet.DestinationPlanet() in ([p.PlanetID() for p in neutralPlanets]):
neutralPlanets.remove( pw.GetPlanet(fleet.DestinationPlanet()) )
if ( enemySize <= 0 ):
winRatio = 0
else:
winRatio = float(mySize)/enemySize
f.write('winRatio: ' + str(winRatio) + '\n')
# if winRatio >= 2.5:
# return # regeneration
f.write('======= Calculate Available ships based on upcoming enemy fleets ======= \n')
# available ships
available_ships = {}
for my_pln in myPlanets:
available_ships[my_pln.PlanetID()] = my_pln.NumShips()
f.write('available_ships: ' + str(available_ships) + '\n')
# save fleets for defence (game w/o help - not good strategy :( )
defend_req = {}
for fleet in enemyFleets:
att_pl = pw.GetPlanet( fleet.DestinationPlanet() )
if att_pl.PlanetID() in ([p.PlanetID() for p in myPlanets]):
if fleet.TurnsRemaining()*att_pl.GrowthRate() + att_pl.NumShips() < \
fleet.NumShips():
available_ships[att_pl.PlanetID()] = 0 # let the battle begin
else:
upc_ships = fleet.NumShips()
available_ships[att_pl.PlanetID()] -= upc_ships
# prevent closest planets attack
for my_pln in myPlanets:
distances = []
for att_pln in enemyPlanets:
if att_pln.NumShips() == 0:
continue
dist = pw.Distance(my_pln.PlanetID(), att_pln.PlanetID())
distances.append([att_pln, dist])
distances.sort(key=lambda x: x[1], reverse=True)
worst_case = 0
for att_rank in distances[:3]:
# if att_rank[1] >
if worst_case < att_rank[0].NumShips():
worst_case = att_rank[0].NumShips()
if available_ships[my_pln.PlanetID()] < worst_case:
available_ships[my_pln.PlanetID()] = 0 # wait
f.write('======= Prevent Spreading Enemy on Neutral Planets ======= \n')
for efl in enemyFleets:
if efl.TurnsRemaining() < 5 and\
pw.GetPlanet(efl.DestinationPlanet()).Owner() == 0:
enemyPlanets.append(pw.GetPlanet(efl.DestinationPlanet()))
f.write('======= Co-op Targets Processing ======= \n')
if len(messageHistory) > 2:
for msh in messageHistory[-1]:
if msh[0] in ([nick[0] for nick in allies_nicks]):
# f.write('pick: ' + str(msh[0]) + '\n')
targets = []
targets.append(int(str(msh[1])[0:3:]) - 100)
targets.append(int(str(msh[1])[3:6:]) - 100)
targets.append(int(str(msh[1])[6:9:]) - 100)
f.write('targets: ' + str(targets) + '\n')
for trg in targets:
f.write('next_targets: ' + str(next_targets) + '\n')
if trg in next_targets:
att_pln = pw.GetPlanet(trg)
# f.write('att_pln: ' + str(att_pln) + '\n')
req_ships = int(att_pln.NumShips() * .10)
f.write('req_ships: ' + str(req_ships) + '\n')
f.write('attack_planet: ' + str(att_pln) + '\n')
if (alliesSize + mySize) > req_ships:
scores = []
for my_pl in myPlanets:
if available_ships[my_pl.PlanetID()] < my_pl.GrowthRate()*10 or \
att_pln.Owner() == 1:
continue
# f.write('score: pw.Distance(...): ' + str(pw.Distance(my_pl.PlanetID(), att_pln.PlanetID())) + '\n')
score = float(available_ships[my_pl.PlanetID()]) / pw.Distance(my_pl.PlanetID(), att_pln.PlanetID())
scores.append([my_pl, score])
scores.sort(key=lambda x: x[1], reverse=True)
f.write('scores: ' + str(scores) + '\n')
distances = scores
# req_orig_ships = att_pln.NumShips()
# f.write('req_orig_ships: ' + str(req_orig_ships) + '\n')
req_ships = att_pln.NumShips() + 1
still_need = req_ships
# f.write('enemyFleets: ' + str(enemyFleets) + '\n')
# send only req-d num of ships
for flt in partyFleets:
if flt.DestinationPlanet() == att_pln.PlanetID():
still_need -= flt.NumShips() - (att_pln.GrowthRate() * flt.TurnsRemaining())*2
req_ships = still_need
if len(distances) > 0:
source1 = distances[0][0] # source planet
if len(distances) > 1:
f.write('111\n')
source2 = distances[1][0]
if source1.NumShips() > 0 and source2.NumShips() > 0:
# f.write('111 1: ' + str(float(source1.NumShips()) / source2.NumShips()) +'\n')
# if source1 is very strong
if float(source1.NumShips()) / source2.NumShips() > 3:
source1 = source2
# if source2 is very strong
elif float(source2.NumShips()) / source1.NumShips() > 3:
f.write('111 2\n')
source2 = source1
else:
if att_pln.NumShips() == 0:
koef1 = 1
koef2 = 1
else:
koef1= float(source1.NumShips()) / att_pln.NumShips()
koef2= float(source2.NumShips()) / att_pln.NumShips()
if koef1 > 0 and koef2 >0:
if koef1 > koef2:
koef = koef2/koef1
else:
koef = koef1/koef2
req_ships1 = req_ships*koef
req_ships2 = req_ships - req_ships1
if available_ships[source1.PlanetID()] >= req_ships1 and \
available_ships[source2.PlanetID()] >= req_ships2:
"???"
# available_ships[source1.PlanetID()] -= req_ships1
# available_ships[source2.PlanetID()] -= req_ships2
# pw.IssueOrder(source1.PlanetID(), att_pln.PlanetID(), req_ships1)
# pw.IssueOrder(source2.PlanetID(), att_pln.PlanetID(), req_ships2)
if len(distances) == 1 or \
source1.PlanetID() == source2.PlanetID():
f.write('222\n')
req_ships += att_pln.GrowthRate()*pw.Distance(my_pl.PlanetID(), att_pln.PlanetID())
dec = int(len(group_ids)*1.5) #1.85)
if dec ==0:
dec = 1
req_ships = int(req_ships/dec)
# req_ships += att_pln.GrowthRate()*pw.Distance(source1.PlanetID(), att_pln.PlanetID())
# req_ships += att_pln.GrowthRate()*3
# req_ships += available_ships[source1.PlanetID()] - 10
f.write("new req: " + str(req_ships) + '\n')
f.write("available: " + str(available_ships[source1.PlanetID()]) + '\n')
if req_ships > 0 and \
available_ships[source1.PlanetID()] >= req_ships:
f.write('av: ' + str(available_ships[source1.PlanetID()]) + '\n')
available_ships[source1.PlanetID()] -= req_ships
pw.IssueOrder(source1.PlanetID(), att_pln.PlanetID(), req_ships)
# attack_targets[att_pln.PlanetID()] = [source1.PlanetID(), req_ships]
# else:
# next_targets.append(att_pln.PlanetID())
f.write('======= Neutral Planets Pre-processing ======= \n')
# f.write("neutralPlanets: " + str(neutralPlanets) + '\n')
# neutral_targets = []
# for my_pl in myPlanets:
# if available_ships[my_pl.PlanetID()] < my_pl.GrowthRate()*10 or available_ships[my_pl.PlanetID()] == 0:
# continue
# distances = []
# for att_pln in neutralPlanets:
# score = float(att_pln.NumShips() + 5*att_pln.GrowthRate()) / pw.Distance(my_pl.PlanetID(), att_pln.PlanetID())
# # dist = pw.Distance(my_pl.PlanetID(), att_pln.PlanetID())
# distances.append([att_pln, score])
# distances.sort(key=lambda x: x[1], reverse=True)
# # f.write('distances: ' + str(distances) + '\n')
# ordered = ([pl_rank[0] for pl_rank in distances])
# for item in ordered[:1]:
# f.write('item: ' + str(item) + '\n')
# if item in neutral_targets:
# continue
# neutral_targets.append(item)
# f.write("neutral_targets: " + str(neutral_targets) + '\n')
my_top_pl_id = -1
source_score = -999.0
for my_pl in myPlanets:
score = int(my_pl.NumShips())
if score > source_score:
source_score = score
my_top_pl_id = my_pl.PlanetID()
f.write("my_top_pl_id: " + str(my_top_pl_id) + '\n')
neut_score = []
for ne_pl in neutralPlanets:
# f.write("ne_pl: " + str(ne_pl) + '\n')
f.write("ne_pl id: " + str(ne_pl.PlanetID()) + '\n')
gr = ne_pl.GrowthRate()
if gr == 0:
gr = 1
ns = ne_pl.NumShips()
if ns == 0:
ns = 1
dist = 1
if my_top_pl_id >= 0:
dist = pw.Distance(ne_pl.PlanetID(), my_top_pl_id)
score = float(gr) / (ns * dist)
neut_score.append([ne_pl, score])
neut_score.sort(key=lambda x: x[1], reverse=True)
tmp = []
dec = int(len(neut_score)/2)
if dec > 0:
dec = 1
for ne_pl in neut_score[:dec]:
tmp.append(ne_pl[0])
neutralPlanets = tmp
f.write("tmp: " + str(tmp) + '\n')
dec = int(len(neutralPlanets)/2)
if dec > 0:
neutralPlanets = random.sample(neutralPlanets,dec)
f.write("neutralPlanets: " + str(neutralPlanets) + '\n')
enemyPlanets += neutralPlanets
# enemyPlanets += neutral_targets
f.write('======= Calculate Targets ======= \n')
# next_targets = []
del next_targets[:]
attack_targets = {}
for att_pln in enemyPlanets:
distances = []
for my_pl in myPlanets:
if available_ships[my_pl.PlanetID()] < my_pl.GrowthRate()*10:
continue
score = float(my_pl.NumShips()) / pw.Distance(my_pl.PlanetID(), att_pln.PlanetID())
# dist = pw.Distance(my_pl.PlanetID(), att_pln.PlanetID())
distances.append([my_pl, score])
distances.sort(key=lambda x: x[1], reverse=True)
# f.write('distances: ' + str(distances) + '\n')
req_ships = att_pln.NumShips() + 1
still_need = req_ships
# f.write('enemyFleets: ' + str(enemyFleets) + '\n')
# send only req-d num of ships
for flt in partyFleets:
if flt.DestinationPlanet() == att_pln.PlanetID():
still_need -= flt.NumShips() - (att_pln.GrowthRate() * flt.TurnsRemaining())
req_ships = still_need
if (req_ships < 10 and not att_pln.Owner() == 0) or req_ships < 0:
continue
# f.write('req_orig_ships: ' + str(req_orig_ships) + '\n')
f.write('req_ships: ' + str(req_ships) + '\n')
if len(distances) > 0:
source1 = distances[0][0] # source planet
if len(distances) > 1:
f.write('111\n')
source2 = distances[1][0]
if source1.NumShips() > 0 and source2.NumShips() > 0:
# f.write('111 1: ' + str(float(source1.NumShips()) / source2.NumShips()) +'\n')
# if source1 is very strong
if float(source1.NumShips()) / source2.NumShips() > 3:
f.write('111 1\n')
source1 = source2
# if source2 is very strong
elif float(source2.NumShips()) / source1.NumShips() > 3:
f.write('111 2\n')
source2 = source1
else:
f.write('111 3\n')
if source1.NumShips() > 0 and source2.NumShips() > 0:
if att_pln.NumShips() == 0:
koef1 = 1
koef2 = 1
else:
koef1= float(source1.NumShips()) / att_pln.NumShips()
koef2= float(source2.NumShips()) / att_pln.NumShips()
if koef1 > 0 and koef2 >0:
if koef1 > koef2:
koef = koef2/koef1
else:
koef = koef1/koef2
req_ships1 = req_ships*koef
req_ships2 = req_ships - req_ships1
if available_ships[source1.PlanetID()] >= req_ships1 and \
available_ships[source2.PlanetID()] >= req_ships2:
# koef1= source1.NumShips() / att_pln.NumShips()
# koef2= source2.NumShips() / att_pln.NumShips()
# if available_ships[source1.PlanetID()] >= req_ships and \
# available_ships[source2.PlanetID()] >= req_ships:
# available_ships[source1.PlanetID()] -= req_ships
# if attack_targets.has_key(att_pln.PlanetID()):
# attack_targets[att_pln.PlanetID()] += [source1.PlanetID(), req_ships]
# else:
# attack_targets[att_pln.PlanetID()] = [source1.PlanetID(), req_ships]
# "ee"
available_ships[source1.PlanetID()] -= req_ships1
available_ships[source2.PlanetID()] -= req_ships2
pw.IssueOrder(source1.PlanetID(), att_pln.PlanetID(), req_ships1)
pw.IssueOrder(source2.PlanetID(), att_pln.PlanetID(), req_ships2)
else:
next_targets.append(att_pln.PlanetID())
if len(distances) == 1 or \
source1.PlanetID() == source2.PlanetID():
f.write('222\n')
req_ships += att_pln.GrowthRate()*pw.Distance(my_pl.PlanetID(), att_pln.PlanetID())
req_ships += att_pln.GrowthRate()*3
f.write("new req: " + str(req_ships) + '\n')
# f.write("distances[0][1]: " + str(distances[0][1]) + '\n')
if available_ships[source1.PlanetID()] >= req_ships:
f.write('av: ' + str(available_ships[source1.PlanetID()]) + '\n')
available_ships[source1.PlanetID()] -= req_ships
if attack_targets.has_key(att_pln.PlanetID()):
attack_targets[att_pln.PlanetID()] += [source1.PlanetID(), req_ships]
else:
attack_targets[att_pln.PlanetID()] = [source1.PlanetID(), req_ships]
if req_ships > 0 and \
available_ships[source1.PlanetID()] >= req_ships:
f.write('av: ' + str(available_ships[source1.PlanetID()]) + '\n')
available_ships[source1.PlanetID()] -= req_ships
pw.IssueOrder(source1.PlanetID(), att_pln.PlanetID(), req_ships)
else:
next_targets.append(att_pln.PlanetID())
else:
next_targets.append(att_pln.PlanetID())
# f.write('next_targets (init): ' + str(next_targets) + '\n')
tmp = []
score = -999999
for nt in next_targets:
nt = pw.GetPlanet(nt)
gr = nt.GrowthRate()
sr = nt.NumShips()
if nt.GrowthRate() == 0:
gr = 1
if nt.NumShips() == 0:
sr = 1
score = gr / sr
tmp.append([nt.PlanetID(), score])
tmp.sort(key=lambda x: x[1], reverse=True)
# next_targets = []
del next_targets[:]
for pl_rate in tmp:
next_targets.append(pl_rate[0])
f.write('attack_targets: ' + str(attack_targets) + '\n')
f.write('next_targets: ' + str(next_targets) + '\n')
# if source1 and source2:
# else:
# if
# pw.IssueOrder(source.PlanetID(), att_pln.PlanetID(), num_ships)
# rank =
# attack_targets.append([p, rank])
# planets_rank.sort(key=lambda x: x[1], reverse=True)
# if not commandGame:
# # (1) If we currently have a fleet in flight, just do nothing.
# # if len(pw.MyFleets()) >= 2:
# # return
# # (2) Find my strongest planet.
# source = -1
# source_score = -999999.0
# # source_num_ships = 0
# my_planets = pw.MyPlanets()
# for p in my_planets:
# score = float(p.NumShips())
# # if score > source_score:
# if score > p.GrowthRate() * 10:
# source_score = score
# source = p #.PlanetID()
# # source_num_ships = p.NumShips()
# # (3) Find the weakest enemy or neutral planet.
# # dest = -1
# # dest_score = -999999.0
# # not_my_planets = pw.NotMyPlanets()
# planets = pw.Planets()
# # (3.1) remove group members planets from list
# tmp = []
# for p in planets:
# if p.Owner() in group_ids:
# continue
# tmp.append(p)
# planets = tmp
# # f.write('not_my_planets: ' + ', '.join([str( p.Owner() ) for p in not_my_planets]) + '\n')
# # (3.2) remove planets which awaiting group member fleets
# planet_ids = [] # not_my_planets ids
# if planets:
# planet_ids = ([p.PlanetID() for p in planets])
# # f.write('planet_ids: ' + ', '.join([str( p ) for p in planet_ids]) + '\n')
# # f.write('planet_ids: ' + ', '.join([str( p ) for p in planet_ids]) + '\n')
# gfp_ids = [] # group_fleet_planet_ids
# # f.write('EnemyFleets DestinationPlanets: ' + ', '.join([str( fl.DestinationPlanet() ) for fl in pw.EnemyFleets() ]) + '\n')
# for fl in pw.EnemyFleets():
# if fl.Owner() in group_ids and fl.DestinationPlanet() in planet_ids:
# gfp_ids.append(fl.DestinationPlanet())
# gfp_ids = set(gfp_ids)
# # f.write('gfp_ids: ' + ', '.join([str( fl ) for fl in gfp_ids]) + '\n')
# # (3.3) remove planets which awaiting my fleets
# # my_fp_ids = [] # my_fleet_planet_ids
# # # f.write('EnemyFleets DestinationPlanets: ' + ', '.join([str( fl.DestinationPlanet() ) for fl in pw.EnemyFleets() ]) + '\n')
# # for fl in pw.MyFleets():
# # if fl.DestinationPlanet() in planet_ids:
# # my_fp_ids.append(fl.DestinationPlanet())
# # my_fp_ids = set(my_fp_ids)
# # ATTACK Planets
# # not_my_planets
# my_planet_ids = ([p.PlanetID() for p in pw.MyPlanets()])
# # (4) analyze planets
# # not_my_planets = tmp
# # f.write('not_my_planets: ' + ', '.join([str( p.Owner() ) for p in not_my_planets]) + '\n')
# planets_rank = []
# for p in planets:
# if p.PlanetID() in gfp_ids: # or p.PlanetID() in my_fp_ids:
# continue
# # planet's obtain score or defeat
# killrate = 1
# defeat = 1
# if winRatio >= 1.5:
# # Killer mode
# f.write('KILLMODE \n')
# if p.Owner() != 0: # enemy
# killrate += 1000
# elif p.PlanetID() in my_planet_ids:
# for fleet in pw.EnemyFleets():
# if fl.Owner() in group_ids:
# continue
# if fleet.DestinationPlanet() == p.PlanetID():
# defeat = 1000
# # defeat = ( pw.Distance(source.PlanetID(), p.PlanetID()) )
# score = (1.0 * p.GrowthRate() * killrate * defeat ) / (1 + p.NumShips() + 2 * pw.Distance(source.PlanetID(), p.PlanetID()))
# planets_rank.append([p, score])
# # if score > dest_score:
# # dest_score = score
# # dest = p #.PlanetID()
# # f.write('planets_rank: ' + ', '.join([str( p[1] ) for p in planets_rank]) + '\n')
# planets_rank.sort(key=lambda x: x[1], reverse=True)
# f.write('planets_rank Owner: ' + ', '.join([str( p[0].Owner() ) for p in planets_rank]) + '\n')
# # f.write('planets_rank NumShips: ' + ', '.join([str( p[0] ) for p in planets_rank]) + '\n')
# f.write('planets_rank: ' + ', '.join([str( p[1] ) for p in planets_rank]) + '\n')
# # (5) Send ships from my strongest planet to others
# sent_ships = 0
# for elem in planets_rank:
# f.write('planet: ' + str(elem[0]) + '\n')
# f.write('planets score: ' + str(elem[1]) + '\n')
# if (source.NumShips() - sent_ships * 3) <= 0:
# break
# dest = elem[0] # planet
# req_num_ships = dest.NumShips()
# # NOTE: worse result on current
# #
# for fleet in pw.EnemyFleets():
# if fl.Owner() in group_ids:
# continue
# if fleet.DestinationPlanet() == dest.PlanetID():
# req_num_ships += fleet.NumShips()
# for fleet in pw.MyFleets():
# if fleet.DestinationPlanet() == dest.PlanetID():
# req_num_ships -= fleet.NumShips()
# if req_num_ships < 0:
# continue
# f.write('req_num_ships: ' + str(req_num_ships) + '\n')
# add_ships = 1
# f.write('dest.Owner(): ' + str(dest.Owner()) + '\n')
# if dest.Owner() != 0: # foe (not neutral)
# add_ships += dest.GrowthRate() * int(pw.Distance(source.PlanetID(), dest.PlanetID() ))
# f.write('add_ships: ' + str(add_ships) + '\n')
# if winRatio >= 1.5:
# # Killer mode
# add_ships += int(add_ships * 0.3)
# num_ships = req_num_ships * 2 + add_ships
# f.write('num_ships: ' + str(num_ships) + '\n')
# if (source.NumShips() - sent_ships - num_ships) >= 0:
# sent_ships += num_ships
# pw.IssueOrder(source.PlanetID(), dest.PlanetID(), num_ships)
# # else:
# # num_ships = source.NumShips() - sent_ships
# # if num_ships > 50:
# # sent_ships += int(num_ships/2)
# # pw.IssueOrder(source.PlanetID(), dest.PlanetID(), int(num_ships/2))
# end of "if not commandGame:"
f.write('======= Message Send Section ======= \n')
# keep actual nickname (not dead)
if len(messageHistory) > 1 and allies_nicks:
nicknames = []
for nick in allies_nicks:
nick = nick[0]
if nick in ([msh[0] for msh in messageHistory[-1]]):
nicknames.append(nick)
# for msh in messageHistory[-1]:
# f.write('======= msh: ' + str(msh) + '\n')
# if nick in ([msh_nick[0] for msh_nick in msh]):
f.write('======= nicknames: ' + str(nicknames) + '\n')
if nicknames:
# for trg in next_targets[:3]:
next_target1 = '100'
next_target2 = '100'
next_target3 = '100'
if len(next_targets) == 1:
next_target1 = str(int('100') + next_targets[0])
elif len(next_targets) == 2:
next_target1 = str(int('100') + next_targets[0])
next_target2 = str(int('100') + next_targets[1])
elif len(next_targets) > 2:
next_target1 = str(int('100') + next_targets[0])
next_target2 = str(int('100') + next_targets[1])
next_target3 = str(int('100') + next_targets[2])
# gtype = '1' # rush/save-game
# planet = '000'
# leader = '00'
# vote = '0'
mes = int(next_target1 + next_target2 + next_target3)
f.write('======= mes: ' + str(mes) + '\n')
pw.SendMessage(nickname,mes)
def main():
group_ids = []
nickname = 'X'
mz_map_edit = False
my_id = -1
if '-g' in sys.argv:
group_ids = [int(k) for k in sys.argv[2].split(',')]
if '-n' in sys.argv:
nickname = str(sys.argv[4])
if '-mz' in sys.argv:
mz_map_edit = True
f = open('MyBot4_' + str(nickname)+ '.log', 'w')
f.write('group: ' + str(group_ids) + '\n')
f.write('nick: ' + str(nickname) + '\n')
map_data = ''
while(True):
current_line = raw_input()
f.write(current_line + '\n')
if len(current_line) >= 1 and current_line.startswith("."):
if mz_map_edit:
tmp_map = ''
lines = map_data.split("\n")
if my_id < 0:
for line in lines:
line = line.split("#")[0]
tokens = line.split(" ")
if tokens[0] == "Y":
if len(tokens) == 2:
my_id = int(tokens[1])
f.write('my_id: ' + str(my_id) + '\n')
for line in lines:
line = line.split("#")[0] # remove comments
tokens = line.split(" ")
if tokens[0] == "P":
if len(tokens) == 7:
new_line = line
if int(tokens[5]) == my_id:
# stdout.write('1-- ' + "\n")
new_line = "P " + str(tokens[1]) + " " + str(float(tokens[2])) + " " + str(float(tokens[3])) + " " + str(int(tokens[4])) + " " + str(1) + " " + str(int(tokens[6]))
new_line = new_line.replace(".0 ", " ")
elif int(tokens[5]) == 1:
# stdout.write('2-- ' + "\n")
new_line = "P " + str(tokens[1]) + " " + str(float(tokens[2])) + " " + str(float(tokens[3])) + " " + str(int(tokens[4])) + " " + str(my_id) + " " + str(int(tokens[6]))
new_line = new_line.replace(".0 ", " ")
tmp_map += new_line + '\n'
else:
tmp_map += line + '\n'
# stdout.write(str(tmp_map)+ "\n")
map_data = tmp_map
pw = PlanetWars(map_data)
f.write('-------------- NEW TURN ----------------\n')
DoTurn(pw, group_ids, nickname, f)
f.write('----------------------------------------\n')
pw.FinishTurn()
map_data = ''
else:
map_data += current_line + '\n'
if __name__ == '__main__':
try:
import psyco
psyco.full()
except ImportError:
pass
try:
main()
except KeyboardInterrupt:
print 'ctrl-c, leaving ...'