ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.C
Revision: 1.76
Committed: Fri Nov 6 13:31:47 2009 UTC (14 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_90
Changes since 1.75: +0 -45 lines
Log Message:
remove or document dead code

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