ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.C
Revision: 1.39
Committed: Fri Mar 2 15:04:35 2007 UTC (17 years, 3 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_0
Changes since 1.38: +1 -104 lines
Log Message:
- possess is too dangerous
- typo

File Contents

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