forked from cwren/davesgalaxy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollectsteel.py
executable file
·69 lines (56 loc) · 1.9 KB
/
collectsteel.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
#!/usr/bin/env python
import game
from optparse import OptionParser
import sys
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("-t", "--type", dest="type",
help="type of ship to build", default="frigates")
parser.add_option("-r", "--radius", dest="radius",
help="maximum distance from sink to initiate a build",
default=5.0,
type="float")
(options, args) = parser.parse_args()
sink_opt = args[0]
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:
sink = g.get_planet(int(sink_opt))
except ValueError:
sink = g.find_planet(sink_opt)
sink.load()
print "using planet %d with name %s" % (sink.planetid, sink.name)
collectsteel(g, sink, options.radius, options.type)
def collectsteel(g, sink, radius, type):
total = 0
print 'Looking for planets with excess steel within %f of %s.' % (
radius, sink.name)
for p in g.planets:
if p.planetid != sink.planetid and sink.distance_to(p) <= radius:
p.load()
surplus = 1
while p.can_build({type: surplus}):
surplus += 1
surplus -= 1
if surplus > 0:
fleet = p.build_fleet({type: surplus},
interactive=False,
skip_check=True)
if fleet:
total += surplus
fleet.move_to_planet(sink)
else:
print "build failed."
value = game.ship_cost({type: total})
print "collected %d steel" % value['steel']
if __name__ == "__main__":
main()