ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/attack.C
Revision: 1.145
Committed: Tue Jan 3 11:25:35 2012 UTC (12 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.144: +1 -1 lines
Log Message:
update copyrights to 2012

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * 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 Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
25 #include <assert.h>
26 #include <global.h>
27 #include <living.h>
28 #include <material.h>
29 #include <skills.h>
30 #include <sounds.h>
31 #include <sproto.h>
32
33 typedef struct att_msg_str
34 {
35 char *msg1;
36 char *msg2;
37 } att_msg;
38
39 /*#define ATTACK_DEBUG*/
40
41 /* did_make_save_item just checks to make sure the item actually
42 * made its saving throw based on the tables. It does not take
43 * any further action (like destroying the item).
44 */
45 static int
46 did_make_save_item (object *op, uint32_t type, object *originator)
47 {
48 int saves = 0, attacks = 0;
49 materialtype_t *mt = op->material;
50
51 // destroying objects without material has many bad effects
52 if (mt == MATERIAL_NULL)
53 return 1;
54
55 int roll = rndm (1, 20);
56
57 /* the attacktypes have no meaning for object saves
58 * If the type is only magic, don't adjust type - basically, if
59 * pure magic is hitting an object, it should save. However, if it
60 * is magic teamed with something else, then strip out the
61 * magic type, and instead let the fire, cold, or whatever component
62 * destroy the item. Otherwise, you get the case of poisoncloud
63 * destroying objects because it has magic attacktype.
64 */
65 if (type != AT_MAGIC)
66 type &= ~(AT_CONFUSION | AT_DRAIN | AT_GHOSTHIT | AT_POISON | AT_SLOW |
67 AT_PARALYZE | AT_TURN_UNDEAD | AT_FEAR | AT_DEPLETE | AT_DEATH |
68 AT_COUNTERSPELL | AT_HOLYWORD | AT_BLIND | AT_LIFE_STEALING | AT_MAGIC);
69
70
71 if (type == 0) return TRUE;
72
73 if (roll == 20) return TRUE;
74 if (roll == 1) return FALSE;
75
76 for_all_bits_sparse_32 (type, number)
77 {
78 attacks++;
79
80 if (op->resist[number] == 100)
81 saves++;
82 else if (roll >= mt->save[number] - op->magic - op->resist[number] / 100)
83 saves++;
84 else if ((20 - mt->save[number]) / 3 > originator->stats.dam)
85 saves++;
86 }
87
88 if (saves == attacks || attacks == 0)
89 return TRUE;
90
91 if (saves == 0 || (rndm (1, attacks) > saves))
92 return FALSE;
93
94 return TRUE;
95 }
96
97 /* cancels object *op. Cancellation basically means an object loses
98 * its magical benefits.
99 */
100 void
101 cancellation (object *op)
102 {
103 if (op->invisible)
104 return;
105
106 if (op->flag [FLAG_ALIVE] || op->type == CONTAINER || op->type == THROWN_OBJ)
107 {
108 /* Recurse through the inventory */
109 for (object *tmp = op->inv; tmp; tmp = tmp->below)
110 if (!did_make_save_item (tmp, AT_CANCELLATION, op))
111 cancellation (tmp);
112 }
113 else if (fabs (op->magic) <= rndm (0, 5))
114 {
115 /* Nullify this object. This code could probably be more complete */
116 /* in what abilities it should cancel */
117 op->magic = 0;
118
119 op->clr_flag (FLAG_DAMNED);
120 op->clr_flag (FLAG_CURSED);
121 op->clr_flag (FLAG_KNOWN_MAGICAL);
122 op->clr_flag (FLAG_KNOWN_CURSED);
123
124 if (object *pl = op->visible_to ())
125 esrv_update_item (UPD_FLAGS, pl, op);
126 }
127 }
128
129 /* This function calls did_make_save_item. It then performs the
130 * appropriate actions to the item (such as burning the item up,
131 * calling cancellation, etc.)
132 */
133 void
134 save_throw_object (object *op, int type, object *originator)
135 {
136 op = op->head_ ();
137
138 if (!did_make_save_item (op, type, originator))
139 {
140 object *env = op->env;
141 int x = op->x, y = op->y;
142 maptile *m = op->map;
143
144 op = stop_item (op);
145 if (!op)
146 return;
147
148 /* Hacked the following so that type LIGHTER will work.
149 * Also, objects which are potential "lights" that are hit by
150 * flame/elect attacks will be set to glow. "lights" are any
151 * object with +/- glow_radius and an "other_arch" to change to.
152 * (and please note that we cant fail our save and reach this
153 * function if the object doesnt contain a material that can burn.
154 * So forget lighting magical swords on fire with this!) -b.t.
155 */
156 if (type & (AT_FIRE | AT_ELECTRICITY))
157 {
158 // seems LAMPs and TORCHes are always IS_LIGHTABLE?
159 if (op->type == LAMP || op->type == TORCH)
160 {
161 apply_lamp (op, true); // turn on a lamp
162 return;
163 }
164 else if (op->flag [FLAG_IS_LIGHTABLE])
165 {
166 if (op->other_arch)
167 {
168 const char *arch = op->other_arch->archname;
169
170 if (op->decrease ())
171 fix_stopped_item (op, m, originator);
172
173 if ((op = archetype::get (arch)))
174 {
175 if (env)
176 env->insert (op);
177 else
178 m->insert (op, x, y, originator);
179 }
180 }
181
182 return;
183 }
184 }
185
186 if (type & AT_CANCELLATION)
187 { /* Cancellation. */
188 cancellation (op);
189 fix_stopped_item (op, m, originator);
190
191 return;
192 }
193
194 if (type & (AT_FIRE | AT_ELECTRICITY))
195 if (env)
196 {
197 object *op = archetype::get (shstr_burnout);
198 op->x = env->x, op->y = env->y;
199 env->insert (op);
200 }
201 else
202 replace_insert_ob_in_map (shstr_burnout, originator);
203
204 if (op->nrof > 1)
205 {
206 if (op->decrease (rndm (0, op->nrof - 1)))
207 fix_stopped_item (op, m, originator);
208 }
209 else
210 {
211 // drop everything to the ground, if possible
212 op->insert_at (originator);
213 op->drop_and_destroy ();
214 }
215
216 return;
217 }
218
219 /* The value of 50 is arbitrary. */
220 if (type & AT_COLD && (op->resist[ATNR_COLD] < 50) && !op->flag [FLAG_NO_PICK] && rndm (2))
221 {
222 archetype *at = archetype::find (shstr_icecube);
223
224 if (at == NULL)
225 return;
226
227 op = stop_item (op);
228 if (op == NULL)
229 return;
230
231 object *tmp = present_arch (at, op->map, op->x, op->y);
232 if (!tmp)
233 {
234 tmp = at->instance ();
235 tmp->x = op->x, tmp->y = op->y;
236 /* This was in the old (pre new movement code) -
237 * icecubes have slow_move set to 1 - don't want
238 * that for ones we create.
239 */
240 tmp->move_slow_penalty = 0;
241 tmp->move_slow = 0;
242 insert_ob_in_map (tmp, op->map, originator, 0);
243 }
244
245 tmp->insert (op);
246 return;
247 }
248 }
249
250 /* Object op is hitting the map.
251 * op is going in direction 'dir'
252 * type is the attacktype of the object.
253 * full_hit is set if monster area does not matter.
254 * returns 1 if it hits something, 0 otherwise.
255 */
256 int
257 hit_map (object *op, int dir, uint32_t type, int full_hit)
258 {
259 maptile *map;
260 sint16 x, y;
261 int retflag = 0; /* added this flag.. will return 1 if it hits a monster */
262
263 if (op->flag [FLAG_FREED])
264 {
265 LOG (llevError, "BUG: hit_map(): free object\n");
266 return 0;
267 }
268
269 if (op->flag [FLAG_REMOVED] || op->env != NULL)
270 {
271 LOG (llevError, "BUG: hit_map(): hitter (arch %s, name %s) not on a map\n", &op->arch->archname, &op->name);
272 return 0;
273 }
274
275 if (!op->map)
276 {
277 LOG (llevError, "BUG: hit_map(): %s has no map\n", &op->name);
278 return 0;
279 }
280
281 if (op->head)
282 op = op->head;
283
284 mapxy pos (op);
285 pos.move (dir);
286
287 if (!pos.normalise ())
288 return 0;
289
290 // elmex: a safe map tile can't be hit!
291 // this should prevent most harmful effects on items and players there.
292 mapspace &ms = pos.ms ();
293
294 if (ms.flags () & P_SAFE)
295 return 0;
296
297 /* peterm: a few special cases for special attacktypes --counterspell
298 * must be out here because it strikes things which are not alive
299 */
300 if (type & (AT_COUNTERSPELL | AT_CHAOS))
301 {
302 if (type & AT_COUNTERSPELL)
303 {
304 counterspell (op, dir); /* see spell_effect.c */
305
306 /* If the only attacktype is counterspell or magic, don't need
307 * to do any further processing.
308 */
309 if (!(type & ~(AT_COUNTERSPELL | AT_MAGIC)))
310 return 0;
311
312 type &= ~AT_COUNTERSPELL;
313 }
314
315 if (type & AT_CHAOS)
316 {
317 shuffle_attack (op, 1); /* flag tells it to change the face */
318 update_object (op, UP_OBJ_FACE);
319 type &= ~AT_CHAOS;
320 }
321 }
322
323 /* There may still be objects that were above 'next', but there is no
324 * simple way to find out short of copying all object references and
325 * tags into a temporary array before we start processing the first
326 * object. That's why we just abort on destroy.
327 *
328 * This happens whenever attack spells (like fire) hit a pile
329 * of objects. This is not a bug - nor an error.
330 */
331 for (object *next = ms.bot; next && !next->destroyed (); )
332 {
333 object *tmp = next;
334 next = tmp->above;
335
336 /* Something could have happened to 'tmp' while 'tmp->below' was processed.
337 * For example, 'tmp' was put in an icecube.
338 * This is one of the few cases where on_same_map should not be used.
339 */
340 if (tmp->map != pos.m || tmp->x != pos.x || tmp->y != pos.y)
341 continue;
342
343 if (tmp->flag [FLAG_ALIVE])
344 {
345 hit_player (tmp, op->stats.dam, op, type, full_hit);
346 retflag |= 1;
347
348 if (op->destroyed ())
349 break;
350 }
351 /* Here we are potentially destroying an object. If the object has
352 * NO_PASS set, it is also immune - you can't destroy walls. Note
353 * that weak walls have is_alive set, which prevent objects from
354 * passing over/through them. We don't care what type of movement
355 * the wall blocks - if it blocks any type of movement, can't be
356 * destroyed right now.
357 * Without the material check the server completely fails to work,
358 * objects detsroy themselves, floors get destroyed etc. etc.
359 */
360 else if (op->stats.dam > 0 && !tmp->move_block && tmp->material != MATERIAL_NULL)
361 {
362 save_throw_object (tmp, type, op);
363
364 if (op->destroyed ())
365 break;
366 }
367 }
368
369 return 0;
370 }
371
372 static void
373 attack_message (int dam, int type, object *op, object *hitter)
374 {
375 char buf[MAX_BUF], buf1[MAX_BUF], buf2[MAX_BUF];
376 int i, found = 0;
377 maptile *map;
378 object *next, *tmp;
379
380 /* put in a few special messages for some of the common attacktypes
381 * a player might have. For example, fire, electric, cold, etc
382 * [garbled 20010919]
383 */
384 if (dam == 9998 && op->type == DOOR)
385 {
386 sprintf (buf1, "unlock %s", &op->name);
387 sprintf (buf2, " unlocks");
388 found++;
389 }
390 else if (dam < 0)
391 {
392 sprintf (buf1, "hit %s", &op->name);
393 sprintf (buf2, " hits");
394 found++;
395 }
396 else if (dam == 0)
397 {
398 sprintf (buf1, "missed %s", &op->name);
399 sprintf (buf2, " misses");
400 found++;
401 }
402 else if ((hitter->type == DISEASE || hitter->type == SYMPTOM ||
403 hitter->type == POISONING || (type & AT_POISON && op->is_alive ())) && !found)
404 {
405 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_SUFFER][i].level != -1; i++)
406 if (dam < attack_mess[ATM_SUFFER][i].level || attack_mess[ATM_SUFFER][i + 1].level == -1)
407 {
408 sprintf (buf1, "%s %s%s", attack_mess[ATM_SUFFER][i].buf1, &op->name, attack_mess[ATM_SUFFER][i].buf2);
409 strcpy (buf2, attack_mess[ATM_SUFFER][i].buf3);
410 found++;
411 break;
412 }
413 }
414 else if (op->type == DOOR && !found)
415 {
416 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_DOOR][i].level != -1; i++)
417 if (dam < attack_mess[ATM_DOOR][i].level || attack_mess[ATM_DOOR][i + 1].level == -1)
418 {
419 sprintf (buf1, "%s %s%s", attack_mess[ATM_DOOR][i].buf1, &op->name, attack_mess[ATM_DOOR][i].buf2);
420 strcpy (buf2, attack_mess[ATM_DOOR][i].buf3);
421 found++;
422 break;
423 }
424 }
425 else if (hitter->type == PLAYER && op->is_alive ())
426 {
427 if (USING_SKILL (hitter, SK_KARATE))
428 {
429 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_KARATE][i].level != -1; i++)
430 if (dam < attack_mess[ATM_KARATE][i].level || attack_mess[ATM_KARATE][i + 1].level == -1)
431 {
432 sprintf (buf1, "%s %s%s", attack_mess[ATM_KARATE][i].buf1, &op->name, attack_mess[ATM_KARATE][i].buf2);
433 strcpy (buf2, attack_mess[ATM_KARATE][i].buf3);
434 found++;
435 break;
436 }
437 }
438 else if (USING_SKILL (hitter, SK_CLAWING))
439 {
440 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_CLAW][i].level != -1; i++)
441 if (dam < attack_mess[ATM_CLAW][i].level || attack_mess[ATM_CLAW][i + 1].level == -1)
442 {
443 sprintf (buf1, "%s %s%s", attack_mess[ATM_CLAW][i].buf1, &op->name, attack_mess[ATM_CLAW][i].buf2);
444 strcpy (buf2, attack_mess[ATM_CLAW][i].buf3);
445 found++;
446 break;
447 }
448 }
449 else if (USING_SKILL (hitter, SK_PUNCHING))
450 {
451 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_PUNCH][i].level != -1; i++)
452 if (dam < attack_mess[ATM_PUNCH][i].level || attack_mess[ATM_PUNCH][i + 1].level == -1)
453 {
454 sprintf (buf1, "%s %s%s", attack_mess[ATM_PUNCH][i].buf1, &op->name, attack_mess[ATM_PUNCH][i].buf2);
455 strcpy (buf2, attack_mess[ATM_PUNCH][i].buf3);
456 found++;
457 break;
458 }
459 }
460 }
461
462 if (found)
463 {
464 /* done */
465 }
466 else if (hitter->is_arrow () && (type == AT_PHYSICAL || type == AT_MAGIC))
467 {
468 sprintf (buf1, "hit"); /* just in case */
469 for (i = 0; i < MAXATTACKMESS; i++)
470 if (dam < attack_mess[ATM_ARROW][i].level || attack_mess[ATM_ARROW][i + 1].level == -1)
471 {
472 strcpy (buf2, attack_mess[ATM_ARROW][i].buf3);
473 found++;
474 break;
475 }
476 }
477 else if (type & AT_DRAIN && op->is_alive ())
478 {
479 /* drain is first, because some items have multiple attypes */
480 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_DRAIN][i].level != -1; i++)
481 if (dam < attack_mess[ATM_DRAIN][i].level || attack_mess[ATM_DRAIN][i + 1].level == -1)
482 {
483 sprintf (buf1, "%s %s%s", attack_mess[ATM_DRAIN][i].buf1, &op->name, attack_mess[ATM_DRAIN][i].buf2);
484 strcpy (buf2, attack_mess[ATM_DRAIN][i].buf3);
485 found++;
486 break;
487 }
488 }
489 else if (type & AT_ELECTRICITY && op->is_alive ())
490 {
491 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_ELEC][i].level != -1; i++)
492 if (dam < attack_mess[ATM_ELEC][i].level || attack_mess[ATM_ELEC][i + 1].level == -1)
493 {
494 sprintf (buf1, "%s %s%s", attack_mess[ATM_ELEC][i].buf1, &op->name, attack_mess[ATM_ELEC][i].buf2);
495 strcpy (buf2, attack_mess[ATM_ELEC][i].buf3);
496 found++;
497 break;
498 }
499 }
500 else if (type & AT_COLD && op->is_alive ())
501 {
502 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_COLD][i].level != -1; i++)
503 if (dam < attack_mess[ATM_COLD][i].level || attack_mess[ATM_COLD][i + 1].level == -1)
504 {
505 sprintf (buf1, "%s %s%s", attack_mess[ATM_COLD][i].buf1, &op->name, attack_mess[ATM_COLD][i].buf2);
506 strcpy (buf2, attack_mess[ATM_COLD][i].buf3);
507 found++;
508 break;
509 }
510 }
511 else if (type & AT_FIRE)
512 {
513 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_FIRE][i].level != -1; i++)
514 if (dam < attack_mess[ATM_FIRE][i].level || attack_mess[ATM_FIRE][i + 1].level == -1)
515 {
516 sprintf (buf1, "%s %s%s", attack_mess[ATM_FIRE][i].buf1, &op->name, attack_mess[ATM_FIRE][i].buf2);
517 strcpy (buf2, attack_mess[ATM_FIRE][i].buf3);
518 found++;
519 break;
520 }
521 }
522 else if (hitter->current_weapon)
523 {
524 int mtype;
525
526 switch (hitter->current_weapon->weapontype)
527 {
528 case WEAP_HIT: mtype = ATM_BASIC; break;
529 case WEAP_SLASH: mtype = ATM_SLASH; break;
530 case WEAP_PIERCE: mtype = ATM_PIERCE; break;
531 case WEAP_CLEAVE: mtype = ATM_CLEAVE; break;
532 case WEAP_SLICE: mtype = ATM_SLICE; break;
533 case WEAP_STAB: mtype = ATM_STAB; break;
534 case WEAP_WHIP: mtype = ATM_WHIP; break;
535 case WEAP_CRUSH: mtype = ATM_CRUSH; break;
536 case WEAP_BLUD: mtype = ATM_BLUD; break;
537 default: mtype = ATM_BASIC; break;
538 }
539
540 for (i = 0; i < MAXATTACKMESS && attack_mess[mtype][i].level != -1; i++)
541 if (dam < attack_mess[mtype][i].level || attack_mess[mtype][i + 1].level == -1)
542 {
543 sprintf (buf1, "%s %s%s", attack_mess[mtype][i].buf1, &op->name, attack_mess[mtype][i].buf2);
544 strcpy (buf2, attack_mess[mtype][i].buf3);
545 found++;
546 break;
547 }
548 }
549 else
550 {
551 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_BASIC][i].level != -1; i++)
552 if (dam < attack_mess[ATM_BASIC][i].level || attack_mess[ATM_BASIC][i + 1].level == -1)
553 {
554 sprintf (buf1, "%s %s%s", attack_mess[ATM_BASIC][i].buf1, &op->name, attack_mess[ATM_BASIC][i].buf2);
555 strcpy (buf2, attack_mess[ATM_BASIC][i].buf3);
556 found++;
557 break;
558 }
559 }
560
561 if (!found)
562 {
563 strcpy (buf1, "hit");
564 strcpy (buf2, " hits");
565 }
566
567 /* bail out if a monster is casting spells */
568 if (!(hitter->type == PLAYER || (hitter->owner != NULL && hitter->owner->type == PLAYER)))
569 return;
570
571 /* scale down magic considerably. */
572 if (type & AT_MAGIC && rndm (0, 5))
573 return;
574
575 /* Did a player hurt another player? Inform both! */
576 if (op->type == PLAYER && (hitter->owner == NULL ? hitter->type : hitter->owner->type) == PLAYER)
577 {
578 if (hitter->owner != NULL)
579 sprintf (buf, "%s's %s%s you.", &hitter->owner->name, &hitter->name, buf2);
580 else
581 {
582 sprintf (buf, "%s%s you.", &hitter->name, buf2);
583
584 if (dam != 0)
585 {
586 if (dam < 10)
587 op->contr->play_sound (sound_find ("player_is_hit1"));
588 else if (dam < 20)
589 op->contr->play_sound (sound_find ("player_is_hit2"));
590 else
591 op->contr->play_sound (sound_find ("player_is_hit3"));
592 }
593 }
594
595 new_draw_info (NDI_BLACK, 0, op, buf);
596 } /* end of player hitting player */
597
598 if (hitter->type == PLAYER)
599 {
600 sprintf (buf, "You %s.", buf1);
601
602 if (dam != 0)
603 {
604 if (dam < 10)
605 op->play_sound (sound_find ("player_hits1"));
606 else if (dam < 20)
607 op->play_sound (sound_find ("player_hits2"));
608 else
609 op->play_sound (sound_find ("player_hits3"));
610 }
611
612 new_draw_info (NDI_BLACK, 0, hitter, buf);
613 }
614 else if (hitter->owner != NULL && hitter->owner->type == PLAYER)
615 {
616 /* look for stacked spells and start reducing the message chances */
617 if (hitter->type == SPELL_EFFECT && (hitter->subtype == SP_EXPLOSION || hitter->subtype == SP_BULLET || hitter->subtype == SP_CONE))
618 {
619 i = 4;
620 map = hitter->map;
621 if (out_of_map (map, hitter->x, hitter->y))
622 return;
623
624 next = GET_MAP_OB (map, hitter->x, hitter->y);
625 if (next)
626 while (next)
627 {
628 if (next->type == SPELL_EFFECT && (next->subtype == SP_EXPLOSION || next->subtype == SP_BULLET || next->subtype == SP_CONE))
629 i *= 3;
630
631 tmp = next;
632 next = tmp->above;
633 }
634
635 if (i < 0)
636 return;
637
638 if (rndm (0, i) != 0)
639 return;
640 }
641 else if (rndm (0, 5) != 0)
642 return;
643
644 sprintf (buf, "Your %s%s %s.", &hitter->name, buf2, &op->name);
645 op->play_sound (sound_find ("player_hits4"));
646 new_draw_info (NDI_BLACK, 0, hitter->owner, buf);
647 }
648 }
649
650 static int
651 get_attack_mode (object **target, object **hitter, int *simple_attack)
652 {
653 if ((*target)->flag [FLAG_FREED] || (*hitter)->flag [FLAG_FREED])
654 {
655 LOG (llevError, "BUG: get_attack_mode(): freed object\n");
656 return 1;
657 }
658
659 *target = (*target)->head_ ();
660 *hitter = (*hitter)->head_ ();
661
662 if ((*target)->type == LOCKED_DOOR)
663 return 1; // locked doors cannot be hit
664
665 if ((*hitter)->env || (*target)->env)
666 {
667 *simple_attack = 1;
668 return 0;
669 }
670
671 if ((*target)->flag [FLAG_REMOVED]
672 || (*hitter)->flag [FLAG_REMOVED]
673 || !(*hitter)->map
674 || !on_same_map (*hitter, *target))
675 {
676 LOG (llevError | logBacktrace, "BUG: hitter (%s) with no relation to target (%s)\n",
677 (*hitter)->debug_desc (), (*target)->debug_desc ());
678 return 1;
679 }
680
681 *simple_attack = 0;
682 return 0;
683 }
684
685 static int
686 abort_attack (object *target, object *hitter, int simple_attack)
687 {
688 /* Check if target and hitter are still in a relation similar to the one
689 * determined by get_attack_mode(). Returns true if the relation has changed.
690 */
691 int new_mode;
692
693 if (hitter->env == target || target->env == hitter)
694 new_mode = 1;
695 else if (hitter->flag [FLAG_REMOVED] || target->flag [FLAG_REMOVED]
696 || hitter->map == NULL || !on_same_map (hitter, target))
697 return 1;
698 else
699 new_mode = 0;
700
701 return new_mode != simple_attack;
702 }
703
704 /* thrown_item_effect() - handles any special effects of thrown
705 * items (like attacking living creatures--a potion thrown at a
706 * monster).
707 */
708 static void
709 thrown_item_effect (object *hitter, object *victim)
710 {
711 if (!hitter->flag [FLAG_ALIVE])
712 {
713 /* May not need a switch for just 2 types, but this makes it
714 * easier for expansion.
715 */
716 switch (hitter->type)
717 {
718 case POTION:
719 /* should player get a save throw instead of checking magic protection? */
720 if (victim->flag [FLAG_ALIVE] && !victim->flag [FLAG_UNDEAD] && (victim->resist[ATNR_MAGIC] < 60))
721 apply_potion (victim, hitter);
722 break;
723
724 case POISON: /* poison drinks */
725 /* As with potions, should monster get a save? */
726 if (victim->flag [FLAG_ALIVE] && !victim->flag [FLAG_UNDEAD] && (victim->resist[ATNR_POISON] < 60))
727 apply_poison (victim, hitter);
728 break;
729
730 /* Removed case statements that did nothing.
731 * food may be poisonous, but monster must be willing to eat it,
732 * so we don't handle it here.
733 * Containers should perhaps break open, but that code was disabled.
734 */
735 }
736 }
737 }
738
739 /* determine if the object is an 'aimed' missile */
740 static int
741 is_aimed_missile (object *op)
742 {
743 /* I broke what used to be one big if into a few nested
744 * ones so that figuring out the logic is at least possible.
745 */
746 if (op && (op->move_type & MOVE_FLYING))
747 if (op->type == ARROW || op->type == THROWN_OBJ)
748 return 1;
749 else if (op->type == SPELL_EFFECT && (op->subtype == SP_BULLET || op->subtype == SP_EXPLOSION))
750 return 1;
751
752 return 0;
753 }
754 /* adj_attackroll() - adjustments to attacks by various conditions */
755 static int
756 adj_attackroll (object *hitter, object *target)
757 {
758 object *attacker = hitter;
759 int adjust = 0;
760
761 /* safety */
762 if (!target || !hitter || !hitter->map || !target->map || !on_same_map (hitter, target))
763 {
764 LOG (llevError, "BUG: adj_attackroll(): hitter and target not on same " "map\n");
765 return 0;
766 }
767
768 /* aimed missiles use the owning object's sight */
769 if (is_aimed_missile (hitter))
770 {
771 if ((attacker = hitter->owner) == NULL)
772 attacker = hitter;
773 /* A player who saves but hasn't quit still could have objects
774 * owned by him - need to handle that case to avoid crashes.
775 */
776 if (attacker->flag [FLAG_REMOVED])
777 attacker = hitter;
778 }
779 else if (!hitter->flag [FLAG_ALIVE])
780 return 0;
781
782 /* determine the condtions under which we make an attack.
783 * Add more cases, as the need occurs. */
784
785 if (!can_see_enemy (attacker, target))
786 {
787 /* target is unseen */
788 if (target->invisible || attacker->flag [FLAG_BLIND])
789 adjust -= 10;
790 /* dark map penalty for the hitter (lacks infravision if we got here). */
791 else if (!stand_in_light (target))
792 adjust -= target->map->darklevel ();
793 }
794
795 if (attacker->flag [FLAG_SCARED])
796 adjust -= 3;
797
798 if (target->flag [FLAG_UNAGGRESSIVE])
799 adjust += 1;
800
801 if (target->flag [FLAG_SCARED])
802 adjust += 1;
803
804 if (attacker->flag [FLAG_CONFUSED])
805 adjust -= 3;
806
807 /* if we attack at a different 'altitude' its harder */
808 if ((attacker->move_type & target->move_type) == 0)
809 adjust -= 2;
810
811 #if 0
812 /* slower attacks are less likely to succeed. We should use a
813 * comparison between attacker/target speeds BUT, players have
814 * a generally faster speed, so this will wind up being a HUGE
815 * disadantage for the monsters! Too bad, because missiles which
816 * fly fast should have a better chance of hitting a slower target.
817 */
818 if (hitter->speed < target->speed)
819 adjust += ((float) hitter->speed - target->speed);
820 #endif
821
822 #if 0
823 LOG (llevDebug, "adj_attackroll() returns %d (%d)\n", adjust, attacker->count);
824 #endif
825
826 return adjust;
827 }
828
829 static int
830 attack_ob_simple (object *op, object *hitter, int base_dam, int base_wc)
831 {
832 int simple_attack, roll, dam = 0;
833 uint32 type;
834 shstr op_name;
835
836 if (get_attack_mode (&op, &hitter, &simple_attack))
837 goto error;
838
839 if (hitter->current_weapon)
840 if (INVOKE_OBJECT (WEAPON_ATTACK, hitter->current_weapon, ARG_OBJECT (hitter), ARG_OBJECT (op)))
841 return RESULT_INT (0);
842
843 if (INVOKE_OBJECT (ATTACK, op, ARG_OBJECT (hitter)))
844 return RESULT_INT (0);
845
846 /*
847 * A little check to make it more difficult to dance forward and back
848 * to avoid ever being hit by monsters.
849 */
850 if (!simple_attack && op->flag [FLAG_MONSTER] && op->speed_left > op->speed * -0.3f)
851 {
852 /* Decrease speed BEFORE calling process_object. Otherwise, an
853 * infinite loop occurs, with process_object calling move_monster,
854 * which then gets here again. By decreasing the speed before
855 * we call process_object, the 'if' statement above will fail.
856 */
857 --op->speed_left;
858 process_object (op);
859
860 if (op->destroyed () || hitter->destroyed () || abort_attack (op, hitter, simple_attack))
861 goto error;
862 }
863
864 op_name = op->name;
865
866 roll = random_roll (1, 20, hitter, PREFER_HIGH);
867
868 /* Adjust roll for various situations. */
869 if (!simple_attack)
870 roll += adj_attackroll (hitter, op);
871
872 /* See if we hit the creature */
873 if (roll == 20 || op->stats.ac >= base_wc - roll)
874 {
875 int hitdam = base_dam;
876
877 if (!simple_attack)
878 {
879 /* If you hit something, the victim should *always* wake up.
880 * Before, invisible hitters could avoid doing this.
881 * -b.t. */
882 if (op->flag [FLAG_SLEEP])
883 op->clr_flag (FLAG_SLEEP);
884
885 /* If the victim can't see the attacker, it may alert others
886 * for help. */
887 if (op->type != PLAYER && !can_see_enemy (op, hitter) && !op->owner && rndm (0, op->stats.Int))
888 npc_call_help (op);
889
890 /* if you were hidden and hit by a creature, you are discovered */
891 if (op->flag [FLAG_HIDDEN] && hitter->flag [FLAG_ALIVE])
892 {
893 make_visible (op);
894
895 if (op->type == PLAYER)
896 new_draw_info (NDI_UNIQUE, 0, op, "You were hit by a wild attack. You are no longer hidden!");
897 }
898
899 /* thrown items (hitter) will have various effects
900 * when they hit the victim. For things like thrown daggers,
901 * this sets 'hitter' to the actual dagger, and not the
902 * wrapper object.
903 */
904 thrown_item_effect (hitter, op);
905
906 if (hitter->destroyed () || op->destroyed () || abort_attack (op, hitter, simple_attack))
907 goto leave;
908 }
909
910 /* Need to do at least 1 damage, otherwise there is no point
911 * to go further and it will cause FPE's below.
912 */
913 max_it (hitdam, 1);
914
915 type = hitter->attacktype;
916
917 /* Ok, because some of the brainfucks of the good *sigh* old *sigh*
918 * Crossfire we have to default the attacktype here to AT_PHYSICAL.
919 * This check is important for the most simple monsters out there in the
920 * game content (maps, archs). For example orcs: They would have
921 * no attacktype at all.
922 *
923 * Some time in the future someone should just go into the game data
924 * and fix every monster out there ;-/ Until then we will kill some
925 * more trees in the african rain forests with this check.
926 */
927 if (!type)
928 type = AT_PHYSICAL;
929
930 /* Handle monsters that hit back */
931 if (!simple_attack && op->flag [FLAG_HITBACK] && hitter->flag [FLAG_ALIVE])
932 {
933 if (op->attacktype & AT_ACID && hitter->type == PLAYER)
934 new_draw_info (NDI_UNIQUE, 0, hitter, "You are splashed by acid!\n");
935
936 hit_player (hitter, random_roll (0, (op->stats.dam), hitter, PREFER_LOW), op, op->attacktype, 1);
937
938 if (op->destroyed () || hitter->destroyed () || abort_attack (op, hitter, simple_attack))
939 goto leave;
940 }
941
942 /* In the new attack code, it should handle multiple attack
943 * types in its area, so remove it from here.
944 */
945 dam = hit_player (op, random_roll (1, hitdam, hitter, PREFER_HIGH), hitter, type, 1);
946
947 if (op->destroyed () || hitter->destroyed () || abort_attack (op, hitter, simple_attack))
948 goto leave;
949 } /* end of if hitter hit op */
950 /* if we missed, dam=0 */
951
952 /*attack_message(dam, type, op, hitter); */
953
954 goto leave;
955
956 error:
957 dam = 1;
958
959 leave:
960
961 return dam;
962 }
963
964 int
965 attack_ob (object *op, object *hitter)
966 {
967 return attack_ob_simple (op, hitter->head_ (), hitter->stats.dam, hitter->stats.wc);
968 }
969
970 /* op is the arrow, tmp is what is stopping the arrow.
971 *
972 * Returns 1 if op was inserted into tmp's inventory, 0 otherwise.
973 */
974 static int
975 stick_arrow (object *op, object *tmp)
976 {
977 /* If the missile hit a player, we insert it in their inventory.
978 * However, if the missile is heavy, we don't do so (assume it falls
979 * to the ground after a hit). What a good value for this is up to
980 * debate - 5000 is 5 kg, so arrows, knives, and other light weapons
981 * stick around.
982 */
983 if (op->weight <= 5000 && tmp->stats.hp >= 0)
984 {
985 tmp->head_ ()->insert (op);
986 return 1;
987 }
988 else
989 return 0;
990 }
991
992 /* hit_with_arrow() disassembles the missile, attacks the victim and
993 * reassembles the missile.
994 *
995 * It returns a pointer to the reassembled missile, or NULL if the missile
996 * isn't available anymore.
997 */
998 object *
999 hit_with_arrow (object *op, object *victim)
1000 {
1001 object *container, *hitter;
1002 int hit_something = 0;
1003
1004 /* Disassemble missile */
1005 if (op->inv)
1006 {
1007 container = op;
1008 hitter = op->inv;
1009 hitter->insert_at (container, hitter, INS_NO_MERGE | INS_NO_WALK_ON);
1010 /* Note that we now have an empty THROWN_OBJ on the map. Code that
1011 * might be called until this THROWN_OBJ is either reassembled or
1012 * removed at the end of this function must be able to deal with empty
1013 * THROWN_OBJs. */
1014 }
1015 else
1016 {
1017 container = 0;
1018 hitter = op;
1019 }
1020
1021 /* Try to hit victim */
1022 hit_something = attack_ob_simple (victim, hitter, op->stats.dam, op->stats.wc);
1023
1024 /* Arrow attacks door, rune of summoning is triggered, demon is put on
1025 * arrow, move_apply() calls this function, arrow sticks in demon,
1026 * attack_ob_simple() returns, and we've got an arrow that still exists
1027 * but is no longer on the map. Ugh. (Beware: Such things can happen at
1028 * other places as well!)
1029 */
1030 if (hitter->destroyed () || hitter->env != NULL)
1031 {
1032 if (container)
1033 container->destroy ();
1034
1035 return 0;
1036 }
1037
1038 /* Missile hit victim */
1039 /* if the speed is > 10, then this is a fast moving arrow, we go straight
1040 * through the target
1041 */
1042 if (hit_something && op->speed <= 10.0)
1043 {
1044 /* Stop arrow */
1045 if (!container)
1046 {
1047 hitter = fix_stopped_arrow (hitter);
1048 if (!hitter)
1049 return 0;
1050 }
1051 else
1052 container->destroy ();
1053
1054 /* Try to stick arrow into victim */
1055 if (!victim->destroyed () && stick_arrow (hitter, victim))
1056 return 0;
1057
1058 /* Else try to put arrow on victim's map square
1059 * remove check for P_WALL here. If the arrow got to this
1060 * space, that is good enough - with the new movement code,
1061 * there is now the potential for lots of spaces where something
1062 * can fly over but not otherwise move over. What is the correct
1063 * way to handle those otherwise?
1064 */
1065 if (victim->x != hitter->x || victim->y != hitter->y)
1066 {
1067 if (victim->destroyed ())
1068 hitter->destroy ();
1069 else
1070 {
1071 hitter->remove ();
1072 hitter->x = victim->x;
1073 hitter->y = victim->y;
1074 insert_ob_in_map (hitter, victim->map, hitter, 0);
1075 }
1076 }
1077 else
1078 /* Else leave arrow where it is */
1079 merge_ob (hitter, NULL);
1080
1081 return 0;
1082 }
1083
1084 if (hit_something && op->speed >= 10.0)
1085 op->set_speed (op->speed - 1.f);
1086
1087 /* Missile missed victim - reassemble missile */
1088 if (container)
1089 {
1090 hitter->remove ();
1091 insert_ob_in_ob (hitter, container);
1092 }
1093
1094 return op;
1095 }
1096
1097 static void
1098 tear_down_wall (object *op)
1099 {
1100 if (!op->stats.maxhp)
1101 LOG (llevError, "TEAR_DOWN wall %s had no maxhp.\n", &op->name);
1102 else if (!op->has_anim ())
1103 {
1104 /* Object has been called - no animations, so remove it */
1105 if (op->stats.hp < 0)
1106 op->destroy ();
1107
1108 return; /* no animations, so nothing more to do */
1109 }
1110
1111 // we use frames 1..num-2 as intermediate frames, so
1112 // the last frame is used only when hp < 0.
1113 int perc = clamp (
1114 lerp_ru<int> (op->stats.hp, 0, op->stats.maxhp, op->anim_frames () - 2, 0),
1115 0, op->anim_frames () - 1
1116 );
1117
1118 op->set_anim_frame (perc);
1119 update_object (op, UP_OBJ_FACE);
1120
1121 if (op->stats.hp < 0)
1122 { /* Reached the last animation */
1123 if (op->face == blank_face)
1124 /* If the last face is blank, remove the ob */
1125 op->destroy ();
1126 else
1127 { /* The last face was not blank, leave an image */
1128 op->flag [FLAG_BLOCKSVIEW] = false;
1129 update_all_los (op->map, op->x, op->y);
1130 op->move_block = 0;
1131 op->flag [FLAG_ALIVE] = false;
1132 }
1133 }
1134 }
1135
1136 static void
1137 scare_creature (object *target, object *hitter)
1138 {
1139 target->flag [FLAG_SCARED] = true;
1140
1141 if (!target->enemy)
1142 target->enemy = hitter->outer_owner ();
1143 }
1144
1145 /* GROS: This code comes from hit_player. It has been made external to
1146 * allow script procedures to "kill" objects in a combat-like fashion.
1147 * It was initially used by (kill-object) developed for the Collector's
1148 * Sword. Note that nothing has been changed from the original version
1149 * of the following code.
1150 * op is what is being killed.
1151 * dam is the damage done to it.
1152 * hitter is what is hitting it.
1153 * type is the attacktype.
1154 *
1155 * This function was a bit of a mess with hitter getting changed,
1156 * values being stored away but not used, etc. I've cleaned it up
1157 * a bit - I think it should be functionally equivalant.
1158 * MSW 2002-07-17
1159 */
1160 int
1161 kill_object (object *op, int dam, object *hitter, int type)
1162 {
1163 char buf[MAX_BUF];
1164 shstr skill;
1165 int maxdam = 0;
1166 int battleg = 0; /* true if op standing on battleground */
1167 int pk = 0; /* true if op and what controls hitter are both players */
1168 object *owner = 0;
1169 object *skop = 0;
1170
1171 if (op->stats.hp >= 0)
1172 return -1;
1173
1174 if (INVOKE_OBJECT (KILL, op, ARG_OBJECT (hitter)))
1175 return 0;
1176
1177 /* maxdam needs to be the amount of damage it took to kill
1178 * this creature. The function(s) that call us have already
1179 * adjusted the creatures HP total, so that is negative.
1180 */
1181 maxdam = dam + op->stats.hp + 1;
1182
1183 if (op->flag [FLAG_BLOCKSVIEW])
1184 update_all_los (op->map, op->x, op->y); /* makes sure los will be recalculated */
1185
1186 if (op->type == DOOR)
1187 {
1188 op->set_speed (0.1f);
1189 op->speed_left = -0.05f;
1190 return maxdam;
1191 }
1192
1193 if (op->flag [FLAG_FRIENDLY] && op->type != PLAYER)
1194 {
1195 op->drop_and_destroy ();
1196 return maxdam;
1197 }
1198
1199 /* Now lets start dealing with experience we get for killing something */
1200
1201 owner = hitter->outer_owner ();
1202 if (!owner)
1203 owner = hitter;
1204
1205 /* is the victim (op) standing on battleground? */
1206 if (op_on_battleground (op, NULL, NULL))
1207 battleg = 1;
1208
1209 /* is this player killing? */
1210 if (op->type == PLAYER && owner->type == PLAYER)
1211 pk = 1;
1212
1213 /* Player killed something */
1214 if (owner->type == PLAYER)
1215 {
1216 /* Log players killing other players - makes it easier to detect
1217 * and filter out malicious player killers - that is why the
1218 * ip address is included.
1219 */
1220 if (op->type == PLAYER && !battleg)
1221 {
1222 time_t t = time (NULL);
1223 struct tm *tmv;
1224 char buf[256];
1225
1226 tmv = localtime (&t);
1227 strftime (buf, 256, "%a %b %d %H:%M:%S %Y", tmv);
1228
1229 LOG (llevInfo, "%s PLAYER_KILL_PLAYER: %s (%s) killed %s\n", buf, &owner->name, owner->contr->ns->host, query_name (op));
1230 }
1231
1232 /* try to filter some things out - basically, if you are
1233 * killing a level 1 creature and your level 20, you
1234 * probably don't want to see that.
1235 */
1236 if (owner->level < op->level * 2 || op->stats.exp > 1000)
1237 {
1238 if (owner != hitter)
1239 new_draw_info_format (NDI_BLACK, 0, owner, "You killed %s with %s.", query_name (op), query_name (hitter));
1240 else
1241 new_draw_info_format (NDI_BLACK, 0, owner, "You killed %s.", query_name (op));
1242
1243 /* Only play sounds for melee kills */
1244 if (hitter->type == PLAYER)
1245 owner->play_sound (sound_find ("player_kills"));
1246 }
1247
1248 /* If a player kills another player, not on
1249 * battleground, the "killer" looses 1 luck. Since this is
1250 * not reversible, it's actually quite a pain IMHO. -AV
1251 * Fix bug in that we were changing the luck of the hitter, not
1252 * player that the object belonged to - so if you killed another player
1253 * with spells, pets, whatever, there was no penalty.
1254 * Changed to make luck penalty configurable in settings.
1255 */
1256 if (op->type == PLAYER && owner != op && !battleg)
1257 owner->change_luck (-settings.pk_luck_penalty);
1258
1259 /* This code below deals with finding the appropriate skill
1260 * to credit exp to. This is a bit problematic - we should
1261 * probably never really have to look at current_weapon->skill
1262 */
1263 if (hitter->skill && hitter->type != PLAYER)
1264 skill = hitter->skill;
1265 else if (owner->chosen_skill)
1266 {
1267 skop = owner->chosen_skill;
1268 skill = skop->skill;
1269 }
1270 else if (owner->flag [FLAG_READY_WEAPON] && owner->current_weapon)
1271 skill = owner->current_weapon->skill;
1272 else
1273 {
1274 LOG (llevError | logBacktrace,
1275 "BUG: kill_object - unable to find skill that killed monster\n"
1276 "op: %s\n" "hitter: %s\n" "op: %s\n",
1277 op->debug_desc (), hitter->debug_desc (), op->debug_desc ());
1278 skill = 0;
1279 }
1280
1281 /* We have the skill we want to credit to - now find the object this goes
1282 * to. Make sure skop is an actual skill, and not a skill tool!
1283 */
1284 skop = owner->contr->find_skill (skill);
1285 } /* Was it a player that hit somethign */
1286 else
1287 skill = 0;
1288
1289 /* These may have been set in the player code section above */
1290 if (!skop)
1291 skop = hitter->chosen_skill;
1292
1293 if (!skill && skop)
1294 skill = skop->skill;
1295
1296 /* If you didn't kill yourself, and your not the wizard */
1297 if (owner != op && !op->flag [FLAG_WIZ])
1298 {
1299 int exp;
1300
1301 /* Really don't give much experience for killing other players */
1302 // schmorp: temporarily? reduce the amount of exp gained for pking enourmously
1303 if (battleg)
1304 {
1305 if (op->is_player ())
1306 {
1307 new_draw_info (NDI_UNIQUE, 0, owner, "Your foe has fallen!");
1308 new_draw_info (NDI_UNIQUE, 0, owner, "VICTORY!!!");
1309 }
1310
1311 exp = 0;
1312 }
1313 else if (op->is_player ())
1314 exp = op->stats.exp / 1000;
1315 else
1316 exp = calc_skill_exp (owner, op, skop);
1317
1318 /* Don't know why this is set this way - doesn't make
1319 * sense to just divide everything by two for no reason.
1320 */
1321
1322 if (!settings.simple_exp)
1323 exp = exp / 2;
1324
1325 if (owner->type != PLAYER || owner->contr->party == NULL)
1326 change_exp (owner, exp, skill, 0);
1327 else
1328 {
1329 int shares = 0, count = 0;
1330 partylist *party = owner->contr->party;
1331
1332 add_kill_to_party (party, query_name (owner), query_name (op), exp);
1333
1334 for_all_players (pl)
1335 if (party && pl->ob->contr->party == party && on_same_map (pl->ob, owner))
1336 {
1337 count++;
1338 shares += (pl->ob->level + 4);
1339 }
1340
1341 if (count == 1 || shares > exp || !shares)
1342 change_exp (owner, exp, skill, SK_EXP_TOTAL);
1343 else
1344 {
1345 int share = exp / shares, given = 0, nexp;
1346
1347 for_all_players (pl)
1348 if (party && pl->ob->contr->party == party && on_same_map (pl->ob, owner))
1349 {
1350 nexp = (pl->ob->level + 4) * share;
1351 change_exp (pl->ob, nexp, skill, SK_EXP_TOTAL);
1352 given += nexp;
1353 }
1354
1355 exp -= given;
1356 /* give any remainder to the player */
1357 change_exp (owner, exp, skill, SK_EXP_ADD_SKILL);
1358 }
1359 } /* else part of a party */
1360 } /* end if person didn't kill himself */
1361
1362 if (op->type != PLAYER)
1363 {
1364 if (op->flag [FLAG_FRIENDLY])
1365 {
1366 object *owner1 = op->owner;
1367
1368 if (owner1 && owner1->type == PLAYER)
1369 {
1370 owner1->contr->play_sound (sound_find ("pet_is_killed"));
1371 /* Maybe we should include the owner that killed this, maybe not */
1372 new_draw_info_format (NDI_UNIQUE, 0, owner1, "Your pet, the %s, was killed by %s.", &op->name, &hitter->name);
1373 }
1374
1375 remove_friendly_object (op);
1376 }
1377
1378 op->drop_and_destroy ();
1379 }
1380 else
1381 /* Player has been killed! */
1382 op->contr->killer = owner->type == PLAYER ? owner : hitter;
1383
1384 /* This was return -1 - that doesn't seem correct - if we return -1, process
1385 * continues in the calling function.
1386 */
1387 return maxdam;
1388 }
1389
1390 /* Find out if this is friendly fire (PVP and attacker is peaceful) or not
1391 * Returns 0 this is not friendly fire
1392 */
1393 int
1394 friendly_fire (object *op, object *hitter)
1395 {
1396 object *owner;
1397 int friendlyfire;
1398
1399 if (hitter->head)
1400 hitter = hitter->head;
1401
1402 friendlyfire = 0;
1403
1404 if (op->type == PLAYER)
1405 {
1406 if (op_on_battleground (hitter, 0, 0))
1407 return 0;
1408
1409 if (hitter->type == PLAYER && hitter->contr->peaceful == 1)
1410 return 1;
1411
1412 if ((owner = hitter->owner) != NULL)
1413 {
1414 if (owner->type == PLAYER && owner->contr->peaceful == 1)
1415 friendlyfire = 2;
1416 }
1417
1418 if (hitter->type == SPELL || hitter->type == POISONING || hitter->type == DISEASE || hitter->type == RUNE)
1419 friendlyfire = 0;
1420 }
1421
1422 return friendlyfire;
1423 }
1424
1425 /* This isn't used just for players, but in fact most objects.
1426 * op is the object to be hit, dam is the amount of damage, hitter
1427 * is what is hitting the object, type is the attacktype, and
1428 * full_hit is set if monster area does not matter.
1429 * dam is base damage - protections/vulnerabilities/slaying matches can
1430 * modify it.
1431 */
1432 /* Oct 95 - altered the following slightly for MULTIPLE_GODS hack
1433 * which needs new attacktype AT_HOLYWORD to work . b.t. */
1434 int
1435 hit_player (object *op, int dam, object *hitter, uint32_t type, int full_hit)
1436 {
1437 int magic = type & AT_MAGIC;
1438 int body_attack = op && op->head; /* Did we hit op's head? */
1439 int maxdam = 0, ndam = 0, attacktype = 1;
1440 int maxattacktype;
1441 int simple_attack;
1442 int rtn_kill = 0;
1443 int friendlyfire;
1444
1445 if (get_attack_mode (&op, &hitter, &simple_attack))
1446 return 0;
1447
1448 /* very simple: if our target has no_damage 1 set or is wiz, we can't hurt him */
1449 if (op->flag [FLAG_WIZ] || op->flag [FLAG_NO_DAMAGE])
1450 return 0;
1451
1452 // only allow pk for hostile players
1453 if (op->type == PLAYER)
1454 {
1455 object *owner = hitter->owner;
1456
1457 if (!owner)
1458 owner = hitter;
1459
1460 if (owner->type == PLAYER
1461 && (!op_on_battleground (op, 0, 0)
1462 && (op->contr->peaceful || owner->contr->peaceful))
1463 && op != owner)
1464 return 0;
1465 }
1466
1467 if (body_attack)
1468 {
1469 /* slow and paralyze must hit the head. But we don't want to just
1470 * return - we still need to process other attacks the spell still
1471 * might have. So just remove the paralyze and slow attacktypes,
1472 * and keep on processing if we have other attacktypes.
1473 * return if only magic or nothing is left - under normal code
1474 * we don't attack with pure magic if there is another attacktype.
1475 * Only do processing if the initial attacktype includes one of those
1476 * attack so we don't cancel out things like magic bullet.
1477 */
1478 if (type & (AT_PARALYZE | AT_SLOW))
1479 {
1480 type &= ~(AT_PARALYZE | AT_SLOW);
1481
1482 if (!type || type == AT_MAGIC)
1483 return 0;
1484 }
1485 }
1486
1487 if (!simple_attack && op->type == DOOR)
1488 {
1489 for (object *tmp = op->inv; tmp; tmp = tmp->below)
1490 if (tmp->type == RUNE || tmp->type == TRAP)
1491 {
1492 spring_trap (tmp, hitter);
1493
1494 if (hitter->destroyed () || op->destroyed () || abort_attack (op, hitter, simple_attack))
1495 return 0;
1496
1497 break;
1498 }
1499 }
1500
1501 if (!op->flag [FLAG_ALIVE] || op->stats.hp < 0)
1502 {
1503 /* FIXME: If a player is killed by a rune in a door, the
1504 * destroyed() check above doesn't return, and might get here.
1505 */
1506
1507 /* FIXME: This for example happens when a dead door is on a mover and
1508 gets it's speed_left raised on each mover-tick.
1509 Doors are removed in a kinda funny way by giving them speed and speed_left
1510 and waiting for that to run out.
1511 */
1512 LOG (llevDebug, "victim %s (%d) already dead in hit_player()\n", op->debug_desc (), op->stats.hp);
1513 return 0;
1514 }
1515
1516 #ifdef ATTACK_DEBUG
1517 LOG (llevDebug, "hit player: attacktype %d, dam %d\n", type, dam);
1518 #endif
1519
1520 if (magic)
1521 {
1522 /* basically: dam = dam*(100-op->resist[attacknum])/100;
1523 * in case 0>dam>1, we try to "simulate" a float value-effect */
1524 dam = dam * (100 - op->resist[ATNR_MAGIC]);
1525 if (dam >= 100)
1526 dam /= 100;
1527 else
1528 dam = (dam > rndm (0, 99)) ? 1 : 0;
1529 }
1530
1531 /* AT_CHAOS here is a weapon or monster. Spells are handled by hit_map
1532 * If the attacktype still has chaos, shuffle it, then clear the Chaos bit
1533 */
1534 if (type & AT_CHAOS)
1535 {
1536 shuffle_attack (op, 0); /*0 flag tells it to not change the face */
1537 update_object (op, UP_OBJ_FACE);
1538 type &= ~AT_CHAOS;
1539 }
1540
1541 /* Holyword is really an attacktype modifier (like magic is). If
1542 * holyword is part of an attacktype, then make sure the creature is
1543 * a proper match, otherwise no damage.
1544 */
1545 if (type & AT_HOLYWORD)
1546 {
1547 object *god;
1548
1549 if ((!hitter->slaying
1550 || (!(op->race && hitter->slaying.contains (op->race))
1551 && !(op->name && hitter->slaying.contains (op->name))))
1552 && (!op->flag [FLAG_UNDEAD]
1553 || (hitter->title
1554 && (god = find_god (determine_god (hitter))) != NULL
1555 && god->race.contains (shstr_undead))))
1556 return 0;
1557 }
1558
1559 maxattacktype = type; /* initialise this to something */
1560 for_all_bits_sparse_32 (type, attacknum)
1561 {
1562 uint32_t attacktype = 1 << attacknum;
1563
1564 /* Magic isn't really a true attack type - it gets combined with other
1565 * attack types. As such, skip it over. However, if magic is
1566 * the only attacktype in the group, then still attack with it
1567 */
1568 if ((attacktype == AT_MAGIC) && (type & ~AT_MAGIC))
1569 continue;
1570
1571 /* Go through and hit the player with each attacktype, one by one.
1572 * hit_player_attacktype only figures out the damage, doesn't inflict
1573 * it. It will do the appropriate action for attacktypes with
1574 * effects (slow, paralization, etc.
1575 */
1576 ndam = hit_player_attacktype (op, hitter, dam, attacknum, magic);
1577
1578 /* the >= causes us to prefer messages from special attacks, if
1579 * the damage is equal.
1580 */
1581 if (ndam >= maxdam)
1582 {
1583 maxdam = ndam;
1584 maxattacktype = 1 << attacknum;
1585 }
1586 }
1587
1588 /* if this is friendly fire then do a set % of damage only
1589 * Note - put a check in to make sure this attack is actually
1590 * doing damage - otherwise, the +1 in the code below will make
1591 * an attack do damage before when it otherwise didn't
1592 */
1593 friendlyfire = friendly_fire (op, hitter);
1594 if (friendlyfire && maxdam)
1595 {
1596 maxdam = ((dam * settings.set_friendly_fire) / 100);
1597
1598 #ifdef ATTACK_DEBUG
1599 LOG (llevDebug, "Friendly fire (type:%d setting: %d%) did %d damage dropped to %d\n",
1600 friendlyfire, settings.set_friendly_fire, dam, maxdam);
1601 #endif
1602 }
1603
1604 if (!full_hit)
1605 {
1606 int area;
1607 int remainder;
1608
1609 area = 0;
1610
1611 for (archetype *at = op->arch; at; at = (archetype *)at->more)
1612 area++;
1613
1614 assert (area > 0);
1615
1616 /* basically: maxdam /= area; we try to "simulate" a float
1617 value-effect */
1618 remainder = 100 * (maxdam % area) / area;
1619 maxdam /= area;
1620 if (rndm (100) < remainder)
1621 maxdam++;
1622 }
1623
1624 #ifdef ATTACK_DEBUG
1625 LOG (llevDebug, "Attacktype %d did %d damage\n", type, maxdam);
1626 #endif
1627
1628 // for now, only do this for active objects, otherwise they
1629 // keep a refcount for a long time and I see no usefulness
1630 // for an non-active objetc to know its enemy.
1631 if (op->active)
1632 if (hitter->owner)
1633 op->enemy = hitter->owner;
1634 else if (hitter->flag [FLAG_ALIVE])
1635 op->enemy = hitter;
1636
1637 if (op->flag [FLAG_UNAGGRESSIVE] && op->type != PLAYER)
1638 {
1639 /* The unaggressives look after themselves 8) */
1640 op->clr_flag (FLAG_UNAGGRESSIVE);
1641 npc_call_help (op);
1642 }
1643
1644 if (magic && did_make_save (op, op->level, 0))
1645 maxdam = maxdam / 2;
1646
1647 attack_message (maxdam, maxattacktype, op, hitter);
1648
1649 op->stats.hp -= maxdam;
1650
1651 /* Eneq(@csd.uu.se): Check to see if monster runs away. */
1652 if (op->stats.hp >= 0
1653 && (op->flag [FLAG_MONSTER] || op->type == PLAYER)
1654 && op->stats.hp * 100 < op->stats.maxhp * op->run_away)
1655 {
1656
1657 if (op->flag [FLAG_MONSTER])
1658 op->set_flag (FLAG_RUN_AWAY);
1659 else
1660 scare_creature (op, hitter);
1661 }
1662
1663 if (op->flag [FLAG_TEAR_DOWN])
1664 {
1665 if (maxdam)
1666 tear_down_wall (op);
1667
1668 return maxdam; /* nothing more to do for wall */
1669 }
1670
1671 /* See if the creature has been killed */
1672 rtn_kill = kill_object (op, maxdam, hitter, type);
1673 if (rtn_kill != -1)
1674 return rtn_kill;
1675
1676 /* Used to be ghosthit removal - we now use the ONE_HIT flag. Note
1677 * that before if the player was immune to ghosthit, the monster
1678 * remained - that is no longer the case.
1679 */
1680 if (hitter->flag [FLAG_ONE_HIT])
1681 hitter->drop_and_destroy ();
1682 /* Lets handle creatures that are splitting now */
1683 else if (type & AT_PHYSICAL && !op->flag [FLAG_FREED] && op->flag [FLAG_SPLITTING])
1684 {
1685 int friendly = op->flag [FLAG_FRIENDLY];
1686 int unaggressive = op->flag [FLAG_UNAGGRESSIVE];
1687 object *owner = op->owner;
1688
1689 if (!op->other_arch)
1690 {
1691 LOG (llevError, "SPLITTING without other_arch error.\n");
1692 return maxdam;
1693 }
1694
1695 op->remove ();
1696
1697 for (int i = 0; i < op->stats.food; i++)
1698 { /* This doesn't handle op->more yet */
1699 object *tmp = op->other_arch->instance ();
1700
1701 tmp->stats.hp = op->stats.hp;
1702
1703 if (friendly)
1704 {
1705 add_friendly_object (tmp);
1706 tmp->attack_movement = PETMOVE;
1707
1708 if (owner)
1709 tmp->set_owner (owner);
1710 }
1711
1712 if (unaggressive)
1713 tmp->set_flag (FLAG_UNAGGRESSIVE);
1714
1715 int j = find_first_free_spot (tmp, op->map, op->x, op->y);
1716
1717 if (j == -1) /* No spot to put this monster */
1718 tmp->destroy ();
1719 else
1720 {
1721 tmp->x = op->x + freearr_x[j], tmp->y = op->y + freearr_y[j];
1722 insert_ob_in_map (tmp, op->map, NULL, 0);
1723 }
1724 }
1725
1726 op->destroy ();
1727 }
1728 else if (type & AT_DRAIN && hitter->type == GRIMREAPER && hitter->value++ > 10)
1729 hitter->drop_and_destroy ();
1730
1731 return maxdam;
1732 }
1733
1734 static void
1735 poison_player (object *op, object *hitter, int dam)
1736 {
1737 archetype *at = archetype::find (shstr_poisoning);
1738 object *tmp = present_arch_in_ob (at, op);
1739
1740 if (!tmp)
1741 {
1742 tmp = insert_ob_in_ob (at->instance (), op);
1743 /* peterm: give poisoning some teeth. It should
1744 * be able to kill things better than it does:
1745 * damage should be dependent something--I choose to
1746 * do this: if it's a monster, the damage from the
1747 * poisoning goes as the level of the monster/2.
1748 * If anything else, goes as damage.
1749 */
1750
1751 if (hitter->flag [FLAG_ALIVE])
1752 tmp->stats.dam += hitter->level / 2;
1753 else
1754 tmp->stats.dam = dam;
1755
1756 tmp->set_owner (hitter); /* so we get credit for poisoning kills */
1757 if (hitter->skill && hitter->skill != tmp->skill)
1758 tmp->skill = hitter->skill;
1759
1760 tmp->stats.food += dam; /* more damage, longer poisoning */
1761
1762 if (op->type == PLAYER)
1763 {
1764 /* player looses stats, maximum is -10 of each */
1765 tmp->stats.Con = max (-(dam / 4 + 1), -10);
1766 tmp->stats.Str = max (-(dam / 3 + 2), -10);
1767 tmp->stats.Dex = max (-(dam / 6 + 1), -10);
1768 tmp->stats.Int = max (-(dam / 7 ), -10);
1769 tmp->set_flag (FLAG_APPLIED);
1770 op->update_stats ();
1771 new_draw_info (NDI_UNIQUE, 0, op, "You suddenly feel very ill.");
1772 op->play_sound (tmp->sound);
1773 }
1774
1775 if (hitter->type == PLAYER)
1776 new_draw_info_format (NDI_UNIQUE, 0, hitter, "You poison %s.", &op->name);
1777 else if (hitter->owner != NULL && hitter->owner->type == PLAYER)
1778 new_draw_info_format (NDI_UNIQUE, 0, hitter->owner, "Your %s poisons %s.", &hitter->name, &op->name);
1779
1780 tmp->speed_left = 0;
1781 }
1782 else
1783 tmp->stats.food++;
1784 }
1785
1786 static void
1787 slow_player (object *op, object *hitter, int dam)
1788 {
1789 archetype *at = archetype::find (shstr_slowness);
1790 object *tmp;
1791
1792 if (!at)
1793 LOG (llevError, "Can't find slowness archetype.\n");
1794
1795 if ((tmp = present_arch_in_ob (at, op)) == NULL)
1796 {
1797 tmp = at->instance ();
1798 tmp = insert_ob_in_ob (tmp, op);
1799 new_draw_info (NDI_UNIQUE, 0, op, "The world suddenly moves very fast!");
1800 }
1801 else
1802 tmp->stats.food++;
1803
1804 tmp->set_flag (FLAG_APPLIED);
1805 tmp->speed_left = 0;
1806 op->update_stats ();
1807 }
1808
1809 void
1810 confuse_player (object *op, object *hitter, int dam)
1811 {
1812 object *tmp;
1813 int maxduration;
1814
1815 tmp = present_in_ob_by_name (FORCE, shstr_confusion, op);
1816 if (!tmp)
1817 {
1818 tmp = archetype::get (FORCE_NAME);
1819 tmp = insert_ob_in_ob (tmp, op);
1820 }
1821
1822 /* Duration added per hit and max. duration of confusion both depend
1823 * on the player's resistance
1824 */
1825 tmp->set_speed (0.05);
1826 tmp->subtype = FORCE_CONFUSION;
1827 tmp->duration = 8 + max (1, 5 * (100 - op->resist[ATNR_CONFUSION]) / 100);
1828 tmp->name = shstr_confusion;
1829 maxduration = max (2, 30 * (100 - op->resist[ATNR_CONFUSION]) / 100);
1830
1831 if (tmp->duration > maxduration)
1832 tmp->duration = maxduration;
1833
1834 if (op->type == PLAYER && !op->flag [FLAG_CONFUSED])
1835 new_draw_info (NDI_UNIQUE, 0, op, "You suddenly feel very confused!");
1836
1837 op->set_flag (FLAG_CONFUSED);
1838 }
1839
1840 void
1841 blind_player (object *op, object *hitter, int dam)
1842 {
1843 /* Save some work if we know it isn't going to affect the player */
1844 if (op->resist[ATNR_BLIND] == 100)
1845 return;
1846
1847 object *tmp = present_in_ob (BLINDNESS, op);
1848 if (!tmp)
1849 {
1850 tmp = archetype::get (shstr_blindness);
1851 tmp->set_flag (FLAG_BLIND);
1852 tmp->set_flag (FLAG_APPLIED);
1853 // use floats so we don't lose too much precision due to rounding errors.
1854 tmp->set_speed (lerp<float> (op->resist [ATNR_BLIND], 0, 100, tmp->speed, MIN_ACTIVE_SPEED));
1855
1856 tmp = insert_ob_in_ob (tmp, op);
1857 change_abil (op, tmp); /* Mostly to display any messages */
1858 op->update_stats (); /* This takes care of some other stuff */
1859
1860 new_draw_info_format (NDI_UNIQUE, 0, hitter->outer_owner (), "Your attack blinds %s!", query_name (op));
1861 }
1862
1863 tmp->stats.food += dam;
1864 min_it (tmp->stats.food, 10);
1865 }
1866
1867 void
1868 paralyze_player (object *op, object *hitter, int dam)
1869 {
1870 /* This is strange stuff... someone knows for what this is
1871 * written? Well, i think this can and should be removed
1872 */
1873
1874 /*
1875 if((tmp=present(PARAIMAGE,op->map,op->x,op->y))==NULL) {
1876 tmp=clone_arch(PARAIMAGE);
1877 tmp->x=op->x,tmp->y=op->y;
1878 insert_ob_in_map(tmp,op->map,tmp,INS_NO_MERGE | INS_NO_WALK_ON);
1879 }
1880 */
1881
1882 /* Do this as a float - otherwise, rounding might very well reduce this to 0 */
1883 float effect = dam * 3.f * (100.f - op->resist[ATNR_PARALYZE]) / 100.f;
1884
1885 op->speed_left -= op->speed * effect;
1886 /* tmp->stats.food+=(signed short) effect/op->speed; */
1887
1888 /* max number of ticks to be affected for. */
1889 float max = (100 - op->resist[ATNR_PARALYZE]) / 2;
1890
1891 max_it (op->speed_left, -op->speed * max);
1892
1893 /* tmp->stats.food = (signed short) (max / op->speed); */
1894 }
1895
1896 /* Attempts to kill 'op'. hitter is the attack object, dam is
1897 * the computed damaged.
1898 */
1899 static void
1900 deathstrike_player (object *op, object *hitter, int *dam)
1901 {
1902 /* The intention of a death attack is to kill outright things
1903 ** that are a lot weaker than the attacker, have a chance of killing
1904 ** things somewhat weaker than the caster, and no chance of
1905 ** killing something equal or stronger than the attacker.
1906 ** Also, if a deathstrike attack has a slaying, any monster
1907 ** whose name or race matches a comma-delimited list in the slaying
1908 ** field of the deathstriking object */
1909
1910 int atk_lev, def_lev, kill_lev;
1911
1912 if (hitter->slaying)
1913 if (!((op->flag [FLAG_UNDEAD] && hitter->slaying.contains (shstr_undead))
1914 || (op->race && hitter->slaying.contains (op->race))))
1915 return;
1916
1917 def_lev = op->level;
1918 if (def_lev < 1)
1919 {
1920 LOG (llevError, "BUG: arch %s with level < 1 (%s)\n", &op->arch->archname, op->debug_desc ());
1921 def_lev = 1;
1922 }
1923
1924 atk_lev = (hitter->chosen_skill ? hitter->chosen_skill->level : hitter->level) / 2;
1925 /* LOG(llevDebug,"Deathstrike - attack level %d, defender level %d\n",
1926 atk_lev, def_lev); */
1927
1928 if (atk_lev >= def_lev)
1929 {
1930 kill_lev = random_roll (0, atk_lev - 1, hitter, PREFER_HIGH);
1931
1932 /* Note that the below effectively means the ratio of the atk vs
1933 * defener level is important - if level 52 character has very little
1934 * chance of killing a level 50 monster. This should probably be
1935 * redone.
1936 */
1937 if (kill_lev >= def_lev)
1938 {
1939 *dam = op->stats.hp + 10; /* take all hp. they can still save for 1/2 */
1940 /* I think this doesn't really do much. Because of
1941 * integer rounding, this only makes any difference if the
1942 * attack level is double the defender level.
1943 */
1944 *dam *= kill_lev / def_lev;
1945 }
1946 }
1947 else
1948 *dam = 0; /* no harm done */
1949 }
1950
1951 /* This returns the amount of damage hitter does to op with the
1952 * appropriate attacktype. Only 1 attacktype should be set at a time.
1953 * This doesn't damage the player, but returns how much it should
1954 * take. However, it will do other effects (paralyzation, slow, etc.)
1955 * Note - changed for PR code - we now pass the attack number and not
1956 * the attacktype. Makes it easier for the PR code. */
1957 int
1958 hit_player_attacktype (object *op, object *hitter, int dam, uint32 attacknum, int magic)
1959 {
1960 int doesnt_slay = 1;
1961
1962 /* Catch anyone that may be trying to send us a bitmask instead of the number */
1963 if (attacknum >= NROFATTACKS)
1964 {
1965 LOG (llevError, "hit_player_attacktype: Invalid attacknumber passed: %u\n", attacknum);
1966 return 0;
1967 }
1968
1969 if (dam < 0)
1970 {
1971 LOG (llevError, "hit_player_attacktype called with negative damage %d (hitter %s, target %s)\n",
1972 dam, hitter->debug_desc (), op->debug_desc ());
1973 return 0;
1974 }
1975
1976 /* AT_INTERNAL is supposed to do exactly dam. Put a case here so
1977 * people can't mess with that or it otherwise get confused. */
1978 if (attacknum == ATNR_INTERNAL)
1979 return dam;
1980
1981 if (hitter->slaying)
1982 {
1983 if ((op->race && hitter->slaying.contains (op->race))
1984 || (op->arch && op->arch->archname.contains (hitter->slaying)))
1985 {
1986 doesnt_slay = 0;
1987 dam *= 3;
1988 }
1989 }
1990
1991 /* Adjust the damage for resistance. Note that neg. values increase damage. */
1992 if (op->resist[attacknum])
1993 {
1994 /* basically: dam = dam*(100-op->resist[attacknum])/100;
1995 * in case 0>dam>1, we try to "simulate" a float value-effect */
1996 dam *= (100 - op->resist[attacknum]);
1997 if (dam >= 100)
1998 dam /= 100;
1999 else
2000 dam = (dam > (random_roll (0, 99, op, PREFER_LOW))) ? 1 : 0;
2001 }
2002
2003 /* Special hack. By default, if immune to something, you
2004 * shouldn't need to worry. However, acid is an exception, since
2005 * it can still damage your items. Only include attacktypes if
2006 * special processing is needed */
2007
2008 if (op->resist[attacknum] >= 100
2009 && doesnt_slay
2010 && attacknum != ATNR_ACID)
2011 return 0;
2012
2013 /* Keep this in order - makes things easier to find */
2014
2015 switch (attacknum)
2016 {
2017 case ATNR_PHYSICAL:
2018 /* here also check for diseases */
2019 check_physically_infect (op, hitter);
2020 break;
2021
2022 /* Don't need to do anything for:
2023 magic,
2024 fire,
2025 electricity,
2026 cold */
2027
2028 case ATNR_CONFUSION:
2029 case ATNR_POISON:
2030 case ATNR_SLOW:
2031 case ATNR_PARALYZE:
2032 case ATNR_FEAR:
2033 case ATNR_CANCELLATION:
2034 case ATNR_DEPLETE:
2035 case ATNR_BLIND:
2036 {
2037 /* chance for inflicting a special attack depends on the
2038 * difference between attacker's and defender's level
2039 */
2040 int level_diff = min (110, max (0, op->level - hitter->level));
2041
2042 /* First, only creatures/players with speed can be affected.
2043 * Second, just getting hit doesn't mean it always affects
2044 * you. Third, you still get a saving through against the
2045 * effect.
2046 */
2047 if (op->has_active_speed ()
2048 && (op->flag [FLAG_MONSTER] || op->type == PLAYER)
2049 && !(rndm (0, (attacknum == ATNR_SLOW ? 6 : 3) - 1))
2050 && !did_make_save (op, level_diff, op->resist[attacknum] / 10))
2051 {
2052
2053 /* Player has been hit by something */
2054 if (attacknum == ATNR_CONFUSION)
2055 confuse_player (op, hitter, dam);
2056 else if (attacknum == ATNR_POISON)
2057 poison_player (op, hitter, dam);
2058 else if (attacknum == ATNR_SLOW)
2059 slow_player (op, hitter, dam);
2060 else if (attacknum == ATNR_PARALYZE)
2061 paralyze_player (op, hitter, dam);
2062 else if (attacknum == ATNR_FEAR)
2063 scare_creature (op, hitter);
2064 else if (attacknum == ATNR_CANCELLATION)
2065 cancellation (op);
2066 else if (attacknum == ATNR_DEPLETE)
2067 op->drain_stat ();
2068 else if (attacknum == ATNR_BLIND && !op->flag [FLAG_UNDEAD] && !op->flag [FLAG_GENERATOR])
2069 blind_player (op, hitter, dam);
2070 }
2071
2072 dam = 0; /* These are all effects and don't do real damage */
2073 }
2074 break;
2075
2076 case ATNR_ACID:
2077 {
2078 int flag = 0;
2079
2080 /* Items only get corroded if you're not on a battleground and
2081 * if your acid resistance is below 50%. */
2082 if (!op_on_battleground (op, NULL, NULL) && (op->resist[ATNR_ACID] < 50))
2083 {
2084 for (object *tmp = op->inv; tmp; tmp = tmp->below)
2085 {
2086 if (tmp->invisible)
2087 continue;
2088 if (!tmp->flag [FLAG_APPLIED] || (tmp->resist[ATNR_ACID] >= 10))
2089 /* >= 10% acid res. on items will protect these */
2090 continue;
2091 if (!(tmp->materials & M_IRON))
2092 continue;
2093 if (tmp->magic < -4) /* Let's stop at -5 */
2094 continue;
2095 if (tmp->type == RING
2096 /* removed boots and gloves from exclusion list in PR */
2097 || tmp->type == GIRDLE
2098 || tmp->type == AMULET
2099 || tmp->type == WAND
2100 || tmp->type == ROD
2101 || tmp->type == HORN)
2102 continue; /* To avoid some strange effects */
2103
2104 /* High damage acid has better chance of corroding
2105 objects */
2106 if (rndm (0, dam + 4) > random_roll (0, 39, op, PREFER_HIGH) + 2 * tmp->magic)
2107 {
2108 flag = 1;
2109 tmp->magic--;
2110
2111 if (object *pl = tmp->visible_to ())
2112 {
2113 /* Make this more visible */
2114 new_draw_info_format (NDI_UNIQUE | NDI_RED, 0, pl,
2115 "The %s's acid corrodes your %s!", query_name (hitter), query_name (tmp));
2116
2117 esrv_send_item (op, tmp); //TODO: UPD_NAME should be enough (it's enough in other cases)
2118 }
2119 }
2120 }
2121
2122 if (flag)
2123 op->update_stats (); /* Something was corroded */
2124 }
2125 }
2126 break;
2127
2128 case ATNR_DRAIN:
2129 {
2130 /* rate is the proportion of exp drained. High rate means
2131 * not much is drained, low rate means a lot is drained.
2132 */
2133 int rate;
2134
2135 if (op->resist[ATNR_DRAIN] >= 0)
2136 rate = 400 + op->resist[ATNR_DRAIN] * 3;
2137 else
2138 rate = 400 * 100 / (100 - op->resist[ATNR_DRAIN]);
2139
2140 if (op->stats.exp <= rate)
2141 {
2142 if (op->type == GOLEM)
2143 dam = 9998; /* Its force is "sucked" away. 8) */
2144 else
2145 /* If we can't drain, lets try to do physical damage */
2146 dam = hit_player_attacktype (op, hitter, dam, ATNR_PHYSICAL, magic);
2147 }
2148 else
2149 {
2150 /* Randomly give the hitter some hp */
2151 if (hitter->stats.hp < hitter->stats.maxhp &&
2152 (op->level > hitter->level) && random_roll (0, (op->level - hitter->level + 2), hitter, PREFER_HIGH) > 3)
2153 hitter->stats.hp++;
2154
2155 /* Can't do drains on battleground spaces.
2156 * Move the wiz check up here - before, the hitter wouldn't gain exp
2157 * exp, but the wiz would still lose exp! If drainee is a wiz,
2158 * nothing happens.
2159 * Try to credit the owner. We try to display player -> player drain
2160 * attacks, hence all the != PLAYER checks.
2161 */
2162 if (!op_on_battleground (hitter, NULL, NULL) && !op->flag [FLAG_WIZ])
2163 {
2164 object *owner = hitter->owner;
2165
2166 if (owner && owner != hitter)
2167 {
2168 if (op->type != PLAYER || owner->type != PLAYER)
2169 change_exp (owner, op->stats.exp / (rate * 2),
2170 hitter->chosen_skill ? hitter->chosen_skill->skill : shstr (), SK_EXP_TOTAL);
2171 }
2172 else if (op->type != PLAYER || hitter->type != PLAYER)
2173 change_exp (hitter, op->stats.exp / (rate * 2),
2174 hitter->chosen_skill ? hitter->chosen_skill->skill : shstr (), 0);
2175
2176 change_exp (op, -op->stats.exp / rate, shstr (), 0);
2177 }
2178
2179 dam = 1; /* Drain is an effect. Still return 1 - otherwise, if you have pure
2180 * drain attack, you won't know that you are actually sucking out EXP,
2181 * as the messages will say you missed
2182 */
2183 }
2184 }
2185 break;
2186
2187 case ATNR_TURN_UNDEAD:
2188 {
2189 if (op->flag [FLAG_UNDEAD])
2190 {
2191 object *owner = hitter->owner ? (object *)hitter->owner : hitter;
2192 object *god = find_god (determine_god (owner));
2193 int div = 1;
2194
2195 /* if undead are not an enemy of your god, you turn them
2196 * at half strength */
2197 if (!god || !god->slaying.contains (shstr_undead))
2198 div = 2;
2199
2200 /* Give a bonus if you resist turn undead */
2201 if (op->level * div < (turn_bonus[owner->stats.Wis] + owner->level + (op->resist[ATNR_TURN_UNDEAD] / 100)))
2202 scare_creature (op, owner);
2203 }
2204 else
2205 dam = 0; /* don't damage non undead - should we damage
2206 undead? */
2207 }
2208 break;
2209
2210 case ATNR_DEATH:
2211 deathstrike_player (op, hitter, &dam);
2212 break;
2213
2214 case ATNR_CHAOS:
2215 LOG (llevError, "%s was hit by %s with non-specific chaos.\n", op->debug_desc (), hitter->debug_desc ());
2216 dam = 0;
2217 break;
2218
2219 case ATNR_COUNTERSPELL:
2220 LOG (llevError, "%s was hit by %s with counterspell attack.\n", op->debug_desc (), hitter->debug_desc ());
2221 dam = 0;
2222 /* This should never happen. Counterspell is handled
2223 * seperately and filtered out. If this does happen,
2224 * Counterspell has no effect on anything but spells, so it
2225 * does no damage. */
2226 break;
2227
2228 case ATNR_HOLYWORD:
2229 {
2230 /* This has already been handled by hit_player,
2231 * no need to check twice -- DAMN */
2232 object *owner = hitter->owner ? (object *)hitter->owner : hitter;
2233
2234 /* As with turn undead above, give a bonus on the saving throw */
2235 if ((op->level + (op->resist[ATNR_HOLYWORD] / 100)) < owner->level + turn_bonus[owner->stats.Wis])
2236 scare_creature (op, owner);
2237 }
2238 break;
2239
2240 case ATNR_LIFE_STEALING:
2241 {
2242 int new_hp;
2243
2244 /* this is replacement to drain for players, instead of taking
2245 * exp it takes hp. It is geared for players, probably not
2246 * much use giving it to monsters
2247 *
2248 * life stealing doesn't do a lot of damage, but it gives the
2249 * damage it does do to the player. Given that,
2250 * it only does 1/10'th normal damage (hence the divide by
2251 * 1000).
2252 */
2253 /* You can't steal life from something undead */
2254 if ((op->type == GOLEM) || (op->flag [FLAG_UNDEAD]))
2255 return 0;
2256
2257 /* If drain protection is higher than life stealing, use that */
2258 if (op->resist[ATNR_DRAIN] >= op->resist[ATNR_LIFE_STEALING])
2259 dam = (dam * (100 - op->resist[ATNR_DRAIN])) / 3000;
2260 else
2261 dam = (dam * (100 - op->resist[ATNR_LIFE_STEALING])) / 3000;
2262
2263 /* You die at -1 hp, not zero. */
2264 if (dam > (op->stats.hp + 1))
2265 dam = op->stats.hp + 1;
2266
2267 new_hp = hitter->stats.hp + dam;
2268 if (new_hp > hitter->stats.maxhp)
2269 new_hp = hitter->stats.maxhp;
2270
2271 if (new_hp > hitter->stats.hp)
2272 hitter->stats.hp = new_hp;
2273 }
2274 }
2275
2276 return dam;
2277 }
2278