ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.c
Revision: 1.1
Committed: Fri Feb 3 07:14:30 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Branch point for: UPSTREAM
Log Message:
Initial revision

File Contents

# User Rev Content
1 root 1.1 /*
2     * static char *rcsid_c_wiz_c =
3     * "$Id: c_wiz.c,v 1.78 2006/01/01 11:58:22 akirschbaum Exp $";
4     */
5    
6     /*
7     CrossFire, A Multiplayer game for X-windows
8    
9     Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10     Copyright (C) 1992 Frank Tore Johansen
11    
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16    
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20     GNU General Public License for more details.
21    
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25    
26     The authors can be reached via e-mail at crossfire-devel@real-time.com
27     */
28    
29     #include <global.h>
30     #ifndef __CEXTRACT__
31     #include <sproto.h>
32     #endif
33     #include <spells.h>
34     #include <treasure.h>
35     #include <skills.h>
36    
37     /** Defines for DM item stack **/
38     #define STACK_SIZE 50 /* Stack size, static */
39     /* Values for 'from' field of get_dm_object */
40     #define STACK_FROM_NONE 0 /* Item was not found */
41     #define STACK_FROM_TOP 1 /* Item is stack top */
42     #define STACK_FROM_STACK 2 /* Item is somewhere in stack */
43     #define STACK_FROM_NUMBER 3 /* Item is a number (may be top) */
44    
45    
46     /**
47     * Enough of the DM functions seem to need this that I broke
48     * it out to a seperate function. name is the person
49     * being saught, rq is who is looking for them. This
50     * prints diagnostics messages, and returns the
51     * other player, or NULL otherwise.
52     */
53     static player *get_other_player_from_name(object *op, char *name) {
54     player *pl;
55    
56     if (!name)
57     return NULL;
58    
59     for (pl = first_player; pl != NULL; pl = pl->next)
60     if (!strncmp(pl->ob->name, name, MAX_NAME))
61     break;
62    
63     if (pl == NULL) {
64     new_draw_info(NDI_UNIQUE, 0, op, "No such player.");
65     return NULL;
66     }
67    
68     if (pl->ob == op) {
69     new_draw_info(NDI_UNIQUE, 0, op, "You can't do that to yourself.");
70     return NULL;
71     }
72     if (pl->state != ST_PLAYING) {
73     new_draw_info(NDI_UNIQUE, 0, op, "That player is in no state for that right now.");
74     return NULL;
75     }
76     return pl;
77     }
78    
79     /**
80     * This command will stress server.
81     */
82     int command_loadtest(object *op, char *params) {
83     uint32 x, y;
84     char buf[1024];
85    
86     new_draw_info(NDI_UNIQUE, 0, op, "loadtest will stress server through teleporting");
87     new_draw_info(NDI_UNIQUE, 0, op, "at different map places.");
88     new_draw_info(NDI_UNIQUE, 0, op, "use at your own risks.");
89     new_draw_info(NDI_UNIQUE, 0, op, "Very long loop used so server may have to be reset.");
90     new_draw_info(NDI_UNIQUE, 0, op, "type loadtest TRUE to run");
91     new_draw_info_format(NDI_UNIQUE, 0, op, "{%s}", params);
92     if (!params)
93     return 0;
94     if (strncmp (params, "TRUE", 4))
95     return 0;
96    
97     new_draw_info_format(NDI_UNIQUE, 0, op, "gogogo");
98     for (x = 0; x < settings.worldmaptilesx; x++) {
99     for (y = 0; y < settings.worldmaptilesy; y++) {
100     sprintf(buf, "/world/world_%d_%d", x+settings.worldmapstartx, y+settings.worldmapstarty);
101     command_goto(op, buf);
102     }
103     }
104    
105     return 0;
106     }
107    
108     /**
109     * Actually hides specified player (obviously a DM).
110     * If 'silent_dm' is non zero, other players are informed of DM entering/leaving,
111     * else they just think someone left/entered.
112     */
113     void do_wizard_hide(object *op, int silent_dm) {
114     if (op->contr->hidden) {
115     op->contr->hidden = 0;
116     op->invisible = 1;
117     new_draw_info(NDI_UNIQUE, 0, op, "You are no longer hidden from other players");
118     op->map->players++;
119     new_draw_info_format(NDI_UNIQUE|NDI_ALL|NDI_DK_ORANGE, 5, NULL,
120     "%s has entered the game.", op->name);
121     if (!silent_dm) {
122     new_draw_info(NDI_UNIQUE|NDI_ALL|NDI_LT_GREEN, 1, NULL,
123     "The Dungeon Master has arrived!");
124     }
125     } else {
126     op->contr->hidden = 1;
127     new_draw_info(NDI_UNIQUE, 0, op, "Other players will no longer see you.");
128     op->map->players--;
129     if (!silent_dm) {
130     new_draw_info(NDI_UNIQUE|NDI_ALL|NDI_LT_GREEN, 1, NULL,
131     "The Dungeon Master is gone..");
132     }
133     new_draw_info_format(NDI_UNIQUE|NDI_ALL|NDI_DK_ORANGE, 5, NULL,
134     "%s leaves the game.", op->name);
135     new_draw_info_format(NDI_UNIQUE|NDI_ALL|NDI_DK_ORANGE, 5, NULL,
136     "%s left the game.", op->name);
137     }
138     }
139    
140     int command_hide(object *op, char *params)
141     {
142     do_wizard_hide(op, 0);
143     return 1;
144     }
145    
146     /**
147     * This finds and returns the object which matches the name or
148     * object nubmer (specified via num #whatever).
149     */
150     static object *find_object_both(char *params) {
151     if (!params)
152     return NULL;
153     if (params[0] == '#')
154     return find_object(atol(params+1));
155     else
156     return find_object_name(params);
157     }
158    
159     /**
160     * Sets the god for some objects. params should contain two values -
161     * first the object to change, followed by the god to change it to.
162     */
163     int command_setgod(object *op, char *params) {
164     object *ob, *god;
165     char *str;
166    
167     if (!params || !(str = strchr(params, ' '))) {
168     new_draw_info(NDI_UNIQUE, 0, op, "Usage: setgod object god");
169     return 0;
170     }
171    
172     /* kill the space, and set string to the next param */
173     *str++ = '\0';
174     if (!(ob = find_object_both(params))) {
175     new_draw_info_format(NDI_UNIQUE, 0, op, "Set whose god - can not find object %s?", params);
176     return 1;
177     }
178    
179     /*
180     * Perhaps this is overly restrictive? Should we perhaps be able
181     * to rebless altars and the like?
182     */
183     if (ob->type != PLAYER) {
184     new_draw_info_format(NDI_UNIQUE, 0, op, "%s is not a player - can not change its god", ob->name);
185     return 1;
186     }
187    
188     god = find_god(str);
189     if (god==NULL) {
190     new_draw_info_format(NDI_UNIQUE, 0, op, "No such god %s.", str);
191     return 1;
192     }
193    
194     become_follower(ob, god);
195     return 1;
196     }
197    
198     /**
199     * Add player's IP to ban_file and kick them off the server
200     * I know most people have dynamic IPs but this is more of a short term
201     * solution if they have to get a new IP to play maybe they'll calm down.
202     * This uses the banish_file in the local directory *not* the ban_file
203     * The action is logged with a ! for easy searching. -tm
204     */
205     int command_banish(object *op, char *params) {
206     player *pl;
207     FILE *banishfile;
208     char buf[MAX_BUF];
209     time_t now;
210    
211     if (!params) {
212     new_draw_info(NDI_UNIQUE, 0, op, "Usage: banish <player>.");
213     return 1;
214     }
215    
216     pl = get_other_player_from_name(op, params);
217     if (!pl)
218     return 1;
219    
220     sprintf(buf, "%s/%s", settings.localdir, BANISHFILE);
221    
222     if ((banishfile = fopen(buf, "a")) == NULL) {
223     LOG (llevDebug, "Could not find file banish_file.\n");
224     new_draw_info(NDI_UNIQUE, 0, op, "Could not find banish_file.");
225     return 0;
226     }
227    
228     now = time(NULL);
229     /*
230     * Record this as a comment - then we don't have to worry about changing
231     * the parsing code.
232     */
233     fprintf(banishfile, "# %s (%s) banned by %s at %s\n", pl->ob->name,
234     pl->socket.host, op->name, ctime(&now));
235     fprintf(banishfile, "*@%s\n", pl->socket.host);
236     fclose(banishfile);
237    
238     LOG(llevDebug, "! %s banned %s from IP: %s.\n", op->name, pl->ob->name, pl->socket.host);
239     new_draw_info_format(NDI_UNIQUE|NDI_RED, 0, op, "You banish %s", pl->ob->name);
240     new_draw_info_format(NDI_UNIQUE|NDI_ALL|NDI_RED, 5, op,
241     "%s banishes %s from the land!", op->name, pl->ob->name);
242     command_kick(op, pl->ob->name);
243     return 1;
244     }
245    
246     int command_kick(object *op, char *params) {
247     struct pl *pl;
248    
249     for (pl = first_player; pl != NULL; pl = pl->next) {
250     if ((params == NULL || !strcmp(pl->ob->name, params)) && pl->ob != op) {
251     object *op;
252     int removed = 0;
253    
254     op = pl->ob;
255     if (!QUERY_FLAG(op, FLAG_REMOVED)) {
256     /* Avion : Here we handle the KICK global event */
257     execute_global_event(EVENT_KICK, op, params);
258     remove_ob(op);
259     removed = 1;
260     }
261     op->direction = 0;
262     new_draw_info_format(NDI_UNIQUE|NDI_ALL|NDI_RED, 5, op,
263     "%s is kicked out of the game.", op->name);
264     strcpy(op->contr->killer, "left");
265     check_score(op); /* Always check score */
266    
267     /*
268     * not sure how the player would be freed, but did see
269     * a crash here - if that is the case, don't save the
270     * the player.
271     */
272     if (!removed && !QUERY_FLAG(op, FLAG_FREED)) {
273     (void)save_player(op, 0);
274     if (op->map)
275     op->map->players--;
276     }
277     #if MAP_MAXTIMEOUT
278     if (op->map)
279     op->map->timeout = MAP_TIMEOUT(op->map);
280     #endif
281     pl->socket.status = Ns_Dead;
282     }
283     }
284    
285     return 1;
286     }
287    
288     int command_save_overlay(object *op, char *params) {
289     if (!op)
290     return 0;
291    
292     if (op != NULL && !QUERY_FLAG(op, FLAG_WIZ)) {
293     new_draw_info(NDI_UNIQUE, 0, op,
294     "Sorry, you can't force an overlay save.");
295     return 1;
296     }
297    
298     new_save_map(op->map, 2);
299     new_save_map(op->map, 0);
300     new_draw_info(NDI_UNIQUE, 0, op, "Current map has been saved as an"
301     " overlay.");
302    
303     ready_map_name(op->map->path, 0);
304    
305     return 1;
306     }
307    
308     /*
309     * A simple toggle for the no_shout field.
310     * AKA the MUZZLE command
311     */
312     int command_toggle_shout(object *op, char *params) {
313     player *pl;
314    
315     if (!params) {
316     new_draw_info(NDI_UNIQUE, 0, op, "Usage: toggle_shout <player>.");
317     return 1;
318     }
319    
320     pl = get_other_player_from_name(op, params);
321     if (!pl)
322     return 1;
323    
324     if (pl->ob->contr->no_shout == 0) {
325     pl->ob->contr->no_shout = 1;
326    
327     new_draw_info(NDI_UNIQUE|NDI_RED, 0, pl->ob, "You have been muzzled by the DM!");
328     new_draw_info_format(NDI_UNIQUE, 0, op, "You muzzle %s.", pl->ob->name);
329    
330     /* Avion : Here we handle the MUZZLE global event */
331     execute_global_event(EVENT_MUZZLE, pl->ob, params);
332    
333     return 1;
334     } else {
335     pl->ob->contr->no_shout = 0;
336     new_draw_info(NDI_UNIQUE|NDI_ORANGE, 0, pl->ob,
337     "You are allowed to shout and chat again.");
338     new_draw_info_format(NDI_UNIQUE, 0, op,
339     "You remove %s's muzzle.", pl->ob->name);
340     return 1;
341     }
342     }
343    
344     int command_shutdown(object *op, char *params) {
345     if (op!=NULL && !QUERY_FLAG(op, FLAG_WIZ)) {
346     new_draw_info(NDI_UNIQUE, 0, op, "Sorry, you can't shutdown the server.");
347     return 1;
348     }
349    
350     /*
351     * We need to give op - command_kick expects it. however, this means
352     * the op won't get kicked off, so we do it ourselves
353     */
354     command_kick(op, NULL);
355     check_score(op); /* Always check score */
356     (void)save_player(op, 0);
357     play_again(op);
358     cleanup();
359     /* not reached */
360     return 1;
361     }
362    
363     int command_goto(object *op, char *params)
364     {
365     char *name;
366     object *dummy;
367    
368     if (!op)
369     return 0;
370    
371     if (params == NULL) {
372     new_draw_info(NDI_UNIQUE, 0, op, "Go to what level?");
373     return 1;
374     }
375    
376     name = params;
377     dummy=get_object();
378     dummy->map = op->map;
379     EXIT_PATH(dummy) = add_string (name);
380     dummy->name = add_string(name);
381    
382     enter_exit(op, dummy);
383     free_object(dummy);
384     if (op->contr->loading == NULL) {
385     new_draw_info_format(NDI_UNIQUE, 0, op,
386     "Difficulty: %d.", op->map->difficulty);
387     }
388    
389     return 1;
390     }
391    
392     /* is this function called from somewhere ? -Tero */
393     int command_generate (object *op, char *params)
394     {
395     object *tmp;
396     int nr = 1, i, retry;
397    
398     if (!op)
399     return 0;
400    
401     if (params != NULL)
402     sscanf(params, "%d", &nr);
403     for (i = 0; i < nr; i++) {
404     retry = 50;
405     while ((tmp=generate_treasure(0, op->map->difficulty)) == NULL && --retry)
406     ;
407     if (tmp != NULL) {
408     tmp = insert_ob_in_ob(tmp, op);
409     if (op->type == PLAYER)
410     esrv_send_item(op, tmp);
411     }
412     }
413    
414     return 1;
415     }
416    
417     int command_freeze(object *op, char *params) {
418     int ticks;
419     player *pl;
420    
421     if (!params) {
422     new_draw_info(NDI_UNIQUE, 0, op, "Usage: freeze [ticks] <player>.");
423     return 1;
424     }
425    
426     ticks = atoi(params);
427     if (ticks) {
428     while ((isdigit(*params) || isspace(*params)) && *params != 0)
429     params++;
430     if (*params == 0) {
431     new_draw_info(NDI_UNIQUE, 0, op, "Usage: freeze [ticks] <player>.");
432     return 1;
433     }
434     } else
435     ticks = 100;
436    
437     pl = get_other_player_from_name(op, params);
438     if (!pl)
439     return 1;
440    
441     new_draw_info(NDI_UNIQUE|NDI_RED, 0, pl->ob, "You have been frozen by the DM!");
442     new_draw_info_format(NDI_UNIQUE, 0, op,
443     "You freeze %s for %d ticks", pl->ob->name, ticks);
444     pl->ob->speed_left = -(pl->ob->speed*ticks);
445     return 0;
446     }
447    
448     int command_summon(object *op, char *params) {
449     int i;
450     object *dummy;
451     player *pl;
452    
453     if (!op)
454     return 0;
455    
456     if (params == NULL) {
457     new_draw_info(NDI_UNIQUE, 0, op, "Usage: summon <player>.");
458     return 1;
459     }
460    
461     pl = get_other_player_from_name(op, params);
462     if (!pl)
463     return 1;
464    
465     i = find_free_spot(op, op->map, op->x, op->y, 1, 9);
466     if (i == -1) {
467     new_draw_info(NDI_UNIQUE, 0, op, "Can not find a free spot to place summoned player.");
468     return 1;
469     }
470    
471     dummy = get_object();
472     EXIT_PATH(dummy) = add_string(op->map->path);
473     EXIT_X(dummy) = op->x+freearr_x[i];
474     EXIT_Y(dummy) = op->y+freearr_y[i];
475     enter_exit(pl->ob, dummy);
476     free_object(dummy);
477     new_draw_info(NDI_UNIQUE, 0, pl->ob, "You are summoned.");
478     new_draw_info(NDI_UNIQUE, 0, op, "OK.");
479    
480     return 1;
481     }
482    
483     /**
484     * Teleport next to target player.
485     */
486     /* mids 01/16/2002 */
487     int command_teleport(object *op, char *params) {
488     int i;
489     object *dummy;
490     player *pl;
491    
492     if (!op)
493     return 0;
494    
495     if (params == NULL) {
496     new_draw_info(NDI_UNIQUE, 0, op, "Usage: teleport <player>.");
497     return 1;
498     }
499    
500     pl = get_other_player_from_name(op, params);
501     if (!pl)
502     return 1;
503    
504     i = find_free_spot(pl->ob, pl->ob->map, pl->ob->x, pl->ob->y, 1, 9);
505     if (i == -1) {
506     new_draw_info(NDI_UNIQUE, 0, op, "Can not find a free spot to teleport to.");
507     return 1;
508     }
509    
510     dummy = get_object();
511     EXIT_PATH(dummy) = add_string(pl->ob->map->path);
512     EXIT_X(dummy) = pl->ob->x + freearr_x[i];
513     EXIT_Y(dummy) = pl->ob->y + freearr_y[i];
514     enter_exit(op, dummy);
515     free_object(dummy);
516     if (!op->contr->hidden)
517     new_draw_info(NDI_UNIQUE, 0, pl->ob, "You see a portal open.");
518     new_draw_info(NDI_UNIQUE, 0, op, "OK.");
519     return 1;
520     }
521    
522     /**
523     * This function is a real mess, because we're stucking getting
524     * the entire item description in one block of text, so we just
525     * can't simply parse it - we need to look for double quotes
526     * for example. This could actually get much simpler with just a
527     * little help from the client - if we could get line breaks, it
528     * makes parsing much easier, eg, something like:
529     * arch dragon
530     * name big nasty creature
531     * hp 5
532     * sp 30
533     * Is much easier to parse than
534     * dragon name "big nasty creature" hp 5 sp 30
535     * for example.
536     */
537     int command_create(object *op, char *params) {
538     object *tmp = NULL;
539     int nrof, i, magic, set_magic = 0, set_nrof = 0, gotquote, gotspace;
540     char buf[MAX_BUF], *cp, *bp = buf, *bp2, *bp3, *bp4, *endline;
541     archetype *at, *at_spell = NULL;
542     artifact *art = NULL;
543    
544     if (!op)
545     return 0;
546    
547     if (params == NULL) {
548     new_draw_info(NDI_UNIQUE, 0, op,
549     "Usage: create [nr] [magic] <archetype> [ of <artifact>]"
550     " [variable_to_patch setting]");
551     return 1;
552     }
553     bp = params;
554    
555     /* We need to know where the line ends */
556     endline = bp+strlen(bp);
557    
558     if (sscanf(bp, "%d ", &nrof)) {
559     if ((bp = strchr(params, ' ')) == NULL) {
560     new_draw_info(NDI_UNIQUE, 0, op,
561     "Usage: create [nr] [magic] <archetype> [ of <artifact>]"
562     " [variable_to_patch setting]");
563     return 1;
564     }
565     bp++;
566     set_nrof = 1;
567     LOG(llevDebug, "%s creates: (%d) %s\n", op->name, nrof, bp);
568     }
569     if (sscanf(bp, "%d ", &magic)) {
570     if ((bp = strchr(bp, ' ')) == NULL) {
571     new_draw_info(NDI_UNIQUE, 0, op,
572     "Usage: create [nr] [magic] <archetype> [ of <artifact>]"
573     " [variable_to_patch setting]");
574     return 1;
575     }
576     bp++;
577     set_magic = 1;
578     LOG(llevDebug, "%s creates: (%d) (%d) %s\n", op->name, nrof, magic, bp);
579     }
580     if ((cp = strstr(bp, " of ")) != NULL) {
581     *cp = '\0';
582     cp += 4;
583     }
584     for (bp2 = bp; *bp2; bp2++) {
585     if (*bp2 == ' ') {
586     *bp2 = '\0';
587     bp2++;
588     break;
589     }
590     }
591    
592     if ((at = find_archetype(bp)) == NULL) {
593     new_draw_info(NDI_UNIQUE, 0, op, "No such archetype.");
594     return 1;
595     }
596    
597     if (cp) {
598     char spell_name[MAX_BUF], *fsp = NULL;
599    
600     /*
601     * Try to find a spell object for this. Note that
602     * we also set up spell_name which is only
603     * the first word.
604     */
605    
606     at_spell = find_archetype(cp);
607     if (!at_spell || at_spell->clone.type != SPELL)
608     at_spell = find_archetype_by_object_name(cp);
609     if (!at_spell || at_spell->clone.type != SPELL) {
610     strcpy(spell_name, cp);
611     fsp = strchr(spell_name, ' ');
612     if (fsp) {
613     *fsp = 0;
614     fsp++;
615     at_spell = find_archetype(spell_name);
616    
617     /* Got a spell, update the first string pointer */
618     if (at_spell && at_spell->clone.type == SPELL)
619     bp2 = cp+strlen(spell_name)+1;
620     else
621     at_spell = NULL;
622     }
623     }
624    
625     /* OK - we didn't find a spell - presume the 'of'
626     * in this case means its an artifact.
627     */
628     if (!at_spell) {
629     if (find_artifactlist(at->clone.type) == NULL) {
630     new_draw_info_format(NDI_UNIQUE, 0, op,
631     "No artifact list for type %d\n", at->clone.type);
632     } else {
633     art = find_artifactlist(at->clone.type)->items;
634    
635     do {
636     if (!strcmp(art->item->name, cp))
637     break;
638     art = art->next;
639     } while (art != NULL);
640     if (!art) {
641     new_draw_info_format(NDI_UNIQUE, 0, op,
642     "No such artifact ([%d] of %s)", at->clone.type, cp);
643     }
644     }
645     LOG(llevDebug, "%s creates: (%d) (%d) (%s) of (%s)\n", op->name,
646     set_nrof ? nrof : 0, set_magic ? magic : 0, bp, cp);
647     }
648     } /* if cp */
649    
650     if ((at->clone.type == ROD || at->clone.type == WAND || at->clone.type == SCROLL ||
651     at->clone.type == HORN || at->clone.type == SPELLBOOK) && !at_spell) {
652     new_draw_info_format(NDI_UNIQUE, 0, op,
653     "Unable to find spell %s for object that needs it, or it is of wrong type",
654     cp);
655     return 1;
656     }
657    
658     /*
659     * Rather than have two different blocks with a lot of similar code,
660     * just create one object, do all the processing, and then determine
661     * if that one object should be inserted or if we need to make copies.
662     */
663     tmp = arch_to_object(at);
664     if (settings.real_wiz == FALSE)
665     SET_FLAG(tmp, FLAG_WAS_WIZ);
666     if (set_magic)
667     set_abs_magic(tmp, magic);
668     if (art)
669     give_artifact_abilities(tmp, art->item);
670     if (need_identify(tmp)) {
671     SET_FLAG(tmp, FLAG_IDENTIFIED);
672     CLEAR_FLAG(tmp, FLAG_KNOWN_MAGICAL);
673     }
674    
675     /*
676     * This entire block here tries to find variable pairings,
677     * eg, 'hp 4' or the like. The mess here is that values
678     * can be quoted (eg "my cool sword"); So the basic logic
679     * is we want to find two spaces, but if we got a quote,
680     * any spaces there don't count.
681     */
682     while (*bp2 && bp2 <= endline) {
683     bp4 = NULL;
684     gotspace = 0;
685     gotquote = 0;
686     /* find the first quote */
687     for (bp3 = bp2; *bp3 && gotspace < 2 && gotquote < 2; bp3++) {
688    
689     /* Found a quote - now lets find the second one */
690     if (*bp3 == '"') {
691     *bp3 = ' ';
692     bp2 = bp3+1; /* Update start of string */
693     bp3++;
694     gotquote++;
695     while (*bp3) {
696     if (*bp3 == '"') {
697     *bp3 = '\0';
698     gotquote++;
699     } else
700     bp3++;
701     }
702     } else if (*bp3==' ') {
703     gotspace++;
704     }
705     }
706    
707     /*
708     * If we got two spaces, send the second one to null.
709     * if we've reached the end of the line, increase gotspace -
710     * this is perfectly valid for the list entry listed.
711     */
712     if (gotspace == 2 || gotquote == 2) {
713     bp3--; /* Undo the extra increment */
714     *bp3 = '\0';
715     } else if (*bp3=='\0')
716     gotspace++;
717    
718     if ((gotquote && gotquote != 2) || (gotspace != 2 && gotquote != 2)) {
719     /*
720     * Unfortunately, we've clobbered lots of values, so printing
721     * out what we have probably isn't useful. Break out, because
722     * trying to recover is probably won't get anything useful
723     * anyways, and we'd be confused about end of line pointers
724     * anyways.
725     */
726     new_draw_info_format(NDI_UNIQUE, 0, op,
727     "Malformed create line: %s", bp2);
728     break;
729     }
730     /* bp2 should still point to the start of this line,
731     * with bp3 pointing to the end
732     */
733     if (set_variable(tmp, bp2) == -1)
734     new_draw_info_format(NDI_UNIQUE, 0, op,
735     "Unknown variable %s", bp2);
736     else
737     new_draw_info_format(NDI_UNIQUE, 0, op,
738     "(%s#%d)->%s", tmp->name, tmp->count, bp2);
739     bp2 = bp3+1;
740     }
741    
742     if (at->clone.nrof) {
743     if (at_spell)
744     insert_ob_in_ob(arch_to_object(at_spell), tmp);
745    
746     tmp->x = op->x;
747     tmp->y = op->y;
748     if (set_nrof)
749     tmp->nrof = nrof;
750     tmp->map = op->map;
751    
752     tmp = insert_ob_in_ob(tmp, op);
753     esrv_send_item(op, tmp);
754    
755     /* Let's put this created item on stack so dm can access it easily. */
756     dm_stack_push(op->contr, tmp->count);
757    
758     return 1;
759     } else {
760     for (i = 0 ; i < (set_nrof ? nrof : 1); i++) {
761     archetype *atmp;
762     object *prev = NULL, *head = NULL, *dup;
763    
764     for (atmp = at; atmp != NULL; atmp = atmp->more) {
765     dup = arch_to_object(atmp);
766    
767     if (at_spell)
768     insert_ob_in_ob(arch_to_object(at_spell), dup);
769    
770     /*
771     * The head is what contains all the important bits,
772     * so just copying it over should be fine.
773     */
774     if (head == NULL) {
775     head=dup;
776     copy_object(tmp, dup);
777     }
778     if (settings.real_wiz == FALSE)
779     SET_FLAG(dup, FLAG_WAS_WIZ);
780     dup->x = op->x+dup->arch->clone.x;
781     dup->y = op->y+dup->arch->clone.y;
782     dup->map = op->map;
783    
784     if (head != dup) {
785     dup->head = head;
786     prev->more = dup;
787     }
788     prev = dup;
789     }
790    
791     if (QUERY_FLAG(head, FLAG_ALIVE)) {
792     object *check = head;
793     int size_x = 0;
794     int size_y = 0;
795    
796     while (check) {
797     size_x = MAX(size_x, check->arch->clone.x);
798     size_y = MAX(size_y, check->arch->clone.y);
799     check = check->more;
800     }
801    
802     if (out_of_map(op->map, head->x+size_x, head->y+size_y)) {
803     if (head->x < size_x || head->y < size_y) {
804     dm_stack_pop(op->contr);
805     free_object(head);
806     new_draw_info(NDI_UNIQUE, 0, op, "Object too big to insert in map, or wrong position.");
807     free_object(tmp);
808     return 1;
809     }
810    
811     check = head;
812     while (check) {
813     check->x -= size_x;
814     check->y -= size_y;
815     check = check->more;
816     }
817     }
818    
819     insert_ob_in_map(head, op->map, op, 0);
820     } else
821     head = insert_ob_in_ob(head, op);
822    
823     /* Let's put this created item on stack so dm can access it easily. */
824     /* Wonder if we really want to push all of these, but since
825     * things like rods have nrof 0, we want to cover those.
826     */
827     dm_stack_push(op->contr, head->count);
828    
829     if (at->clone.randomitems != NULL && !at_spell)
830     create_treasure(at->clone.randomitems, head, GT_APPLY,
831     op->map->difficulty, 0);
832     esrv_send_item(op, head);
833     }
834    
835     /* free the one we used to copy */
836     free_object(tmp);
837     }
838    
839     return 1;
840     }
841    
842     /*
843     * Now follows dm-commands which are also acceptable from sockets
844     */
845    
846     int command_inventory(object *op, char *params) {
847     object *tmp;
848     int i;
849    
850     if (!params) {
851     inventory(op, NULL);
852     return 0;
853     }
854    
855     if (!sscanf(params, "%d", &i) || (tmp = find_object(i)) == NULL) {
856     new_draw_info(NDI_UNIQUE, 0, op, "Inventory of what object (nr)?");
857     return 1;
858     }
859    
860     inventory(op, tmp);
861     return 1;
862     }
863    
864     /* just show player's their skills for now. Dm's can
865     * already see skills w/ inventory command - b.t.
866     */
867    
868     int command_skills (object *op, char *params) {
869     show_skills(op, params);
870     return 0;
871     }
872    
873     int command_dump (object *op, char *params) {
874     object *tmp;
875    
876     tmp = get_dm_object(op->contr, &params, NULL);
877     if (!tmp)
878     return 1;
879    
880     dump_object(tmp);
881     new_draw_info(NDI_UNIQUE, 0, op, errmsg);
882     if (QUERY_FLAG(tmp, FLAG_OBJ_ORIGINAL))
883     new_draw_info(NDI_UNIQUE, 0, op, "Object is marked original");
884     return 1;
885     }
886    
887     /**
888     * When DM is possessing a monster, flip aggression on and off, to allow
889     * better motion.
890     */
891     int command_mon_aggr(object *op, char *params) {
892     if (op->enemy || !QUERY_FLAG(op, FLAG_UNAGGRESSIVE)) {
893     op->enemy = NULL;
894     SET_FLAG(op, FLAG_UNAGGRESSIVE);
895     new_draw_info(NDI_UNIQUE, 0, op, "Aggression turned OFF");
896     } else {
897     CLEAR_FLAG(op, FLAG_FRIENDLY);
898     CLEAR_FLAG(op, FLAG_UNAGGRESSIVE);
899     new_draw_info(NDI_UNIQUE, 0, op, "Aggression turned ON");
900     }
901    
902     return 1;
903     }
904    
905     /** DM can possess a monster. Basically, this tricks the client into thinking
906     * a given monster, is actually the player it controls. This allows a DM
907     * to inhabit a monster's body, and run around the game with it.
908     * This function is severely broken - it has tons of hardcoded values,
909     */
910     int command_possess(object *op, char *params) {
911     object *victim, *curinv, *nextinv;
912     player *pl;
913     int i;
914     char buf[MAX_BUF];
915    
916     victim = NULL;
917     if (params != NULL) {
918     if (sscanf(params, "%d", &i))
919     victim = find_object(i);
920     else if (sscanf(params, "%s", buf))
921     victim = find_object_name(buf);
922     }
923     if (victim == NULL) {
924     new_draw_info(NDI_UNIQUE, 0, op, "Patch what object (nr)?");
925     return 1;
926     }
927    
928     if (victim == op) {
929     new_draw_info(NDI_UNIQUE, 0, op, "As insane as you are, I cannot "
930     "allow you to possess yourself.");
931     return 1;
932     }
933    
934     /* clear out the old inventory */
935     curinv = op->inv;
936     while (curinv != NULL) {
937     nextinv = curinv->below;
938     esrv_del_item(op->contr, curinv->count);
939     curinv = nextinv;
940     }
941    
942     /* make the switch */
943     pl = op->contr;
944     victim->contr = pl;
945     pl->ob = victim;
946     victim->type = PLAYER;
947     SET_FLAG(victim, FLAG_WIZ);
948    
949     /* send the inventory to the client */
950     curinv = victim->inv;
951     while (curinv != NULL) {
952     nextinv = curinv->below;
953     esrv_send_item(victim, curinv);
954     curinv = nextinv;
955     }
956     /* basic patchup */
957     /* The use of hard coded values is terrible. Note
958     * that really, to be fair, this shouldn't get changed at
959     * all - if you are possessing a kobold, you should have the
960     * same limitations. As it is, as more body locations are added,
961     * this will give this player more locations than perhaps
962     * they should be allowed.
963     */
964     for (i = 0; i < NUM_BODY_LOCATIONS; i++)
965     if (i == 1 || i == 6 || i == 8 || i == 9)
966     victim->body_info[i] = 2;
967     else
968     victim->body_info[i] = 1;
969    
970     esrv_new_player(pl, 80); /* just pick a wieght, we don't care */
971     esrv_send_inventory(victim, victim);
972    
973     fix_player(victim);
974    
975     do_some_living(victim);
976     return 1;
977     }
978    
979     int command_patch(object *op, char *params) {
980     char *arg, *arg2;
981     object *tmp;
982    
983     tmp = get_dm_object(op->contr, &params, NULL);
984     if (!tmp)
985     /* Player already informed of failure */
986     return 1;
987    
988     /* params set to first value by get_dm_default */
989     arg = params;
990     if (arg == NULL) {
991     new_draw_info(NDI_UNIQUE, 0, op, "Patch what values?");
992     return 1;
993     }
994    
995     if ((arg2 = strchr(arg, ' ')))
996     arg2++;
997     if (settings.real_wiz == FALSE)
998     SET_FLAG(tmp, FLAG_WAS_WIZ); /* To avoid cheating */
999     if (set_variable(tmp, arg) == -1)
1000     new_draw_info_format(NDI_UNIQUE, 0, op, "Unknown variable %s", arg);
1001     else {
1002     new_draw_info_format(NDI_UNIQUE, 0, op,
1003     "(%s#%d)->%s=%s", tmp->name, tmp->count, arg, arg2);
1004     }
1005    
1006     return 1;
1007     }
1008    
1009     int command_remove (object *op, char *params) {
1010     object *tmp;
1011     int from;
1012    
1013     tmp = get_dm_object(op->contr, &params, &from);
1014     if (!tmp) {
1015     new_draw_info(NDI_UNIQUE, 0, op, "Remove what object (nr)?");
1016     return 1;
1017     }
1018    
1019     if (tmp->type == PLAYER) {
1020     new_draw_info(NDI_UNIQUE, 0, op, "Unable to remove a player!");
1021     return 1;
1022     }
1023    
1024     if (QUERY_FLAG(tmp, FLAG_REMOVED)) {
1025     new_draw_info_format(NDI_UNIQUE, 0, op, "%s is already removed!",
1026     query_name(tmp));
1027     return 1;
1028     }
1029    
1030     if (from != STACK_FROM_STACK)
1031     /* Item is either stack top, or is a number thus is now stack top, let's remove it */
1032     dm_stack_pop(op->contr);
1033    
1034     /* Always work on the head - otherwise object will get in odd state */
1035     if (tmp->head)
1036     tmp = tmp->head;
1037     remove_ob(tmp);
1038     return 1;
1039     }
1040    
1041     int command_free(object *op, char *params) {
1042     object *tmp;
1043     int from;
1044    
1045     tmp = get_dm_object(op->contr, &params, &from);
1046    
1047     if (!tmp) {
1048     new_draw_info(NDI_UNIQUE, 0, op, "Free what object (nr)?");
1049     return 1;
1050     }
1051    
1052     if (from != STACK_FROM_STACK)
1053     /* Item is either stack top, or is a number thus is now stack top, let's remove it */
1054     dm_stack_pop(op->contr);
1055    
1056     if (!QUERY_FLAG(tmp, FLAG_REMOVED)) {
1057     new_draw_info(NDI_UNIQUE, 0, op, "Warning, item wasn't removed.");
1058     remove_ob(tmp);
1059     }
1060    
1061     if (tmp->head)
1062     tmp = tmp->head;
1063     free_object(tmp);
1064     return 1;
1065     }
1066    
1067     /**
1068     * This adds exp to a player. We now allow adding to a specific skill.
1069     */
1070     int command_addexp(object *op, char *params) {
1071     char buf[MAX_BUF], skill[MAX_BUF];
1072     int i, q;
1073     object *skillob = NULL;
1074     player *pl;
1075    
1076     if (params == NULL || ((q = sscanf(params, "%s %d", buf, &i)) < 2)) {
1077     new_draw_info(NDI_UNIQUE, 0, op, "Usage: addexp [who] [how much].");
1078     return 1;
1079     }
1080    
1081     for (pl = first_player; pl != NULL; pl = pl->next)
1082     if (!strncmp(pl->ob->name, buf, MAX_NAME))
1083     break;
1084    
1085     if (pl == NULL) {
1086     new_draw_info(NDI_UNIQUE, 0, op, "No such player.");
1087     return 1;
1088     }
1089    
1090     if (q >= 3) {
1091     skillob = find_skill_by_name(pl->ob, skill);
1092     if (!skillob) {
1093     new_draw_info_format(NDI_UNIQUE, 0, op, "Unable to find skill %s in %s", skill, buf);
1094     return 1;
1095     }
1096    
1097     i = check_exp_adjust(skillob, i);
1098     skillob->stats.exp += i;
1099     calc_perm_exp(skillob);
1100     player_lvl_adj(pl->ob, skillob);
1101     }
1102    
1103     pl->ob->stats.exp += i;
1104     calc_perm_exp(pl->ob);
1105     player_lvl_adj(pl->ob, NULL);
1106    
1107     if (settings.real_wiz == FALSE)
1108     SET_FLAG(pl->ob, FLAG_WAS_WIZ);
1109     return 1;
1110     }
1111    
1112     int command_speed(object *op, char *params) {
1113     int i;
1114    
1115     if (params == NULL || !sscanf(params, "%d", &i)) {
1116     sprintf(errmsg, "Current speed is %ld", max_time);
1117     new_draw_info(NDI_UNIQUE, 0, op, errmsg);
1118     return 1;
1119     }
1120    
1121     set_max_time(i);
1122     reset_sleep();
1123     new_draw_info(NDI_UNIQUE, 0, op, "The speed is changed.");
1124     return 1;
1125     }
1126    
1127     /**************************************************************************/
1128     /* Mods made by Tyler Van Gorder, May 10-13, 1992. */
1129     /* CSUChico : tvangod@cscihp.ecst.csuchico.edu */
1130     /**************************************************************************/
1131    
1132     int command_stats(object *op, char *params) {
1133     char thing[20];
1134     player *pl;
1135     char buf[MAX_BUF];
1136    
1137     thing[0] = '\0';
1138     if (params == NULL || !sscanf(params, "%s", thing) || thing == NULL) {
1139     new_draw_info(NDI_UNIQUE, 0, op, "Who?");
1140     return 1;
1141     }
1142    
1143     for (pl = first_player; pl != NULL; pl = pl->next)
1144     if (!strcmp(pl->ob->name, thing)) {
1145     sprintf(buf, "Str : %-2d H.P. : %-4d MAX : %d",
1146     pl->ob->stats.Str, pl->ob->stats.hp, pl->ob->stats.maxhp);
1147     new_draw_info(NDI_UNIQUE, 0, op, buf);
1148     sprintf(buf, "Dex : %-2d S.P. : %-4d MAX : %d",
1149     pl->ob->stats.Dex, pl->ob->stats.sp, pl->ob->stats.maxsp);
1150     new_draw_info(NDI_UNIQUE, 0, op, buf);
1151     sprintf(buf, "Con : %-2d AC : %-4d WC : %d",
1152     pl->ob->stats.Con, pl->ob->stats.ac, pl->ob->stats.wc) ;
1153     new_draw_info(NDI_UNIQUE, 0, op, buf);
1154     #ifndef WIN32
1155     sprintf(buf, "Wis : %-2d EXP : %lld",
1156     pl->ob->stats.Wis, pl->ob->stats.exp);
1157     #else
1158     sprintf(buf, "Wis : %-2d EXP : %I64d",
1159     pl->ob->stats.Wis, pl->ob->stats.exp);
1160     #endif
1161     new_draw_info(NDI_UNIQUE, 0, op, buf);
1162     sprintf(buf, "Cha : %-2d Food : %d",
1163     pl->ob->stats.Cha, pl->ob->stats.food);
1164     new_draw_info(NDI_UNIQUE, 0, op, buf);
1165     sprintf(buf, "Int : %-2d Damage : %d",
1166     pl->ob->stats.Int, pl->ob->stats.dam);
1167     sprintf(buf, "Pow : %-2d Grace : %d",
1168     pl->ob->stats.Pow, pl->ob->stats.grace);
1169     new_draw_info(NDI_UNIQUE, 0, op, buf);
1170     break;
1171     }
1172     if (pl == NULL)
1173     new_draw_info(NDI_UNIQUE, 0, op, "No such player.");
1174     return 1;
1175     }
1176    
1177     int command_abil(object *op, char *params) {
1178     char thing[20], thing2[20];
1179     int iii;
1180     player *pl;
1181     char buf[MAX_BUF];
1182    
1183     iii = 0;
1184     thing[0] = '\0';
1185     thing2[0] = '\0';
1186     if (params == NULL || !sscanf(params, "%s %s %d", thing, thing2, &iii) ||
1187     thing==NULL) {
1188     new_draw_info(NDI_UNIQUE, 0, op, "Who?");
1189     return 1;
1190     }
1191    
1192     if (thing2 == NULL){
1193     new_draw_info(NDI_UNIQUE, 0, op, "You can't change that.");
1194     return 1;
1195     }
1196    
1197     if (iii < MIN_STAT || iii > MAX_STAT) {
1198     new_draw_info(NDI_UNIQUE, 0, op, "Illegal range of stat.\n");
1199     return 1;
1200     }
1201    
1202     for (pl = first_player; pl != NULL; pl = pl->next) {
1203     if (!strcmp(pl->ob->name, thing)) {
1204     if (settings.real_wiz == FALSE)
1205     SET_FLAG(pl->ob, FLAG_WAS_WIZ);
1206     if (!strcmp("str", thing2))
1207     pl->ob->stats.Str = iii, pl->orig_stats.Str = iii;
1208     if (!strcmp("dex", thing2))
1209     pl->ob->stats.Dex = iii, pl->orig_stats.Dex = iii;
1210     if (!strcmp("con", thing2))
1211     pl->ob->stats.Con = iii, pl->orig_stats.Con = iii;
1212     if (!strcmp("wis", thing2))
1213     pl->ob->stats.Wis = iii, pl->orig_stats.Wis = iii;
1214     if (!strcmp("cha", thing2))
1215     pl->ob->stats.Cha = iii, pl->orig_stats.Cha = iii;
1216     if (!strcmp("int", thing2))
1217     pl->ob->stats.Int = iii, pl->orig_stats.Int = iii;
1218     if (!strcmp("pow", thing2))
1219     pl->ob->stats.Pow = iii, pl->orig_stats.Pow = iii;
1220     sprintf(buf, "%s has been altered.", pl->ob->name);
1221     new_draw_info(NDI_UNIQUE, 0, op, buf);
1222     fix_player(pl->ob);
1223     return 1;
1224     }
1225     }
1226    
1227     new_draw_info(NDI_UNIQUE, 0, op, "No such player.");
1228     return 1;
1229     }
1230    
1231     int command_reset (object *op, char *params) {
1232     mapstruct *m;
1233     object *dummy = NULL, *tmp = NULL;
1234    
1235     if (params == NULL) {
1236     new_draw_info(NDI_UNIQUE, 0, op, "Reset what map [name]?");
1237     return 1;
1238     }
1239    
1240     if (strcmp(params, ".") == 0)
1241     params = op->map->path;
1242     m = has_been_loaded(params);
1243     if (m == NULL) {
1244     new_draw_info(NDI_UNIQUE, 0, op, "No such map.");
1245     return 1;
1246     }
1247    
1248     if (m->in_memory != MAP_SWAPPED) {
1249     if (m->in_memory != MAP_IN_MEMORY) {
1250     LOG(llevError, "Tried to swap out map which was not in memory.\n");
1251     return 0;
1252     }
1253    
1254     /*
1255     * Only attempt to remove the player that is doing the reset, and not other
1256     * players or wiz's.
1257     */
1258     if (op->map == m) {
1259     dummy = get_object();
1260     dummy->map = NULL;
1261     EXIT_X(dummy) = op->x;
1262     EXIT_Y(dummy) = op->y;
1263     EXIT_PATH(dummy) = add_string(op->map->path);
1264     remove_ob(op);
1265     op->map = NULL;
1266     tmp = op;
1267     }
1268     swap_map(m);
1269     }
1270    
1271     if (m->in_memory == MAP_SWAPPED) {
1272     LOG(llevDebug, "Resetting map %s.\n", m->path);
1273    
1274     /* setting this effectively causes an immediate reload */
1275     m->reset_time = 1;
1276     flush_old_maps();
1277     new_draw_info(NDI_UNIQUE, 0, op, "OK.");
1278     if (tmp) {
1279     enter_exit(tmp, dummy);
1280     free_object(dummy);
1281     }
1282     return 1;
1283     } else {
1284     player *pl;
1285     int playercount = 0;
1286    
1287     /* Need to re-insert player if swap failed for some reason */
1288     if (tmp) {
1289     insert_ob_in_map(op, m, NULL, 0);
1290     free_object(dummy);
1291     }
1292    
1293     new_draw_info(NDI_UNIQUE, 0, op,
1294     "Reset failed, couldn't swap map, the following players are on it:");
1295     for (pl = first_player; pl != NULL; pl = pl->next) {
1296     if (pl->ob->map == m && pl->ob != op) {
1297     new_draw_info_format(NDI_UNIQUE, 0, op, "%s", pl->ob->name);
1298     playercount++;
1299     }
1300     }
1301     if (!playercount)
1302     new_draw_info(NDI_UNIQUE, 0, op,
1303     "hmm, I don't see any other players on this map, something else is the problem.");
1304     return 1;
1305     }
1306     }
1307    
1308     int command_nowiz(object *op, char *params) { /* 'noadm' is alias */
1309     CLEAR_FLAG(op, FLAG_WIZ);
1310     CLEAR_FLAG(op, FLAG_WIZPASS);
1311     CLEAR_FLAG(op, FLAG_WIZCAST);
1312    
1313     if (settings.real_wiz == TRUE)
1314     CLEAR_FLAG(op, FLAG_WAS_WIZ);
1315     if (op->contr->hidden) {
1316     new_draw_info(NDI_UNIQUE, 0, op, "You are no longer hidden from other players");
1317     op->map->players++;
1318     new_draw_info_format(NDI_UNIQUE|NDI_ALL|NDI_DK_ORANGE, 5, NULL,
1319     "%s has entered the game.", op->name);
1320     op->contr->hidden = 0;
1321     op->invisible = 1;
1322     } else
1323     new_draw_info(NDI_UNIQUE|NDI_ALL|NDI_LT_GREEN, 1, NULL, "The Dungeon Master is gone..");
1324     return 1;
1325     }
1326    
1327     /**
1328     * object *op is trying to become dm.
1329     * pl_name is name supplied by player. Restrictive DM will make it harder
1330     * for socket users to become DM - in that case, it will check for the players
1331     * character name.
1332     */
1333     static int checkdm(object *op, const char *pl_name, const char *pl_passwd, const char *pl_host)
1334     {
1335     FILE *dmfile;
1336     char buf[MAX_BUF];
1337     char line_buf[160], name[160], passwd[160], host[160];
1338    
1339     #ifdef RESTRICTIVE_DM
1340     *pl_name = op->name ? op->name : "*";
1341     #endif
1342    
1343     sprintf(buf, "%s/%s", settings.confdir, DMFILE);
1344     if ((dmfile = fopen(buf, "r")) == NULL) {
1345     LOG(llevDebug, "Could not find DM file.\n");
1346     return 0;
1347     }
1348    
1349     while (fgets(line_buf, 160, dmfile) != NULL) {
1350     if (line_buf[0] == '#')
1351     continue;
1352     if (sscanf(line_buf, "%[^:]:%[^:]:%s\n", name, passwd, host) != 3) {
1353     LOG(llevError, "Warning - malformed dm file entry: %s\n", line_buf);
1354     } else if ((!strcmp(name, "*") || (pl_name && !strcmp(pl_name, name)))
1355     && (!strcmp(passwd, "*") || !strcmp(passwd, pl_passwd)) &&
1356     (!strcmp(host, "*") || !strcmp(host, pl_host))) {
1357     fclose(dmfile);
1358     return (1);
1359     }
1360     }
1361     fclose(dmfile);
1362     return (0);
1363     }
1364    
1365     int do_wizard_dm(object *op, char *params, int silent) {
1366     if (!op->contr)
1367     return 0;
1368    
1369     if (QUERY_FLAG(op, FLAG_WIZ)) {
1370     new_draw_info(NDI_UNIQUE, 0, op, "You are already the Dungeon Master!");
1371     return 0;
1372     }
1373    
1374     if (checkdm(op, op->name,
1375     (params ? params : "*"), op->contr->socket.host)) {
1376     SET_FLAG(op, FLAG_WIZ);
1377     SET_FLAG(op, FLAG_WAS_WIZ);
1378     SET_FLAG(op, FLAG_WIZPASS);
1379     SET_FLAG(op, FLAG_WIZCAST);
1380     new_draw_info(NDI_UNIQUE, 0, op, "Ok, you are the Dungeon Master!");
1381     /*
1382     * Remove setting flying here - that won't work, because next
1383     * fix_player() is called that will get cleared - proper solution
1384     * is probably something like a wiz_force which gives that and any
1385     * other desired abilities.
1386     */
1387     clear_los(op);
1388     op->contr->write_buf[0] ='\0';
1389    
1390     if (!silent)
1391     new_draw_info(NDI_UNIQUE|NDI_ALL|NDI_LT_GREEN, 1, NULL,
1392     "The Dungeon Master has arrived!");
1393    
1394     return 1;
1395     } else {
1396     new_draw_info(NDI_UNIQUE, 0, op, "Sorry Pal, I don't think so.");
1397     op->contr->write_buf[0] ='\0';
1398     return 0;
1399     }
1400     }
1401    
1402     /*
1403     * Actual command to perhaps become dm. Changed aroun a bit in version 0.92.2
1404     * - allow people on sockets to become dm, and allow better dm file
1405     */
1406     int command_dm(object *op, char *params) {
1407     if (!op->contr)
1408     return 0;
1409    
1410     do_wizard_dm(op, params, 0);
1411    
1412     return 1;
1413     }
1414    
1415     int command_invisible(object *op, char *params) {
1416     if (op) {
1417     op->invisible += 100;
1418     update_object(op, UP_OBJ_FACE);
1419     new_draw_info(NDI_UNIQUE, 0, op, "You turn invisible.");
1420     }
1421    
1422     return 0;
1423     }
1424    
1425     /**
1426     * Returns spell object (from archetypes) by name.
1427     * Returns NULL if 0 or more than one spell matches.
1428     * Used for wizard's learn spell/prayer.
1429     *
1430     * op is the player issuing the command.
1431     *
1432     * Ignores archetypes "spelldirect_xxx" since these archetypes are not used
1433     * anymore (but may still be present in some player's inventories and thus
1434     * cannot be removed). We have to ignore them here since they have the same
1435     * name than other "spell_xxx" archetypes and would always conflict.
1436     */
1437     static object *get_spell_by_name(object *op, const char *spell_name) {
1438     archetype *ar;
1439     archetype *found;
1440     int conflict_found;
1441     size_t spell_name_length;
1442    
1443     /* First check for full name matches. */
1444     conflict_found = 0;
1445     found = NULL;
1446     for (ar = first_archetype; ar != NULL; ar = ar->next) {
1447     if (ar->clone.type != SPELL)
1448     continue;
1449    
1450     if (strncmp(ar->name, "spelldirect_", 12) == 0)
1451     continue;
1452    
1453     if (strcmp(ar->clone.name, spell_name) != 0)
1454     continue;
1455    
1456     if (found != NULL) {
1457     if (!conflict_found) {
1458     conflict_found = 1;
1459     new_draw_info_format(NDI_UNIQUE, 0, op, "More than one archetype matches the spell name %s:", spell_name);
1460     new_draw_info_format(NDI_UNIQUE, 0, op, "- %s", found->name);
1461     }
1462     new_draw_info_format(NDI_UNIQUE, 0, op, "- %s", ar->name);
1463     continue;
1464     }
1465    
1466     found = ar;
1467     }
1468    
1469     /* No match if more more than one archetype matches. */
1470     if (conflict_found)
1471     return NULL;
1472    
1473     /* Return if exactly one archetype matches. */
1474     if (found != NULL)
1475     return arch_to_object(found);
1476    
1477     /* No full match found: now check for partial matches. */
1478     spell_name_length = strlen(spell_name);
1479     conflict_found = 0;
1480     found = NULL;
1481     for (ar = first_archetype; ar != NULL; ar = ar->next) {
1482     if (ar->clone.type != SPELL)
1483     continue;
1484    
1485     if (strncmp(ar->name, "spelldirect_", 12) == 0)
1486     continue;
1487    
1488     if (strncmp(ar->clone.name, spell_name, spell_name_length) != 0)
1489     continue;
1490    
1491     if (found != NULL) {
1492     if (!conflict_found) {
1493     conflict_found = 1;
1494     new_draw_info_format(NDI_UNIQUE, 0, op, "More than one spell matches %s:", spell_name);
1495     new_draw_info_format(NDI_UNIQUE, 0, op, "- %s", found->clone.name);
1496     }
1497     new_draw_info_format(NDI_UNIQUE, 0, op, "- %s", ar->clone.name);
1498     continue;
1499     }
1500    
1501     found = ar;
1502     }
1503    
1504     /* No match if more more than one archetype matches. */
1505     if (conflict_found)
1506     return NULL;
1507    
1508     /* Return if exactly one archetype matches. */
1509     if (found != NULL)
1510     return arch_to_object(found);
1511    
1512     /* No spell found: just print an error message. */
1513     new_draw_info_format(NDI_UNIQUE, 0, op, "The spell %s does not exist.", spell_name);
1514     return NULL;
1515     }
1516    
1517     static int command_learn_spell_or_prayer(object *op, char *params,
1518     int special_prayer) {
1519     object *tmp;
1520    
1521     if (op->contr == NULL || params == NULL) {
1522     new_draw_info(NDI_UNIQUE, 0, op, "Which spell do you want to learn?");
1523     return 0;
1524     }
1525    
1526     tmp = get_spell_by_name(op, params);
1527     if (tmp == NULL) {
1528     return 0;
1529     }
1530    
1531     if (check_spell_known(op, tmp->name)) {
1532     new_draw_info_format(NDI_UNIQUE, 0, op, "You already know the spell %s.", tmp->name);
1533     return 0;
1534     }
1535    
1536     do_learn_spell(op, tmp, special_prayer);
1537     free_object(tmp);
1538     return 1;
1539     }
1540    
1541     int command_learn_spell(object *op, char *params) {
1542     return command_learn_spell_or_prayer(op, params, 0);
1543     }
1544    
1545     int command_learn_special_prayer(object *op, char *params)
1546     {
1547     return command_learn_spell_or_prayer(op, params, 1);
1548     }
1549    
1550     int command_forget_spell(object *op, char *params)
1551     {
1552     object *spell;
1553    
1554     if (op->contr == NULL || params == NULL) {
1555     new_draw_info(NDI_UNIQUE, 0, op, "Which spell do you want to forget?");
1556     return 0;
1557     }
1558    
1559     spell = lookup_spell_by_name(op, params);
1560     if (spell == NULL) {
1561     new_draw_info_format(NDI_UNIQUE, 0, op, "You do not know the spell %s.", params);
1562     return 0;
1563     }
1564    
1565     do_forget_spell(op, spell->name);
1566     return 1;
1567     }
1568    
1569     /**
1570     * Lists all plugins currently loaded with their IDs and full names.
1571     */
1572     int command_listplugins(object *op, char *params)
1573     {
1574     plugins_display_list(op);
1575     return 1;
1576     }
1577    
1578     /**
1579     * Loads the given plugin. The DM specifies the name of the library to load (no
1580     * pathname is needed). Do not ever attempt to load the same plugin more than
1581     * once at a time, or bad things could happen.
1582     */
1583     int command_loadplugin(object *op, char *params) {
1584     char buf[MAX_BUF];
1585    
1586     if (params == NULL) {
1587     new_draw_info(NDI_UNIQUE, 0, op, "Load which plugin?");
1588     return 1;
1589     }
1590    
1591     strcpy(buf, LIBDIR);
1592     strcat(buf, "/plugins/");
1593     strcat(buf, params);
1594     LOG(llevDebug, "Requested plugin file is %s\n", buf);
1595     if (plugins_init_plugin(buf) == 0)
1596     new_draw_info(NDI_UNIQUE, 0, op, "Plugin successfully loaded.");
1597     else
1598     new_draw_info(NDI_UNIQUE, 0, op, "Could not load plugin.");
1599     return 1;
1600     }
1601    
1602     /**
1603     * Unloads the given plugin. The DM specified the ID of the library to unload.
1604     * Note that some things may behave strangely if the correct plugins are not
1605     * loaded.
1606     */
1607     int command_unloadplugin(object *op, char *params)
1608     {
1609     if (params == NULL) {
1610     new_draw_info(NDI_UNIQUE, 0, op, "Remove which plugin?");
1611     return 1;
1612     }
1613    
1614     if (plugins_remove_plugin(params) == 0)
1615     new_draw_info(NDI_UNIQUE, 0, op, "Plugin successfully removed.");
1616     else
1617     new_draw_info(NDI_UNIQUE, 0, op, "Could not remove plugin.");
1618     return 1;
1619     }
1620    
1621     /**
1622     * A players wants to become DM and hide.
1623     * Let's see if that's authorized.
1624     * Make sure to not tell anything to anyone.
1625     */
1626     int command_dmhide(object *op, char *params) {
1627     if (!do_wizard_dm(op, params, 1))
1628     return 0;
1629    
1630     do_wizard_hide(op, 1);
1631    
1632     return 1;
1633     }
1634    
1635     void dm_stack_pop(player *pl) {
1636     if (!pl->stack_items || !pl->stack_position) {
1637     new_draw_info(NDI_UNIQUE, 0, pl->ob, "Empty stack!");
1638     return;
1639     }
1640    
1641     pl->stack_position--;
1642     new_draw_info_format(NDI_UNIQUE, 0, pl->ob, "Popped item from stack, %d left.", pl->stack_position);
1643     }
1644    
1645     /**
1646     * Get current stack top item for player.
1647     * Returns NULL if no stacked item.
1648     * If stacked item disappeared (freed), remove it.
1649     *
1650     * Ryo, august 2004
1651     */
1652     object *dm_stack_peek(player *pl) {
1653     object* ob;
1654    
1655     if (!pl->stack_position) {
1656     new_draw_info(NDI_UNIQUE, 0, pl->ob, "Empty stack!");
1657     return NULL;
1658     }
1659    
1660     ob = find_object(pl->stack_items[pl->stack_position-1]);
1661     if (!ob) {
1662     new_draw_info(NDI_UNIQUE, 0, pl->ob, "Stacked item was removed!");
1663     dm_stack_pop(pl);
1664     return NULL;
1665     }
1666    
1667     return ob;
1668     }
1669    
1670     /**
1671     * Push specified item on player stack.
1672     * Inform player of position.
1673     * Initializes variables if needed.
1674     */
1675     void dm_stack_push(player *pl, tag_t item) {
1676     if (!pl->stack_items) {
1677     pl->stack_items = (tag_t *)malloc(sizeof(tag_t)*STACK_SIZE);
1678     memset(pl->stack_items, 0, sizeof(tag_t)*STACK_SIZE);
1679     }
1680    
1681     if (pl->stack_position == STACK_SIZE) {
1682     new_draw_info(NDI_UNIQUE, 0, pl->ob, "Item stack full!");
1683     return;
1684     }
1685    
1686     pl->stack_items[pl->stack_position] = item;
1687     new_draw_info_format(NDI_UNIQUE, 0, pl->ob, "Item stacked as %d.", pl->stack_position);
1688     pl->stack_position++;
1689     }
1690    
1691     /**
1692     * Checks 'params' for object code.
1693     *
1694     * Can be:
1695     * * empty => get current object stack top for player
1696     * * number => get item with that tag, stack it for future use
1697     * * $number => get specified stack item
1698     * * "me" => player himself
1699     *
1700     * At function exit, params points to first non-object char
1701     *
1702     * 'from', if not NULL, contains at exit:
1703     * * STACK_FROM_NONE => object not found
1704     * * STACK_FROM_TOP => top item stack, may be NULL if stack was empty
1705     * * STACK_FROM_STACK => item from somewhere in the stack
1706     * * STACK_FROM_NUMBER => item by number, pushed on stack
1707     *
1708     * Ryo, august 2004
1709     */
1710     object *get_dm_object(player *pl, char **params, int *from) {
1711     int item_tag, item_position;
1712     object *ob;
1713    
1714     if (!pl)
1715     return NULL;
1716    
1717     if (!params || !*params || **params == '\0') {
1718     if (from)
1719     *from = STACK_FROM_TOP;
1720     /* No parameter => get stack item */
1721     return dm_stack_peek(pl);
1722     }
1723    
1724     /* Let's clean white spaces */
1725     while (**params == ' ')
1726     (*params)++;
1727    
1728     /* Next case: number => item tag */
1729     if (sscanf(*params, "%d", &item_tag)) {
1730     /* Move parameter to next item */
1731     while (isdigit(**params))
1732     (*params)++;
1733    
1734     /* And skip blanks, too */
1735     while (**params == ' ')
1736     (*params)++;
1737    
1738     /* Get item */
1739     ob = find_object(item_tag);
1740     if (!ob) {
1741     if (from)
1742     *from = STACK_FROM_NONE;
1743     new_draw_info_format(NDI_UNIQUE, 0, pl->ob, "No such item %d!", item_tag);
1744     return NULL;
1745     }
1746    
1747     /* Got one, let's push it on stack */
1748     dm_stack_push(pl, item_tag);
1749     if (from)
1750     *from = STACK_FROM_NUMBER;
1751     return ob;
1752     }
1753    
1754     /* Next case: $number => stack item */
1755     if (sscanf(*params, "$%d", &item_position)) {
1756     /* Move parameter to next item */
1757     (*params)++;
1758    
1759     while (isdigit(**params))
1760     (*params)++;
1761     while (**params == ' ')
1762     (*params)++;
1763    
1764     if (item_position >= pl->stack_position) {
1765     if (from)
1766     *from = STACK_FROM_NONE;
1767     new_draw_info_format(NDI_UNIQUE, 0, pl->ob, "No such stack item %d!", item_position);
1768     return NULL;
1769     }
1770    
1771     ob = find_object(pl->stack_items[item_position]);
1772     if (!ob) {
1773     if (from)
1774     *from = STACK_FROM_NONE;
1775     new_draw_info_format(NDI_UNIQUE, 0, pl->ob, "Stack item %d was removed.", item_position);
1776     return NULL;
1777     }
1778    
1779     if (from)
1780     *from = item_position < pl->stack_position-1 ? STACK_FROM_STACK : STACK_FROM_TOP;
1781     return ob;
1782     }
1783    
1784     /* Next case: 'me' => return pl->ob */
1785     if (!strncmp(*params, "me", 2)) {
1786     if (from)
1787     *from = STACK_FROM_NUMBER;
1788     dm_stack_push(pl, pl->ob->count);
1789    
1790     /* Skip to next token */
1791     (*params) += 2;
1792     while (**params == ' ')
1793     (*params)++;
1794    
1795     return pl->ob;
1796     }
1797    
1798     /* Last case: get stack top */
1799     if (from)
1800     *from = STACK_FROM_TOP;
1801     return dm_stack_peek(pl);
1802     }
1803    
1804     /**
1805     * Pop the stack top.
1806     */
1807     int command_stack_pop(object *op, char *params) {
1808     dm_stack_pop(op->contr);
1809     return 0;
1810     }
1811    
1812     /**
1813     * Push specified item on stack.
1814     */
1815     int command_stack_push(object *op, char *params) {
1816     object *ob;
1817     int from;
1818     ob = get_dm_object(op->contr, &params, &from);
1819    
1820     if (ob && from != STACK_FROM_NUMBER)
1821     /* Object was from stack, need to push it again */
1822     dm_stack_push(op->contr, ob->count);
1823    
1824     return 0;
1825     }
1826    
1827     /**
1828     * Displays stack contents.
1829     */
1830     int command_stack_list(object *op, char *params) {
1831     int item;
1832     object *display;
1833     player *pl = op->contr;
1834    
1835     new_draw_info(NDI_UNIQUE, 0, op, "Item stack contents:");
1836    
1837     for (item = 0; item < pl->stack_position; item++) {
1838     display = find_object(pl->stack_items[item]);
1839     if (display)
1840     new_draw_info_format(NDI_UNIQUE, 0, op, " %d : %s [%d]", item, display->name, display->count);
1841     else
1842     /* Item was freed */
1843     new_draw_info_format(NDI_UNIQUE, 0, op, " %d : (lost item: %d)", item, pl->stack_items[item]);
1844     }
1845    
1846     return 0;
1847     }
1848    
1849     /**
1850     * Empty DM item stack.
1851     */
1852     int command_stack_clear(object *op, char *params) {
1853     op->contr->stack_position = 0;
1854     new_draw_info(NDI_UNIQUE, 0, op, "Item stack cleared.");
1855     return 0;
1856     }
1857    
1858     /**
1859     * Get a diff of specified items.
1860     * Second item is compared to first, and differences displayed.
1861     * Note: get_ob_diff works the opposite way (first compared to 2nd),
1862     * but it's easier with stack functions to do it this way, so you can do:
1863     * * stack_push <base>
1864     * * stack_push <object to be compared>
1865     * * diff
1866     * * patch xxx <---- applies to object compared to base, easier :)
1867     *
1868     * Ryo, august 2004
1869     */
1870     int command_diff(object *op, char *params) {
1871     object *left, *right, *top;
1872     const char *diff;
1873     int left_from, right_from;
1874    
1875     top = NULL;
1876    
1877     left = get_dm_object(op->contr, &params, &left_from);
1878     if (!left) {
1879     new_draw_info(NDI_UNIQUE, 0, op, "Compare to what item?");
1880     return 0;
1881     }
1882    
1883     if (left_from == STACK_FROM_NUMBER)
1884     /* Item was stacked, remove it else right will be the same... */
1885     dm_stack_pop(op->contr);
1886    
1887     right = get_dm_object(op->contr, &params, &right_from);
1888    
1889     if (!right) {
1890     new_draw_info(NDI_UNIQUE, 0, op, "Compare what item?");
1891     return 0;
1892     }
1893    
1894     new_draw_info(NDI_UNIQUE, 0, op, "Item difference:");
1895    
1896     if (left_from == STACK_FROM_TOP && right_from == STACK_FROM_TOP) {
1897     /*
1898     * Special case: both items were taken from stack top.
1899     * Override the behaviour, taking left as item just below top, if exists.
1900     * See function description for why.
1901     * Besides, if we don't do anything, compare an item to itself, not really useful.
1902     */
1903     if (op->contr->stack_position > 1) {
1904     left = find_object(op->contr->stack_items[op->contr->stack_position-2]);
1905     if (left)
1906     new_draw_info(NDI_UNIQUE, 0, op, "(Note: first item taken from undertop)");
1907     else
1908     /* Stupid case: item under top was freed, fallback to stack top */
1909     left = right;
1910     }
1911     }
1912    
1913     diff = get_ob_diff(left, right);
1914    
1915     if (!diff) {
1916     new_draw_info(NDI_UNIQUE, 0, op, "Objects are the same.");
1917     return 0;
1918     }
1919    
1920     new_draw_info(NDI_UNIQUE, 0, op, diff);
1921     return 0;
1922     }