-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
executable file
·343 lines (304 loc) · 8.96 KB
/
game.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "lib/types.h"
#include "lib/loader.h"
struct room * cached_read_room(World *w, const char *path);
void world_add_room(World *w, struct room *room);
void showHelp() {
printf(
"================================================================================\n"
"Help\n"
"================================================================================\n"
" (l)ook\t\tinspect environment, or item\n"
" look me\tinformation on current player\n"
" (ex)amine\texamine surroundings\n"
" (u)p\t\tmove up\n"
" (d)own\t\tmove down\n"
" quit\t\texit the game\n"
" (h)elp\t\tshow this screen\n"
);
}
struct commandResult {
int exit;
int moved;
};
void print_room(struct room * current) {
printf("%s\n%s\n", current->name, current->desc);
struct connection *link = current->link;
printf("Exits: -\n");
while(link) {
printf(" %s ", link->direction);
link = link->next;
}
printf("\n");
}
int get_exit(char * filePath, struct room * current, const char * to) {
struct connection * head = current->link;
int found = 0;
do {
int isMatch = strcmp(head->direction, to)==0;
if(isMatch) {
strcpy(filePath, head->file);
found = 1;
break;
}
head=head->next;
} while(head && strlen(head->file)>0);
return found;
}
int has_exit(struct room * current, const char * to) {
struct connection * head = current->link;
int found = 0;
do {
int isMatch = strcmp(head->direction, to)==0;
if(isMatch){
found = 1;
break;
}
head=head->next;
} while(head && strlen(head->file)>0);
return found;
}
int find_alias(char * expanded, char * cmd_buffer, struct player * me) {
struct alias * head = me->aliases;
int found = 0;
do {
int isMatch = strcmp(head->src, cmd_buffer)==0;
if(isMatch) {
strcpy(expanded, head->dest);
found = 1;
break;
}
head=head->next;
} while(head!=NULL && strlen(head->src)>0);
return found;
}
void read_player(struct player * me) {
printf("Welcome to the Adventure.\n\nEnter your name: ");
fgets(me->name, 55, stdin);
char * ch = me->name;
while(*ch != '\0'){
ch++;
}
ch--;
if(*ch=='\n') {
*ch='\0';
}
time(&me->connectionTime);
}
void look_player(struct player * me) {
print_room(me->currentRoom);
}
struct commandResult execute_command(struct player * me, char * cmd_buffer, char * cmd_param) {
World *w = me->world;
struct commandResult result;
int ret = 0;
int moved = 0;
if(strlen(cmd_buffer) == 0) {
printf("What?\n");
}
else if(has_exit(me->currentRoom, cmd_buffer)) {
printf("You go: %s\n", cmd_buffer);
char room_file[30];
memset(&room_file, '\0', sizeof(char)*30);
get_exit(&room_file, me->currentRoom, cmd_buffer);
//free currentRoom..
struct room * nextRoom = cached_read_room(w, room_file);
if(nextRoom != NULL) {
// Also free links, items etc
//free(me->currentRoom);
me->currentRoom=NULL;
me->currentRoom = nextRoom;
moved = 1;
} else {
printf("You didn't move.\n");
}
}
else if(strcmp(cmd_buffer, "examine") == 0) {
if((me->currentRoom)->items == NULL) {
printf("You do not notice anything.\n");
} else {
printf("You notice:-\n");
struct item * itemPtr = (me->currentRoom)->items;
do {
printf("%s\n", itemPtr->name);
itemPtr = itemPtr->link;
} while(itemPtr != NULL);
}
}
else if(strcmp(cmd_buffer, "look") == 0) {
if(cmd_param != NULL && strlen(cmd_param) > 0) {
// printf("%s\n", cmd_param);
if(strcmp(cmd_param, "me") == 0) {
time_t now;
time(&now);
int seconds = (int)difftime(now, me->connectionTime);
printf(
"================================================================================\n"
"Player: %s\n"
"================================================================================\n"
"Current room: %s\n"
"Connected for: %d seconds\n",
me->name, (me->currentRoom)->name, seconds);
} else {
printf("You do not notice anything special about: '%s'.\n", cmd_param);
}
}
else {
look_player(me);
}
}
else if(strcmp(cmd_buffer, "mem") == 0) {
printf("World: %s\n\n", w->name);
LoadedRoom *head = w->rooms;
printf("Loaded rooms:\n");
do {
printf("- %s\n", (head->current)->fileName);
head = head->next;
} while(head != NULL);
// && head->next!=NULL);
}
else if(strcmp(cmd_buffer, "help") == 0) {
showHelp();
}
else if(strcmp(cmd_buffer, "quit")==0) {
ret = 1;
}
else {
char expanded[30];
if(find_alias(&expanded, cmd_buffer, me)) {
printf("Using alias %s.\n", expanded);
struct commandResult childResult = execute_command(me, &expanded, cmd_param);
ret = childResult.exit;
moved = childResult.moved;
} else {
printf("I do not understand: '%s'.. ?\n", cmd_buffer);
}
}
result.exit = ret;
result.moved = moved;
return result;
}
int parse_parameter(char* cmd_buffer, char* cmd_param) {
int parsedParam = 0;
char * spacep = strstr(cmd_buffer, " ");
char * cmd_buffer_p = cmd_buffer;
if(spacep) {
while(cmd_buffer_p != spacep) {
cmd_buffer_p++;
}
// 'look me' -> 'look'
*cmd_buffer_p='\0';
spacep++;
// ' me'
strcpy(cmd_param, spacep);
parsedParam = 1;
}
}
void loop_player(struct player * me) {
char cmd_buffer[80];
char cmd_param[80];
while(1) {
printf(PROMPT);
memset(&cmd_buffer, '\0', sizeof(char)*80);
memset(&cmd_param, '\0', sizeof(char)*80);
if(fgets(cmd_buffer, 80, stdin)) {
trimEnd(&cmd_buffer, '\n');
int parsed = parse_parameter(&cmd_buffer, &cmd_param);
struct commandResult result = execute_command(me, (char*)&cmd_buffer, cmd_param);
if(result.moved) {
memset(&cmd_param, '\0', sizeof(char)*80);
result = execute_command(me, (char*)&"look", cmd_param);
}
if(result.exit) {
break;
}
}else {
break;
}
}
printf("OK... Bye.\n");
}
void initalizeAlias(struct alias * item){
memset(item->src, '\0', sizeof(char)*30);
memset(item->dest, '\0', sizeof(char)*30);
item->next=NULL;
}
void push_alias(struct player *me, const char * src, const char * dest) {
if(me->aliases == NULL) {
struct alias * aliases = (struct alias*)malloc(sizeof(struct alias));
initalizeAlias(aliases);
me->aliases=aliases;
}
struct alias * head = me->aliases;
do {
if(strlen ( head->src ) == 0) {
break;
} else if(head->next !=NULL) {
head = head->next;
} else {
struct alias * nextItem = (struct alias*)malloc(sizeof(struct alias));
initalizeAlias(nextItem);
head->next = nextItem;
head = head->next;
}
} while(1);
if(head!=NULL) {
strcpy(head->src, src);
strcpy(head->dest, dest);
}
}
void initPlayer(struct player * me, World *w) {
me->aliases = (void*)NULL;
memset(me->name, '\0', sizeof(me->name));
me->world = w;
}
void world_add_room(World *w, struct room *current) {
if(w->rooms == NULL) {
w->rooms=(LoadedRoom*)malloc(sizeof(LoadedRoom));
LoadedRoom *p1=w->rooms;
p1->current = current;
p1->next = NULL;
return;
}
LoadedRoom* p = w->rooms;
printf("%s\n", p->current);
while(p->next != NULL) {
p = p->next;
}
p->next = (LoadedRoom*)malloc(sizeof(LoadedRoom));
p=p->next;
p->current = current;
p->next = NULL;
}
struct room * cached_read_room(World *w, const char * path) {
LoadedRoom *head = w->rooms;
while(head != NULL) {
if(strcmp(head->current->fileName, path)==0) {
return head->current;
}
head = head->next;
}
struct room *theRoom = read_room(path);
world_add_room(w, theRoom);
return theRoom;
}
int main(void) {
World *w = new_world("Adventure In C");
struct room *current = cached_read_room(w, "start");
struct player me;
initPlayer(&me, w);
read_player(&me);
push_alias(&me, "l", "look");
push_alias(&me, "u", "up");
push_alias(&me, "d", "down");
push_alias(&me, "lo", "look"); // Just an extra alias
push_alias(&me, "h", "help");
push_alias(&me, "ex", "examine");
me.currentRoom = current;
printf("%s enters the world.\n\n",me.name);
look_player(&me);
loop_player(&me);
}