Mercurial > hg > early-roguelike
comparison urogue/state.c @ 256:c495a4f288c6
Import UltraRogue from the Roguelike Restoration Project (r1490)
author | John "Elwin" Edwards |
---|---|
date | Tue, 31 Jan 2017 19:56:04 -0500 |
parents | |
children | 2908dc47f9e2 |
comparison
equal
deleted
inserted
replaced
253:d9badb9c0179 | 256:c495a4f288c6 |
---|---|
1 /* | |
2 state.c - Portable Rogue Save State Code | |
3 | |
4 Copyright (C) 1993, 1995 Nicholas J. Kisseberth | |
5 | |
6 Redistribution and use in source and binary forms, with or without | |
7 modification, are permitted provided that the following conditions | |
8 are met: | |
9 1. Redistributions of source code must retain the above copyright | |
10 notice, this list of conditions and the following disclaimer. | |
11 2. Redistributions in binary form must reproduce the above copyright | |
12 notice, this list of conditions and the following disclaimer in the | |
13 documentation and/or other materials provided with the distribution. | |
14 3. Neither the name(s) of the author(s) nor the names of other contributors | |
15 may be used to endorse or promote products derived from this software | |
16 without specific prior written permission. | |
17 | |
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND | |
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE | |
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
27 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
28 SUCH DAMAGE. | |
29 */ | |
30 | |
31 /* | |
32 Notes | |
33 | |
34 Should move all game variables into one place | |
35 Should move save/restore code into save.c or some such | |
36 */ | |
37 | |
38 #include <assert.h> | |
39 #include <stdlib.h> | |
40 #include <string.h> | |
41 #include "rogue.h" | |
42 | |
43 /* | |
44 Variables for global game state. | |
45 | |
46 All variables that need to get saved when saving a game | |
47 are defined in this file. Long term goal is to move many | |
48 of these variables into a "struct level" data type of some | |
49 kind... perhaps not, maybe struct game... | |
50 | |
51 Other global variables that don't need to get saved are | |
52 kept in main.c. | |
53 | |
54 Other global variables that don't change during the course | |
55 of a game are kept in urogue.c, monsdata.c, data.c. | |
56 */ | |
57 | |
58 #define _X_ { 0, 0, 0, 0, 0 } | |
59 | |
60 struct delayed_action | |
61 d_list[MAXDAEMONS] = /* daemon/fuse list */ | |
62 { | |
63 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, | |
64 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, | |
65 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, | |
66 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, | |
67 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, | |
68 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, | |
69 }; | |
70 | |
71 #undef _X_ | |
72 | |
73 char *s_names[MAXSCROLLS]; /* Names of the scrolls */ | |
74 char *p_colors[MAXPOTIONS]; /* Colors of the potions */ | |
75 char *r_stones[MAXRINGS]; /* Stone settings of the rings */ | |
76 char *ws_made[MAXSTICKS]; /* What sticks are made of */ | |
77 char *ws_type[MAXSTICKS]; /* Is it a wand or a staff */ | |
78 | |
79 char *guess_items[MAXMAGICTYPES][MAXMAGICITEMS]; /* guess magic is */ | |
80 int know_items[MAXMAGICTYPES][MAXMAGICITEMS]; /* flag knowlede of magic */ | |
81 /* object data */ | |
82 | |
83 struct trap traps[2 * MAXTRAPS]; /* 2x for special effects */ | |
84 struct room rooms[MAXROOMS]; /* One for each room -- A level */ | |
85 struct room *oldrp = NULL; /* Roomin(&player.t_oldpos) */ | |
86 struct thing player; /* The rogue */ | |
87 struct linked_list *lvl_obj = NULL; /* Treasure on this level */ | |
88 struct linked_list *fam_ptr = NULL; /* A ptr to the familiar */ | |
89 struct linked_list *mlist = NULL; /* Monsters on this level */ | |
90 struct thing *beast; /* The last beast that attacked */ | |
91 struct object *cur_armor = NULL; /* what rogue wears */ | |
92 struct object *cur_weapon = NULL; /* ... and wields */ | |
93 struct object *cur_ring[10]; /* His rings */ | |
94 struct linked_list *curr_mons = NULL; /* The mons. currently moving */ | |
95 struct linked_list *next_mons = NULL; /* The mons. after curr_mons */ | |
96 | |
97 /* Misc. game state info */ | |
98 char dummybuf1[50000]; | |
99 char dummybuf2[50000]; | |
100 char msgbuf[10][2*LINELEN]; /* message buffer history */ | |
101 int msg_index = 0; /* index in msg history buffer for nxt msg */ | |
102 int foodlev = 1; /* how fast he eats food */ | |
103 int ntraps = 0; /* Number of traps on this level */ | |
104 int dnum = 0; /* Dungeon number */ | |
105 int max_level = 0; /* Deepest player has gone */ | |
106 int lost_dext = 0; /* amount of lost dexterity */ | |
107 int no_command = 0; | |
108 int level = 0; | |
109 int see_dist = 3; | |
110 int no_food = 0; | |
111 int count = 0; | |
112 int food_left = HUNGERTIME; | |
113 int group = 1; | |
114 int hungry_state = F_OK; | |
115 int infest_dam = 0; | |
116 int lost_str = 0; | |
117 int hold_count = 0; | |
118 int trap_tries = 0; | |
119 int has_artifact = 0; | |
120 int picked_artifact = 0; | |
121 int luck = 0; | |
122 int resurrect = 0; | |
123 int fam_type = 0; /* The type of familiar */ | |
124 int mons_summoned = 0; /* Number of summoned monsters */ | |
125 char PLAYER = VPLAYER; /* what the player looks like */ | |
126 char take = 0; /* Thing the rogue is taking */ | |
127 char runch = 0; /* Direction player is running */ | |
128 int char_type = C_NOTSET; /* what type of character is player */ | |
129 int inv_type = INV_CLEAR; /* Overwrite style of inventory */ | |
130 int pool_teleport = FALSE; /* just teleported from a pool */ | |
131 int inwhgt = FALSE; /* true if from wghtchk() */ | |
132 int after = 0; /* True if we want after daemons */ | |
133 int waswizard = 0; /* Was a wizard sometime */ | |
134 int canwizard = 1; /* Will be permitted to do this */ | |
135 int playing = TRUE; | |
136 int running = FALSE; | |
137 int fighting = FALSE; | |
138 int wizard = FALSE; | |
139 int wiz_verbose = TRUE; | |
140 int moving = FALSE; | |
141 coord delta; /* Change indicated to get_dir() */ | |
142 LEVTYPE levtype; /* type of level i'm on */ | |
143 long purse = 0; | |
144 unsigned long total = 0; | |
145 | |
146 WINDOW *cw; /* Window that the player sees */ | |
147 WINDOW *hw; /* Used for the help command */ | |
148 WINDOW *mw; /* Used to store mosnters */ | |
149 | |
150 /* options.o */ | |
151 /* game options */ | |
152 | |
153 int terse = FALSE; | |
154 int door_stop = FALSE; | |
155 int jump = TRUE; | |
156 int doorstop = TRUE; | |
157 int firstmove = FALSE; | |
158 int askme = FALSE; | |
159 char whoami[2 * LINELEN]; /* Name of player */ | |
160 char fruit[2 * LINELEN]; /* Favorite fruit */ | |
161 char file_name[2 * LINELEN]; /* Save file name */ | |
162 char score_file[2 * LINELEN]; /* Score file name */ | |
163 | |
164 /****************************************************************************/ | |
165 /* Portable Save State Code */ | |
166 /* */ | |
167 /* UltraRogue v1.04 */ | |
168 /****************************************************************************/ | |
169 | |
170 #define URS_STATS 0xABCD0001 | |
171 #define URS_THING 0xABCD0002 | |
172 #define URS_OBJECT 0xABCD0003 | |
173 #define URS_MAGIC 0xABCD0004 | |
174 #define URS_KNOWS 0xABCD0005 | |
175 #define URS_GUESSES 0xABCD0006 | |
176 #define URS_STACKOBJECT 0xABCD0007 | |
177 #define URS_BAGOBJECT 0xABCD0008 | |
178 #define URS_MONSTERLIST 0xABCD0009 | |
179 #define URS_MONSTERSTATS 0xABCD000A | |
180 #define URS_MONSTER 0xABCD000B | |
181 #define URS_TRAP 0xABCD000C | |
182 #define URS_WINDOW 0xABCD000D | |
183 #define URS_DAEMONS 0xABCD000E | |
184 | |
185 void | |
186 ur_write(FILE *savef, void *ptr, size_t size) | |
187 { | |
188 if (size == 0) | |
189 return; | |
190 | |
191 fwrite(ptr,size,1,savef); | |
192 } | |
193 | |
194 void | |
195 ur_read(FILE *savef, void *ptr, size_t size) | |
196 { | |
197 if (size == 0) | |
198 return; | |
199 | |
200 fread(ptr,size,1,savef); | |
201 } | |
202 | |
203 void | |
204 ur_write_int(FILE *savef, int c) | |
205 { | |
206 ur_write(savef,&c,sizeof(int)); | |
207 } | |
208 | |
209 int | |
210 ur_read_int(FILE *savef) | |
211 { | |
212 int i; | |
213 | |
214 ur_read(savef, &i, sizeof(int)); | |
215 | |
216 return(i); | |
217 } | |
218 | |
219 void | |
220 ur_write_short(FILE *savef, short c) | |
221 { | |
222 ur_write(savef,&c,sizeof(short)); | |
223 } | |
224 | |
225 short | |
226 ur_read_short(FILE *savef) | |
227 { | |
228 short s; | |
229 | |
230 ur_read(savef, &s, sizeof(short)); | |
231 | |
232 return(s); | |
233 } | |
234 | |
235 void | |
236 ur_write_long(FILE *savef, long c) | |
237 { | |
238 ur_write(savef,&c,sizeof(long)); | |
239 } | |
240 | |
241 long | |
242 ur_read_long(FILE *savef) | |
243 { | |
244 long l; | |
245 | |
246 ur_read(savef, &l, sizeof(long)); | |
247 | |
248 return(l); | |
249 } | |
250 | |
251 void | |
252 ur_write_ulong(FILE *savef, unsigned long c) | |
253 { | |
254 ur_write(savef,&c,sizeof(unsigned long)); | |
255 } | |
256 | |
257 unsigned long | |
258 ur_read_ulong(FILE *savef) | |
259 { | |
260 long l; | |
261 | |
262 ur_read(savef, &l, sizeof(unsigned long)); | |
263 | |
264 return(l); | |
265 } | |
266 | |
267 void | |
268 ur_unread_long(FILE *savef) | |
269 { | |
270 fseek(savef, -(long)sizeof(long), SEEK_CUR); | |
271 } | |
272 | |
273 void | |
274 ur_write_char(FILE *savef, char c) | |
275 { | |
276 ur_write(savef,&c,sizeof(char)); | |
277 } | |
278 | |
279 char | |
280 ur_read_char(FILE *savef) | |
281 { | |
282 char c; | |
283 | |
284 ur_read(savef, &c, sizeof(char)); | |
285 | |
286 return(c); | |
287 } | |
288 | |
289 void | |
290 ur_write_string(FILE *savef, char *s) | |
291 { | |
292 size_t len; | |
293 | |
294 len = (s == NULL) ? 0L : strlen(s) + 1 ; | |
295 | |
296 ur_write_long(savef, (long) len); | |
297 ur_write(savef,s,len); | |
298 } | |
299 | |
300 | |
301 char * | |
302 ur_read_string(FILE *savef) | |
303 { | |
304 size_t len; | |
305 char *buf; | |
306 | |
307 len = ur_read_long(savef); | |
308 | |
309 if (len == 0) | |
310 return(NULL); | |
311 | |
312 buf = ur_alloc(len); | |
313 | |
314 if (buf == NULL) /* Should flag a global error condition... */ | |
315 return(NULL); | |
316 | |
317 ur_read(savef,buf,len); | |
318 | |
319 return(buf); | |
320 } | |
321 | |
322 void | |
323 ur_write_coord(FILE *savef, coord c) | |
324 { | |
325 ur_write_int(savef, c.x); | |
326 ur_write_int(savef, c.y); | |
327 } | |
328 | |
329 coord | |
330 ur_read_coord(FILE *savef) | |
331 { | |
332 coord c; | |
333 | |
334 c.x = ur_read_int(savef); | |
335 c.y = ur_read_int(savef); | |
336 | |
337 return(c); | |
338 } | |
339 | |
340 void | |
341 ur_write_room(FILE *savef, struct room *r) | |
342 { | |
343 int i; | |
344 | |
345 ur_write_coord(savef, r->r_pos); | |
346 ur_write_coord(savef, r->r_max); | |
347 | |
348 for(i=0; i<MAXDOORS; i++) | |
349 ur_write_coord(savef, r->r_exit[i]); | |
350 | |
351 ur_write_int(savef, r->r_flags); | |
352 ur_write_int(savef, r->r_nexits); | |
353 ur_write_int(savef, r->r_flags); | |
354 } | |
355 | |
356 struct room * | |
357 ur_read_room(FILE *savef) | |
358 { | |
359 struct room *r; | |
360 int i; | |
361 | |
362 r = ur_alloc( sizeof(struct room) ); | |
363 | |
364 r->r_pos = ur_read_coord(savef); | |
365 r->r_max = ur_read_coord(savef); | |
366 | |
367 for(i=0; i<MAXDOORS; i++) | |