ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.C
Revision: 1.27
Committed: Sat Dec 23 13:56:25 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.26: +54 -63 lines
Log Message:
an experiment

File Contents

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