ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/attack.C
Revision: 1.155
Committed: Sat Oct 8 21:54:05 2022 UTC (19 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.154: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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