ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.c
Revision: 1.1.1.2 (vendor branch)
Committed: Wed Feb 22 18:03:20 2006 UTC (18 years, 3 months ago) by elmex
Content type: text/plain
Branch: UPSTREAM
CVS Tags: UPSTREAM_2006_03_15, UPSTREAM_2006_02_22
Changes since 1.1.1.1: +98 -6 lines
Log Message:
cvs -z7 -d:ext:elmex@cvs.schmorp.de:/schmorpforge import cf.schmorp.de UPSTREAM UPSTREAM_2006_02_22

File Contents

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