Skip to content

Commit 00ca518

Browse files
committed
Collision & Docking Detection.
1 parent fcd7564 commit 00ca518

9 files changed

+139
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@
3434
/__pycache__/ShipStarbase.cpython-37.pyc
3535
/__pycache__/ShipKlingon.cpython-37.pyc
3636
/__pycache__/ShipEnterprise.cpython-37.pyc
37+
/__pycache__/ErrorCollision.cpython-37.pyc

Calculators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def sublight_navigation(game):
2424
game.display()
2525
game.display("Sub-light engines engaged.")
2626
game.display()
27-
game.game_map.go_to(dest_sys)
27+
game.move_to(dest_sys)
2828

2929
game.time_remaining -= 1
3030
game.star_date += 1
@@ -68,7 +68,7 @@ def warp_navigation(game):
6868
game.display()
6969
game.enterprise.energy -= energy_required
7070

71-
game.game_map.go_to(dest_sys)
71+
game.move_to(dest_sys)
7272

7373
game.time_remaining -= 1
7474
game.star_date += 1

ErrorCollision.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class ErrorEnterpriseCollision(Exception):
2+
'''
3+
... because some problems simpy have to wait ... =)
4+
'''
5+
def __init__(self, glyph):
6+
super().__init__()
7+
self.glyph = glyph
8+
9+
10+
11+

MapGame.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ShipKlingon import ShipKlingon
66
from Points import Destination
77
from Quadrant import Quadrant
8+
from ErrorCollision import ErrorEnterpriseCollision
89

910
import MapSparse
1011

@@ -57,19 +58,30 @@ def place(self, takers):
5758

5859
def enterprise_in(self, dest=None):
5960
''' Place the ENTERPRISE at the destination, else a
60-
random one. Return the x, y location - else False '''
61-
area = self.area()
61+
random one.
62+
63+
Will raise an ErrorEnterpriseCollision, upon same.
64+
65+
Returns the final x, y location upon success '''
66+
area = self.pw_area()
67+
berror = False
6268
if area:
69+
for p in area._pieces:
70+
if p.xpos == dest.xpos and p.ypos == dest.ypos:
71+
pos = area.place_glyph(Glyphs.ENTERPRISE)
72+
berror = p.glyph
6373
pos = area.place_glyph(Glyphs.ENTERPRISE, dest)
74+
if berror:
75+
raise ErrorEnterpriseCollision(berror)
6476
if pos:
6577
return pos
6678
return False
6779

6880
def enterprise_location(self):
6981
''' Get Enterprise location. False if not found. '''
70-
area = self.area()
82+
area = self.pw_area()
7183
if area:
72-
for obj in area.objs:
84+
for obj in area._pieces:
7385
if obj.glyph == Glyphs.ENTERPRISE:
7486
return obj.xpos, obj.ypos
7587
return False
@@ -82,7 +94,7 @@ def enterprise_out(self):
8294

8395
def place_glyph(self, glyph, dest=None):
8496
''' Place the glyph as the destination, else a random one '''
85-
area = self.area()
97+
area = self.pw_area()
8698
if area:
8799
pos = area.place_glyph(self, glyph, dest)
88100
if pos:
@@ -91,11 +103,11 @@ def place_glyph(self, glyph, dest=None):
91103

92104
def remove(self, xpos, ypos):
93105
''' Remove ANYTHING from the present AREA '''
94-
area = self.area()
106+
area = self.pw_area()
95107
if area:
96108
area.remove(xpos, ypos)
97109

98-
def area(self):
110+
def pw_area(self):
99111
'''
100112
Return the internal / sparsely populated AREA object.
101113
Return an empty / default AREA upon coordinate error.
@@ -119,9 +131,9 @@ def scan_quad(self, sector):
119131
def _count_area(self, glyph):
120132
''' Tally the number of glyphs in the AREA '''
121133
count = 0
122-
area = self.area()
134+
area = self.pw_area()
123135
if area:
124-
for obj in area.objs:
136+
for obj in area._pieces:
125137
if obj.glyph == glyph:
126138
count += 1
127139
return count
@@ -134,7 +146,7 @@ def update_counts(self):
134146
self.stars += area.count_glyphs(Glyphs.STAR)
135147

