/
planetbase.py
230 lines (184 loc) · 6.75 KB
/
planetbase.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
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
# -*- encoding: utf-8 -*-
import sys
import random
import namegen
from buildings import *
from cmd import Cmd
import state
import logging
log_level = logging.DEBUG if "--debug" in sys.argv else logging.ERROR
logging.basicConfig(stream=sys.stderr, level=log_level)
state = state.State()
state.buildings = [Central(), Transport(), PowerLine()]
state.enemy_units = [combat.AngryAnt("enemy"), combat.AngryAnt("enemy")]
def startswith(start, names):
return [name for name in names if name.startswith(start)]
def startbuilding(what):
state.buildings.append(Building(what))
print "building started"
def starttraining(who):
state.buildings.append(Training(who))
print "training started"
class PlanetBase(Cmd):
def __init__(self):
Cmd.__init__(self)
self.prompt = "> "
self.intro = "Planetbase sim"
def do_status(self, line):
state.printStatus()
def help_status(self):
print "Show current base status"
def do_exit(self, line):
exit()
def help_exit(self):
print "Exit simulation"
def do_units(self, line):
print state.units
def help_units(self):
print "Show active units"
def do_train(self, line):
if line=="agent":
starttraining(TrainableAgent(namegen.createName()))
elif line == "medic":
starttraining(TrainableMedic(namegen.createName()))
elif line == "droid":
starttraining(TrainableDroid())
else:
print "train who?"
def complete_train(self, text, line, i, j):
return startswith(text, ["agent", "medic", "droid"])
def help_train(self):
print "Start training [agent], [medic] or [droid]"
print "train medic"
print "train agent"
print "train droid"
def do_attack(self, line):
#all current agents move to attack
if line == "":
for unit in state.units:
unit.set_attacking(True)
elif line == "stop":
for unit in state.units:
unit.set_attacking(False)
else:
print "what?!"
def complete_attack(self, text, line, i, j):
return startswith(text, ["stop"])
def help_attack(self):
print "Send living units to attack enemy"
def do_build(self, line):
if line == "powergen":
startbuilding(PowerGenerator())
elif line == "massgen":
startbuilding(MassGenerator())
elif line == "transport":
startbuilding(Transport())
elif line == "powerline":
startbuilding(PowerLine())
else:
print "build what?"
def complete_build(self, text, line, i, j):
return startswith(text, ["powergen", "massgen", "transport", "powerline"])
def help_build(self):
print "Start construction of new building"
print " build powergen"
print " build massgen"
print " build transport"
print " build powerline"
def do_stop(self, line):
try:
state.buildings[int(line)].stop()
except:
print "wrong argument"
def help_stop(self):
print "Stop active building"
print " stop NUMBER"
def do_start(self, line):
try:
state.buildings[int(line)].start()
except:
print "wrong argument"
def help_start(self):
print "Start again inactive building"
print " start NUMBER"
def default(self, line):
print "what?!"
def emptyline(self):
self.end_turn()
self.begin_turn()
def defend_base(self):
attackers = [unit for unit in state.enemy_units if random.random() < 0.005]
if len(attackers) >0:
state.warning("We were attacked by " +str(len(attackers)) + " unit(s)")
real_attackers = attackers[:]
defenders = state.units[:]
combat.play(defenders, real_attackers)
for unit in attackers:
if unit not in real_attackers:
state.enemy_units.remove(unit)
for unit in state.units:
if unit not in defenders:
state.badnews(unit.name + " KIA")
state.units.remove(unit)
if not state.units:
state.badnews("Enemy broke through!")
for i in range(len(real_attackers)):
b = random.choice(state.buildings)
if random.random()<0.1:
state.badnews(str(b) + " was destroyed")
state.buildings.remove(b)
def perform_mission(self):
our_units = [unit for unit in state.units if unit.attacking]
attackers = [unit for unit in state.enemy_units if random.random() < 0.01]
if our_units and attackers:
state.warning("Encountered " +str(len(attackers)) + " unit(s)")
real_attackers = attackers[:]
defenders = state.units[:]
combat.play(our_units, real_attackers)
for unit in attackers:
if unit not in real_attackers:
state.enemy_units.remove(unit)
for unit in state.units:
if unit not in our_units:
state.badnews(unit.name + " KIA in a mission")
state.units.remove(unit)
def begin_turn(self):
state.buildings = filter(lambda x: x!=None, state.buildings)
self.defend_base()
if not state.buildings:
raise Exception("YOU LOST")
state.buildings.sort(key = lambda x: x.sortkey())
state.printStatus()
def autoheal(self):
for unit in state.units:
if unit.wounded() and state.transport > 0 and state.mass > 0 and \
state.energy > 0 and state.powerline > 0:
unit.heal()
state.transport -= 1
state.mass -= 1
state.energy -= 1
state.powerline -= 1
state.goodnews(unit.name+" was healed")
def tick_buildings(self):
for building in state.buildings:
building.tick(state)
def tick_enemy(self):
new_units = []
for unit in state.enemy_units:
r = random.random()
if r < 0.01:
unit.levelup()
elif r < 0.03:
new_units.append(combat.AngryAnt("enemy"))
elif r < 0.5:
unit.heal()
state.enemy_units += new_units
def end_turn(self):
self.perform_mission()
state.clear()
self.tick_buildings()
self.autoheal()
self.tick_enemy()
game = PlanetBase()
game.begin_turn()
game.cmdloop()