-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
295 lines (258 loc) · 8.81 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
/* Helper macros */
/* Consts/enums */
#define WIDTH 80
#define HEIGHT 24
#define LAST_LINE_STR "25"
#define MAP_SIZE (WIDTH*HEIGHT)
#define NOT_WALKABLE 0x0
#define FLOOR 0x1
#define CORRIDOR 0x2
#define NOT_WALKABLE_CHAR '-'
#define FLOOR_CHAR '.'
#define CORRIDOR_CHAR '/'
#define STAIRS_UP_CHAR '>'
#define STAIRS_DOWN_CHAR '<'
#define PLAYER_CHAR 'P'
#define AMULET_CHAR '*'
#define ENEMY_CHAR 'x'
#define STATE_PLAY 0
#define STATE_WIN 1
#define STATE_LOSS 2
/* Typedefs/structs */
typedef unsigned char Floor;
typedef char bool;
typedef char Map[MAP_SIZE];
typedef struct {
char x;
char y;
} Point;
#define FLOORS 4
#define ENEMIES 1
/* Globals */
Map current_map;
Point player_pos;
Point old_player_pos;
Point amulet_pos[FLOORS + 1];
Point enemies_pos[FLOORS + 1][ENEMIES];
Point stairs_pos[FLOORS + 1];
/* NOTE: we are limited to 255 floors */
Floor cur_floor;
Floor old_floor;
bool has_amulet;
/* Look-up tables and similar */
const char symbol_lut[] = {
NOT_WALKABLE_CHAR,
FLOOR_CHAR,
CORRIDOR_CHAR,
};
const char amulet_char_lut[] = {
AMULET_CHAR,
FLOOR_CHAR
};
/* NOTE: this function will disappear as soon as we start to generate a map */
char state_lut(const char c) {
switch(c) {
case NOT_WALKABLE_CHAR: return NOT_WALKABLE;
case FLOOR_CHAR: return FLOOR;
case CORRIDOR_CHAR: return CORRIDOR;
default: return NOT_WALKABLE; /* Should never happen */
}
}
/* Functions and macro-as-functions */
#define clear() printf("\033[H\033[J")
#define print_to_coordinates(pos, c) do { \
printf("\033[%d;%dH%c", (pos).y+1, (pos).x+1, c); \
printf("\033[" LAST_LINE_STR ";1H"); \
fflush(stdout); \
} while(0)
#define map_at(map, pos) ((map)[(pos).x + WIDTH * (pos).y])
void update_current_map() {
int e;
print_to_coordinates(old_player_pos, symbol_lut[map_at(current_map, old_player_pos)]);
/* Has to be done after enemies are cleared to avoid overwriting */
for (e = 0; e < ENEMIES; e++) {
print_to_coordinates(enemies_pos[cur_floor][e], ENEMY_CHAR);
}
print_to_coordinates(amulet_pos[cur_floor], amulet_char_lut[has_amulet]);
print_to_coordinates(stairs_pos[cur_floor - 1], STAIRS_UP_CHAR);
print_to_coordinates(stairs_pos[cur_floor], STAIRS_DOWN_CHAR);
print_to_coordinates(player_pos, PLAYER_CHAR);
}
void generate_new_map() {
int i = 0;
char map[MAP_SIZE] =
"--------------------------------------------------------------------------------"
"--------------------------------------------------------------------------------"
"--------------------------------------------------------------------------------"
"--------------------------------------------------------------------------------"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................//////////////////////////////////////.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"----.................--------------------------------------.................----"
"--------------------------------------------------------------------------------"
"--------------------------------------------------------------------------------"
"--------------------------------------------------------------------------------"
"--------------------------------------------------------------------------------";
clear();
while (i < MAP_SIZE) {
current_map[i] = state_lut(map[i]);
putchar(symbol_lut[current_map[i++]]);
if(i % WIDTH == 0) putchar('\n');
}
update_current_map();
}
#define on_stairs_up(player_pos, stairs_pos, floor) player_pos.x == stairs_pos[floor - 1].x && player_pos.y == stairs_pos[floor - 1].y
#define on_stairs_down(player_pos, stairs_pos, floor) player_pos.x == stairs_pos[floor].x && player_pos.y == stairs_pos[floor].y
#define move(input_ch) do {\
old_floor = cur_floor;\
old_player_pos = player_pos;\
switch((input_ch)) {\
case 'w': player_pos.y--; break;\
case 's': player_pos.y++; break;\
case 'a': player_pos.x--; break;\
case 'd': player_pos.x++; break;\
case 'c': cur_floor -= on_stairs_up(player_pos, stairs_pos, cur_floor); break;\
case 'v': cur_floor += on_stairs_down(player_pos, stairs_pos, cur_floor); break;\
}\
if(NOT_WALKABLE == map_at(current_map, player_pos)\
|| player_pos.x < 0\
|| player_pos.y < 0\
|| player_pos.x > WIDTH\
|| player_pos.y > HEIGHT) {\
player_pos = old_player_pos;\
}\
if (cur_floor > FLOORS - 1) {\
cur_floor = FLOORS - 1;\
}\
if (cur_floor <= 0) {\
cur_floor = 1;\
}\
} while(0)
#define same_pos(pos1, pos2) ((pos1).x == (pos2).x && (pos1).y == (pos2).y)
#define FSM_STATES() \
FSM_STATE_MACRO(START) \
FSM_STATE_MACRO(NEW_FLOOR) \
FSM_STATE_MACRO(ON_FLOOR) \
FSM_STATE_MACRO(BATTLE) \
FSM_STATE_MACRO(WIN) \
FSM_STATE_MACRO(LOSE)
/************************************************/
/***** PROTECTED FSM SECTION, DO NOT MODIFY *****/
#define FSM_FUN_NAME(X) fsm_state_fun_ ## X
#define FSM_STATE_NAME(X) FSM_STATE_ ## X
#define FSM_LOOP() do {\
State fsm_state = FSM_STATE_NAME(START);\
while((fsm_state = fsm_state_table[fsm_state]()) != FSM_STATE_NAME(END));\
} while(0)
#define FSM_STATE_MACRO(X) FSM_STATE_NAME(X),
typedef enum {
FSM_STATE_END = -1,
FSM_STATES()
FSM_STATE_CNT
} State;
#undef FSM_STATE_MACRO
#define FSM_STATE_MACRO(X) State FSM_FUN_NAME(X) (void);
FSM_STATES()
#undef FSM_STATE_MACRO
#define FSM_STATE_MACRO(X) FSM_FUN_NAME(X),
State(*fsm_state_table[FSM_STATE_CNT])(void) = {
FSM_STATES()
};
#undef FSM_STATE_MACRO
/********* END OF PROTECTED FSM SECTION *********/
/************************************************/
State FSM_FUN_NAME(START)(void) {
int i;
/* Game preamble */
player_pos.x = 5;
player_pos.y = 5;
/* Highest floor has no stairs up */
stairs_pos[0].x = -2;
stairs_pos[0].y = -2;
stairs_pos[1].x = 62;
stairs_pos[1].y = 11;
stairs_pos[2].x = 72;
stairs_pos[2].y = 15;
/* Lowest floor has no stairs down */
stairs_pos[3].x = -2;
stairs_pos[3].y = -2;
/* Enemies */
enemies_pos[1][0].x = 59;
enemies_pos[1][0].y = 8;
enemies_pos[2][0].x = 60;
enemies_pos[2][0].y = 7;
enemies_pos[3][0].x = 61;
enemies_pos[3][0].y = 6;
enemies_pos[4][0].x = 59;
enemies_pos[4][0].y = 5;
/* Ah, the things you do not to use ifs */
for (i = 1; i < FLOORS; i++) {
amulet_pos[i].x = -2;
amulet_pos[i].y = -2;
}
amulet_pos[FLOORS - 1].x = 72;
amulet_pos[FLOORS - 1].y = 18;
cur_floor = 1;
has_amulet = 0;
return FSM_STATE_NAME(NEW_FLOOR);
}
State FSM_FUN_NAME(NEW_FLOOR)(void) {
generate_new_map();
printf("Level -%03d\n%s\n", cur_floor, has_amulet ? "You have the amulet!" : "Find the amulet!");
return (cur_floor == 1 && has_amulet) ? FSM_STATE_NAME(WIN) : FSM_STATE_NAME(ON_FLOOR);
}
State FSM_FUN_NAME(ON_FLOOR)(void) {
int i;
move(getchar());
update_current_map();
for (i = 0; i < ENEMIES; i++) {
/* XXX: can we avoid this if? */
if(same_pos(player_pos, enemies_pos[cur_floor][i])) {
return FSM_STATE_NAME(BATTLE);
}
}
has_amulet = same_pos(player_pos, amulet_pos[cur_floor]) ? 1 : has_amulet;
printf("Level -%03d\n%s\n", cur_floor, has_amulet ? "You have the amulet!" : "Find the amulet!");
return (cur_floor == old_floor) ? FSM_STATE_NAME(ON_FLOOR) : FSM_STATE_NAME(NEW_FLOOR);
}
State FSM_FUN_NAME(BATTLE)(void) {
return FSM_STATE_NAME(LOSE);
}
State FSM_FUN_NAME(WIN)(void) {
puts("\033[DYou have got the amulet! - GAME OVER\n");
return FSM_STATE_NAME(END);
}
State FSM_FUN_NAME(LOSE)(void) {
puts("\033[DYou died! - GAME OVER\n");
return FSM_STATE_NAME(END);
}
int main() {
/* Terminal stuff*/
static struct termios oldt, newt;
/* Write the attributes of stdin(STDIN_FILENO) to oldt */
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
/* Disables "wait for '\n' or EOF" mode */
newt.c_lflag &= ~(ICANON);
tcsetattr( STDIN_FILENO, TCSANOW, &newt);
FSM_LOOP();
/* Restore old attributes */
tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
return 0;
}