136148
def remove_items(self, removed):
137-
area = self.area()
149+
area = self.pw_area()
138150
for obj in removed:
139151
area.remove(obj.xpos, obj.ypos)
140152
self.update_counts()
@@ -144,7 +156,7 @@ def get_area_klingons(self):
144156
Return this Area's data for Kingons, in an array.
145157
'''
146158
results = []
147-
area = self.area()
159+
area = self.pw_area()
148160
for data in area.get_data(Glyphs.KLINGON):
149161
ship = ShipKlingon()
150162
ship.from_map(data.xpos, data.ypos)
@@ -166,14 +178,14 @@ def get_area_objects(self):
166178
NOTE: Changes to this collection will update Area
167179
content.
168180
'''
169-
area = self.area()
170-
return area.objs
181+
area = self.pw_area()
182+
return area._pieces
171183

172184
def game_id(self, piece):
173185
'''
174186
Uniquely identify a game piece / object.
175187
'''
176-
area = self.area()
188+
area = self.pw_area()
177189
num = (area.number * 100) + (piece.ypos * 8) + piece.xpos
178190
return f"{piece.glyph[1]}x{num}"
179191

@@ -188,25 +200,29 @@ def get_all(self, glyph):
188200
return results
189201

190202
def quad(self):
191-
area = self.area()
203+
area = self.pw_area()
192204
return Quadrant.from_area(area)
193205

194206
def get_map(self):
195207
'''
196208
Generate AREA map of the present sector.
197209
'''
198-
area = self.area()
210+
area = self.pw_area()
199211
return area.get_map()
200212

201213
def random_jump(self):
202214
dest = Destination(
203-
random.randint(1, 65),
215+
random.randint(1, 64),
204216
random.randint(0, 7),
205217
random.randint(0, 7)
206218
)
207-
self.go_to(dest)
219+
self._go_to(dest)
208220

209-
def go_to(self, dest):
221+
def _go_to(self, dest):
222+
'''
223+
Place the main player (Enterprise, for now) into the Area.
224+
Returns the final, effective, player location.
225+
'''
210226
if self.last_nav:
211227
self.enterprise_out()
212228
if dest.sector > 0:
@@ -217,8 +233,11 @@ def go_to(self, dest):
217233
dest.sector = self.sector
218234
dest.xpos = self.xpos
219235
dest.ypos = self.ypos
220-
self.enterprise_in(dest)
236+
pos = self.enterprise_in(dest)
237+
dest.xpos = pos[0]
238+
dest.ypos = pos[1]
221239
self.last_nav = dest
240+
return dest
222241

223242
def randomize(self, bases=None, stars=None, aliens=None):
224243
if not aliens:

MapSparse.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self):
3838
self.name = ""
3939
self.number = -1
4040
self.scanned = False
41-
self.objs = []
41+
self._pieces = []
4242

4343
def is_null(self):
4444
'''
@@ -48,21 +48,21 @@ def is_null(self):
4848
return dum.name == self.name and \
4949
dum.number == self.number and \
5050
dum.scanned == self.scanned and \
51-
len(dum.objs) == len(self.objs)
51+
len(dum.objs) == len(self._pieces)
5252

5353
def is_empty(self):
5454
''' Checks to see if the Area has anything ...'''
55-
return len(self.objs) == True
55+
return len(self._pieces) == True
5656

5757
def items(self):
5858
''' Items in the Area ...'''
59-
return len(self.objs)
59+
return len(self._pieces)
6060

6161
def remove(self, xpos, ypos):
6262
''' Remove an item from the Area. '''
63-
for ss, obj in enumerate(self.objs):
63+
for ss, obj in enumerate(self._pieces):
6464
if obj.xpos == xpos and obj.ypos == ypos:
65-
self.objs.remove(obj)
65+
self._pieces.remove(obj)
6666
return
6767

6868
def get_map(self):
@@ -71,7 +71,7 @@ def get_map(self):
7171
of Glyphs.SPACE on error.
7272
'''
7373
results = [[Glyphs.SPACE for _ in range(8)] for _ in range(8)]
74-
for obj in self.objs:
74+
for obj in self._pieces:
7575
results[obj.ypos][obj.xpos] = obj.glyph # ASSURED
7676
return results
7777

