-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_dungeon.py
executable file
·339 lines (292 loc) · 13.2 KB
/
play_dungeon.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject, GdkPixbuf
from pathlib import Path
from dungeon.dungeon import Dungeon
import yaml
import logging
import argparse
import sys
from dungeon.ui.dialogs import DialogUser
from dungeon.ui.objectwidget import ObjectWidget
from dungeon.utils import roll_dice
from dungeon.ui.uiutils import frame_wrap
import tempfile
import subprocess
import os
import re
import random
logger = logging.getLogger()
class PlayDungeon(Gtk.Application, DialogUser):
def __init__(self):
Gtk.Application.__init__(self)
builder = Gtk.Builder()
builder.add_objects_from_file(sys.path[0] + "/ui/windows.glade",
('mainWindow', 'imgParty', 'imgInventory',
'imgTree', 'imgMap', 'imgSkeletonKey',
'inventoryWindow', 'treeWindow', 'imgRun'))
builder.connect_signals(self)
self.builder = builder
# find the objects we care about
self.window = builder.get_object('mainWindow')
self.mapImage = builder.get_object('mapImage')
#self.mapImage.modify_bg(Gtk.StateType.NORMAL, Gdk.Color.parse("white")[1])
self.mapImage.modify_bg(Gtk.StateType.NORMAL, Gdk.RGBA(1.0, 1.0, 1.0, 1.0).to_color())
self.roomLabel = builder.get_object('roomLabel')
self.visitedLabel = builder.get_object('visitedLabel')
self.xpLabel = builder.get_object('xpLabel')
self.detailsPanel = builder.get_object('detailsPanel')
self.exitsPanel = builder.get_object('exitsPanel')
self.monstersPanel = builder.get_object('monstersPanel')
self.dungeonTime = builder.get_object('dungeonTime')
# variables
self.dungeon = None
self.dungeon_filename = None
self.all_map = False
self.tree_scale = 0
self.tree_svg = None
self.map_scale = 0
self.map_svg = None
self.scales = [1, 1.25, 1.5, 2, 2.5]
self.window.show()
def onNew(self, caller):
print("on file new")
def onOpen(self, caller):
new_filename = self.file_dialog(mode='r', title="Choose a dungeon file")
if new_filename is not None:
try:
self.load_dungeon(new_filename)
except Exception as e:
self.message_box(Gtk.MessageType.ERROR, "Open Failed", e)
def onSave(self, caller):
new_name = self.dungeon_filename
if new_name is None:
self.onSaveAs(caller, 'untitled.dng')
else:
try:
with open(new_name, 'w') as f:
yaml.dump(self.dungeon, stream=f)
self.dungeon_filename = new_name
self.window.set_title(f"Play Dungeon: {new_name}")
except Exception as e:
self.message_box(Gtk.MessageType.ERROR, "Save Failed", e)
def onSaveAs(self, caller, suggested=None):
if suggested is None and self.dungeon_filename is not None:
suggested = self.dungeon_filename
new_filename = self.file_dialog(mode='w', suggested_name=suggested, title="Save Dungeon")
if new_filename is not None:
self.dungeon_filename = new_filename
self.onSave(caller)
def onParty(self, caller):
self.dungeon.player_data['passive_perception'] = self.keypad_input("Passive Perception",
"Enter the party's maximum\npassive perception",
length=2, default=str(self.dungeon.player_data['passive_perception']))
self.onUpdateData(caller)
def onInventory(self, caller):
if not self.dungeon.inventory.contents:
self.message_box(Gtk.MessageType.INFO, "Inventory", "There are no items in the inventory")
return
value = self.dungeon.inventory.get_value()
window = self.builder.get_object('inventoryWindow')
label = self.builder.get_object('lblInventory')
label.set_markup(f"<b>Inventory (value: {value:0.2f}gp)</b>")
box = self.builder.get_object('boxInventory')
for o in box.get_children():
box.remove(o)
for i in self.dungeon.inventory.contents:
ow = ObjectWidget(self.dungeon, i, controls=False)
frame = frame_wrap(ow, f"{i.class_label()} {i.id}",
hpad=5, vpad=0, hmargin=10, vmargin=5)
box.pack_start(frame, True, True, 0)
window.show()
window.run()
window.hide()
def onMap(self, caller):
self.all_map = caller.get_active()
self.onUpdateData(caller)
def onTree(self, caller):
window = self.builder.get_object("treeWindow")
def hide_tree_window(caller, event):
caller.hide()
return True
window.connect('delete-event', hide_tree_window)
def zoom_tree_window(caller, event):
if event.string == '-':
self.tree_scale = max(0, self.tree_scale - 1)
elif event.string == '+':
self.tree_scale = min(4, self.tree_scale + 1)
self.updateTree()
window.connect('key_press_event', zoom_tree_window)
window.show()
if self.tree_svg is None:
self.onUpdateData(caller)
def updateTree(self):
factor = self.scales[self.tree_scale]
loader = GdkPixbuf.PixbufLoader()
# Get the size of the svg
if m := re.search(r'viewBox="(.+?)\s+(.+?)\s+(.+?)\s+(.+?)"', str(self.tree_svg)):
loader.set_size(float(m.group(3)) * factor, float(m.group(4)) * factor)
loader.write(self.tree_svg)
loader.close()
pixbuf = loader.get_pixbuf()
self.builder.get_object("treeImage").set_from_pixbuf(pixbuf)
def updateMap(self):
factor = self.scales[self.map_scale]
loader = GdkPixbuf.PixbufLoader()
# Get the size of the svg
if m := re.search(r'viewBox="(.+?)\s+(.+?)\s+(.+?)\s+(.+?)"', str(self.map_svg)):
loader.set_size(float(m.group(3)) * factor, float(m.group(4)) * factor)
loader.write(self.map_svg)
loader.close()
pixbuf = loader.get_pixbuf()
self.mapImage.set_from_pixbuf(pixbuf)
def onKeyPress(self, caller, event):
if event.string == '-':
self.map_scale = max(0, self.map_scale - 1)
elif event.string == '+':
self.map_scale = min(4, self.map_scale + 1)
self.updateMap()
def onSkeletonKey(self, caller):
self.dungeon.state['skeleton_key'] = caller.get_active()
def onRunTo(self, caller):
# update the run-to menu
scrolled = Gtk.ScrolledWindow()
scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
scrolled.set_hexpand(True)
flowbox = Gtk.FlowBox()
flowbox.set_valign(Gtk.Align.START)
flowbox.set_max_children_per_line(5)
flowbox.set_selection_mode(Gtk.SelectionMode.NONE)
scrolled.add(flowbox)
menu = Gtk.Popover()
menu.set_size_request(240, 150)
menu.add(scrolled)
if self.all_map:
rooms = [x for x in self.dungeon.objects.values() if x.is_a('Room')]
else:
rooms = [x for x in self.dungeon.objects.values() if x.is_a('Room') and x.visited]
for r in sorted(rooms, key=lambda x: int(x.id[1:])):
x = Gtk.Button(label=r.id)
x.connect('clicked', self.onRunToChoice)
flowbox.add(x)
menu.set_relative_to(caller)
menu.set_position(Gtk.PositionType.BOTTOM)
menu.show_all()
menu.popup()
self.runtopopup = menu
def onRunToChoice(self, caller):
logger.debug(f"User has run to room {caller.get_label()}")
self.dungeon.state['current_room'] = self.dungeon.objects[caller.get_label()]
self.dungeon.state['current_room'].visited = True
self.runtopopup.popdown()
self.onUpdateData(caller)
def onDestroyWindow(self, caller, something):
if self.ok_cancel_dialog(Gtk.MessageType.QUESTION, "Quit Application?",
"Any progress you have made since the last save will be lost"):
Gtk.main_quit()
return True
def load_dungeon(self, filename):
with open(filename, 'r') as f:
self.dungeon = yaml.load(f, Loader=yaml.FullLoader)
self.dungeon_filename = filename
self.window.set_title(f"Play Dungeon: {filename}")
self.onUpdateData(None)
def onUpdateData(self, caller):
""" Update all of the data elements """
if self.dungeon == None:
return
# generate the map image
dot = self.dungeon.generate_map_dot(all_rooms=self.all_map).encode('utf-8')
p = subprocess.run(['neato', '-Tsvg'], input=dot, stdout=subprocess.PIPE, check=True)
self.map_svg = p.stdout
self.updateMap()
# only update the tree if it's showing...
if self.builder.get_object("treeWindow").get_visible():
dot = self.dungeon.generate_object_graph_dot().encode('utf-8')
p = subprocess.run(['neato', '-Tsvg'], input=dot, stdout=subprocess.PIPE, check=True)
self.tree_svg = p.stdout
self.updateTree()
else:
self.tree_svg = None
# update the panels.
current_room = self.dungeon.state['current_room']
panels = [
[self.detailsPanel, [current_room], False],
[self.exitsPanel, list(x for x in current_room.contents if x.is_a('Door')), True],
[self.monstersPanel, list(x for x in current_room.contents if x.is_a('Monster')), True]
]
for panel, contents, use_frame in panels:
for c in panel.get_children():
panel.remove(c)
for c in contents:
ow = ObjectWidget(self.dungeon, c)
ow.connect('update_data', self.onUpdateData)
ow.connect('update_time', self.onUpdateTime)
ow.show()
if use_frame:
flag = " \u2714 " if c.is_a('Door') and c.visited else ""
frame = frame_wrap(ow, f"{c.class_label()} {c.id}{flag}")
panel.add(frame)
else:
panel.add(ow)
# update other information
self.roomLabel.set_label(f"{current_room.class_label()} {current_room.id}")
count = 0
visited = 0
for o in self.dungeon.objects.values():
if o.is_a('Room'):
count += 1
if o.visited:
visited += 1
self.visitedLabel.set_label(f"Rooms visited: {visited}/{count}")
self.xpLabel.set_label(f"XP Earned: {self.dungeon.state['xp_earned']}")
def onUpdateTime(self, caller, time):
state = self.dungeon.state
state['current_time'] += time
total = state['current_time']
days = total // (24 * 60)
total -= days * (24 * 60)
hours = total // 60
mins = total - hours * 60
if days:
t = f"{days}+{hours:02d}:{mins:02d}"
else:
t = f"{hours:02d}:{mins:02d}"
self.dungeonTime.set_label(t)
# handle wandering monsters
total = state['current_time']
if 'next_wandering_monster' not in state:
state['next_wandering_monster'] = total + 10 + random.randint(1, 30)
if total > state['next_wandering_monster']:
# we're due for a monster, maybe.
logger.debug("check for wandering monster")
if random.randint(0, 100) <= self.dungeon.parameters['wandering_monsters']['percent_chance']:
logger.debug(f"Monster should appear")
# see if there are any open doors in this room. If not, no monster!
for c in self.dungeon.state['current_room'].contents:
if c.is_a('Door') and c.is_open:
monster = random.choice(self.dungeon.wm.contents).clone()
self.dungeon.add_object(monster)
state['current_room'].store(monster)
logger.debug(f"Wandering monster {monster.id} encountered")
caller.emit('update_data')
break
state['next_wandering_monster'] = total + 10 + random.randint(1, 30)
logger.debug(f"Next wandering monster after {state['next_wandering_monster']} ({state['current_time']})")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("dungeon", metavar="<dungeon>", nargs='?', default=None, help="Load a dungeon at start")
parser.add_argument("--debug", default=False, action='store_true', help="Turn on debugging")
args = parser.parse_args()
logger.setLevel(logging.DEBUG if args.debug else logging.INFO)
log_handler =logging.StreamHandler(sys.stderr)
log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log_handler.setFormatter(log_formatter)
logger.addHandler(log_handler)
app = PlayDungeon()
if args.dungeon is not None:
app.load_dungeon(args.dungeon)
Gtk.main()
if __name__ == "__main__":
main()