Debian Codegraph @

server/skills.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, see the
9 * 'LICENSE' and 'COPYING' files.
10 *
11 * The authors can be reached via e-mail to [email protected]
12 *
13 * skills.c -- core skill handling
14 */
15 #define _GNU_SOURCE // strcasestr() is a GNU extension in string.h
16
17 #include "global.h"
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "book.h"
23 #include "living.h"
24 #include "object.h"
25 #include "skills.h"
26 #include "spells.h"
27 #include "sproto.h"
28
29 /**
30 * Computes stealing chance.
31 * Increased values indicate better attempts.
32 *
33 * @param op
34 * who is stealing.
35 * @param victim
36 * who to steal from.
37 * @param roll
38 * dice roll.
39 * @return
40 * -1 if op can't steal, else adjusted roll value.
41 * @todo rename roll to something more meaningful (check attempt_steal()).
42 */
43 static int adj_stealchance(object *op, object *victim, int roll) {
44 if (!op || !victim || !roll)
45 return -1;
46
47 /* Only prohibit stealing if the player does not have a free
48 * hand available and in fact does have hands.
49 */
50 if (op->type == PLAYER
51 && op->body_used[BODY_ARMS] <= 0
52 && op->body_info[BODY_ARMS]) {
53 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
54 "But you have no free hands to steal with!");
55 return -1;
56 }
57
58 /* ADJUSTMENTS */
59
60 /* Its harder to steal from hostile beings! */
61 if (!QUERY_FLAG(victim, FLAG_UNAGGRESSIVE))
62 roll = roll/2;
63
64 /* Easier to steal from sleeping beings, or if the thief is
65 * unseen */
66 if (QUERY_FLAG(victim, FLAG_SLEEP))
67 roll = roll*3;
68 else if (op->invisible)
69 roll = roll*2;
70
71 /* check stealing 'encumberance'. Having this equipment applied makes
72 * it quite a bit harder to steal.
73 */
74 FOR_INV_PREPARE(op, equip) {
75 if (equip->type == WEAPON && QUERY_FLAG(equip, FLAG_APPLIED)) {
76 roll -= equip->weight/10000;
77 }
78 if (equip->type == BOW && QUERY_FLAG(equip, FLAG_APPLIED))
79 roll -= equip->weight/5000;
80 if (equip->type == SHIELD && QUERY_FLAG(equip, FLAG_APPLIED)) {
81 roll -= equip->weight/2000;
82 }
83 if (equip->type == ARMOUR && QUERY_FLAG(equip, FLAG_APPLIED))
84 roll -= equip->weight/5000;
85 if (equip->type == GLOVES && QUERY_FLAG(equip, FLAG_APPLIED))
86 roll -= equip->weight/100;
87 } FOR_INV_FINISH();
88 if (roll < 0)
89 roll = 0;
90 return roll;
91 }
92
93 /**
94 * Steal objects.
95 * When stealing: dependent on the intelligence/wisdom of whom you're
96 * stealing from (op in attempt_steal), offset by your dexterity and
97 * skill at stealing. They may notice your attempt, whether successful
98 * or not.
99 *
100 * @param op
101 * target (person being pilfered).
102 * @param who
103 * person doing the stealing.
104 * @param skill
105 * stealing skill object.
106 * @retval 0
107 * nothing was stolen.
108 * @retval 1
109 * something was stolen.
110 */
111 static int attempt_steal(object *op, object *who, object *skill) {
112 object *success = NULL, *tmp = NULL;
113 int roll = 0, chance = 0, stats_value;
114 rv_vector rv;
115 char name[MAX_BUF];
116
117 stats_value = ((who->stats.Dex+who->stats.Int)*3)/2;
118
119 /* if the victim is aware of a thief in the area (FLAG_NO_STEAL set on them)
120 * they will try to prevent stealing if they can. Only unseen theives will
121 * have much chance of success.
122 */
123 if (op->type != PLAYER && QUERY_FLAG(op, FLAG_NO_STEAL)) {
124 if (monster_can_detect_enemy(op, who, &rv)) {
125 monster_npc_call_help(op);
126 CLEAR_FLAG(op, FLAG_UNAGGRESSIVE);
127 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
128 "Your attempt is prevented!");
129 return 0;
130 }
131
132 /* help npc to detect thief next time by raising its wisdom
133 * This probably isn't the right approach - we shouldn't be
134 * changing the stats of the monsters - better approach
135 * might be to use force objects for this - MSW 2009/02/24
136 */
137 op->stats.Wis += (op->stats.Int/5)+1;
138 if (op->stats.Wis > settings.max_stat)
139 op->stats.Wis = settings.max_stat;
140 }
141 if (op->type == PLAYER && QUERY_FLAG(op, FLAG_WIZ)) {
142 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
143 "You can't steal from the dungeon master!");
144 return 0;
145 }
146 if (op->type == PLAYER && who->type == PLAYER && settings.no_player_stealing) {
147 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
148 "You can't steal from other players!");
149 return 0;
150 }
151
152
153 /* Ok then, go thru their inventory, stealing */
154 FOR_INV_PREPARE(op, inv) {
155 /* you can't steal worn items, starting items, wiz stuff,
156 * innate abilities, or items w/o a type. Generally
157 * speaking, the invisibility flag prevents experience or
158 * abilities from being stolen since these types are currently
159 * always invisible objects. I was implicit here so as to prevent
160 * future possible problems. -b.t.
161 * Flesh items generated w/ fix_flesh_item should have FLAG_NO_STEAL
162 * already -b.t.
163 */
164
165 if (QUERY_FLAG(inv, FLAG_WAS_WIZ)
166 || QUERY_FLAG(inv, FLAG_APPLIED)
167 || !(inv->type)
168 || inv->type == SPELL
169 || QUERY_FLAG(inv, FLAG_STARTEQUIP)
170 || QUERY_FLAG(inv, FLAG_NO_STEAL)
171 || inv->invisible)
172 continue;
173
174 /* Okay, try stealing this item. Dependent on dexterity of thief,
175 * skill level, see the adj_stealroll fctn for more detail.
176 */
177
178 roll = die_roll(2, 100, who, PREFER_LOW)/2; /* weighted 1-100 */
179
180 chance = adj_stealchance(who, op, stats_value+skill->level*10-op->level*3);
181 if (chance == -1)
182 return 0;
183 if (roll < chance) {
184 tag_t inv_count = inv->count;
185
186 pick_up(who, inv);
187 /* need to see if the player actually stole this item -
188 * if it is in the players inv, assume it is. This prevents
189 * abuses where the player can not carry the item, so just
190 * keeps stealing it over and over.
191 */
192 if (object_was_destroyed(inv, inv_count) || inv->env != op) {
193 /* for players, play_sound: steals item */
194 success = inv;
195 CLEAR_FLAG(inv, FLAG_INV_LOCKED);
196 }
197 tmp = inv;
198 break;
199 }
200 } FOR_INV_FINISH(); /* for loop looking for an item */
201
202 if (!tmp) {
203 query_name(op, name, MAX_BUF);
204 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
205 "%s%s has nothing you can steal!",
206 op->type == PLAYER ? "" : "The ", name);
207 return 0;
208 }
209
210 /* If you arent high enough level, you might get something BUT
211 * the victim will notice your stealing attempt. Ditto if you
212 * attempt to steal something heavy off them, they're bound to notice
213 */
214
215 if (roll >= skill->level
216 || !chance
217 || (tmp && tmp->weight > 250*random_roll(0, stats_value+skill->level*10-1, who, PREFER_LOW))) {
218 /* victim figures out where the thief is! */
219 if (who->hide)
220 make_visible(who);
221
222 if (op->type != PLAYER) {
223 /* The unaggressives look after themselves 8) */
224 if (who->type == PLAYER) {
225 monster_npc_call_help(op);
226 query_name(op, name, MAX_BUF);
227 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
228 "%s notices your attempted pilfering!",
229 name);
230 }
231 CLEAR_FLAG(op, FLAG_UNAGGRESSIVE);
232 /* all remaining npc items are guarded now. Set flag NO_STEAL
233 * on the victim.
234 */
235 SET_FLAG(op, FLAG_NO_STEAL);
236 } else { /* stealing from another player */
237 char buf[MAX_BUF];
238
239 /* Notify the other player */
240 if (success && who->stats.Int > random_roll(0, 19, op, PREFER_LOW)) {
241 query_name(success, name, MAX_BUF);
242 snprintf(buf, sizeof(buf), "Your %s is missing!", name);
243 } else {
244 snprintf(buf, sizeof(buf), "Your pack feels strangely lighter.");
245 }
246 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_REMOVE,
247 buf);
248 if (!success) {
249 if (who->invisible) {
250 snprintf(buf, sizeof(buf), "you feel itchy fingers getting at your pack.");
251 } else {
252 query_name(who, name, MAX_BUF);
253 snprintf(buf, sizeof(buf), "%s looks very shifty.", name);
254 }
255 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_STEAL,
256 buf);
257 }
258 } /* else stealing from another player */
259 /* play_sound("stop! thief!"); kindofthing */
260 } /* if you weren't 100% successful */
261 return success ? 1 : 0;
262 }
263
264
265 /**
266 * Main stealing function.
267 *
268 * @param op
269 * thief.
270 * @param dir
271 * direction to steal from.
272 * @param skill
273 * stealing skill.
274 * @return
275 * experience gained for stealing, 0 if nothing was stolen.
276 */
277 int steal(object *op, int dir, object *skill) {
278 object *tmp;
279 int16_t x, y;
280 mapstruct *m;
281 int mflags;
282
283 x = op->x+freearr_x[dir];
284 y = op->y+freearr_y[dir];
285
286 if (dir == 0) {
287 /* Can't steal from ourself! */
288 return 0;
289 }
290
291 m = op->map;
292 mflags = get_map_flags(m, &m, x, y, &x, &y);
293 /* Out of map - can't do it. If nothing alive on this space,
294 * don't need to look any further.
295 */
296 if ((mflags&P_OUT_OF_MAP) || !(mflags&P_IS_ALIVE))
297 return 0;
298
299 /* If player can't move onto the space, can't steal from it. */
300 if (OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, x, y)))
301 return 0;
302
303 /* Find the topmost object at this spot */
304 tmp = GET_MAP_OB(m, x, y);
305 FOR_OB_AND_ABOVE_PREPARE(tmp)
306 if (tmp->above == NULL)
307 break;
308 FOR_OB_AND_ABOVE_FINISH();
309
310 /* For all the stacked objects at this point, attempt a steal */
311 FOR_OB_AND_BELOW_PREPARE(tmp) {
312 /* Minor hack--for multi square beings - make sure we get
313 * the 'head' coz 'tail' objects have no inventory! - b.t.
314 */
315 tmp = HEAD(tmp);
316 if (tmp->type != PLAYER && !QUERY_FLAG(tmp, FLAG_MONSTER))
317 continue;
318
319 /* do not reveal hidden DMs */
320 if (tmp->type == PLAYER && QUERY_FLAG(tmp, FLAG_WIZ) && tmp->contr->hidden)
321 continue;
322 if (attempt_steal(tmp, op, skill)) {
323 if (tmp->type == PLAYER) /* no xp for stealing from another player */
324 return 0;
325
326 /* no xp for stealing from pets (of players) */
327 if (QUERY_FLAG(tmp, FLAG_FRIENDLY) && tmp->attack_movement == PETMOVE) {
328 object *owner = object_get_owner(tmp);
329 if (owner != NULL && owner->type == PLAYER)
330 return 0;
331 }
332
333 return (calc_skill_exp(op, tmp, skill));
334 }
335 } FOR_OB_AND_BELOW_FINISH();
336 return 0;
337 }
338
339 /**
340 * Attempt to pick a lock. Handles traps.
341 *
342 * @param door
343 * lock to pick.
344 * @param pl
345 * player picking.
346 * @param skill
347 * locking skill.
348 * @retval 0
349 * no lock was picked.
350 * @retval 1
351 * door was locked.
352 */
353 static int attempt_pick_lock(object *door, object *pl, object *skill) {
354 int difficulty = pl->map->difficulty ? pl->map->difficulty : 0;
355 int success = 0, number; /* did we get anything? */
356
357 /* Try to pick the lock on this item (doors only for now).
358 * Dependent on dexterity/skill SK_level of the player and
359 * the map level difficulty.
360 */
361 number = (die_roll(2, 40, pl, PREFER_LOW)-2)/2;
362 // Spring traps even if the lock is picked.
363 if (door->inv && (door->inv->type == RUNE || door->inv->type == TRAP)) { /* set off any traps? */
364 spring_trap(door->inv, pl);
365 }
366 if (number < pl->stats.Dex + skill->level*2 - difficulty ) {
367 remove_door(door);
368 success = difficulty;
369 }
370 return success;
371 }
372
373
374 /**
375 * Lock pick handling.
376 *
377 * Implementation by bt. ([email protected])
378 * monster implementation 7-7-95 by bt.
379 *
380 * @param pl
381 * player picking the lock.
382 * @param dir
383 * direction to pick.
384 * @param skill
385 * lock picking skill.
386 * @return
387 * experience for picking a lock, 0 if nothing was picked.
388 */
389 int pick_lock(object *pl, int dir, object *skill) {
390 object *tmp;
391 int x = pl->x+freearr_x[dir];
392 int y = pl->y+freearr_y[dir];
393 int difficulty=0;
394
395 if (!dir)
396 dir = pl->facing;
397
398 /* For all the stacked objects at this point find a door*/
399 if (OUT_OF_REAL_MAP(pl->map, x, y)) {
400 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
401 "There is no lock there.");
402 return 0;
403 }
404
405 for (tmp = GET_MAP_OB(pl->map, x, y); tmp; tmp = tmp->above)
406 if (tmp->type == DOOR || tmp->type == LOCKED_DOOR)
407 break;
408
409 if (!tmp) {
410 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
411 "There is no lock there.");
412 return 0;
413 }
414
415 if (execute_event(tmp, EVENT_TRIGGER, pl, skill, NULL, SCRIPT_FIX_ALL) != 0)
416 return 0;
417
418 if (tmp->type == LOCKED_DOOR) {
419 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
420 "You can't pick that lock!");
421 return 0;
422 }
423
424 if (!tmp->move_block) {
425 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
426 "The door has no lock!");
427 return 0;
428 }
429
430 difficulty = attempt_pick_lock(tmp, pl, skill);
431 /* Failure */
432 if (!difficulty) {
433 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
434 "You fail to pick the lock.");
435 return 0;
436 }
437
438 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
439 "You pick the lock.");
440 return (calc_skill_exp(pl, NULL, skill) * isqrt(difficulty));
441 }
442
443
444 /**
445 * Someone is trying to hide.
446 * The user becomes undetectable (not just 'invisible') for
447 * a short while (success and duration dependant on player SK_level,
448 * dexterity, charisma, and map difficulty).
449 * Players have a good chance of becoming 'unhidden' if they move
450 * and like invisiblity will be come visible if they attack
451 * Implemented by b.t. ([email protected])
452 * July 7, 1995 - made hiding possible for monsters. -b.t.
453 *
454 * @param op
455 * living trying to hide.
456 * @param skill
457 * hiding skill.
458 * @retval 0
459 * op couldn't hide.
460 * @retval 1
461 * op successfully hide.
462 */
463 static int attempt_hide(object *op, object *skill) {
464 int number, difficulty = op->map->difficulty;
465 int terrain = hideability(op);
466
467 if (terrain < -10) /* not enough cover here */
468 return 0;
469
470 /* Hiding success and duration dependant on skill level,
471 * op->stats.Dex, map difficulty and terrain.
472 */
473
474 number = (die_roll(2, 25, op, PREFER_LOW)-2)/2;
475 if (!stand_near_hostile(op) && number < op->stats.Dex+skill->level+terrain-difficulty) {
476 op->invisible += 100; /* set the level of 'hiddeness' */
477 if (op->type == PLAYER)
478 op->contr->tmp_invis = 1;
479 op->hide = 1;
480 return 1;
481 }
482 return 0;
483 }
484
485 /**
486 * Main hide handling.
487 * @param op
488 * living trying to hide.
489 * @param skill
490 * hiding skill.
491 * @return
492 * experience gained for the skill use (can be 0).
493 */
494 int hide(object *op, object *skill) {
495 /* the preliminaries -- Can we really hide now? */
496 /* this keeps monsters from using invisibilty spells and hiding */
497
498 if (QUERY_FLAG(op, FLAG_MAKE_INVIS)) {
499 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
500 "You don't need to hide while invisible!");
501 return 0;
502 }
503
504 if (!op->hide && op->invisible > 0 && op->type == PLAYER) {
505 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_END,
506 "Your attempt to hide breaks the invisibility spell!");
507 make_visible(op);
508 }
509
510 if (op->invisible > 50*skill->level) {
511 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
512 "You are as hidden as you can get.");
513 return 0;
514 }
515
516 if (attempt_hide(op, skill)) {
517 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
518 "You hide in the shadows.");
519 object_update(op, UP_OBJ_FACE);
520 return calc_skill_exp(op, NULL, skill);
521 }
522 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
523 "You fail to conceal yourself.");
524 return 0;
525 }
526
527
528 /**
529 * End of jump. Restore the map.
530 * @param pl
531 * player.
532 * @todo Is fix_object() required?
533 */
534 static void stop_jump(object *pl) {
535 fix_object(pl);
536 object_insert_in_map_at(pl, pl->map, pl, 0, pl->x, pl->y);
537 // Some monsters can also jump. They do not check for pickup with check_pick().
538 if (pl->contr)
539 check_pick(pl);
540 }
541
542 /**
543 * Someone is trying to jump.
544 * @param pl
545 * living trying to jump.
546 * @param dir
547 * direction to jump in.
548 * @param spaces
549 * distance to jump.
550 * @param skill
551 * jumping skill.
552 * @return
553 * 1 if jump was successful, 0 else.
554 */
555 static int attempt_jump(object *pl, int dir, int spaces, object *skill) {
556 /**
557 * Make sure we aren't in a transport.
558 * This should be set to null if we aren't.
559 */
560 if (pl->contr && pl->contr->transport){
561 char trans_name[MAX_BUF];
562 query_name(pl->contr->transport, trans_name, MAX_BUF);
563 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
564 "Your bounce off the walls of %s.", trans_name);
565 // We failed to jump. Return as a failure.
566 return 0;
567 }
568
569 int i, dx = freearr_x[dir], dy = freearr_y[dir], mflags;
570 int16_t x, y;
571 mapstruct *m;
572
573 /* Jump loop. Go through spaces opject wants to jump. Halt the
574 * jump if a wall or creature is in the way. We set FLY_LOW
575 * temporarily to allow player to aviod exits/archs that are not
576 * move_on/off fly_low. This will also prevent pickup of objects
577 * while jumping over them.
578 */
579
580 object_remove(pl);
581
582 /*
583 * I don't think this is actually needed - all the movement
584 * code is handled in this function, and I don't see anyplace
585 * that cares about the move_type being flying.
586 */
587 pl->move_type |= MOVE_FLY_LOW;
588
589 for (i = 0; i <= spaces; i++) {
590 x = pl->x+dx;
591 y = pl->y+dy;
592 m = pl->map;
593
594 mflags = get_map_flags(m, &m, x, y, &x, &y);
595
596 if (mflags&P_OUT_OF_MAP) {
597 (void)stop_jump(pl);
598 return 0;
599 }
600 if (OB_TYPE_MOVE_BLOCK(pl, GET_MAP_MOVE_BLOCK(m, x, y))) {
601 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
602 "Your jump is blocked.");
603 stop_jump(pl);
604 return 0;
605 }
606
607 FOR_MAP_PREPARE(m, x, y, tmp) {
608 tmp = HEAD(tmp);
609 /* Jump into creature */
610 if (QUERY_FLAG(tmp, FLAG_MONSTER)
611 || (tmp->type == PLAYER && (!QUERY_FLAG(tmp, FLAG_WIZ) || !tmp->contr->hidden))) {
612 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
613 "You jump into %s%s.",
614 tmp->type == PLAYER ? "" : "the ", tmp->name);
615
616 stop_jump(pl);
617 if (tmp->type != PLAYER
618 || (pl->type == PLAYER && pl->contr->party == NULL)
619 || (pl->type == PLAYER && tmp->type == PLAYER && pl->contr->party != tmp->contr->party))
620 skill_attack(tmp, pl, pl->facing, "kicked", skill); /* pl makes an attack */
621
622 return 1;
623 }
624 /* If the space has fly on set (no matter what the space is),
625 * we should get the effects - after all, the player is
626 * effectively flying.
627 */
628 if (tmp->move_on&MOVE_FLY_LOW) {
629 pl->x = x;
630 pl->y = y;
631 pl->map = m;
632 if (pl->contr)
633 esrv_map_scroll(&pl->contr->socket, dx, dy);
634 stop_jump(pl);
635 return 1;
636 }
637 } FOR_MAP_FINISH();
638 pl->x = x;
639 pl->y = y;
640 pl->map = m;
641 if (pl->contr)
642 esrv_map_scroll(&pl->contr->socket, dx, dy);
643 }
644 stop_jump(pl);
645 return 1;
646 }
647
648 /**
649 * Jump skill handling.
650 * This is both a new type of movement for player/monsters and
651 * an attack as well.
652 * Perhaps we should allow more spaces based on level, eg, level 50
653 * jumper can jump several spaces?
654 *
655 * @param pl
656 * object jumping.
657 * @param dir
658 * direction to jump to.
659 * @param skill
660 * jumping skill.
661 * @return
662 * 1 if jump was successful, 0 else
663 */
664 int jump(object *pl, int dir, object *skill) {
665 int spaces = 0, stats;
666 int str = pl->stats.Str;
667 int dex = pl->stats.Dex;
668
669 dex = dex ? dex : 15;
670 str = str ? str : 10;
671
672 stats = str*str*str*dex*skill->level;
673
674 if (pl->carrying != 0) /* don't want div by zero !! */
675 spaces = (int)(stats/pl->carrying);
676 else
677 spaces = 2; /* pl has no objects - gets the far jump */
678
679 if (spaces > 2)
680 spaces = 2;
681 else if (spaces == 0) {
682 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
683 "You are carrying too much weight to jump.");
684 return 0;
685 }
686 return attempt_jump(pl, dir, spaces, skill);
687 }
688
689 /**
690 * Runs a 'detect curse' check on a given item
691 *
692 * @param pl
693 * player detecting a curse.
694 * @param tmp
695 * object to check for curse
696 * @param skill
697 * detect skill object.
698 * @return
699 * amount of experience gained (on successful detecting).
700 */
701 int detect_curse_on_item(object *pl, object *tmp, object *skill) {
702 if (!QUERY_FLAG(tmp, FLAG_IDENTIFIED) && !QUERY_FLAG(tmp, FLAG_KNOWN_CURSED)
703 && (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED))
704 && tmp->item_power < skill->level) {
705 SET_FLAG(tmp, FLAG_KNOWN_CURSED);
706 esrv_update_item(UPD_FLAGS, pl, tmp);
707 return calc_skill_exp(pl, tmp, skill);
708 }
709 return 0;
710 }
711 /**
712 * Check for cursed object with the 'detect curse' skill.
713 *
714 * @param pl
715 * player detecting.
716 * @param skill
717 * detect skill object.
718 * @return
719 * amount of experience gained (on successful detecting).
720 */
721 static int do_skill_detect_curse(object *pl, object *skill) {
722 int success = 0;
723
724 FOR_INV_PREPARE(pl, tmp)
725 if (!tmp->invisible) success += detect_curse_on_item(pl, tmp, skill);
726 FOR_INV_FINISH();
727
728 /* Check ground, too, but only objects the player could pick up. Cauldrons are exceptions,
729 * you definitely want to know if they are cursed */
730 FOR_MAP_PREPARE(pl->map, pl->x, pl->y, tmp)
731 if (object_can_pick(pl, tmp) || QUERY_FLAG(tmp, FLAG_IS_CAULDRON))
732 success += detect_curse_on_item(pl, tmp, skill);
733 FOR_MAP_FINISH();
734
735 return success;
736 }
737 /**
738 * Runs a 'detect magic' check on a given item
739 *
740 * @param pl
741 * player detecting magic.
742 * @param tmp
743 * object to check for magic
744 * @param skill
745 * detect skill object.
746 * @return
747 * amount of experience gained (on successful detecting).
748 */
749
750 int detect_magic_on_item(object *pl, object *tmp, object *skill) {
751 if (!QUERY_FLAG(tmp, FLAG_IDENTIFIED) && !QUERY_FLAG(tmp, FLAG_KNOWN_MAGICAL)
752 && (is_magical(tmp)) && tmp->item_power < skill->level) {
753 SET_FLAG(tmp, FLAG_KNOWN_MAGICAL);
754 esrv_update_item(UPD_FLAGS, pl, tmp);
755 return calc_skill_exp(pl, tmp, skill);
756 }
757 return 0;
758 }
759
760 /**
761 * Check for magic object with the 'detect magic' skill.
762 *
763 * @param pl
764 * player detecting.
765 * @param skill
766 * detect skill object.
767 * @return
768 * amount of experience gained (on successful detecting).
769 */
770 static int do_skill_detect_magic(object *pl, object *skill) {
771 int success = 0;
772
773 FOR_INV_PREPARE(pl, tmp)
774 if (!tmp->invisible)
775 success += detect_magic_on_item(pl, tmp, skill);
776 FOR_INV_FINISH();
777
778 /* Check ground, too, but like above, only if the object can be picked up*/
779 FOR_MAP_PREPARE(pl->map, pl->x, pl->y, tmp)
780 if (object_can_pick(pl, tmp))
781 success += detect_magic_on_item(pl, tmp, skill);
782 FOR_MAP_FINISH();
783
784 return success;
785 }
786
787 /**
788 * Helper function for do_skill_ident, so that we can loop
789 * over inventory AND objects on the ground conveniently.
790 *
791 * @param tmp
792 * object to try to identify.
793 * @param pl
794 * object identifying.
795 * @param skill
796 * identification skill.
797 * @param print_on_success
798 * 1 to print a description if the object is identified, 0 to leave it to the calling function
799 * @return
800 * experience for successful identification.
801 */
802 int identify_object_with_skill(object *tmp, object *pl, object *skill, int print_on_success) {
803 int success = 0, chance, ip;
804 int skill_value = (skill->level && pl->stats.Int) ? pl->stats.Int : 10;
805
806 if (!tmp->invisible && !QUERY_FLAG(tmp, FLAG_NO_SKILL_IDENT) && !is_identified(tmp)) {
807 ip = tmp->magic;
808 if (tmp->item_power > ip)
809 ip = tmp->item_power;
810
811 chance = die_roll(3, 10, pl, PREFER_LOW)-3+rndm(0, (tmp->magic ? tmp->magic*5 : 1)-1);
812 if (skill_value >= chance) {
813 tmp = identify(tmp);
814 if (pl->type == PLAYER && print_on_success) {
815 char desc[MAX_BUF];
816
817 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
818 "You identify %s.",
819 ob_describe(tmp, pl, 1, desc, sizeof(desc)));
820 if (tmp->msg) {
821 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_ITEM, MSG_TYPE_ITEM_INFO,
822 "The item has a story:\n%s",
823 tmp->msg);
824 }
825 }
826 success += calc_skill_exp(pl, tmp, skill);
827 } else
828 SET_FLAG(tmp, FLAG_NO_SKILL_IDENT);
829 }
830 return success;
831 }
832
833 /**
834 * Workhorse for skill_ident() -b.t.
835 *
836 * @param pl
837 * player identifying.
838 * @param obj_class
839 * type of objects to identify.
840 * @param skill
841 * skill to give experience to.
842 * @return
843 * experience gained by identifying items.
844 */
845 static int do_skill_ident(object *pl, int obj_class, object *skill) {
846 int success = 0, area, i;
847
848 /* check the player */
849 FOR_INV_PREPARE(pl, tmp)
850 if (tmp->type == obj_class)
851 success += identify_object_with_skill(tmp, pl, skill, 1);
852 FOR_INV_FINISH();
853
854 /* check the ground */
855 /* Altered to allow ident skills to increase in area with
856 * experience. -- Aaron Baugher
857 */
858
859 if (skill->level > 64) { /* Adjust these levels? */
860 area = 49;
861 } else if (skill->level > 16) {
862 area = 25;
863 } else if (skill->level > 4) {
864 area = 9;
865 } else {
866 area = 1;
867 }
868
869 for (i = 0; i < area; i++) {
870 int16_t x = pl->x+freearr_x[i];
871 int16_t y = pl->y+freearr_y[i];
872 mapstruct *m = pl->map;
873 int mflags;
874
875 mflags = get_map_flags(m, &m, x, y, &x, &y);
876 if (mflags&P_OUT_OF_MAP)
877 continue;
878
879 if (can_see_monsterP(m, pl->x, pl->y, i)) {
880 FOR_MAP_PREPARE(m, x, y, tmp)
881 if (tmp->type == obj_class)
882 success += identify_object_with_skill(tmp, pl, skill, 1);
883 FOR_MAP_FINISH();
884 }
885 }
886 return success;
887 }
888
889 /**
890 * Main identification skill handling.
891 * @param pl
892 * player identifying.
893 * @param skill
894 * identification skill.
895 * @return
896 * experience gained for identification.
897 */
898 int skill_ident(object *pl, object *skill) {
899 int success = 0;
900 int i, identifiable_types=0;
901 const typedata *tmptype;
902
903 if (pl->type != PLAYER)
904 return 0; /* only players will skill-identify */
905
906 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
907 "You look at the objects nearby...");
908
909 switch (skill->subtype) {
910 case SK_DET_CURSE:
911 success = do_skill_detect_curse(pl, skill);
912 if (success)
913 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
914 "...and discover cursed items!");
915 break;
916
917 case SK_DET_MAGIC:
918 success = do_skill_detect_magic(pl, skill);
919 if (success)
920 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
921 "...and discover items imbued with mystic forces!");
922 break;
923
924 default:
925 /* we will try to identify items with this skill instead */
926 for (i=0; i<=OBJECT_TYPE_MAX; i++) {
927 tmptype = get_typedata(i);
928 if (tmptype) {
929 if (skill->subtype == tmptype->identifyskill || skill->subtype == tmptype->identifyskill2) {
930 success += do_skill_ident(pl, i, skill);
931 identifiable_types++;
932 }
933 }
934 }
935 if (identifiable_types == 0) {
936 LOG(llevError, "Error: skill_ident() called with skill %d which can't identify any items\n", skill->subtype);
937 return 0;
938 break;
939 }
940 break;
941 }
942 if (!success) {
943 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
944 "...and learn nothing more.");
945 }
946 return success;
947 }
948
949
950 /**
951 * Oratory skill handling.
952 * Players using this skill can 'charm' a monster --
953 * into working for them. It can only be used on
954 * non-special (see below) 'neutral' creatures.
955 * -b.t. ([email protected])
956 *
957 * @param pl
958 * player trying to convince a monster.
959 * @param dir
960 * direction to orate in.
961 * @param skill
962 * oratory skill object.
963 * @return
964 * experience gained for oratoring.
965 * @todo
966 * check if can't be simplified, code looks duplicated.
967 */
968 int use_oratory(object *pl, int dir, object *skill) {
969 int16_t x = pl->x+freearr_x[dir], y = pl->y+freearr_y[dir];
970 int mflags, chance;
971 object *tmp;
972 mapstruct *m;
973 char name[MAX_BUF];
974
975 if (pl->type != PLAYER)
976 return 0; /* only players use this skill */
977 m = pl->map;
978 mflags = get_map_flags(m, &m, x, y, &x, &y);
979 if (mflags&P_OUT_OF_MAP)
980 return 0;
981
982 /* Save some processing - we have the flag already anyways
983 */
984 if (!(mflags&P_IS_ALIVE)) {
985 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
986 "There is nothing to orate to.");
987 return 0;
988 }
989
990 tmp = NULL;
991 FOR_MAP_PREPARE(m, x, y, tmp2) {
992 tmp2 = HEAD(tmp2);
993 /* can't persuade players - return because there is nothing else
994 * on that space to charm.
995 */
996 if (tmp2->type == PLAYER)
997 return 0;
998
999 if (QUERY_FLAG(tmp2, FLAG_MONSTER)) {
1000 const char *value = object_get_value(tmp2, "no_mood_change");
1001 if (value && strcmp(value, "1") == 0)
1002 return 0;
1003
1004 tmp = tmp2;
1005 break;
1006 }
1007 } FOR_MAP_FINISH();
1008
1009 if (!tmp) {
1010 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1011 "There is nothing to orate to.");
1012 return 0;
1013 }
1014
1015 query_name(tmp, name, MAX_BUF);
1016 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1017 "You orate to the %s.",
1018 name);
1019
1020 /* the following conditions limit who may be 'charmed' */
1021
1022 /* it's hostile! */
1023 if (!QUERY_FLAG(tmp, FLAG_UNAGGRESSIVE) && !QUERY_FLAG(tmp, FLAG_FRIENDLY)) {
1024 query_name(tmp, name, MAX_BUF);
1025 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1026 "Too bad the %s isn't listening!",
1027 name);
1028 return 0;
1029 }
1030
1031 /* it's already allied! */
1032 if (QUERY_FLAG(tmp, FLAG_FRIENDLY) && tmp->attack_movement == PETMOVE) {
1033 if (object_get_owner(tmp) == pl) {
1034 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1035 "Your follower loves your speech.");
1036 return 0;
1037 }
1038
1039 if (skill->level > tmp->level) {
1040 /* you steal the follower. Perhaps we should really look at the
1041 * level of the owner above?
1042 */
1043 object_set_owner(tmp, pl);
1044 query_name(tmp, name, MAX_BUF);
1045 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1046 "You convince the %s to follow you instead!",
1047 name);
1048
1049 FREE_AND_COPY(tmp->skill, skill->skill);
1050
1051 /* Abuse fix - don't give exp since this can otherwise
1052 * be used by a couple players to gets lots of exp.
1053 */
1054 return 0;
1055 }
1056
1057 /* In this case, you can't steal it from the other player */
1058 return 0;
1059 } /* Creature was already a pet of someone */
1060
1061 chance = skill->level*2+(pl->stats.Cha-2*tmp->stats.Int)/2;
1062
1063 /* Ok, got a 'sucker' lets try to make them a follower */
1064 if (chance > 0 && tmp->level < random_roll(0, chance-1, pl, PREFER_HIGH)-1) {
1065 int64_t exp;
1066 query_name(tmp, name, MAX_BUF);
1067 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1068 "You convince the %s to become your follower.",
1069 name);
1070
1071 object_set_owner(tmp, pl);
1072 /* compute exp before setting to 0, else the monster's experience is not taken into account. */
1073 tmp->stats.exp /= 5; /* why 5? because. */
1074 exp = calc_skill_exp(pl, tmp, skill);
1075 tmp->stats.exp = 0;
1076 add_friendly_object(tmp);
1077 SET_FLAG(tmp, FLAG_FRIENDLY);
1078 tmp->attack_movement = PETMOVE;
1079 /* keep oratory skill, so exp goes where it should if the pet kills something */
1080 FREE_AND_COPY(tmp->skill, skill->skill);
1081 return exp;
1082 }
1083
1084 /* Charm failed. Creature may be angry now */
1085 if (skill->level+(pl->stats.Cha-10)/2 < random_roll(1, 2*tmp->level, pl, PREFER_LOW)) {
1086 query_name(tmp, name, MAX_BUF);
1087 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1088 "Your speech angers the %s!",
1089 name);
1090
1091 if (QUERY_FLAG(tmp, FLAG_FRIENDLY)) {
1092 CLEAR_FLAG(tmp, FLAG_FRIENDLY);
1093 remove_friendly_object(tmp);
1094 tmp->attack_movement = 0; /* needed? */
1095 }
1096 CLEAR_FLAG(tmp, FLAG_UNAGGRESSIVE);
1097 }
1098
1099 return 0; /* Fall through - if we get here, we didn't charm anything */
1100 }
1101
1102 /**
1103 * Singing skill handling.
1104 * This skill allows the player to pacify nearby creatures.
1105 * There are few limitations on who/what kind of
1106 * non-player creatures that may be pacified. Right now, a player
1107 * may pacify creatures which have Int == 0. In this routine, once
1108 * successfully pacified the creature gets Int=1. Thus, a player
1109 * may only pacify a creature once.
1110 * BTW, I appologize for the naming of the skill, I couldnt think
1111 * of anything better! -b.t.
1112 *
1113 * @param pl
1114 * player singing.
1115 * @param dir
1116 * direction to sing in.
1117 * @param skill
1118 * singing skill object.
1119 * @return
1120 * experience gained for singing.
1121 */
1122 int singing(object *pl, int dir, object *skill) {
1123 int i, exp = 0, chance, mflags;
1124 object *tmp;
1125 mapstruct *m;
1126 int16_t x, y;
1127 char name[MAX_BUF];
1128 const char *value;
1129
1130 if (pl->type != PLAYER)
1131 return 0; /* only players use this skill */
1132
1133 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1134 "You sing");
1135 for (i = dir; i < (dir+MIN(skill->level, SIZEOFFREE)); i++) {
1136 x = pl->x+freearr_x[i];
1137 y = pl->y+freearr_y[i];
1138 m = pl->map;
1139
1140 mflags = get_map_flags(m, &m, x, y, &x, &y);
1141 if (mflags&P_OUT_OF_MAP)
1142 continue;
1143 if (!(mflags&P_IS_ALIVE))
1144 continue;
1145
1146 tmp = NULL;
1147 FOR_MAP_PREPARE(m, x, y, tmp2) {
1148 tmp2 = HEAD(tmp2);
1149 if (QUERY_FLAG(tmp2, FLAG_MONSTER)) {
1150 tmp = tmp2;
1151 break;
1152 }
1153 /* can't affect players */
1154 if (tmp2->type == PLAYER) {
1155 tmp = tmp2;
1156 break;
1157 }
1158 } FOR_MAP_FINISH();
1159
1160 /* Whole bunch of checks to see if this is a type of monster that would
1161 * listen to singing.
1162 */
1163 if (tmp
1164 && QUERY_FLAG(tmp, FLAG_MONSTER)
1165 && !QUERY_FLAG(tmp, FLAG_NO_STEAL) /* Been charmed or abused before */
1166 && !QUERY_FLAG(tmp, FLAG_SPLITTING) /* no ears */
1167 && !QUERY_FLAG(tmp, FLAG_HITBACK) /* was here before */
1168 && (tmp->level <= skill->level)
1169 && !QUERY_FLAG(tmp, FLAG_UNDEAD)
1170 && !QUERY_FLAG(tmp, FLAG_UNAGGRESSIVE) /* already calm */
1171 && !QUERY_FLAG(tmp, FLAG_FRIENDLY)) { /* already calm */
1172 /* stealing isn't really related (although, maybe it should
1173 * be). This is mainly to prevent singing to the same monster
1174 * over and over again and getting exp for it.
1175 */
1176 chance = skill->level*2+(pl->stats.Cha-5-tmp->stats.Int)/2;
1177
1178 value = object_get_value(tmp, "no_mood_change");
1179 if (value && strcmp(value, "1") == 0)
1180 chance = 0;
1181
1182 if (chance && tmp->level*2 < random_roll(0, chance-1, pl, PREFER_HIGH)) {
1183 SET_FLAG(tmp, FLAG_UNAGGRESSIVE);
1184 query_name(tmp, name, MAX_BUF);
1185 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1186 "You calm down the %s",
1187 name);
1188
1189 /* Give exp only if they are not aware */
1190 if (!QUERY_FLAG(tmp, FLAG_NO_STEAL))
1191 exp += calc_skill_exp(pl, tmp, skill);
1192 SET_FLAG(tmp, FLAG_NO_STEAL);
1193 } else {
1194 query_name(tmp, name, MAX_BUF);
1195 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1196 "Too bad the %s isn't listening!",
1197 name);
1198 SET_FLAG(tmp, FLAG_NO_STEAL);
1199 }
1200 }
1201 }
1202 return exp;
1203 }
1204
1205 /**
1206 * Checks for traps on the spaces around the player or in certain objects.
1207 *
1208 * @param pl
1209 * player searching.
1210 * @param skill
1211 * find trap skill object.
1212 * @return
1213 * experience gained for finding traps.
1214 */
1215 int find_traps(object *pl, object *skill) {
1216 int i, expsum = 0, mflags;
1217 int16_t x, y;
1218 mapstruct *m;
1219
1220 /* First we search all around us for runes and traps, which are
1221 * all type RUNE
1222 */
1223
1224 for (i = 0; i < 9; i++) {
1225 x = pl->x+freearr_x[i];
1226 y = pl->y+freearr_y[i];
1227 m = pl->map;
1228
1229 mflags = get_map_flags(m, &m, x, y, &x, &y);
1230 if (mflags&P_OUT_OF_MAP)
1231 continue;
1232
1233 /* Check everything in the square for trapness */
1234 FOR_MAP_PREPARE(m, x, y, tmp) {
1235 /* And now we'd better do an inventory traversal of each
1236 * of these objects' inventory
1237 * We can narrow this down a bit - no reason to search through
1238 * the players inventory or monsters for that matter.
1239 */
1240 if (tmp->type != PLAYER && !QUERY_FLAG(tmp, FLAG_MONSTER)) {
1241 FOR_INV_PREPARE(tmp, tmp2)
1242 if (tmp2->type == RUNE || tmp2->type == TRAP)
1243 if (trap_see(pl, tmp2)) {
1244 trap_show(tmp2, tmp);
1245 if (tmp2->stats.Cha > 1) {
1246 object *owner;
1247
1248 owner = object_get_owner(tmp2);
1249 if (owner == NULL || owner->type != PLAYER)
1250 expsum += calc_skill_exp(pl, tmp2, skill);
1251
1252 tmp2->stats.Cha = 1; /* unhide the trap */
1253 }
1254 }
1255 FOR_INV_FINISH();
1256 }
1257 if ((tmp->type == RUNE || tmp->type == TRAP) && trap_see(pl, tmp)) {
1258 trap_show(tmp, tmp);
1259 if (tmp->stats.Cha > 1) {
1260 object *owner;
1261
1262 owner = object_get_owner(tmp);
1263 if (owner == NULL || owner->type != PLAYER)
1264 expsum += calc_skill_exp(pl, tmp, skill);
1265 tmp->stats.Cha = 1; /* unhide the trap */
1266 }
1267 }
1268 } FOR_MAP_FINISH();
1269 }
1270 draw_ext_info(NDI_BLACK, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1271 "You search the area.");
1272 return expsum;
1273 }
1274
1275 /**
1276 * This skill will disarm any previously discovered trap.
1277 * the algorithm is based (almost totally) on the old command_disarm() - b.t.
1278 *
1279 * @param op
1280 * player disarming. Must be on a map.
1281 * @param skill
1282 * disarming skill.
1283 * @return
1284 * experience gained to disarm.
1285 */
1286 int remove_trap(object *op, object *skill) {
1287 int i, success = 0, mflags;
1288 mapstruct *m;
1289 int16_t x, y;
1290
1291 for (i = 0; i < 9; i++) {
1292 x = op->x+freearr_x[i];
1293 y = op->y+freearr_y[i];
1294 m = op->map;
1295
1296 mflags = get_map_flags(m, &m, x, y, &x, &y);
1297 if (mflags&P_OUT_OF_MAP)
1298 continue;
1299
1300 /* Check everything in the square for trapness */
1301 FOR_MAP_PREPARE(m, x, y, tmp) {
1302 /* And now we'd better do an inventory traversal of each
1303 * of these objects inventory. Like above, only
1304 * do this for interesting objects.
1305 */
1306
1307 if (tmp->type != PLAYER && !QUERY_FLAG(tmp, FLAG_MONSTER)) {
1308 FOR_INV_PREPARE(tmp, tmp2)
1309 if ((tmp2->type == RUNE || tmp2->type == TRAP) && tmp2->stats.Cha <= 1) {
1310 object *owner;
1311
1312 trap_show(tmp2, tmp);
1313 if (trap_disarm(op, tmp2, 1, skill) && ((owner = object_get_owner(tmp2)) == NULL || owner->type != PLAYER)) {
1314 tmp2->stats.exp = tmp2->stats.Cha*tmp2->level;
1315 success += calc_skill_exp(op, tmp2, skill);
1316 } else {
1317 /* Can't continue to disarm after failure */
1318 return success;
1319 }
1320 }
1321 FOR_INV_FINISH();
1322 }
1323 if ((tmp->type == RUNE || tmp->type == TRAP) && tmp->stats.Cha <= 1) {
1324 object *owner;
1325
1326 trap_show(tmp, tmp);
1327 if (trap_disarm(op, tmp, 1, skill) && ((owner = object_get_owner(tmp)) == NULL || owner->type != PLAYER)) {
1328 tmp->stats.exp = tmp->stats.Cha*tmp->level;
1329 success += calc_skill_exp(op, tmp, skill);
1330 } else {
1331 /* Can't continue to disarm after failure */
1332 return success;
1333 }
1334 }
1335 } FOR_MAP_FINISH();
1336 }
1337 return success;
1338 }
1339
1340
1341 /**
1342 * Praying skill handling.
1343 *
1344 * When this skill is called from do_skill(), it allows
1345 * the player to regain lost grace points at a faster rate. -b.t.
1346 *
1347 * This always returns 0 - return value is used by calling function
1348 * such that if it returns true, player gets exp in that skill. This
1349 * the effect here can be done on demand, we probably don't want to
1350 * give infinite exp by returning true in any cases.
1351 *
1352 * @param pl
1353 * object praying, should be a player.
1354 * @param skill
1355 * praying skill.
1356 * @return
1357 * 0.
1358 */
1359 int pray(object *pl, object *skill) {
1360 char buf[MAX_BUF];
1361
1362 if (pl->type != PLAYER)
1363 return 0;
1364
1365 snprintf(buf, sizeof(buf), "You pray.");
1366
1367 /* Check all objects - we could stop at floor objects,
1368 * but if someone buries an altar, I don't see a problem with
1369 * going through all the objects, and it shouldn't be much slower
1370 * than extra checks on object attributes.
1371 */
1372 FOR_BELOW_PREPARE(pl, tmp)
1373 /* Only if the altar actually belongs to someone do you get special benefits */
1374 if (tmp->type == HOLY_ALTAR && tmp->other_arch) {
1375 snprintf(buf, sizeof(buf), "You pray over the %s.", tmp->name);
1376 pray_at_altar(pl, tmp, skill);
1377 break; /* Only pray at one altar */
1378 }
1379 FOR_BELOW_FINISH();
1380
1381 draw_ext_info(NDI_BLACK, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1382 buf);
1383
1384 if (pl->stats.grace < pl->stats.maxgrace) {
1385 pl->stats.grace++;
1386 pl->last_grace = -1;
1387 }
1388 return 0;
1389 }
1390
1391 /**
1392 * Meditation skill handling.
1393 *
1394 * This skill allows the player to regain a few sp or hp for a
1395 * brief period of concentration.
1396 * The amount of time needed to concentrate and the # of points regained is dependant on
1397 * the level of the user.
1398 *
1399 * Depending on the level, the player can wear armour or not.
1400 * @author b.t. [email protected]
1401 *
1402 * @param pl
1403 * livng meditating, should be a player.
1404 * @param skill
1405 * meditation skill.
1406 */
1407 void meditate(object *pl, object *skill) {
1408 if (pl->type != PLAYER)
1409 return; /* players only */
1410
1411 /* check if pl has removed encumbering armour and weapons */
1412 if (QUERY_FLAG(pl, FLAG_READY_WEAPON) && skill->level < 6) {
1413 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1414 "You can't concentrate while wielding a weapon!");
1415 return;
1416 }
1417
1418 FOR_INV_PREPARE(pl, tmp)
1419 if (((tmp->type == ARMOUR && skill->level < 12)
1420 || (tmp->type == HELMET && skill->level < 10)
1421 || (tmp->type == SHIELD && skill->level < 6)
1422 || (tmp->type == BOOTS && skill->level < 4)
1423 || (tmp->type == GLOVES && skill->level < 2))
1424 && QUERY_FLAG(tmp, FLAG_APPLIED)) {
1425 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1426 "You can't concentrate while wearing so much armour!");
1427 return;
1428 }
1429 FOR_INV_FINISH();
1430
1431 /* ok let's meditate! Spell points are regained first, then once
1432 * they are maxed we get back hp. Actual incrementing of values
1433 * is handled by the do_some_living() (in player.c). This way magical
1434 * bonuses for healing/sp regeneration are included properly
1435 * No matter what, we will eat up some playing time trying to
1436 * meditate. (see 'factor' variable for what sets the amount of time)
1437 */
1438
1439 draw_ext_info(NDI_BLACK, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1440 "You meditate.");
1441
1442 if (pl->stats.sp < pl->stats.maxsp) {
1443 pl->stats.sp++;
1444 pl->last_sp = -1;
1445 } else if (pl->stats.hp < pl->stats.maxhp) {
1446 pl->stats.hp++;
1447 pl->last_heal = -1;
1448 }
1449 }
1450
1451 /**
1452 * Called by write_on_item() to inscribe a message in an ordinary book.
1453 *
1454 * @param pl
1455 * Player attempting to write
1456 * @param item
1457 * Book to write the message in
1458 * @param msg
1459 * Message to write
1460 * @return
1461 * Experience gained from using the skill
1462 * @todo assert() instead of simple check.
1463 */
1464 static int write_note(object *pl, object *item, const char *msg) {
1465 char buf[BOOK_BUF];
1466 object *newBook = NULL;
1467
1468 // The item should exist and be a book, but check it just in case.
1469 if (!item || item->type != BOOK) {
1470 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1471 "That was interesting...");
1472 // TODO: Print a scary log message.
1473 return 0;
1474 }
1475
1476 // The message should never be NULL, but check it just in case.
1477 if (!msg) {
1478 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1479 "Hmm... what was I going to write?");
1480 // TODO: Print a scary log message.
1481 return 0;
1482 }
1483
1484 // Don't let the player write a reserved keyword.
1485 if (strcasestr_local(msg, "endmsg")) {
1486 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1487 "Trying to cheat now are we?");
1488 return 0;
1489 }
1490
1491 /* Lauwenmark: Handle for plugin book writing (trigger) event */
1492 if (execute_event(item, EVENT_TRIGGER, pl, NULL, msg, SCRIPT_FIX_ALL) != 0)
1493 return strlen(msg);
1494
1495 buf[0] = 0;
1496
1497 // Write the message in the book if it doesn't overflow the buffer.
1498 if (!book_overflow(item->msg, msg, BOOK_BUF)) {
1499 // TODO: Garble some characters depending on intelligence/skill.
1500
1501 // If there was already text, append the new message on the end.
1502 if (item->msg) {
1503 snprintf(buf, sizeof(buf), "%s%s\n", item->msg, msg);
1504 } else {
1505 snprintf(buf, sizeof(buf), "%s\n", msg);
1506 }
1507
1508 // If there were multiple items in a stack, unstack one and write.
1509 if (item->nrof > 1) {
1510 newBook = object_new();
1511 object_copy(item, newBook);
1512 object_decrease_nrof_by_one(item);
1513 newBook->nrof = 1;
1514 object_set_msg(newBook, buf);
1515 newBook = object_insert_in_ob(newBook, pl);
1516 } else {
1517 object_set_msg(item, buf);
1518
1519 // This shouldn't be necessary; the object hasn't changed visibly.
1520 //esrv_send_item(pl, item);
1521 }
1522
1523 // Tell the player that he/she wrote in the object.
1524 query_short_name(item, buf, BOOK_BUF);
1525 draw_ext_info_format(
1526 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1527 "You write in the %s.", buf);
1528
1529 // Give the player experience for writing.
1530 return strlen(msg);
1531 } else {
1532 query_short_name(item, buf, BOOK_BUF);
1533 draw_ext_info_format(
1534 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1535 "Your message won't fit in the %s!", buf);
1536 return 0;
1537 }
1538 }
1539
1540 /**
1541 * Called by write_on_item() to inscribe spell scrolls that the player can
1542 * cast. Backfire effects are possible, varying in severity depending on the
1543 * difficulty of the spell being attempted.
1544 *
1545 * @param pl
1546 * player writing a scroll.
1547 * @param scroll
1548 * object to write into.
1549 * @param skill
1550 * writing skill.
1551 * @return
1552 * experience gained.
1553 */
1554 static int write_scroll(object *pl, object *scroll, object *skill) {
1555 int success = 0, confused = 0, grace_cost = 0;
1556 object *newscroll, *chosen_spell, *tmp;
1557
1558 // The item should exist and be a scroll, but check it just in case.
1559 if (!scroll || scroll->type != SCROLL) {
1560 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1561 "A spell must be written on a magic scroll!");
1562 // TODO: Print a scary log message.
1563 return 0;
1564 }
1565
1566 // The player must have a spell readied to inscribe.
1567 chosen_spell = pl->contr->ranges[range_magic];
1568 if (!chosen_spell) {
1569 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1570 "You should ready the spell you wish to inscribe.");
1571 return 0;
1572 }
1573
1574 // Make sure the player has enough SP or grace to write the spell.
1575 grace_cost = SP_level_spellpoint_cost(pl, chosen_spell, SPELL_GRACE);
1576 if (grace_cost > 0 && grace_cost > pl->stats.grace) {
1577 draw_ext_info_format(
1578 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1579 "You don't have enough grace to write a scroll of %s.",
1580 chosen_spell->name);
1581 return 0;
1582 }
1583
1584 if (SP_level_spellpoint_cost(pl, chosen_spell, SPELL_MANA) > pl->stats.sp) {
1585 draw_ext_info_format(
1586 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1587 "You don't have enough mana to write a scroll of %s.",
1588 chosen_spell->name);
1589 return 0;
1590 }
1591
1592 // Prevent players from writing spells that they are denied.
1593 if (chosen_spell->path_attuned & pl->path_denied
1594 && settings.allow_denied_spells_writing == 0) {
1595 char name[MAX_BUF];
1596
1597 query_name(chosen_spell, name, MAX_BUF);
1598 draw_ext_info_format(
1599 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1600 "Just the idea of writing a scroll of %s makes you sick!",
1601 name);
1602 return 0;
1603 }
1604
1605 // If the scroll already had a spell written on it, the player could
1606 // accidentally read it while trying to write a new one. Give the player
1607 // a 50% chance to overwrite a spell at their own level.
1608 if ((scroll->stats.sp || scroll->inv) &&
1609 random_roll(0, scroll->level*2, pl, PREFER_LOW) > skill->level) {
1610 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1611 "Oops! You accidently read it while trying to write on it.");
1612 apply_manual(pl, scroll, 0);
1613 return 0;
1614 }
1615
1616 if (execute_event(scroll, EVENT_TRIGGER, pl, chosen_spell, NULL, 0) != 0) {
1617 return 0;
1618 }
1619
1620 // Find out if the player is confused or not.
1621 if (QUERY_FLAG(pl, FLAG_CONFUSED)) {
1622 confused = 1;
1623 }
1624
1625 // Mana or grace is lost no matter if the inscription is successful.
1626 pl->stats.grace -= SP_level_spellpoint_cost(pl, chosen_spell, SPELL_GRACE);
1627 pl->stats.sp -= SP_level_spellpoint_cost(pl, chosen_spell, SPELL_MANA);
1628
1629 if (random_roll(0, chosen_spell->level * 4 - 1, pl, PREFER_LOW) <
1630 skill->level) {
1631 // If there were multiple items in a stack, unstack one.
1632 if (scroll->nrof > 1) {
1633 newscroll = object_new();
1634 object_copy(scroll, newscroll);
1635 object_decrease_nrof_by_one(scroll);
1636 newscroll->nrof = 1;
1637 } else {
1638 newscroll = scroll;
1639 }
1640
1641 // Write spell if not confused; otherwise write random spell.
1642 newscroll->level = MAX(skill->level, chosen_spell->level);
1643
1644 if (!confused) {
1645 draw_ext_info(
1646 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_SUCCESS,
1647 "You succeed in writing a new scroll.");
1648 } else {
1649 chosen_spell = find_random_spell_in_ob(pl, NULL);
1650 if (!chosen_spell) {
1651 return 0;
1652 }
1653
1654 draw_ext_info(
1655 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1656 "In your confused state, you write down some odd spell.");
1657 }
1658
1659 if (newscroll->inv) {
1660 object *ninv;
1661 ninv = newscroll->inv;
1662 object_remove(ninv);
1663 object_free(ninv, FREE_OBJ_NO_DESTROY_CALLBACK);
1664 }
1665
1666 tmp = object_new();
1667 object_copy(chosen_spell, tmp);
1668 object_insert_in_ob(tmp, newscroll);
1669
1670 // This is needed so casting from the scroll works correctly with
1671 // moving_ball types, which checks attunements.
1672 newscroll->path_attuned = tmp->path_repelled;
1673
1674 // Same code as from treasure.c - so they can better merge.
1675 // If players want to sell them, so be it.
1676 newscroll->value = newscroll->arch->clone.value *
1677 newscroll->inv->value * (newscroll->level + 50 ) /
1678 (newscroll->inv->level + 50);
1679 newscroll->stats.exp = newscroll->value / 5;
1680
1681 // Finish manipulating the scroll before inserting it.
1682 if (newscroll == scroll) {
1683 // Remove object to stack correctly with other items.
1684 object_remove(newscroll);
1685 }
1686
1687 newscroll = object_insert_in_ob(newscroll, pl);
1688 success = calc_skill_exp(pl, newscroll, skill);
1689
1690 if (!confused) {
1691 success *= 2;
1692 }
1693
1694 success = success * skill->level;
1695 return success;
1696 }
1697
1698 // Inscription wasn't successful; do something bad to the player.
1699 if (chosen_spell->level > skill->level || confused) {
1700 draw_ext_info(
1701 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1702 "Ouch! Your attempt to write a new scroll strains your mind!");
1703
1704 // Either drain a stat or subtract experience.
1705 if (random_roll(0, 1, pl, PREFER_LOW) == 1) {
1706 drain_specific_stat(pl, 4);
1707 } else {
1708 confuse_living(pl, pl, 99);
1709 return -30 * chosen_spell->level;
1710 }
1711 } else if (random_roll(0, pl->stats.Int-1, pl, PREFER_HIGH) < 15) {
1712 draw_ext_info(
1713 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1714 "Your attempt to write a new scroll rattles your mind!");
1715 confuse_living(pl, pl, 99);
1716 } else {
1717 draw_ext_info(
1718 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1719 "You fail to write a new scroll.");
1720 }
1721
1722 return 0;
1723 }
1724
1725 /**
1726 * Implement the 'inscription' skill, which checks for the required skills and
1727 * marked items before running either write_note() or write_scroll().
1728 *
1729 * @param pl
1730 * Player attempting to write
1731 * @param params
1732 * Message to write, blank to write a spell
1733 * @param skill
1734 * Writing skill
1735 * @return
1736 * Experience gained from using the skill
1737 */
1738 int write_on_item(object *pl, const char *params, object *skill) {
1739 object *item;
1740 const char *string = params;
1741 int msgtype;
1742 archetype *skat;
1743
1744 // Only players can use the inscription skill.
1745 if (pl->type != PLAYER) {
1746 return 0;
1747 }
1748
1749 // No message was given, so both strings are set to empty.
1750 if (!params) {
1751 params = "";
1752 string = params;
1753 }
1754
1755 // You must be able to read before you can write.
1756 skat = get_archetype_by_type_subtype(SKILL, SK_LITERACY);
1757
1758 if (!find_skill_by_name(pl, skat->clone.skill)) {
1759 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_MISSING,
1760 "You must learn to read before you can write!");
1761 return 0;
1762 }
1763
1764 // You must not be blind to write, unless you're a DM.
1765 if (QUERY_FLAG(pl, FLAG_BLIND) && !QUERY_FLAG(pl, FLAG_WIZ)) {
1766 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1767 "You are unable to write while blind.");
1768 return 0;
1769 }
1770
1771 // If a message was given, write a book. Otherwise, write a scroll.
1772 if (string[0] != '\0') {
1773 msgtype = BOOK;
1774 } else {
1775 msgtype = SCROLL;
1776 }
1777
1778 // Find and attempt to write on the player's marked item.
1779 item = find_marked_object(pl);
1780 if (item == NULL) {
1781 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1782 "You haven't marked any items to write on yet.");
1783 return 0;
1784 }
1785
1786 // Don't let the player write on an unpaid item.
1787 if (QUERY_FLAG(item, FLAG_UNPAID)) {
1788 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1789 "You had better pay for that before you write on it!");
1790 return 0;
1791 }
1792
1793 // Check if the marked item is the type of item we're writing.
1794 if (msgtype != item->type) {
1795 draw_ext_info_format(
1796 NDI_UNIQUE, 0, pl, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1797 "You must mark a %s to write %s.",
1798 msgtype == BOOK ? "book" : "magic scroll",
1799 msgtype == BOOK ? "your message on" : "your spell down");
1800 return 0;
1801 }
1802
1803 if (msgtype == BOOK) {
1804 return write_note(pl, item, string);
1805 } else if (msgtype == SCROLL) {
1806 return write_scroll(pl, item, skill);
1807 } else {
1808 // This case should never be reached.
1809 abort();
1810 }
1811 }
1812
1813 /**
1814 * Find an object to throw.
1815 * If there is a suitable marked item, use it.
1816 * Else find the first item that is of the correct race.
1817 *
1818 * @param op
1819 * object wishing to throw.
1820 * @param race
1821 * 'throwing' race.
1822 * @return
1823 * throwable object, NULL if none suitable found.
1824 */
1825 static object *find_throw_ob(object *op, sstring race) {
1826 object *tmp;
1827 char name[MAX_BUF];
1828
1829 if (!op) { /* safety */
1830 LOG(llevError, "find_throw_ob(): confused! have a NULL thrower!\n");
1831 return (object *)NULL;
1832 }
1833
1834 /* prefer marked item */
1835 tmp = find_marked_object(op);
1836 if (tmp != NULL) {
1837 /* can't toss invisible or inv-locked items */
1838 if (tmp->invisible || QUERY_FLAG(tmp, FLAG_INV_LOCKED)) {
1839 tmp = NULL;
1840 }
1841 }
1842
1843 /* look through the inventory if no marked found */
1844 if (tmp == NULL) {
1845 FOR_INV_PREPARE(op, tmp2) {
1846 /* can't toss invisible items */
1847 if (tmp2->invisible)
1848 continue;
1849
1850 if (tmp2->type == CONTAINER && QUERY_FLAG(tmp2, FLAG_APPLIED) && tmp2->race == race) {
1851 tmp = find_throw_ob(tmp2, race);
1852 if (tmp != NULL) {
1853 break;
1854 }
1855 }
1856
1857 /* if not a container, then don't look if locked */
1858 if (tmp2->type == CONTAINER || QUERY_FLAG(tmp2, FLAG_INV_LOCKED))
1859 continue;
1860
1861 if (tmp2->race == race) {
1862 tmp = tmp2;
1863 break;
1864 }
1865
1866 } FOR_INV_FINISH();
1867 }
1868
1869 /* this should prevent us from throwing away
1870 * cursed items, worn armour, etc. Only weapons
1871 * can be thrown from 'hand'.
1872 */
1873 if (!tmp)
1874 return NULL;
1875
1876 if (QUERY_FLAG(tmp, FLAG_APPLIED)) {
1877 if (tmp->type != WEAPON) {
1878 query_name(tmp, name, MAX_BUF);
1879 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1880 "You can't throw %s.",
1881 name);
1882 tmp = NULL;
1883 } else if (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED)) {
1884 query_name(tmp, name, MAX_BUF);
1885 draw_ext_info_format(NDI_UNIQUE, 0, op,
1886 MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
1887 "The %s sticks to your hand!",
1888 name);
1889 tmp = NULL;
1890 } else {
1891 if (apply_special(op, tmp, AP_UNAPPLY|AP_NO_MERGE)) {
1892 LOG(llevError, "BUG: find_throw_ob(): couldn't unapply\n");
1893 tmp = NULL;
1894 }
1895 }
1896 } else if (QUERY_FLAG(tmp, FLAG_UNPAID)) {
1897 query_name(tmp, name, MAX_BUF);
1898 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1899 "You should pay for the %s first.",
1900 name);
1901 tmp = NULL;
1902 }
1903
1904 if (tmp && QUERY_FLAG(tmp, FLAG_INV_LOCKED)) {
1905 LOG(llevError, "BUG: find_throw_ob(): object is locked\n");
1906 tmp = NULL;
1907 }
1908 return tmp;
1909 }
1910
1911 /**
1912 * We construct the 'carrier' object in
1913 * which we will insert the object that is being thrown.
1914 * This combination becomes the 'thrown object'. -b.t.
1915 *
1916 * @param orig
1917 * object to wrap.
1918 * @return
1919 * object to throw.
1920 */
1921
1922 static object *make_throw_ob(object *orig) {
1923 object *toss_item;
1924
1925 if (!orig)
1926 return NULL;
1927
1928 toss_item = object_new();
1929 if (QUERY_FLAG(orig, FLAG_APPLIED)) {
1930 LOG(llevError, "BUG: make_throw_ob(): ob is applied\n");
1931 /* insufficient workaround, but better than nothing */
1932 CLEAR_FLAG(orig, FLAG_APPLIED);
1933 }
1934 object_copy(orig, toss_item);
1935 toss_item->type = THROWN_OBJ;
1936 CLEAR_FLAG(toss_item, FLAG_CHANGING);
1937 toss_item->stats.dam = 0; /* default damage */
1938 object_insert_in_ob(orig, toss_item);
1939 return toss_item;
1940 }
1941
1942
1943 /**
1944 * Op throws any object toss_item. This code
1945 * was borrowed from fire_bow.
1946 *
1947 * @param op
1948 * living thing throwing something.
1949 * @param part
1950 * part of op throwing.
1951 * @param toss_item
1952 * item thrown.
1953 * @param dir
1954 * direction to throw.
1955 * @param skill
1956 * throwing skill.
1957 * @retval 0
1958 * skill use failed.
1959 * @retval 1
1960 * skill was successfully used.
1961 * @todo this messy function should probably be simplified.
1962 */
1963 static int do_throw(object *op, object *part, object *toss_item, int dir, object *skill) {
1964 object *throw_ob = toss_item, *left = NULL;
1965 int eff_str = 0, str = op->stats.Str, dam = 0;
1966 int pause_f, weight_f = 0, mflags;
1967 float str_factor = 1.0, load_factor = 1.0, item_factor = 1.0;
1968 mapstruct *m;
1969 int16_t sx, sy;
1970 tag_t tag;
1971 char name[MAX_BUF];
1972
1973 if (throw_ob == NULL) {
1974 if (op->type == PLAYER) {
1975 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1976 "You have nothing to throw.");
1977 }
1978 return 0;
1979 }
1980 if (QUERY_FLAG(throw_ob, FLAG_STARTEQUIP)) {
1981 if (op->type == PLAYER) {
1982 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
1983 "The gods won't let you throw that.");
1984 }
1985 return 0;
1986 }
1987
1988 /* Lauwenmark - Now we can call the associated script_throw event (if any) */
1989 tag = throw_ob->count;
1990 execute_event(throw_ob, EVENT_THROW, op, NULL, NULL, SCRIPT_FIX_ACTIVATOR);
1991 if (object_was_destroyed(throw_ob, tag)) {
1992 return 1;
1993 }
1994
1995 /* Because throwing effectiveness must be reduced by the
1996 * encumbrance of the thrower and weight of the object. THus,
1997 * we use the concept of 'effective strength' as defined below.
1998 */
1999
2000 /* if str exceeds settings.max_stat (30, eg giants), lets assign a str_factor > 1 */
2001 if (str > settings.max_stat) {
2002 str_factor = (float)str/(float)settings.max_stat;
2003 str = settings.max_stat;
2004 }
2005
2006 /* the more we carry, the less we can throw. Limit only on players */
2007 if (op->type == PLAYER
2008 && op->carrying > (get_weight_limit(op->stats.Str)*FREE_PLAYER_LOAD_PERCENT)
2009 && (FREE_PLAYER_LOAD_PERCENT < 1.0)) {
2010 /**
2011 * Fixed logic here
2012 *
2013 * The old code made you have a better factor the more encumbered past the LOAD_PCT
2014 * you were. This new way essentially flips the variable to be higher at lower encumbrance.
2015 *
2016 * It also only encumbers throwing once carryng more than FREE_PLAYER_LOAD_PERCENT of our capacity,
2017 * since we have a MIN on load_factor when we use it, so before the limit it is clipped to 1.0.
2018 *
2019 * Also ,use 2.0f since op->carrying/(get_weight_limit(op->stats.Str)*FREE_PLAYER_LOAD_PERCENT) = 1 if
2020 * they are equal. As op->carrying increases, this slides higher, making the factor lower.
2021 *
2022 * SilverNexus 2019-01-03
2023 */
2024 load_factor = 2.0f - (float)(op->carrying) / (float)(get_weight_limit(op->stats.Str)*FREE_PLAYER_LOAD_PERCENT);
2025 // Only clip to 1.0 if we get in here, since it is 1.0 if we do not get in here.
2026 load_factor = MIN(load_factor, 1.0f);
2027 }
2028
2029 /* lighter items are thrown harder, farther, faster */
2030 if (throw_ob->weight > 0)
2031 item_factor = (float)(get_weight_limit(op->stats.Str)*FREE_PLAYER_LOAD_PERCENT)/(float)(3.0*throw_ob->weight);
2032 else { /* 0 or negative weight?!? Odd object, can't throw it */
2033 query_name(throw_ob, name, MAX_BUF);
2034 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_ERROR,
2035 "You can't throw %s.",
2036 name);
2037 return 0;
2038 }
2039
2040 eff_str = str*load_factor;
2041 eff_str = (float)eff_str*item_factor*str_factor;
2042
2043 /* alas, arrays limit us to a value of settings.max_stat (30). Use str_factor to
2044 * account for super-strong throwers. */
2045 if (eff_str > settings.max_stat)
2046 eff_str = settings.max_stat;
2047
2048 #ifdef DEBUG_THROW
2049 LOG(llevDebug, "%s carries %d, eff_str=%d\n", op->name, op->carrying, eff_str);
2050 LOG(llevDebug, " max_c=%d, item_f=%f, load_f=%f, str=%d\n", (get_weight_limit(op->stats.Str)*FREE_PLAYER_LOAD_PERCENT), item_factor, load_factor, op->stats.Str);
2051 LOG(llevDebug, " str_factor=%f\n", str_factor);
2052 LOG(llevDebug, " item %s weight= %d\n", throw_ob->name, throw_ob->weight);
2053 #endif
2054
2055 /* 3 things here prevent a throw, you aimed at your feet, you
2056 * have no effective throwing strength, or you threw at something
2057 * that flying objects can't get through.
2058 */
2059 mflags = get_map_flags(part->map, &m, part->x+freearr_x[dir], part->y+freearr_y[dir], &sx, &sy);
2060
2061 if (!dir
2062 || (eff_str <= 1)
2063 || (mflags&P_OUT_OF_MAP)
2064 || (GET_MAP_MOVE_BLOCK(m, sx, sy)&MOVE_FLY_LOW)) {
2065 /* bounces off 'wall', and drops to feet */
2066 object_remove(throw_ob);
2067 object_insert_in_map_at(throw_ob, part->map, op, 0, part->x, part->y);
2068 if (op->type == PLAYER) {
2069 if (eff_str <= 1) {
2070 query_name(throw_ob, name, MAX_BUF);
2071 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
2072 "Your load is so heavy you drop %s to the ground.",
2073 name);
2074 } else if (!dir) {
2075 query_name(throw_ob, name, MAX_BUF);
2076 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
2077 "You throw %s at the ground.",
2078 name);
2079 } else
2080 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
2081 "Something is in the way.");
2082 }
2083 return 0;
2084 } /* if object can't be thrown */
2085
2086 left = throw_ob; /* these are throwing objects left to the player */
2087
2088 /* sometimes object_split() can't split an object (because op->nrof==0?)
2089 * and returns NULL. We must use 'left' then
2090 */
2091 throw_ob = object_split(throw_ob, 1, NULL, 0);
2092 if (throw_ob == NULL) {
2093 throw_ob = left;
2094 object_remove(left);
2095 }
2096
2097 /* special case: throwing powdery substances like dust, dirt */
2098 if (throw_ob->type == POTION && throw_ob->subtype == POT_DUST) {
2099 cast_dust(op, throw_ob, dir);
2100 return 1;
2101 }
2102
2103 /* Make a thrown object -- insert real object in a 'carrier' object.
2104 * If unsuccessfull at making the "thrown_obj", we just reinsert
2105 * the original object back into inventory and exit
2106 */
2107 toss_item = make_throw_ob(throw_ob);
2108 if (toss_item) {
2109 throw_ob = toss_item;
2110 if (throw_ob->skill)
2111 free_string(throw_ob->skill);
2112 throw_ob->skill = add_string(skill->skill);
2113 } else {
2114 object_insert_in_ob(throw_ob, op);
2115 return 0;
2116 }
2117
2118 object_set_owner(throw_ob, op);
2119 /* At some point in the attack code, the actual real object (op->inv)
2120 * becomes the hitter. As such, we need to make sure that has a proper
2121 * owner value so exp goes to the right place.
2122 */
2123 object_set_owner(throw_ob->inv, op);
2124 throw_ob->direction = dir;
2125
2126 /* the damage bonus from the force of the throw */
2127 dam = str_factor*get_dam_bonus(eff_str);
2128
2129 /* Now, lets adjust the properties of the thrown_ob. */
2130
2131 /* how far to fly */
2132 throw_ob->last_sp = (eff_str*3)/5;
2133
2134 /* speed */
2135 throw_ob->speed = (get_speed_bonus(eff_str)+1.0)/1.5;
2136 throw_ob->speed = MIN(1.0, throw_ob->speed); /* no faster than an arrow! */
2137
2138 /* item damage. Eff_str and item weight influence damage done */
2139 weight_f = MIN(throw_ob->weight/2000, settings.max_stat);
2140 throw_ob->stats.dam += (dam/3)+get_dam_bonus(weight_f)+(throw_ob->weight/15000)-2;
2141
2142 /* chance of breaking. Proportional to force used and weight of item */
2143 throw_ob->stats.food = (dam/2)+(throw_ob->weight/60000);
2144
2145 /* replace 25 with a call to clone.arch wc? messes up w/ NPC */
2146 throw_ob->stats.wc = 25-get_dex_bonus(op->stats.Dex)-get_thaco_bonus(eff_str)-skill->level;
2147
2148
2149 /* the properties of objects which are meant to be thrown (ie dart,
2150 * throwing knife, etc) will differ from ordinary items. Lets tailor
2151 * this stuff in here.
2152 */
2153
2154 if (QUERY_FLAG(throw_ob->inv, FLAG_IS_THROWN)) {
2155 throw_ob->last_sp += eff_str/3; /* fly a little further */
2156 throw_ob->stats.dam += throw_ob->inv->stats.dam+throw_ob->magic+2;
2157 throw_ob->stats.wc -= throw_ob->magic+throw_ob->inv->stats.wc;
2158 /* only throw objects get directional faces */
2159 if (GET_ANIM_ID(throw_ob) && NUM_ANIMATIONS(throw_ob))
2160 object_update_turn_face(throw_ob);
2161 } else {
2162 /* some materials will adjust properties.. */
2163 if (throw_ob->material&M_LEATHER) {
2164 throw_ob->stats.dam -= 1;
2165 throw_ob->stats.food -= 10;
2166 }
2167 if (throw_ob->material&M_GLASS)
2168 throw_ob->stats.food += 60;
2169
2170 if (throw_ob->material&M_ORGANIC) {
2171 throw_ob->stats.dam -= 3;
2172 throw_ob->stats.food += 55;
2173 }
2174 if (throw_ob->material&M_PAPER || throw_ob->material&M_CLOTH) {
2175 throw_ob->stats.dam -= 5;
2176 throw_ob->speed *= 0.8;
2177 throw_ob->stats.wc += 3;
2178 throw_ob->stats.food -= 30;
2179 }
2180 /* light obj have more wind resistance, fly slower*/
2181 if (throw_ob->weight > 500)
2182 throw_ob->speed *= 0.8;
2183 if (throw_ob->weight > 50)
2184 throw_ob->speed *= 0.5;
2185 } /* else tailor thrown object */
2186
2187 /* some limits, and safeties (needed?) */
2188 if (throw_ob->stats.dam < 0)
2189 throw_ob->stats.dam = 0;
2190 if (throw_ob->last_sp > eff_str)
2191 throw_ob->last_sp = eff_str;
2192 if (throw_ob->stats.food < 0)
2193 throw_ob->stats.food = 0;
2194 if (throw_ob->stats.food > 100)
2195 throw_ob->stats.food = 100;
2196 if (throw_ob->stats.wc > 30)
2197 throw_ob->stats.wc = 30;
2198
2199 /* how long to pause the thrower. Higher values mean less pause */
2200 pause_f = ((2*eff_str)/3)+20+skill->level;
2201
2202 /* Put a lower limit on this */
2203 if (pause_f < 10)
2204 pause_f = 10;
2205 if (pause_f > 100)
2206 pause_f = 100;
2207
2208 /* Changed in 0.94.2 - the calculation before was really goofy.
2209 * In short summary, a throw can take anywhere between speed 5 and
2210 * speed 0.5
2211 */
2212 op->speed_left -= 50.0/pause_f;
2213
2214 object_update_speed(throw_ob);
2215 throw_ob->speed_left = 0;
2216
2217 throw_ob->move_type = MOVE_FLY_LOW;
2218 throw_ob->move_on = MOVE_FLY_LOW|MOVE_WALK;
2219
2220 #ifdef DEBUG_THROW
2221 LOG(llevDebug, " pause_f=%d \n", pause_f);
2222 LOG(llevDebug, " %s stats: wc=%d dam=%d dist=%d spd=%f break=%d\n", throw_ob->name, throw_ob->stats.wc, throw_ob->stats.dam, throw_ob->last_sp, throw_ob->speed, throw_ob->stats.food);
2223 LOG(llevDebug, "inserting tossitem (%d) into map\n", throw_ob->count);
2224 #endif
2225 tag = throw_ob->count;
2226 object_insert_in_map_at(throw_ob, part->map, op, 0, part->x, part->y);
2227 if (!object_was_destroyed(throw_ob, tag))
2228 ob_process(throw_ob);
2229 return 1;
2230 }
2231
2232 /**
2233 * Throwing skill handling.
2234 * @param op
2235 * object throwing.
2236 * @param part
2237 * actual part of op throwing.
2238 * @param dir
2239 * direction to throw into.
2240 * @param skill
2241 * throwing skill.
2242 * @retval 0
2243 * skill use failed.
2244 * @retval 1
2245 * skill was successfully used.
2246 */
2247 int skill_throw(object *op, object *part, int dir, object *skill) {
2248 object *throw_ob;
2249
2250 if (op->type == PLAYER)
2251 throw_ob = find_throw_ob(op, find_string("throwing"));
2252 else
2253 throw_ob = monster_find_throw_ob(op);
2254
2255 return do_throw(op, part, throw_ob, dir, skill);
2256 }