ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_object.C
Revision: 1.81
Committed: Mon Oct 6 18:26:35 2008 UTC (15 years, 8 months ago) by elmex
Content type: text/plain
Branch: MAIN
Changes since 1.80: +85 -31 lines
Log Message:
refactored drop code a bit, and hopefully fixed some bugs with it.

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
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
23
24 /*
25 * Object (handling) commands
26 */
27
28 #include <global.h>
29 #include <loader.h>
30 #include <skills.h>
31 #include <sproto.h>
32 #include <living.h>
33 #include <math.h>
34
35 /*
36 * Object id parsing functions
37 */
38
39 #define ADD_ITEM(NEW,COUNT)\
40 if(!first) {\
41 first = new objectlink;\
42 last=first;\
43 } else {\
44 last->next = new objectlink;\
45 last=last->next;\
46 }\
47 last->next=0;\
48 last->ob=(NEW);\
49 last->id=(COUNT);
50
51 /**
52 * Search the inventory of 'pl' for what matches best with params.
53 * we use item_matched_string above - this gives us consistent behaviour
54 * between many commands. Return the best match, or NULL if no match.
55 * aflag is used with apply -u , and apply -a to
56 * only unapply applied, or apply unapplied objects
57 **/
58 static object *
59 find_best_apply_object_match (object *pl, const char *params, enum apply_flag aflag)
60 {
61 object *best = 0;
62 int match_val = 0;
63
64 for (object *tmp = pl->inv; tmp; tmp = tmp->below)
65 {
66 if (tmp->invisible)
67 continue;
68
69 int tmpmatch = item_matched_string (pl, tmp, params);
70
71 if (tmpmatch > match_val)
72 {
73 if ((aflag == AP_APPLY) && (QUERY_FLAG (tmp, FLAG_APPLIED)))
74 continue;
75
76 if ((aflag == AP_UNAPPLY) && (!QUERY_FLAG (tmp, FLAG_APPLIED)))
77 continue;
78
79 match_val = tmpmatch;
80 best = tmp;
81 }
82 }
83
84 return best;
85 }
86
87 /**
88 * Shortcut to find_best_apply_object_match(pl, params, AF_NULL);
89 **/
90 object *
91 find_best_object_match (object *pl, const char *params)
92 {
93 return find_best_apply_object_match (pl, params, AP_TOGGLE);
94 }
95
96 static bool
97 can_split (object *pl, object *&op, sint32 nrof)
98 {
99 if (object *tmp = op->split (nrof ? nrof : op->number_of ()))
100 {
101 op = tmp;
102 return true;
103 }
104 else
105 {
106 if (op->nrof > 1)
107 new_draw_info_format (NDI_UNIQUE, 0, pl, "There are only %d %s.", op->nrof, &op->name_pl);
108 else
109 new_draw_info_format (NDI_UNIQUE, 0, pl, "There is only one %s.", &op->name);
110
111 return false;
112 }
113 }
114
115 int
116 command_uskill (object *pl, char *params)
117 {
118 if (!params)
119 {
120 new_draw_info (NDI_UNIQUE, 0, pl, "Usage: use_skill <skill name>");
121 return 0;
122 }
123
124 return use_skill (pl, params);
125 }
126
127 int
128 command_rskill (object *pl, char *params)
129 {
130 object *skill;
131
132 if (!params)
133 {
134 new_draw_info (NDI_UNIQUE, 0, pl, "Usage: ready_skill <skill name>");
135 return 0;
136 }
137
138 skill = find_skill_by_name (pl, params);
139
140 if (!skill)
141 {
142 new_draw_info_format (NDI_UNIQUE, 0, pl, "You have no knowledge of the skill %s", params);
143 return 0;
144 }
145
146 pl->change_skill (0);
147 apply_special (pl, skill, AP_APPLY);
148 return 1;
149 }
150
151 /* These functions (command_search, command_disarm) are really just wrappers for
152 * things like 'use_skill ...'). In fact, they should really be obsoleted
153 * and replaced with those.
154 */
155 int
156 command_search (object *op, char *params)
157 {
158 return use_skill (op, skill_names[SK_FIND_TRAPS]);
159 }
160
161 int
162 command_disarm (object *op, char *params)
163 {
164 return use_skill (op, skill_names[SK_DISARM_TRAPS]);
165 }
166
167 /* A little special because we do want to pass the full params along
168 * as it includes the object to throw.
169 */
170 int
171 command_throw (object *op, char *params)
172 {
173 if (object *skop = find_skill_by_name (op, skill_names[SK_THROWING]))
174 return do_skill (op, op, skop, op->facing, params);
175 else
176 new_draw_info (NDI_UNIQUE, 0, op, "You have no knowledge of the skill throwing.");
177
178 return 0;
179 }
180
181 int
182 command_apply (object *op, char *params)
183 {
184 if (!params)
185 {
186 player_apply_below (op);
187 return 0;
188 }
189 else
190 {
191 apply_flag aflag = (apply_flag)0;
192
193 while (*params == ' ')
194 params++;
195
196 if (!strncmp (params, "-a ", 3))
197 {
198 aflag = AP_APPLY;
199 params += 3;
200 }
201
202 if (!strncmp (params, "-u ", 3))
203 {
204 aflag = AP_UNAPPLY;
205 params += 3;
206 }
207
208 while (*params == ' ')
209 params++;
210
211 if (object *inv = find_best_apply_object_match (op, params, aflag))
212 player_apply (op, inv, aflag, 0);
213 else
214 new_draw_info_format (NDI_UNIQUE, 0, op, "Could not find any match to the %s.", params);
215 }
216
217 return 0;
218 }
219
220 /*
221 * Check if an item op can be put into a sack. If pl exists then tell
222 * a player the reason of failure.
223 * returns 1 if it will fit, 0 if it will not. nrof is the number of
224 * objects (op) we want to put in. We specify it separately instead of
225 * using op->nrof because often times, a player may have specified a
226 * certain number of objects to drop, so we can pass that number, and
227 * not need to use split_ob and stuff.
228 */
229 int
230 sack_can_hold (object *pl, object *sack, object *op, uint32 nrof)
231 {
232 if (!QUERY_FLAG (sack, FLAG_APPLIED))
233 {
234 new_draw_info_format (NDI_UNIQUE, 0, pl, "The %s is not active.", query_name (sack));
235 return 0;
236 }
237
238 if (sack == op)
239 {
240 new_draw_info_format (NDI_UNIQUE, 0, pl, "You can't put the %s into itself.", query_name (sack));
241 return 0;
242 }
243
244 if (sack->race && (sack->race != op->race || op->type == CONTAINER || (sack->stats.food && sack->stats.food != op->type)))
245 {
246 new_draw_info_format (NDI_UNIQUE, 0, pl, "You can put only %s into the %s.", &sack->race, query_name (sack));
247 return 0;
248 }
249
250 if (op->type == SPECIAL_KEY && sack->slaying && op->slaying)
251 {
252 new_draw_info_format (NDI_UNIQUE, 0, pl, "You can't put the key into %s.", query_name (sack));
253 return 0;
254 }
255
256 if (sack->weight_limit && (sint32) (sack->carrying + (nrof ? nrof : 1) *
257 (op->weight + (op->type == CONTAINER ? (op->carrying * op->stats.Str) : 0))
258 * (100 - sack->stats.Str) / 100) > sack->weight_limit)
259 {
260 new_draw_info_format (NDI_UNIQUE, 0, pl, "That won't fit in the %s!", query_name (sack));
261 return 0;
262 }
263
264 /* All other checks pass, must be OK */
265 return 1;
266 }
267
268 /* Pick up commands follow */
269
270 /* pl = player (not always - monsters can use this now)
271 * op is the object to put tmp into,
272 * tmp is the object to pick up, nrof is the number to
273 * pick up (0 means all of them)
274 */
275 static void
276 pick_up_object (object *pl, object *op, object *tmp, int nrof)
277 {
278 object *env = tmp->env;
279 uint32 weight, effective_weight_limit;
280 int tmp_nrof = tmp->number_of ();
281
282 /* IF the player is flying & trying to take the item out of a container
283 * that is in his inventory, let him. tmp->env points to the container
284 * (sack, luggage, etc), tmp->env->env then points to the player (nested
285 * containers not allowed as of now)
286 */
287 if ((pl->move_type & MOVE_FLYING) && !QUERY_FLAG (pl, FLAG_WIZ) && tmp->in_player () != pl)
288 {
289 pl->failmsg ("You are levitating, you can't reach the ground! "
290 "H<You have to stop levitating first, if you can, either by using your levitation skill, "
291 "or waiting till the levitation effect wears off.>");
292 return;
293 }
294
295 if (QUERY_FLAG (tmp, FLAG_NO_DROP))
296 return;
297
298 if (nrof > tmp_nrof || nrof <= 0)
299 nrof = tmp_nrof;
300
301 /* Figure out how much weight this object will add to the player */
302 weight = tmp->weight * nrof;
303 if (tmp->inv)
304 weight += tmp->carrying * (100 - tmp->stats.Str) / 100;
305
306 effective_weight_limit = weight_limit [min (MAX_STAT, pl->stats.Str)];
307
308 if ((pl->weight + pl->carrying + weight) > effective_weight_limit)
309 {
310 new_draw_info (0, 0, pl, "That item is too heavy for you to pick up.");
311 return;
312 }
313
314 if (!can_split (pl, tmp, nrof))
315 return;
316
317 if (QUERY_FLAG (tmp, FLAG_UNPAID))
318 {
319 tmp->flag.reset (FLAG_UNPAID);
320 new_draw_info_format (NDI_UNIQUE, 0, pl, "%s will cost you %s.", query_name (tmp), query_cost_string (tmp, pl, F_BUY | F_SHOP));
321 tmp->flag.set (FLAG_UNPAID);
322 }
323 else
324 new_draw_info_format (NDI_UNIQUE, 0, pl, "You pick up the %s.", query_name (tmp));
325
326 op->insert (tmp);
327 }
328
329 /* modified slightly to allow monsters use this -b.t. 5-31-95 */
330 void
331 pick_up (object *op, object *alt)
332 {
333 int need_fix_tmp = 0;
334 object *tmp = NULL;
335 maptile *tmp_map = NULL;
336 int count;
337
338 /* Decide which object to pick. */
339 if (alt)
340 {
341 if (!can_pick (op, alt))
342 {
343 new_draw_info_format (NDI_UNIQUE, 0, op, "You can't pick up the %s.", &alt->name);
344 goto leave;
345 }
346
347 tmp = alt;
348 }
349 else
350 {
351 if (op->below == NULL || !can_pick (op, op->below))
352 {
353 new_draw_info (NDI_UNIQUE, 0, op, "There is nothing to pick up here.");
354 goto leave;
355 }
356
357 tmp = op->below;
358 }
359
360 /* Try to catch it. */
361 tmp_map = tmp->map;
362 tmp = stop_item (tmp);
363 if (tmp == NULL)
364 goto leave;
365
366 need_fix_tmp = 1;
367 if (!can_pick (op, tmp))
368 goto leave;
369
370 if (op->type == PLAYER)
371 {
372 count = op->contr->count;
373 if (count == 0)
374 count = tmp->nrof;
375 }
376 else
377 count = tmp->nrof;
378
379 /* container is open, so use it */
380 if (tmp->flag [FLAG_STARTEQUIP])
381 alt = op;
382 else if (op->container)
383 {
384 alt = op->container;
385 if (alt != tmp->env && !sack_can_hold (op, alt, tmp, count))
386 goto leave;
387 }
388 else
389 { /* non container pickup */
390 for (alt = op->inv; alt; alt = alt->below)
391 if (alt->type == CONTAINER && QUERY_FLAG (alt, FLAG_APPLIED) &&
392 alt->race && alt->race == tmp->race && sack_can_hold (NULL, alt, tmp, count))
393 break; /* perfect match */
394
395 if (!alt)
396 for (alt = op->inv; alt; alt = alt->below)
397 if (alt->type == CONTAINER && QUERY_FLAG (alt, FLAG_APPLIED) && sack_can_hold (NULL, alt, tmp, count))
398 break; /* General container comes next */
399
400 if (!alt)
401 alt = op; /* No free containers */
402 }
403
404 if (tmp->env == alt)
405 {
406 /* here it could be possible to check rent,
407 * if someone wants to implement it
408 */
409 alt = op;
410 }
411
412 #ifdef PICKUP_DEBUG
413 LOG (llevDebug, "Pick_up(): %s picks %s (%d) and inserts it %s.\n", op->name, tmp->name, op->contr->count, alt->name);
414 #endif
415
416 /* startequip items are not allowed to be put into containers: */
417 if (op->type == PLAYER && alt->type == CONTAINER && QUERY_FLAG (tmp, FLAG_STARTEQUIP))
418 {
419 new_draw_info (NDI_UNIQUE, 0, op, "This object cannot be put into containers!");
420 goto leave;
421 }
422
423 pick_up_object (op, alt, tmp, count);
424
425 if (tmp->destroyed () || tmp->env)
426 need_fix_tmp = 0;
427
428 if (op->type == PLAYER)
429 op->contr->count = 0;
430
431 goto leave;
432
433 leave:
434 if (need_fix_tmp)
435 fix_stopped_item (tmp, tmp_map, op);
436 }
437
438 /* This takes (picks up) and item. op is the player
439 * who issued the command. params is a string to
440 * match against the item name. Basically, always
441 * returns zero, but that should be improved.
442 */
443 int
444 command_take (object *op, char *params)
445 {
446 object *tmp, *next;
447
448 if (op->container)
449 tmp = op->container->inv;
450 else
451 {
452 tmp = op->above;
453 if (tmp)
454 while (tmp->above)
455 tmp = tmp->above;
456
457 if (!tmp)
458 tmp = op->below;
459 }
460
461 if (!tmp)
462 {
463 new_draw_info (NDI_UNIQUE, 0, op, "Nothing to take!");
464 return 0;
465 }
466
467 /* Makes processing easier */
468 if (params && *params == '\0')
469 params = 0;
470
471 int cnt = MAX_ITEM_PER_DROP;
472
473 while (tmp)
474 {
475 next = tmp->below;
476
477 if (tmp->invisible)
478 {
479 tmp = next;
480 continue;
481 }
482
483 /* This following two if and else if could be merged into line
484 * but that probably will make it more difficult to read, and
485 * not make it any more efficient
486 */
487 if (params && item_matched_string (op, tmp, params))
488 {
489 if (--cnt < 0) break;
490 pick_up (op, tmp);
491 }
492 else if (can_pick (op, tmp) && !params)
493 {
494 if (--cnt < 0) break;
495 pick_up (op, tmp);
496 break;
497 }
498
499 tmp = next;
500 }
501
502 if (cnt < 0)
503 {
504 op->failmsg ("Couldn't pick up so many items at once.");
505 return 0;
506 }
507
508 if (!params && !tmp)
509 {
510 for (tmp = op->below; tmp; tmp = tmp->below)
511 if (!tmp->invisible)
512 {
513 new_draw_info_format (NDI_UNIQUE, 0, op, "You can't pick up a %s.", &tmp->name);
514 break;
515 }
516
517 if (!tmp)
518 new_draw_info (NDI_UNIQUE, 0, op, "There is nothing to pick up.");
519 }
520
521 return 0;
522 }
523
524 /*
525 * This function was part of drop, now is own function.
526 * Player 'op' tries to put object 'tmp' into sack 'sack',
527 * if nrof is non zero, then nrof objects is tried to put into sack.
528 * Note that the 'sack' in question can now be a transport,
529 * so this function isn't named very good anymore.
530 */
531 void
532 put_object_in_sack (object *op, object *sack, object *tmp, uint32 nrof)
533 {
534 object *tmp2, *sack2;
535 char buf[MAX_BUF];
536
537 if (sack == tmp)
538 return; /* Can't put an object in itself */
539
540 if (QUERY_FLAG (tmp, FLAG_STARTEQUIP))
541 {
542 new_draw_info_format (NDI_UNIQUE, 0, op, "You cannot put the %s in the %s.", query_name (tmp), query_name (sack));
543 return;
544 }
545
546 if (tmp->type == CONTAINER && tmp->inv)
547 {
548 /* Eneq(@csd.uu.se): If the object to be dropped is a container
549 * we instead move the contents of that container into the active
550 * container, this is only done if the object has something in it.
551 */
552 sack2 = tmp;
553 new_draw_info_format (NDI_UNIQUE, 0, op, "You move the items from %s into %s.", query_name (tmp), query_name (sack));
554
555 for (tmp2 = tmp->inv; tmp2; tmp2 = tmp)
556 {
557 tmp = tmp2->below;
558
559 if ((sack->type == CONTAINER && sack_can_hold (op, op->container, tmp2, tmp2->nrof)))
560 put_object_in_sack (op, sack, tmp2, 0);
561 else
562 {
563 sprintf (buf, "Your %s fills up.", query_name (sack));
564 new_draw_info (NDI_UNIQUE, 0, op, buf);
565 break;
566 }
567 }
568
569 return;
570 }
571
572 /* Don't worry about this for containers - our caller should have
573 * already checked this.
574 */
575 if ((sack->type == CONTAINER) && !sack_can_hold (op, sack, tmp, (nrof ? nrof : tmp->nrof)))
576 return;
577
578 if (QUERY_FLAG (tmp, FLAG_APPLIED))
579 if (apply_special (op, tmp, AP_UNAPPLY | AP_NO_MERGE))
580 return;
581
582 /* we want to put some portion of the item into the container */
583 if (!can_split (op, tmp, nrof))
584 return;
585
586 new_draw_info_format (NDI_UNIQUE, 0, op, "You put the %s in %s.", query_name (tmp), query_name (sack));
587 sack->insert (tmp);
588 }
589
590 /*
591 * This function was part of drop, now is own function.
592 * Player 'op' tries to drop object 'tmp', if tmp is non zero, then
593 * nrof objects is tried to dropped.
594 * This is used when dropping objects onto the floor.
595 */
596 void
597 drop_object (object *op, object *tmp, uint32 nrof)
598 {
599 if (QUERY_FLAG (tmp, FLAG_NO_DROP))
600 return;
601
602 if (QUERY_FLAG (tmp, FLAG_APPLIED))
603 if (apply_special (op, tmp, AP_UNAPPLY | AP_NO_MERGE))
604 return; /* can't unapply it */
605
606 //fprintf (stderr, "ui, on space is %ld\n", op->ms ().volume ());//D
607
608 /* We are only dropping some of the items. We split the current object
609 * off
610 */
611 if (!can_split (op, tmp, nrof))
612 return;
613
614 drop_object (op, tmp);
615
616 if (!tmp->destroyed () && !tmp->is_in_map_or_inv ())
617 {
618 // if nothing happened with the object we give it back
619 op->insert (tmp);
620 }
621 }
622
623 /* In contrast to drop_object (op, tmp, nrof) above this function takes the
624 * already split off object, and feeds it to the event handlers and does
625 * other magic with it.
626 *
627 * <droppper> is the object that dropped this object and <obj> is the
628 * object that was dropped.
629 *
630 * Make sure to check what happened with <obj> after this function returns!
631 * Otherwise you may leak this object.
632 */
633
634 void
635 drop_object (object *dropper, object *obj)
636 {
637 if (INVOKE_OBJECT (DROP, obj, ARG_OBJECT (dropper)))
638 return;
639
640 // safe guard for programming errors:
641 if (obj->destroyed () || obj->is_in_map_or_inv ())
642 {
643 LOG (llevError, "BUG: A plugin destroyed or put the object somewhere without overriding the DROP event (%s)!", obj->debug_desc ());
644 return;
645 }
646
647 if (QUERY_FLAG (obj, FLAG_STARTEQUIP))
648 {
649 dropper->statusmsg (format ("You drop the %s.", query_name (obj)));
650 dropper->statusmsg ("The god who lent it to you retrieves it.");
651
652 obj->destroy ();
653 dropper->update_stats ();
654 return;
655 }
656
657 for (object *floor = GET_MAP_OB (dropper->map, dropper->x, dropper->y);
658 floor;
659 floor = floor->above)
660 if (INVOKE_OBJECT (DROP_ON, floor, ARG_OBJECT (obj), ARG_OBJECT (dropper)))
661 return;
662
663 // safe guard for programming errors:
664 if (obj->destroyed () || obj->is_in_map_or_inv ())
665 {
666 LOG (llevError, "BUG: A plugin destroyed or put the object somewhere without overriding the DROP_ON event (%s)!", obj->debug_desc ());
667 return;
668 }
669
670 if (is_in_shop (dropper) && !QUERY_FLAG (obj, FLAG_UNPAID) && obj->type != MONEY)
671 if (!sell_item (obj, dropper))
672 return;
673
674 /* If nothing special happened with this object, the default action is to
675 * insert it below the dropper:
676 */
677
678 obj->x = dropper->x;
679 obj->y = dropper->y;
680
681 insert_ob_in_map (obj, dropper->map, dropper, INS_BELOW_ORIGINATOR);
682 }
683
684 void
685 drop (object *op, object *tmp)
686 {
687 /* Hopeful fix for disappearing objects when dropping from a container -
688 * somehow, players get an invisible object in the container, and the
689 * old logic would skip over invisible objects - works fine for the
690 * playes inventory, but drop inventory wants to use the next value.
691 */
692 if (tmp->invisible)
693 {
694 /* if the following is the case, it must be in an container. */
695 if (tmp->env && tmp->env->type != PLAYER)
696 {
697 /* Just toss the object - probably shouldn't be hanging
698 * around anyways
699 */
700 tmp->destroy ();
701 return;
702 }
703 else
704 {
705 while (tmp && tmp->invisible)
706 tmp = tmp->below;
707 }
708 }
709
710 if (tmp == NULL)
711 {
712 new_draw_info (NDI_UNIQUE, 0, op, "You don't have anything to drop.");
713 return;
714 }
715
716 if (QUERY_FLAG (tmp, FLAG_INV_LOCKED))
717 {
718 new_draw_info (NDI_UNIQUE, 0, op, "This item is locked");
719 return;
720 }
721
722 if (QUERY_FLAG (tmp, FLAG_NO_DROP))
723 {
724 #if 0
725 /* Eneq(@csd.uu.se): Objects with NO_DROP defined can't be dropped. */
726 new_draw_info (NDI_UNIQUE, 0, op, "This item can't be dropped.");
727 #endif
728 return;
729 }
730
731 if (op->type == PLAYER && op->contr->last_used == tmp)
732 op->contr->last_used = tmp->below ? tmp->below
733 : tmp->above ? tmp->above
734 : 0;
735
736 if (op->container)
737 {
738 if (op->type == PLAYER)
739 put_object_in_sack (op, op->container, tmp, op->contr->count);
740 else
741 put_object_in_sack (op, op->container, tmp, 0);
742 }
743 else
744 {
745 if (op->type == PLAYER)
746 drop_object (op, tmp, op->contr->count);
747 else
748 drop_object (op, tmp, 0);
749 }
750
751 if (op->type == PLAYER)
752 op->contr->count = 0;
753 }
754
755 /* Command will drop all items that have not been locked */
756 int
757 command_dropall (object *op, char *params)
758 {
759
760 if (op->inv == NULL)
761 {
762 new_draw_info (NDI_UNIQUE, 0, op, "Nothing to drop!");
763 return 0;
764 }
765
766 object *curinv = op->inv;
767 object *nextinv;
768
769 /*
770 This is the default. Drops everything not locked or considered
771 not something that should be dropped.
772 */
773 /*
774 Care must be taken that the next item pointer is not to money as
775 the drop() routine will do unknown things to it when dropping
776 in a shop. --Tero.Pelander@utu.fi
777 */
778
779 int cnt = MAX_ITEM_PER_DROP;
780
781 if (!params)
782 {
783 while (curinv)
784 {
785 nextinv = curinv->below;
786
787 while (nextinv && nextinv->type == MONEY)
788 nextinv = nextinv->below;
789
790 if (!QUERY_FLAG (curinv, FLAG_INV_LOCKED) && curinv->type != MONEY &&
791 curinv->type != FOOD && curinv->type != KEY &&
792 curinv->type != SPECIAL_KEY && curinv->type != GEM &&
793 !curinv->invisible && (curinv->type != CONTAINER || op->container != curinv))
794 {
795 drop (op, curinv);
796 if (--cnt <= 0) break;
797 }
798
799 curinv = nextinv;
800 }
801 }
802 else if (strcmp (params, "weapons") == 0)
803 {
804 while (curinv)
805 {
806 nextinv = curinv->below;
807
808 while (nextinv && nextinv->type == MONEY)
809 nextinv = nextinv->below;
810
811 if (!QUERY_FLAG (curinv, FLAG_INV_LOCKED) && ((curinv->type == WEAPON) || (curinv->type == BOW) || (curinv->type == ARROW)))
812 {
813 drop (op, curinv);
814 if (--cnt <= 0) break;
815 }
816
817 curinv = nextinv;
818 }
819 }
820 else if (strcmp (params, "armor") == 0 || strcmp (params, "armour") == 0)
821 {
822 while (curinv)
823 {
824 nextinv = curinv->below;
825
826 while (nextinv && nextinv->type == MONEY)
827 nextinv = nextinv->below;
828
829 if (!QUERY_FLAG (curinv, FLAG_INV_LOCKED) && ((curinv->type == ARMOUR) || curinv->type == SHIELD || curinv->type == HELMET))
830 {
831 drop (op, curinv);
832 if (--cnt <= 0) break;
833 }
834
835 curinv = nextinv;
836 }
837 }
838 else if (strcmp (params, "misc") == 0)
839 {
840 while (curinv)
841 {
842 nextinv = curinv->below;
843
844 while (nextinv && nextinv->type == MONEY)
845 nextinv = nextinv->below;
846
847 if (!QUERY_FLAG (curinv, FLAG_INV_LOCKED) && !QUERY_FLAG (curinv, FLAG_APPLIED))
848 {
849 switch (curinv->type)
850 {
851 case HORN:
852 case BOOK:
853 case SPELLBOOK:
854 case GIRDLE:
855 case AMULET:
856 case RING:
857 case CLOAK:
858 case BOOTS:
859 case GLOVES:
860 case BRACERS:
861 case SCROLL:
862 case ARMOUR_IMPROVER:
863 case WEAPON_IMPROVER:
864 case WAND:
865 case ROD:
866 case POTION:
867 drop (op, curinv);
868 curinv = nextinv;
869 break;
870 default:
871 curinv = nextinv;
872 break;
873 }
874
875 if (--cnt <= 0) break;
876 }
877
878 curinv = nextinv;
879 }
880 }
881
882 if (cnt <= 0)
883 op->failmsg ("Only dropped some items, can't drop that many items at once.");
884
885 /* draw_look(op);*/
886 return 0;
887 }
888
889
890 /* This function tries to drop all objects in the <objs> vector.
891 * <dropper> is the object that wants to drop them.
892 * <cnt> can be a 0 pointer or a pointer to the maximum number of
893 * drop operations to perform.
894 *
895 * Returns true if at least one item was dropped.
896 */
897 bool
898 drop_vector (object *dropper, vector<object *> &objs, int *cnt)
899 {
900 vector<object *>::iterator i;
901
902 bool did_one = false;
903
904 for (i = objs.begin (); i != objs.end (); i++)
905 {
906 drop (dropper, *i);
907 if (cnt && --*cnt <= 0) break;
908 did_one = true;
909 }
910
911 return did_one;
912 }
913
914 /* Object op wants to drop object(s) params. params can be a
915 * comma seperated list.
916 */
917 int
918 command_drop (object *op, char *params)
919 {
920 object *tmp, *next;
921 int did_one = 0;
922
923 if (!params)
924 {
925 new_draw_info (NDI_UNIQUE, 0, op, "Drop what?");
926 return 0;
927 }
928 else
929 {
930 vector<object *> matched_objs;
931
932 for (tmp = op->inv; tmp; tmp = next)
933 {
934 next = tmp->below;
935 if (QUERY_FLAG (tmp, FLAG_NO_DROP) || tmp->invisible)
936 continue;
937
938 if (item_matched_string (op, tmp, params))
939 matched_objs.push_back (tmp);
940 }
941
942 int cnt = MAX_ITEM_PER_DROP;
943
944 if (!drop_vector (op, matched_objs, &cnt))
945 new_draw_info (NDI_UNIQUE, 0, op, "Nothing to drop.");
946
947 if (cnt <= 0)
948 op->failmsg ("Only dropped some items, can't drop that many items at once.");
949 }
950
951 return 0;
952 }
953
954 int
955 command_examine (object *op, char *params)
956 {
957 if (!params)
958 {
959 object *tmp = op->below;
960
961 while (tmp && !tmp->client_visible ())
962 tmp = tmp->below;
963
964 if (tmp)
965 examine (op, tmp);
966 }
967 else
968 {
969 object *tmp = find_best_object_match (op, params);
970
971 if (tmp)
972 examine (op, tmp);
973 else
974 op->contr->infobox (MSG_CHANNEL ("examine"), format ("Could not find an object that matches %s", params));
975 }
976
977 return 0;
978 }
979
980 /* op should be a player.
981 * we return the object the player has marked with the 'mark' command
982 * below. If no match is found (or object has changed), we return
983 * NULL. We leave it up to the calling function to print messages if
984 * nothing is found.
985 */
986 object *
987 find_marked_object (object *op)
988 {
989 object *tmp;
990
991 if (!op || !op->contr)
992 return NULL;
993
994 if (!op->contr->mark)
995 {
996 /* new_draw_info(NDI_UNIQUE,0,op,"You have no marked object");*/
997 return 0;
998 }
999
1000 /* This may seem like overkill, but we need to make sure that they
1001 * player hasn't dropped the item. We use count on the off chance that
1002 * an item got reincarnated at some point.
1003 */
1004 for (tmp = op->inv; tmp; tmp = tmp->below)
1005 {
1006 if (tmp->invisible)
1007 continue;
1008
1009 if (tmp == op->contr->mark)
1010 {
1011 if (!tmp->destroyed ())
1012 return tmp;
1013 else
1014 {
1015 op->contr->mark = 0;
1016 /* new_draw_info(NDI_UNIQUE,0,op,"You have no marked object");*/
1017 return 0;
1018 }
1019 }
1020 }
1021
1022 return 0;
1023 }
1024
1025 std::string
1026 object::describe_monster (object *who)
1027 {
1028 dynbuf_text buf (512, 512);
1029
1030 object *mon = head ? head : this;
1031
1032 if (QUERY_FLAG (mon, FLAG_UNDEAD))
1033 buf << "It is an undead force.\r";
1034
1035 if (mon->level > who->level)
1036 buf << "It is likely more powerful than you.\r";
1037 else if (mon->level < who->level)
1038 buf << "It is likely less powerful than you.\r";
1039 else
1040 buf << "It is probably as powerful as you.\r";
1041
1042 if (mon->attacktype & AT_ACID)
1043 buf << "You seem to smell an acrid odor.\r";
1044
1045 /* Anyone know why this used to use the clone value instead of the
1046 * maxhp field? This seems that it should give more accurate results.
1047 */
1048 switch ((mon->stats.hp + 1) * 4 / (mon->stats.maxhp + 1))
1049 { /* From 1-4 */
1050 case 1:
1051 buf << "It is in a bad shape.\r";
1052 break;
1053 case 2:
1054 buf << "It is hurt.\r";
1055 break;
1056 case 3:
1057 buf << "It is somewhat hurt.\r";
1058 break;
1059 case 4:
1060 buf << "It is in excellent shape.\r";
1061 break;
1062 }
1063
1064 if (present_in_ob (POISONING, mon))
1065 buf << "It looks very ill.\r";
1066
1067 buf << '\n';
1068
1069 return buf;
1070 }
1071
1072 /* tmp is the object being described, pl is who is examing it. */
1073 const char *
1074 long_desc (object *tmp, object *pl)
1075 {
1076 static std::string s;
1077
1078 return (s = tmp->long_desc (pl)).c_str ();
1079 }
1080
1081 std::string
1082 object::long_desc (object *who)
1083 {
1084 std::string buf (query_name (this));
1085
1086 switch (type)
1087 {
1088 case RING:
1089 case SKILL:
1090 case WEAPON:
1091 case ARMOUR:
1092 case BRACERS:
1093 case HELMET:
1094 case SHIELD:
1095 case BOOTS:
1096 case GLOVES:
1097 case AMULET:
1098 case GIRDLE:
1099 case BOW:
1100 case ARROW:
1101 case CLOAK:
1102 case FOOD:
1103 case DRINK:
1104 case FLESH:
1105 case SKILL_TOOL:
1106 case POWER_CRYSTAL:
1107 {
1108 const char *cp = ::describe_item (this, who);
1109
1110 if (*cp)
1111 {
1112 buf.append (" ");
1113 buf.append (cp);
1114 }
1115 }
1116 }
1117
1118 return buf;
1119 }
1120
1121 /* op is the player
1122 * tmp is the monster being examined.
1123 */
1124 void
1125 examine_monster (object *op, object *tmp)
1126 {
1127 new_draw_info (NDI_UNIQUE, 0, op, tmp->describe_monster (op).c_str ());
1128 }
1129
1130 std::string
1131 object::describe (object *who)
1132 {
1133 dynbuf_text buf (1024, 1024);
1134
1135 buf.printf ("That is: %s.\r", long_desc (who).c_str ());
1136
1137 if (custom_name)
1138 buf.printf ("You call it %s.\r", &custom_name);
1139
1140 switch (type)
1141 {
1142 case SPELLBOOK:
1143 if (flag [FLAG_IDENTIFIED] && inv)
1144 buf.printf ("%s is a %s %s spell.\r", &inv->name, get_levelnumber (inv->level), &inv->skill);
1145 break;
1146
1147 case BOOK:
1148 if (msg)
1149 buf << "Something is written in it.\r";
1150 break;
1151
1152 case CONTAINER:
1153 if (race)
1154 {
1155 if (weight_limit && stats.Str < 100)
1156 buf.printf ("It can hold only %s and its weight limit is %.1f kg.\r",
1157 &race, weight_limit / (10.0 * (100 - stats.Str)));
1158 else
1159 buf.printf ("It can hold only %s.\r", &race);
1160 }
1161 else if (weight_limit && stats.Str < 100)
1162 buf.printf ("Its weight limit is %.1f kg.\r", weight_limit / (10.0 * (100 - stats.Str)));
1163 break;
1164
1165 case WAND:
1166 if (flag [FLAG_IDENTIFIED])
1167 buf.printf ("It has %d charges left.\r", stats.food);
1168 break;
1169 }
1170
1171 if (materialname && !msg)
1172 buf.printf ("It is made of: %s.\r", &materialname);
1173
1174 if (who)
1175 /* Where to wear this item */
1176 for (int i = 0; i < NUM_BODY_LOCATIONS; i++)
1177 if (slot[i].info)
1178 {
1179 buf << (who->slot[i].info ? body_locations[i].use_name : body_locations[i].nonuse_name);
1180
1181 if (slot[i].info < -1 && who->slot[i].info)
1182 buf.printf ("(%d)", -slot[i].info);
1183
1184 buf << ".\r";
1185 }
1186
1187 if (weight)
1188 buf.printf ("%s %3.3f kg.\r", nrof > 1 ? "They weigh" : "It weighs", weight * (nrof ? nrof : 1) / 1000.0);
1189
1190 if (value && !flag [FLAG_STARTEQUIP] && !flag [FLAG_NO_PICK] && who)
1191 {
1192 buf.printf ("You reckon %s worth %s.\r", nrof > 1 ? "they are" : "it is", query_cost_string (this, who, F_TRUE | F_APPROX));
1193
1194 if (is_in_shop (who))
1195 {
1196 if (flag [FLAG_UNPAID])
1197 buf.printf ("%s would cost you %s.\r", nrof > 1 ? "They" : "It", query_cost_string (this, who, F_BUY | F_SHOP));
1198 else
1199 buf.printf ("You are offered %s for %s.\r", query_cost_string (this, who, F_SELL + F_SHOP), nrof > 1 ? "them" : "it");
1200 }
1201 }
1202
1203 if (flag [FLAG_MONSTER])
1204 buf << describe_monster (who);
1205
1206 /* Is this item buildable? */
1207 if (flag [FLAG_IS_BUILDABLE])
1208 buf << "This is a buildable item.\r";
1209
1210 /* Does the object have a message? Don't show message for all object
1211 * types - especially if the first entry is a match
1212 */
1213 if (msg && type != EXIT && type != BOOK && type != CORPSE && !move_on && !has_dialogue ())
1214 {
1215 /* This is just a hack so when identifying the items, we print
1216 * out the extra message
1217 */
1218 if (need_identify (this) && flag [FLAG_IDENTIFIED])
1219 buf << "The object has a story:\r";
1220
1221 buf << msg << '\n';
1222 }
1223
1224 buf << '\n';
1225
1226 return std::string (buf.linearise (), buf.size ());
1227 }
1228
1229 static void
1230 display_new_pickup (object *op)
1231 {
1232 int i = op->contr->mode;
1233
1234 if (!(i & PU_NEWMODE))
1235 return;
1236
1237 new_draw_info_format (NDI_UNIQUE, 0, op, "%d NEWMODE", i & PU_NEWMODE ? 1 : 0);
1238 new_draw_info_format (NDI_UNIQUE, 0, op, "%d DEBUG", i & PU_DEBUG ? 1 : 0);
1239 new_draw_info_format (NDI_UNIQUE, 0, op, "%d INHIBIT", i & PU_INHIBIT ? 1 : 0);
1240 new_draw_info_format (NDI_UNIQUE, 0, op, "%d STOP", i & PU_STOP ? 1 : 0);
1241
1242 new_draw_info_format (NDI_UNIQUE, 0, op, "%d <= x pickup weight/value RATIO (0==off)", (i & PU_RATIO) * 5);
1243
1244 new_draw_info_format (NDI_UNIQUE, 0, op, "%d FOOD", i & PU_FOOD ? 1 : 0);
1245 new_draw_info_format (NDI_UNIQUE, 0, op, "%d DRINK", i & PU_DRINK ? 1 : 0);
1246 new_draw_info_format (NDI_UNIQUE, 0, op, "%d VALUABLES", i & PU_VALUABLES ? 1 : 0);
1247
1248 new_draw_info_format (NDI_UNIQUE, 0, op, "%d BOW", i & PU_BOW ? 1 : 0);
1249 new_draw_info_format (NDI_UNIQUE, 0, op, "%d ARROW", i & PU_ARROW ? 1 : 0);
1250
1251 new_draw_info_format (NDI_UNIQUE, 0, op, "%d HELMET", i & PU_HELMET ? 1 : 0);
1252 new_draw_info_format (NDI_UNIQUE, 0, op, "%d SHIELD", i & PU_SHIELD ? 1 : 0);
1253 new_draw_info_format (NDI_UNIQUE, 0, op, "%d ARMOUR", i & PU_ARMOUR ? 1 : 0);
1254
1255 new_draw_info_format (NDI_UNIQUE, 0, op, "%d BOOTS", i & PU_BOOTS ? 1 : 0);
1256 new_draw_info_format (NDI_UNIQUE, 0, op, "%d GLOVES", i & PU_GLOVES ? 1 : 0);
1257 new_draw_info_format (NDI_UNIQUE, 0, op, "%d CLOAK", i & PU_CLOAK ? 1 : 0);
1258 new_draw_info_format (NDI_UNIQUE, 0, op, "%d KEY", i & PU_KEY ? 1 : 0);
1259
1260 new_draw_info_format (NDI_UNIQUE, 0, op, "%d MISSILEWEAPON", i & PU_MISSILEWEAPON ? 1 : 0);
1261 new_draw_info_format (NDI_UNIQUE, 0, op, "%d ALLWEAPON", i & PU_ALLWEAPON ? 1 : 0);
1262 new_draw_info_format (NDI_UNIQUE, 0, op, "%d MAGICAL", i & PU_MAGICAL ? 1 : 0);
1263 new_draw_info_format (NDI_UNIQUE, 0, op, "%d POTION", i & PU_POTION ? 1 : 0);
1264
1265 new_draw_info_format (NDI_UNIQUE, 0, op, "%d SPELLBOOK", i & PU_SPELLBOOK ? 1 : 0);
1266 new_draw_info_format (NDI_UNIQUE, 0, op, "%d SKILLSCROLL", i & PU_SKILLSCROLL ? 1 : 0);
1267 new_draw_info_format (NDI_UNIQUE, 0, op, "%d READABLES", i & PU_READABLES ? 1 : 0);
1268 new_draw_info_format (NDI_UNIQUE, 0, op, "%d MAGICDEVICE", i & PU_MAGIC_DEVICE ? 1 : 0);
1269
1270 new_draw_info_format (NDI_UNIQUE, 0, op, "%d NOT CURSED", i & PU_NOT_CURSED ? 1 : 0);
1271
1272 new_draw_info_format (NDI_UNIQUE, 0, op, "%d JEWELS", i & PU_JEWELS ? 1 : 0);
1273 new_draw_info_format (NDI_UNIQUE, 0, op, "%d FLESH", i & PU_FLESH ? 1 : 0);
1274
1275 new_draw_info_format (NDI_UNIQUE, 0, op, "");
1276 }
1277
1278 int
1279 command_pickup (object *op, char *params)
1280 {
1281 uint32 i;
1282 static const char *names[] = {
1283 "debug", "inhibit", "stop", "food", "drink", "valuables", "bow", "arrow", "helmet",
1284 "shield", "armour", "boots", "gloves", "cloak", "key", "missile", "allweapon",
1285 "magical", "potion", "spellbook", "skillscroll", "readables", "magicdevice", "notcursed",
1286 "jewels", "flesh", NULL
1287 };
1288 static uint32 modes[] = {
1289 PU_DEBUG, PU_INHIBIT, PU_STOP, PU_FOOD, PU_DRINK, PU_VALUABLES, PU_BOW, PU_ARROW, PU_HELMET,
1290 PU_SHIELD, PU_ARMOUR, PU_BOOTS, PU_GLOVES, PU_CLOAK, PU_KEY, PU_MISSILEWEAPON, PU_ALLWEAPON,
1291 PU_MAGICAL, PU_POTION, PU_SPELLBOOK, PU_SKILLSCROLL, PU_READABLES, PU_MAGIC_DEVICE, PU_NOT_CURSED,
1292 PU_JEWELS, PU_FLESH, 0
1293 };
1294
1295 if (!params)
1296 {
1297 /* if the new mode is used, just print the settings */
1298 if (op->contr->mode & PU_NEWMODE)
1299 {
1300 display_new_pickup (op);
1301 return 1;
1302 }
1303 if (1)
1304 LOG (llevDebug, "command_pickup: !params\n");
1305 set_pickup_mode (op, (op->contr->mode > 6) ? 0 : op->contr->mode + 1);
1306 return 0;
1307 }
1308
1309 while (*params == ' ' && *params)
1310 params++;
1311
1312 if (*params == '+' || *params == '-')
1313 {
1314 int mode;
1315
1316 for (mode = 0; names[mode]; mode++)
1317 {
1318 if (!strcmp (names[mode], params + 1))
1319 {
1320 i = op->contr->mode;
1321 if (!(i & PU_NEWMODE))
1322 i = PU_NEWMODE;
1323 if (*params == '+')
1324 i = i | modes[mode];
1325 else
1326 i = i & ~modes[mode];
1327 op->contr->mode = i;
1328 display_new_pickup (op);
1329 return 1;
1330 }
1331 }
1332 new_draw_info_format (NDI_UNIQUE, 0, op, "Pickup: invalid item %s\n", params);
1333 return 1;
1334 }
1335
1336 if (sscanf (params, "%u", &i) != 1)
1337 {
1338 if (1)
1339 LOG (llevDebug, "command_pickup: params==NULL\n");
1340 new_draw_info (NDI_UNIQUE, 0, op, "Usage: pickup <0-7> or <value_density> .");
1341 return 1;
1342 }
1343 set_pickup_mode (op, i);
1344
1345 return 1;
1346 }
1347
1348 void
1349 set_pickup_mode (object *op, int i)
1350 {
1351 switch (op->contr->mode = i)
1352 {
1353 case 0:
1354 new_draw_info (NDI_UNIQUE, 0, op, "Mode: Don't pick up.");
1355 break;
1356 case 1:
1357 new_draw_info (NDI_UNIQUE, 0, op, "Mode: Pick up one item.");
1358 break;
1359 case 2:
1360 new_draw_info (NDI_UNIQUE, 0, op, "Mode: Pick up one item and stop.");
1361 break;
1362 case 3:
1363 new_draw_info (NDI_UNIQUE, 0, op, "Mode: Stop before picking up.");
1364 break;
1365 case 4:
1366 new_draw_info (NDI_UNIQUE, 0, op, "Mode: Pick up all items.");
1367 break;
1368 case 5:
1369 new_draw_info (NDI_UNIQUE, 0, op, "Mode: Pick up all items and stop.");
1370 break;
1371 case 6:
1372 new_draw_info (NDI_UNIQUE, 0, op, "Mode: Pick up all magic items.");
1373 break;
1374 case 7:
1375 new_draw_info (NDI_UNIQUE, 0, op, "Mode: Pick up all coins and gems");
1376 break;
1377 }
1378 }
1379
1380 int
1381 command_search_items (object *op, char *params)
1382 {
1383 char buf[MAX_BUF];
1384
1385 if (settings.search_items == FALSE)
1386 return 1;
1387
1388 if (params == NULL)
1389 {
1390 if (op->contr->search_str[0] == '\0')
1391 {
1392 new_draw_info (NDI_UNIQUE, 0, op, "Example: search magic+1");
1393 new_draw_info (NDI_UNIQUE, 0, op, "Would automatically pick up all");
1394 new_draw_info (NDI_UNIQUE, 0, op, "items containing the word 'magic+1'.");
1395 return 1;
1396 }
1397
1398 op->contr->search_str[0] = '\0';
1399 new_draw_info (NDI_UNIQUE, 0, op, "Search mode turned off.");
1400 op->update_stats ();
1401 return 1;
1402 }
1403
1404 if ((int) strlen (params) >= MAX_BUF)
1405 {
1406 new_draw_info (NDI_UNIQUE, 0, op, "Search string too long.");
1407 return 1;
1408 }
1409
1410 strcpy (op->contr->search_str, params);
1411 sprintf (buf, "Searching for '%s'.", op->contr->search_str);
1412 new_draw_info (NDI_UNIQUE, 0, op, buf);
1413 op->update_stats ();
1414 return 1;
1415 }
1416