ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skills.C
Revision: 1.27
Committed: Tue Apr 24 12:32:16 2007 UTC (17 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.26: +2 -0 lines
Log Message:
server crashed this morning because a freed golem tried to follow
his owner, triggering the assertion failure.

the golem had no owner, but the owner still had him in his range slots.

I refactored a bit of the code and rearranged it to hopefully increase
chances of this not occuring again.

File Contents

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