1 /* $NetBSD: hit.c,v 1.9 2008/01/14 00:23:51 dholland Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)hit.c 8.1 (Berkeley) 5/31/93";
40 __RCSID("$NetBSD: hit.c,v 1.9 2008/01/14 00:23:51 dholland Exp $");
47 * This source herein may be modified and/or distributed by anybody who
48 * so desires, with the following restrictions:
49 * 1.) No portion of this notice shall be removed.
50 * 2.) Credit shall not be taken for the creation of this source.
51 * 3.) This code is not to be traded, sold, or used for personal
58 static int damage_for_strength(void);
59 static int get_w_damage(const object
*);
60 static int to_hit(const object
*);
62 static object
*fight_monster
= NULL
;
63 char hit_message
[HIT_MESSAGE_SIZE
] = "";
66 mon_hit(object
*monster
)
68 short damage
, hit_chance
;
72 if (fight_monster
&& (monster
!= fight_monster
)) {
75 monster
->trow
= NO_ROOM
;
76 if (cur_level
>= (AMULET_LEVEL
* 2)) {
79 hit_chance
= monster
->m_hit_chance
;
80 hit_chance
-= (((2 * rogue
.exp
) + (2 * ring_exp
)) - r_rings
);
88 mn
= mon_name(monster
);
90 if (!rand_percent(hit_chance
)) {
92 messagef(1, "%sthe %s misses", hit_message
, mn
);
98 messagef(1, "%sthe %s hit", hit_message
, mn
);
101 if (!(monster
->m_flags
& STATIONARY
)) {
102 damage
= get_damage(monster
->m_damage
, 1);
103 if (cur_level
>= (AMULET_LEVEL
* 2)) {
104 minus
= (float)((AMULET_LEVEL
* 2) - cur_level
);
106 minus
= (float)get_armor_class(rogue
.armor
) * 3.00;
107 minus
= minus
/100.00 * (float)damage
;
109 damage
-= (short)minus
;
111 damage
= monster
->stationary_damage
++;
117 rogue_damage(damage
, monster
, 0);
119 if (monster
->m_flags
& SPECIAL_HIT
) {
120 special_hit(monster
);
125 rogue_hit(object
*monster
, boolean force_hit
)
127 short damage
, hit_chance
;
130 if (check_imitator(monster
)) {
133 hit_chance
= force_hit
? 100 : get_hit_chance(rogue
.weapon
);
138 if (!rand_percent(hit_chance
)) {
139 if (!fight_monster
) {
140 (void)strlcpy(hit_message
, "you miss ",
141 sizeof(hit_message
));
145 damage
= get_weapon_damage(rogue
.weapon
);
152 if (mon_damage(monster
, damage
)) { /* still alive? */
153 if (!fight_monster
) {
154 (void)strlcpy(hit_message
, "you hit ",
155 sizeof(hit_message
));
158 RET
: check_gold_seeker(monster
);
164 rogue_damage(short d
, object
*monster
, short other
)
166 if (d
>= rogue
.hp_current
) {
167 rogue
.hp_current
= 0;
168 print_stats(STAT_HP
);
169 killed_by(monster
, other
);
172 rogue
.hp_current
-= d
;
173 print_stats(STAT_HP
);
178 get_damage(const char *ds
, boolean r
)
180 int i
= 0, j
, n
, d
, total
= 0;
183 n
= get_number(ds
+i
);
184 while ((ds
[i
] != 'd') && ds
[i
]) {
191 d
= get_number(ds
+i
);
192 while ((ds
[i
] != '/') && ds
[i
]) {
199 for (j
= 0; j
< n
; j
++) {
201 total
+= get_rand(1, d
);
211 get_w_damage(const object
*obj
)
214 int tmp_to_hit
, tmp_damage
;
217 if ((!obj
) || (obj
->what_is
!= WEAPON
)) {
220 tmp_to_hit
= get_number(obj
->damage
) + obj
->hit_enchant
;
221 while ((obj
->damage
[i
] != 'd') && obj
->damage
[i
]) {
224 if (obj
->damage
[i
] == 'd') {
227 tmp_damage
= get_number(obj
->damage
+ i
) + obj
->d_enchant
;
229 snprintf(new_damage
, sizeof(new_damage
), "%dd%d",
230 tmp_to_hit
, tmp_damage
);
232 return(get_damage(new_damage
, 1));
236 get_number(const char *s
)
241 while ((s
[i
] >= '0') && (s
[i
] <= '9')) {
242 total
= (10 * total
) + (s
[i
] - '0');
249 lget_number(const char *s
)
254 while ((s
[i
] >= '0') && (s
[i
] <= '9')) {
255 total
= (10 * total
) + (s
[i
] - '0');
262 to_hit(const object
*obj
)
267 return(get_number(obj
->damage
) + obj
->hit_enchant
);
271 damage_for_strength(void)
275 strength
= rogue
.str_current
+ add_strength
;
280 if (strength
<= 14) {
283 if (strength
<= 17) {
286 if (strength
<= 18) {
289 if (strength
<= 20) {
292 if (strength
<= 21) {
295 if (strength
<= 30) {
302 mon_damage(object
*monster
, short damage
)
307 monster
->hp_to_kill
-= damage
;
309 if (monster
->hp_to_kill
<= 0) {
312 dungeon
[row
][col
] &= ~MONSTER
;
313 mvaddch(row
, col
, get_dungeon_char(row
, col
));
317 mn
= mon_name(monster
);
318 messagef(1, "%sdefeated the %s", hit_message
, mn
);
320 add_exp(monster
->kill_exp
, 1);
321 take_from_pack(monster
, &level_monsters
);
323 if (monster
->m_flags
& HOLDS
) {
326 free_object(monster
);
333 fight(boolean to_the_death
)
337 boolean first_miss
= 1;
338 short possible_damage
;
342 while (!is_direction(ch
= rgetchar(), &d
)) {
345 messagef(0, "direction?");
353 row
= rogue
.row
; col
= rogue
.col
;
354 get_dir_rc(d
, &row
, &col
, 0);
356 c
= mvinch(row
, col
);
357 if (((c
< 'A') || (c
> 'Z')) ||
358 (!can_move(rogue
.row
, rogue
.col
, row
, col
))) {
359 messagef(0, "I see no monster there");
362 if (!(fight_monster
= object_at(&level_monsters
, row
, col
))) {
365 if (!(fight_monster
->m_flags
& STATIONARY
)) {
366 possible_damage
= ((get_damage(fight_monster
->m_damage
, 0) * 2) / 3);
368 possible_damage
= fight_monster
->stationary_damage
- 1;
370 while (fight_monster
) {
371 (void)one_move_rogue(ch
, 0);
372 if (((!to_the_death
) && (rogue
.hp_current
<= possible_damage
)) ||
373 interrupted
|| (!(dungeon
[row
][col
] & MONSTER
))) {
376 monster
= object_at(&level_monsters
, row
, col
);
377 if (monster
!= fight_monster
) {
385 get_dir_rc(short dir
, short *row
, short *col
, short allow_off_screen
)
389 if (allow_off_screen
|| (*col
> 0)) {
394 if (allow_off_screen
|| (*row
< (DROWS
-2))) {
399 if (allow_off_screen
|| (*row
> MIN_ROW
)) {
404 if (allow_off_screen
|| (*col
< (DCOLS
-1))) {
409 if (allow_off_screen
|| ((*row
> MIN_ROW
) && (*col
> 0))) {
415 if (allow_off_screen
|| ((*row
> MIN_ROW
) && (*col
< (DCOLS
-1)))) {
421 if (allow_off_screen
|| ((*row
< (DROWS
-2)) && (*col
< (DCOLS
-1)))) {
427 if (allow_off_screen
|| ((*row
< (DROWS
-2)) && (*col
> 0))) {
436 get_hit_chance(const object
*weapon
)
441 hit_chance
+= 3 * to_hit(weapon
);
442 hit_chance
+= (((2 * rogue
.exp
) + (2 * ring_exp
)) - r_rings
);
447 get_weapon_damage(const object
*weapon
)
451 damage
= get_w_damage(weapon
);
452 damage
+= damage_for_strength();
453 damage
+= ((((rogue
.exp
+ ring_exp
) - r_rings
) + 1) / 2);
458 s_con_mon(object
*monster
)
461 monster
->m_flags
|= CONFUSED
;
462 monster
->moves_confused
+= get_rand(12, 22);
463 messagef(0, "the monster appears confused");