-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·193 lines (167 loc) · 5.39 KB
/
main.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
#!/usr/bin/env python
import os
import sys
import urwid
import argparse
import random
import yaml
from runes import architecture
from runes import creature
from runes import architecture
# Detect OS and select appropriate data directory
if sys.platform.startswith('linux') or sys.platform == 'cygwin':
data_dir = os.path.join(os.environ['HOME'], '.runes')
elif sys.platform.startswith('win'):
data_dir = os.path.join(os.environ['APPDATA'], 'runes')
elif sys.platform == 'darwin':
data_dir = os.path.join(os.environ['HOME'], 'Library', 'runes')
# Create data directory if it does not exist
if not os.path.exists(data_dir):
os.mkdir(data_dir)
# Read config file if it exists
config_path = os.path.join(data_dir, 'runes.conf')
if os.path.exists(config_path):
with open(config_path) as config_file:
config = yaml.safe_load(config_file)
else:
config = {
'numpad' : False
}
# Default config goes here.
save_path = os.path.join(data_dir, 'runes.save')
if os.path.exists(save_path):
with open(save_path) as save_file:
loaded_game = yaml.safe_load(save_file)
random.setstate(loaded_game[state])
# TODO: More game state here.
os.remove(save_path)
parser = argparse.ArgumentParser(prog='RUNES', description='Reality '
'Undermining Magical Exploration Simulation\n\n'
'RUNES is a roguelike with a complex and flexible magic system.')
parser.add_argument('-d', '--debug', action='store_true',
dest='debug', help='Print debugging messages')
parser.add_argument('-v', '--verbose', action='store_true',
dest='verbose', help='Print informational messages')
args = parser.parse_args()
def output_filter(output, args):
if not args.debug:
output = [line for line in output if line[0] != 'debug']
if not args.verbose:
output = [line for line in output if line[0] != 'info']
return output
command = ''
do_function = {}
command_function = {}
# Standard actions in the form of do_*
def do_go(key):
global command
command = ''
output = []
output.extend(player.go(direction_keys[key]))
return output
do_function['go'] = do_go
def do_open(key):
global command
command = 'open'
output = []
return output
do_function['open'] = do_open
def do_close(key):
global command
command = 'close'
output = []
return output
do_function['close'] = do_close
def do_quit(key):
with open(config_path) as config_file:
yaml.dump(config, config_file)
raise urwid.ExitMainLoop()
do_function['quit'] = do_quit
def do_save(key):
game_state = {}
game_state['state'] = random.getstate()
with open(save_path, 'w') as save_file:
yaml.dump(game_state, save_file)
do_quit(key)
do_function['save'] = do_save
# Extended command functions in the form of command_*
def command_open(key):
global command
output = []
if key in direction_keys:
output.extend(player.open_door(direction_keys[key]))
command = ''
return output
command_function['open'] = command_open
def command_close(key):
global command
output = []
if key in direction_keys:
output.extend(player.close_door(direction_keys[key]))
command = ''
return output
command_function['close'] = command_close
# Set keyboard mapping
keymap = {}
keymap['o'] = 'open'
keymap['c'] = 'close'
keymap['Q'] = 'quit'
keymap['S'] = 'save'
# Set direction keys based on numpad config option
if config['numpad']:
direction_keys = {
'1': 'southwest',
'2': 'south',
'3': 'southeast',
'6': 'east',
'9': 'northeast',
'8': 'north',
'7': 'northwest',
'4': 'west'}
else:
direction_keys = {
'h': 'west',
'j': 'south',
'k': 'north',
'l': 'east',
'u': 'northeast',
'y': 'northwest',
'b': 'southwest',
'n': 'southeast'}
# Add direction keys to keymap
for key in direction_keys.keys():
keymap[key] = 'go'
def handle_keys(key):
global command
output = []
if command:
output.extend(command_function[command](key))
else:
output.extend(do_function[keymap[key]](key))
if output:
output = output_filter(output, args)
messages.body.contents.extend(map(urwid.Text, output))
messages.body.set_focus(messages.body.get_focus()[1] + len(output))
# Re-render map after turn
map_box.set_text(('map', active_map.render()))
if __name__ == '__main__':
# Initialize map and player
active_map = level.Map(architecture.Cell())
player = creature.Player(pos=active_map.cell(8, 8))
active_map.draw_shape(((10, 10), (10, 20), (20, 20), (20, 10)), closed=True)
active_map.draw_point((15, 20), type='door')
# Initialize layout
map_box = urwid.Text(('map', active_map.render()))
status_bar = urwid.Text(('status', 'Running...'))
div = urwid.Divider()
messages = urwid.ListBox([urwid.Text('Welcome to RUNES')])
pile = urwid.Pile([('pack', map_box), ('pack', status_bar), ('pack',
div), messages])
palette = [
('map', 'default', 'default'),
('player', 'bold', 'default'),
('debug', 'light gray', 'default'),
('info', 'dark gray', 'default'),
('warn', 'dark red', 'default'), ]
loop = urwid.MainLoop(pile, palette, unhandled_input=handle_keys)
loop.run()