forked from cwren/davesgalaxy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
redirarcs
executable file
·245 lines (186 loc) · 7.65 KB
/
redirarcs
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
#!/usr/bin/env python
# vim: set ts=2 sw=2 expandtab:
import game
from optparse import OptionParser
import sys
import shape
import random
def main():
parser = OptionParser()
parser.add_option("-U", "--username", dest="username",
help="username of login")
parser.add_option("-P", "--password", dest="password",
help="password for login")
parser.add_option("-n", "--noupgrade", dest="doupgrade",
action="store_false", default=True, help="dry run")
parser.add_option("-s", "--source_route", dest="source",
type="string", help="source route area")
parser.add_option("-d", "--dest_route", dest="dest",
type="string", help="destination route area")
parser.add_option("-r", "--random", dest="random",
action="store_true", default=False, help="pick a random planet in the target area")
(options, args) = parser.parse_args()
print "options " + str(options)
g=game.Galaxy()
if options.username and options.password:
# explicit login
g.login(options.username, options.password, force=True)
else:
# try to pick up stored credentials
g.login()
try:
dest_route = g.find_route(options.dest)
dest_shape = shape.Polygon(*(dest_route.points))
except:
print "could't find dest route"
sys.exit(1)
source_shape = None
try:
source_route = g.find_route(options.source)
source_shape = shape.Polygon(*(source_route.points))
except:
pass
g.load_fleet_cache()
if source_shape == None:
print "could't find source route"
sys.exit(1)
RedirArcsFromOwned(g, options.doupgrade, source_shape, dest_shape)
RedirReturningArcs(g, options.doupgrade, source_shape, dest_shape)
# RedirArcsFromSource(g, options.doupgrade, source_shape, dest_shape, options.random)
g.write_fleet_cache()
# redirect all arcs with targets in source shape into dest shape
def RedirArcsFromSource(g, doupgrade, source_shape, dest_shape, random, overlap=True, rearrange=False):
print "finding planets in source sector..."
source_targets = game.FindAllPlanetsInShape(g, source_shape)
print "%d planets in source sector" % len(source_targets)
print "finding planets in target sector..."
targets = game.FindUnownedPlanetsInShape(g, dest_shape)
print "%d unowned planets in target sector" % len(targets)
# only handle planets that aren't in source
if overlap:
print "removing overlap planets from source..."
for p in targets:
for s in source_targets:
if p.planetid == s.planetid:
targets.remove(p)
break
print "%d unowned planets in target sector" % len(targets)
# if we're rearranging things, then consider targets that are already targetted
if not rearrange:
print "removing already targetted planets in target sector..."
targets = game.TrimColonyTargettedPlanets(g, targets)
print "%d unowned planets in target sector" % len(targets)
if len(targets) == 0:
print "all planets in destination already claimed"
return False
redir_arcs = []
for f in g.fleets:
f.load()
try:
if f.ships['arcs'] >= 1:
#print "found potential arc fleet %s, destination %s" % (f, f.destination)
dest_planet_id = game.FleetDestToPlanetID(f.destination)
if dest_planet_id != None:
for p in source_targets:
if int(p.planetid) == int(dest_planet_id):
print "found arc %s heading into source space" % f
redir_arcs.append(f)
else:
#print "not going to planet"
#print f.destination
if source_shape.inside(f.destination):
print "found arc %s heading into source space" % f
redir_arcs.append(f)
except:
pass
print "%d arcs to redirect from source" % len(redir_arcs)
RedirArcs(g, doupgrade, redir_arcs, targets, random)
# redirect arcs with their home as their target
def RedirReturningArcs(g, doupgrade, source_shape, dest_shape):
targets = game.FindUnownedPlanetsInShape(g, dest_shape)
owned_planets = game.FindOwnedPlanetsInShape(g, dest_shape)
print "%d unowned planets in sector" % len(targets)
targets = game.TrimColonyTargettedPlanets(g, targets)
print "%d unowned planets in sector after trimming colony targets" % len(targets)
# if len(targets) == 0:
# print "all planets in destination already claimed"
# return False
redir_arcs = []
for f in g.fleets:
f.load()
if f.ships.get('arcs', 0) > 0:
#print "found potential arc fleet %s, destination %s" % (f, f.destination)
dest_planet_id = game.FleetDestToPlanetID(f.destination)
#print "fleet %s dest %s home %s" % (f, str(dest_planet_id), str(f.home.planetid))
if source_shape == None or source_shape.inside(f.coords):
if f.home != None:
if dest_planet_id == f.home.planetid:
print "found returning arc %s" % f
redir_arcs.append(f)
RedirArcs(g, doupgrade, redir_arcs, targets)
# redirect arcs away from owned planets
def RedirArcsFromOwned(g, doupgrade, source_shape, dest_shape):
targets = game.FindUnownedPlanetsInShape(g, dest_shape)
print "%d unowned planets in sector" % len(targets)
targets = game.TrimColonyTargettedPlanets(g, targets)
print "%d unowned planets in sector after trimming colony targets" % len(targets)
owned_planets = game.FindOwnedPlanetsInShape(g, source_shape)
print "%d owned planets in source sector" % len(owned_planets)
# if len(targets) == 0:
# print "all planets in destination already claimed"
# return False
redir_arcs = []
for f in g.fleets:
f.load()
try:
if f.ships['arcs'] >= 1:
#print "found potential arc fleet %s, destination %s" % (f, f.destination)
dest_planet_id = game.FleetDestToPlanetID(f.destination)
for p in owned_planets:
if int(p.planetid) == int(dest_planet_id):
print "found newly owned planet %s with arc %s heading for it" % (p, f)
redir_arcs.append(f)
break
except:
pass
RedirArcs(g, doupgrade, redir_arcs, targets)
def RedirArcs(g, doupgrade, arc_fleets, targets, rand=False):
print "redirecting %d arcs to %d targets" % (len(arc_fleets), len(targets))
#print arc_fleets
#print targets
#sys.exit(1)
if rand:
for f in arc_fleets:
index = random.randrange(0, len(targets)-1)
t = targets[index]
print "moving fleet %s to planet %s, distance %f" % (f, t, game.distance_between(f.coords, t.location))
if doupgrade:
f.move_to_planet(t)
# try not to pick this one again
targets.remove(t)
if len(targets) == 0:
print "ran out of targets"
break
else: # not random
# sort the shortest distances for each fleet
for f in arc_fleets:
f.targets = sorted(targets, key=lambda planet: game.distance_between(f.coords, planet.location))
# iterate over all the fleets, building the shortest route of all of them
while len(arc_fleets) > 0 and len(targets) > 0:
# sort all of the remaining arcs by their shortest route
arc_fleets = sorted(arc_fleets, key=lambda fleet: game.distance_between(fleet.coords, fleet.targets[0].location))
arc = arc_fleets[0]
target = arc.targets[0]
print "moving fleet %s to planet %s, distance %f" % (arc, target, game.distance_between(arc.coords, target.location))
if doupgrade:
arc.move_to_planet(target)
# remove this fleet and this target out of all of the lists
arc_fleets.remove(arc)
for f in arc_fleets:
f.targets.remove(target)
# trim the master target list
targets.remove(target)
for f in arc_fleets:
del f.targets
if __name__ == "__main__":
main()