1+ # 这个是一个示例文件,展示了如何使用OpenRA_Copilot_Library库,尽可能详细的包含了库中的所有功能,可以作为参考
2+
3+ # 该代码对应了一个基础的开局行为:展开基地车,建造电厂,兵营,各种基本单位,然后步兵探索地图,飞机探索地图,同时发展了我的科技,建造了各种部队,最后进攻敌方基地
4+
5+ import sys
6+ import os
7+ import time
8+ import json
9+ import random
10+
11+ sys .path .insert (0 , os .path .dirname (os .path .abspath (__file__ )))
12+ sys .path .insert (0 , os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))))
13+ sys .path .insert (0 , os .path .dirname (os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))))
14+
15+
16+ import OpenRA_Copilot_Library as OpenRA
17+ from OpenRA_Copilot_Library import *
18+
19+ api = OpenRA .GameAPI ("localhost" )
20+
21+ # 展开基地车
22+
23+ # api.deploy_mcv_and_wait(wait_time=1.0)
24+ # print("基地车已展开完毕")
25+
26+ # # 建造电厂和兵营
27+ # api.ensure_can_build_wait("电厂")
28+ # build = api.produce_wait("电厂", 1, True)
29+ # api.ensure_can_build_wait("矿场")
30+ # build = api.produce_wait("矿场", 1, True)
31+ # # 因为电厂本来就是兵营前置了,所以先电厂后兵营
32+ # api.ensure_can_build_wait("兵营")
33+ # build = api.produce_wait("兵营", 1, True)
34+
35+
36+ # if api.ensure_can_produce_unit("步兵"):
37+ # print("开始生产3个步兵...")
38+ # wtank = api.produce("步兵", 5)
39+ # api.wait(wtank)
40+ # else:
41+ # raise RuntimeError("无法生产防空车")
42+
43+ # # # # 蓝噪声算法,用于探索地图
44+ # def _shadow_points(mapinfo: MapQueryResult, step, exponly=True):
45+ # W, H = mapinfo.MapWidth -2 , mapinfo.MapHeight -2
46+ # StartX, StartY = 2, 2
47+ # vis = mapinfo.IsVisible
48+ # exp = mapinfo.IsExplored
49+ # if exponly: # 备选:完全未探明的
50+ # pts = [Location(x, y) for y in range(StartY, H, step)
51+ # for x in range(StartX, H, step) if not exp[x][y]]
52+ # else:
53+ # pts = [Location(x, y) for y in range(StartY, W, step)
54+ # for x in range(StartX, H, step) if not vis[x][y]]
55+ # return pts
56+
57+
58+ # def explore(api: GameAPI, unit: list[Actor], mapinfo: MapQueryResult, r=18, per_unit=4, spacing=4, step=2):
59+ # if not unit:
60+ # return
61+ # all_shadow = _shadow_points(mapinfo, step)
62+ # if not all_shadow:
63+ # return
64+
65+ # taken = [] # 全局蓝噪声约束
66+ # batch = []
67+
68+ # for u in unit:
69+ # api.update_actor(u)
70+ # upos = u.position
71+ # local = [p for p in all_shadow if p.manhattan_distance(upos) <= r]
72+ # if not local:
73+ # local = [
74+ # p for p in all_shadow if p.manhattan_distance(upos) <= 2*r]
75+ # if not local:
76+ # continue
77+ # # 远且分散优先
78+ # random.shuffle(local)
79+ # local.sort(key=lambda p: min([p.manhattan_distance(q)
80+ # for q in taken] or [1e9]), reverse=True)
81+
82+ # picks = []
83+ # for i in range(per_unit):
84+ # last = None
85+ # if last:
86+ # local.sort(key=lambda p: 2 * min([p.manhattan_distance(q)
87+ # for q in taken] or [1e9]) - p.manhattan_distance(last), reverse=True)
88+ # picked = False
89+ # for p in local:
90+ # if len(picks) >= per_unit:
91+ # picked = True
92+ # break
93+ # if all(p.manhattan_distance(q) >= spacing for q in picks) and all(p.manhattan_distance(q) >= spacing for q in taken):
94+ # picks.append(p)
95+ # taken.append(p)
96+ # picked = True
97+ # last = p
98+ # if not picked:
99+ # r += 5
100+ # local = [
101+ # p for p in all_shadow if p.manhattan_distance(upos) <= r]
102+ # i -= 1
103+
104+ # api.move_units_by_path([u], picks, attack_move=True)
105+
106+ # infantry_list = api.query_actor(
107+ # TargetsQueryParam(type=["步兵"], faction="自己"))
108+ # api.form_group(infantry_list, group_id=1)
109+ # mapinfo = api.map_query()
110+
111+ # explore(api, infantry_list, mapinfo)
112+
113+ # api.ensure_can_build_wait("机场")
114+ # api.produce("核电厂", 1, auto_place_building=True)
115+ # api.produce("矿场", 1, auto_place_building=True)
116+ # api.produce("防空车", 5)
117+ # api.produce("修理中心", 1, auto_place_building=True)
118+ # api.produce_wait("机场", 1)
119+ # api.produce("车间", 1, auto_place_building=True)
120+ # api.produce("步兵", 10)
121+ # time.sleep(1)
122+ # api.produce_wait("Yak", 3)
123+ # api.produce("矿车", 2)
124+ # api.produce("核电厂", 1,auto_place_building=True)
125+ # api.produce("修理厂", 1,auto_place_building=True)
126+ # api.produce("科技中心", 1,auto_place_building=True)
127+
128+
129+ # # 再探索一下
130+ # infantry_list = api.query_actor(
131+ # TargetsQueryParam(type=["步兵"], faction="自己"))
132+ # mapinfo = api.map_query()
133+ # explore(api, infantry_list, mapinfo, r=28)
134+
135+
136+ # # 飞机探索就大一点
137+ # inti_r = 48
138+ # enemy_base = None
139+ # for i in range(5):
140+ # aircraft_list = api.query_actor(TargetsQueryParam(type=["Yak"], faction="自己"))
141+ # mapinfo = api.map_query()
142+ # explore(api, aircraft_list, mapinfo, r=inti_r,
143+ # per_unit=4, spacing=10, step=8)
144+ # for x in range(40):
145+ # enemy = api.query_actor(TargetsQueryParam(type=[""], faction="敌方"))
146+ # for unit in enemy:
147+ # if unit.type == "基地" or unit.type == "建造厂":
148+ # enemy_base = unit
149+ # break
150+ # if enemy_base:
151+ # break
152+ # time.sleep(0.5)
153+ # inti_r += 20
154+
155+ # # 建造"矿场"、"车间"以便生产载具
156+ # api.produce("步兵", 5)
157+ # api.produce("火箭兵", 5)
158+ # api.produce("重坦", 3)
159+ # api.produce("V2", 3)
160+ # api.produce("天启坦克", 3)
161+
162+ if enemy_base :
163+ battle_unit = api .query_actor (
164+ TargetsQueryParam (type = ["战斗单位" ], faction = "自己" ))
165+ api .move_units_by_location (
166+ battle_unit , enemy_base .position , attack_move = True )
167+
168+ # time.sleep(30)
169+
170+ enemy = api .query_actor (TargetsQueryParam (type = ["" ], faction = "敌方" ))
171+ for unit in enemy :
172+ if unit .type == "基地" or unit .type == "建造厂" :
173+ enemy_base = unit
174+ break
175+
176+ for i in range (2000 ):
177+ if enemy_base :
178+ battle_unit = api .query_actor (
179+ TargetsQueryParam (type = ["战斗单位" ], faction = "自己" ))
180+ print (battle_unit )
181+ api .move_units_by_location (
182+ battle_unit , enemy_base .position , attack_move = True )
183+ for unit in battle_unit :
184+ api .update_actor (unit )
185+ print (unit .position .manhattan_distance (enemy_base .position ))
186+ if unit .position .manhattan_distance (enemy_base .position ) <= 5 :
187+ api .attack_target (unit , enemy_base )
188+ if not battle_unit :
189+ break
190+ else :
191+ enemy = api .query_actor (TargetsQueryParam (type = ["" ], faction = "敌方" ))
192+ for unit in enemy :
193+ if unit .type == "基地" or unit .type == "建造厂" :
194+ enemy_base = unit
195+ break
196+ time .sleep (0.1 )
0 commit comments