ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.C
Revision: 1.52
Committed: Sun Sep 30 20:22:22 2007 UTC (16 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_3
Changes since 1.51: +5 -10 lines
Log Message:
- clean up message system, combine all boxes into one.
- suppress too long messages (we need a more robust solution to this problem).
- get rid of INS_MAP_LOAD, leading to slightly cleaner/faster code
  and certainly one special case less.
- insert objects manually at load time, this is both faster and also more
  correct, as loading a map is never supposed to trigger anything (and
  also for symmetry to the save code).

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