ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.C
Revision: 1.54
Committed: Tue Apr 22 07:01:47 2008 UTC (16 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_52
Changes since 1.53: +8 -16 lines
Log Message:
see Changes

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Deliantra 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 *
13 * 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 *
18 * 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 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
23
24 #include <global.h>
25 #include <sproto.h>
26 #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
33 /* 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 static player *
48 get_other_player_from_name (object *op, char *name)
49 {
50 if (!name)
51 return NULL;
52
53 for_all_players (pl)
54 if (!strncmp (pl->ob->name, name, MAX_NAME))
55 {
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
62 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
68 return pl;
69 }
70
71 new_draw_info (NDI_UNIQUE, 0, op, "No such player.");
72 return 0;
73 }
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 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
91 if (!silent_dm)
92 new_draw_info (NDI_UNIQUE | NDI_ALL | NDI_LT_GREEN, 1, NULL, "The Dungeon Master has arrived!");
93 }
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
100 if (!silent_dm)
101 new_draw_info (NDI_UNIQUE | NDI_ALL | NDI_LT_GREEN, 1, NULL, "The Dungeon Master is gone..");
102
103 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 }
106 }
107
108 int
109 command_hide (object *op, char *params)
110 {
111 do_wizard_hide (op, 0);
112 return 1;
113 }
114
115 /**
116 * This finds and returns the object which matches the name or
117 * object nubmer (specified via num #whatever).
118 */
119 static object *
120 find_object_both (char *params)
121 {
122 if (!params)
123 return NULL;
124
125 if (params[0] == '#')
126 return find_object (atol (params + 1));
127 else
128 return find_object_name (params);
129 }
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 int
136 command_setgod (object *op, char *params)
137 {
138 object *ob, *god;
139 char *str;
140
141 if (!params || !(str = strchr (params, ' ')))
142 {
143 new_draw_info (NDI_UNIQUE, 0, op, "Usage: setgod object god");
144 return 0;
145 }
146
147 /* 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 }
154
155 /*
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 }
164
165 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 }
171
172 become_follower (ob, god);
173 return 1;
174 }
175
176 // 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 //TODO
198 #if 0
199 int
200 command_save_overlay (object *op, char *params)
201 {
202 if (!op)
203 return 0;
204
205 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
217 return 1;
218 }
219 #endif
220
221 int
222 command_freeze (object *op, char *params)
223 {
224 int ticks;
225 player *pl;
226
227 if (!params)
228 {
229 new_draw_info (NDI_UNIQUE, 0, op, "Usage: freeze [ticks] <player>.");
230 return 1;
231 }
232
233 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 }
243 }
244 else
245 ticks = 100;
246
247 pl = get_other_player_from_name (op, params);
248 if (!pl)
249 return 1;
250
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 }
256
257 int
258 command_arrest (object *op, char *params)
259 {
260 object *dummy;
261 player *pl;
262
263 if (!op)
264 return 0;
265
266 if (params == NULL)
267 {
268 new_draw_info (NDI_UNIQUE, 0, op, "Usage: arrest <player>.");
269 return 1;
270 }
271
272 pl = get_other_player_from_name (op, params);
273 if (!pl)
274 return 1;
275
276 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
284 pl->ob->enter_exit (dummy);
285 dummy->destroy ();
286 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 }
291
292 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 return 1;
311
312 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 dummy = object::create ();
320 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 pl->ob->enter_exit (dummy);
324 dummy->destroy ();
325 new_draw_info (NDI_UNIQUE, 0, pl->ob, "You are summoned.");
326 new_draw_info (NDI_UNIQUE, 0, op, "OK.");
327
328 return 1;
329 }
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 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
364 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
381 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
389 bp++;
390 set_magic = 1;
391 LOG (llevDebug, "%s creates: (%d) (%d) %s\n", &op->name, nrof, magic, bp);
392 }
393
394 if ((cp = strstr (bp, " of ")) != NULL)
395 {
396 *cp = '\0';
397 cp += 4;
398 }
399
400 for (bp2 = bp; *bp2; bp2++)
401 {
402 if (*bp2 == ' ')
403 {
404 *bp2 = '\0';
405 bp2++;
406 break;
407 }
408 }
409
410 if ((at = archetype::find (bp)) == NULL)
411 {
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 at_spell = archetype::find (cp);
427 if (!at_spell || at_spell->type != SPELL)
428 at_spell = find_archetype_by_object_name (cp);
429 if (!at_spell || at_spell->type != SPELL)
430 {
431 assign (spell_name, cp);
432 fsp = strchr (spell_name, ' ');
433 if (fsp)
434 {
435 *fsp = 0;
436 fsp++;
437 at_spell = archetype::find (spell_name);
438
439 /* Got a spell, update the first string pointer */
440 if (at_spell && at_spell->type == SPELL)
441 bp2 = cp + strlen (spell_name) + 1;
442 else
443 at_spell = NULL;
444 }
445 }
446
447 /* 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 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 else
455 {
456 art = find_artifactlist (at->type)->items;
457
458 while (art)
459 {
460 if (!strcmp (art->item->name, cp))
461 break;
462
463 art = art->next;
464 }
465
466 if (!art)
467 new_draw_info_format (NDI_UNIQUE, 0, op, "No such artifact ([%d] of %s)", at->type, cp);
468 }
469
470 LOG (llevDebug, "%s creates: (%d) (%d) (%s) of (%s)\n", &op->name, set_nrof ? nrof : 0, set_magic ? magic : 0, bp, cp);
471 }
472 } /* if cp */
473
474 if ((at->type == ROD || at->type == WAND || at->type == SCROLL ||
475 at->type == HORN || at->type == SPELLBOOK) && !at_spell)
476 {
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
481 /*
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
488 if (set_magic)
489 set_abs_magic (tmp, magic);
490
491 if (art)
492 give_artifact_abilities (tmp, art->item);
493
494 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
513 /* 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 }
534 }
535 else if (*bp3 == ' ')
536 gotspace++;
537 }
538
539 /*
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
565 /* 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
573 bp2 = bp3 + 1;
574 }
575
576 if (at->nrof)
577 {
578 if (at_spell)
579 tmp->insert (arch_to_object (at_spell));
580
581 tmp->x = op->x;
582 tmp->y = op->y;
583 tmp->map = op->map;
584
585 if (set_nrof)
586 tmp->nrof = nrof;
587
588 op->insert (tmp);
589
590 /* Let's put this created item on stack so dm can access it easily. */
591 dm_stack_push (op->contr, tmp->count);
592
593 return 1;
594 }
595 else
596 {
597 for (i = 0; i < (set_nrof ? nrof : 1); i++)
598 {
599 object *prev = 0, *head = 0;
600
601 for (archetype *atmp = at; atmp; atmp = (archetype *)atmp->more)
602 {
603 object *dup = arch_to_object (atmp);
604
605 if (at_spell)
606 insert_ob_in_ob (arch_to_object (at_spell), dup);
607
608 /*
609 * The head is what contains all the important bits,
610 * so just copying it over should be fine.
611 */
612 if (!head)
613 {
614 head = dup;
615 tmp->copy_to (dup);
616 }
617
618 dup->x = op->x + dup->arch->x;
619 dup->y = op->y + dup->arch->y;
620 dup->map = op->map;
621
622 if (head != dup)
623 {
624 dup->head = head;
625 prev->more = dup;
626 }
627
628 prev = dup;
629 }
630
631 if (QUERY_FLAG (head, FLAG_ALIVE))
632 {
633 object *check = head;
634 int size_x = 0;
635 int size_y = 0;
636
637 while (check)
638 {
639 size_x = MAX (size_x, check->arch->x);
640 size_y = MAX (size_y, check->arch->y);
641 check = check->more;
642 }
643
644 if (out_of_map (op->map, head->x + size_x, head->y + size_y))
645 {
646 if (head->x < size_x || head->y < size_y)
647 {
648 dm_stack_pop (op->contr);
649 head->destroy ();
650 new_draw_info (NDI_UNIQUE, 0, op, "Object too big to insert in map, or wrong position.");
651 tmp->destroy ();
652 return 1;
653 }
654
655 check = head;
656
657 while (check)
658 {
659 check->x -= size_x;
660 check->y -= size_y;
661 check = check->more;
662 }
663 }
664
665 insert_ob_in_map (head, op->map, op, 0);
666 }
667 else
668 head = insert_ob_in_ob (head, op);
669
670 /* Let's put this created item on stack so dm can access it easily. */
671 /* Wonder if we really want to push all of these, but since
672 * things like rods have nrof 0, we want to cover those.
673 */
674 dm_stack_push (op->contr, head->count);
675
676 if (at->randomitems && !at_spell)
677 create_treasure (at->randomitems, head, GT_APPLY, op->map->difficulty, 0);
678 }
679
680 /* free the one we used to copy */
681 tmp->destroy ();
682 }
683
684 return 1;
685 }
686
687 /*
688 * Now follows dm-commands which are also acceptable from sockets
689 */
690
691 int
692 command_inventory (object *op, char *params)
693 {
694 int i;
695 object *tmp;
696
697 if (!params || !sscanf (params, "%d", &i) || !(tmp = find_object (i)))
698 {
699 op->contr->failmsg ("Inventory of what object (nr)?");
700 return 1;
701 }
702
703 op->contr->infobox (MSG_CHANNEL ("examine"), tmp->query_inventory (op));
704
705 return 1;
706 }
707
708 /* just show player's their skills for now. Dm's can
709 * already see skills w/ inventory command - b.t.
710 */
711
712 int
713 command_skills (object *op, char *params)
714 {
715 show_skills (op, params);
716 return 0;
717 }
718
719 int
720 command_dump (object *op, char *params)
721 {
722 object *tmp;
723
724 tmp = get_dm_object (op->contr, &params, NULL);
725 if (!tmp)
726 return 1;
727
728 char *dump = dump_object (tmp);
729 new_draw_info (NDI_UNIQUE, 0, op, dump);
730 free (dump);
731
732 if (QUERY_FLAG (tmp, FLAG_OBJ_ORIGINAL))
733 new_draw_info (NDI_UNIQUE, 0, op, "Object is marked original");
734
735 return 1;
736 }
737
738 int
739 command_patch (object *op, char *params)
740 {
741 char *arg, *arg2;
742 object *tmp;
743
744 tmp = get_dm_object (op->contr, &params, NULL);
745 if (!tmp)
746 /* Player already informed of failure */
747 return 1;
748
749 /* params set to first value by get_dm_default */
750 arg = params;
751 if (arg == NULL)
752 {
753 new_draw_info (NDI_UNIQUE, 0, op, "Patch what values?");
754 return 1;
755 }
756
757 if ((arg2 = strchr (arg, ' ')))
758 arg2++;
759
760 if (set_variable (tmp, arg) == -1)
761 new_draw_info_format (NDI_UNIQUE, 0, op, "Unknown variable %s", arg);
762 else
763 new_draw_info_format (NDI_UNIQUE, 0, op, "(%s#%d)->%s=%s", &tmp->name, tmp->count, arg, arg2);
764
765 return 1;
766 }
767
768 int
769 command_remove (object *op, char *params)
770 {
771 object *tmp;
772 int from;
773
774 tmp = get_dm_object (op->contr, &params, &from);
775 if (!tmp)
776 {
777 new_draw_info (NDI_UNIQUE, 0, op, "Remove what object (nr)?");
778 return 1;
779 }
780
781 if (tmp->type == PLAYER)
782 {
783 new_draw_info (NDI_UNIQUE, 0, op, "Unable to remove a player!");
784 return 1;
785 }
786
787 if (QUERY_FLAG (tmp, FLAG_REMOVED))
788 {
789 new_draw_info_format (NDI_UNIQUE, 0, op, "%s is already removed!", query_name (tmp));
790 return 1;
791 }
792
793 if (from != STACK_FROM_STACK)
794 /* Item is either stack top, or is a number thus is now stack top, let's remove it */
795 dm_stack_pop (op->contr);
796
797 /* Always work on the head - otherwise object will get in odd state */
798 if (tmp->head)
799 tmp = tmp->head;
800 tmp->remove ();
801 return 1;
802 }
803
804 int
805 command_free (object *op, char *params)
806 {
807 object *tmp;
808 int from;
809
810 tmp = get_dm_object (op->contr, &params, &from);
811
812 if (!tmp)
813 {
814 new_draw_info (NDI_UNIQUE, 0, op, "Free what object (nr)?");
815 return 1;
816 }
817
818 if (from != STACK_FROM_STACK)
819 /* Item is either stack top, or is a number thus is now stack top, let's remove it */
820 dm_stack_pop (op->contr);
821
822 if (tmp->head)
823 tmp = tmp->head;
824
825 tmp->destroy ();
826 return 1;
827 }
828
829 /**
830 * This adds exp to a player. We now allow adding to a specific skill.
831 */
832 int
833 command_addexp (object *op, char *params)
834 {
835 char buf[MAX_BUF], skill[MAX_BUF];
836 int q;
837 long long i; // use sint64 and finally provide format specifiers for sint64 etc. via configure
838 object *skillob = NULL;
839
840 skill[0] = '\0';
841 if ((params == NULL) || (strlen (params) > MAX_BUF) || ((q = sscanf (params, "%s %lld %s", buf, &i, skill)) < 2))
842 {
843 new_draw_info (NDI_UNIQUE, 0, op, "Usage: addexp <who> <how much> [<skill>].");
844 return 1;
845 }
846
847 for_all_players (pl)
848 if (!strncmp (pl->ob->name, buf, MAX_NAME))
849 {
850 if (q >= 3)
851 {
852 skillob = find_skill_by_name (pl->ob, skill);
853 if (!skillob)
854 {
855 new_draw_info_format (NDI_UNIQUE, 0, op, "Unable to find skill %s in %s", skill, buf);
856 return 1;
857 }
858
859 i = check_exp_adjust (skillob, i);
860 skillob->stats.exp += i;
861 calc_perm_exp (skillob);
862 player_lvl_adj (pl->ob, skillob);
863 }
864
865 pl->ob->stats.exp += i;
866 calc_perm_exp (pl->ob);
867 player_lvl_adj (pl->ob, NULL);
868
869 return 1;
870 }
871
872 new_draw_info (NDI_UNIQUE, 0, op, "No such player.");
873 return 1;
874 }
875
876 /**************************************************************************/
877
878 /* Mods made by Tyler Van Gorder, May 10-13, 1992. */
879
880 /* CSUChico : tvangod@cscihp.ecst.csuchico.edu */
881
882 /**************************************************************************/
883
884 int
885 command_stats (object *op, char *params)
886 {
887 char thing[20];
888 char buf[MAX_BUF];
889
890 thing[0] = '\0';
891 if (params == NULL || !sscanf (params, "%s", thing) || thing == NULL)
892 {
893 new_draw_info (NDI_UNIQUE, 0, op, "Who?");
894 return 1;
895 }
896
897 for_all_players (pl)
898 if (!strcmp (pl->ob->name, thing))
899 {
900 sprintf (buf, "Str : %-2d H.P. : %-4d MAX : %d", pl->ob->stats.Str, pl->ob->stats.hp, pl->ob->stats.maxhp);
901 new_draw_info (NDI_UNIQUE, 0, op, buf);
902 sprintf (buf, "Dex : %-2d S.P. : %-4d MAX : %d", pl->ob->stats.Dex, pl->ob->stats.sp, pl->ob->stats.maxsp);
903 new_draw_info (NDI_UNIQUE, 0, op, buf);
904 sprintf (buf, "Con : %-2d AC : %-4d WC : %d", pl->ob->stats.Con, pl->ob->stats.ac, pl->ob->stats.wc);
905 new_draw_info (NDI_UNIQUE, 0, op, buf);
906 sprintf (buf, "Int : %-2d Damage : %d", pl->ob->stats.Int, pl->ob->stats.dam);
907 new_draw_info (NDI_UNIQUE, 0, op, buf);
908 sprintf (buf, "Wis : %-2d EXP : %" PRId64, pl->ob->stats.Wis, pl->ob->stats.exp);
909 new_draw_info (NDI_UNIQUE, 0, op, buf);
910 sprintf (buf, "Pow : %-2d Grace : %d", pl->ob->stats.Pow, pl->ob->stats.grace);
911 new_draw_info (NDI_UNIQUE, 0, op, buf);
912 sprintf (buf, "Cha : %-2d Food : %d", pl->ob->stats.Cha, pl->ob->stats.food);
913 new_draw_info (NDI_UNIQUE, 0, op, buf);
914 return 1;
915 }
916
917 new_draw_info (NDI_UNIQUE, 0, op, "No such player.");
918 return 1;
919 }
920
921 int
922 command_abil (object *op, char *params)
923 {
924 char thing[20], thing2[20];
925 int iii;
926 char buf[MAX_BUF];
927
928 iii = 0;
929 thing[0] = '\0';
930 thing2[0] = '\0';
931 if (params == NULL || !sscanf (params, "%s %s %d", thing, thing2, &iii) || thing == NULL)
932 {
933 new_draw_info (NDI_UNIQUE, 0, op, "Who?");
934 return 1;
935 }
936
937 if (thing2 == NULL)
938 {
939 new_draw_info (NDI_UNIQUE, 0, op, "You can't change that.");
940 return 1;
941 }
942
943 if (iii < MIN_STAT || iii > MAX_STAT)
944 {
945 new_draw_info (NDI_UNIQUE, 0, op, "Illegal range of stat.\n");
946 return 1;
947 }
948
949 for_all_players (pl)
950 {
951 if (!strcmp (pl->ob->name, thing))
952 {
953 if (!strcmp ("str", thing2)) pl->ob->stats.Str = iii, pl->orig_stats.Str = iii;
954 if (!strcmp ("dex", thing2)) pl->ob->stats.Dex = iii, pl->orig_stats.Dex = iii;
955 if (!strcmp ("con", thing2)) pl->ob->stats.Con = iii, pl->orig_stats.Con = iii;
956 if (!strcmp ("wis", thing2)) pl->ob->stats.Wis = iii, pl->orig_stats.Wis = iii;
957 if (!strcmp ("cha", thing2)) pl->ob->stats.Cha = iii, pl->orig_stats.Cha = iii;
958 if (!strcmp ("int", thing2)) pl->ob->stats.Int = iii, pl->orig_stats.Int = iii;
959 if (!strcmp ("pow", thing2)) pl->ob->stats.Pow = iii, pl->orig_stats.Pow = iii;
960
961 sprintf (buf, "%s has been altered.", &pl->ob->name);
962 new_draw_info (NDI_UNIQUE, 0, op, buf);
963 pl->ob->update_stats ();
964 return 1;
965 }
966 }
967
968 new_draw_info (NDI_UNIQUE, 0, op, "No such player.");
969 return 1;
970 }
971
972 int
973 command_nowiz (object *op, char *params)
974 { /* 'nodm' is alias */
975 CLEAR_FLAG (op, FLAG_WIZ);
976 CLEAR_FLAG (op, FLAG_WIZPASS);
977 CLEAR_FLAG (op, FLAG_WIZCAST);
978
979 if (op->contr->hidden)
980 {
981 new_draw_info (NDI_UNIQUE, 0, op, "You are no longer hidden from other players");
982 op->map->players++;
983 new_draw_info_format (NDI_UNIQUE | NDI_ALL | NDI_DK_ORANGE, 5, NULL, "%s has entered the game.", &op->name);
984 op->contr->hidden = 0;
985 op->invisible = 1;
986 }
987 else
988 new_draw_info (NDI_UNIQUE | NDI_ALL | NDI_LT_GREEN, 1, NULL, "The Dungeon Master is gone..");
989 return 1;
990 }
991
992 /**
993 * object *op is trying to become dm.
994 * pl_name is name supplied by player. Restrictive DM will make it harder
995 * for socket users to become DM - in that case, it will check for the players
996 * character name.
997 */
998 static int
999 checkdm (object *op, const char *pl_name, const char *pl_passwd, const char *pl_host)
1000 {
1001 FILE *dmfile;
1002 char buf[MAX_BUF];
1003 char line_buf[160], name[160], passwd[160], host[160];
1004
1005 #ifdef RESTRICTIVE_DM
1006 *pl_name = op->name ? op->name : "*";
1007 #endif
1008
1009 sprintf (buf, "%s/%s", settings.confdir, DMFILE);
1010 if ((dmfile = fopen (buf, "r")) == NULL)
1011 {
1012 LOG (llevDebug, "Could not find DM file.\n");
1013 return 0;
1014 }
1015
1016 while (fgets (line_buf, 160, dmfile) != NULL)
1017 {
1018 if (line_buf[0] == '#')
1019 continue;
1020 if (sscanf (line_buf, "%[^:]:%[^:]:%s\n", name, passwd, host) != 3)
1021 {
1022 LOG (llevError, "Warning - malformed dm file entry: %s\n", line_buf);
1023 }
1024 else if ((!strcmp (name, "*") || (pl_name && !strcmp (pl_name, name)))
1025 && (!strcmp (passwd, "*") || !strcmp (passwd, pl_passwd)) && (!strcmp (host, "*") || !strcmp (host, pl_host)))
1026 {
1027 fclose (dmfile);
1028 return (1);
1029 }
1030 }
1031 fclose (dmfile);
1032 return (0);
1033 }
1034
1035 int
1036 do_wizard_dm (object *op, char *params, int silent)
1037 {
1038 if (!op->contr)
1039 return 0;
1040
1041 if (QUERY_FLAG (op, FLAG_WIZ))
1042 {
1043 new_draw_info (NDI_UNIQUE, 0, op, "You are already the Dungeon Master!");
1044 return 0;
1045 }
1046
1047 if (checkdm (op, op->name, (params ? params : "*"), op->contr->ns->host))
1048 {
1049 SET_FLAG (op, FLAG_WIZ);
1050 SET_FLAG (op, FLAG_WIZPASS);
1051 SET_FLAG (op, FLAG_WIZCAST);
1052
1053 new_draw_info (NDI_UNIQUE, 0, op, "Ok, you are the Dungeon Master!");
1054 /*
1055 * Remove setting flying here - that won't work, because next
1056 * fix_player() is called that will get cleared - proper solution
1057 * is probably something like a wiz_force which gives that and any
1058 * other desired abilities.
1059 */
1060 clear_los (op->contr);
1061 op->contr->write_buf[0] = '\0';
1062
1063 if (!silent)
1064 new_draw_info (NDI_UNIQUE | NDI_ALL | NDI_LT_GREEN, 1, NULL, "The Dungeon Master has arrived!");
1065
1066 return 1;
1067 }
1068 else
1069 {
1070 new_draw_info (NDI_UNIQUE, 0, op, "Sorry Pal, I don't think so.");
1071 op->contr->write_buf[0] = '\0';
1072 return 0;
1073 }
1074 }
1075
1076 /*
1077 * Actual command to perhaps become dm. Changed aroun a bit in version 0.92.2
1078 * - allow people on sockets to become dm, and allow better dm file
1079 */
1080 int
1081 command_dm (object *op, char *params)
1082 {
1083 if (!op->contr)
1084 return 0;
1085
1086 do_wizard_dm (op, params, 0);
1087
1088 return 1;
1089 }
1090
1091 int
1092 command_invisible (object *op, char *params)
1093 {
1094 if (op)
1095 {
1096 op->invisible += 100;
1097 update_object (op, UP_OBJ_CHANGE);
1098 new_draw_info (NDI_UNIQUE, 0, op, "You turn invisible.");
1099 }
1100
1101 return 0;
1102 }
1103
1104 /**
1105 * Returns spell object (from archetypes) by name.
1106 * Returns NULL if 0 or more than one spell matches.
1107 * Used for wizard's learn spell/prayer.
1108 *
1109 * op is the player issuing the command.
1110 *
1111 * Ignores archetypes "spelldirect_xxx" since these archetypes are not used
1112 * anymore (but may still be present in some player's inventories and thus
1113 * cannot be removed). We have to ignore them here since they have the same
1114 * name than other "spell_xxx" archetypes and would always conflict.
1115 */
1116 static object *
1117 get_spell_by_name (object *op, const char *spell_name)
1118 {
1119 archetype *at;
1120 archetype *found;
1121 int conflict_found;
1122 size_t spell_name_length;
1123
1124 /* First check for full name matches. */
1125 conflict_found = 0;
1126 found = NULL;
1127 for_all_archetypes (at)
1128 {
1129 if (at->type != SPELL)
1130 continue;
1131
1132 if (strncmp (at->archname, "spelldirect_", 12) == 0)
1133 continue;
1134
1135 if (strcmp (at->object::name, spell_name) != 0)
1136 continue;
1137
1138 if (found != NULL)
1139 {
1140 if (!conflict_found)
1141 {
1142 conflict_found = 1;
1143 new_draw_info_format (NDI_UNIQUE, 0, op, "More than one archetype matches the spell name %s:", spell_name);
1144 new_draw_info_format (NDI_UNIQUE, 0, op, "- %s", &found->archname);
1145 }
1146
1147 new_draw_info_format (NDI_UNIQUE, 0, op, "- %s", &at->archname);
1148 continue;
1149 }
1150
1151 found = at;
1152 }
1153
1154 /* No match if more more than one archetype matches. */
1155 if (conflict_found)
1156 return NULL;
1157
1158 /* Return if exactly one archetype matches. */
1159 if (found != NULL)
1160 return arch_to_object (found);
1161
1162 /* No full match found: now check for partial matches. */
1163 spell_name_length = strlen (spell_name);
1164 conflict_found = 0;
1165 found = NULL;
1166 for_all_archetypes (at)
1167 {
1168 if (at->type != SPELL)
1169 continue;
1170
1171 if (strncmp (at->archname, "spelldirect_", 12) == 0)
1172 continue;
1173
1174 if (strncmp (at->object::name, spell_name, spell_name_length) != 0)
1175 continue;
1176
1177 if (found != NULL)
1178 {
1179 if (!conflict_found)
1180 {
1181 conflict_found = 1;
1182 new_draw_info_format (NDI_UNIQUE, 0, op, "More than one spell matches %s:", spell_name);
1183 new_draw_info_format (NDI_UNIQUE, 0, op, "- %s", &found->object::name);
1184 }
1185 new_draw_info_format (NDI_UNIQUE, 0, op, "- %s", &at->object::name);
1186 continue;
1187 }
1188
1189 found = at;
1190 }
1191
1192 /* No match if more more than one archetype matches. */
1193 if (conflict_found)
1194 return NULL;
1195
1196 /* Return if exactly one archetype matches. */
1197 if (found != NULL)
1198 return arch_to_object (found);
1199
1200 /* No spell found: just print an error message. */
1201 new_draw_info_format (NDI_UNIQUE, 0, op, "The spell %s does not exist.", spell_name);
1202 return NULL;
1203 }
1204
1205 static int
1206 command_learn_spell_or_prayer (object *op, char *params, int special_prayer)
1207 {
1208 object *tmp;
1209
1210 if (op->contr == NULL || params == NULL)
1211 {
1212 new_draw_info (NDI_UNIQUE, 0, op, "Which spell do you want to learn?");
1213 return 0;
1214 }
1215
1216 tmp = get_spell_by_name (op, params);
1217 if (tmp == NULL)
1218 {
1219 return 0;
1220 }
1221
1222 if (check_spell_known (op, tmp->name))
1223 {
1224 new_draw_info_format (NDI_UNIQUE, 0, op, "You already know the spell %s.", &tmp->name);
1225 return 0;
1226 }
1227
1228 do_learn_spell (op, tmp, special_prayer);
1229 tmp->destroy ();
1230 return 1;
1231 }
1232
1233 int
1234 command_learn_spell (object *op, char *params)
1235 {
1236 return command_learn_spell_or_prayer (op, params, 0);
1237 }
1238
1239 int
1240 command_learn_special_prayer (object *op, char *params)
1241 {
1242 return command_learn_spell_or_prayer (op, params, 1);
1243 }
1244
1245 int
1246 command_forget_spell (object *op, char *params)
1247 {
1248 object *spell;
1249
1250 if (op->contr == NULL || params == NULL)
1251 {
1252 new_draw_info (NDI_UNIQUE, 0, op, "Which spell do you want to forget?");
1253 return 0;
1254 }
1255
1256 spell = lookup_spell_by_name (op, params);
1257 if (spell == NULL)
1258 {
1259 new_draw_info_format (NDI_UNIQUE, 0, op, "You do not know the spell %s.", params);
1260 return 0;
1261 }
1262
1263 do_forget_spell (op, spell->name);
1264 return 1;
1265 }
1266
1267 /**
1268 * A players wants to become DM and hide.
1269 * Let's see if that's authorized.
1270 * Make sure to not tell anything to anyone.
1271 */
1272 int
1273 command_dmhide (object *op, char *params)
1274 {
1275 if (!do_wizard_dm (op, params, 1))
1276 return 0;
1277
1278 do_wizard_hide (op, 1);
1279
1280 return 1;
1281 }
1282
1283 void
1284 dm_stack_pop (player *pl)
1285 {
1286 if (!pl->stack_items || !pl->stack_position)
1287 {
1288 new_draw_info (NDI_UNIQUE, 0, pl->ob, "Empty stack!");
1289 return;
1290 }
1291
1292 pl->stack_position--;
1293 new_draw_info_format (NDI_UNIQUE, 0, pl->ob, "Popped item from stack, %d left.", pl->stack_position);
1294 }
1295
1296 /**
1297 * Get current stack top item for player.
1298 * Returns NULL if no stacked item.
1299 * If stacked item disappeared (freed), remove it.
1300 *
1301 * Ryo, august 2004
1302 */
1303 object *
1304 dm_stack_peek (player *pl)
1305 {
1306 object *ob;
1307
1308 if (!pl->stack_position)
1309 {
1310 new_draw_info (NDI_UNIQUE, 0, pl->ob, "Empty stack!");
1311 return NULL;
1312 }
1313
1314 ob = find_object (pl->stack_items[pl->stack_position - 1]);
1315 if (!ob)
1316 {
1317 new_draw_info (NDI_UNIQUE, 0, pl->ob, "Stacked item was removed!");
1318 dm_stack_pop (pl);
1319 return NULL;
1320 }
1321
1322 return ob;
1323 }
1324
1325 /**
1326 * Push specified item on player stack.
1327 * Inform player of position.
1328 * Initializes variables if needed.
1329 */
1330 void
1331 dm_stack_push (player *pl, tag_t item)
1332 {
1333 if (!pl->stack_items)
1334 {
1335 pl->stack_items = (tag_t *) malloc (sizeof (tag_t) * STACK_SIZE);
1336 memset (pl->stack_items, 0, sizeof (tag_t) * STACK_SIZE);
1337 }
1338
1339 if (pl->stack_position == STACK_SIZE)
1340 {
1341 new_draw_info (NDI_UNIQUE, 0, pl->ob, "Item stack full!");
1342 return;
1343 }
1344
1345 pl->stack_items[pl->stack_position] = item;
1346 new_draw_info_format (NDI_UNIQUE, 0, pl->ob, "Item stacked as %d.", pl->stack_position);
1347 pl->stack_position++;
1348 }
1349
1350 /**
1351 * Checks 'params' for object code.
1352 *
1353 * Can be:
1354 * * empty => get current object stack top for player
1355 * * number => get item with that tag, stack it for future use
1356 * * $number => get specified stack item
1357 * * "me" => player himself
1358 *
1359 * At function exit, params points to first non-object char
1360 *
1361 * 'from', if not NULL, contains at exit:
1362 * * STACK_FROM_NONE => object not found
1363 * * STACK_FROM_TOP => top item stack, may be NULL if stack was empty
1364 * * STACK_FROM_STACK => item from somewhere in the stack
1365 * * STACK_FROM_NUMBER => item by number, pushed on stack
1366 *
1367 * Ryo, august 2004
1368 */
1369 object *
1370 get_dm_object (player *pl, char **params, int *from)
1371 {
1372 int item_tag, item_position;
1373 object *ob;
1374
1375 if (!pl)
1376 return NULL;
1377
1378 if (!params || !*params || **params == '\0')
1379 {
1380 if (from)
1381 *from = STACK_FROM_TOP;
1382 /* No parameter => get stack item */
1383 return dm_stack_peek (pl);
1384 }
1385
1386 /* Let's clean white spaces */
1387 while (**params == ' ')
1388 (*params)++;
1389
1390 /* Next case: number => item tag */
1391 if (sscanf (*params, "%d", &item_tag))
1392 {
1393 /* Move parameter to next item */
1394 while (isdigit (**params))
1395 (*params)++;
1396
1397 /* And skip blanks, too */
1398 while (**params == ' ')
1399 (*params)++;
1400
1401 /* Get item */
1402 ob = find_object (item_tag);
1403 if (!ob)
1404 {
1405 if (from)
1406 *from = STACK_FROM_NONE;
1407 new_draw_info_format (NDI_UNIQUE, 0, pl->ob, "No such item %d!", item_tag);
1408 return NULL;
1409 }
1410
1411 /* Got one, let's push it on stack */
1412 dm_stack_push (pl, item_tag);
1413 if (from)
1414 *from = STACK_FROM_NUMBER;
1415 return ob;
1416 }
1417
1418 /* Next case: $number => stack item */
1419 if (sscanf (*params, "$%d", &item_position))
1420 {
1421 /* Move parameter to next item */
1422 (*params)++;
1423
1424 while (isdigit (**params))
1425 (*params)++;
1426 while (**params == ' ')
1427 (*params)++;
1428
1429 if (item_position >= pl->stack_position)
1430 {
1431 if (from)
1432 *from = STACK_FROM_NONE;
1433 new_draw_info_format (NDI_UNIQUE, 0, pl->ob, "No such stack item %d!", item_position);
1434 return NULL;
1435 }
1436
1437 ob = find_object (pl->stack_items[item_position]);
1438 if (!ob)
1439 {
1440 if (from)
1441 *from = STACK_FROM_NONE;
1442 new_draw_info_format (NDI_UNIQUE, 0, pl->ob, "Stack item %d was removed.", item_position);
1443 return NULL;
1444 }
1445
1446 if (from)
1447 *from = item_position < pl->stack_position - 1 ? STACK_FROM_STACK : STACK_FROM_TOP;
1448 return ob;
1449 }
1450
1451 /* Next case: 'me' => return pl->ob */
1452 if (!strncmp (*params, "me", 2))
1453 {
1454 if (from)
1455 *from = STACK_FROM_NUMBER;
1456 dm_stack_push (pl, pl->ob->count);
1457
1458 /* Skip to next token */
1459 (*params) += 2;
1460 while (**params == ' ')
1461 (*params)++;
1462
1463 return pl->ob;
1464 }
1465
1466 /* Last case: get stack top */
1467 if (from)
1468 *from = STACK_FROM_TOP;
1469 return dm_stack_peek (pl);
1470 }
1471
1472 /**
1473 * Pop the stack top.
1474 */
1475 int
1476 command_stack_pop (object *op, char *params)
1477 {
1478 dm_stack_pop (op->contr);
1479 return 0;
1480 }
1481
1482 /**
1483 * Push specified item on stack.
1484 */
1485 int
1486 command_stack_push (object *op, char *params)
1487 {
1488 object *ob;
1489 int from;
1490
1491 ob = get_dm_object (op->contr, &params, &from);
1492
1493 if (ob && from != STACK_FROM_NUMBER)
1494 /* Object was from stack, need to push it again */
1495 dm_stack_push (op->contr, ob->count);
1496
1497 return 0;
1498 }
1499
1500 /**
1501 * Displays stack contents.
1502 */
1503 int
1504 command_stack_list (object *op, char *params)
1505 {
1506 int item;
1507 object *display;
1508 player *pl = op->contr;
1509
1510 new_draw_info (NDI_UNIQUE, 0, op, "Item stack contents:");
1511
1512 for (item = 0; item < pl->stack_position; item++)
1513 {
1514 display = find_object (pl->stack_items[item]);
1515 if (display)
1516 new_draw_info_format (NDI_UNIQUE, 0, op, " %d : %s [%d]", item, &display->name, display->count);
1517 else
1518 /* Item was freed */
1519 new_draw_info_format (NDI_UNIQUE, 0, op, " %d : (lost item: %d)", item, pl->stack_items[item]);
1520 }
1521
1522 return 0;
1523 }
1524
1525 /**
1526 * Empty DM item stack.
1527 */
1528 int
1529 command_stack_clear (object *op, char *params)
1530 {
1531 op->contr->stack_position = 0;
1532 new_draw_info (NDI_UNIQUE, 0, op, "Item stack cleared.");
1533 return 0;
1534 }
1535
1536 int
1537 command_insert_into (object *op, char *params)
1538 {
1539 object *left, *right, *inserted;
1540 int left_from, right_from;
1541
1542 left = get_dm_object (op->contr, &params, &left_from);
1543 if (!left)
1544 {
1545 new_draw_info (NDI_UNIQUE, 0, op, "Insert into what object?");
1546 return 0;
1547 }
1548
1549 if (left_from == STACK_FROM_NUMBER)
1550 /* Item was stacked, remove it else right will be the same... */
1551 dm_stack_pop (op->contr);
1552
1553 right = get_dm_object (op->contr, &params, &right_from);
1554
1555 if (!right)
1556 {
1557 new_draw_info (NDI_UNIQUE, 0, op, "Insert what item?");
1558 return 0;
1559 }
1560
1561 if (left_from == STACK_FROM_TOP && right_from == STACK_FROM_TOP)
1562 {
1563 /*
1564 * Special case: both items were taken from stack top.
1565 * Override the behaviour, taking left as item just below top, if exists.
1566 * See function description for why.
1567 * Besides, can't insert an item into itself.
1568 */
1569 if (op->contr->stack_position > 1)
1570 {
1571 left = find_object (op->contr->stack_items[op->contr->stack_position - 2]);
1572 if (left)
1573 new_draw_info (NDI_UNIQUE, 0, op, "(Note: item to insert into taken from undertop)");
1574 else
1575 /* Stupid case: item under top was freed, fallback to stack top */
1576 left = right;
1577 }
1578 }
1579
1580 if (left == right)
1581 {
1582 new_draw_info (NDI_UNIQUE, 0, op, "Can't insert an object into itself!");
1583 return 0;
1584 }
1585
1586 if (right->type == PLAYER)
1587 {
1588 new_draw_info (NDI_UNIQUE, 0, op, "Can't insert a player into something!");
1589 return 0;
1590 }
1591
1592 if (!QUERY_FLAG (right, FLAG_REMOVED))
1593 right->remove ();
1594
1595 insert_ob_in_ob (right, left);
1596
1597 new_draw_info_format (NDI_UNIQUE, 0, op, "Inserted %s in %s", query_name (inserted), query_name (left));
1598
1599 return 0;
1600
1601 }