Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / rogue / throw.c
1 /*
2  * Copyright (c) 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Timothy C. Stoehr.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)throw.c     8.1 (Berkeley) 5/31/93";
40 #endif
41 static const char rcsid[] =
42  "$FreeBSD: src/games/rogue/throw.c,v 1.3 1999/11/30 03:49:28 billf Exp $";
43 #endif /* not lint */
44
45 /*
46  * throw.c
47  *
48  * This source herein may be modified and/or distributed by anybody who
49  * so desires, with the following restrictions:
50  *    1.)  No portion of this notice shall be removed.
51  *    2.)  Credit shall not be taken for the creation of this source.
52  *    3.)  This code is not to be traded, sold, or used for personal
53  *         gain or profit.
54  *
55  */
56
57 #include "rogue.h"
58
59 extern short cur_room;
60 extern char *curse_message;
61 extern char hit_message[];
62
63 throw()
64 {
65         short wch, d;
66         boolean first_miss = 1;
67         object *weapon;
68         short dir, row, col;
69         object *monster;
70
71         while (!is_direction(dir = rgetchar(), &d)) {
72                 sound_bell();
73                 if (first_miss) {
74                         message("direction? ", 0);
75                         first_miss = 0;
76                 }
77         }
78         check_message();
79         if (dir == CANCEL) {
80                 return;
81         }
82         if ((wch = pack_letter("throw what?", WEAPON)) == CANCEL) {
83                 return;
84         }
85         check_message();
86
87         if (!(weapon = get_letter_object(wch))) {
88                 message("no such item.", 0);
89                 return;
90         }
91         if ((weapon->in_use_flags & BEING_USED) && weapon->is_cursed) {
92                 message(curse_message, 0);
93                 return;
94         }
95         row = rogue.row; col = rogue.col;
96
97         if ((weapon->in_use_flags & BEING_WIELDED) && (weapon->quantity <= 1)) {
98                 unwield(rogue.weapon);
99         } else if (weapon->in_use_flags & BEING_WORN) {
100                 mv_aquatars();
101                 unwear(rogue.armor);
102                 print_stats(STAT_ARMOR);
103         } else if (weapon->in_use_flags & ON_EITHER_HAND) {
104                 un_put_on(weapon);
105         }
106         monster = get_thrown_at_monster(weapon, d, &row, &col);
107         mvaddch(rogue.row, rogue.col, rogue.fchar);
108         refresh();
109
110         if (rogue_can_see(row, col) && ((row != rogue.row) || (col != rogue.col))){
111                 mvaddch(row, col, get_dungeon_char(row, col));
112         }
113         if (monster) {
114                 wake_up(monster);
115                 check_gold_seeker(monster);
116
117                 if (!throw_at_monster(monster, weapon)) {
118                         flop_weapon(weapon, row, col);
119                 }
120         } else {
121                 flop_weapon(weapon, row, col);
122         }
123         vanish(weapon, 1, &rogue.pack);
124 }
125
126 throw_at_monster(monster, weapon)
127 object *monster, *weapon;
128 {
129         short damage, hit_chance;
130         short t;
131
132         hit_chance = get_hit_chance(weapon);
133         damage = get_weapon_damage(weapon);
134         if ((weapon->which_kind == ARROW) &&
135                 (rogue.weapon && (rogue.weapon->which_kind == BOW))) {
136                 damage += get_weapon_damage(rogue.weapon);
137                 damage = ((damage * 2) / 3);
138                 hit_chance += (hit_chance / 3);
139         } else if ((weapon->in_use_flags & BEING_WIELDED) &&
140                 ((weapon->which_kind == DAGGER) ||
141                 (weapon->which_kind == SHURIKEN) ||
142                 (weapon->which_kind == DART))) {
143                 damage = ((damage * 3) / 2);
144                 hit_chance += (hit_chance / 3);
145         }
146         t = weapon->quantity;
147         weapon->quantity = 1;
148         sprintf(hit_message, "the %s", name_of(weapon));
149         weapon->quantity = t;
150
151         if (!rand_percent(hit_chance)) {
152                 (void) strcat(hit_message, "misses  ");
153                 return(0);
154         }
155         s_con_mon(monster);
156         (void) strcat(hit_message, "hit  ");
157         (void) mon_damage(monster, damage);
158         return(1);
159 }
160
161 object *
162 get_thrown_at_monster(obj, dir, row, col)
163 object *obj;
164 short dir;
165 short *row, *col;
166 {
167         short orow, ocol;
168         short i, ch;
169
170         orow = *row; ocol = *col;
171
172         ch = get_mask_char(obj->what_is);
173
174         for (i = 0; i < 24; i++) {
175                 get_dir_rc(dir, row, col, 0);
176                 if (    (((*col <= 0) || (*col >= DCOLS-1)) ||
177                                 (dungeon[*row][*col] == NOTHING)) ||
178                                 ((dungeon[*row][*col] & (HORWALL | VERTWALL | HIDDEN)) &&
179                                         (!(dungeon[*row][*col] & TRAP)))) {
180                         *row = orow;
181                         *col = ocol;
182                         return(0);
183                 }
184                 if ((i != 0) && rogue_can_see(orow, ocol)) {
185                         mvaddch(orow, ocol, get_dungeon_char(orow, ocol));
186                 }
187                 if (rogue_can_see(*row, *col)) {
188                         if (!(dungeon[*row][*col] & MONSTER)) {
189                                 mvaddch(*row, *col, ch);
190                         }
191                         refresh();
192                 }
193                 orow = *row; ocol = *col;
194                 if (dungeon[*row][*col] & MONSTER) {
195                         if (!imitating(*row, *col)) {
196                                 return(object_at(&level_monsters, *row, *col));
197                         }
198                 }
199                 if (dungeon[*row][*col] & TUNNEL) {
200                         i += 2;
201                 }
202         }
203         return(0);
204 }
205
206 flop_weapon(weapon, row, col)
207 object *weapon;
208 short row, col;
209 {
210         object *new_weapon, *monster;
211         short i = 0;
212         char msg[80];
213         boolean found = 0;
214         short mch, dch;
215         unsigned short mon;
216
217         while ((i < 9) && dungeon[row][col] & ~(FLOOR | TUNNEL | DOOR | MONSTER)) {
218                 rand_around(i++, &row, &col);
219                 if ((row > (DROWS-2)) || (row < MIN_ROW) ||
220                         (col > (DCOLS-1)) || (col < 0) || (!dungeon[row][col]) ||
221                         (dungeon[row][col] & ~(FLOOR | TUNNEL | DOOR | MONSTER))) {
222                         continue;
223                 }
224                 found = 1;
225                 break;
226         }
227
228         if (found || (i == 0)) {
229                 new_weapon = alloc_object();
230                 *new_weapon = *weapon;
231                 new_weapon->in_use_flags = NOT_USED;
232                 new_weapon->quantity = 1;
233                 new_weapon->ichar = 'L';
234                 place_at(new_weapon, row, col);
235                 if (rogue_can_see(row, col) &&
236                                 ((row != rogue.row) || (col != rogue.col))) {
237                         mon = dungeon[row][col] & MONSTER;
238                         dungeon[row][col] &= (~MONSTER);
239                         dch = get_dungeon_char(row, col);
240                         if (mon) {
241                                 mch = mvinch(row, col);
242                                 if (monster = object_at(&level_monsters, row, col)) {
243                                         monster->trail_char = dch;
244                                 }
245                                 if ((mch < 'A') || (mch > 'Z')) {
246                                         mvaddch(row, col, dch);
247                                 }
248                         } else {
249                                 mvaddch(row, col, dch);
250                         }
251                         dungeon[row][col] |= mon;
252                 }
253         } else {
254                 short t;
255
256                 t = weapon->quantity;
257                 weapon->quantity = 1;
258                 sprintf(msg, "the %svanishes as it hits the ground",
259                 name_of(weapon));
260                 weapon->quantity = t;
261                 message(msg, 0);
262         }
263 }
264
265 rand_around(i, r, c)
266 short i, *r, *c;
267 {
268         static char pos[] = "\010\007\001\003\004\005\002\006\0";
269         static short row, col;
270         short j;
271
272         if (i == 0) {
273                 short x, y, o, t;
274
275                 row = *r;
276                 col = *c;
277
278                 o = get_rand(1, 8);
279
280                 for (j = 0; j < 5; j++) {
281                         x = get_rand(0, 8);
282                         y = (x + o) % 9;
283                         t = pos[x];
284                         pos[x] = pos[y];
285                         pos[y] = t;
286                 }
287         }
288         switch((short)pos[i]) {
289         case 0:
290                 *r = row + 1;
291                 *c = col + 1;
292                 break;
293         case 1:
294                 *r = row + 1;
295                 *c = col - 1;
296                 break;
297         case 2:
298                 *r = row - 1;
299                 *c = col + 1;
300                 break;
301         case 3:
302                 *r = row - 1;
303                 *c = col - 1;
304                 break;
305         case 4:
306                 *r = row;
307                 *c = col + 1;
308                 break;
309         case 5:
310                 *r = row + 1;
311                 *c = col;
312                 break;
313         case 6:
314                 *r = row;
315                 *c = col;
316                 break;
317         case 7:
318                 *r = row - 1;
319                 *c = col;
320                 break;
321         case 8:
322                 *r = row;
323                 *c = col - 1;
324                 break;
325         }
326 }