]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/throw.c
ANSIfy function declarations.x
[bsdgames-darwin.git] / rogue / throw.c
1 /* $NetBSD: throw.c,v 1.10 2008/01/14 03:50:03 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Timothy C. Stoehr.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)throw.c 8.1 (Berkeley) 5/31/93";
39 #else
40 __RCSID("$NetBSD: throw.c,v 1.10 2008/01/14 03:50:03 dholland Exp $");
41 #endif
42 #endif /* not lint */
43
44 /*
45 * throw.c
46 *
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
52 * gain or profit.
53 *
54 */
55
56 #include "rogue.h"
57
58 void
59 throw(void)
60 {
61 short wch, d;
62 boolean first_miss = 1;
63 object *weapon;
64 short dir, row, col;
65 object *monster;
66
67 while (!is_direction(dir = rgetchar(), &d)) {
68 sound_bell();
69 if (first_miss) {
70 messagef(0, "direction? ");
71 first_miss = 0;
72 }
73 }
74 check_message();
75 if (dir == CANCEL) {
76 return;
77 }
78 if ((wch = pack_letter("throw what?", WEAPON)) == CANCEL) {
79 return;
80 }
81 check_message();
82
83 if (!(weapon = get_letter_object(wch))) {
84 messagef(0, "no such item.");
85 return;
86 }
87 if ((weapon->in_use_flags & BEING_USED) && weapon->is_cursed) {
88 messagef(0, curse_message);
89 return;
90 }
91 row = rogue.row; col = rogue.col;
92
93 if ((weapon->in_use_flags & BEING_WIELDED) && (weapon->quantity <= 1)) {
94 unwield(rogue.weapon);
95 } else if (weapon->in_use_flags & BEING_WORN) {
96 mv_aquatars();
97 unwear(rogue.armor);
98 print_stats(STAT_ARMOR);
99 } else if (weapon->in_use_flags & ON_EITHER_HAND) {
100 un_put_on(weapon);
101 }
102 monster = get_thrown_at_monster(weapon, d, &row, &col);
103 mvaddch(rogue.row, rogue.col, rogue.fchar);
104 refresh();
105
106 if (rogue_can_see(row, col) && ((row != rogue.row) || (col != rogue.col))){
107 mvaddch(row, col, get_dungeon_char(row, col));
108 }
109 if (monster) {
110 wake_up(monster);
111 check_gold_seeker(monster);
112
113 if (!throw_at_monster(monster, weapon)) {
114 flop_weapon(weapon, row, col);
115 }
116 } else {
117 flop_weapon(weapon, row, col);
118 }
119 vanish(weapon, 1, &rogue.pack);
120 }
121
122 boolean
123 throw_at_monster(object *monster, object *weapon)
124 {
125 short damage, hit_chance;
126 short t;
127
128 hit_chance = get_hit_chance(weapon);
129 damage = get_weapon_damage(weapon);
130 if ((weapon->which_kind == ARROW) &&
131 (rogue.weapon && (rogue.weapon->which_kind == BOW))) {
132 damage += get_weapon_damage(rogue.weapon);
133 damage = ((damage * 2) / 3);
134 hit_chance += (hit_chance / 3);
135 } else if ((weapon->in_use_flags & BEING_WIELDED) &&
136 ((weapon->which_kind == DAGGER) ||
137 (weapon->which_kind == SHURIKEN) ||
138 (weapon->which_kind == DART))) {
139 damage = ((damage * 3) / 2);
140 hit_chance += (hit_chance / 3);
141 }
142 t = weapon->quantity;
143 weapon->quantity = 1;
144 snprintf(hit_message, HIT_MESSAGE_SIZE, "the %s", name_of(weapon));
145 weapon->quantity = t;
146
147 if (!rand_percent(hit_chance)) {
148 (void)strlcat(hit_message, "misses ", HIT_MESSAGE_SIZE);
149 return(0);
150 }
151 s_con_mon(monster);
152 (void)strlcat(hit_message, "hit ", HIT_MESSAGE_SIZE);
153 (void)mon_damage(monster, damage);
154 return(1);
155 }
156
157 object *
158 get_thrown_at_monster(object *obj, short dir, short *row, short *col)
159 {
160 short orow, ocol;
161 short i, ch;
162
163 orow = *row; ocol = *col;
164
165 ch = get_mask_char(obj->what_is);
166
167 for (i = 0; i < 24; i++) {
168 get_dir_rc(dir, row, col, 0);
169 if ( (((*col <= 0) || (*col >= DCOLS-1)) ||
170 (dungeon[*row][*col] == NOTHING)) ||
171 ((dungeon[*row][*col] & (HORWALL | VERTWALL | HIDDEN)) &&
172 (!(dungeon[*row][*col] & TRAP)))) {
173 *row = orow;
174 *col = ocol;
175 return(0);
176 }
177 if ((i != 0) && rogue_can_see(orow, ocol)) {
178 mvaddch(orow, ocol, get_dungeon_char(orow, ocol));
179 }
180 if (rogue_can_see(*row, *col)) {
181 if (!(dungeon[*row][*col] & MONSTER)) {
182 mvaddch(*row, *col, ch);
183 }
184 refresh();
185 }
186 orow = *row; ocol = *col;
187 if (dungeon[*row][*col] & MONSTER) {
188 if (!imitating(*row, *col)) {
189 return(object_at(&level_monsters, *row, *col));
190 }
191 }
192 if (dungeon[*row][*col] & TUNNEL) {
193 i += 2;
194 }
195 }
196 return(0);
197 }
198
199 void
200 flop_weapon(object *weapon, short row, short col)
201 {
202 object *new_weapon, *monster;
203 short i = 0;
204 boolean found = 0;
205 short mch, dch;
206 unsigned short mon;
207
208 if ((row < 0) || (row >= DROWS) || (col < 0) || (col >= DCOLS))
209 clean_up("flop_weapon: weapon landed outside of dungeon");
210
211 while ((i < 9) && dungeon[row][col] & ~(FLOOR | TUNNEL | DOOR | MONSTER)) {
212 rand_around(i++, &row, &col);
213 if ((row > (DROWS-2)) || (row < MIN_ROW) ||
214 (col > (DCOLS-1)) || (col < 0) || (!dungeon[row][col]) ||
215 (dungeon[row][col] & ~(FLOOR | TUNNEL | DOOR | MONSTER))) {
216 continue;
217 }
218 found = 1;
219 break;
220 }
221
222 if (found || (i == 0)) {
223 new_weapon = alloc_object();
224 *new_weapon = *weapon;
225 new_weapon->in_use_flags = NOT_USED;
226 new_weapon->quantity = 1;
227 new_weapon->ichar = 'L';
228 place_at(new_weapon, row, col);
229 if (rogue_can_see(row, col) &&
230 ((row != rogue.row) || (col != rogue.col))) {
231 mon = dungeon[row][col] & MONSTER;
232 dungeon[row][col] &= (~MONSTER);
233 dch = get_dungeon_char(row, col);
234 if (mon) {
235 mch = mvinch(row, col);
236 if ((monster = object_at(&level_monsters,
237 row, col)) != NULL) {
238 monster->trail_char = dch;
239 }
240 if ((mch < 'A') || (mch > 'Z')) {
241 mvaddch(row, col, dch);
242 }
243 } else {
244 mvaddch(row, col, dch);
245 }
246 dungeon[row][col] |= mon;
247 }
248 } else {
249 short t;
250
251 t = weapon->quantity;
252 weapon->quantity = 1;
253 messagef(0, "the %svanishes as it hits the ground",
254 name_of(weapon));
255 weapon->quantity = t;
256 }
257 }
258
259 void
260 rand_around(short i, short *r, short *c)
261 {
262 static char pos[] = "\010\007\001\003\004\005\002\006\0";
263 static short row, col;
264 short j;
265
266 if (i == 0) {
267 short x, y, o, t;
268
269 row = *r;
270 col = *c;
271
272 o = get_rand(1, 8);
273
274 for (j = 0; j < 5; j++) {
275 x = get_rand(0, 8);
276 y = (x + o) % 9;
277 t = pos[x];
278 pos[x] = pos[y];
279 pos[y] = t;
280 }
281 }
282 switch((short)pos[i]) {
283 case 0:
284 *r = row + 1;
285 *c = col + 1;
286 break;
287 case 1:
288 *r = row + 1;
289 *c = col - 1;
290 break;
291 case 2:
292 *r = row - 1;
293 *c = col + 1;
294 break;
295 case 3:
296 *r = row - 1;
297 *c = col - 1;
298 break;
299 case 4:
300 *r = row;
301 *c = col + 1;
302 break;
303 case 5:
304 *r = row + 1;
305 *c = col;
306 break;
307 case 6:
308 *r = row;
309 *c = col;
310 break;
311 case 7:
312 *r = row - 1;
313 *c = col;
314 break;
315 case 8:
316 *r = row;
317 *c = col - 1;
318 break;
319 }
320 }