ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/attack.C
Revision: 1.119
Committed: Tue Nov 10 16:02:07 2009 UTC (14 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.118: +19 -11 lines
Log Message:
fix material, should probbaly use null pointer

File Contents

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