-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAction.py
More file actions
198 lines (175 loc) · 6.36 KB
/
Action.py
File metadata and controls
198 lines (175 loc) · 6.36 KB
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
# Actions class
import random
import time
from Ship import Ship
from MyEvent import MyEvent
class Action:
def __init__(self, mud):
self.mud = mud
def help(self):
print("Available commands: ")
for x in dir(self):
if not str(x).startswith('__') and x != 'mud':
print(x)
def dock(self, ship_name="none"):
do_event(self.mud)
if len(self.mud.space) > 0:
if len(self.mud.space) == 1:
sel = 0
else:
sel = find_name(self.mud.space, ship_name)
if sel < 0:
sel = prompt_index(self.mud.space, "dock")
s = self.mud.space[sel]
s.dock_ship()
print("BEEP BEEP Docking " + str(s))
self.mud.ships.append(self.mud.space.pop(sel))
else:
print("No ships in space")
def build(self, build_type="ship"):
do_event(self.mud)
if build_type == "ship":
if self.mud.met > 9:
ship_name = input("Enter the name of your new ship: ")
new_ship = Ship(ship_name, 1)
self.mud.ships.append(new_ship)
self.mud.met -= 10
print("Congratulations!!! You successfully built the ship " + new_ship.name)
display_ship()
else:
print("You don't have enough Metal")
else:
print("You want to build a base?!?")
def proc(self, num=0):
self.process(num)
# if no user input, process all, otherwise process number passed in
def process(self, num=0):
print("Analyzing events taking place in the galaxy...")
do_event(self.mud)
# if no user input, process all, otherwise process number passed in
def refine(self, num=0):
if self.mud.ore > 0:
if int(num) == 0:
ore_to_proc = int(self.mud.ore)
else:
ore_to_proc = int(num)
if 0 < ore_to_proc <= self.mud.ore:
new_fuel = ore_to_proc * 2
self.mud.fuel += new_fuel
show_progress(2)
print('Refined ' + str(ore_to_proc) + ' raw ore into ' + str(new_fuel) + ' fuel.')
else:
print("Not enough ore to process. Only have " + str(self.mud.ore))
def go(self, num=1):
do_event(self.mud)
for n in range(0, int(num)):
if len(self.mud.space) > 0:
for i, s in enumerate(self.mud.space):
if (s.go(1)):
# get a new event
eve = self.mud.new_event(10)
assert isinstance(eve, MyEvent)
print("Discovered " + eve.name() + " event!")
show_progress(3)
else:
print("No ships in space.")
return
def launch(self, ship_name="none"):
ship_index = find_name(self.mud.ships, ship_name)
if ship_index < 0:
if len(self.mud.ships) == 1:
ship_index = 0
elif len(self.mud.ships) > 1:
ship_index = prompt_index(self.mud.ships, "space")
else:
print("No ships yet so sending probe...")
self.mudl.fuel -= 1
eve = self.mud.new_event(10)
print("Discovered " + eve.name() + " event!")
show_progress(1)
return
f = prompt_number("How much fuel do you take? " + str(self.mud.fuel) + ": ", self.mud.fuel)
if int(f) > self.mud.fuel:
print("Sorry you have " + str(self.mud.fuel) + " fuel.")
return
self.mud.ships[ship_index].refuel(int(f))
self.mud.fuel = self.mud.fuel - int(f)
print("Wooooooooosssssshh sending " + str(self.mud.ships[ship_index].name) + " to space!!")
sel_ship = self.mud.ships.pop(ship_index)
self.mud.space.append(sel_ship)
def show(self, arg="none"):
if arg == "none":
print("Ore: " + str(self.mud.ore))
print("Gas: " + str(self.mud.gas))
print("Metal: " + str(self.mud.met))
print("Fuel: " + str(self.mud.fuel))
print("Crystals: " + str(self.mud.cry))
print("Bases: " + str(self.mud.bases))
print("Ships: ")
for i, val in enumerate(self.mud.ships):
print(" " + str(val))
print("Space: ")
for i, val in enumerate(self.mud.space):
print(" " + str(val))
else:
ship_name = arg
ship_index = find_name(self.mud.ships, ship_name)
if ship_index > -1:
my_ship = self.mud.ships[ship_index]
print(str(my_ship))
else:
print("Sorry don't know that ship.")
def prompt_index(my_list, action_name):
p_num = 0
for i, val in enumerate(my_list):
print(i, val)
while True:
p = input("Which (##) would you like to " + action_name + "? ")
try:
p_num = int(p)
break
except ValueError:
print("Please input a number.")
continue
return p_num
def prompt_number(message, default_value=0):
p_num = default_value
while True:
try:
p = input(message)
# take the default value when none is entered
if not p and not default_value==0:
break
p_num = int(p)
break
except ValueError:
print("Please input a number.")
continue
return p_num
def do_event(mud):
if len(mud.events) > 0:
# index = prompt_index(self.mud.events, "process")
# always process first one
index = 0
mud.events[index].start()
mud.ore += mud.events.pop(index).process()
if mud.ore < 0:
mud.ore = 0
print('You now have ' + str(mud.ore) + ' ore.')
def show_progress(seconds):
for c in range(0, seconds):
print(".", end='', flush=True)
time.sleep(1)
print()
# look for the name in the list and return index
def find_name(search_list, search_name):
for i, val in enumerate(search_list):
if search_name == val.name:
return i
return -1
def display_ship():
print(" +--->")
print("=??|() -\\")
print(" | - $$>")
print("=??|() -/")
print(" +--->")