/
rlg229.c
157 lines (139 loc) · 4.11 KB
/
rlg229.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
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include "dungeon.h"
#include "character.h"
#include "utils.h"
#include "pc.h"
#include "npc.h"
#include "move.h"
#include "io.h"
#include "descriptions.h"
static void usage(char *name)
{
fprintf(stderr,
"Usage: %s [-r|--rand <seed>] [-h|--huge]\n"
"%*c[-n|--nummon <number of monsters>]\n",
name, (int) strlen(name) + 8, ' ');
exit(-1);
}
int main(int argc, char *argv[])
{
dungeon_t d;
time_t seed;
uint32_t nummon;
struct timeval tv;
uint32_t i;
uint32_t do_seed;
uint32_t long_arg;
uint32_t do_huge;
memset(&d, 0, sizeof (d));
/* Default behavior: Seed with the time, generate a new dungeon, *
* and don't write to disk. */
do_seed = 1;
do_huge = 0;
nummon = 10;
/* The project spec requires '--load' and '--store'. It's common *
* to have short and long forms of most switches (assuming you *
* don't run out of letters). For now, we've got plenty. Long *
* forms use whole words and take two dashes. Short forms use an *
* abbreviation after a single dash. We'll add '--rand' (to *
* specify a random seed), which will take an argument of its *
* own, and we'll add short forms for all three commands, '-l', *
* '-s', and '-r', respectively. We're also going to allow an *
* optional argument to load to allow us to load non-default save *
* files. No means to store to non-default locations, however. */
if (argc > 1) {
for (i = 1, long_arg = 0; i < argc; i++, long_arg = 0) {
if (argv[i][0] == '-') { /* All switches start with a dash */
if (argv[i][1] == '-') {
argv[i]++; /* Make the argument have a single dash so we can */
long_arg = 1; /* handle long and short args at the same place. */
}
switch (argv[i][1]) {
case 'r':
if ((!long_arg && argv[i][2]) ||
(long_arg && strcmp(argv[i], "-rand")) ||
argc < ++i + 1 /* No more arguments */ ||
!sscanf(argv[i], "%lu", &seed) /* Argument is not an integer */) {
usage(argv[0]);
}
do_seed = 0;
break;
case 'n':
if ((!long_arg && argv[i][2]) ||
(long_arg && strcmp(argv[i], "-nummon")) ||
argc < ++i + 1 /* No more arguments */ ||
!sscanf(argv[i], "%u", &nummon) /* Argument is not an int */) {
usage(argv[0]);
}
break;
case 'h':
if ((!long_arg && argv[i][2]) ||
(long_arg && strcmp(argv[i], "-huge"))) {
usage(argv[0]);
}
do_huge = 1;
break;
default:
usage(argv[0]);
}
} else { /* No dash */
usage(argv[0]);
}
}
}
if (do_seed) {
/* Allows me to generate more than one dungeon *
* per second, as opposed to time(). */
gettimeofday(&tv, NULL);
seed = (tv.tv_usec ^ (tv.tv_sec << 20)) & 0xffffffff;
}
if (do_huge) {
d.render_whole_dungeon = 1;
} else {
d.render_whole_dungeon = 0;
}
printf("Seed is %ld.\n", seed);
srand(seed);
parse_descriptions(&d);
io_init_terminal();
init_dungeon(&d);
if (read_dungeon(&d)) {
gen_dungeon(&d);
config_pc(&d);
gen_monsters(&d, nummon, 0);
gen_objects(&d, 10);
}
io_display(&d);
while (pc_is_alive(&d) && dungeon_has_npcs(&d) &&
!(d.save_and_exit || d.quit_no_save)) {
do_moves(&d);
io_display(&d);
if (!pc_is_alive(&d)) {
break;
}
io_handle_input(&d);
}
io_display(&d);
io_reset_terminal();
if (!(d.save_and_exit || d.quit_no_save)) {
sleep(2);
if (pc_is_alive(&d)) {
printf("%s says, \"%s\"\n", d.pc.pc->name, d.pc.pc->catch_phrase);
} else {
printf("The monster hordes growl menacingly.\n");
}
unlink_dungeon();
} else if (d.save_and_exit) {
write_dungeon(&d);
} else /* d.quit_no_save */ {
unlink_dungeon();
}
pc_delete(d.pc.pc);
destroy_descriptions(&d);
delete_dungeon(&d);
return 0;
}