# 没什么用,该游戏的模块调用不友好,取数据难
import queue
from battle import commander
# import math
unit_client = commander.Client()
doing_last_order = True
towerid = []
towerrg = []
towerfr = []
towerps = []
tower_id = 0
towers = []
safe_posi=[40,40]
center1 = [[40,0],[20,0]]
center2 = [[20,7],[20,17]]
center3 = [[20,7],[20,18]]
# START Possible orders:
def get_time(*args, **kawargs):
my_time = unit_client.ask_cur_time()
unit_client.when_time(my_time, get_to)
def get_to(*args, **kawargs):
global safe_posi
unit_posi = unit_client.ask_units()
safe_posi = [int(unit_posi[0]['coordinates'][0]/2+unit_posi[1]['coordinates'][0]/2), int(unit_posi[0]['coordinates'][1]/2+unit_posi[1]['coordinates'][1]/2)]
unit_client.do_move(safe_posi)
def gather(*args, **kawargs):
unit_client.do_message_to_craft(get_to)
def attack_center(*args, **kawargs):
center = unit_client.ask_center()
unit_client.do_attack(center['id'])
def attack_nearest(data=None, *args, **kawargs):
enemy = unit_client.ask_nearest_enemy()
unit_client.do_attack(enemy['id'])
def go_to_center1(*args, **kwargs):
global safe_posi
unit_client.do_moves(center1)
safe_posi = center1[1]
return safe_posi
def go_to_center2(*args, **kwargs):
global safe_posi
unit_client.do_moves(center2)
safe_posi = center2[1]
return safe_posi
def go_to_center3(*args, **kwargs):
global safe_posi
unit_client.do_moves(center3)
safe_posi = center3[1]
return safe_posi
def go_to_center_safe(*args, **kwargs):
unit_client.do_move(safe_posi)
def info_get(*args, **kwargs):
global safe_posi
global tower_id
global towerid
global towerrg
global towerfr
global towers
global towerps
towers = unit_client.ask_towers()
for i in range(len(towers)):
towerid.append(towers[i]["id"])
towerfr.append(towers[i]["firing_range"])
towerps.append(towers[i]["coordinates"])
x, y = towers[i]["coordinates"]
lenth = pow((x-safe_posi[0])**2+(y-safe_posi[1])**2,0.5)
towerrg.append(int(lenth))
def attack_posi(*args, **kwargs):
global towerid
global towers
global safe_posi
global tower_id
global towerrg
global towerfr
# global towers
# towers = unit_client.ask_towers()
first = towerrg.index(min(towerrg))
attac_posi = towerrg[first]-towerfr[first]-1
percent = attac_posi/towerrg[first]
safe_posi = (towerps[first][0] - safe_posi[0])*percent, (towerps[first][1] - safe_posi[1])*percent
unit_client.do_move(safe_posi)
tower_id = towerid[first]
unit_client.do_attack(tower_id)
my_info = unit_client.ask_my_info()
safe_posi = my_info["coordinates"]
towerid.pop(first)
towerrg.pop(first)
towerfr.pop(first)
towerps.pop(first)
## END
## START Orders, captain!
orders = queue.Queue()
# orders.put_nowait(gather)
orders.put_nowait(go_to_center1)
orders.put_nowait(go_to_center2)
# orders.put_nowait(attack_nearest)
orders.put_nowait(get_to)
orders.put_nowait(info_get)
# orders.put_nowait(get_time)
# orders.put_nowait(attack_nearest)
# orders.put_nowait(attack_nearest)
# orders.put_nowait(attack_posi)
# orders.put_nowait(go_to_center3)
# orders.put_nowait(attack_posi)
# orders.put_nowait(attack_posi)
# orders.put_nowait(attack_posi)
# orders.put_nowait(attack_posi)
orders.put_nowait(attack_center)
# orders.put_nowait(attack_nearest)
# orders.put_nowait(attack_nearest)
# orders.put_nowait(attack_nearest)
# orders.put_nowait(attack_nearest)
#Loop engine:
def mark_as_idle(*args, **kwargs):
global doing_last_order
doing_last_order = False
# print('ORDERS DOING: Is now', doing_last_order)
unit_client.when_idle(global_scheduler)
def mark_as_doing(*args, **kwargs):
global doing_last_order
doing_last_order = True
# print('ORDERS DOING: Is now', doing_last_order)
def global_scheduler(*args, **kwargs):
global doing_last_order
# print('LOOP: scheduler woke')
# print('ORDERS COUNT:', orders.qsize())
# print('DOING ORDER ?:', doing_last_order) # needed only if coding async
while not doing_last_order and not orders.empty():
mark_as_doing()
next_order = orders.get()
# print('ORDERS: next is "%s"' % next_order.__name__)
next_order()
# print('ORDERS: doing "%s"' % next_order.__name__)
unit_client.when_idle(mark_as_idle)
# print('LOOP: scheduler set')
print ('ORDERS START COUNT:', orders.qsize())
mark_as_idle()