-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patholdrunner.h
284 lines (250 loc) · 8.68 KB
/
oldrunner.h
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
/* $Id: oldrunner.h,v 1.2 2012/01/06 10:11:30 culot Exp $ */
/* vim: et ai sts=2 ts=2 sw=2:
* */
/*
* Copyright (c) 2010, 2012 Frederic Culot <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef OLDRUNNER_H
#define OLDRUNNER_H
#include <stdlib.h>
#include <stdio.h>
#include "config.h"
#include "compat.h"
#include "cfg.h"
/* Option to set auto-center mode on/off */
#define AUTO_CENTER_MODE 0
/* Game level screen properties. */
#define LEVEL_DEFAULT_WIDTH 26
#define LEVEL_DEFAULT_HEIGHT 16
#define LEVEL_MAX_WIDTH 128
#define LEVEL_MAX_HEIGHT 128
#define VIEWPORT_WIDTH 80
#define VIEWPORT_HEIGHT 24
/* Possible characters states. */
#define STATE_ALIVE 0x01
#define STATE_FALLING 0x02
#define STATE_DIGGING 0x04
#define STATE_TRAPPED 0x08
/* Game sprites. */
enum sprite {
/* Sprites used to draw a level. */
SP_NONE,
SP_BRICK,
SP_BRICK_SCRACK,
SP_BRICK_LCRACK,
SP_BRICK_BROKEN,
SP_CIMENT,
SP_FAKE_BRICK,
SP_LADDER,
SP_ROPE,
SP_ESCAPE_LADDER,
/* Sprites used for objects. */
SP_MONEY,
/* Sprites used to draw characters. */
SP_HERO,
SP_FOE,
SP_INVALID,
SPRITES
};
enum move {
MOV_UP,
MOV_DOWN,
MOV_LEFT,
MOV_RIGHT,
MOV_FALL,
MOV_NONE,
MOVES
};
struct size {
unsigned h; /* height */
unsigned w; /* width */
};
struct coord {
int y; /* Position on the vertical axis. */
int x; /* Position on the horizontal axis. */
};
struct timer {
double time; /* time in usec. */
double elapsed;
};
/* Structure defining all possible user inputs. */
struct usr_input {
int key;
};
struct game_info {
#define NO_CHANGE 0x0
#define LEVEL_CHANGE 0x1
#define SCORE_CHANGE 0x2
#define LIVES_CHANGE 0x4
int state;
unsigned level;
unsigned score;
unsigned lives;
};
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define __FILE_POS__ __FILE__ ":" TOSTRING(__LINE__)
#define ERROR_MSG(...) do { \
char msg[BUFSIZ]; \
int len; \
\
len = snprintf (msg, BUFSIZ, "%s: %d: ", __FILE__, __LINE__); \
(void)snprintf (msg + len, BUFSIZ - len, __VA_ARGS__); \
(void)fprintf (stderr, "%s\n", msg); \
} while (0)
#define EXIT(...) do { \
ERROR_MSG(__VA_ARGS__); \
exit (1); \
} while (0)
#define EXIT_IF(cond, ...) do { \
if ((cond)) \
EXIT(__VA_ARGS__); \
} while (0)
/* bricks.c */
#define BRICK_COMEBACK_TIME (OLDRUNNER_TIMEOUT * 80)
void bricks_init (void);
void bricks_free (void);
void bricks_break (const struct coord *);
void bricks_draw (void);
void bricks_update (void);
unsigned bricks_broken_at (const struct coord *);
/* coord.c */
void coord_copy (const struct coord *, struct coord *);
void coord_below (const struct coord *, struct coord *);
void coord_above (const struct coord *, struct coord *);
enum move coord_opposite_dir (enum move);
void coord_compute (const struct coord *, enum move, struct coord *);
void coord_set_yx (struct coord *, int, int);
unsigned coord_equal (const struct coord *, const struct coord *);
void coord_diff (const struct coord *, const struct coord *,
struct coord *);
/* foes.c */
void foes_init (void);
void foes_free (void);
void foes_add (const struct coord *);
void foes_draw (void);
unsigned foes_at_pos (const struct coord *);
void foes_update_pos (void);
unsigned foes_wallup_at (const struct coord *);
/* game.c */
void game_won (void);
void game_init (int);
void game_end (void);
void game_update (void);
unsigned game_load (const char *);
void game_score_inc (void);
int game_level_num (void);
void game_level_inc (void);
void game_level_dec (void);
void game_lives_inc (void);
void game_lives_dec (void);
/* gfx.c */
void gfx_init (void);
void gfx_end (void);
void gfx_alert (void);
void gfx_center_at (struct coord *);
void gfx_update (void);
void gfx_get_input (struct usr_input *);
void gfx_show_sprite (enum sprite, const struct coord *);
void gfx_move_sprite (enum sprite, const struct coord *, const struct coord *);
void gfx_update_info (const struct game_info *);
void gfx_popup (const char *, const char *);
void gfx_game_over (void);
void gfx_game_won (void);
/* hero.c */
void hero_init (void);
void hero_draw (void);
void hero_set_pos (const struct coord *);
void hero_set_initpos (const struct coord *);
void hero_get_pos (struct coord *);
void hero_get_initpos (struct coord *);
unsigned hero_at_pos (const struct coord *);
void hero_move (enum move);
void hero_dig (enum move);
void hero_dig_done (void);
void hero_trapped (void);
void hero_die (void);
unsigned hero_wallup_at (const struct coord *);
/* io.c */
FILE *io_fopen (const char *);
void io_fclose (FILE *);
char *io_getln (FILE *);
/* lvl.c */
void lvl_started (void);
enum sprite lvl_decor_at_pos (const struct coord *);
void lvl_init (void);
unsigned lvl_load (int);
unsigned lvl_load_next (void);
unsigned lvl_load_prev (void);
void lvl_won (void);
void lvl_lost (void);
void lvl_draw_escape_ladder (void);
unsigned lvl_set_name (const char *);
unsigned lvl_set_author (const char *);
unsigned lvl_set_size (char *);
unsigned lvl_set_row (int, int, const char *);
unsigned lvl_width (void);
unsigned lvl_height (void);
unsigned lvl_add_new (void);
unsigned lvl_valid_move (const struct coord *, enum move, struct coord *,
enum sprite);
unsigned lvl_valid_dig (const struct coord *);
unsigned lvl_nothing_below (const struct coord *);
unsigned lvl_obstacle_at (const struct coord *);
void lvl_objects_update (void);
unsigned lvl_got_hole_below (const struct coord *);
enum move lvl_shortest_way (const struct coord *, enum move, enum move);
int lvl_random_xpos (void);
enum move lvl_random_hdir (void);
/* mem.c */
void *xmalloc (size_t);
void *xcalloc (size_t, size_t);
void *xrealloc (void *, size_t, size_t);
char *xstrdup (const char *);
void xfree (void *);
/* money.c */
void money_init (void);
void money_free (void);
void money_add (const struct coord *);
void money_draw (void);
void money_check_at (const struct coord *);
unsigned money_all_collected (void);
/* timer.c */
void timer_get_time (struct timer *);
void timer_start (struct timer *);
void timer_stop (struct timer *);
double timer_diff (const struct timer *, const struct timer *);
double timer_elapsed (const struct timer *);
void timer_set (const struct timer *, struct timer *);
void timer_add (struct timer *, double);
unsigned timer_delay_elapsed (struct timer *, double);
/* usr.c */
void usr_input (void);
void usr_reset_input (void);
#endif /* OLDRUNNER_H */