Home | History | Annotate | Line # | Download | only in rogue
hit.c revision 1.3
      1  1.3  cgd /*	$NetBSD: hit.c,v 1.3 1995/04/22 10:27:30 cgd Exp $	*/
      2  1.3  cgd 
      3  1.1  cgd /*
      4  1.3  cgd  * Copyright (c) 1988, 1993
      5  1.3  cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1  cgd  *
      7  1.1  cgd  * This code is derived from software contributed to Berkeley by
      8  1.1  cgd  * Timothy C. Stoehr.
      9  1.1  cgd  *
     10  1.1  cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1  cgd  * modification, are permitted provided that the following conditions
     12  1.1  cgd  * are met:
     13  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1  cgd  *    must display the following acknowledgement:
     20  1.1  cgd  *	This product includes software developed by the University of
     21  1.1  cgd  *	California, Berkeley and its contributors.
     22  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1  cgd  *    may be used to endorse or promote products derived from this software
     24  1.1  cgd  *    without specific prior written permission.
     25  1.1  cgd  *
     26  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1  cgd  * SUCH DAMAGE.
     37  1.1  cgd  */
     38  1.1  cgd 
     39  1.1  cgd #ifndef lint
     40  1.3  cgd #if 0
     41  1.3  cgd static char sccsid[] = "@(#)hit.c	8.1 (Berkeley) 5/31/93";
     42  1.3  cgd #else
     43  1.3  cgd static char rcsid[] = "$NetBSD: hit.c,v 1.3 1995/04/22 10:27:30 cgd Exp $";
     44  1.3  cgd #endif
     45  1.1  cgd #endif /* not lint */
     46  1.1  cgd 
     47  1.1  cgd /*
     48  1.1  cgd  * hit.c
     49  1.1  cgd  *
     50  1.1  cgd  * This source herein may be modified and/or distributed by anybody who
     51  1.1  cgd  * so desires, with the following restrictions:
     52  1.1  cgd  *    1.)  No portion of this notice shall be removed.
     53  1.1  cgd  *    2.)  Credit shall not be taken for the creation of this source.
     54  1.1  cgd  *    3.)  This code is not to be traded, sold, or used for personal
     55  1.1  cgd  *         gain or profit.
     56  1.1  cgd  *
     57  1.1  cgd  */
     58  1.1  cgd 
     59  1.1  cgd #include "rogue.h"
     60  1.1  cgd 
     61  1.1  cgd object *fight_monster = 0;
     62  1.1  cgd char hit_message[80] = "";
     63  1.1  cgd 
     64  1.1  cgd extern short halluc, blind, cur_level;
     65  1.1  cgd extern short add_strength, ring_exp, r_rings;
     66  1.1  cgd extern boolean being_held, interrupted, wizard, con_mon;
     67  1.1  cgd 
     68  1.1  cgd mon_hit(monster)
     69  1.1  cgd register object *monster;
     70  1.1  cgd {
     71  1.1  cgd 	short damage, hit_chance;
     72  1.1  cgd 	char *mn;
     73  1.1  cgd 	float minus;
     74  1.1  cgd 
     75  1.1  cgd 	if (fight_monster && (monster != fight_monster)) {
     76  1.1  cgd 		fight_monster = 0;
     77  1.1  cgd 	}
     78  1.1  cgd 	monster->trow = NO_ROOM;
     79  1.1  cgd 	if (cur_level >= (AMULET_LEVEL * 2)) {
     80  1.1  cgd 		hit_chance = 100;
     81  1.1  cgd 	} else {
     82  1.1  cgd 		hit_chance = monster->m_hit_chance;
     83  1.1  cgd 		hit_chance -= (((2 * rogue.exp) + (2 * ring_exp)) - r_rings);
     84  1.1  cgd 	}
     85  1.1  cgd 	if (wizard) {
     86  1.1  cgd 		hit_chance /= 2;
     87  1.1  cgd 	}
     88  1.1  cgd 	if (!fight_monster) {
     89  1.1  cgd 		interrupted = 1;
     90  1.1  cgd 	}
     91  1.1  cgd 	mn = mon_name(monster);
     92  1.1  cgd 
     93  1.1  cgd 	if (!rand_percent(hit_chance)) {
     94  1.1  cgd 		if (!fight_monster) {
     95  1.1  cgd 			sprintf(hit_message + strlen(hit_message), "the %s misses", mn);
     96  1.1  cgd 			message(hit_message, 1);
     97  1.1  cgd 			hit_message[0] = 0;
     98  1.1  cgd 		}
     99  1.1  cgd 		return;
    100  1.1  cgd 	}
    101  1.1  cgd 	if (!fight_monster) {
    102  1.1  cgd 		sprintf(hit_message + strlen(hit_message), "the %s hit", mn);
    103  1.1  cgd 		message(hit_message, 1);
    104  1.1  cgd 		hit_message[0] = 0;
    105  1.1  cgd 	}
    106  1.1  cgd 	if (!(monster->m_flags & STATIONARY)) {
    107  1.1  cgd 		damage = get_damage(monster->m_damage, 1);
    108  1.1  cgd 		if (cur_level >= (AMULET_LEVEL * 2)) {
    109  1.1  cgd 			minus = (float) ((AMULET_LEVEL * 2) - cur_level);
    110  1.1  cgd 		} else {
    111  1.1  cgd 			minus = (float) get_armor_class(rogue.armor) * 3.00;
    112  1.1  cgd 			minus = minus/100.00 * (float) damage;
    113  1.1  cgd 		}
    114  1.1  cgd 		damage -= (short) minus;
    115  1.1  cgd 	} else {
    116  1.1  cgd 		damage = monster->stationary_damage++;
    117  1.1  cgd 	}
    118  1.1  cgd 	if (wizard) {
    119  1.1  cgd 		damage /= 3;
    120  1.1  cgd 	}
    121  1.1  cgd 	if (damage > 0) {
    122  1.1  cgd 		rogue_damage(damage, monster, 0);
    123  1.1  cgd 	}
    124  1.1  cgd 	if (monster->m_flags & SPECIAL_HIT) {
    125  1.1  cgd 		special_hit(monster);
    126  1.1  cgd 	}
    127  1.1  cgd }
    128  1.1  cgd 
    129  1.1  cgd rogue_hit(monster, force_hit)
    130  1.1  cgd register object *monster;
    131  1.1  cgd boolean force_hit;
    132  1.1  cgd {
    133  1.1  cgd 	short damage, hit_chance;
    134  1.1  cgd 
    135  1.1  cgd 	if (monster) {
    136  1.1  cgd 		if (check_imitator(monster)) {
    137  1.1  cgd 			return;
    138  1.1  cgd 		}
    139  1.1  cgd 		hit_chance = force_hit ? 100 : get_hit_chance(rogue.weapon);
    140  1.1  cgd 
    141  1.1  cgd 		if (wizard) {
    142  1.1  cgd 			hit_chance *= 2;
    143  1.1  cgd 		}
    144  1.1  cgd 		if (!rand_percent(hit_chance)) {
    145  1.1  cgd 			if (!fight_monster) {
    146  1.1  cgd 				(void) strcpy(hit_message, "you miss  ");
    147  1.1  cgd 			}
    148  1.1  cgd 			goto RET;
    149  1.1  cgd 		}
    150  1.1  cgd 		damage = get_weapon_damage(rogue.weapon);
    151  1.1  cgd 		if (wizard) {
    152  1.1  cgd 			damage *= 3;
    153  1.1  cgd 		}
    154  1.1  cgd 		if (con_mon) {
    155  1.1  cgd 			s_con_mon(monster);
    156  1.1  cgd 		}
    157  1.1  cgd 		if (mon_damage(monster, damage)) {	/* still alive? */
    158  1.1  cgd 			if (!fight_monster) {
    159  1.1  cgd 				(void) strcpy(hit_message, "you hit  ");
    160  1.1  cgd 			}
    161  1.1  cgd 		}
    162  1.1  cgd RET:	check_gold_seeker(monster);
    163  1.1  cgd 		wake_up(monster);
    164  1.1  cgd 	}
    165  1.1  cgd }
    166  1.1  cgd 
    167  1.1  cgd rogue_damage(d, monster, other)
    168  1.1  cgd short d;
    169  1.1  cgd object *monster;
    170  1.1  cgd short other;
    171  1.1  cgd {
    172  1.1  cgd 	if (d >= rogue.hp_current) {
    173  1.1  cgd 		rogue.hp_current = 0;
    174  1.1  cgd 		print_stats(STAT_HP);
    175  1.1  cgd 		killed_by(monster, other);
    176  1.1  cgd 	}
    177  1.1  cgd 	if (d > 0) {
    178  1.1  cgd 		rogue.hp_current -= d;
    179  1.1  cgd 		print_stats(STAT_HP);
    180  1.1  cgd 	}
    181  1.1  cgd }
    182  1.1  cgd 
    183  1.1  cgd get_damage(ds, r)
    184  1.1  cgd char *ds;
    185  1.1  cgd boolean r;
    186  1.1  cgd {
    187  1.1  cgd 	register i = 0, j, n, d, total = 0;
    188  1.1  cgd 
    189  1.1  cgd 	while (ds[i]) {
    190  1.1  cgd 		n = get_number(ds+i);
    191  1.1  cgd 		while (ds[i++] != 'd') ;
    192  1.1  cgd 		d = get_number(ds+i);
    193  1.1  cgd 		while ((ds[i] != '/') && ds[i]) i++;
    194  1.1  cgd 
    195  1.1  cgd 		for (j = 0; j < n; j++) {
    196  1.1  cgd 			if (r) {
    197  1.1  cgd 				total += get_rand(1, d);
    198  1.1  cgd 			} else {
    199  1.1  cgd 				total += d;
    200  1.1  cgd 			}
    201  1.1  cgd 		}
    202  1.1  cgd 		if (ds[i] == '/') {
    203  1.1  cgd 			i++;
    204  1.1  cgd 		}
    205  1.1  cgd 	}
    206  1.1  cgd 	return(total);
    207  1.1  cgd }
    208  1.1  cgd 
    209  1.1  cgd get_w_damage(obj)
    210  1.1  cgd object *obj;
    211  1.1  cgd {
    212  1.1  cgd 	char new_damage[12];
    213  1.1  cgd 	register to_hit, damage;
    214  1.1  cgd 	register i = 0;
    215  1.1  cgd 
    216  1.1  cgd 	if ((!obj) || (obj->what_is != WEAPON)) {
    217  1.1  cgd 		return(-1);
    218  1.1  cgd 	}
    219  1.1  cgd 	to_hit = get_number(obj->damage) + obj->hit_enchant;
    220  1.1  cgd 	while (obj->damage[i++] != 'd') ;
    221  1.1  cgd 	damage = get_number(obj->damage + i) + obj->d_enchant;
    222  1.1  cgd 
    223  1.1  cgd 	sprintf(new_damage, "%dd%d", to_hit, damage);
    224  1.1  cgd 
    225  1.1  cgd 	return(get_damage(new_damage, 1));
    226  1.1  cgd }
    227  1.1  cgd 
    228  1.1  cgd get_number(s)
    229  1.1  cgd register char *s;
    230  1.1  cgd {
    231  1.1  cgd 	register i = 0;
    232  1.1  cgd 	register total = 0;
    233  1.1  cgd 
    234  1.1  cgd 	while ((s[i] >= '0') && (s[i] <= '9')) {
    235  1.1  cgd 		total = (10 * total) + (s[i] - '0');
    236  1.1  cgd 		i++;
    237  1.1  cgd 	}
    238  1.1  cgd 	return(total);
    239  1.1  cgd }
    240  1.1  cgd 
    241  1.1  cgd long
    242  1.1  cgd lget_number(s)
    243  1.1  cgd char *s;
    244  1.1  cgd {
    245  1.1  cgd 	short i = 0;
    246  1.1  cgd 	long total = 0;
    247  1.1  cgd 
    248  1.1  cgd 	while ((s[i] >= '0') && (s[i] <= '9')) {
    249  1.1  cgd 		total = (10 * total) + (s[i] - '0');
    250  1.1  cgd 		i++;
    251  1.1  cgd 	}
    252  1.1  cgd 	return(total);
    253  1.1  cgd }
    254  1.1  cgd 
    255  1.1  cgd to_hit(obj)
    256  1.1  cgd object *obj;
    257  1.1  cgd {
    258  1.1  cgd 	if (!obj) {
    259  1.1  cgd 		return(1);
    260  1.1  cgd 	}
    261  1.1  cgd 	return(get_number(obj->damage) + obj->hit_enchant);
    262  1.1  cgd }
    263  1.1  cgd 
    264  1.1  cgd damage_for_strength()
    265  1.1  cgd {
    266  1.1  cgd 	short strength;
    267  1.1  cgd 
    268  1.1  cgd 	strength = rogue.str_current + add_strength;
    269  1.1  cgd 
    270  1.1  cgd 	if (strength <= 6) {
    271  1.1  cgd 		return(strength-5);
    272  1.1  cgd 	}
    273  1.1  cgd 	if (strength <= 14) {
    274  1.1  cgd 		return(1);
    275  1.1  cgd 	}
    276  1.1  cgd 	if (strength <= 17) {
    277  1.1  cgd 		return(3);
    278  1.1  cgd 	}
    279  1.1  cgd 	if (strength <= 18) {
    280  1.1  cgd 		return(4);
    281  1.1  cgd 	}
    282  1.1  cgd 	if (strength <= 20) {
    283  1.1  cgd 		return(5);
    284  1.1  cgd 	}
    285  1.1  cgd 	if (strength <= 21) {
    286  1.1  cgd 		return(6);
    287  1.1  cgd 	}
    288  1.1  cgd 	if (strength <= 30) {
    289  1.1  cgd 		return(7);
    290  1.1  cgd 	}
    291  1.1  cgd 	return(8);
    292  1.1  cgd }
    293  1.1  cgd 
    294  1.1  cgd mon_damage(monster, damage)
    295  1.1  cgd object *monster;
    296  1.1  cgd short damage;
    297  1.1  cgd {
    298  1.1  cgd 	char *mn;
    299  1.1  cgd 	short row, col;
    300  1.1  cgd 
    301  1.1  cgd 	monster->hp_to_kill -= damage;
    302  1.1  cgd 
    303  1.1  cgd 	if (monster->hp_to_kill <= 0) {
    304  1.1  cgd 		row = monster->row;
    305  1.1  cgd 		col = monster->col;
    306  1.1  cgd 		dungeon[row][col] &= ~MONSTER;
    307  1.1  cgd 		mvaddch(row, col, (int) get_dungeon_char(row, col));
    308  1.1  cgd 
    309  1.1  cgd 		fight_monster = 0;
    310  1.1  cgd 		cough_up(monster);
    311  1.1  cgd 		mn = mon_name(monster);
    312  1.1  cgd 		sprintf(hit_message+strlen(hit_message), "defeated the %s", mn);
    313  1.1  cgd 		message(hit_message, 1);
    314  1.1  cgd 		hit_message[0] = 0;
    315  1.1  cgd 		add_exp(monster->kill_exp, 1);
    316  1.1  cgd 		take_from_pack(monster, &level_monsters);
    317  1.1  cgd 
    318  1.1  cgd 		if (monster->m_flags & HOLDS) {
    319  1.1  cgd 			being_held = 0;
    320  1.1  cgd 		}
    321  1.1  cgd 		free_object(monster);
    322  1.1  cgd 		return(0);
    323  1.1  cgd 	}
    324  1.1  cgd 	return(1);
    325  1.1  cgd }
    326  1.1  cgd 
    327  1.1  cgd fight(to_the_death)
    328  1.1  cgd boolean to_the_death;
    329  1.1  cgd {
    330  1.1  cgd 	short ch, c, d;
    331  1.1  cgd 	short row, col;
    332  1.1  cgd 	boolean first_miss = 1;
    333  1.1  cgd 	short possible_damage;
    334  1.1  cgd 	object *monster;
    335  1.1  cgd 
    336  1.1  cgd 	while (!is_direction(ch = rgetchar(), &d)) {
    337  1.1  cgd 		sound_bell();
    338  1.1  cgd 		if (first_miss) {
    339  1.1  cgd 			message("direction?", 0);
    340  1.1  cgd 			first_miss = 0;
    341  1.1  cgd 		}
    342  1.1  cgd 	}
    343  1.1  cgd 	check_message();
    344  1.1  cgd 	if (ch == CANCEL) {
    345  1.1  cgd 		return;
    346  1.1  cgd 	}
    347  1.1  cgd 	row = rogue.row; col = rogue.col;
    348  1.1  cgd 	get_dir_rc(d, &row, &col, 0);
    349  1.1  cgd 
    350  1.1  cgd 	c = mvinch(row, col);
    351  1.1  cgd 	if (((c < 'A') || (c > 'Z')) ||
    352  1.1  cgd 		(!can_move(rogue.row, rogue.col, row, col))) {
    353  1.1  cgd 		message("I see no monster there", 0);
    354  1.1  cgd 		return;
    355  1.1  cgd 	}
    356  1.1  cgd 	if (!(fight_monster = object_at(&level_monsters, row, col))) {
    357  1.1  cgd 		return;
    358  1.1  cgd 	}
    359  1.1  cgd 	if (!(fight_monster->m_flags & STATIONARY)) {
    360  1.1  cgd 		possible_damage = ((get_damage(fight_monster->m_damage, 0) * 2) / 3);
    361  1.1  cgd 	} else {
    362  1.1  cgd 		possible_damage = fight_monster->stationary_damage - 1;
    363  1.1  cgd 	}
    364  1.1  cgd 	while (fight_monster) {
    365  1.1  cgd 		(void) one_move_rogue(ch, 0);
    366  1.1  cgd 		if (((!to_the_death) && (rogue.hp_current <= possible_damage)) ||
    367  1.1  cgd 			interrupted || (!(dungeon[row][col] & MONSTER))) {
    368  1.1  cgd 			fight_monster = 0;
    369  1.1  cgd 		} else {
    370  1.1  cgd 			monster = object_at(&level_monsters, row, col);
    371  1.1  cgd 			if (monster != fight_monster) {
    372  1.1  cgd 				fight_monster = 0;
    373  1.1  cgd 			}
    374  1.1  cgd 		}
    375  1.1  cgd 	}
    376  1.1  cgd }
    377  1.1  cgd 
    378  1.1  cgd get_dir_rc(dir, row, col, allow_off_screen)
    379  1.1  cgd short dir;
    380  1.1  cgd short *row, *col;
    381  1.1  cgd short allow_off_screen;
    382  1.1  cgd {
    383  1.1  cgd 	switch(dir) {
    384  1.1  cgd 	case LEFT:
    385  1.1  cgd 		if (allow_off_screen || (*col > 0)) {
    386  1.1  cgd 			(*col)--;
    387  1.1  cgd 		}
    388  1.1  cgd 		break;
    389  1.1  cgd 	case DOWN:
    390  1.1  cgd 		if (allow_off_screen || (*row < (DROWS-2))) {
    391  1.1  cgd 			(*row)++;
    392  1.1  cgd 		}
    393  1.1  cgd 		break;
    394  1.1  cgd 	case UPWARD:
    395  1.1  cgd 		if (allow_off_screen || (*row > MIN_ROW)) {
    396  1.1  cgd 			(*row)--;
    397  1.1  cgd 		}
    398  1.1  cgd 		break;
    399  1.1  cgd 	case RIGHT:
    400  1.1  cgd 		if (allow_off_screen || (*col < (DCOLS-1))) {
    401  1.1  cgd 			(*col)++;
    402  1.1  cgd 		}
    403  1.1  cgd 		break;
    404  1.1  cgd 	case UPLEFT:
    405  1.1  cgd 		if (allow_off_screen || ((*row > MIN_ROW) && (*col > 0))) {
    406  1.1  cgd 			(*row)--;
    407  1.1  cgd 			(*col)--;
    408  1.1  cgd 		}
    409  1.1  cgd 		break;
    410  1.1  cgd 	case UPRIGHT:
    411  1.1  cgd 		if (allow_off_screen || ((*row > MIN_ROW) && (*col < (DCOLS-1)))) {
    412  1.1  cgd 			(*row)--;
    413  1.1  cgd 			(*col)++;
    414  1.1  cgd 		}
    415  1.1  cgd 		break;
    416  1.1  cgd 	case DOWNRIGHT:
    417  1.1  cgd 		if (allow_off_screen || ((*row < (DROWS-2)) && (*col < (DCOLS-1)))) {
    418  1.1  cgd 			(*row)++;
    419  1.1  cgd 			(*col)++;
    420  1.1  cgd 		}
    421  1.1  cgd 		break;
    422  1.1  cgd 	case DOWNLEFT:
    423  1.1  cgd 		if (allow_off_screen || ((*row < (DROWS-2)) && (*col > 0))) {
    424  1.1  cgd 			(*row)++;
    425  1.1  cgd 			(*col)--;
    426  1.1  cgd 		}
    427  1.1  cgd 		break;
    428  1.1  cgd 	}
    429  1.1  cgd }
    430  1.1  cgd 
    431  1.1  cgd get_hit_chance(weapon)
    432  1.1  cgd object *weapon;
    433  1.1  cgd {
    434  1.1  cgd 	short hit_chance;
    435  1.1  cgd 
    436  1.1  cgd 	hit_chance = 40;
    437  1.1  cgd 	hit_chance += 3 * to_hit(weapon);
    438  1.1  cgd 	hit_chance += (((2 * rogue.exp) + (2 * ring_exp)) - r_rings);
    439  1.1  cgd 	return(hit_chance);
    440  1.1  cgd }
    441  1.1  cgd 
    442  1.1  cgd get_weapon_damage(weapon)
    443  1.1  cgd object *weapon;
    444  1.1  cgd {
    445  1.1  cgd 	short damage;
    446  1.1  cgd 
    447  1.1  cgd 	damage = get_w_damage(weapon);
    448  1.1  cgd 	damage += damage_for_strength();
    449  1.1  cgd 	damage += ((((rogue.exp + ring_exp) - r_rings) + 1) / 2);
    450  1.1  cgd 	return(damage);
    451  1.1  cgd }
    452  1.1  cgd 
    453  1.1  cgd s_con_mon(monster)
    454  1.1  cgd object *monster;
    455  1.1  cgd {
    456  1.1  cgd 	if (con_mon) {
    457  1.1  cgd 		monster->m_flags |= CONFUSED;
    458  1.1  cgd 		monster->moves_confused += get_rand(12, 22);
    459  1.1  cgd 		message("the monster appears confused", 0);
    460  1.1  cgd 		con_mon = 0;
    461  1.1  cgd 	}
    462  1.1  cgd }
    463