ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.c
Revision: 1.2
Committed: Thu May 11 15:43:17 2006 UTC (18 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.1: +100 -9 lines
Log Message:
fix player saving on kick command (mostly for shutdown)

File Contents

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