ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.C
Revision: 1.48
Committed: Tue Jun 5 13:05:02 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.47: +1 -2 lines
Log Message:
- improve observe
- remove more cruft code
- archetype loading almost works again, reloading probbaly still borked.

File Contents

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