ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_wiz.C
Revision: 1.87
Committed: Tue Jan 3 11:25:36 2012 UTC (12 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.86: +1 -1 lines
Log Message:
update copyrights to 2012

File Contents

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