ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skills.C
Revision: 1.113
Committed: Sat Nov 17 23:40:04 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.112: +1 -0 lines
Log Message:
copyright update 2018

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 * Copyright (©) 2003 Mark Wedel & Crossfire Development Team
7 * Copyright (©) 1992 Frank Tore Johansen
8 *
9 * Deliantra is free software: you can redistribute it and/or modify it under
10 * the terms of the Affero GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the Affero GNU General Public License
20 * and the GNU General Public License along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 *
23 * The authors can be reached via e-mail to <support@deliantra.net>
24 */
25
26 #include <global.h>
27 #include <object.h>
28 #include <sproto.h>
29 #include <living.h>
30 #include <skills.h>
31 #include <spells.h>
32 #include <book.h>
33
34 /* adj_stealchance() - increased values indicate better attempts */
35 static int
36 adj_stealchance (object *op, object *victim, int roll)
37 {
38 object *equip;
39
40 if (!op || !victim || !roll)
41 return -1;
42
43 /* Only prohibit stealing if the player does not have a free
44 * hand available and in fact does have hands.
45 */
46 if (op->type == PLAYER && op->slot[body_arm].used <= 0 && op->slot[body_arm].info)
47 {
48 op->failmsg ("But you have no free hands to steal with! "
49 "H<Try to unapply weapons or things you hold in your hands, use the C<body> command.>");
50 return -1;
51 }
52
53 /* ADJUSTMENTS */
54
55 /* It's harder to steal from hostile beings! */
56 if (!victim->flag [FLAG_UNAGGRESSIVE])
57 roll = roll / 2;
58
59 /* Easier to steal from sleeping beings, or if the thief is
60 * unseen */
61 if (victim->flag [FLAG_SLEEP])
62 roll = roll * 3;
63 else if (op->invisible)
64 roll = roll * 2;
65
66 /* check stealing 'encumberance'. Having this equipment applied makes
67 * it quite a bit harder to steal.
68 */
69 for (equip = op->inv; equip; equip = equip->below)
70 {
71 if (equip->type == WEAPON && equip->flag [FLAG_APPLIED])
72 roll -= equip->weight / 10000;
73
74 if (equip->type == BOW && equip->flag [FLAG_APPLIED])
75 roll -= equip->weight / 5000;
76
77 if (equip->type == SHIELD && equip->flag [FLAG_APPLIED])
78 roll -= equip->weight / 2000;
79
80 if (equip->type == ARMOUR && equip->flag [FLAG_APPLIED])
81 roll -= equip->weight / 5000;
82
83 if (equip->type == GLOVES && equip->flag [FLAG_APPLIED])
84 roll -= equip->weight / 100;
85 }
86
87 if (roll < 0)
88 roll = 0;
89
90 return roll;
91 }
92
93 /*
94 * When stealing: dependent on the intelligence/wisdom of whom you're
95 * stealing from (op in attempt_steal), offset by your dexterity and
96 * skill at stealing. They may notice your attempt, whether successful
97 * or not.
98 * op is the target (person being pilfered)
99 * who is the person doing the stealing.
100 * skill is the skill object (stealing).
101 */
102 static int
103 attempt_steal (object *op, object *who, object *skill)
104 {
105 object *success = NULL, *tmp = NULL, *next;
106 int roll = 0, chance = 0, stats_value;
107 rv_vector rv;
108
109 stats_value = (who->stats.Dex + who->stats.Int) * 3 / 2;
110
111 /* if the victim is aware of a thief in the area (FLAG_NO_STEAL set on them)
112 * they will try to prevent stealing if they can. Only unseen theives will
113 * have much chance of success.
114 */
115 if (!op->is_player () && op->flag [FLAG_NO_STEAL])
116 {
117 if (can_detect_enemy (op, who, &rv))
118 {
119 npc_call_help (op);
120 op->clr_flag (FLAG_UNAGGRESSIVE);
121 who->failmsg ("Your attempt is prevented!");
122 return 0;
123 }
124 else /* help npc to detect thief next time by raising its wisdom */
125 op->stats.Wis += (op->stats.Int / 5) + 1;
126
127 if (op->stats.Wis > MAX_STAT)
128 op->stats.Wis = MAX_STAT;
129 }
130
131 if (op->is_player () && op->flag [FLAG_WIZ])
132 {
133 who->failmsg ("You can't steal from the dungeon master!\n");
134 return 0;
135 }
136
137 // only allow stealing between hostile players (TODO: probably should change)
138 if (op->is_player () && who->is_player () && (who->contr->peaceful || op->contr->peaceful))
139 {
140 who->failmsg ("You can't steal from other players!\n");
141 return 0;
142 }
143
144 /* Ok then, go through their inventory, stealing */
145 for (tmp = op->inv; tmp; tmp = next)
146 {
147 next = tmp->below;
148
149 /* you can't steal worn items, starting items, wiz stuff,
150 * innate abilities, or items w/o a type. Generally
151 * speaking, the invisibility flag prevents experience or
152 * abilities from being stolen since these types are currently
153 * always invisible objects. I was implicit here so as to prevent
154 * future possible problems. -b.t.
155 * Flesh items generated w/ fix_flesh_item should have FLAG_NO_STEAL
156 * already -b.t.
157 */
158
159 if (tmp->flag [FLAG_APPLIED]
160 || !tmp->type
161 || tmp->type == SPELL
162 || tmp->flag [FLAG_STARTEQUIP]
163 || tmp->flag [FLAG_NO_STEAL]
164 || tmp->invisible)
165 continue;
166
167 /* Okay, try stealing this item. Dependent on dexterity of thief,
168 * skill level, see the adj_stealroll fctn for more detail.
169 */
170
171 roll = die_roll (2, 100, who, PREFER_LOW) / 2; /* weighted 1-100 */
172
173 if ((chance = adj_stealchance (who, op, (stats_value + skill->level * 10 - op->level * 3))) == -1)
174 return 0;
175 else if (roll < chance)
176 {
177 pick_up (who, tmp);
178 /* need to see if the player actually stole this item -
179 * if it is in the players inv, assume it is. This prevents
180 * abuses where the player can not carry the item, so just
181 * keeps stealing it over and over.
182 */
183 if (tmp->destroyed () || tmp->env != op)
184 {
185 /* for players, play_sound: steals item */
186 success = tmp;
187 tmp->clr_flag (FLAG_INV_LOCKED);
188 }
189
190 break;
191 }
192 } /* for loop looking for an item */
193
194 if (!tmp)
195 {
196 new_draw_info_format (NDI_UNIQUE, 0, who, "%s%s has nothing you can steal!", op->type == PLAYER ? "" : "The ", query_name (op));
197 return 0;
198 }
199
200 /* If you arent high enough level, you might get something BUT
201 * the victim will notice your stealing attempt. Ditto if you
202 * attempt to steal something heavy off them, they're bound to notice
203 */
204
205 if ((roll >= skill->level) || !chance
206 || (tmp && tmp->weight > (250 * (random_roll (0, stats_value + skill->level * 10 - 1, who, PREFER_LOW)))))
207 {
208 /* victim figures out where the thief is! */
209 if (who->flag [FLAG_HIDDEN])
210 make_visible (who);
211
212 if (op->type != PLAYER)
213 {
214 /* The unaggressives look after themselves 8) */
215 if (who->type == PLAYER)
216 {
217 npc_call_help (op);
218 new_draw_info_format (NDI_UNIQUE, 0, who, "%s notices your attempted pilfering!", query_name (op));
219 }
220
221 op->clr_flag (FLAG_UNAGGRESSIVE);
222 /* all remaining npc items are guarded now. Set flag NO_STEAL
223 * on the victim.
224 */
225 op->set_flag (FLAG_NO_STEAL);
226 }
227 else
228 { /* stealing from another player */
229 char buf[MAX_BUF];
230
231 /* Notify the other player */
232 if (success && who->stats.Int > random_roll (0, 19, op, PREFER_LOW))
233 sprintf (buf, "Your %s is missing!", query_name (success));
234 else
235 sprintf (buf, "Your pack feels strangely lighter.");
236
237 new_draw_info (NDI_UNIQUE, 0, op, buf);
238 if (!success)
239 {
240 if (who->invisible)
241 sprintf (buf, "you feel itchy fingers getting at your pack.");
242 else
243 sprintf (buf, "%s looks very shifty.", query_name (who));
244
245 new_draw_info (NDI_UNIQUE, 0, op, buf);
246 }
247 } /* else stealing from another player */
248 /* play_sound("stop! thief!"); kindofthing */
249 } /* if you weren't 100% successful */
250
251 return success ? 1 : 0;
252 }
253
254 int
255 steal (object *op, int dir, object *skill)
256 {
257 object *tmp, *next;
258 sint16 x, y;
259 maptile *m;
260 int mflags;
261
262 x = op->x + DIRX (dir);
263 y = op->y + DIRY (dir);
264
265 if (dir == 0)
266 return 0; // you can't steal from ourself!
267
268 m = op->map;
269 mflags = get_map_flags (m, &m, x, y, &x, &y);
270 /* Out of map - can't do it. If nothing alive on this space,
271 * don't need to look any further.
272 */
273 if ((mflags & P_OUT_OF_MAP) || !(mflags & P_IS_ALIVE))
274 return 0;
275
276 /* If player can't move onto the space, can't steal from it. */
277 if (OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, x, y)))
278 return 0;
279
280 /* Find the topmost object at this spot */
281 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL && tmp->above != NULL; tmp = tmp->above);
282
283 /* For all the stacked objects at this point, attempt a steal */
284 for (; tmp; tmp = next)
285 {
286 next = tmp->below;
287 /* Minor hack--for multi square beings - make sure we get
288 * the 'head' coz 'tail' objects have no inventory! - b.t.
289 */
290 if (tmp->head)
291 tmp = tmp->head;
292
293 if (tmp->type != PLAYER && !tmp->flag [FLAG_MONSTER])
294 continue;
295
296 if (attempt_steal (tmp, op, skill))
297 {
298 if (tmp->type == PLAYER) /* no xp for stealing from another player */
299 return 0;
300
301 /* no xp for stealing from pets (of players) */
302 if (tmp->flag [FLAG_FRIENDLY] && tmp->attack_movement == PETMOVE)
303 {
304 object *owner = tmp->owner;
305
306 if (owner && owner->type == PLAYER)
307 return 0;
308 }
309
310 // reduce monster experience by experience we gained, as to
311 // limit the amount of exp that can be gained by stealing from monsters
312 // (jessies gave ~20,000,000 exp otherwise.
313 int exp = calc_skill_exp (op, tmp, skill);
314
315 exp = min (tmp->stats.exp, exp);
316 tmp->stats.exp -= exp;
317 return exp;
318 }
319 }
320
321 return 0;
322 }
323
324 static int
325 attempt_pick_lock (object *door, object *pl, object *skill)
326 {
327 int difficulty = pl->map->difficulty ? pl->map->difficulty : 0;
328 int success = 0, number; /* did we get anything? */
329
330
331 /* Try to pick the lock on this item (doors only for now).
332 * Dependent on dexterity/skill SK_level of the player and
333 * the map level difficulty.
334 */
335 number = (die_roll (2, 40, pl, PREFER_LOW) - 2) / 2;
336 if (number < (pl->stats.Dex + skill->level - difficulty))
337 {
338 remove_door (door);
339 success = 1;
340 }
341 else if (door->inv && (door->inv->type == RUNE || door->inv->type == TRAP))
342 { /* set off any traps? */
343 spring_trap (door->inv, pl);
344 }
345 return success;
346 }
347
348
349 /* Implementation by bt. (thomas@astro.psu.edu)
350 * monster implementation 7-7-95 by bt.
351 */
352
353 int
354 pick_lock (object *pl, int dir, object *skill)
355 {
356 object *tmp;
357
358 if (!dir)
359 dir = pl->facing;
360
361 mapxy pos (pl); pos.move (dir);
362
363 /* For all the stacked objects at this point find a door */
364 if (!pos.normalise ())
365 {
366 pl->failmsg ("There is no lock there.");
367 return 0;
368 }
369
370 for (tmp = pos->bot; tmp; tmp = tmp->above)
371 if (tmp->type == DOOR || tmp->type == LOCKED_DOOR)
372 break;
373
374 if (!tmp)
375 {
376 pl->failmsg ("There is no lock there.");
377 return 0;
378 }
379
380 if (tmp->type == LOCKED_DOOR)
381 {
382 pl->failmsg ("You can't pick that lock! H<You need the right key, no matter how skilled you might be.>");
383 return 0;
384 }
385
386 if (!tmp->move_block)
387 {
388 pl->failmsg ("The door has no lock!");
389 return 0;
390 }
391
392 if (attempt_pick_lock (tmp, pl, skill))
393 {
394 new_draw_info (NDI_UNIQUE, 0, pl, "You pick the lock.");
395 return calc_skill_exp (pl, NULL, skill);
396 }
397 else
398 {
399 new_draw_info (NDI_UNIQUE, 0, pl, "You fail to pick the lock.");
400 return 0;
401 }
402 }
403
404 /* HIDE CODE. The user becomes undetectable (not just 'invisible') for
405 * a short while (success and duration dependant on player SK_level,
406 * dexterity, charisma, and map difficulty).
407 * Players have a good chance of becoming 'unhidden' if they move
408 * and like invisiblity will be come visible if they attack
409 * Implemented by b.t. (thomas@astro.psu.edu)
410 * July 7, 1995 - made hiding possible for monsters. -b.t.
411 */
412 static int
413 attempt_hide (object *op, object *skill)
414 {
415 int number, difficulty = op->map->difficulty;
416 int terrain = hideability (op);
417
418 if (terrain < -10) /* not enough cover here */
419 return 0;
420
421 /* Hiding success and duration dependant on skill level,
422 * op->stats.Dex, map difficulty and terrain.
423 */
424 number = (die_roll (2, 25, op, PREFER_LOW) - 2) / 2;
425
426 if (!stand_near_hostile (op) && (number < (op->stats.Dex + skill->level + terrain - difficulty)))
427 {
428 op->invisible += 100; /* set the level of 'hiddeness' */
429
430 if (op->type == PLAYER)
431 op->contr->tmp_invis = 1;
432
433 op->flag [FLAG_HIDDEN] = 1;
434 return 1;
435 }
436
437 return 0;
438 }
439
440 /* patched this to take terrain into consideration */
441 int
442 hide (object *op, object *skill)
443 {
444 /* the preliminaries -- Can we really hide now? */
445 /* this keeps monsters from using invisibilty spells and hiding */
446
447 if (op->flag [FLAG_MAKE_INVIS])
448 {
449 new_draw_info (NDI_UNIQUE, 0, op, "You don't need to hide while invisible!");
450 return 0;
451 }
452 else if (!op->flag [FLAG_HIDDEN] && op->invisible > 0 && op->type == PLAYER)
453 {
454 new_draw_info (NDI_UNIQUE, 0, op, "Your attempt to hide breaks the invisibility spell!");
455 make_visible (op);
456 }
457
458 if (op->invisible > 50 * skill->level)
459 {
460 new_draw_info (NDI_UNIQUE, 0, op, "You are as hidden as you can get.");
461 return 0;
462 }
463
464 if (attempt_hide (op, skill))
465 {
466 new_draw_info (NDI_UNIQUE, 0, op, "You hide in the shadows.");
467 update_object (op, UP_OBJ_FACE);
468 return calc_skill_exp (op, NULL, skill);
469 }
470
471 new_draw_info (NDI_UNIQUE, 0, op, "You fail to conceal yourself.");
472 return 0;
473 }
474
475 /* stop_jump() - End of jump. Clear flags, restore the map, and
476 * freeze the jumper a while to simulate the exhaustion
477 * of jumping.
478 */
479 static void
480 stop_jump (object *pl, int dist, int spaces)
481 {
482 pl->update_stats ();
483 pl->map->insert (pl, pl->x, pl->y, pl);
484 pl->speed_left -= pl->speed * 8.;
485 }
486
487 static int
488 attempt_jump (object *pl, int dir, int spaces, object *skill)
489 {
490 object *tmp;
491 int i, dx = DIRX (dir), dy = DIRY (dir), mflags;
492 sint16 x, y;
493 maptile *m;
494
495 /* Jump loop. Go through spaces object wants to jump. Halt the
496 * jump if a wall or creature is in the way. We set FLAG_FLYING
497 * temporarily to allow player to aviod exits/archs that are not
498 * fly_on, fly_off. This will also prevent pickup of objects
499 * while jumping over them.
500 */
501 pl->remove ();
502 pl->move_type |= MOVE_FLY_LOW;
503
504 for (i = 0; i <= spaces; i++)
505 {
506 x = pl->x + dx;
507 y = pl->y + dy;
508 m = pl->map;
509
510 mflags = get_map_flags (m, &m, x, y, &x, &y);
511
512 if (mflags & P_OUT_OF_MAP)
513 {
514 stop_jump (pl, i, spaces);
515 return 0;
516 }
517
518 for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
519 {
520 /* Jump into creature */
521 if (tmp->flag [FLAG_MONSTER] || tmp->type == PLAYER)
522 {
523 new_draw_info_format (NDI_UNIQUE, 0, pl, "You jump into %s%s.", tmp->type == PLAYER ? "" : "the ", &tmp->name);
524
525 stop_jump (pl, i, spaces);
526
527 int exp = 0;
528
529 if (tmp->type != PLAYER
530 || (pl->type == PLAYER && !pl->contr->party)
531 || (pl->type == PLAYER && tmp->type == PLAYER && pl->contr->party != tmp->contr->party))
532 exp = skill_attack (tmp, pl, pl->facing, "kicked", skill); /* pl makes an attack */
533
534 return exp; /* note that calc_skill_exp() is already called by skill_attack() */
535 }
536 }
537
538 if (OB_TYPE_MOVE_BLOCK (pl, GET_MAP_MOVE_BLOCK (m, x, y)))
539 {
540 new_draw_info (NDI_UNIQUE, 0, pl, "Your jump is blocked.");
541 stop_jump (pl, i, spaces);
542 return 0;
543 }
544
545 pl->x = x;
546 pl->y = y;
547 pl->map = m;
548
549 if (m->at (x, y).move_on & pl->move_type)
550 {
551 new_draw_info (NDI_UNIQUE, 0, pl, "Your jump is stopped.");
552 stop_jump (pl, i, spaces);
553 return 0;
554 }
555 }
556
557 stop_jump (pl, i, spaces);
558
559 return calc_skill_exp (pl, NULL, skill);
560 }
561
562 /* jump() - this is both a new type of movement for player/monsters and
563 * an attack as well.
564 * Perhaps we should allow more spaces based on level, eg, level 50
565 * jumper can jump several spaces?
566 */
567 int
568 jump (object *pl, int dir, object *skill)
569 {
570 int str = pl->stats.Str;
571 int dex = pl->stats.Dex;
572
573 dex = dex ? dex : 15;
574 str = str ? str : 10;
575
576 int stats = str * str * str * dex * skill->level;
577 int spaces = min (3, skill->level, stats / (pl->carrying + 1));
578
579 if (spaces == 0)
580 {
581 pl->failmsg ("You are carrying too much weight to jump.");
582 return 0;
583 }
584
585 return attempt_jump (pl, dir, spaces, skill);
586 }
587
588 /* skill_ident() - this code is supposed to allow players to identify
589 * classes of objects with the various "auto-ident" skills. Player must
590 * have unidentified objects of the right type in order for the skill
591 * to work. While multiple classes of objects may be identified,
592 * this code is kind of yucky -- it would be nice to make it a bit
593 * more generalized. Right now, skill indices are embedded in this routine.
594 * Returns amount of experience gained (on successful ident).
595 * - b.t. (thomas@astro.psu.edu)
596 */
597 static int
598 do_skill_detect_curse (object *pl, object *skill)
599 {
600 int success = 0;
601
602 for (object *tmp = pl->inv; tmp; tmp = tmp->below)
603 if (!tmp->invisible
604 && tmp->need_identify ()
605 && !tmp->flag [FLAG_IDENTIFIED] && !tmp->flag [FLAG_KNOWN_CURSED]
606 && (tmp->flag [FLAG_CURSED] || tmp->flag [FLAG_DAMNED])
607 && tmp->item_power < skill->level)
608 {
609 tmp->set_flag (FLAG_KNOWN_CURSED);
610 esrv_update_item (UPD_FLAGS, pl, tmp);
611 success += calc_skill_exp (pl, tmp, skill);
612 }
613
614 /* Check ground, too, but only objects the player could pick up */
615 for (object *tmp = GET_MAP_OB (pl->map, pl->x, pl->y); tmp; tmp = tmp->above)
616 if (can_pick (pl, tmp)
617 && tmp->need_identify ()
618 && !tmp->flag [FLAG_IDENTIFIED] && !tmp->flag [FLAG_KNOWN_CURSED]
619 && (tmp->flag [FLAG_CURSED] || tmp->flag [FLAG_DAMNED])
620 && tmp->item_power < skill->level)
621 {
622 tmp->set_flag (FLAG_KNOWN_CURSED);
623 esrv_update_item (UPD_FLAGS, pl, tmp);
624 success += calc_skill_exp (pl, tmp, skill);
625 }
626
627 return success;
628 }
629
630 static int
631 do_skill_detect_magic (object *pl, object *skill)
632 {
633 int success = 0;
634
635 for (object *tmp = pl->inv; tmp; tmp = tmp->below)
636 if (!tmp->invisible
637 && tmp->need_identify ()
638 && !tmp->flag [FLAG_IDENTIFIED] && !tmp->flag [FLAG_KNOWN_MAGICAL]
639 && is_magical (tmp)
640 && tmp->item_power < skill->level)
641 {
642 tmp->set_flag (FLAG_KNOWN_MAGICAL);
643 esrv_update_item (UPD_FLAGS, pl, tmp);
644 success += calc_skill_exp (pl, tmp, skill);
645 }
646
647 /* Check ground, too, but like above, only if the object can be picked up */
648 for (object *tmp = GET_MAP_OB (pl->map, pl->x, pl->y); tmp; tmp = tmp->above)
649 if (can_pick (pl, tmp)
650 && tmp->need_identify ()
651 && !tmp->flag [FLAG_IDENTIFIED] && !tmp->flag [FLAG_KNOWN_MAGICAL]
652 && is_magical (tmp)
653 && tmp->item_power < skill->level)
654 {
655 tmp->set_flag (FLAG_KNOWN_MAGICAL);
656 esrv_update_item (UPD_FLAGS, pl, tmp);
657 success += calc_skill_exp (pl, tmp, skill);
658 }
659
660 return success;
661 }
662
663 /* Helper function for do_skill_ident, so that we can loop
664 * over inventory AND objects on the ground conveniently.
665 */
666 static int
667 do_skill_ident2 (object *tmp, object *pl, int obj_class, object *skill)
668 {
669 int success = 0, chance;
670 int skill_value = skill->level * pl->stats.Int ? pl->stats.Int : 10;
671
672 if (!tmp->flag [FLAG_IDENTIFIED]
673 && !tmp->flag [FLAG_NO_SKILL_IDENT]
674 && tmp->need_identify ()
675 && !tmp->invisible
676 && tmp->type == obj_class)
677 {
678 chance = die_roll (3, 10, pl, PREFER_LOW) - 3 + rndm (0, (tmp->magic ? tmp->magic * 5 : 1) - 1);
679
680 if (skill_value >= chance)
681 {
682 identify (tmp);
683
684 if (pl->type == PLAYER)
685 {
686 new_draw_info_format (NDI_UNIQUE, 0, pl, "You identify %s.", long_desc (tmp, pl));
687
688 if (tmp->msg)
689 {
690 new_draw_info (NDI_UNIQUE, 0, pl, "The item has a story:");
691 new_draw_info (NDI_UNIQUE, 0, pl, tmp->msg);
692 }
693 }
694
695 success += calc_skill_exp (pl, tmp, skill);
696 }
697 else
698 tmp->set_flag (FLAG_NO_SKILL_IDENT);
699 }
700
701 return success;
702 }
703
704 /* do_skill_ident() - workhorse for skill_ident() -b.t.
705 */
706 static int
707 do_skill_ident (object *pl, int obj_class, object *skill)
708 {
709 int success = 0;
710
711 for (object *tmp = pl->inv; tmp; tmp = tmp->below)
712 success += do_skill_ident2 (tmp, pl, obj_class, skill);
713 /* check the ground */
714
715 for (object *tmp = pl->ms ().bot; tmp; tmp = tmp->above)
716 success += do_skill_ident2 (tmp, pl, obj_class, skill);
717
718 return success;
719 }
720
721 int
722 skill_ident (object *pl, object *skill)
723 {
724 int success = 0;
725
726 if (pl->type != PLAYER)
727 return 0; /* only players will skill-identify */
728
729 new_draw_info (NDI_UNIQUE, 0, pl, "You look at the objects nearby...");
730
731 switch (skill->subtype)
732 {
733 case SK_SMITHERY:
734 success += do_skill_ident (pl, WEAPON, skill) + do_skill_ident (pl, ARMOUR, skill)
735 + do_skill_ident (pl, BRACERS, skill) + do_skill_ident (pl, CLOAK, skill)
736 + do_skill_ident (pl, BOOTS, skill) + do_skill_ident (pl, SHIELD, skill)
737 + do_skill_ident (pl, GIRDLE, skill) + do_skill_ident (pl, HELMET, skill) + do_skill_ident (pl, GLOVES, skill);
738 break;
739
740 case SK_BOWYER:
741 success += do_skill_ident (pl, BOW, skill) + do_skill_ident (pl, ARROW, skill);
742 break;
743
744 case SK_ALCHEMY:
745 success += do_skill_ident (pl, POTION, skill) + do_skill_ident (pl, POISON, skill)
746 + do_skill_ident (pl, CONTAINER, skill) + do_skill_ident (pl, DRINK, skill) + do_skill_ident (pl, INORGANIC, skill);
747 break;
748
749 case SK_WOODSMAN:
750 success += do_skill_ident (pl, FOOD, skill) + do_skill_ident (pl, DRINK, skill) + do_skill_ident (pl, FLESH, skill);
751 break;
752
753 case SK_JEWELER:
754 success += do_skill_ident (pl, GEM, skill) + do_skill_ident (pl, RING, skill) + do_skill_ident (pl, AMULET, skill);
755 break;
756
757 case SK_LITERACY:
758 success += do_skill_ident (pl, SPELLBOOK, skill) + do_skill_ident (pl, SCROLL, skill) + do_skill_ident (pl, BOOK, skill);
759 break;
760
761 case SK_THAUMATURGY:
762 success += do_skill_ident (pl, WAND, skill) + do_skill_ident (pl, ROD, skill) + do_skill_ident (pl, HORN, skill);
763 break;
764
765 case SK_DET_CURSE:
766 success = do_skill_detect_curse (pl, skill);
767 if (success)
768 new_draw_info (NDI_UNIQUE, 0, pl, "... and discover cursed items!");
769 break;
770
771 case SK_DET_MAGIC:
772 success = do_skill_detect_magic (pl, skill);
773 if (success)
774 new_draw_info (NDI_UNIQUE, 0, pl, "... and discover items imbued with mystic forces!");
775 break;
776
777 default:
778 LOG (llevError, "Error: bad call to skill_ident()\n");
779 return 0;
780 }
781
782 if (!success)
783 new_draw_info (NDI_UNIQUE, 0, pl, "... and learn nothing more.");
784
785 return success;
786 }
787
788 /* players using this skill can 'charm' a monster --
789 * into working for them. It can only be used on
790 * non-special (see below) 'neutral' creatures.
791 * -b.t. (thomas@astro.psu.edu)
792 */
793 int
794 use_oratory (object *pl, int dir, object *skill)
795 {
796 if (pl->type != PLAYER)
797 return 0; /* only players use this skill */
798
799 if (!dir)
800 {
801 pl->failmsg ("In what direction?");
802 return 0;
803 }
804
805 sint16 x = pl->x + DIRX (dir),
806 y = pl->y + DIRY (dir);
807 maptile *m = pl->map;
808
809 int mflags = get_map_flags (m, &m, x, y, &x, &y);
810 if (mflags & P_OUT_OF_MAP)
811 return 0;
812
813 /* Save some processing - we have the flag already anyways
814 */
815 if (!(mflags & P_IS_ALIVE))
816 {
817 pl->failmsg ("There is nothing to orate to.");
818 return 0;
819 }
820
821 object *tmp;
822 for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
823 {
824 /* can't persuade players - return because there is nothing else
825 * on that space to charm. Same for multi space monsters and
826 * special monsters - we don't allow them to be charmed, and there
827 * is no reason to do further processing since they should be the
828 * only monster on the space.
829 */
830 if (tmp->type == PLAYER
831 || tmp->more || tmp->head_ () != tmp
832 || tmp->msg)
833 return 0;
834
835 if (tmp->flag [FLAG_MONSTER])
836 break;
837 }
838
839 if (!tmp)
840 {
841 pl->failmsg ("There is nothing to orate to.");
842 return 0;
843 }
844
845 new_draw_info_format (NDI_UNIQUE, 0, pl, "You orate to the %s.", query_name (tmp));
846
847 /* the following conditions limit who may be 'charmed' */
848
849 /* it's hostile! */
850 if (!tmp->flag [FLAG_UNAGGRESSIVE] && !tmp->flag [FLAG_FRIENDLY])
851 {
852 new_draw_info_format (NDI_UNIQUE, 0, pl, "Too bad the %s isn't listening!\n", query_name (tmp));
853 return 0;
854 }
855
856 /* it's already allied! */
857 if (tmp->flag [FLAG_FRIENDLY] && tmp->attack_movement == PETMOVE)
858 {
859 if (tmp->owner == pl)
860 {
861 new_draw_info (NDI_UNIQUE, 0, pl, "Your follower loves your speech.\n");
862 return 0;
863 }
864 else if (skill->level > tmp->level)
865 {
866 /* you steal the follower. Perhaps we should really look at the
867 * level of the owner above?
868 */
869 tmp->set_owner (pl);
870 tmp->skill = skill->skill;
871
872 new_draw_info_format (NDI_UNIQUE, 0, pl, "You convince the %s to follow you instead!\n", query_name (tmp));
873 /* Abuse fix - don't give exp since this can otherwise
874 * be used by a couple players to gets lots of exp.
875 */
876 return 0;
877 }
878 else
879 {
880 /* In this case, you can't steal it from the other player */
881 return 0;
882 }
883 } /* Creature was already a pet of someone */
884
885 int level = skill->level + (pl->stats.Cha - tmp->stats.Int) / 2;
886
887 /* Ok, got a 'sucker' lets try to make them a follower */
888 if (level > 0 && tmp->level < (random_roll (0, level - 1, pl, PREFER_HIGH) - 1))
889 {
890 new_draw_info_format (NDI_UNIQUE, 0, pl, "You convince the %s to become your follower.\n", query_name (tmp));
891
892 INVOKE_OBJECT (KILL, tmp, ARG_OBJECT (pl));
893 tmp->set_owner (pl);
894 tmp->skill = skill->skill;
895 tmp->stats.exp = 0;
896 tmp->attack_movement = PETMOVE;
897
898 if (!tmp->flag [FLAG_FRIENDLY])
899 add_friendly_object (tmp);
900
901 return calc_skill_exp (pl, tmp, skill);
902 }
903 /* Charm failed. Creature may be angry now */
904 else if ((skill->level + ((pl->stats.Cha - 10) / 2)) < random_roll (1, 2 * tmp->level, pl, PREFER_LOW))
905 {
906 new_draw_info_format (NDI_UNIQUE, 0, pl, "Your speech angers the %s!\n", query_name (tmp));
907 if (tmp->flag [FLAG_FRIENDLY])
908 {
909 remove_friendly_object (tmp);
910 tmp->attack_movement = 0; /* needed? */
911 }
912
913 tmp->clr_flag (FLAG_UNAGGRESSIVE);
914 }
915
916 return 0; /* Fall through - if we get here, we didn't charm anything */
917 }
918
919 /* Singing() -this skill allows the player to pacify nearby creatures.
920 * There are few limitations on who/what kind of
921 * non-player creatures that may be pacified. Right now, a player
922 * may pacify creatures which have Int == 0. In this routine, once
923 * successfully pacified the creature gets Int=1. Thus, a player
924 * may only pacify a creature once.
925 * BTW, I appologize for the naming of the skill, I couldnt think
926 * of anything better! -b.t.
927 */
928 int
929 singing (object *pl, int dir, object *skill)
930 {
931 int exp = 0;
932 object *tmp;
933 maptile *m;
934 sint16 x, y;
935
936 if (pl->type != PLAYER)
937 return 0; /* only players use this skill */
938
939 if (!dir)
940 {
941 pl->failmsg ("In what direction?");
942 return 0;
943 }
944
945 new_draw_info_format (NDI_UNIQUE, 0, pl, "You sing.");
946 for (int i = skill->level >= SIZEOFFREE1 ? 0 : dir;
947 i < min (skill->level, SIZEOFFREE);
948 i++)
949 {
950 x = pl->x + DIRX (i);
951 y = pl->y + DIRY (i);
952 m = pl->map;
953
954 int mflags = get_map_flags (m, &m, x, y, &x, &y);
955 if (mflags & P_OUT_OF_MAP)
956 continue;
957 if (!(mflags & P_IS_ALIVE))
958 continue;
959
960 for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
961 {
962 if (tmp->flag [FLAG_MONSTER])
963 break;
964 /* can't affect players */
965 if (tmp->type == PLAYER)
966 break;
967 }
968
969 /* Whole bunch of checks to see if this is a type of monster that would
970 * listen to singing.
971 */
972 if (tmp && tmp->flag [FLAG_MONSTER] && !tmp->flag [FLAG_NO_STEAL] && /* Been charmed or abused before */
973 !tmp->flag [FLAG_SPLITTING] && /* no ears */
974 !tmp->flag [FLAG_HITBACK] && /* was here before */
975 (tmp->level <= skill->level) && (!tmp->head) && !tmp->flag [FLAG_UNDEAD] && !tmp->flag [FLAG_UNAGGRESSIVE] && /* already calm */
976 !tmp->flag [FLAG_FRIENDLY])
977 { /* already calm */
978
979 /* stealing isn't really related (although, maybe it should
980 * be). This is mainly to prevent singing to the same monster
981 * over and over again and getting exp for it.
982 */
983 int level = skill->level + (pl->stats.Cha - 5 - tmp->stats.Int) / 2;
984
985 if (level && tmp->level < random_roll (0, level - 1, pl, PREFER_HIGH))
986 {
987 tmp->set_flag (FLAG_UNAGGRESSIVE);
988 new_draw_info_format (NDI_UNIQUE, 0, pl, "You calm down the %s\n", query_name (tmp));
989 /* Give exp only if they are not aware */
990
991 if (!tmp->flag [FLAG_NO_STEAL])
992 exp += calc_skill_exp (pl, tmp, skill);
993
994 tmp->set_flag (FLAG_NO_STEAL);
995 }
996 else
997 {
998 new_draw_info_format (NDI_UNIQUE, 0, pl, "Too bad the %s isn't listening!\n", query_name (tmp));
999 tmp->set_flag (FLAG_NO_STEAL);
1000 }
1001 }
1002 }
1003 return exp;
1004 }
1005
1006 /* The find_traps skill (aka, search). Checks for traps
1007 * on the spaces or in certain objects
1008 */
1009 int
1010 find_traps (object *pl, object *skill)
1011 {
1012 object *tmp, *tmp2;
1013 int i, expsum = 0, mflags;
1014 sint16 x, y;
1015 maptile *m;
1016
1017 /* First we search all around us for runes and traps, which are
1018 * all type RUNE
1019 */
1020 for (i = 0; i < 9; i++)
1021 {
1022 x = pl->x + DIRX (i);
1023 y = pl->y + DIRY (i);
1024 m = pl->map;
1025
1026 mflags = get_map_flags (m, &m, x, y, &x, &y);
1027 if (mflags & P_OUT_OF_MAP)
1028 continue;
1029
1030 /* Check everything in the square for trapness */
1031 for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
1032 {
1033 /* And now we'd better do an inventory traversal of each
1034 * of these objects' inventory
1035 * We can narrow this down a bit - no reason to search through
1036 * the players inventory or monsters for that matter.
1037 */
1038 if (tmp->type != PLAYER && !tmp->flag [FLAG_MONSTER])
1039 for (tmp2 = tmp->inv; tmp2 != NULL; tmp2 = tmp2->below)
1040 if (tmp2->type == RUNE || tmp2->type == TRAP)
1041 if (trap_see (pl, tmp2))
1042 {
1043 trap_show (tmp2, tmp);
1044 if (tmp2->stats.Cha > 1)
1045 {
1046 if (!tmp2->owner || tmp2->owner->type != PLAYER)
1047 expsum += calc_skill_exp (pl, tmp2, skill);
1048
1049 tmp2->stats.Cha = 1; /* unhide the trap */
1050 }
1051 }
1052
1053 if ((tmp->type == RUNE || tmp->type == TRAP) && trap_see (pl, tmp))
1054 {
1055 trap_show (tmp, tmp);
1056 if (tmp->stats.Cha > 1)
1057 {
1058 if (!tmp->owner || tmp->owner->type != PLAYER)
1059 expsum += calc_skill_exp (pl, tmp, skill);
1060 tmp->stats.Cha = 1; /* unhide the trap */
1061 }
1062 }
1063 }
1064 }
1065
1066 new_draw_info (NDI_BLACK, 0, pl, "You search the area.");
1067 return expsum;
1068 }
1069
1070 /* remove_trap() - This skill will disarm any previously discovered trap
1071 * the algorithm is based (almost totally) on the old command_disarm() - b.t.
1072 */
1073 int
1074 remove_trap (object *op, int dir, object *skill)
1075 {
1076 object *tmp, *tmp2;
1077 int i, success = 0, mflags;
1078 maptile *m;
1079 sint16 x, y;
1080
1081 for (i = 0; i < 9; i++)
1082 {
1083 x = op->x + DIRX (i);
1084 y = op->y + DIRY (i);
1085 m = op->map;
1086
1087 mflags = get_map_flags (m, &m, x, y, &x, &y);
1088 if (mflags & P_OUT_OF_MAP)
1089 continue;
1090
1091 /* Check everything in the square for trapness */
1092 for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
1093 {
1094 /* And now we'd better do an inventory traversal of each
1095 * of these objects inventory. Like above, only
1096 * do this for interesting objects.
1097 */
1098
1099 if (tmp->type != PLAYER && !tmp->flag [FLAG_MONSTER])
1100 {
1101 for (tmp2 = tmp->inv; tmp2; tmp2 = tmp2->below)
1102 if ((tmp2->type == RUNE || tmp2->type == TRAP) && tmp2->stats.Cha <= 1)
1103 {
1104 trap_show (tmp2, tmp);
1105
1106 if (trap_disarm (op, tmp2, 1, skill) && (!tmp2->owner || tmp2->owner->type != PLAYER))
1107 {
1108 tmp->stats.exp = tmp->stats.Cha * tmp->level;
1109 success += calc_skill_exp (op, tmp2, skill);
1110 }
1111 }
1112 }
1113
1114 if ((tmp->type == RUNE || tmp->type == TRAP) && tmp->stats.Cha <= 1)
1115 {
1116 trap_show (tmp, tmp);
1117
1118 if (trap_disarm (op, tmp, 1, skill) && (!tmp->owner || tmp->owner->type != PLAYER))
1119 {
1120 tmp->stats.exp = tmp->stats.Cha * tmp->level;
1121 success += calc_skill_exp (op, tmp, skill);
1122 }
1123 }
1124 }
1125 }
1126
1127 return success;
1128 }
1129
1130 /* pray() - when this skill is called from do_skill(), it allows
1131 * the player to regain lost grace points at a faster rate. -b.t.
1132 * This always returns 0 - return value is used by calling function
1133 * such that if it returns true, player gets exp in that skill. This
1134 * the effect here can be done on demand, we probably don't want to
1135 * give infinite exp by returning true in any cases.
1136 */
1137 int
1138 pray (object *pl, object *skill)
1139 {
1140 char buf[MAX_BUF];
1141 object *tmp;
1142
1143 if (pl->type != PLAYER)
1144 return 0;
1145
1146 strcpy (buf, "You pray.");
1147
1148 /* Check all objects - we could stop at floor objects,
1149 * but if someone buries an altar, I don't see a problem with
1150 * going through all the objects, and it shouldn't be much slower
1151 * than extra checks on object attributes.
1152 */
1153 for (tmp = pl->below; tmp; tmp = tmp->below)
1154 {
1155 /* Only if the altar actually belongs to someone do you get special benefits */
1156 if (tmp && tmp->type == HOLY_ALTAR && tmp->other_arch)
1157 {
1158 sprintf (buf, "You pray over the %s.", &tmp->name);
1159 pray_at_altar (pl, tmp, skill);
1160 break; /* Only pray at one altar */
1161 }
1162 }
1163
1164 new_draw_info (NDI_BLACK, 0, pl, buf);
1165
1166 if (pl->stats.grace < pl->stats.maxgrace)
1167 {
1168 pl->stats.grace++;
1169 pl->last_grace = -1;
1170 }
1171
1172 return 0;
1173 }
1174
1175 /* This skill allows the player to regain a few sp or hp for a
1176 * brief period of concentration. No armour or weapons may be
1177 * wielded/applied for this to work. The amount of time needed
1178 * to concentrate and the # of points regained is dependant on
1179 * the level of the user. - b.t. thomas@astro.psu.edu
1180 */
1181 void
1182 meditate (object *pl, object *skill)
1183 {
1184 object *tmp;
1185
1186 if (pl->type != PLAYER)
1187 return; /* players only */
1188
1189 /* check if pl has removed encumbering armour and weapons */
1190 if (pl->flag [FLAG_READY_WEAPON] && skill->level < 6)
1191 {
1192 pl->failmsg ("You can't concentrate while wielding a weapon!\n");
1193 return;
1194 }
1195 else
1196 {
1197 for (tmp = pl->inv; tmp; tmp = tmp->below)
1198 if (((tmp->type == ARMOUR && skill->level < 12)
1199 || (tmp->type == HELMET && skill->level < 10)
1200 || (tmp->type == SHIELD && skill->level < 6)
1201 || (tmp->type == BOOTS && skill->level < 4)
1202 || (tmp->type == GLOVES && skill->level < 2))
1203 && tmp->flag [FLAG_APPLIED])
1204 {
1205 pl->failmsg ("You can't concentrate while wearing so much armour!\n");
1206 return;
1207 }
1208 }
1209
1210 /* ok let's meditate! Spell points are regained first, then once
1211 * they are maxed we get back hp. Actual incrementing of values
1212 * is handled by the do_some_living() (in player.c). This way magical
1213 * bonuses for healing/sp regeneration are included properly
1214 * No matter what, we will eat up some playing time trying to
1215 * meditate. (see 'factor' variable for what sets the amount of time)
1216 */
1217
1218 new_draw_info (NDI_BLACK, 0, pl, "You meditate.");
1219
1220 if (pl->stats.sp < pl->stats.maxsp)
1221 {
1222 pl->stats.sp++;
1223 pl->last_sp = -1;
1224 }
1225 else if (pl->stats.hp < pl->stats.maxhp)
1226 {
1227 pl->stats.hp++;
1228 pl->last_heal = -1;
1229 }
1230 }
1231
1232 /* write_note() - this routine allows players to inscribe messages in
1233 * ordinary inscribable 'books' (anything that is not a SPELL). b.t.
1234 */
1235 static int
1236 write_note (object *pl, object *item, const char *msg, object *skill)
1237 {
1238 if (!msg_is_safe (msg))
1239 {
1240 pl->failmsg ("Trying to cheat now are we? H<@-signs are not allowed in notes.>");
1241 LOG (llevInfo, "write_note: player %s tried to write bogus note %s\n", &pl->name, msg);
1242 return 0;
1243 }
1244
1245 int len = strlen (msg);
1246
1247 if (!is_utf8_string ((U8 *)msg, len))
1248 {
1249 pl->failmsg ("Your message is garbled (text must be UTF-8, client-bug)!");
1250 return 0;
1251 }
1252
1253 if (INVOKE_OBJECT (INSCRIBE_NOTE, item, ARG_PLAYER (pl->contr), ARG_STRING (msg), ARG_OBJECT (skill)))
1254 return RESULT_INT (0);
1255
1256 STRLEN char_len = utf8_length ((U8 *)msg, (U8 *)(msg+len));
1257
1258 if (char_len <= item->weight_limit)
1259 {
1260 object *newbook = item->other_arch->instance ();
1261 item->decrease ();
1262 newbook->nrof = 1;
1263 newbook->msg = shstr (msg);
1264 newbook->flag [FLAG_IDENTIFIED] = true;
1265
1266 if (item->subtype == 1) // mailscrolls
1267 {
1268 newbook->name = item->name;
1269 newbook->name_pl = item->name_pl;
1270 }
1271
1272 pl->insert (newbook);
1273
1274 pl->contr->play_sound (sound_find ("inscribe_success"));
1275 new_draw_info_format (NDI_UNIQUE, 0, pl, "You write in the %s.", &item->name);
1276 return char_len;
1277 }
1278 else
1279 new_draw_info_format (NDI_UNIQUE, 0, pl, "Your message won't fit in the %s! H<It is only big enough for %d characters, your message had %d characters.>",
1280 &item->name, item->weight_limit, char_len);
1281
1282 return 0;
1283 }
1284
1285 /* write_scroll() - this routine allows players to inscribe spell scrolls
1286 * of spells which they know. Backfire effects are possible with the
1287 * severity of the backlash correlated with the difficulty of the scroll
1288 * that is attempted. -b.t. thomas@astro.psu.edu
1289 */
1290 static int
1291 write_scroll (object *pl, object *scroll, object *skill)
1292 {
1293 int success = 0, confused = 0;
1294
1295 /* Check if we are ready to attempt inscription */
1296 object *chosen_spell = pl->contr->ranged_ob;
1297
1298 if (!chosen_spell || chosen_spell->type != SPELL)
1299 {
1300 pl->failmsg ("You need a spell readied in order to inscribe!");
1301 return 0;
1302 }
1303
1304 if (SP_level_spellpoint_cost (pl, chosen_spell, SPELL_GRACE) > pl->stats.grace)
1305 {
1306 pl->failmsgf ("You don't have enough grace to write a scroll of %s.", &chosen_spell->name);
1307 return 0;
1308 }
1309
1310 if (SP_level_spellpoint_cost (pl, chosen_spell, SPELL_MANA) > pl->stats.sp)
1311 {
1312 pl->failmsgf ("You don't have enough mana to write a scroll of %s.", &chosen_spell->name);
1313 return 0;
1314 }
1315
1316 /* ok, we are ready to try inscription */
1317 if (pl->flag [FLAG_CONFUSED])
1318 confused = 1;
1319
1320 /* Lost mana/grace no matter what */
1321 pl->stats.grace -= SP_level_spellpoint_cost (pl, chosen_spell, SPELL_GRACE);
1322 pl->stats.sp -= SP_level_spellpoint_cost (pl, chosen_spell, SPELL_MANA);
1323
1324 if (random_roll (0, chosen_spell->level * 4 - 1, pl, PREFER_LOW) < skill->level)
1325 {
1326 object *newscroll = scroll->other_arch->instance ();
1327 scroll->decrease ();
1328 newscroll->nrof = 1;
1329 newscroll->randomitems = 0; // make sure randomitems doesn't destroy the scroll
1330
1331 pl->contr->play_sound (sound_find ("inscribe_success"));
1332
1333 if (!confused)
1334 {
1335 newscroll->level = max (skill->level, chosen_spell->level);
1336 newscroll->flag [FLAG_IDENTIFIED] = true;
1337 new_draw_info (NDI_UNIQUE, 0, pl, "You succeed in writing the spell.");
1338 }
1339 else
1340 {
1341 chosen_spell = find_random_spell_in_ob (pl, NULL);
1342 if (!chosen_spell)
1343 return 0;
1344
1345 newscroll->level = max (skill->level, chosen_spell->level);
1346 new_draw_info (NDI_UNIQUE, 0, pl, "In your confused state, you write down some odd spell.");
1347 }
1348
1349 object *tmp = chosen_spell->clone ();
1350 tmp->flag [FLAG_APPLIED] = false;
1351 insert_ob_in_ob (tmp, newscroll);
1352
1353 /* Same code as from treasure.C - so they can better merge.
1354 * if players want to sell them, so be it.
1355 */
1356 newscroll->value = newscroll->arch->value * newscroll->inv->value * (newscroll->level + 50) / (newscroll->inv->level + 50);
1357 newscroll->stats.exp = newscroll->value / 5;
1358
1359 pl->insert (newscroll);
1360
1361 success = calc_skill_exp (pl, newscroll, skill);
1362 if (!confused)
1363 success *= 2;
1364
1365 success = success * skill->level;
1366 return success;
1367 }
1368 else
1369 { /* Inscription has failed */
1370 pl->contr->play_sound (sound_find ("inscribe_fail"));
1371
1372 if (chosen_spell->level > skill->level || confused)
1373 { /*backfire! */
1374 new_draw_info (NDI_UNIQUE, 0, pl, "Ouch! Your attempt to write a new scroll strains your mind! H<The spell level is too high for your inscription skill.>");
1375
1376 if (random_roll (0, 1, pl, PREFER_LOW) == 1)
1377 pl->drain_specific_stat (4);
1378 else
1379 {
1380 confuse_player (pl, pl, 99);
1381 return -30 * chosen_spell->level;
1382 }
1383 }
1384 else if (random_roll (0, pl->stats.Int - 1, pl, PREFER_HIGH) < 15)
1385 {
1386 new_draw_info (NDI_UNIQUE, 0, pl, "Your attempt to write a new scroll rattles your mind! H<Frankly spoken, you were too dumb and unlucky.>");
1387 confuse_player (pl, pl, 99);
1388 }
1389 else
1390 new_draw_info (NDI_UNIQUE, 0, pl, "You fail to write a new scroll. H<Try a lower-level spell, if possible at all.>");
1391 }
1392
1393 return 0;
1394 }
1395
1396 /* write_on_item() - wrapper for write_note and write_scroll */
1397 int
1398 write_on_item (object *pl, const char *params, object *skill)
1399 {
1400 archetype *skat;
1401
1402 if (pl->type != PLAYER)
1403 return 0;
1404
1405 if (!params)
1406 params = "";
1407
1408 skat = get_archetype_by_type_subtype (SKILL, SK_LITERACY);
1409
1410 /* Need to be able to read before we can write! */
1411 if (!find_skill_by_name (pl, skat->skill))
1412 {
1413 new_draw_info (NDI_UNIQUE, 0, pl, "You must learn to read before you can write! H<You lack the literacy skill.>");
1414 return 0;
1415 }
1416
1417 object *item = pl->mark ();
1418
1419 /* find an item of correct type to write on */
1420 if (!item)
1421 {
1422 new_draw_info (NDI_UNIQUE, 0, pl, "You don't have any marked item to write on. H<Use the mark command or the popup menu to make an item.>");
1423 return 0;
1424 }
1425
1426 if (item->type != INSCRIBABLE)
1427 {
1428 new_draw_info_format (NDI_UNIQUE, 0, pl, "You cannot inscribe this! H<You can only inscribe empty scrolls and books.>");
1429 return 0;
1430 }
1431
1432 if (item->flag [FLAG_UNPAID])
1433 {
1434 new_draw_info (NDI_UNIQUE, 0, pl, "You had better pay for that before you write on it.");
1435 return 0;
1436 }
1437
1438 if (item->other_arch->type == SCROLL)
1439 {
1440 if (*params)
1441 {
1442 // check readied scroll
1443 pl->failmsgf ("When inscribing spells you need to ready a spell and do not specify a string argument.\n"
1444 "Usage: cast [spell name]; use_skill %s", &skill->skill);
1445 return 0;
1446 }
1447
1448 return write_scroll (pl, item, skill);
1449 }
1450 else
1451 {
1452 if (!*params)
1453 {
1454 pl->failmsgf ("When inscribing books you need to specify the words you want to inscribe as command argument.\n"
1455 "Usage: use_skill %s <message>", &skill->skill);
1456 return 0;
1457 }
1458
1459 return write_note (pl, item, params, skill);
1460 }
1461
1462 return 0;
1463 }
1464
1465 /* find_throw_ob() - if we request an object, then
1466 * we search for it in the inventory of the owner (you've
1467 * got to be carrying something in order to throw it!).
1468 * If we didnt request an object, then the top object in inventory
1469 * (that is "throwable", ie no throwing your skills away!)
1470 * is the object of choice. Also check to see if object is
1471 * 'throwable' (ie not applied cursed obj, worn, etc).
1472 */
1473 static object *
1474 find_throw_ob (object *op, const char *request)
1475 {
1476 /* prefer marked item */
1477 object *tmp = op->mark ();
1478
1479 /* can't toss invisible or inv-locked items */
1480 if (tmp && (tmp->invisible || tmp->flag [FLAG_INV_LOCKED]))
1481 tmp = 0;
1482
1483 /* look through the inventory */
1484 if (!tmp)
1485 for (tmp = op->inv; tmp; tmp = tmp->below)
1486 {
1487 /* can't toss invisible or inv-locked items */
1488 if (tmp->invisible || tmp->flag [FLAG_INV_LOCKED])
1489 continue;
1490
1491 if (!request || !strcmp (query_name (tmp), request) || !strcmp (&tmp->name, request))
1492 break;
1493 }
1494
1495 /* this should prevent us from throwing away
1496 * cursed items, worn armour, etc. Only weapons
1497 * can be thrown from 'hand'.
1498 */
1499 if (!tmp)
1500 return 0;
1501
1502 if (tmp->flag [FLAG_APPLIED])
1503 {
1504 if (tmp->type != WEAPON)
1505 {
1506 op->failmsgf ("You can't throw %s. H<Unapply it first.>", query_name (tmp));
1507 tmp = 0;
1508 }
1509 else if (tmp->flag [FLAG_CURSED] || tmp->flag [FLAG_DAMNED])
1510 {
1511 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s sticks to your hand! H<Cursed or damned items cannot be removed.>", query_name (tmp));
1512 tmp = 0;
1513 }
1514 else
1515 {
1516 if (!op->apply (tmp, AP_UNAPPLY))
1517 {
1518 LOG (llevError, "BUG: find_throw_ob(): couldn't unapply\n");
1519 tmp = 0;
1520 }
1521 }
1522 }
1523 else if (tmp->flag [FLAG_UNPAID])
1524 {
1525 op->failmsgf ("You should pay for the %s first.", query_name (tmp));
1526 tmp = 0;
1527 }
1528
1529 //TODO: why not chekc first and give sensible message to player?
1530 if (tmp && tmp->flag [FLAG_INV_LOCKED])
1531 {
1532 LOG (llevError, "BUG: find_throw_ob(): object is locked\n");
1533 tmp = 0;
1534 }
1535
1536 return tmp;
1537 }
1538
1539 /* make_throw_ob() We construct the 'carrier' object in
1540 * which we will insert the object that is being thrown.
1541 * This combination becomes the 'thrown object'. -b.t.
1542 */
1543 static object *
1544 make_throw_ob (object *orig)
1545 {
1546 if (!orig)
1547 return 0;
1548
1549 if (orig->flag [FLAG_APPLIED])
1550 {
1551 LOG (llevError, "BUG: make_throw_ob(): ob is applied\n");
1552 /* insufficient workaround, but better than nothing */
1553 orig->clr_flag (FLAG_APPLIED);
1554 }
1555
1556 object *toss_item = orig->clone ();
1557
1558 toss_item->type = THROWN_OBJ;
1559 toss_item->clr_flag (FLAG_CHANGING);
1560 toss_item->set_flag (FLAG_NO_PICK);
1561 toss_item->stats.dam = 0; /* default damage */
1562 toss_item->insert (orig);
1563
1564 return toss_item;
1565 }
1566
1567 /* do_throw() - op throws any object toss_item. This code
1568 * was borrowed from fire_bow.
1569 * Returns 1 if skill was successfully used, 0 if not
1570 */
1571 static int
1572 do_throw (object *op, object *part, object *toss_item, int dir, object *skill)
1573 {
1574 object *throw_ob = toss_item, *left = NULL;
1575 int eff_str = 0, maxc, str = op->stats.Str, dam = 0;
1576 int pause_f, weight_f = 0, mflags;
1577 float str_factor = 1.0, load_factor = 1.0, item_factor = 1.0;
1578 maptile *m;
1579 sint16 sx, sy;
1580
1581 if (!throw_ob)
1582 {
1583 op->failmsg ("You have nothing to throw.");
1584 return 0;
1585 }
1586
1587 if (throw_ob->flag [FLAG_STARTEQUIP])
1588 {
1589 op->failmsg ("The gods won't let you throw that. "
1590 "H<Well, they would, but they would retrieve it "
1591 "- you wouldn't want that, would you?>");
1592
1593 return 0;
1594 }
1595
1596 /* Because throwing effectiveness must be reduced by the
1597 * encumbrance of the thrower and weight of the object. THus,
1598 * we use the concept of 'effective strength' as defined below.
1599 */
1600
1601 /* if str exceeds MAX_STAT (30, eg giants), lets assign a str_factor > 1 */
1602 if (str > MAX_STAT)
1603 {
1604 str_factor = (float) str / (float) MAX_STAT;
1605 str = MAX_STAT;
1606 }
1607
1608 /* the more we carry, the less we can throw. Limit only on players */
1609 maxc = max_carry[str] * 1000;
1610 if (op->carrying > maxc && op->type == PLAYER)
1611 load_factor = (float) maxc / (float) op->carrying;
1612
1613 /* lighter items are thrown harder, farther, faster */
1614 if (throw_ob->weight > 0)
1615 item_factor = (float) maxc / (float) (3.0 * throw_ob->weight);
1616 else
1617 { /* 0 or negative weight?!? Odd object, can't throw it */
1618 op->failmsgf ("You can't throw %s. H<Better report this to your dungeon master.>\n", query_name (throw_ob));
1619 return 0;
1620 }
1621
1622 eff_str = (str * (load_factor < 1.0 ? load_factor : 1.0))
1623 * item_factor * str_factor;
1624
1625 /* alas, arrays limit us to a value of MAX_STAT (30). Use str_factor to
1626 * account for super-strong throwers. */
1627 min_it (eff_str, MAX_STAT);
1628
1629 #ifdef DEBUG_THROW
1630 LOG (llevDebug, "%s carries %d, eff_str=%d\n", op->name, op->carrying, eff_str);
1631 LOG (llevDebug, " max_c=%d, item_f=%f, load_f=%f, str=%d\n", maxc, item_factor, load_factor, op->stats.Str);
1632 LOG (llevDebug, " str_factor=%f\n", str_factor);
1633 LOG (llevDebug, " item %s weight= %d\n", throw_ob->name, throw_ob->weight);
1634 #endif
1635
1636 /* 3 things here prevent a throw, you aimed at your feet, you
1637 * have no effective throwing strength, or you threw at something
1638 * that flying objects can't get through.
1639 */
1640 mflags = get_map_flags (part->map, &m, part->x + DIRX (dir), part->y + DIRY (dir), &sx, &sy);
1641
1642 if (!dir || (eff_str <= 1) || (mflags & P_OUT_OF_MAP) || (GET_MAP_MOVE_BLOCK (m, sx, sy) & MOVE_FLY_LOW))
1643 {
1644 /* bounces off 'wall', and drops to feet */
1645 throw_ob->insert_at (part, op);
1646
1647 if (op->type == PLAYER)
1648 {
1649 if (eff_str <= 1)
1650 new_draw_info_format (NDI_UNIQUE, 0, op, "Your load is so heavy you drop %s to the ground.", query_name (throw_ob));
1651 else if (!dir)
1652 new_draw_info_format (NDI_UNIQUE, 0, op, "You throw %s at the ground.", query_name (throw_ob));
1653 else
1654 new_draw_info (NDI_UNIQUE, 0, op, "Something is in the way.");
1655 }
1656
1657 return 0;
1658 } /* if object can't be thrown */
1659
1660 left = throw_ob; /* these are throwing objects left to the player */
1661
1662 /* sometimes get_split_ob can't split an object (because op->nrof==0?)
1663 * and returns NULL. We must use 'left' then
1664 */
1665 if (!(throw_ob = throw_ob->split ()))
1666 {
1667 throw_ob = left;
1668 left->remove ();
1669 }
1670
1671 /* special case: throwing powdery substances like dust, dirt */
1672 if (throw_ob->type == POTION && throw_ob->subtype == POT_DUST)
1673 {
1674 cast_dust (op, throw_ob, dir);
1675 return 1;
1676 }
1677
1678 /* Make a thrown object -- insert real object in a 'carrier' object.
1679 * If unsuccessfull at making the "thrown_obj", we just reinsert
1680 * the original object back into inventory and exit
1681 */
1682 if ((toss_item = make_throw_ob (throw_ob)))
1683 {
1684 throw_ob = toss_item;
1685 throw_ob->skill = skill->skill;
1686 }
1687 else
1688 {
1689 insert_ob_in_ob (throw_ob, op);
1690 return 0;
1691 }
1692
1693 throw_ob->set_owner (op);
1694 /* At some point in the attack code, the actual real object (op->inv)
1695 * becomes the hitter. As such, we need to make sure that has a proper
1696 * owner value so exp goes to the right place.
1697 */
1698 throw_ob->inv->set_owner (op);
1699 throw_ob->direction = dir;
1700
1701 /* the damage bonus from the force of the throw */
1702 dam = int (str_factor * dam_bonus[eff_str]);
1703
1704 /* Now, lets adjust the properties of the thrown_ob. */
1705
1706 /* how far to fly */
1707 throw_ob->last_sp = (eff_str * 3) / 5;
1708
1709 /* speed */
1710 throw_ob->set_speed (min (1.0, speed_bonus[eff_str] + 1.0) / 1.5); /* no faster than an arrow! */
1711
1712 /* item damage. Eff_str and item weight influence damage done */
1713 weight_f = (throw_ob->weight / 2000) > MAX_STAT ? MAX_STAT : (throw_ob->weight / 2000);
1714 throw_ob->stats.dam += (dam / 3) + dam_bonus[weight_f] + (throw_ob->weight / 15000) - 2;
1715
1716 /* chance of breaking. Proportional to force used and weight of item */
1717 throw_ob->stats.food = (dam / 2) + (throw_ob->weight / 60000);
1718
1719 /* replace 25 with a call to clone.arch wc? messes up w/ NPC */
1720 throw_ob->stats.wc = 25 - dex_bonus[op->stats.Dex] - thaco_bonus[eff_str] - skill->level;
1721
1722 /* the properties of objects which are meant to be thrown (ie dart,
1723 * throwing knife, etc) will differ from ordinary items. Lets tailor
1724 * this stuff in here.
1725 */
1726
1727 if (throw_ob->inv->flag [FLAG_IS_THROWN])
1728 {
1729 throw_ob->last_sp += eff_str / 3; /* fly a little further */
1730 throw_ob->stats.dam += throw_ob->inv->stats.dam + throw_ob->magic + 2;
1731 throw_ob->stats.wc -= throw_ob->magic + throw_ob->inv->stats.wc;
1732 /* only throw objects get directional faces */
1733 if (throw_ob->has_anim () && throw_ob->anim_frames ())
1734 throw_ob->set_anim_frame (dir);
1735 }
1736 else
1737 {
1738 uint16 mat = throw_ob->materials;
1739
1740 /* some materials will adjust properties.. */
1741 if (mat & M_LEATHER)
1742 {
1743 throw_ob->stats.dam -= 1;
1744 throw_ob->stats.food -= 10;
1745 }
1746
1747 if (mat & M_GLASS)
1748 throw_ob->stats.food += 60;
1749
1750 if (mat & M_ORGANIC)
1751 {
1752 throw_ob->stats.dam -= 3;
1753 throw_ob->stats.food += 55;
1754 }
1755
1756 if (mat & M_PAPER || mat & M_CLOTH)
1757 {
1758 throw_ob->stats.dam -= 5;
1759 throw_ob->speed *= 0.8;
1760 throw_ob->stats.wc += 3;
1761 throw_ob->stats.food -= 30;
1762 }
1763
1764 /* light obj have more wind resistance, fly slower */
1765 if (throw_ob->weight > 500)
1766 throw_ob->speed *= 0.8;
1767
1768 if (throw_ob->weight > 50)
1769 throw_ob->speed *= 0.5;
1770 } /* else tailor thrown object */
1771
1772 /* some limits, and safeties (needed?) */
1773 max_it (throw_ob->stats.dam, 0);
1774 min_it (throw_ob->last_sp, eff_str);
1775 max_it (throw_ob->stats.food, 0);
1776 min_it (throw_ob->stats.wc, 30);
1777
1778 /* how long to pause the thrower. Higher values mean less pause */
1779 pause_f = ((2 * eff_str) / 3) + 20 + skill->level;
1780
1781 /* Put a lower limit on this */
1782 clamp_it (pause_f, 10, 100);
1783
1784 /* Changed in 0.94.2 - the calculation before was really goofy.
1785 * In short summary, a throw can take anywhere between speed 5 and
1786 * speed 0.5
1787 */
1788 op->speed_left -= 50 / pause_f;
1789
1790 throw_ob->speed_left = 0;
1791 throw_ob->map = part->map;
1792
1793 throw_ob->move_type = MOVE_FLY_LOW;
1794 throw_ob->move_on = MOVE_FLY_LOW | MOVE_WALK;
1795
1796 throw_ob->set_speed (throw_ob->speed);
1797
1798 #if 0
1799 /* need to put in a good sound for this */
1800 play_sound_map (op->map, op->x, op->y, SOUND_THROW_OBJ);
1801 #endif
1802
1803 /* Lauwenmark - Now we can call the associated script_throw event (if any) */
1804 INVOKE_OBJECT (THROW, throw_ob, ARG_OBJECT (op));
1805 #ifdef DEBUG_THROW
1806 LOG (llevDebug, " pause_f=%d \n", pause_f);
1807 LOG (llevDebug, " %s stats: wc=%d dam=%d dist=%d spd=%f break=%d\n",
1808 throw_ob->name, throw_ob->stats.wc, throw_ob->stats.dam, throw_ob->last_sp, throw_ob->speed, throw_ob->stats.food);
1809 LOG (llevDebug, "inserting tossitem (%d) into map\n", throw_ob->count);
1810 #endif
1811
1812 throw_ob->insert_at (part, op);
1813
1814 if (!throw_ob->destroyed ())
1815 move_arrow (throw_ob);
1816
1817 return 1;
1818 }
1819
1820 int
1821 skill_throw (object *op, object *part, int dir, const char *params, object *skill)
1822 {
1823 object *throw_ob;
1824
1825 if (op->type == PLAYER)
1826 throw_ob = find_throw_ob (op, params);
1827 else
1828 throw_ob = find_mon_throw_ob (op);
1829
1830 return do_throw (op, part, throw_ob, dir, skill);
1831 }
1832
1833 static void
1834 give_player_quad_material_for (object *pl, object *ob)
1835 {
1836 if (!ob->flag [FLAG_IS_QUAD] || !ob->other_arch)
1837 return;
1838
1839 object *t = ob->other_arch->instance ();
1840 pl->insert (t);
1841 }
1842
1843 bool
1844 skill_mining (object *who, object *tool, object *skill, int dir, const char *string)
1845 {
1846 if (!who->is_player ())
1847 return 0;
1848
1849 if (!dir)
1850 {
1851 who->failmsg ("In what direction?");
1852 return 0;
1853 }
1854
1855 mapxy pos (who); pos.move (dir);
1856
1857 if (!pos.normalise ())
1858 {
1859 who->failmsgf (
1860 "You brandish your %s, with not so convincing results. "
1861 "H<There is nothing in this direction.>",
1862 &tool->name
1863 );
1864 return 0;
1865 }
1866
1867 mapspace &ms = pos.ms ();
1868
1869 for (object *mine_obj = pos.ms ().top; mine_obj; mine_obj = mine_obj->below)
1870 if (mine_obj->type == VEIN)
1871 {
1872 who->speed_left -= who->speed / tool->speed;
1873
1874 who->play_sound (tool->sound);
1875
1876 if (rndm (100 - mine_obj->stats.ac) > rndm (tool->stats.wc))
1877 who->failmsgf (
1878 "You use your %s.... nothing. "
1879 "H<Try again, perhaps?>",
1880 &tool->name
1881 );
1882 else if (mine_obj->race != tool->race)
1883 who->failmsgf (
1884 "You use your %s.... but it doesn't work. "
1885 "H<Maybe you are using the wrong tool?>",
1886 &tool->name
1887 );
1888 else if (!mine_obj->stats.food)
1889 who->failmsgf (
1890 "You use your %s.... but there is nothing. "
1891 "H<This space is exhausted, you should try somewhere else.>",
1892 &tool->name
1893 );
1894 else
1895 {
1896 --mine_obj->stats.food;
1897
1898 object *ore = mine_obj->other_arch->instance ();
1899
1900 who->statusmsg (format (
1901 "You use your %s.... and find some %s!",
1902 &tool->name, &ore->name
1903 ));
1904
1905 ore->insert_at (who);
1906
1907 return true;
1908 }
1909 }
1910 else if (mine_obj->flag [FLAG_IS_QUAD])
1911 {
1912 if (mine_obj->flag [FLAG_IS_FLOOR])
1913 {
1914 maptile *lower_floor = pos.m->tile_available (TILE_DOWN);
1915
1916 if (!lower_floor)
1917 {
1918 who->failmsgf (
1919 "Whoops, you failed to remove the %s. H<You may try again.>",
1920 &mine_obj->name
1921 );
1922 return false;
1923 }
1924
1925 mapxy below_pos (lower_floor, pos.x, pos.y);
1926
1927 if (!below_pos.normalise ())
1928 {
1929 who->failmsgf (
1930 "Whoops, you failed to remove the %s. H<You may try again.>",
1931 &mine_obj->name
1932 );
1933 return false;
1934 }
1935
1936 // first insert open space here:
1937 object *open_space_floor = archetype::get (shstr_quad_open_space);
1938 open_space_floor->stats.hp = pos.x;
1939 open_space_floor->stats.sp = pos.y;
1940 insert_ob_in_map_at (open_space_floor, pos.m, mine_obj,
1941 INS_BELOW_ORIGINATOR, pos.x, pos.y);
1942
1943 // next we will remove the wall in the lower layer:
1944 mapspace &below_ms = below_pos.ms ();
1945 for (object *quad_obj = below_ms.top; quad_obj; quad_obj = quad_obj->below)
1946 {
1947 if (quad_obj->flag [FLAG_IS_FLOOR])
1948 break;
1949
1950 if (quad_obj->flag [FLAG_IS_QUAD])
1951 {
1952 quad_obj->map->queue_physics_at (quad_obj->x, quad_obj->y);
1953 give_player_quad_material_for (who, quad_obj);
1954 quad_obj->destroy ();
1955 break;
1956 }
1957 }
1958 }
1959
1960 mine_obj->map->queue_physics_at (mine_obj->x, mine_obj->y);
1961
1962 // FIXME: there should be chance assigned, like above with veins!
1963 who->play_sound (tool->sound);
1964 give_player_quad_material_for (who, mine_obj);
1965 mine_obj->destroy ();
1966 who->contr->fire_on = 0; // TODO: stopgap, do not remove more than one per keypress
1967 return true;
1968 }
1969
1970 return false;
1971 }
1972