-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.c
263 lines (228 loc) · 5.2 KB
/
match.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
#include "copyright.h"
/* Routines for parsing arguments */
#include <stdlib.h>
#include <ctype.h>
#include "externs.h"
#include "interface.h"
#include "db.h"
#include "config.h"
#include "match.h"
#define DOWNCASE(x) (isupper(x) ? tolower(x) : (x))
static dbref exact_match = NOTHING; /* holds result of exact match */
static int check_keys = 0; /* if non-zero, check for keys */
static dbref last_match = NOTHING; /* holds result of last match */
static int match_count; /* holds total number of inexact matches */
static dbref match_who; /* player who is being matched around */
static const char *match_name; /* name to match */
static int preferred_type = NOTYPE; /* preferred type */
void init_match(dbref player, const char *name, int type)
{
exact_match = last_match = NOTHING;
match_count = 0;
match_who = player;
match_name = name;
check_keys = 0;
preferred_type = type;
}
void init_match_check_keys(dbref player, const char *name, int type)
{
init_match(player, name, type);
check_keys = 1;
}
static dbref choose_thing(dbref thing1, dbref thing2)
{
int has1;
int has2;
if(thing1 == NOTHING) {
return thing2;
} else if(thing2 == NOTHING) {
return thing1;
}
if(preferred_type != NOTYPE) {
if(Typeof(thing1) == preferred_type) {
if(Typeof(thing2) != preferred_type) {
return thing1;
}
} else if(Typeof(thing2) == preferred_type) {
return thing2;
}
}
if(check_keys) {
has1 = could_doit(match_who, thing1);
has2 = could_doit(match_who, thing2);
if(has1 && !has2) {
return thing1;
} else if (has2 && !has1) {
return thing2;
}
/* else fall through */
}
return (random() % 2 ? thing1 : thing2);
}
void match_player(void)
{
dbref match;
const char *p;
if(*match_name == LOOKUP_TOKEN) {
for(p = match_name + 1; isspace(*p); p++);
if((match = lookup_player(p)) != NOTHING) {
exact_match = match;
}
}
}
/* returns nnn if name = #nnn, else NOTHING */
static dbref absolute_name(void)
{
dbref match;
if(*match_name == NUMBER_TOKEN) {
match = parse_dbref(match_name+1);
if(match < 0 || match >= db_top) {
return NOTHING;
} else {
return match;
}
} else {
return NOTHING;
}
}
void match_absolute(void)
{
dbref match;
if((match = absolute_name()) != NOTHING) {
exact_match = match;
}
}
void match_me(void)
{
if(!string_compare(match_name, "me")) {
exact_match = match_who;
}
}
void match_here(void)
{
if(!string_compare(match_name, "here")
&& db[match_who].location != NOTHING) {
exact_match = db[match_who].location;
}
}
static void match_list(dbref first)
{
dbref absolute;
absolute = absolute_name();
if(!controls(match_who, absolute)) absolute = NOTHING;
DOLIST(first, first) {
if(first == absolute) {
exact_match = first;
return;
} else if(!string_compare(db[first].name, match_name)) {
/* if there are multiple exact matches, randomly choose one */
exact_match = choose_thing(exact_match, first);
} else if(string_match(db[first].name, match_name)) {
last_match = first;
match_count++;
}
}
}
void match_possession(void)
{
match_list(db[match_who].contents);
}
void match_neighbor(void)
{
dbref loc;
if((loc = db[match_who].location) != NOTHING) {
match_list(db[loc].contents);
}
}
void match_exit(void)
{
dbref loc;
dbref exit;
dbref absolute;
const char *match;
const char *p;
if((loc = db[match_who].location) != NOTHING) {
absolute = absolute_name();
if(!controls(match_who, absolute)) absolute = NOTHING;
DOLIST(exit, db[loc].exits) {
if(exit == absolute) {
exact_match = exit;
} else {
match = db[exit].name;
while(*match) {
/* check out this one */
for(p = match_name;
(*p
&& DOWNCASE(*p) == DOWNCASE(*match)
&& *match != EXIT_DELIMITER);
p++, match++);
/* did we get it? */
if(*p == '\0') {
/* make sure there's nothing afterwards */
while(isspace(*match)) match++;
if(*match == '\0' || *match == EXIT_DELIMITER) {
/* we got it */
exact_match = choose_thing(exact_match, exit);
goto next_exit; /* got this match */
}
}
/* we didn't get it, find next match */
while(*match && *match++ != EXIT_DELIMITER);
while(isspace(*match)) match++;
}
}
next_exit:
;
}
}
}
void match_everything(void)
{
match_exit();
match_neighbor();
match_possession();
match_me();
match_here();
if(Wizard(match_who)) {
match_absolute();
match_player();
}
}
dbref match_result(void)
{
if(exact_match != NOTHING) {
return exact_match;
} else {
switch(match_count) {
case 0:
return NOTHING;
case 1:
return last_match;
default:
return AMBIGUOUS;
}
}
}
/* use this if you don't care about ambiguity */
dbref last_match_result(void)
{
if(exact_match != NOTHING) {
return exact_match;
} else {
return last_match;
}
}
dbref noisy_match_result(void)
{
dbref match;
switch(match = match_result()) {
case NOTHING:
notify(match_who, NOMATCH_MESSAGE);
return NOTHING;
case AMBIGUOUS:
notify(match_who, AMBIGUOUS_MESSAGE);
return NOTHING;
default:
return match;
}
}