ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/skills.c
Revision: 1.2
Committed: Fri Jun 2 02:01:12 2006 UTC (17 years, 11 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.1: +15 -3 lines
Log Message:
Fixed issue: Allow players to steal when hostile
Bug ticket:  http://cf.schmorp.de/tracker/view.php?id=22

File Contents

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