Debian Codegraph @

server/attack.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * This handles all attacks, magical or not.
17 * @todo
18 * clean functions. Are all parameters required? Seems quite a mess to send damage/wc etc in attack_ob_simple().
19 */
20
21 #include "global.h"
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "living.h"
28 #include "material.h"
29 #include "skills.h"
30 #include "sounds.h"
31 #include "sproto.h"
32
33 /*#define ATTACK_DEBUG*/
34
35 static void slow_living(object *op, object *hitter, int dam);
36 static void deathstrike_living(object *op, object *hitter, int *dam);
37 static int adj_attackroll(object *hitter, object *target);
38 static int is_aimed_missile(object *op);
39 static int did_make_save_item(object *op, int type, object *originator);
40 static void poison_living(object *op, object *hitter, int dam);
41
42 /**
43 * Cancels object *op. Cancellation basically means an object loses
44 * its magical benefits.
45 *
46 * @param op
47 * item to cancel. Its inventory will also be cancelled.
48 */
49 static void cancellation(object *op) {
50 if (op->invisible)
51 return;
52
53 if (QUERY_FLAG(op, FLAG_ALIVE) || op->type == CONTAINER || op->type == THROWN_OBJ) {
54 /* Recur through the inventory */
55 FOR_INV_PREPARE(op, inv)
56 if (!did_make_save_item(inv, AT_CANCELLATION, op))
57 cancellation(inv);
58 FOR_INV_FINISH();
59 } else if (FABS(op->magic) <= rndm(0, 5)) {
60 /* Nullify this object. This code could probably be more complete */
61 /* in what abilities it should cancel */
62 op->magic = 0;
63 CLEAR_FLAG(op, FLAG_DAMNED);
64 CLEAR_FLAG(op, FLAG_CURSED);
65 CLEAR_FLAG(op, FLAG_KNOWN_MAGICAL);
66 CLEAR_FLAG(op, FLAG_KNOWN_CURSED);
67 if (op->env && op->env->type == PLAYER) {
68 esrv_update_item(UPD_FLAGS, op->env, op);
69 }
70 }
71 }
72
73 static void object_get_materialtype(object* op, materialtype_t** mt) {
74 if (op->materialname == NULL) {
75 for (*mt = materialt; *mt != NULL && (*mt)->next != NULL; *mt = (*mt)->next) {
76 if (op->material & (*mt)->material)
77 break;
78 }
79 } else
80 *mt = name_to_material(op->materialname);
81 }
82
83 /**
84 * Checks to make sure the item actually
85 * made its saving throw based on the tables. It does not take
86 * any further action (like destroying the item).
87 * @param op
88 * object to check.
89 * @param type
90 * attack type.
91 * @param originator
92 * what it attacking?
93 * @return
94 * TRUE if item saved, FALSE else.
95 * @todo
96 * check meaning of originator.
97 */
98 static int did_make_save_item(object *op, int type, object *originator) {
99 int i, roll, saves = 0, attacks = 0, number;
100 materialtype_t* mt;
101 object_get_materialtype(op, &mt);
102 if (mt == NULL)
103 return TRUE;
104 roll = rndm(1, 20);
105
106 /* the attacktypes have no meaning for object saves
107 * If the type is only magic, don't adjust type - basically, if
108 * pure magic is hitting an object, it should save. However, if it
109 * is magic teamed with something else, then strip out the
110 * magic type, and instead let the fire, cold, or whatever component
111 * destroy the item. Otherwise, you get the case of poisoncloud
112 * destroying objects because it has magic attacktype.
113 */
114 if (type != AT_MAGIC)
115 type &= ~(AT_CONFUSION|AT_DRAIN|AT_GHOSTHIT|AT_POISON|AT_SLOW|
116 AT_PARALYZE|AT_TURN_UNDEAD|AT_FEAR|AT_DEPLETE|AT_DEATH|
117 AT_COUNTERSPELL|AT_HOLYWORD|AT_BLIND|AT_LIFE_STEALING|
118 AT_MAGIC);
119
120 if (type == 0)
121 return TRUE;
122 if (roll == 20)
123 return TRUE;
124
125 for (number = 0; number < NROFATTACKS; number++) {
126 i = 1<<number;
127 if (!(i&type))
128 continue;
129 attacks++;
130 if (op->resist[number] == 100)
131 saves++;
132 else if (roll >= mt->save[number]-op->magic-op->resist[number]/100)
133 saves++;
134 else if ((20-mt->save[number])/3 > originator->stats.dam)
135 saves++;
136 }
137
138 if (saves == attacks || attacks == 0)
139 return TRUE;
140 if (saves == 0 || rndm(1, attacks) > saves)
141 return FALSE;
142 return TRUE;
143 }
144
145 static void put_in_icecube(object* op, object* originator) {
146 archetype* at = find_archetype("icecube");
147 if (at == NULL)
148 return;
149 op = stop_item(op);
150 if (op == NULL)
151 return;
152
153 // Put in existing icecube if one exists, otherwise make one
154 object* tmp = map_find_by_archetype(op->map, op->x, op->y, at);
155 if (tmp == NULL) {
156 tmp = arch_to_object(at);
157 /* This was in the old (pre new movement code) -
158 * icecubes have slow_move set to 1 - don't want
159 * that for ones we create.
160 */
161 tmp->move_slow_penalty = 0;
162 tmp->move_slow = 0;
163 object_insert_in_map_at(tmp, op->map, originator, 0, op->x, op->y);
164 }
165 if (!QUERY_FLAG(op, FLAG_REMOVED))
166 object_remove(op);
167 (void)object_insert_in_ob(op, tmp);
168 }
169
170 /**
171 * Object is attacked with some attacktype (fire, ice, ...).
172 * Calls did_make_save_item(). It then performs the
173 * appropriate actions to the item (such as burning the item up,
174 * calling cancellation(), etc.)
175 * @param op
176 * victim of the attack.
177 * @param type
178 * attacktype.
179 * @param originator
180 * what is attacking.
181 */
182 void save_throw_object(object *op, uint32_t type, object *originator) {
183 int dam;
184
185 if (!did_make_save_item(op, type, originator)) {
186 object *env = op->env;
187 int x = op->x, y = op->y;
188 mapstruct *m = op->map;
189 /* For use with burning off equipped items */
190 int weight = op->weight;
191
192 op = stop_item(op);
193 if (op == NULL)
194 return;
195
196 /*
197 * If this object is a transport and has players in it, make them disembark.
198 */
199 if (op->type == TRANSPORT && op->inv) {
200 if (op->map == NULL) {
201 LOG(llevError, "Transport %s not on a map but with an item %s in it?\n", op->name, op->inv->name);
202 } else {
203 char name[MAX_BUF];
204 query_name(op, name, sizeof(name));
205 FOR_INV_PREPARE(op, inv) {
206 if (inv->contr) {
207 draw_ext_info_format(NDI_UNIQUE, 0, inv, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
208 "You are expelled from the %s during its destruction.",
209 name);
210 inv->contr->transport = NULL;
211 }
212 }
213 FOR_INV_FINISH();
214 }
215 }
216
217
218 /* Set off runes in the inventory of the object being destroyed. */
219 FOR_INV_PREPARE(op, inv)
220 if (inv->type == RUNE)
221 spring_trap(inv, originator);
222 FOR_INV_FINISH();
223
224 /* Hacked the following so that type LIGHTER will work.
225 * Also, objects which are potenital "lights" that are hit by
226 * flame/elect attacks will be set to glow. "lights" are any
227 * object with +/- glow_radius and an "other_arch" to change to.
228 * (and please note that we cant fail our save and reach this
229 * function if the object doesnt contain a material that can burn.
230 * So forget lighting magical swords on fire with this!) -b.t.
231 */
232 if (type&(AT_FIRE|AT_ELECTRICITY)
233 && op->other_arch
234 && QUERY_FLAG(op, FLAG_IS_LIGHTABLE)) {
235 const char *arch = op->other_arch->name;
236
237 op = object_decrease_nrof(op, 1);
238 if (op)
239 fix_stopped_item(op, m, originator);
240 op = create_archetype(arch);
241 if (op != NULL) {
242 if (env) {
243 op->x = env->x,
244 op->y = env->y;
245 object_insert_in_ob(op, env);
246 } else
247 object_insert_in_map_at(op, m, originator, 0, x, y);
248 }
249 return;
250 }
251 if (type&AT_CANCELLATION) { /* Cancellation. */
252 cancellation(op);
253 fix_stopped_item(op, m, originator);
254 return;
255 }
256 if (op->nrof > 1) {
257 op = object_decrease_nrof(op, rndm(0, op->nrof-1));
258 if (op)
259 fix_stopped_item(op, m, originator);
260 } else {
261 if (!QUERY_FLAG(op, FLAG_REMOVED))
262 object_remove(op);
263 object_free_drop_inventory(op);
264 }
265 if (type&(AT_FIRE|AT_ELECTRICITY)) {
266 if (env) {
267 /* Check to see if the item being burnt is being worn */
268 if (QUERY_FLAG(op, FLAG_APPLIED)) {
269 /* if the object is applied, it should be safe to assume env is a player or creature. */
270 if (env->resist[ATNR_FIRE] < 100)
271 /* Should the message type be something different? */
272 draw_ext_info_format(NDI_RED, 0, env, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
273 "OUCH! It burns!");
274 else
275 draw_ext_info_format(NDI_UNIQUE, 0, env, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
276 "Despite the flame, you feel nothing.");
277 /* burning off an item causes 1 point of fire damage for every kilogram of mass the item has */
278 dam = weight / 1000 * (100 - env->resist[ATNR_FIRE]) / 100;
279 /* Double the damage on cursed items */
280 if (QUERY_FLAG(op, FLAG_CURSED))
281 dam *= 2;
282 /* Triple the damage on damned items. A cursed and damned item would thus inflict 6x damage. */
283 if (QUERY_FLAG(op, FLAG_DAMNED))
284 dam *= 3;
285 env->stats.hp -= dam;
286 /* You die at -1, not 0 */
287 if (env->stats.hp < 0)
288 kill_player(env, NULL);
289 }
290 op = create_archetype("burnout");
291 op->x = env->x,
292 op->y = env->y;
293 object_insert_in_ob(op, env);
294 } else {
295 object_replace_insert_in_map("burnout", originator);
296 }
297 }
298 return;
299 }
300 /* The value of 50 is arbitrary. */
301 if (type & AT_COLD && (op->resist[ATNR_COLD] < 50) &&
302 !QUERY_FLAG(op, FLAG_NO_PICK) && (RANDOM() & 2)) {
303 put_in_icecube(op, originator);
304 }
305 }
306
307 /**
308 * Attack a spot on the map.
309 *
310 * @param op
311 * object hitting the map.
312 * @param dir
313 * direction op is hitting/going.
314 * @param type
315 * attacktype.
316 * @param full_hit
317 * if set then monster area does not matter, it gets all damage. Else damage is proportional to
318 * affected area vs full monster area.
319 * @return
320 * 1 if it hits something, 0 otherwise.
321 */
322 int hit_map(object *op, int dir, uint32_t type, int full_hit) {
323 mapstruct *map;
324 int16_t x, y;
325 int retflag = 0; /* added this flag.. will return 1 if it hits a monster */
326 tag_t op_tag;
327
328 if (QUERY_FLAG(op, FLAG_FREED)) {
329 LOG(llevError, "BUG: hit_map(): free object\n");
330 return 0;
331 }
332
333 if (QUERY_FLAG(op, FLAG_REMOVED) || op->env != NULL) {
334 LOG(llevError, "BUG: hit_map(): hitter (arch %s, name %s) not on a map\n", op->arch->name, op->name);
335 return 0;
336 }
337
338 if (!op->map) {
339 LOG(llevError, "BUG: hit_map(): %s has no map\n", op->name);
340 return 0;
341 }
342
343 op = HEAD(op);
344 op_tag = op->count;
345
346 map = op->map;
347 x = op->x+freearr_x[dir];
348 y = op->y+freearr_y[dir];
349 if (get_map_flags(map, &map, x, y, &x, &y)&P_OUT_OF_MAP)
350 return 0;
351
352 /* peterm: a few special cases for special attacktypes --counterspell
353 * must be out here because it strikes things which are not alive
354 */
355
356 if (type&AT_COUNTERSPELL) {
357 counterspell(op, dir); /* see spell_effect.c */
358
359 /* If the only attacktype is counterspell or magic, don't need
360 * to do any further processing.
361 */
362 if (!(type&~(AT_COUNTERSPELL|AT_MAGIC))) {
363 return 0;
364 }
365 type &= ~AT_COUNTERSPELL;
366 }
367
368 if (type&AT_CHAOS) {
369 shuffle_attack(op, 1); /*1 flag tells it to change the face */
370 object_update(op, UP_OBJ_FACE);
371 type &= ~AT_CHAOS;
372 }
373
374 FOR_MAP_PREPARE(map, x, y, tmp) {
375 if (QUERY_FLAG(tmp, FLAG_FREED)) {
376 LOG(llevError, "BUG: hit_map(): found freed object\n");
377 break;
378 }
379
380 /* Something could have happened to 'tmp' while 'tmp->below' was processed.
381 * For example, 'tmp' was put in an icecube.
382 * This is one of the few cases where on_same_map should not be used.
383 */
384 if (tmp->map != map || tmp->x != x || tmp->y != y)
385 continue;
386
387 tmp = HEAD(tmp);
388
389 /* Need to hit everyone in the transport with this spell */
390 if (tmp->type == TRANSPORT) {
391 FOR_INV_PREPARE(tmp, pl)
392 if (pl->type == PLAYER)
393 hit_player(pl, op->stats.dam, op, type, full_hit);
394 FOR_INV_FINISH();
395 }
396
397 if (QUERY_FLAG(tmp, FLAG_ALIVE)) {
398 hit_player(tmp, op->stats.dam, op, type, full_hit);
399 retflag |= 1;
400 if (object_was_destroyed(op, op_tag))
401 break;
402 }
403 /* Here we are potentially destroying an object. If the object has
404 * NO_PASS set, it is also immune - you can't destroy walls. Note
405 * that weak walls have is_alive set, which prevent objects from
406 * passing over/through them. We don't care what type of movement
407 * the wall blocks - if it blocks any type of movement, can't be
408 * destroyed right now.
409 */
410 else if ((tmp->material || tmp->materialname) && op->stats.dam > 0 && !tmp->move_block) {
411 save_throw_object(tmp, type, op);
412 if (object_was_destroyed(op, op_tag))
413 break;
414 }
415 } FOR_MAP_FINISH();
416 return 0;
417 }
418
419 /**
420 * Send an attack message to someone.
421 *
422 * @param dam
423 * amount of damage done.
424 * @param type
425 * attack type.
426 * @param op
427 * victim of the attack.
428 * @param hitter
429 * who is hitting.
430 * @todo
431 * move check for player at function start? this function seems called for everyone.
432 * use string safe functions.
433 */
434 static void attack_message(int dam, int type, object *op, object *hitter) {
435 char buf[MAX_BUF], buf1[MAX_BUF], buf2[MAX_BUF];
436 int i, found = 0;
437 mapstruct *map;
438 object *owner;
439
440 /* put in a few special messages for some of the common attacktypes
441 * a player might have. For example, fire, electric, cold, etc
442 * [garbled 20010919]
443 */
444
445 if (dam == 9998 && op->type == DOOR) {
446 snprintf(buf1, sizeof(buf1), "unlock %s", op->name);
447 snprintf(buf2, sizeof(buf2), " unlocks");
448 found++;
449 }
450 if (dam < 0) {
451 snprintf(buf1, sizeof(buf1), "hit %s", op->name);
452 snprintf(buf2, sizeof(buf2), " hits");
453 found++;
454 } else if (dam == 0) {
455 snprintf(buf1, sizeof(buf1), "missed %s", op->name);
456 snprintf(buf2, sizeof(buf2), " misses");
457 found++;
458 } else if ((hitter->type == DISEASE
459 || hitter->type == SYMPTOM
460 || hitter->type == POISONING
461 || (type&AT_POISON && IS_LIVE(op))) && !found) {
462 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_SUFFER][i].level != -1; i++)
463 if (dam < attack_mess[ATM_SUFFER][i].level
464 || attack_mess[ATM_SUFFER][i+1].level == -1) {
465 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_SUFFER][i].buf1, op->name, attack_mess[ATM_SUFFER][i].buf2);
466 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_SUFFER][i].buf3);
467 found++;
468 break;
469 }
470 } else if (op->type == DOOR && !found) {
471 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_DOOR][i].level != -1; i++)
472 if (dam < attack_mess[ATM_DOOR][i].level
473 || attack_mess[ATM_DOOR][i+1].level == -1) {
474 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_DOOR][i].buf1, op->name, attack_mess[ATM_DOOR][i].buf2);
475 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_DOOR][i].buf3);
476 found++;
477 break;
478 }
479 } else if (hitter->type == PLAYER && IS_LIVE(op)) {
480 if (USING_SKILL(hitter, SK_KARATE)) {
481 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_KARATE][i].level != -1; i++)
482 if (dam < attack_mess[ATM_KARATE][i].level
483 || attack_mess[ATM_KARATE][i+1].level == -1) {
484 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_KARATE][i].buf1, op->name, attack_mess[ATM_KARATE][i].buf2);
485 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_KARATE][i].buf3);
486 found++;
487 break;
488 }
489 } else if (USING_SKILL(hitter, SK_CLAWING)) {
490 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_CLAW][i].level != -1; i++)
491 if (dam < attack_mess[ATM_CLAW][i].level
492 || attack_mess[ATM_CLAW][i+1].level == -1) {
493 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_CLAW][i].buf1, op->name, attack_mess[ATM_CLAW][i].buf2);
494 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_CLAW][i].buf3);
495 found++;
496 break;
497 }
498 } else if (USING_SKILL(hitter, SK_PUNCHING)) {
499 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_PUNCH][i].level != -1; i++)
500 if (dam < attack_mess[ATM_PUNCH][i].level
501 || attack_mess[ATM_PUNCH][i+1].level == -1) {
502 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_PUNCH][i].buf1, op->name, attack_mess[ATM_PUNCH][i].buf2);
503 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_PUNCH][i].buf3);
504 found++;
505 break;
506 }
507 } else if (USING_SKILL(hitter, SK_WRAITH_FEED)) {
508 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_WRAITH_FEED][i].level != -1; i++)
509 if (dam < attack_mess[ATM_WRAITH_FEED][i].level) {
510 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_WRAITH_FEED][i].buf1, op->name, attack_mess[ATM_WRAITH_FEED][i].buf2);
511 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_WRAITH_FEED][i].buf3);
512 found++;
513 break;
514 }
515 }
516 }
517 if (found) {
518 /* done */
519 } else if (IS_ARROW(hitter) && (type == AT_PHYSICAL || type == AT_MAGIC)) {
520 snprintf(buf1, sizeof(buf1), "hit"); /* just in case */
521 for (i = 0; i < MAXATTACKMESS; i++)
522 if (dam < attack_mess[ATM_ARROW][i].level
523 || attack_mess[ATM_ARROW][i+1].level == -1) {
524 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_ARROW][i].buf3);
525 found++;
526 break;
527 }
528 } else if (type&AT_DRAIN && IS_LIVE(op)) {
529 /* drain is first, because some items have multiple attypes */
530 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_DRAIN][i].level != -1; i++)
531 if (dam < attack_mess[ATM_DRAIN][i].level
532 || attack_mess[ATM_DRAIN][i+1].level == -1) {
533 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_DRAIN][i].buf1, op->name, attack_mess[ATM_DRAIN][i].buf2);
534 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_DRAIN][i].buf3);
535 found++;
536 break;
537 }
538 } else if (type&AT_ELECTRICITY && IS_LIVE(op)) {
539 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_ELEC][i].level != -1; i++)
540 if (dam < attack_mess[ATM_ELEC][i].level
541 || attack_mess[ATM_ELEC][i+1].level == -1) {
542 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_ELEC][i].buf1, op->name, attack_mess[ATM_ELEC][i].buf2);
543 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_ELEC][i].buf3);
544 found++;
545 break;
546 }
547 } else if (type&AT_COLD && IS_LIVE(op)) {
548 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_COLD][i].level != -1; i++)
549 if (dam < attack_mess[ATM_COLD][i].level
550 || attack_mess[ATM_COLD][i+1].level == -1) {
551 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_COLD][i].buf1, op->name, attack_mess[ATM_COLD][i].buf2);
552 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_COLD][i].buf3);
553 found++;
554 break;
555 }
556 } else if (type&AT_FIRE) {
557 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_FIRE][i].level != -1; i++)
558 if (dam < attack_mess[ATM_FIRE][i].level
559 || attack_mess[ATM_FIRE][i+1].level == -1) {
560 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_FIRE][i].buf1, op->name, attack_mess[ATM_FIRE][i].buf2);
561 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_FIRE][i].buf3);
562 found++;
563 break;
564 }
565 } else if (hitter->current_weapon != NULL) {
566 int mtype;
567
568 switch (hitter->current_weapon->weapontype) {
569 case WEAP_HIT: mtype = ATM_BASIC; break;
570 case WEAP_SLASH: mtype = ATM_SLASH; break;
571 case WEAP_PIERCE: mtype = ATM_PIERCE; break;
572 case WEAP_CLEAVE: mtype = ATM_CLEAVE; break;
573 case WEAP_SLICE: mtype = ATM_SLICE; break;
574 case WEAP_STAB: mtype = ATM_STAB; break;
575 case WEAP_WHIP: mtype = ATM_WHIP; break;
576 case WEAP_CRUSH: mtype = ATM_CRUSH; break;
577 case WEAP_BLUD: mtype = ATM_BLUD; break;
578 default: mtype = ATM_BASIC; break;
579 }
580 for (i = 0; i < MAXATTACKMESS && attack_mess[mtype][i].level != -1; i++)
581 if (dam < attack_mess[mtype][i].level
582 || attack_mess[mtype][i+1].level == -1) {
583 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[mtype][i].buf1, op->name, attack_mess[mtype][i].buf2);
584 snprintf(buf2, sizeof(buf2), "%s", attack_mess[mtype][i].buf3);
585 found++;
586 break;
587 }
588 } else {
589 for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_BASIC][i].level != -1; i++)
590 if (dam < attack_mess[ATM_BASIC][i].level
591 || attack_mess[ATM_BASIC][i+1].level == -1) {
592 snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_BASIC][i].buf1, op->name, attack_mess[ATM_BASIC][i].buf2);
593 snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_BASIC][i].buf3);
594 found++;
595 break;
596 }
597 }
598
599 if (!found) {
600 snprintf(buf1, sizeof(buf1), "hit");
601 snprintf(buf2, sizeof(buf2), "hits");
602 }
603
604 if (dam > 0) {
605 if (hitter->chosen_skill)
606 play_sound_map(SOUND_TYPE_HIT, hitter, 0, hitter->chosen_skill->name);
607 }
608
609 /* bail out if a monster is casting spells */
610 owner = object_get_owner(hitter);
611 if (hitter->type != PLAYER && (owner == NULL || owner->type != PLAYER))
612 return;
613
614 /* scale down magic considerably. */
615 if (type&AT_MAGIC && rndm(0, 5))
616 return;
617
618 /* Did a player hurt another player? Inform both! */
619 /* only show half the player->player combat messages */
620 if (op->type == PLAYER
621 && rndm(0, 1)
622 && ((owner != NULL ? owner : hitter)->type) == PLAYER) {
623 if (owner != NULL)
624 snprintf(buf, sizeof(buf), "%s's %s %s you.", owner->name, hitter->name, buf2);
625 else {
626 snprintf(buf, sizeof(buf), "%s%s you.", hitter->name, buf2);
627 }
628 draw_ext_info(NDI_BLACK, 0, op, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT, buf);
629 } /* end of player hitting player */
630
631 /* scale down these messages too */
632 /*if(hitter->type == PLAYER && rndm(0, 2) == 0) {*/
633 if (hitter->type == PLAYER) {
634 snprintf(buf, sizeof(buf), "You %s.", buf1);
635 draw_ext_info(NDI_BLACK, 0, hitter, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_HIT,
636 buf);
637 } else if (owner != NULL && owner->type == PLAYER) {
638 /* look for stacked spells and start reducing the message chances */
639 if (hitter->type == SPELL_EFFECT
640 && (hitter->subtype == SP_EXPLOSION || hitter->subtype == SP_BULLET || hitter->subtype == SP_CONE)) {
641 i = 4;
642 map = hitter->map;
643 if (OUT_OF_REAL_MAP(map, hitter->x, hitter->y))
644 return;
645 FOR_MAP_PREPARE(map, hitter->x, hitter->y, next)
646 if (next->type == SPELL_EFFECT
647 && (next->subtype == SP_EXPLOSION || next->subtype == SP_BULLET || next->subtype == SP_CONE)) {
648 i *= 3;
649 if (i > 10000)
650 /* no need to test more, and avoid overflows */
651 break;
652 }
653 FOR_MAP_FINISH();
654 if (i < 0)
655 return;
656 if (rndm(0, i) != 0)
657 return;
658 } else if (rndm(0, 5) != 0)
659 return;
660 play_sound_map(SOUND_TYPE_HIT, owner, 0, "hit");
661 draw_ext_info_format(NDI_BLACK, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_PET_HIT,
662 "Your %s%s %s.",
663 hitter->name, buf2, op->name);
664 }
665 }
666
667 /**
668 * Find correct parameters for attack, do some sanity checks.
669 * @param target
670 * will point to victim's head.
671 * @param hitter
672 * will point to hitter's head.
673 * @param simple_attack
674 * will be 1 if one of victim or target isn't on a map, 0 else.
675 * @return
676 * 0 if hitter can attack target, 1 else.
677 */
678 static int get_attack_mode(object **target, object **hitter,
679 int *simple_attack) {
680 if (QUERY_FLAG(*target, FLAG_FREED) || QUERY_FLAG(*hitter, FLAG_FREED)) {
681 LOG(llevError, "BUG: get_attack_mode(): freed object\n");
682 return 1;
683 }
684 *target = HEAD(*target);
685 *hitter = HEAD(*hitter);
686 if ((*hitter)->env != NULL || (*target)->env != NULL) {
687 *simple_attack = 1;
688 return 0;
689 }
690 if (QUERY_FLAG(*target, FLAG_REMOVED)
691 || QUERY_FLAG(*hitter, FLAG_REMOVED)
692 || (*hitter)->map == NULL
693 || !on_same_map((*hitter), (*target))) {
694 LOG(llevError, "BUG: hitter (arch %s, name %s) with no relation to target\n", (*hitter)->arch->name, (*hitter)->name);
695 return 1;
696 }
697 *simple_attack = 0;
698 return 0;
699 }
700
701 /**
702 * Check if target and hitter are still in a relation similar to the one
703 * determined by get_attack_mode().
704 *
705 * @param target
706 * who is attacked.
707 * @param hitter
708 * who is attacking.
709 * @param simple_attack
710 * previous mode as returned by get_attack_mode().
711 * @return
712 * 1 if the relation has changed, 0 else.
713 */
714 static int abort_attack(object *target, object *hitter, int simple_attack) {
715 int new_mode;
716
717 if (hitter->env == target || target->env == hitter)
718 new_mode = 1;
719 else if (QUERY_FLAG(hitter, FLAG_REMOVED)
720 || QUERY_FLAG(target, FLAG_REMOVED)
721 || hitter->map == NULL || !on_same_map(hitter, target))
722 return 1;
723 else
724 new_mode = 0;
725 return new_mode != simple_attack;
726 }
727
728 static void thrown_item_effect(object *, object *);
729
730 /**
731 * Handles simple attack cases.
732 * @param op
733 * victim. Should be the head part.
734 * @param hitter
735 * attacked. Should be the head part.
736 * @param base_dam
737 * damage to do.
738 * @param base_wc
739 * wc to hit with.
740 * @return
741 * dealt damage.
742 * @todo
743 * fix void return values. Try to remove gotos. Better document when it's called.
744 */
745 static int attack_ob_simple(object *op, object *hitter, int base_dam, int base_wc) {
746 int simple_attack, roll, dam;
747 uint32_t type;
748 tag_t op_tag, hitter_tag;
749 const char *anim_suffix = NULL;
750
751 if (get_attack_mode(&op, &hitter, &simple_attack))
752 return 1;
753
754 /* Lauwenmark: This is used to handle script_weapons with weapons.
755 * Only used for players.
756 */
757 if (hitter->type == PLAYER) {
758 if (hitter->current_weapon != NULL) {
759 /* Lauwenmark: Handle for plugin attack event */
760 if (execute_event(hitter->current_weapon, EVENT_ATTACKS,
761 hitter, op, NULL, SCRIPT_FIX_ALL) != 0)
762 return 0;
763 }
764 }
765
766 if (hitter->current_weapon) {
767 anim_suffix = hitter->current_weapon->anim_suffix;
768 } else if (hitter->chosen_skill) {
769 anim_suffix = hitter->chosen_skill->anim_suffix;
770 }
771
772 if (!anim_suffix) {
773 anim_suffix = "attack";
774 }
775 apply_anim_suffix(hitter, anim_suffix);
776
777
778 op_tag = op->count;
779 hitter_tag = hitter->count;
780 /*
781 * A little check to make it more difficult to dance forward and back
782 * to avoid ever being hit by monsters.
783 */
784 if (!simple_attack && QUERY_FLAG(op, FLAG_MONSTER)
785 && op->speed_left > -(FABS(op->speed))*0.3) {
786 /* Decrease speed BEFORE calling process_object. Otherwise, an
787 * infinite loop occurs, with process_object calling monster_move(),
788 * which then gets here again. By decreasing the speed before
789 * we call process_object, the 'if' statement above will fail.
790 */
791 op->speed_left--;
792 process_object(op);
793 if (object_was_destroyed(op, op_tag)
794 || object_was_destroyed(hitter, hitter_tag)
795 || abort_attack(op, hitter, simple_attack))
796 return 1;
797 }
798
799 roll = random_roll(1, 20, hitter, PREFER_HIGH);
800
801 /* Adjust roll for various situations. */
802 if (!simple_attack)
803 roll += adj_attackroll(hitter, op);
804
805 /* See if we hit the creature */
806 if (roll >= 20 || op->stats.ac >= base_wc-roll) {
807 if (settings.casting_time == TRUE) {
808 if (hitter->type == PLAYER && hitter->casting_time > -1) {
809 hitter->casting_time = -1;
810 draw_ext_info(NDI_UNIQUE, 0, hitter, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_FUMBLE,
811 "You attacked and lost your spell!");
812 }
813 if (op->casting_time > -1 && base_dam > 0) {
814 op->casting_time = -1;
815 if (op->type == PLAYER) {
816 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_FUMBLE,
817 "You were hit and lost your spell!");
818 draw_ext_info_format(NDI_ALL|NDI_UNIQUE, 5, NULL,
819 MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_FUMBLE,
820 "%s was hit by %s and lost a spell.",
821 op->name, hitter->name);
822 }
823 }
824 }
825 if (!simple_attack) {
826 /* If you hit something, the victim should *always *wake up.
827 * Before, invisible hitters could avoid doing this.
828 * -b.t. */
829 if (QUERY_FLAG(op, FLAG_SLEEP))
830 CLEAR_FLAG(op, FLAG_SLEEP);
831
832 /* If the victim can't see the attacker, it may alert others
833 * for help. */
834 if (op->type != PLAYER && !monster_can_see_enemy(op, hitter)
835 && !object_get_owner(op) && rndm(0, op->stats.Int))
836 monster_npc_call_help(op);
837
838 /* if you were hidden and hit by a creature, you are discovered*/
839 if (op->hide && QUERY_FLAG(hitter, FLAG_ALIVE)) {
840 make_visible(op);
841 if (op->type == PLAYER)
842 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT,
843 "You were hit by a wild attack. You are no longer hidden!");
844 }
845
846 /* thrown items (hitter) will have various effects
847 * when they hit the victim. For things like thrown daggers,
848 * this sets 'hitter' to the actual dagger, and not the
849 * wrapper object.
850 */
851 thrown_item_effect(hitter, op);
852 if (object_was_destroyed(hitter, hitter_tag)
853 || object_was_destroyed(op, op_tag)
854 || abort_attack(op, hitter, simple_attack)) {
855 return 0;
856 }
857 }
858
859 /* Need to do at least 1 damage, otherwise there is no point
860 * to go further and it will cause FPE's below.
861 */
862 if (base_dam <= 0)
863 base_dam = 1;
864
865 type = hitter->attacktype;
866 if (!type)
867 type = AT_PHYSICAL;
868 /* Handle monsters that hit back */
869 if (!simple_attack && QUERY_FLAG(op, FLAG_HITBACK)
870 && QUERY_FLAG(hitter, FLAG_ALIVE)) {
871 if (op->attacktype&AT_ACID && hitter->type == PLAYER)
872 draw_ext_info(NDI_UNIQUE, 0, hitter, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT,
873 "You are splashed by acid!\n");
874 hit_player(hitter, random_roll(0, (op->stats.dam), hitter, PREFER_LOW), op, op->attacktype, 1);
875 if (object_was_destroyed(op, op_tag)
876 || object_was_destroyed(hitter, hitter_tag)
877 || abort_attack(op, hitter, simple_attack)) {
878 return 0;
879 }
880 }
881
882 /* In the new attack code, it should handle multiple attack
883 * types in its area, so remove it from here.
884 */
885 dam = hit_player(op, random_roll(1, base_dam, hitter, PREFER_HIGH), hitter, type, 1);
886 if (object_was_destroyed(op, op_tag)
887 || object_was_destroyed(hitter, hitter_tag)
888 || abort_attack(op, hitter, simple_attack)) {
889 return 0;
890 }
891 } else {/* end of if hitter hit op */
892 play_sound_map(SOUND_TYPE_HIT, hitter, 0, "miss");
893 dam = 0; /* if we missed, dam=0 */
894 }
895
896 /*attack_message(dam, type, op, hitter);*/
897
898 return dam;
899 }
900
901 /**
902 * Simple wrapper for attack_ob_simple(), will use hitter's values.
903 * @param op
904 * victim.
905 * @param hitter
906 * attacker.
907 * @return
908 * dealt damage.
909 */
910 int attack_ob(object *op, object *hitter) {
911 hitter = HEAD(hitter);
912 return attack_ob_simple(op, hitter, hitter->stats.dam, hitter->stats.wc);
913 }
914
915 /**
916 * Try to put an arrow in inventory.
917 *
918 * @param op
919 * arrow to try to insert.
920 * @param tmp
921 * what is stopping the arrow.
922 * @return
923 * 1 if op was inserted into tmp's inventory, 0 otherwise.
924 */
925 static int stick_arrow(object *op, object *tmp) {
926 /* If the missile hit a player, we insert it in their inventory.
927 * However, if the missile is heavy, we don't do so (assume it falls
928 * to the ground after a hit). What a good value for this is up to
929 * debate - 5000 is 5 kg, so arrows, knives, and other light weapons
930 * stick around.
931 */
932 if (op->weight <= 5000 && tmp->stats.hp >= 0) {
933 object_remove(op);
934 op = object_insert_in_ob(op, HEAD(tmp));
935 return 1;
936 } else
937 return 0;
938 }
939
940 /**
941 * hit_with_arrow() disassembles the missile, attacks the victim and
942 * reassembles the missile.
943 *
944 * @param op
945 * missile hitting.
946 * @param victim
947 * who is hit by op.
948 * @return
949 * pointer to the reassembled missile, or NULL if the missile
950 * isn't available anymore.
951 */
952 object *hit_with_arrow(object *op, object *victim) {
953 object *container, *hitter;
954 int hit_something = 0;
955 tag_t victim_tag, hitter_tag, container_tag;
956 int16_t victim_x, victim_y;
957 mapstruct *victim_map;
958 const char *old_skill = NULL;
959
960 /* Disassemble missile */
961 hitter = op->inv;
962 FOR_OB_AND_BELOW_PREPARE(hitter)
963 if (hitter->type != EVENT_CONNECTOR) {
964 break;
965 }
966 FOR_OB_AND_BELOW_FINISH();
967 if (!hitter) {
968 container = NULL;
969 hitter = op;
970 if (free_no_drop(hitter))
971 return NULL;
972 } else {
973 container = op;
974 object_remove(hitter);
975 if (free_no_drop(hitter))
976 return NULL;
977
978 object_insert_in_map_at(hitter, container->map, hitter, INS_NO_MERGE|INS_NO_WALK_ON, container->x, container->y);
979 /* Note that we now have an empty THROWN_OBJ on the map. Code that
980 * might be called until this THROWN_OBJ is either reassembled or
981 * removed at the end of this function must be able to deal with empty
982 * THROWN_OBJs. */
983 container_tag = container->count;
984 }
985
986 /* Try to hit victim */
987 victim_x = victim->x;
988 victim_y = victim->y;
989 victim_map = victim->map;
990 victim_tag = victim->count;
991 hitter_tag = hitter->count;
992 /* Lauwenmark: Handling plugin attack event for thrown items */
993 /* FIXME provide also to script the skill? hitter is the throwed
994 items, but there is no information about the fact it was
995 thrown
996 */
997 if (execute_event(op, EVENT_ATTACKS, hitter, victim, NULL, SCRIPT_FIX_ALL) == 0) {
998 /*
999 * temporary set the hitter's skill to the one associated with the
1000 * throw wrapper. This is needed to that thrower gets it's xp at the
1001 * correct level. This might proves an awfull hack :/ We should really
1002 * provide attack_ob_simple with the skill to use...
1003 */
1004 if (container != NULL) {
1005 old_skill = hitter->skill;
1006 hitter->skill = add_refcount(container->skill);
1007 }
1008 hit_something = attack_ob_simple(victim, hitter, op->stats.dam, op->stats.wc);
1009 }
1010 /* Arrow attacks door, rune of summoning is triggered, demon is put on
1011 * arrow, move_apply() calls this function, arrow sticks in demon,
1012 * attack_ob_simple() returns, and we've got an arrow that still exists
1013 * but is no longer on the map. Ugh. (Beware: Such things can happen at
1014 * other places as well!)
1015 */
1016 if (object_was_destroyed(hitter, hitter_tag) || hitter->env != NULL) {
1017 if (container) {
1018 object_remove(container);
1019 object_free_drop_inventory(container);
1020 }
1021 return NULL;
1022 }
1023 if (container != NULL) {
1024 free_string(hitter->skill);
1025 hitter->skill = old_skill;
1026 }
1027 /* Missile hit victim */
1028 /* if the speed is > 10, then this is a fast moving arrow, we go straight
1029 * through the target
1030 */
1031 if (hit_something && op->speed <= 10.0) {
1032 /* Stop arrow */
1033 if (container == NULL) {
1034 hitter = fix_stopped_arrow(hitter);
1035 if (hitter == NULL)
1036 return NULL;
1037 } else {
1038 if(!object_was_destroyed(container, container_tag)) {
1039 object_remove(container);
1040 object_free_drop_inventory(container);
1041 }
1042 }
1043
1044 /* Try to stick arrow into victim */
1045 if (!object_was_destroyed(victim, victim_tag)
1046 && stick_arrow(hitter, victim)) {
1047 object_set_owner(hitter, NULL);
1048 return NULL;
1049 }
1050
1051 /* Else try to put arrow on victim's map square
1052 * remove check for P_WALL here. If the arrow got to this
1053 * space, that is good enough - with the new movement code,
1054 * there is now the potential for lots of spaces where something
1055 * can fly over but not otherwise move over. What is the correct
1056 * way to handle those otherwise?
1057 */
1058 if (victim_x != hitter->x || victim_y != hitter->y) {
1059 object_remove(hitter);
1060 object_set_owner(hitter, NULL);
1061 object_insert_in_map_at(hitter, victim_map, hitter, 0, victim_x, victim_y);
1062 } else {
1063 /* Else leave arrow where it is */
1064 object_merge(hitter, NULL);
1065 object_set_owner(hitter, NULL);
1066 }
1067 return NULL;
1068 }
1069
1070 if (hit_something && op->speed >= 10.0)
1071 op->speed -= 1.0;
1072
1073 /* Missile missed victim - reassemble missile */
1074 if (container) {
1075 object_remove(hitter);
1076 object_insert_in_ob(hitter, container);
1077 }
1078 return op;
1079 }
1080 /**
1081 * Handles wall tearing animation. Will change the face according to the hp/maxhp ration.
1082 *
1083 * If the wall reaches its last animation, either free it or set it non living so it doesn't block anymore.
1084 * @param op
1085 * wall to update.
1086 */
1087 static void tear_down_wall(object *op) {
1088 int perc = 0;
1089
1090 if (!op->stats.maxhp) {
1091 LOG(llevError, "TEAR_DOWN wall %s had no maxhp.\n", op->name);
1092 perc = 1;
1093 } else if (!GET_ANIM_ID(op)) {
1094 /* Object has been called - no animations, so remove it */
1095 if (op->stats.hp < 0) {
1096 object_remove(op); /* Should update LOS */
1097 object_free_drop_inventory(op);
1098 /* Don't know why this is here - object_remove() should do it for us */
1099 /*update_position(m, x, y);*/
1100 }
1101 return; /* no animations, so nothing more to do */
1102 }
1103 assert(op->stats.maxhp > 0);
1104 perc = NUM_ANIMATIONS(op)-((int)NUM_ANIMATIONS(op)*op->stats.hp)/op->stats.maxhp;
1105
1106 if (perc >= (int)NUM_ANIMATIONS(op))
1107 perc = NUM_ANIMATIONS(op)-1;
1108 else if (perc < 1)
1109 perc = 1;
1110 SET_ANIMATION(op, perc);
1111 object_update(op, UP_OBJ_FACE);
1112 if (perc == NUM_ANIMATIONS(op)-1) { /* Reached the last animation */
1113 if (op->face == blank_face) {
1114 /* If the last face is blank, remove the ob */
1115 object_remove(op); /* Should update LOS */
1116 object_free_drop_inventory(op);
1117
1118 /* object_remove() should call update_position for us */
1119 /*update_position(m, x, y);*/
1120 } else { /* The last face was not blank, leave an image */
1121 CLEAR_FLAG(op, FLAG_BLOCKSVIEW);
1122 update_all_los(op->map, op->x, op->y);
1123 op->move_block = 0;
1124 CLEAR_FLAG(op, FLAG_ALIVE);
1125 }
1126 }
1127 }
1128
1129 /**
1130 * Creature is scared, update its values.
1131 * @param target
1132 * scared creature.
1133 * @param hitter
1134 * who scared target.
1135 */
1136 static void scare_creature(object *target, object *hitter) {
1137 object *owner = object_get_owner(hitter);
1138
1139 if (!owner)
1140 owner = hitter;
1141
1142 SET_FLAG(target, FLAG_SCARED);
1143 if (!target->enemy)
1144 object_set_enemy(target, owner);
1145 }
1146
1147 /**
1148 * Handles one attacktype's damage.
1149 * This doesn't damage the creature, but returns how much it should
1150 * take. However, it will do other effects (paralyzation, slow, etc.).
1151 * @param op
1152 * victim of the attack.
1153 * @param hitter
1154 * attacker.
1155 * @param dam
1156 * maximum dealt damage.
1157 * @param attacknum
1158 * number of the attacktype of the attack. See @ref Attacktypes "the ATNR_xxx"
1159 * values.
1160 * @return
1161 * damage to actually do.
1162 */
1163 static int hit_with_one_attacktype(object *op, object *hitter, int dam, uint32_t attacknum) {
1164 int doesnt_slay = 1;
1165 char name_hitter[MAX_BUF], name_op[MAX_BUF];
1166
1167 /* Catch anyone that may be trying to send us a bitmask instead of the number */
1168 if (attacknum >= NROFATTACKS) {
1169 LOG(llevError, "hit_with_one_attacktype: Invalid attacknumber passed: %u\n", attacknum);
1170 return 0;
1171 }
1172
1173 if (dam < 0) {
1174 LOG(llevError, "hit_with_one_attacktype called with negative damage: %d\n", dam);
1175 return 0;
1176 }
1177
1178 /* AT_INTERNAL is supposed to do exactly dam. Put a case here so
1179 * people can't mess with that or it otherwise get confused. */
1180 if (attacknum == ATNR_INTERNAL)
1181 return dam;
1182
1183 if (hitter->slaying) {
1184 // Look for the race as one of many that can be listed.
1185 if ((op->race != NULL && strstr(op->race, hitter->slaying))
1186 /*
1187 * Since we can do multiple races, we should be able to avoid the arch check.
1188 * This gives a more flexible race system, so skeletal mages can be a type of skeleton and such
1189 || (op->arch && op->arch->name != NULL && strstr(op->arch->name, hitter->slaying))
1190 */) {
1191 doesnt_slay = 0;
1192 dam *= 3;
1193 }
1194 }
1195
1196 /* Adjust the damage for resistance. Note that neg. values increase damage. */
1197 /*
1198 * Skip lifestealing here, because it undergoes a more specific resistance scaling
1199 * in its own section that involves the better of drain/life stealing resistance
1200 *
1201 * Daniel Hawkins 2018-05-31
1202 */
1203 if (attacknum != ATNR_LIFE_STEALING && op->resist[attacknum]) {
1204 /* basically: dam = dam*(100-op->resist[attacknum])/100;
1205 * in case 0>dam>1, we try to "simulate" a float value-effect */
1206 dam *= (100-op->resist[attacknum]);
1207 if (dam >= 100)
1208 dam /= 100;
1209 else
1210 dam = (dam > (random_roll(0, 99, op, PREFER_LOW))) ? 1 : 0;
1211 }
1212
1213 /* Special hack. By default, if immune to something, you
1214 * shouldn't need to worry. However, acid is an exception, since
1215 * it can still damage your items. Only include attacktypes if
1216 * special processing is needed */
1217
1218 if (op->resist[attacknum] >= 100
1219 && doesnt_slay
1220 && attacknum != ATNR_ACID)
1221 return 0;
1222
1223 /* Keep this in order - makes things easier to find */
1224
1225 switch (attacknum) {
1226 case ATNR_PHYSICAL:
1227 /* here also check for diseases */
1228 check_physically_infect(op, hitter);
1229 break;
1230
1231 /* Don't need to do anything for:
1232 magic,
1233 fire,
1234 electricity,
1235 cold */
1236
1237 case ATNR_CONFUSION:
1238 case ATNR_POISON:
1239 case ATNR_SLOW:
1240 case ATNR_PARALYZE:
1241 case ATNR_FEAR:
1242 case ATNR_CANCELLATION:
1243 case ATNR_DEPLETE:
1244 case ATNR_BLIND: {
1245 /* chance for inflicting a special attack depends on the
1246 * difference between attacker's and defender's level
1247 */
1248 int level_diff = MIN(110, MAX(0, op->level-hitter->level));
1249
1250 /* First, only creatures/players with speed can be affected.
1251 * Second, just getting hit doesn't mean it always affects
1252 * you. Third, you still get a saving through against the
1253 * effect.
1254 */
1255 if (op->speed
1256 && (QUERY_FLAG(op, FLAG_MONSTER) || op->type == PLAYER)
1257 && !(rndm(0, (attacknum == ATNR_SLOW ? 6 : 3)-1))
1258 && !did_make_save(op, level_diff, op->resist[attacknum]/10)) {
1259 /* Player has been hit by something */
1260 if (attacknum == ATNR_CONFUSION)
1261 confuse_living(op, hitter, dam);
1262 else if (attacknum == ATNR_POISON)
1263 poison_living(op, hitter, dam);
1264 else if (attacknum == ATNR_SLOW)
1265 slow_living(op, hitter, dam);
1266 else if (attacknum == ATNR_PARALYZE)
1267 paralyze_living(op, dam);
1268 else if (attacknum == ATNR_FEAR)
1269 scare_creature(op, hitter);
1270 else if (attacknum == ATNR_CANCELLATION)
1271 cancellation(op);
1272 else if (attacknum == ATNR_DEPLETE)
1273 drain_stat(op);
1274 else if (attacknum == ATNR_BLIND
1275 && !QUERY_FLAG(op, FLAG_UNDEAD)
1276 && !QUERY_FLAG(op, FLAG_GENERATOR))
1277 blind_living(op, hitter, dam);
1278 }
1279 dam = 0; /* These are all effects and don't do real damage */
1280 }
1281 break;
1282
1283 case ATNR_ACID: {
1284 int flag = 0;
1285
1286 /* Items only get corroded if you're not on a battleground and
1287 * if your acid resistance is below 50%. */
1288 if (!op_on_battleground(op, NULL, NULL, NULL)
1289 && (op->resist[ATNR_ACID] < 50)) {
1290 FOR_INV_PREPARE(op, tmp) {
1291 if (tmp->invisible)
1292 continue;
1293 if (!QUERY_FLAG(tmp, FLAG_APPLIED)
1294 || (tmp->resist[ATNR_ACID] >= 10))
1295 /* >= 10% acid res. on itmes will protect these */
1296 continue;
1297 if (!(tmp->material&M_IRON))
1298 continue;
1299 if (tmp->magic < -4) /* Let's stop at -5 */
1300 continue;
1301 if (tmp->type == RING
1302 /* removed boots and gloves from exclusion list in PR */
1303 || tmp->type == GIRDLE
1304 || tmp->type == AMULET
1305 || tmp->type == WAND
1306 || tmp->type == ROD)
1307 continue; /* To avoid some strange effects */
1308
1309 /* High damage acid has better chance of corroding objects */
1310 if (rndm(0, dam+4) > random_roll(0, 39, op, PREFER_HIGH)+2*tmp->magic) {
1311 if (op->type == PLAYER) {
1312 /* Make this more visible */
1313 query_name(hitter, name_hitter, MAX_BUF);
1314 query_name(tmp, name_op, MAX_BUF);
1315 draw_ext_info_format(NDI_UNIQUE|NDI_RED, 0, op,
1316 MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT,
1317 "The %s's acid corrodes your %s!",
1318 name_hitter, name_op);
1319 }
1320 flag = 1;
1321 tmp->magic--;
1322 if (op->type == PLAYER)
1323 esrv_update_item(UPD_NAME, op, tmp);
1324 }
1325 } FOR_INV_FINISH();
1326 if (flag)
1327 fix_object(op); /* Something was corroded */
1328 }
1329 }
1330 break;
1331
1332 case ATNR_DRAIN: {
1333 /* rate is the proportion of exp drained. High rate means
1334 * not much is drained, low rate means a lot is drained.
1335 */
1336 int rate;
1337
1338 if (op->resist[ATNR_DRAIN] >= 0)
1339 rate = 50+op->resist[ATNR_DRAIN]/2;
1340 else
1341 rate = 5000/(100-op->resist[ATNR_DRAIN]);
1342
1343 /* full protection has no effect. Nothing else in this
1344 * function needs to get done, so just return. */
1345 if (!rate)
1346 return 0;
1347
1348 if (op->stats.exp <= rate) {
1349 if (op->type == GOLEM)
1350 dam = 999; /* Its force is "sucked" away. 8) */
1351 else
1352 /* If we can't drain, lets try to do physical damage */
1353 dam = hit_with_one_attacktype(op, hitter, dam, ATNR_PHYSICAL);
1354 } else {
1355 /* Randomly give the hitter some hp */
1356 if (hitter->stats.hp < hitter->stats.maxhp
1357 && (op->level > hitter->level)
1358 && random_roll(0, (op->level-hitter->level+2), hitter, PREFER_HIGH) > 3)
1359 hitter->stats.hp++;
1360
1361 /* Can't do drains on battleground spaces.
1362 * Move the wiz check up here - before, the hitter wouldn't gain exp
1363 * exp, but the wiz would still lose exp! If drainee is a wiz,
1364 * nothing happens.
1365 * Try to credit the owner. We try to display player -> player drain
1366 * attacks, hence all the != PLAYER checks.
1367 */
1368 if (!op_on_battleground(hitter, NULL, NULL, NULL) && !QUERY_FLAG(op, FLAG_WAS_WIZ)) {
1369 object *owner = object_get_owner(hitter);
1370 int64_t orig_exp = op->stats.exp;
1371
1372 change_exp(op, -op->stats.exp/rate, NULL, 0);
1373
1374 if (owner && owner != hitter) {
1375 if (op->type != PLAYER || owner->type != PLAYER)
1376 change_exp(owner, MIN(op->stats.exp/(rate*2), orig_exp - op->stats.exp),
1377 hitter->chosen_skill ? hitter->chosen_skill->skill : NULL, SK_EXP_TOTAL);
1378 } else if (op->type != PLAYER || hitter->type != PLAYER) {
1379 change_exp(hitter, MIN(op->stats.exp/(rate*2), orig_exp - op->stats.exp),
1380 hitter->chosen_skill ? hitter->chosen_skill->skill : NULL, 0);
1381 }
1382 }
1383 dam = 1; /* Drain is an effect. Still return 1 - otherwise, if you have pure
1384 * drain attack, you won't know that you are actually sucking out EXP,
1385 * as the messages will say you missed
1386 */
1387 }
1388 }
1389 break;
1390
1391 case ATNR_TURN_UNDEAD: {
1392 if (QUERY_FLAG(op, FLAG_UNDEAD)) {
1393 object *owner = object_get_owner(hitter) == NULL ? hitter : object_get_owner(hitter);
1394 const object *god = find_god(determine_god(owner));
1395 int div = 1;
1396
1397 /* if undead are not an enemy of your god, you turn them
1398 * at half strength */
1399 if (!god
1400 || !god->slaying
1401 || strstr(god->slaying, undead_name) == NULL)
1402 div = 2;
1403
1404 /* The previous code was highly suspect - resist turn undead/100 would
1405 * at best give a bonus of 1 - increase that to resist turn undead/20 -
1406 * this gives a bit higher bonus. Also the bonus was added to the wrong
1407 * side of the equation, actually making it easier to turn creatures
1408 * if they had that resistance.
1409 */
1410 if ((op->level*div + (op->resist[ATNR_TURN_UNDEAD] / 20)) < (get_turn_bonus(owner->stats.Wis)+owner->level))
1411 scare_creature(op, owner);
1412 } else
1413 dam = 0; /* don't damage non undead - should we damage undead? */
1414 }
1415 break;
1416
1417 case ATNR_DEATH:
1418 deathstrike_living(op, hitter, &dam);
1419 break;
1420
1421 case ATNR_CHAOS:
1422 query_name(op, name_op, MAX_BUF);
1423 query_name(hitter, name_hitter, MAX_BUF);
1424 LOG(llevError, "%s was hit by %s with non-specific chaos.\n", name_op, name_hitter);
1425 dam = 0;
1426 break;
1427
1428 case ATNR_COUNTERSPELL:
1429 dam = 0;
1430 /* While counterspell is handled separately and filtered out when it
1431 * moves, players can still step on a square that has an active
1432 * counterspell. When this happens, do no damage because counterspell
1433 * has no effect on anything but spells. */
1434 break;
1435
1436 case ATNR_HOLYWORD: {
1437 /* This has already been handled by hit_player,
1438 * no need to check twice -- DAMN */
1439
1440 object *owner = object_get_owner(hitter) == NULL ? hitter : object_get_owner(hitter);
1441
1442 /* As with turn undead above, give a bonus on the saving throw */
1443 if (op->level+(op->resist[ATNR_HOLYWORD]/100) < owner->level+get_turn_bonus(owner->stats.Wis))
1444 scare_creature(op, owner);
1445 }
1446 break;
1447
1448 case ATNR_LIFE_STEALING: {
1449 int new_hp;
1450 /* this is replacement to drain for players, instead of taking
1451 * exp it takes hp. It is geared for players, probably not
1452 * much use giving it to monsters
1453 *
1454 * life stealing doesn't do a lot of damage, but it gives the
1455 * damage it does do to the player. Given that,
1456 * it only does 1/30'th normal damage (hence the divide by
1457 * 3000). Wraith get 1/2 of the damage, and therefore divide
1458 * by 200. This number may need tweaking for game balance.
1459 */
1460
1461 int dam_modifier = is_wraith_pl(hitter) ? 200 : 3000;
1462
1463 /* You can't steal life from something undead or not alive. */
1464 if (op->type == GOLEM
1465 || (QUERY_FLAG(op, FLAG_UNDEAD))
1466 || !(QUERY_FLAG(op, FLAG_ALIVE))
1467 || (op->type == DOOR))
1468 return 0;
1469 /* If drain protection is higher than life stealing, use that */
1470 if (op->resist[ATNR_DRAIN] >= op->resist[ATNR_LIFE_STEALING])
1471 dam = (dam*(100-op->resist[ATNR_DRAIN]))/dam_modifier;
1472 else
1473 dam = (dam*(100-op->resist[ATNR_LIFE_STEALING]))/dam_modifier;
1474 /* You die at -1 hp, not zero. */
1475 if (dam > op->stats.hp+1)
1476 dam = op->stats.hp+1;
1477 new_hp = hitter->stats.hp+dam;
1478 if (new_hp > hitter->stats.maxhp)
1479 new_hp = hitter->stats.maxhp;
1480 if (new_hp > hitter->stats.hp)
1481 hitter->stats.hp = new_hp;
1482
1483 /* Wraith also get food through life stealing */
1484 if (is_wraith_pl(hitter)) {
1485 if (hitter->stats.food+dam >= MAX_FOOD)
1486 hitter->stats.food = MAX_FOOD;
1487 else
1488 hitter->stats.food += dam;
1489 fix_object(hitter);
1490 }
1491 }
1492 }
1493 return dam;
1494 }
1495
1496 /*
1497 * This function is defined in party.c, but conditionally, something "make proto"
1498 * doesn't handle. So define it locally.
1499 */
1500 #ifdef PARTY_KILL_LOG
1501 void party_add_kill(partylist *party, const char *killer, const char *dead, long exp);
1502 #endif
1503
1504 /**
1505 * An object was killed, handle various things (logging, messages, ...).
1506 *
1507 * GROS: This code comes from hit_player. It has been made external to
1508 * allow script procedures to "kill" objects in a combat-like fashion.
1509 * It was initially used by (kill-object) developed for the Collector's
1510 * Sword. Note that nothing has been changed from the original version
1511 * of the following code.
1512 *
1513 * Will LOG pk, handles battleground, and so on.
1514 *
1515 * This function was a bit of a mess with hitter getting changed,
1516 * values being stored away but not used, etc. I've cleaned it up
1517 * a bit - I think it should be functionally equivalant.
1518 * MSW 2002-07-17
1519 *
1520 * @param op
1521 * what is being killed.
1522 * @param dam
1523 * damage done to it.
1524 * @param hitter
1525 * what is hitting it.
1526 * @return
1527 * dealt damage.
1528 * @todo
1529 * finish commenting what it does exactly.
1530 */
1531 static int kill_object(object *op, int dam, object *hitter) {
1532 char kill_message[MAX_BUF];
1533 const char *skill;
1534 int maxdam = 0;
1535 int battleg = 0; /* true if op standing on battleground */
1536 int pk = 0; /* true if op and what controls hitter are both players*/
1537 object *owner = NULL;
1538 const object *skop = NULL;
1539 sstring death_animation;
1540
1541 if (op->stats.hp >= 0)
1542 return -1;
1543
1544 /* Lauwenmark: Handle for plugin death event */
1545 if (execute_event(op, EVENT_DEATH, hitter, NULL, NULL, SCRIPT_FIX_ALL) != 0)
1546 return 0;
1547 /* Lauwenmark: Handle for the global kill event */
1548 execute_global_event(EVENT_GKILL, op, hitter);
1549
1550 if (op->map) {
1551 death_animation = object_get_value(op, "death_animation");
1552 if (death_animation != NULL) {
1553 object *death = create_archetype(death_animation);
1554
1555 if (death)
1556 object_insert_in_map_at(death, op->map, op, 0, op->x, op->y);
1557 }
1558 }
1559
1560 /* maxdam needs to be the amount of damage it took to kill
1561 * this creature. The function(s) that call us have already
1562 * adjusted the creatures HP total, so that is negative.
1563 */
1564 maxdam = dam+op->stats.hp+1;
1565
1566 if (QUERY_FLAG(op, FLAG_BLOCKSVIEW))
1567 update_all_los(op->map, op->x, op->y); /* makes sure los will be recalculated */
1568
1569 if (op->type == DOOR) {
1570 op->speed = 0.1;
1571 object_update_speed(op);
1572 op->speed_left = -0.05;
1573 return maxdam;
1574 }
1575 if (QUERY_FLAG(op, FLAG_FRIENDLY) && op->type != PLAYER) {
1576 object *owner;
1577
1578 remove_friendly_object(op);
1579 owner = object_get_owner(op);
1580 if (owner != NULL
1581 && owner->type == PLAYER) {
1582 if (owner->contr->ranges[range_golem] == op) {
1583 owner->contr->ranges[range_golem] = NULL;
1584 owner->contr->golem_count = 0;
1585 }
1586
1587 /*play_sound_player_only(owner1->contr, SOUND_PET_IS_KILLED, 0, 0);*/
1588 /* Maybe we should include the owner that killed this, maybe not */
1589 draw_ext_info_format(NDI_UNIQUE, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_PET_DIED,
1590 "Your pet, the %s, is killed by %s.",
1591 op->name, hitter->name);
1592 }
1593 /*
1594 * there can be items both friendly and without any owner, for instance
1595 * in various maps, so this isn't an error.
1596 else
1597 LOG(llevError, "BUG: hit_player(): Encountered golem without owner.\n");
1598 */
1599
1600 object_remove(op);
1601 object_free_drop_inventory(op);
1602 return maxdam;
1603 }
1604
1605 /* Now lets start dealing with experience we get for killing something */
1606
1607 owner = object_get_owner(hitter);
1608 if (owner == NULL)
1609 owner = hitter;
1610
1611 /* is the victim (op) standing on battleground? */
1612 if (op_on_battleground(op, NULL, NULL, NULL))
1613 battleg = 1;
1614
1615 /* is this player killing? -- Don't count it if you suicide, though. */
1616 if (op->type == PLAYER && owner->type == PLAYER && owner != op)
1617 pk = 1;
1618
1619 /* Player killed something */
1620 if (owner->type == PLAYER) {
1621
1622 /* Log players killing other players - makes it easier to detect
1623 * and filter out malicious player killers - that is why the
1624 * ip address is included.
1625 */
1626 if (op->type == PLAYER && !battleg) {
1627 time_t t = time(NULL);
1628 struct tm *tmv;
1629 char buf[256];
1630 char name[MAX_BUF];
1631
1632 tmv = localtime(&t);
1633 strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tmv);
1634 query_name(op, name, MAX_BUF);
1635
1636 LOG(llevInfo, "%s PLAYER_KILL_PLAYER: %s (%s) killed %s\n", buf, owner->name, owner->contr->socket.host, name);
1637 }
1638
1639 /* try to filter some things out - basically, if you are
1640 * killing a level 1 creature and your level 20, you
1641 * probably don't want to see that.
1642 */
1643 if (owner->level < op->level*2 || op->stats.exp > 1000) {
1644 if (owner != hitter) {
1645 char killed[MAX_BUF], with[MAX_BUF];
1646
1647 query_name(op, killed, MAX_BUF);
1648 query_name(hitter, with, MAX_BUF);
1649 draw_ext_info_format(NDI_BLACK, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_KILL,
1650 "You killed %s with %s.",
1651 killed, with);
1652 } else {
1653 char killed[MAX_BUF];
1654
1655 query_name(op, killed, MAX_BUF);
1656 draw_ext_info_format(NDI_BLACK, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_KILL,
1657 "You killed %s.",
1658 killed);
1659 }
1660 }
1661
1662 /* If a player kills another player, not on
1663 * battleground, the "killer" looses 1 luck. Since this is
1664 * not reversible, it's actually quite a pain IMHO. -AV
1665 * Fix bug in that we were changing the luck of the hitter, not
1666 * player that the object belonged to - so if you killed another player
1667 * with spells, pets, whatever, there was no penalty.
1668 * Changed to make luck penalty configurable in settings.
1669 *
1670 * Simplified comparison since pk is no longer set to 1 if self-kill
1671 * -- SilverNexus 2017-06-17+
1672 */
1673 if (pk == 1 && !battleg)
1674 change_luck(owner, -settings.pk_luck_penalty);
1675
1676 /* This code below deals with finding the appropriate skill
1677 * to credit exp to. This is a bit problematic - we should
1678 * probably never really have to look at current_weapon->skill
1679 */
1680 skill = NULL;
1681 if (hitter->skill && hitter->type != PLAYER)
1682 skill = hitter->skill;
1683 else if (owner->chosen_skill) {
1684 skill = owner->chosen_skill->skill;
1685 skop = owner->chosen_skill;
1686 } else if (QUERY_FLAG(owner, FLAG_READY_WEAPON))
1687 skill = owner->current_weapon->skill;
1688 else
1689 LOG(llevError, "kill_object - unable to find skill that killed monster\n");
1690
1691 /* We have the skill we want to credit to - now find the object this goes
1692 * to. Make sure skop is an actual skill, and not a skill tool!
1693 */
1694 if ((!skop || skop->type != SKILL) && skill) {
1695 skop = find_applied_skill_by_name(owner, skill);
1696 }
1697 } /* Was it a player that hit somethign */
1698 else {
1699 skill = NULL;
1700 }
1701
1702 /* Pet (or spell) killed something. */
1703 if (owner != hitter) {
1704 char name_op[MAX_BUF], name_hitter[MAX_BUF];
1705 const char *owner_prefix;
1706 const char *op_prefix;
1707
1708 owner_prefix = !battleg && pk && owner->contr != NULL && !owner->contr->peaceful ? "hostile " : "";
1709 op_prefix = !battleg && pk && op->contr != NULL && !op->contr->peaceful ? "hostile " : "";
1710
1711 query_name(op, name_op, MAX_BUF);
1712 query_name(hitter, name_hitter, MAX_BUF);
1713 snprintf(kill_message, sizeof(kill_message), "%s%s killed %s%s with %s%s.", owner_prefix, owner->name, op_prefix, name_op, name_hitter, battleg ? " (duel)" : (pk ? " (pk)" : ""));
1714 } else {
1715 const char *hitter_prefix;
1716 const char *op_prefix;
1717
1718 hitter_prefix = !battleg && pk && hitter->contr != NULL && !hitter->contr->peaceful ? "hostile " : "";
1719 op_prefix = !battleg && pk && op->contr != NULL && !op->contr->peaceful ? "hostile " : "";
1720
1721 snprintf(kill_message, sizeof(kill_message), "%s%s killed %s%s%s%s.", hitter_prefix, hitter->name, op_prefix, op->name,
1722 (QUERY_FLAG(hitter, FLAG_MONSTER)) || hitter->type == PLAYER ?
1723 " in hand to hand combat" : "", battleg ? " (duel)" : (pk ? " (pk)" : ""));
1724 }
1725 /* These may have been set in the player code section above */
1726 if (!skop)
1727 skop = hitter->chosen_skill;
1728 if (!skill && skop)
1729 skill = skop->skill;
1730
1731 draw_ext_info(NDI_ALL, op->type == PLAYER ? 1 : 10, NULL, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_PLAYER,
1732 kill_message);
1733 play_sound_map(SOUND_TYPE_HIT, op, 0, "kill");
1734
1735
1736 /* If you didn't kill yourself, and your not the wizard */
1737 if (owner != op && !QUERY_FLAG(op, FLAG_WAS_WIZ)) {
1738 int64_t exp;
1739
1740 exp = calc_skill_exp(owner, op, skop);
1741
1742 /* Really don't give much experience for killing other players */
1743 if (op->type == PLAYER) {
1744 if (battleg) {
1745 draw_ext_info(NDI_UNIQUE, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_KILL,
1746 "Your foe has fallen!\nVICTORY!!!");
1747 } else {
1748 exp = settings.pk_max_experience_percent*exp/100;
1749 if (settings.pk_max_experience >= 0)
1750 exp = MIN(settings.pk_max_experience, exp);
1751 /* Never exceed what victim can lose considering permanent exp. */
1752 exp = check_exp_loss(op, exp);
1753 }
1754 }
1755
1756 /* Don't know why this is set this way - doesn't make
1757 * sense to just divide everything by two for no reason.
1758 */
1759
1760 if (!settings.simple_exp)
1761 exp = exp/2;
1762
1763 /* if op is standing on "battleground" (arena), no way to gain
1764 * exp by killing him
1765 */
1766 if (battleg)
1767 exp = 0;
1768
1769 #ifdef PARTY_KILL_LOG
1770 if (owner->type == PLAYER && owner->contr->party != NULL) {
1771 char name[MAX_BUF];
1772 char op_name[MAX_BUF];
1773
1774 query_name(owner, name, MAX_BUF);
1775 query_name(op, op_name, sizeof(op_name));
1776 party_add_kill(owner->contr->party, name, op_name, exp);
1777 }
1778 #endif
1779 share_exp(owner, exp, skill, SK_EXP_TOTAL);
1780 } /* end if person didn't kill himself */
1781
1782 if (op->type != PLAYER) {
1783 object_remove(op);
1784 object_free_drop_inventory(op);
1785 /* Player has been killed! */
1786 } else {
1787 if (owner->type == PLAYER) {
1788 snprintf(op->contr->killer, BIG_NAME, "%s the %s", owner->name, owner->contr->title);
1789 } else {
1790 strncpy(op->contr->killer, hitter->name, BIG_NAME);
1791 op->contr->killer[BIG_NAME-1] = '\0';
1792 }
1793 /* Need to run kill_player (just in case, make sure is not wiz) */
1794 if (!QUERY_FLAG(op, FLAG_WIZ))
1795 kill_player(op, owner->type == PLAYER ? owner : hitter);
1796 }
1797 /* This was return -1 - that doesn't seem correct - if we return -1, process
1798 * continues in the calling function.
1799 */
1800 return maxdam;
1801 }
1802
1803 /**
1804 * Find out if this is friendly fire (PVP and attacker is peaceful) or not
1805 *
1806 * @param op
1807 * victim.
1808 * @param hitter
1809 * attacker.
1810 * @return
1811 * 0 this is not friendly fire, 1 if hitter is a peaceful player, 2 if hitter is a pet of a peaceful player.
1812 */
1813 int friendly_fire(object *op, object *hitter) {
1814 object *owner;
1815 int friendlyfire;
1816
1817 hitter = HEAD(hitter);
1818 friendlyfire = 0;
1819
1820 if (op->type == PLAYER) {
1821 if (hitter->type == PLAYER && hitter->contr->peaceful == 1)
1822 return 1;
1823
1824 owner = object_get_owner(hitter);
1825 if (owner != NULL) {
1826 if (owner->type == PLAYER && owner->contr->peaceful == 1)
1827 friendlyfire = 2;
1828 }
1829
1830 if (hitter->type == SPELL
1831 || hitter->type == POISONING
1832 || hitter->type == DISEASE
1833 || hitter->type == RUNE)
1834 friendlyfire = 0;
1835 }
1836 return friendlyfire;
1837 }
1838
1839 /**
1840 * Object is attacked by something.
1841 *
1842 * This isn't used just for players, but in fact most objects.
1843 *
1844 * Oct 95 - altered the following slightly for MULTIPLE_GODS hack
1845 * which needs new attacktype AT_HOLYWORD to work . b.t.
1846 *
1847 * @param op
1848 * object to be hit
1849 * @param dam
1850 * base damage - protections/vulnerabilities/slaying matches can modify it.
1851 * @param hitter
1852 * what is hitting the object
1853 * @param type
1854 * attacktype
1855 * @param full_hit
1856 * set if monster area does not matter.
1857 * @return
1858 * dealt damage.
1859 * @todo
1860 * rename to something more meaningful.
1861 */
1862 int hit_player(object *op, int dam, object *hitter, uint32_t type, int full_hit) {
1863 int maxdam = 0, ndam = 0, magic = (type&AT_MAGIC);
1864 int maxattacktype, attacknum;
1865 int body_attack = op->head != NULL; /* Did we hit op's head? */
1866 int simple_attack;
1867 tag_t op_tag, hitter_tag;
1868 int rtn_kill = 0;
1869 int friendlyfire;
1870 object *owner;
1871
1872 /* Lauwenmark: Handle for plugin attack event */
1873 if (execute_event(op, EVENT_ATTACKED, hitter, hitter->current_weapon ? hitter->current_weapon : hitter, NULL, SCRIPT_FIX_ALL) != 0)
1874 return 0;
1875 FOR_INV_PREPARE(op, inv)
1876 if (execute_event(inv, EVENT_ATTACKED, hitter, hitter->current_weapon ? hitter->current_weapon : hitter, NULL, SCRIPT_FIX_ALL) != 0)
1877 return 0;
1878 FOR_INV_FINISH();
1879
1880 if (get_attack_mode(&op, &hitter, &simple_attack))
1881 return 0;
1882
1883 /* very simple: if our target has no_damage 1 set or is wiz, we can't hurt him */
1884 if (QUERY_FLAG(op, FLAG_WIZ) || QUERY_FLAG(op, FLAG_NO_DAMAGE))
1885 return 0;
1886
1887 op_tag = op->count;
1888 hitter_tag = hitter->count;
1889
1890 if (body_attack) {
1891 /* slow and paralyze must hit the head. But we don't want to just
1892 * return - we still need to process other attacks the spell still
1893 * might have. So just remove the paralyze and slow attacktypes,
1894 * and keep on processing if we have other attacktypes.
1895 * return if only magic or nothing is left - under normal code
1896 * we don't attack with pure magic if there is another attacktype.
1897 * Only do processing if the initial attacktype includes one of those
1898 * attack so we don't cancel out things like magic bullet.
1899 */
1900 if (type&(AT_PARALYZE|AT_SLOW)) {
1901 type &= ~(AT_PARALYZE|AT_SLOW);
1902 if (!type || type == AT_MAGIC)
1903 return 0;
1904 }
1905 }
1906
1907 if (!simple_attack && op->type == DOOR) {
1908 object *tmp;
1909
1910 tmp = object_find_by_type2(op, RUNE, TRAP);
1911 if (tmp != NULL) {
1912 spring_trap(tmp, hitter);
1913 if (object_was_destroyed(hitter, hitter_tag)
1914 || object_was_destroyed(op, op_tag)
1915 || abort_attack(op, hitter, simple_attack))
1916 return 0;
1917 }
1918 }
1919
1920 if (!QUERY_FLAG(op, FLAG_ALIVE) || op->stats.hp < 0) {
1921 /* FIXME: If a player is killed by a rune in a door, the
1922 * object_was_destroyed() check above doesn't return, and might get here.
1923 */
1924 LOG(llevDebug, "victim (arch %s, name %s) already dead in hit_player()\n", op->arch->name, op->name);
1925 return 0;
1926 }
1927
1928 #ifdef ATTACK_DEBUG
1929 LOG(llevDebug, "hit player: attacktype %d, dam %d\n", type, dam);
1930 #endif
1931
1932 if (magic) {
1933 /* basically: dam = dam*(100-op->resist[attacknum])/100;
1934 * in case 0>dam>1, we try to "simulate" a float value-effect */
1935 dam = dam*(100-op->resist[ATNR_MAGIC]);
1936 if (dam >= 100)
1937 dam /= 100;
1938 else
1939 dam = (dam > (rndm(0, 99))) ? 1 : 0;
1940 }
1941
1942 /* AT_CHAOS here is a weapon or monster. Spells are handled by hit_map
1943 * We don't use shuffle_attack(), because that changes the it in the
1944 * creature structure, and thus is permanent until fix_object() is
1945 * called again. Chaos should change on each attack.
1946 */
1947 if (type&AT_CHAOS) {
1948 type = ATTACKS[RANDOM()%(sizeof(ATTACKS)/sizeof(*ATTACKS))].attacktype|AT_MAGIC;
1949 }
1950
1951 /* Holyword is really an attacktype modifier (like magic is). If
1952 * holyword is part of an attacktype, then make sure the creature is
1953 * a proper match, otherwise no damage.
1954 */
1955 if (type&AT_HOLYWORD) {
1956 const object *god;
1957
1958 if ((!hitter->slaying || (!(op->race && strstr(hitter->slaying, op->race))
1959 && !(op->name && strstr(hitter->slaying, op->name))))
1960 && (!QUERY_FLAG(op, FLAG_UNDEAD) || (hitter->title != NULL
1961 && (god = find_god(determine_god(hitter))) != NULL
1962 && god->race != NULL
1963 && strstr(god->race, undead_name) != NULL)))
1964 return 0;
1965 }
1966
1967 maxattacktype = type; /* initialize this to something */
1968 for (attacknum = 0; attacknum < NROFATTACKS; attacknum++) {
1969 int attacktype;
1970
1971 attacktype = 1<<attacknum;
1972
1973 /* Magic isn't really a true attack type - it gets combined with other
1974 * attack types. As such, skip it over. However, if magic is
1975 * the only attacktype in the group, then still attack with it
1976 */
1977 if (attacktype == AT_MAGIC && (type&~AT_MAGIC))
1978 continue;
1979
1980 /* Go through and hit the player with each attacktype, one by one.
1981 * hit_with_one_attacktype only figures out the damage, doesn't inflict
1982 * it. It will do the appropriate action for attacktypes with
1983 * effects (slow, paralization, etc.
1984 */
1985 if (type&attacktype) {
1986 ndam = hit_with_one_attacktype(op, hitter, dam, attacknum);
1987 /* the >= causes us to prefer messages from special attacks, if
1988 * the damage is equal.
1989 */
1990 if (ndam >= maxdam) {
1991 maxdam = ndam;
1992 maxattacktype = 1<<attacknum;
1993 }
1994 /* Special case: death attack always deals all damage, as it should kill the monster
1995 * right away. */
1996 if (attacktype == AT_DEATH && ndam > 0)
1997 full_hit = 1;
1998 }
1999 }
2000
2001
2002 /* if this is friendly fire then do a set % of damage only
2003 * Note - put a check in to make sure this attack is actually
2004 * doing damage - otherwise, the +1 in the coe below will make
2005 * an attack do damage before when it otherwise didn't
2006 * Only reduce damage if not on battlground - if in arena, do
2007 * full damage. Note that it is intentional that the check for
2008 * battleground is inside the friendlyfire if statement - op_on_battleground()
2009 * is a fairly costly function to call, and we don't want to call it for
2010 * every attack - by doing it only for friendlyfire, it shouldn't get called
2011 * that often
2012 */
2013 friendlyfire = friendly_fire(op, hitter);
2014 if (friendlyfire && maxdam) {
2015 if (!op_on_battleground(op, NULL, NULL, NULL)) {
2016 maxdam = ((dam*settings.set_friendly_fire)/100)+1;
2017 #ifdef ATTACK_DEBUG
2018 LOG(llevDebug, "Friendly fire (type:%d setting: %d%) did %d damage dropped to %d\n", friendlyfire, settings.set_friendly_fire, dam, maxdam);
2019 #endif
2020 }
2021 }
2022
2023 if (!full_hit) {
2024 archetype *at;
2025 unsigned int area = 0;
2026 for (at = op->arch; at != NULL; at = at->more)
2027 area++;
2028
2029 /* basically: maxdam /= area; we try to "simulate" a float
2030 value-effect */
2031 unsigned int remainder = 100*(maxdam%area)/area;
2032 maxdam /= area;
2033 if (RANDOM()%100 < remainder)
2034 maxdam++;
2035 }
2036
2037 #ifdef ATTACK_DEBUG
2038 LOG(llevDebug, "Attacktype %d did %d damage\n", type, maxdam);
2039 #endif
2040
2041 owner = object_get_owner(hitter);
2042 if (owner != NULL) {
2043 if (op->enemy != hitter)
2044 object_set_enemy(op, owner);
2045 } else if (QUERY_FLAG(hitter, FLAG_ALIVE))
2046 if (op->enemy == NULL || rndm(1, 20) == 0)
2047 object_set_enemy(op, hitter);
2048
2049 if (QUERY_FLAG(op, FLAG_UNAGGRESSIVE) && op->type != PLAYER) {
2050 /* The unaggressives look after themselves 8) */
2051 CLEAR_FLAG(op, FLAG_UNAGGRESSIVE);
2052 monster_npc_call_help(op);
2053 }
2054
2055 if (magic && did_make_save(op, op->level, 0))
2056 maxdam = maxdam/2;
2057
2058 attack_message(maxdam, maxattacktype, op, hitter);
2059
2060 op->stats.hp -= maxdam;
2061
2062 /* Eneq(@csd.uu.se): Check to see if monster runs away. */
2063 if (op->stats.hp >= 0
2064 && (QUERY_FLAG(op, FLAG_MONSTER) || op->type == PLAYER)
2065 && op->stats.hp < (int16_t)((int32_t)op->run_away * op->stats.maxhp / 100)) {
2066 if (QUERY_FLAG(op, FLAG_MONSTER))
2067 SET_FLAG(op, FLAG_RUN_AWAY);
2068 else
2069 scare_creature(op, hitter);
2070 }
2071
2072 if (QUERY_FLAG(op, FLAG_TEAR_DOWN)) {
2073 if (maxdam)
2074 tear_down_wall(op);
2075 return maxdam; /* nothing more to do for wall */
2076 }
2077
2078 /* See if the creature has been killed */
2079 rtn_kill = kill_object(op, maxdam, hitter);
2080 if (rtn_kill != -1)
2081 return rtn_kill;
2082
2083 /* Used to be ghosthit removal - we now use the ONE_HIT flag. Note
2084 * that before if the player was immune to ghosthit, the monster
2085 * remained - that is no longer the case.
2086 */
2087 if (QUERY_FLAG(hitter, FLAG_ONE_HIT)) {
2088 if (QUERY_FLAG(hitter, FLAG_FRIENDLY))
2089 remove_friendly_object(hitter);
2090 object_remove(hitter);
2091 object_free_drop_inventory(hitter);
2092 /* Lets handle creatures that are splitting now */
2093 } else if (type&AT_PHYSICAL && !QUERY_FLAG(op, FLAG_FREED) && QUERY_FLAG(op, FLAG_SPLITTING)) {
2094 change_object(op);
2095 } else if (type&AT_DRAIN && hitter->type == GRIMREAPER && hitter->value++ > 10) {
2096 object_remove(hitter);
2097 object_free_drop_inventory(hitter);
2098 }
2099 return maxdam;
2100 }
2101
2102 /**
2103 * Poison a living thing.
2104 *
2105 * @param op
2106 * victim.
2107 * @param hitter
2108 * who is attacking.
2109 * @param dam
2110 * damage to deal.
2111 */
2112 static void poison_living(object *op, object *hitter, int dam) {
2113 archetype *at = find_archetype("poisoning");
2114 if (at == NULL) {
2115 return;
2116 }
2117 object *tmp = arch_present_in_ob(at, op);
2118 const char *skill;
2119
2120 if (tmp == NULL) {
2121 tmp = arch_to_object(at);
2122 if (tmp == NULL)
2123 LOG(llevError, "Failed to clone arch poisoning.\n");
2124 else {
2125 tmp = object_insert_in_ob(tmp, op);
2126 /* peterm: give poisoning some teeth. It should
2127 * be able to kill things better than it does:
2128 * damage should be dependent something--I choose to
2129 * do this: if it's a monster, the damage from the
2130 * poisoning goes as the level of the monster/2.
2131 * If anything else, goes as damage.
2132 */
2133
2134 if (QUERY_FLAG(hitter, FLAG_ALIVE))
2135 tmp->stats.dam += hitter->level/2;
2136 else
2137 tmp->stats.dam = dam;
2138
2139 object_copy_owner(tmp, hitter); /* so we get credit for poisoning kills */
2140 skill = hitter->skill;
2141 if (!skill && hitter->chosen_skill)
2142 skill = hitter->chosen_skill->name;
2143
2144 if (skill && skill != tmp->skill) {
2145 if (tmp->skill)
2146 free_string(tmp->skill);
2147 tmp->skill = add_refcount(skill);
2148 }
2149
2150 tmp->stats.food += dam; /* more damage, longer poisoning */
2151
2152 if (op->type == PLAYER) {
2153 /* player looses stats, maximum is -10 of each */
2154 tmp->stats.Con = MAX(-(dam/4+1), -10);
2155 tmp->stats.Str = MAX(-(dam/3+2), -10);
2156 tmp->stats.Dex = MAX(-(dam/6+1), -10);
2157 tmp->stats.Int = MAX(-dam/7, -10);
2158 SET_FLAG(tmp, FLAG_APPLIED);
2159 fix_object(op);
2160 draw_ext_info(NDI_UNIQUE, 0, op,
2161 MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START,
2162 "You suddenly feel very ill.");
2163 }
2164 if (hitter->type == PLAYER)
2165 draw_ext_info_format(NDI_UNIQUE, 0, hitter,
2166 MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_HIT,
2167 "You poison %s.",
2168 op->name);
2169 else {
2170 object *owner;
2171
2172 owner = object_get_owner(hitter);
2173 if (owner != NULL && owner->type == PLAYER)
2174 draw_ext_info_format(NDI_UNIQUE, 0, owner,
2175 MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_PET_HIT,
2176 "Your %s poisons %s.",
2177 hitter->name, op->name);
2178 }
2179 tmp->speed_left = 0;
2180 }
2181 } else
2182 tmp->stats.food++;
2183 }
2184
2185 int slow_living_by(object *op, const int speed_penalty) {
2186 archetype *at = find_archetype("slowness");
2187 if (at == NULL) {
2188 return 0;
2189 }
2190 object* tmp = arch_present_in_ob(at, op);
2191 int ret;
2192 if (tmp == NULL) {
2193 tmp = arch_to_object(at);
2194 tmp->stats.exp = -speed_penalty;
2195 tmp = object_insert_in_ob(tmp, op);
2196 ret = 1;
2197 }
2198 // If we are hitting for more intense slowness, override the old one.
2199 else if (tmp->stats.exp > -speed_penalty) {
2200 tmp->stats.exp = -speed_penalty;
2201 tmp->stats.food -= 3; // But also reduce the duration to compensate.
2202 ret = 2;
2203 } else {
2204 tmp->stats.food++;
2205 ret = 3;
2206 }
2207 SET_FLAG(tmp, FLAG_APPLIED);
2208 tmp->speed_left = 0;
2209 fix_object(op);
2210 return ret;
2211 }
2212
2213 /**
2214 * Slow a living thing.
2215 *
2216 * @param op
2217 * victim.
2218 * @param hitter
2219 * who is attacking.
2220 * @param dam
2221 * damage to deal.
2222 */
2223 static void slow_living(object *op, object *hitter, int dam) {
2224 // Used to calculate the speed penalty of the slow attempt
2225 int speed_penalty;
2226
2227 /**
2228 * Give slow attacks some oomph -- always speed -1 was REALLY weak
2229 *
2230 * Daniel Hawkins 2018-05-31
2231 */
2232 // Higher level slow effects make you slower.
2233 speed_penalty = hitter->level - op->level + random_roll(1, 5, hitter, PREFER_LOW);
2234 // Resistance to slow will also affect how much you are slowed by.
2235 speed_penalty = speed_penalty * (100-op->resist[ATNR_SLOW]) / 100;
2236 // Make sure we actually have a penalty amount. We can assume that op is not immune in this method.
2237 if (speed_penalty < 1)
2238 speed_penalty = 1;
2239 else if (speed_penalty > 30) // Data barrier for stats.exp is 127, but that is huge slowness. Pick something less than that.
2240 speed_penalty = 30;
2241 switch (slow_living_by(op, speed_penalty)) {
2242 case 1:
2243 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE,
2244 MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START,
2245 "The world suddenly moves very fast!");
2246 break;
2247 case 2:
2248 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE,
2249 MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START,
2250 "The world moves even faster!");
2251 break;
2252
2253 }
2254 }
2255
2256 /**
2257 * Confuse a living thing.
2258 *
2259 * @param op
2260 * victim.
2261 * @param hitter
2262 * who is attacking.
2263 * @param dam
2264 * damage to deal.
2265 */
2266 void confuse_living(object *op, object *hitter, int dam) {
2267 object *tmp;
2268 int maxduration;
2269
2270 tmp = object_present_in_ob_by_name(FORCE, "confusion", op);
2271 if (!tmp) {
2272 tmp = create_archetype(FORCE_NAME);
2273 tmp = object_insert_in_ob(tmp, op);
2274 }
2275
2276 /* Duration added per hit and max. duration of confusion both depend
2277 * on the player's resistance
2278 */
2279 tmp->speed = 0.05;
2280 tmp->subtype = FORCE_CONFUSION;
2281 tmp->duration = 8+MAX(1, 5*(100-op->resist[ATNR_CONFUSION])/100);
2282 if (tmp->name)
2283 free_string(tmp->name);
2284 tmp->name = add_string("confusion");
2285 maxduration = MAX(2, 30*(100-op->resist[ATNR_CONFUSION])/100);
2286 if (tmp->duration > maxduration)
2287 tmp->duration = maxduration;
2288
2289 if (op->type == PLAYER && !QUERY_FLAG(op, FLAG_CONFUSED))
2290 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START,
2291 "You suddenly feel very confused!");
2292 SET_FLAG(op, FLAG_CONFUSED);
2293 }
2294
2295 /**
2296 * Blind a living thing.
2297 *
2298 * @param op
2299 * victim.
2300 * @param hitter
2301 * who is attacking.
2302 * @param dam
2303 * damage to deal.
2304 */
2305 void blind_living(object *op, object *hitter, int dam) {
2306 object *tmp, *owner;
2307 char victim[MAX_BUF];
2308
2309 /* Save some work if we know it isn't going to affect the player */
2310 if (op->resist[ATNR_BLIND] == 100)
2311 return;
2312
2313 tmp = object_present_in_ob(BLINDNESS, op);
2314 if (!tmp) {
2315 tmp = create_archetype("blindness");
2316 SET_FLAG(tmp, FLAG_BLIND);
2317 SET_FLAG(tmp, FLAG_APPLIED);
2318 /* use floats so we don't lose too much precision due to rounding errors.
2319 * speed is a float anyways.
2320 */
2321 tmp->speed = tmp->speed*(100.0-(float)op->resist[ATNR_BLIND])/100;
2322
2323 tmp = object_insert_in_ob(tmp, op);
2324 change_abil(op, tmp); /* Mostly to display any messages */
2325 fix_object(op); /* This takes care of some other stuff */
2326
2327 owner = object_get_owner(hitter);
2328 if (owner == NULL)
2329 owner = hitter;
2330
2331 query_name(op, victim, MAX_BUF);
2332 draw_ext_info_format(NDI_UNIQUE, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_HIT,
2333 "Your attack blinds %s!",
2334 victim);
2335 }
2336 tmp->stats.food += dam;
2337 if (tmp->stats.food > 10)
2338 tmp->stats.food = 10;
2339 }
2340
2341 /**
2342 * Paralyze a living thing.
2343 *
2344 * @param op
2345 * victim.
2346 * @param dam
2347 * damage to deal.
2348 */
2349 void paralyze_living(object *op, int dam) {
2350 float effect, max;
2351
2352 /* Do this as a float - otherwise, rounding might very well reduce this to 0 */
2353 effect = (float)dam*3.0*(100.0-op->resist[ATNR_PARALYZE])/100;
2354
2355 if (effect == 0)
2356 return;
2357
2358 op->speed_left -= FABS(op->speed)*effect;
2359
2360 /* max number of ticks to be affected for. */
2361 max = (100-op->resist[ATNR_PARALYZE])/2;
2362 if (op->speed_left < -(FABS(op->speed)*max))
2363 op->speed_left = (float)-(FABS(op->speed)*max);
2364 // Set a paralyze flag and print a message to a player if the flag isn't set;
2365 // this tells the player that he/she has been hit by a paralysis attack.
2366 if (!QUERY_FLAG(op, FLAG_PARALYZED)) {
2367 SET_FLAG(op, FLAG_PARALYZED);
2368 if (op->type == PLAYER)
2369 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START,
2370 "You limbs stop moving!");
2371 }
2372 /* If the flag is already set, then the paralysis is merely extended.
2373 * At this point, we do nothing.
2374 * It may be worthwhile to give players another message on paralysis extensions.
2375 *
2376 * Daniel Hawkins 2017-08-22
2377 */
2378 }
2379
2380 /**
2381 * Attempts to kill 'op'.
2382 *
2383 * The intention of a death attack is to kill outright things
2384 * that are a lot weaker than the attacker, have a chance of killing
2385 * things somewhat weaker than the caster, and no chance of
2386 * killing something equal or stronger than the attacker.
2387 *
2388 * If a deathstrike attack has a slaying, only a monster
2389 * whose name or race matches a comma-delimited list in the slaying
2390 * field of the deathstriking object is affected (this includes undead).
2391 * If no slaying set, only undead are unaffected.
2392 *
2393 * @param op
2394 * victim.
2395 * @param hitter
2396 * attacker.
2397 * @param[out] dam
2398 * damage to deal, will contain computed damage or 0 if strike failed.
2399 */
2400 static void deathstrike_living(object *op, object *hitter, int *dam) {
2401 int atk_lev, def_lev, kill_lev, roll;
2402
2403 if (hitter->slaying) {
2404 if (!((QUERY_FLAG(op, FLAG_UNDEAD) && strstr(hitter->slaying, undead_name))
2405 || (op->race && strstr(op->race, hitter->slaying))))
2406 {
2407 *dam = 0; // Don't do damage if we aren't deathstriking them.
2408 return;
2409 }
2410 } else
2411 if (QUERY_FLAG(op, FLAG_UNDEAD))
2412 {
2413 *dam = 0; // Don't do damage if we aren't deathstriking them.
2414 return;
2415 }
2416
2417 def_lev = op->level;
2418 if (def_lev < 1) {
2419 LOG(llevError, "deathstrike_living: arch %s (%s in %s at %d, %d) has level < 1\n", op->arch->name, op->name, op->map->name, op->x, op->y);
2420 def_lev = 1;
2421 }
2422 /*
2423 * Redo this calculation -- you could essentially only kill creatures less than half your level,
2424 * making death extremely weak at high levels.
2425 * Refactoring to use a d20 roll with a fairly high DC (still dependent on level)
2426 * Also, toss in a resistance-based hit modifier.
2427 * Higher resistance requires higher levels in order to kill with a death attack.
2428 *
2429 * Daniel Hawkins 2018-05-21
2430 */
2431 atk_lev = (hitter->chosen_skill ? hitter->chosen_skill->level : hitter->level);
2432 /* LOG(llevDebug, "Deathstrike - attack level %d, defender level %d\n", atk_lev, def_lev); */
2433
2434 roll = random_roll(1, 20, hitter, PREFER_HIGH);
2435 kill_lev = roll - 18 + atk_lev; // Use 19+ as a kill for same level and no resistance;
2436 kill_lev = kill_lev * (100 - op->resist[ATNR_DEATH]) / 100; // Do not compress to *= for roundoff reasons.
2437
2438 // If we hit, then kill them. Otherwise, damage is 0.
2439 if (kill_lev > def_lev) {
2440 *dam = op->stats.hp+10; /* take all hp. they can still save for 1/2 */
2441 /* I think this doesn't really do much. Because of
2442 * integer rounding, this only makes any difference if the
2443 * attack level is double the defender level.
2444 */
2445 *dam *= kill_lev/def_lev;
2446 }
2447 else {
2448 *dam = 0; /* no harm done */
2449 }
2450 }
2451
2452 /**
2453 * Handles any special effects of thrown
2454 * items (like attacking living creatures--a potion thrown at a
2455 * monster).
2456 *
2457 * @param hitter
2458 * thrown item.
2459 * @param victim
2460 * object that is hit by hitter.
2461 * @todo
2462 * invert parameters for coherence with other functions?
2463 */
2464 static void thrown_item_effect(object *hitter, object *victim) {
2465 if (!QUERY_FLAG(hitter, FLAG_ALIVE)) {
2466 /* May not need a switch for just 2 types, but this makes it
2467 * easier for expansion.
2468 */
2469 switch (hitter->type) {
2470 case POTION:
2471 /* should player get a save throw instead of checking magic protection? */
2472 if (QUERY_FLAG(victim, FLAG_ALIVE)
2473 && !QUERY_FLAG(victim, FLAG_UNDEAD)
2474 && (victim->resist[ATNR_MAGIC] < 60))
2475 (void)ob_apply(hitter, victim, 0);
2476 break;
2477
2478 case POISON: /* poison drinks */
2479 /* As with potions, should monster get a save? */
2480 if (QUERY_FLAG(victim, FLAG_ALIVE)
2481 && !QUERY_FLAG(victim, FLAG_UNDEAD)
2482 && (victim->resist[ATNR_POISON] < 60))
2483 (void)ob_apply(victim, hitter, 0);
2484 break;
2485
2486 /* Removed case statements that did nothing.
2487 * food may be poisonous, but monster must be willing to eat it,
2488 * so we don't handle it here.
2489 * Containers should perhaps break open, but that code was disabled.
2490 */
2491 }
2492 }
2493 }
2494
2495 /**
2496 * Adjustments to attack rolls by various conditions
2497 * @param hitter
2498 * who is hitting.
2499 * @param target
2500 * victim of the attack.
2501 * @return
2502 * adjustment to attack roll.
2503 */
2504 static int adj_attackroll(object *hitter, object *target) {
2505 object *attacker = hitter;
2506 int adjust = 0;
2507
2508 /* safety */
2509 if (!target || !hitter || !hitter->map || !target->map || !on_same_map(hitter, target)) {
2510 LOG(llevError, "BUG: adj_attackroll(): hitter and target not on same map\n");
2511 return 0;
2512 }
2513
2514 /* aimed missiles use the owning object's sight */
2515 if (is_aimed_missile(hitter)) {
2516 attacker = object_get_owner(hitter);
2517 if (attacker == NULL)
2518 attacker = hitter;
2519 /* A player who saves but hasn't quit still could have objects
2520 * owned by him - need to handle that case to avoid crashes.
2521 */
2522 if (QUERY_FLAG(attacker, FLAG_REMOVED))
2523 attacker = hitter;
2524 } else if (!QUERY_FLAG(hitter, FLAG_ALIVE))
2525 return 0;
2526
2527 /* determine the condtions under which we make an attack.
2528 * Add more cases, as the need occurs. */
2529
2530 if (!monster_can_see_enemy(attacker, target)) {
2531 /* target is unseen */
2532 if (target->invisible || QUERY_FLAG(attacker, FLAG_BLIND))
2533 adjust -= 10;
2534 /* dark map penalty for the hitter, though xray can help for a player */
2535 else if (target->map && target->map->darkness > 0 && !monster_stand_in_light(target) && (hitter->type != PLAYER || !player_can_view(hitter, target)))
2536 adjust -= target->map->darkness;
2537 }
2538
2539 if (QUERY_FLAG(attacker, FLAG_SCARED))
2540 adjust -= 3;
2541
2542 if (QUERY_FLAG(target, FLAG_UNAGGRESSIVE))
2543 adjust += 1;
2544
2545 if (QUERY_FLAG(target, FLAG_SCARED))
2546 adjust += 1;
2547
2548 if (QUERY_FLAG(attacker, FLAG_CONFUSED))
2549 adjust -= 3;
2550
2551 /* if we attack at a different 'altitude' its harder
2552 * Note - only make this adjustment if the target actually
2553 * has a move type. Doors don't (they don't move), and
2554 * this would evaluate as true. If anything, something without
2555 * a move type should be easier to hit.
2556 */
2557 if (target->move_type && (attacker->move_type&target->move_type) == 0)
2558 adjust -= 2;
2559
2560 return adjust;
2561 }
2562
2563 /**
2564 * Determine if the object is an 'aimed' missile.
2565 *
2566 * @param op
2567 * object to check.
2568 * @return
2569 * 1 if aimed missile, 0 else.
2570 */
2571 static int is_aimed_missile(object *op) {
2572 /* I broke what used to be one big if into a few nested
2573 * ones so that figuring out the logic is at least possible.
2574 */
2575 if (op && (op->move_type&MOVE_FLYING)) {
2576 if (op->type == ARROW || op->type == THROWN_OBJ)
2577 return 1;
2578 else if (op->type == SPELL_EFFECT
2579 && (op->subtype == SP_BULLET || op->subtype == SP_EXPLOSION))
2580 return 1;
2581 }
2582 return 0;
2583 }