ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.C
Revision: 1.57
Committed: Sun May 4 15:30:40 2008 UTC (16 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_53
Changes since 1.56: +1 -1 lines
Log Message:
fix arrest

File Contents

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