ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.C
Revision: 1.75
Committed: Fri Nov 6 12:49:19 2009 UTC (14 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.74: +2 -2 lines
Log Message:
make effectively static symbols actually static, part 1

File Contents

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