@@ -91,7 +91,7 @@ def range_ok(self, xpos, ypos):
9191

9292
def get_data(self, glyph):
9393
results = []
94-
for p in self.objs:
94+
for p in self._pieces:
9595
if p.glyph == glyph:
9696
results.append(SparseMap.Area.clone(p))
9797
return results
@@ -101,7 +101,7 @@ def count_glyphs(self, glyph):
101101
Tally the number of glyphs that we have in the Area.
102102
'''
103103
count = 0
104-
for p in self.objs:
104+
for p in self._pieces:
105105
if p.glyph == glyph:
106106
count += 1
107107
return count
@@ -113,11 +113,11 @@ def plot_glyph(self, xpos, ypos, glyph):
113113
'''
114114
if self.range_ok(xpos, ypos) is False:
115115
return None
116-
for p in self.objs:
116+
for p in self._pieces:
117117
if p.xpos is xpos and p.ypos is ypos:
118118
p.glyph = glyph
119119
return xpos, ypos
120-
self.objs.append(SparseMap.Area.Piece(xpos, ypos, glyph))
120+
self._pieces.append(SparseMap.Area.Piece(xpos, ypos, glyph))
121121
return xpos, ypos
122122

123123
def place_glyph(self, glyph, dest=None):

Quips.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@
5353
" curses: 'Thy fathers spreadeth pox!'",
5454
" yells: 'Your mother is progressive!'",
5555
]
56+
MISTAKES = [
57+
"... the crew was not impressed ...",
58+
"... that's going to leave a mark ...",
59+
"... next time, remember to 'carry the 1'? ...",
60+
"... math lives matter ...",
61+
"... that's coming out of your paycheck ...",
62+
"... this is not a bumper car ...",
63+
"... life can be tough, that way ...",
64+
"... who ordered THAT take-out ...",
65+
"... random is, what random does ...",
66+
"... you've got their attention ...",
67+
"... next time, just text them ...",
68+
"... how rude!",
69+
"... yes, karma CAN hurt ...",
70+
"... life is but a dream!",
71+
"... game over.",
72+
"... they will talk about this one for years.",
73+
"... who is going to pay for this?",
74+
"... galactic insurance premiums skyrocket ...",
75+
"... captain goes down with the starship ...",
76+
"... we'll notify your next-of-kin.",
77+
"... that was not in the script ...",
78+
"... you never did THAT in the simulator ...",
79+
]
5680

5781
class Quips():
5882

@@ -73,6 +97,10 @@ def jibe_defeat(noun):
7397
if random.randrange(0, 100) > 25:
7498
return f"Another {noun.lower()} defeated."
7599
return Quips.jibe(noun, DEFEAT_PREFIX, DEFEAT_SUFFIX)
100+
101+
@staticmethod
102+
def jibe_fatal_mistake():
103+
return MISTAKES[random.randrange(0, len(MISTAKES) - 1)]
76104

77105

78106

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ So far:
66

77
* Added random event Quips – should make the game a tad more ‘NPC’?
88

9+
* Added that classic sublight / in system propulsion system. Warp speeds engines are now a seperate navigational system.
10+
911
* Heavily re-factored for growth, testing, re-use, and maintenance using modern Python.
1012

1113

0 commit comments

Comments
 (0)