-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathadd_action.c
754 lines (673 loc) · 18.1 KB
/
add_action.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#include "std.h"
#include "comm.h"
#include "backend.h"
#include "add_action.h"
#ifndef NO_ADD_ACTION
#define MAX_VERB_BUFF 100
object_t * hashed_living[CFG_LIVING_HASH_SIZE] = { 0 };
static int num_living_names;
static int num_searches = 1;
static int search_length = 1;
static int illegal_sentence_action;
static char * last_verb;
static object_t * illegal_sentence_ob;
static void notify_no_command PROT((void))
{
union string_or_func p;
svalue_t *v;
if (!command_giver || !command_giver->interactive)
return;
p = command_giver->interactive->default_err_message;
if (command_giver->interactive->iflags & NOTIFY_FAIL_FUNC) {
save_command_giver(command_giver);
v = call_function_pointer(p.f, 0);
restore_command_giver();
free_funp(p.f);
if (command_giver && command_giver->interactive) {
if (v && v->type == T_STRING) {
tell_object(command_giver, v->u.string, SVALUE_STRLEN(v));
}
command_giver->interactive->iflags &= ~NOTIFY_FAIL_FUNC;
command_giver->interactive->default_err_message.s = 0;
}
} else {
if (p.s) {
tell_object(command_giver, p.s, strlen(p.s));
free_string(p.s);
command_giver->interactive->default_err_message.s = 0;
} else {
tell_object(command_giver, default_fail_message, strlen(default_fail_message));
}
}
}
void clear_notify P1(object_t *, ob)
{
union string_or_func dem;
interactive_t *ip = ob->interactive;
dem = ip->default_err_message;
if (ip->iflags & NOTIFY_FAIL_FUNC) {
free_funp(dem.f);
ip->iflags &= ~NOTIFY_FAIL_FUNC;
}
else if (dem.s)
free_string(dem.s);
ip->default_err_message.s = 0;
}
INLINE_STATIC int hash_living_name P1(char *, str)
{
return whashstr(str, 20) & (CFG_LIVING_HASH_SIZE - 1);
}
object_t *find_living_object P2(char *, str, int, user)
{
object_t **obp, *tmp;
object_t **hl;
if (!str)
return 0;
num_searches++;
hl = &hashed_living[hash_living_name(str)];
for (obp = hl; *obp; obp = &(*obp)->next_hashed_living) {
search_length++;
#ifdef F_SET_HIDE
if ((*obp)->flags & O_HIDDEN) {
if (!valid_hide(current_object))
continue;
}
#endif
if (user && !((*obp)->flags & O_ONCE_INTERACTIVE))
continue;
if (!((*obp)->flags & O_ENABLE_COMMANDS))
continue;
if (strcmp((*obp)->living_name, str) == 0)
break;
}
if (*obp == 0)
return 0;
/* Move the found ob first. */
if (obp == hl)
return *obp;
tmp = *obp;
*obp = tmp->next_hashed_living;
tmp->next_hashed_living = *hl;
*hl = tmp;
return tmp;
}
void remove_living_name P1(object_t *, ob)
{
object_t **hl;
ob->flags &= ~O_ENABLE_COMMANDS;
if (!ob->living_name)
return;
num_living_names--;
DEBUG_CHECK(!ob->living_name, "remove_living_name: no living name set.\n");
hl = &hashed_living[hash_living_name(ob->living_name)];
while (*hl) {
if (*hl == ob)
break;
hl = &(*hl)->next_hashed_living;
}
DEBUG_CHECK1(*hl == 0,
"remove_living_name: Object named %s no in hash list.\n",
ob->living_name);
*hl = ob->next_hashed_living;
free_string(ob->living_name);
ob->next_hashed_living = 0;
ob->living_name = 0;
}
static void set_living_name P2(object_t *, ob, char *, str)
{
int flags = ob->flags & O_ENABLE_COMMANDS;
object_t **hl;
if (ob->flags & O_DESTRUCTED)
return;
remove_living_name(ob);
num_living_names++;
hl = &hashed_living[hash_living_name(str)];
ob->next_hashed_living = *hl;
*hl = ob;
ob->living_name = make_shared_string(str);
ob->flags |= flags;
}
void stat_living_objects P1(outbuffer_t *, out)
{
outbuf_add(out, "Hash table of living objects:\n");
outbuf_add(out, "-----------------------------\n");
outbuf_addv(out, "%d living named objects, average search length: %4.2f\n\n",
num_living_names, (double) search_length / num_searches);
}
void setup_new_commands P2(object_t *, dest, object_t *, item)
{
object_t *next_ob, *ob;
/*
* Setup the new commands. The order is very important, as commands in
* the room should override commands defined by the room. Beware that
* init() in the room may have moved 'item' !
*
* The call of init() should really be done by the object itself (except in
* the -o mode). It might be too slow, though :-(
*/
if (item->flags & O_ENABLE_COMMANDS) {
save_command_giver(item);
(void) apply(APPLY_INIT, dest, 0, ORIGIN_DRIVER);
restore_command_giver();
if (item->super != dest)
return;
}
/*
* Run init of the item once for every present user, and for the
* environment (which can be a user).
*/
for (ob = dest->contains; ob; ob = next_ob) {
next_ob = ob->next_inv;
if (ob == item)
continue;
if (ob->flags & O_DESTRUCTED)
error("An object was destructed at call of " APPLY_INIT "()\n");
if (ob->flags & O_ENABLE_COMMANDS) {
save_command_giver(ob);
(void) apply(APPLY_INIT, item, 0, ORIGIN_DRIVER);
restore_command_giver();
if (dest != item->super)
return;
}
if (item->flags & O_DESTRUCTED) /* marion */
error("The object to be moved was destructed at call of " APPLY_INIT "()\n");
if (ob->flags & O_DESTRUCTED) /* Alaron */
error("An object was destructed at call of " APPLY_INIT "()\n");
if (item->flags & O_ENABLE_COMMANDS) {
save_command_giver(item);
(void) apply(APPLY_INIT, ob, 0, ORIGIN_DRIVER);
restore_command_giver();
if (dest != item->super)
return;
}
}
if (dest->flags & O_DESTRUCTED) /* marion */
error("The destination to move to was destructed at call of " APPLY_INIT "()\n");
if (item->flags & O_DESTRUCTED) /* Alaron */
error("The object to be moved was destructed at call of " APPLY_INIT "()\n");
if (dest->flags & O_ENABLE_COMMANDS) {
save_command_giver(dest);
(void) apply(APPLY_INIT, item, 0, ORIGIN_DRIVER);
restore_command_giver();
}
}
/*
* This will enable an object to use commands normally only
* accessible by interactive users.
* Also check if the user is a wizard. Wizards must not affect the
* value of the wizlist ranking.
*/
static void enable_commands P1(int, num)
{
#ifndef NO_ENVIRONMENT
object_t *pp;
#endif
if (current_object->flags & O_DESTRUCTED)
return;
debug(d_flag, ("Enable commands /%s (ref %d)",
current_object->name, current_object->ref));
if (num) {
current_object->flags |= O_ENABLE_COMMANDS;
set_command_giver(current_object);
} else {
#ifndef NO_ENVIRONMENT
/* Remove all sentences defined for the object */
if (current_object->flags & O_ENABLE_COMMANDS) {
if (current_object->super) {
remove_sent(current_object->super, current_object);
for (pp = current_object->super->contains; pp; pp = pp->next_inv)
remove_sent(pp, current_object);
}
for (pp = current_object->contains; pp; pp = pp->next_inv)
remove_sent(pp, current_object);
}
#endif
current_object->flags &= ~O_ENABLE_COMMANDS;
if (current_object == command_giver)
set_command_giver(0);
}
}
/*
* Find the sentence for a command from the user.
* Return success status.
*/
static int user_parser P1(char *, buff)
{
char verb_buff[MAX_VERB_BUFF];
sentence_t *s;
char *p;
int length;
char *user_verb = 0;
int where;
int save_illegal_sentence_action;
debug(d_flag, ("cmd [/%s]: %s\n", command_giver->name, buff));
/* strip trailing spaces. */
for (p = buff + strlen(buff) - 1; p >= buff; p--) {
if (*p != ' ')
break;
*p = '\0';
}
if (buff[0] == '\0')
return 0;
length = p - buff + 1;
p = strchr(buff, ' ');
if (p == 0) {
user_verb = findstring(buff);
} else {
*p = '\0';
user_verb = findstring(buff);
*p = ' ';
length = p - buff;
}
if (!user_verb) {
/* either an xverb or a verb without a specific add_action */
user_verb = buff;
}
/*
* copy user_verb into a static character buffer to be pointed to by
* last_verb.
*/
strncpy(verb_buff, user_verb, MAX_VERB_BUFF - 1);
if (p) {
int pos;
pos = p - buff;
if (pos < MAX_VERB_BUFF) {
verb_buff[pos] = '\0';
}
}
save_illegal_sentence_action = illegal_sentence_action;
illegal_sentence_action = 0;
for (s = command_giver->sent; s; s = s->next) {
svalue_t *ret;
object_t *command_object;
if (s->flags & (V_NOSPACE | V_SHORT)) {
if (strncmp(buff, s->verb, strlen(s->verb)) != 0)
continue;
} else {
/* note: if was add_action(blah, "") then accept it */
if (s->verb[0] && (user_verb != s->verb))
continue;
}
/*
* Now we have found a special sentence !
*/
if (!(s->flags & V_FUNCTION))
debug(d_flag, ("Local command %s on /%s",
s->function.s, s->ob->name));
if (s->flags & V_NOSPACE) {
int l1 = strlen(s->verb);
int l2 = strlen(verb_buff);
if (l1 < l2)
last_verb = verb_buff + l1;
else
last_verb = "";
} else {
if (!s->verb[0] || (s->flags & V_SHORT))
last_verb = verb_buff;
else
last_verb = s->verb;
}
/*
* If the function is static and not defined by current object, then
* it will fail. If this is called directly from user input, then
* the origin is the driver and it will be allowed.
*/
where = (current_object ? ORIGIN_EFUN : ORIGIN_DRIVER);
/*
* Remember the object, to update moves.
*/
command_object = s->ob;
save_command_giver(command_giver);
if (s->flags & V_NOSPACE) {
copy_and_push_string(&buff[strlen(s->verb)]);
} else if (buff[length] == ' ') {
copy_and_push_string(&buff[length + 1]);
} else {
push_undefined();
}
if (s->flags & V_FUNCTION) {
ret = call_function_pointer(s->function.f, 1);
} else {
if (s->function.s[0] == APPLY___INIT_SPECIAL_CHAR)
error("Illegal function name.\n");
ret = apply(s->function.s, s->ob, 1, where);
}
/* s may be dangling at this point */
restore_command_giver();
last_verb = 0;
/* was this the right verb? */
if (ret == 0) {
/* is it still around? Otherwise, ignore this ...
it moved somewhere or dested itself */
if (s == command_giver->sent) {
char buf[256];
if (s->flags & V_FUNCTION) {
sprintf(buf, "Verb '%s' bound to uncallable function pointer.\n", s->verb);
error(buf);
} else {
sprintf(buf, "Function for verb '%s' not found.\n",
s->verb);
error(buf);
}
}
}
if (ret && (ret->type != T_NUMBER || ret->u.number != 0)) {
#ifdef PACKAGE_MUDLIB_STATS
if (command_giver && command_giver->interactive
#ifndef NO_WIZARDS
&& !(command_giver->flags & O_IS_WIZARD)
#endif
)
add_moves(&command_object->stats, 1);
#endif
if (!illegal_sentence_action)
illegal_sentence_action = save_illegal_sentence_action;
return 1;
}
if (illegal_sentence_action) {
switch (illegal_sentence_action) {
case 1:
error("Illegal to call remove_action() [caller was /%s] from a verb returning zero.\n", illegal_sentence_ob->name);
case 2:
error("Illegal to move or destruct an object (/%s) defining actions from a verb function which returns zero.\n", illegal_sentence_ob->name);
}
}
}
notify_no_command();
illegal_sentence_action = save_illegal_sentence_action;
return 0;
}
/*
* Take a user command and parse it.
* The command can also come from a NPC.
* Beware that 'str' can be modified and extended !
*/
int parse_command P2(char *, str, object_t *, ob)
{
int res;
/* disallow users to issue commands containing ansi escape codes */
#if defined(NO_ANSI) && !defined(STRIP_BEFORE_PROCESS_INPUT)
char *c;
for (c = str; *c; c++) {
if (*c == 27) {
*c = ' '; /* replace ESC with ' ' */
}
}
#endif
save_command_giver(ob);
res = user_parser(str);
restore_command_giver();
return res;
}
/*
* Associate a command with function in this object.
*
* The optinal third argument is a flag that will state that the verb should
* only match against leading characters.
*
* The object must be near the command giver, so that we ensure that the
* sentence is removed when the command giver leaves.
*
* If the call is from a shadow, make it look like it is really from
* the shadowed object.
*/
static void add_action P3(svalue_t *, str, char *, cmd, int, flag)
{
sentence_t *p;
object_t *ob;
if (current_object->flags & O_DESTRUCTED)
return;
ob = current_object;
#ifndef NO_SHADOWS
while (ob->shadowing) {
ob = ob->shadowing;
}
/* don't allow add_actions of a static function from a shadowing object */
if ((ob != current_object) && str->type == T_STRING && is_static(str->u.string, ob)) {
return;
}
#endif
if (command_giver == 0 || (command_giver->flags & O_DESTRUCTED))
return;
if (ob != command_giver
#ifndef NO_ENVIRONMENT
&& ob->super != command_giver &&
ob->super != command_giver->super && ob != command_giver->super
#endif
)
return; /* No need for an error, they know what they
* did wrong. */
p = alloc_sentence();
if (str->type == T_STRING) {
debug(d_flag, ("--Add action %s", str->u.string));
p->function.s = make_shared_string(str->u.string);
p->flags = flag;
} else {
debug(d_flag, ("--Add action <function>"));
p->function.f = str->u.fp;
str->u.fp->hdr.ref++;
p->flags = flag | V_FUNCTION;
}
p->ob = ob;
p->verb = make_shared_string(cmd);
/* This is ok; adding to the top of the list doesn't harm anything */
p->next = command_giver->sent;
command_giver->sent = p;
}
/*
* Remove sentence with specified verb and action. Return 1
* if success. If command_giver, remove his action, otherwise
* remove current_object's action.
*/
static int remove_action P2(char *, act, char *, verb)
{
object_t *ob;
sentence_t **s;
if (command_giver)
ob = command_giver;
else
ob = current_object;
if (ob) {
for (s = &ob->sent; *s; s = &((*s)->next)) {
sentence_t *tmp;
if (((*s)->ob == current_object) && (!((*s)->flags & V_FUNCTION))
&& !strcmp((*s)->function.s, act)
&& !strcmp((*s)->verb, verb)) {
tmp = *s;
*s = tmp->next;
free_sentence(tmp);
illegal_sentence_action = 1;
illegal_sentence_ob = current_object;
return 1;
}
}
}
return 0;
}
/*
* Remove all commands (sentences) defined by object 'ob' in object
* 'user'
*/
#ifndef NO_ENVIRONMENT
void remove_sent P2(object_t *, ob, object_t *, user)
{
sentence_t **s;
if (!(user->flags & O_ENABLE_COMMANDS))
return;
for (s = &user->sent; *s;) {
sentence_t *tmp;
if ((*s)->ob == ob) {
#ifdef DEBUG
if (!((*s)->flags & V_FUNCTION))
debug(d_flag, ("--Unlinking sentence %s\n", (*s)->function.s));
#endif
tmp = *s;
*s = tmp->next;
free_sentence(tmp);
illegal_sentence_action = 2;
illegal_sentence_ob = ob;
} else
s = &((*s)->next);
}
}
#endif
#ifdef F_ADD_ACTION
void
f_add_action PROT((void))
{
int flag;
if (st_num_arg == 3) {
flag = (sp--)->u.number;
} else flag = 0;
if (sp->type == T_ARRAY) {
int i, n = sp->u.arr->size;
svalue_t *sv = sp->u.arr->item;
for (i = 0; i < n; i++) {
if (sv[i].type == T_STRING) {
add_action(sp-1, sv[i].u.string, flag & 3);
}
}
free_array((sp--)->u.arr);
} else {
add_action((sp-1), sp->u.string, flag & 3);
free_string_svalue(sp--);
}
pop_stack();
}
#endif
#ifdef F_COMMAND
/*
* Execute a command for an object. Copy the command into a
* new buffer, because 'parse_command()' can modify the command.
* If the object is not current object, static functions will not
* be executed. This will prevent forcing users to do illegal things.
*
* Return cost of the command executed if success (> 0).
* When failure, return 0.
*/
void f_command PROT((void))
{
int rc = 0;
if (current_object && !(current_object->flags & O_DESTRUCTED))
{
char buff[1000];
int save_eval_cost = eval_cost;
if (SVALUE_STRLEN(sp) > sizeof(buff) - 1)
error("Too long command.\n");
strncpy(buff, sp->u.string, sizeof(buff));
buff[sizeof(buff) - 1] = 0;
if (parse_command(buff, current_object))
rc = save_eval_cost - eval_cost;
}
free_string_svalue(sp);
put_number(rc);
}
#endif
#ifdef F_COMMANDS
void f_commands PROT((void))
{
push_refed_array(commands(current_object));
}
#endif
#ifdef F_DISABLE_COMMANDS
void f_disable_commands PROT((void))
{
enable_commands(0);
}
#endif
#ifdef F_ENABLE_COMMANDS
void f_enable_commands PROT((void))
{
enable_commands(1);
}
#endif
#ifdef F_FIND_LIVING
void f_find_living PROT((void))
{
object_t *ob;
ob = find_living_object(sp->u.string, 0);
free_string_svalue(sp);
/* safe b/c destructed objects have had their living names removed */
if (ob) {
put_unrefed_undested_object(ob, "find_living");
} else {
*sp = const0;
}
}
#endif
#ifdef F_FIND_PLAYER
void f_find_player PROT((void))
{
object_t *ob;
ob = find_living_object(sp->u.string, 1);
free_string_svalue(sp);
/* safe b/c destructed objects have had their living names removed */
if (ob) {
put_unrefed_undested_object(ob, "find_living");
} else {
*sp = const0;
}
}
#endif
#ifdef F_LIVING
void f_living PROT((void))
{
if (sp->u.ob->flags & O_ENABLE_COMMANDS) {
free_object(sp->u.ob, "f_living:1");
*sp = const1;
} else {
free_object(sp->u.ob, "f_living:2");
*sp = const0;
}
}
#endif
#ifdef F_LIVINGS
void f_livings PROT((void))
{
push_refed_array(livings());
}
#endif
#ifdef F_NOTIFY_FAIL
void f_notify_fail PROT((void))
{
if (command_giver && command_giver->interactive) {
clear_notify(command_giver);
if (sp->type == T_STRING) {
command_giver->interactive->default_err_message.s = make_shared_string(sp->u.string);
} else {
command_giver->interactive->iflags |= NOTIFY_FAIL_FUNC;
command_giver->interactive->default_err_message.f = sp->u.fp;
sp->u.fp->hdr.ref++;
}
}
pop_stack();
}
#endif
#ifdef F_QUERY_VERB
void f_query_verb PROT((void))
{
if (!last_verb) {
push_number(0);
return;
}
share_and_push_string(last_verb);
}
#endif
#ifdef F_REMOVE_ACTION
void f_remove_action PROT((void))
{
int success;
success = remove_action((sp - 1)->u.string, sp->u.string);
free_string_svalue(sp--);
free_string_svalue(sp);
put_number(success);
}
#endif
#ifdef F_SET_LIVING_NAME
void f_set_living_name PROT((void))
{
set_living_name(current_object, sp->u.string);
free_string_svalue(sp--);
}
#endif
#endif /* ! NO_ADD_ACTION */