This repository has been archived by the owner on Mar 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
planet.py
191 lines (166 loc) · 5.83 KB
/
planet.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
# -*- coding: utf-8 -*-
from config import options
class Planet(object):
def __init__(self, id, name, coords, url, in_construction_mode=False):
self.id = id
self.name = name
self.url = url
self.coords = coords
self.mother = False
self.galaxy, self.system, self.position = map(int, self.coords.split(":"))
self.in_construction_mode = in_construction_mode
self.mines = (
'metalMine',
'crystalMine',
'deuteriumMine'
)
self.resources = {
'metal': 0,
'crystal': 0,
'deuterium':0,
'energy':0
}
self.buildings = {
'metalMine': {
'level': 0,
'buildUrl':'',
'can_build': False,
'sufficient_energy': False
},
'crystalMine': {
'level': 0,
'buildUrl':'',
'can_build': False,
'sufficient_energy': False
},
'deuteriumMine': {
'level': 0,
'buildUrl':'',
'can_build': False,
'sufficient_energy': False
},
'solarPlant': {
'level': 0,
'buildUrl':'',
'can_build': False,
'sufficient_energy': True
},
'fusionPlant': {
'level': 0,
'buildUrl':'',
'can_build': False,
'sufficient_energy': True
},
'solarSatellite': {
'level': 0,
'buildUrl':'',
'can_build': False,
'sufficient_energy': True
},
}
self.ships = {
'lm': 0,
'hm': 0,
'cr': 0,
'ow': 0,
'pn': 0,
'bb': 0,
'ns': 0,
'gs': 0,
'lt': 0,
'dt': 0,
'cs': 0,
'rc': 0,
'ss': 0
}
def __str__(self):
return self.name
def __eq__(self, other):
return self.id == other.id
def get_mine_to_upgrade(self):
build_options = options['building']
min_energy_level = int(build_options['min_energy_level'])
levels_diff = map(int, build_options['levels_diff'].split(','))
max_fusion_lvl = int(build_options['max_fusion_plant_level'])
b = self.buildings
build_power_plant = self.resources['energy'] <= 0
mine_levels = [0, 0, 0]
for i, mine in enumerate(self.mines):
mine_levels[i] = b[mine]['level']
proposed_levels = [
b['metalMine']['level'],
b['metalMine']['level'] - levels_diff[0],
b['metalMine']['level'] - levels_diff[0] - levels_diff[1]
]
proposed_levels = [0 if l < 0 else l for l in proposed_levels]
if proposed_levels == mine_levels or (mine_levels[1] >= proposed_levels[1] and mine_levels[2] >= proposed_levels[2]):
proposed_levels[0] += 1
num_suff_energy = 0
for i in xrange(3):
building = self.mines[i]
if b[building]['sufficient_energy']:
num_suff_energy += 1
if b[building]['can_build'] and proposed_levels[i] > b[building]['level']:
if b[building]['sufficient_energy']:
return building, b[building]['build_url']
else:
build_power_plant = True
if build_power_plant or num_suff_energy == 0:
if b['solarPlant']['can_build']:
return u'Solar plant', b['solarPlant']['build_url']
elif b['fusionPlant']['can_build'] and \
b['fusionPlant']['level'] < max_fusion_lvl:
return u'Fusion plant', b['fusionPlant']['build_url']
else:
return None, None
else:
return None, None
def is_moon(self):
return False
def has_ships(self):
'''
Return true if any ship is stationing on the planet
'''
return any(self.ships.values())
def get_distance(self, planet):
"""
Return distance to planet `planet`
"""
try:
g, s, p = map(int, planet.split(":"))
except Exception:
return 100000
d = 0
d += (abs(g - self.galaxy) * 100)
d += (abs(s - self.system) * 10)
d += (abs(p - self.position))
return d
def get_fleet_for_resources(self, r):
total = sum([r.get('metal', 0), r.get('crystal', 0), r.get('deuterium', 0)])
to_send = 0
ships = {'dt': 0, 'lt': 0}
for kind in ('dt', 'lt'):
if self.ships[kind] > 0:
for i in xrange(self.ships[kind]):
to_send += (25000 if kind == 'dt' else 5000)
ships[kind] += 1
if to_send > total:
return ships
return ships
def get_nearby_systems(self, radius):
g, s, p = map(int, self.coords.split(":"))
start_system = max(1, s-radius)
end_system = min(499, s+radius)
systems = []
for system in xrange(start_system, end_system):
systems.append("%s:%s" % (g, system))
return systems
class Moon(Planet):
def __init__(self, id, coords, url):
super(Moon, self).__init__(id, 'Moon', coords, url)
def get_mine_to_upgrade(self):
return None, None
def is_moon(self):
return True
def __str__(self):
return '[%s] %s' % (self.coords, self.name)