-
Notifications
You must be signed in to change notification settings - Fork 0
/
cave_system.py
184 lines (105 loc) · 3.46 KB
/
cave_system.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
import random
import game
import debug_console
import farm_config
import expand
from header import *
from pathfinding import Astar, RoomWrapper
from node import Node
from space import Space
class Cave(Space):
def __init__(self):
if farm_config.DEBUG_WIN:
debug_console.get()._print("Cave created.")
self.name = 'Cave'
Space.__init__(self)
# Used only by Astar
self.closed_list = []
self.seed(Room, CAVE_GRAPHICS_DIR + 'room', 5)
if farm_config.DEBUG_WIN:
debug_console.get()._print("Rooms seeded.")
entrance = Entrance(0,
10,
CAVE_GRAPHICS_DIR + 'entrance_internal',
self)
rooms = self.contents['Room']
self.Astar = RoomWrapper(Astar)
halls = self.Astar(rooms, self.closed_list)
if farm_config.DEBUG_WIN:
debug_console.get()._print("%d halls found." % len(halls))
for coor in halls:
Hall.create(coor[0], coor[1], CAVE_GRAPHICS_DIR + 'hall', self)
if farm_config.DEBUG_WIN:
debug_console.get()._print("Halls created.")
class Room(Node):
@classmethod
def create(cls, Ystart, Xstart, graphics_file, space):
room = Room(Ystart, Xstart, graphics_file, space)
return room
def __init__(self, Ystart, Xstart, graphics_file, space):
self.name = 'Room'
Node.__init__(self, Ystart, Xstart, graphics_file, space)
self.doors = {}
self.find_doors()
def find_doors(self):
random.seed()
num_doors = random.randint(1, 2)
if num_doors == 1:
self.doors.update({'entrance': []})
elif num_doors == 2:
self.doors.update({'entrance': [], 'exit': []})
ymin = expand.find_min(0, self.boundaries)
ymax = expand.find_max(0, self.boundaries)
xmin = expand.find_min(1, self.boundaries)
xmax = expand.find_max(1, self.boundaries)
corners = expand.find_corners(ymin, ymax, xmin, xmax)
for door in self.doors:
pos = random.choice(self.boundaries)
while pos in corners:
pos = random.choice(self.boundaries)
self.doors[door].extend(pos)
def draw(self, window):
if self.visible:
for line in self.graphics:
for char in line:
for door in self.doors:
if [self.X, self.Y] == self.doors[door]:
char == '#'
# Check for leading whitespace
if char == ' ':
self.X += 1
else:
break
line = line.strip().rstrip()
window.addstr(self.Y, self.X, line)
self.Y += 1
self.X = self.Xstart
self.Y = self.Ystart
class Entrance(Node):
def __init__(self, Ystart, Xstart, graphics_file, space):
self.name = 'Entrance'
Node.__init__(self, Ystart, Xstart, graphics_file, space)
self.doors = {'exit': [Ystart + 4, Xstart + 2]}
self.vicinity = [[Ystart + 1, Xstart + 2]]
# Make sure the entrance is the first passed to Astar pathfinder
self.space.contents['Room'].insert(0, self)
def interact(self, player):
game.get_instance().msg_win.clear()
game.get_instance().msg_win.addstr(1, 5, "Leave the cave? [y/n]")
game.get_instance().msg_win.refresh()
ans = game.get_instance().msg_win.getch()
while ans != ord('y') and ans != ord('n'):
ans = game.get_instance().msg_win.getch()
if ans == ord('n'):
return
elif ans == ord('y'):
player.current_space = Space.members['World']
player.pos = PLAYER_1_START_POS
class Hall(Node):
@classmethod
def create(cls, Ystart, Xstart, graphics_file, space):
hall = Hall(Ystart, Xstart, graphics_file, space)
return hall
def __init__(self, Ystart, Xstart, graphics_file, space):
self.name = 'Hall'
Node.__init__(self, Ystart, Xstart, graphics_file, space)