Repository navigation
Expand file tree
/
Copy pathIO.py
More file actions
227 lines (201 loc) · 8.23 KB
/
Copy pathIO.py
File metadata and controls
227 lines (201 loc) · 8.23 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
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
import libtcodpy as libtcod
import cPickle as pickle
import glob
import os
import shutil
import game
import util
# autosave game
def autosave(border=True):
# t0 = libtcod.sys_elapsed_seconds()
if not os.path.exists('saves/' + game.player.name.lower()):
os.makedirs('saves/' + game.player.name.lower())
f = open('saves/' + game.player.name.lower() + '/player.tmp', 'wb')
pickle.dump(game.player, f, game.PICKLE_PROTOCOL)
f.close()
f = open('saves/' + game.player.name.lower() + '/worldmap.tmp', 'wb')
pickle.dump(game.worldmap, f, game.PICKLE_PROTOCOL)
f.close()
f = open('saves/' + game.player.name.lower() + '/messages.tmp', 'wb')
pickle.dump(game.message, f, game.PICKLE_PROTOCOL)
f.close()
f = open('saves/' + game.player.name.lower() + '/stuff.tmp', 'wb')
pickle.dump(game.rnd, f, game.PICKLE_PROTOCOL)
pickle.dump(game.turns, f, game.PICKLE_PROTOCOL)
pickle.dump(game.gametime, f, game.PICKLE_PROTOCOL)
pickle.dump(game.fov_torch, f, game.PICKLE_PROTOCOL)
pickle.dump(game.game_state, f, game.PICKLE_PROTOCOL)
pickle.dump(game.times_saved, f, game.PICKLE_PROTOCOL)
f.close()
f = open('saves/' + game.player.name.lower() + '/map' + str(game.current_map.location_id) + '-' + str(game.current_map.location_level) + '.tmp', 'wb')
pickle.dump(game.current_map, f, game.PICKLE_PROTOCOL)
f.close()
# with gzip.GzipFile('saves/' + game.player.name.lower() + '/current_map.tmp', 'wb', 2) as f:
# pickle.dump(game.current_map, f, game.PICKLE_PROTOCOL)
if border:
for i in range(len(game.border_maps)):
f = open('saves/' + game.player.name.lower() + '/map' + str(game.border_maps[i].location_id) + '-' + str(game.border_maps[i].location_level) + '.tmp', 'wb')
pickle.dump(game.border_maps[i], f, game.PICKLE_PROTOCOL)
f.close()
# t1 = libtcod.sys_elapsed_seconds()
# print 'maps save done! (%.3f seconds)' % (t1 - t0)
# autosave current map
def autosave_current_map():
f = open('saves/' + game.player.name.lower() + '/current_map.tmp', 'wb')
pickle.dump(game.current_map, f, game.PICKLE_PROTOCOL)
f.close()
# delete save game and temp files
def delete_game():
if game.player.name.lower() + '.sav' in game.savefiles:
os.remove('saves/' + game.player.name.lower() + '.sav')
if os.path.exists('saves/' + game.player.name.lower()):
shutil.rmtree('saves/' + game.player.name.lower())
# fetch autosave if it exists to override save game data
def fetch_autosave():
if os.path.exists('saves/' + game.player.name.lower() + '/player.tmp'):
f = open('saves/' + game.player.name.lower() + '/player.tmp', 'rb')
game.player = pickle.load(f)
f.close()
if os.path.exists('saves/' + game.player.name.lower() + '/worldmap.tmp'):
f = open('saves/' + game.player.name.lower() + '/worldmap.tmp', 'rb')
game.worldmap = pickle.load(f)
f.close()
if os.path.exists('saves/' + game.player.name.lower() + '/messages.tmp'):
f = open('saves/' + game.player.name.lower() + '/messages.tmp', 'rb')
game.message = pickle.load(f)
f.close()
if os.path.exists('saves/' + game.player.name.lower() + '/stuff.tmp'):
f = open('saves/' + game.player.name.lower() + '/stuff.tmp', 'rb')
game.rnd = pickle.load(f)
game.turns = pickle.load(f)
game.gametime = pickle.load(f)
game.fov_torch = pickle.load(f)
game.game_state = pickle.load(f)
game.times_saved = pickle.load(f)
f.close()
oldmaps = glob.glob('saves/' + game.player.name.lower() + '/map*.tmp')
if os.path.exists('saves/' + game.player.name.lower() + '/current_map.tmp'):
f = open('saves/' + game.player.name.lower() + '/current_map.tmp', 'rb')
game.current_map = pickle.load(f)
f.close()
for i in range(len(oldmaps)):
f = open(oldmaps[i], 'rb')
temp_map = pickle.load(f)
util.store_map(temp_map)
f.close()
# load the game using the pickle module
def load_game():
if not game.savefiles:
contents = ['There are no saved games.']
game.messages.box('Saved games', None, 'center_screenx', 'center_screeny', len(max(contents, key=len)) + 16, len(contents) + 4, contents, input=False, align=libtcod.CENTER)
else:
desc = []
for i in range(len(game.savefiles)):
f = open('saves/' + game.savefiles[i], 'rb')
pl = pickle.load(f)
desc.append(pl.name + ', a level ' + str(pl.level) + ' ' + pl.gender + ' ' + pl.race + ' ' + pl.profession)
f.close()
choice = game.messages.box('Saved games', None, (game.SCREEN_WIDTH - (max(60, len(max(desc, key=len)) + 20))) / 2, ((game.SCREEN_HEIGHT + 1) - max(16, len(desc) + 4)) / 2, max(60, len(max(desc, key=len)) + 20), max(16, len(desc) + 4), desc, step=2, mouse_exit=True)
if choice != -1:
contents = ['Loading.....']
game.messages.box(None, None, 'center_screenx', 'center_screeny', len(max(contents, key=len)) + 16, len(contents) + 4, contents, input=False, align=libtcod.CENTER, nokeypress=True)
f = open('saves/' + game.savefiles[choice], 'rb')
game.player = pickle.load(f)
game.worldmap = pickle.load(f)
game.current_map = pickle.load(f)
game.old_maps = pickle.load(f)
game.message = pickle.load(f)
game.rnd = pickle.load(f)
game.turns = pickle.load(f)
game.gametime = pickle.load(f)
game.fov_torch = pickle.load(f)
game.game_state = pickle.load(f)
game.times_saved = pickle.load(f)
f.close()
fetch_autosave()
game.rnd = libtcod.random_new_from_seed(game.rnd)
game.char = game.current_map.objects[0]
game.worldmap.create_map_images(1)
game.message.empty()
game.message.trim_history()
game.message.new('Welcome back, ' + game.player.name + '!', game.turns, libtcod.Color(96, 212, 238))
if game.current_map.location_id == 0:
util.fetch_border_maps()
util.combine_maps()
game.current_map.check_player_position()
#print game.worldmap.dungeons
return True
return False
# load high scores
def load_high_scores():
if os.path.exists('highscores.dat'):
contents = open('highscores.dat', 'rb')
game.highscore = pickle.load(contents)
contents.close()
# load manual
def load_manual():
contents = open('data/help.txt', 'r').read()
contents = contents.split('\n')
return contents
# load game settings
# stuff to do: add more options
def load_settings():
if os.path.exists('settings.ini'):
contents = open('settings.ini', 'r')
while 1:
line = contents.readline().rstrip()
if line == '[Font]':
game.setting_font = contents.readline().rstrip()
if line == '[History]':
game.setting_history = int(contents.readline().rstrip())
if line == '[Fullscreen]':
game.setting_fullscreen = contents.readline().rstrip()
if not line:
break
contents.close()
if not any(game.setting_font == i for i in ['small', 'medium', 'large']):
game.setting_font = 'small'
if not game.setting_history in range(50, 1000):
game.setting_history = 50
if not any(game.setting_fullscreen == i for i in ['on', 'off']):
game.setting_fullscreen = 'off'
# save the game using the pickle module
def save_game(chargen=False):
if not chargen:
contents = ['Saving.....']
game.messages.box(None, None, 'center_screenx', 'center_screeny', len(max(contents, key=len)) + 28, len(contents) + 4, contents, input=False, align=libtcod.CENTER, nokeypress=True)
delete_game()
if game.current_map.location_id == 0:
if not chargen:
util.decombine_maps()
util.store_map(game.current_map)
for i in range(len(game.border_maps)):
util.store_map(game.border_maps[i])
f = open('saves/' + game.player.name.lower() + '.sav', 'wb')
pickle.dump(game.player, f, game.PICKLE_PROTOCOL)
pickle.dump(game.worldmap, f, game.PICKLE_PROTOCOL)
pickle.dump(game.current_map, f, game.PICKLE_PROTOCOL)
pickle.dump(game.old_maps, f, game.PICKLE_PROTOCOL)
pickle.dump(game.message, f, game.PICKLE_PROTOCOL)
pickle.dump(game.rnd, f, game.PICKLE_PROTOCOL)
pickle.dump(game.turns, f, game.PICKLE_PROTOCOL)
pickle.dump(game.gametime, f, game.PICKLE_PROTOCOL)
pickle.dump(game.fov_torch, f, game.PICKLE_PROTOCOL)
pickle.dump(game.game_state, f, game.PICKLE_PROTOCOL)
pickle.dump(game.times_saved + 1, f, game.PICKLE_PROTOCOL)
f.close()
# save changes to the highscores
def save_high_scores():
f = open('highscores.dat', 'wb')
pickle.dump(game.highscore, f, game.PICKLE_PROTOCOL)
f.close()
# save changes to the game settings
def save_settings(font, history, fs):
f = open('settings.ini', 'wb')
f.write('[Font]\n')
f.write(font + '\n')
f.write('[History]\n')
f.write(history + '\n')
f.write('[Fullscreen]\n')
f.write(fs + '\n')
f.close()