-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser.c
296 lines (251 loc) · 5.69 KB
/
parser.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
#include "parser.h"
#include "spells.h"
#include "skills.h"
#include "keywords.h"
#include "items.h"
#include "unit.h"
#include "region.h"
#include "faction.h"
#include "rtl.h"
#include <quicklist.h>
#include <string.h>
#include <stdlib.h>
const char *skillnames[] = {
"mining",
"lumberjack",
"quarrying",
"horse training",
"weaponsmith",
"armorer",
"building",
"shipbuilding",
"entertainment",
"stealth",
"observation",
"tactics",
"riding",
"sword",
"crossbow",
"longbow",
"magic",
};
const char *itemnames[MAXITEMS][2] = {
{ "iron", "iron" },
{ "wood", "wood" },
{ "stone", "stone" },
{ "horse", "horses" },
{ "sword", "swords" },
{ "crossbow", "crossbows" },
{ "longbow", "longbows" },
{ "chain mail", "chain mail" },
{ "plate armor", "plate armor" },
{ "Amulet of Darkness", "Amulets of Darkness" },
{ "Amulet of Death", "Amulets of Death" },
{ "Amulet of Healing", "Amulets of Healing" },
{ "Amulet of True Seeing", "Amulets of True Seeing" },
{ "Cloak of Invulnerability", "Cloaks of Invulnerability" },
{ "Ring of Invisibility", "Rings of Invisibility" },
{ "Ring of Power", "Rings of Power" },
{ "Runesword", "Runeswords" },
{ "Shieldstone", "Shieldstones" },
{ "Staff of Fire", "Staffs of Fire" },
{ "Staff of Lightning", "Staffs of Lightning" },
{ "Wand of Teleportation", "Wands of Teleportation" }
};
const char *spellnames[MAXSPELLS] = {
"Black Wind",
"Cause Fear",
"Contaminate Water",
"Dazzling Light",
"Fireball",
"Hand of Death",
"Heal",
"Inspire Courage",
"Lightning Bolt",
"Make Amulet of Darkness",
"Make Amulet of Death",
"Make Amulet of Healing",
"Make Amulet of True Seeing",
"Make Cloak of Invulnerability",
"Make Ring of Invisibility",
"Make Ring of Power",
"Make Runesword",
"Make Shieldstone",
"Make Staff of Fire",
"Make Staff of Lightning",
"Make Wand of Teleportation",
"Shield",
"Sunfire",
"Teleport",
};
int strpcmp(const void *s1, const void *s2)
{
return _strcmpl(*(char **) s1, *(char **) s2);
}
keyword_t findkeyword(const char *s)
{
const char **sp;
if (!_strcmpl(s, "describe"))
return K_DISPLAY;
sp = (const char **)bsearch(&s, keywords, MAXKEYWORDS, sizeof s, strpcmp);
if (sp == 0) {
return MAXKEYWORDS;
}
return sp - keywords;
}
keyword_t igetkeyword(const char *s)
{
return (keyword_t)findkeyword(igetstr(s));
}
keyword_t getkeyword(void)
{
return (keyword_t)findkeyword(igetstr(0));
}
static const char * directions[MAXDIRECTIONS][3] = {
{ "north", "n", 0 }, { "south", "s", 0 },
{ "west", "w", 0 }, { "east", "e", 0 },
{ "mir", "m", 0 }, { "ydd", "y", 0 }
};
const char *direction_name(int d) {
if (d >= 0 && d < MAXDIRECTIONS) {
if (config.directions[d]) {
return config.directions[d][0];
}
return directions[d][0];
}
return 0;
}
int finddirection(const char *s) {
int d;
for (d = 0; d != MAXDIRECTIONS; ++d) {
const char ** p = directions[d];
if (config.directions[d]) p = (const char **)config.directions[d];
while (*p) {
if (!_strcmpl(s, *p++)) return d;
}
}
return -1;
}
int findstr(const char **v, const char *s, int n)
{
int i;
for (i = 0; i != n; i++)
if (!_strcmpl(v[i], s))
return i;
return -1;
}
int findskill(const char *s)
{
if (!_strcmpl(s, "horse"))
return SK_HORSE_TRAINING;
if (!_strcmpl(s, "entertain"))
return SK_ENTERTAINMENT;
return findstr(skillnames, s, MAXSKILLS);
}
int getskill(void)
{
return findskill(igetstr(0));
}
int finditem(const char *s)
{
int i;
if (!_strcmpl(s, "chain"))
return I_CHAIN_MAIL;
if (!_strcmpl(s, "plate"))
return I_PLATE_ARMOR;
for (i = 0; i != MAXITEMS; i++) {
if (_strcmpl(itemnames[i][0], s)==0 || _strcmpl(itemnames[i][1], s)==0)
return i;
}
return -1;
}
int getitem(void)
{
return finditem(igetstr(0));
}
int findspell(const char *s)
{
return findstr(spellnames, s, MAXSPELLS);
}
int getspell(void)
{
return findspell(igetstr(0));
}
char *igetstr(const char *s1)
{
int i;
static const char *s;
static char buf[256];
if (s1)
s = s1;
while (*s == ' ')
s++;
i = 0;
while (*s && *s != ' ') {
buf[i] = *s;
if (*s == '_')
buf[i] = ' ';
s++;
i++;
}
buf[i] = 0;
return buf;
}
unit *getnewunit(region * r, const faction * f)
{
int n;
unit *u;
n = atoi(igetstr(0));
if (n <= 0) {
return 0;
}
for (u=r->units;u;u=u->next) {
if (u->faction == f && u->alias == n) {
return u;
}
}
return 0;
}
unit *getunitg(region *r, const faction *f)
{
const char *s;
s = igetstr(0);
if (!_strcmpl(s, "new")) {
return getnewunit(r, f);
}
return findunitg(atoi(s));
}
int getunit(region * r, const faction *f, unit **uptr)
{
int n;
const char *s;
unit *u;
s = igetstr(0);
if (!_strcmpl(s, "new")) {
unit *u = getnewunit(r, f);
if (u) {
*uptr = u;
return U_UNIT;
} else {
*uptr = 0;
return U_NOTFOUND;
}
}
if (!region_isocean(r) && !_strcmpl(s, "peasants")) {
*uptr = 0;
return U_PEASANTS;
}
n = atoi(s);
if (n == 0) {
*uptr = 0;
return U_NONE;
}
for (u=r->units;u;u=u->next) {
if (u->no == n && !u->isnew) {
*uptr = u;
return U_UNIT;
}
}
*uptr = 0;
return U_NOTFOUND;
}