Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / rogue / spec_hit.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[] = "@(#)spec_hit.c  8.1 (Berkeley) 5/31/93";
40 #endif
41 static const char rcsid[] =
42  "$FreeBSD: src/games/rogue/spec_hit.c,v 1.4 1999/11/30 03:49:28 billf Exp $";
43 #endif /* not lint */
44
45 /*
46  * special_hit.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 short less_hp = 0;
60 boolean being_held;
61
62 extern short cur_level, max_level, blind, levitate, ring_exp;
63 extern long level_points[];
64 extern boolean detect_monster, mon_disappeared;
65 extern boolean sustain_strength, maintain_armor;
66 extern char *you_can_move_again;
67
68 special_hit(monster)
69 object *monster;
70 {
71         if ((monster->m_flags & CONFUSED) && rand_percent(66)) {
72                 return;
73         }
74         if (monster->m_flags & RUSTS) {
75                 rust(monster);
76         }
77         if ((monster->m_flags & HOLDS) && !levitate) {
78                 being_held = 1;
79         }
80         if (monster->m_flags & FREEZES) {
81                 freeze(monster);
82         }
83         if (monster->m_flags & STINGS) {
84                 sting(monster);
85         }
86         if (monster->m_flags & DRAINS_LIFE) {
87                 drain_life();
88         }
89         if (monster->m_flags & DROPS_LEVEL) {
90                 drop_level();
91         }
92         if (monster->m_flags & STEALS_GOLD) {
93                 steal_gold(monster);
94         } else if (monster->m_flags & STEALS_ITEM) {
95                 steal_item(monster);
96         }
97 }
98
99 rust(monster)
100 object *monster;
101 {
102         if ((!rogue.armor) || (get_armor_class(rogue.armor) <= 1) ||
103                 (rogue.armor->which_kind == LEATHER)) {
104                 return;
105         }
106         if ((rogue.armor->is_protected) || maintain_armor) {
107                 if (monster && (!(monster->m_flags & RUST_VANISHED))) {
108                         message("the rust vanishes instantly", 0);
109                         monster->m_flags |= RUST_VANISHED;
110                 }
111         } else {
112                 rogue.armor->d_enchant--;
113                 message("your armor weakens", 0);
114                 print_stats(STAT_ARMOR);
115         }
116 }
117
118 freeze(monster)
119 object *monster;
120 {
121         short freeze_percent = 99;
122         short i, n;
123
124         if (rand_percent(12)) {
125                 return;
126         }
127         freeze_percent -= (rogue.str_current+(rogue.str_current / 2));
128         freeze_percent -= ((rogue.exp + ring_exp) * 4);
129         freeze_percent -= (get_armor_class(rogue.armor) * 5);
130         freeze_percent -= (rogue.hp_max / 3);
131
132         if (freeze_percent > 10) {
133                 monster->m_flags |= FREEZING_ROGUE;
134                 message("you are frozen", 1);
135
136                 n = get_rand(4, 8);
137                 for (i = 0; i < n; i++) {
138                         mv_mons();
139                 }
140                 if (rand_percent(freeze_percent)) {
141                         for (i = 0; i < 50; i++) {
142                                 mv_mons();
143                         }
144                         killed_by((object *)0, HYPOTHERMIA);
145                 }
146                 message(you_can_move_again, 1);
147                 monster->m_flags &= (~FREEZING_ROGUE);
148         }
149 }
150
151 steal_gold(monster)
152 object *monster;
153 {
154         int amount;
155
156         if ((rogue.gold <= 0) || rand_percent(10)) {
157                 return;
158         }
159
160         amount = get_rand((cur_level * 10), (cur_level * 30));
161
162         if (amount > rogue.gold) {
163                 amount = rogue.gold;
164         }
165         rogue.gold -= amount;
166         message("your purse feels lighter", 0);
167         print_stats(STAT_GOLD);
168         disappear(monster);
169 }
170
171 steal_item(monster)
172 object *monster;
173 {
174         object *obj;
175         short i, n, t;
176         char desc[80];
177         boolean has_something = 0;
178
179         if (rand_percent(15)) {
180                 return;
181         }
182         obj = rogue.pack.next_object;
183
184         if (!obj) {
185                 goto DSPR;
186         }
187         while (obj) {
188                 if (!(obj->in_use_flags & BEING_USED)) {
189                         has_something = 1;
190                         break;
191                 }
192                 obj = obj->next_object;
193         }
194         if (!has_something) {
195                 goto DSPR;
196         }
197         n = get_rand(0, MAX_PACK_COUNT);
198         obj = rogue.pack.next_object;
199
200         for (i = 0; i <= n; i++) {
201                 obj = obj->next_object;
202                 while ((!obj) || (obj->in_use_flags & BEING_USED)) {
203                         if (!obj) {
204                                 obj = rogue.pack.next_object;
205                         } else {
206                                 obj = obj->next_object;
207                         }
208                 }
209         }
210         (void) strcpy(desc, "she stole ");
211         if (obj->what_is != WEAPON) {
212                 t = obj->quantity;
213                 obj->quantity = 1;
214         }
215         get_desc(obj, desc+10);
216         message(desc, 0);
217
218         obj->quantity = ((obj->what_is != WEAPON) ? t : 1);
219
220         vanish(obj, 0, &rogue.pack);
221 DSPR:
222         disappear(monster);
223 }
224
225 disappear(monster)
226 object *monster;
227 {
228         short row, col;
229
230         row = monster->row;
231         col = monster->col;
232
233         dungeon[row][col] &= ~MONSTER;
234         if (rogue_can_see(row, col)) {
235                 mvaddch(row, col, get_dungeon_char(row, col));
236         }
237         take_from_pack(monster, &level_monsters);
238         free_object(monster);
239         mon_disappeared = 1;
240 }
241
242 cough_up(monster)
243 object *monster;
244 {
245         object *obj;
246         short row, col, i, n;
247
248         if (cur_level < max_level) {
249                 return;
250         }
251
252         if (monster->m_flags & STEALS_GOLD) {
253                 obj = alloc_object();
254                 obj->what_is = GOLD;
255                 obj->quantity = get_rand((cur_level * 15), (cur_level * 30));
256         } else {
257                 if (!rand_percent((int) monster->drop_percent)) {
258                         return;
259                 }
260                 obj = gr_object();
261         }
262         row = monster->row;
263         col = monster->col;
264
265         for (n = 0; n <= 5; n++) {
266                 for (i = -n; i <= n; i++) {
267                         if (try_to_cough(row+n, col+i, obj)) {
268                                 return;
269                         }
270                         if (try_to_cough(row-n, col+i, obj)) {
271                                 return;
272                         }
273                 }
274                 for (i = -n; i <= n; i++) {
275                         if (try_to_cough(row+i, col-n, obj)) {
276                                 return;
277                         }
278                         if (try_to_cough(row+i, col+n, obj)) {
279                                 return;
280                         }
281                 }
282         }
283         free_object(obj);
284 }
285
286 try_to_cough(row, col, obj)
287 short row, col;
288 object *obj;
289 {
290         if ((row < MIN_ROW) || (row > (DROWS-2)) || (col < 0) || (col>(DCOLS-1))) {
291                 return(0);
292         }
293         if ((!(dungeon[row][col] & (OBJECT | STAIRS | TRAP))) &&
294                 (dungeon[row][col] & (TUNNEL | FLOOR | DOOR))) {
295                 place_at(obj, row, col);
296                 if (((row != rogue.row) || (col != rogue.col)) &&
297                         (!(dungeon[row][col] & MONSTER))) {
298                         mvaddch(row, col, get_dungeon_char(row, col));
299                 }
300                 return(1);
301         }
302         return(0);
303 }
304
305 seek_gold(monster)
306 object *monster;
307 {
308         short i, j, rn, s;
309
310         if ((rn = get_room_number(monster->row, monster->col)) < 0) {
311                 return(0);
312         }
313         for (i = rooms[rn].top_row+1; i < rooms[rn].bottom_row; i++) {
314                 for (j = rooms[rn].left_col+1; j < rooms[rn].right_col; j++) {
315                         if ((gold_at(i, j)) && !(dungeon[i][j] & MONSTER)) {
316                                 monster->m_flags |= CAN_FLIT;
317                                 s = mon_can_go(monster, i, j);
318                                 monster->m_flags &= (~CAN_FLIT);
319                                 if (s) {
320                                         move_mon_to(monster, i, j);
321                                         monster->m_flags |= ASLEEP;
322                                         monster->m_flags &= (~(WAKENS | SEEKS_GOLD));
323                                         return(1);
324                                 }
325                                 monster->m_flags &= (~SEEKS_GOLD);
326                                 monster->m_flags |= CAN_FLIT;
327                                 mv_1_monster(monster, i, j);
328                                 monster->m_flags &= (~CAN_FLIT);
329                                 monster->m_flags |= SEEKS_GOLD;
330                                 return(1);
331                         }
332                 }
333         }
334         return(0);
335 }
336
337 gold_at(row, col)
338 short row, col;
339 {
340         if (dungeon[row][col] & OBJECT) {
341                 object *obj;
342
343                 if ((obj = object_at(&level_objects, row, col)) &&
344                                 (obj->what_is == GOLD)) {
345                         return(1);
346                 }
347         }
348         return(0);
349 }
350
351 check_gold_seeker(monster)
352 object *monster;
353 {
354         monster->m_flags &= (~SEEKS_GOLD);
355 }
356
357 check_imitator(monster)
358 object *monster;
359 {
360         char msg[80];
361
362         if (monster->m_flags & IMITATES) {
363                 wake_up(monster);
364                 if (!blind) {
365                         mvaddch(monster->row, monster->col,
366                                         get_dungeon_char(monster->row, monster->col));
367                         check_message();
368                         sprintf(msg, "wait, that's a %s!", mon_name(monster));
369                         message(msg, 1);
370                 }
371                 return(1);
372         }
373         return(0);
374 }
375
376 imitating(row, col)
377 short row, col;
378 {
379         if (dungeon[row][col] & MONSTER) {
380                 object *object_at(), *monster;
381
382                 if (monster = object_at(&level_monsters, row, col)) {
383                         if (monster->m_flags & IMITATES) {
384                                 return(1);
385                         }
386                 }
387         }
388         return(0);
389 }
390
391 sting(monster)
392 object *monster;
393 {
394         short sting_chance = 35;
395         char msg[80];
396
397         if ((rogue.str_current <= 3) || sustain_strength) {
398                 return;
399         }
400         sting_chance += (6 * (6 - get_armor_class(rogue.armor)));
401
402         if ((rogue.exp + ring_exp) > 8) {
403                 sting_chance -= (6 * ((rogue.exp + ring_exp) - 8));
404         }
405         if (rand_percent(sting_chance)) {
406                 sprintf(msg, "the %s's bite has weakened you",
407                 mon_name(monster));
408                 message(msg, 0);
409                 rogue.str_current--;
410                 print_stats(STAT_STRENGTH);
411         }
412 }
413
414 drop_level()
415 {
416         int hp;
417
418         if (rand_percent(80) || (rogue.exp <= 5)) {
419                 return;
420         }
421         rogue.exp_points = level_points[rogue.exp-2] - get_rand(9, 29);
422         rogue.exp -= 2;
423         hp = hp_raise();
424         if ((rogue.hp_current -= hp) <= 0) {
425                 rogue.hp_current = 1;
426         }
427         if ((rogue.hp_max -= hp) <= 0) {
428                 rogue.hp_max = 1;
429         }
430         add_exp(1, 0);
431 }
432
433 drain_life()
434 {
435         short n;
436
437         if (rand_percent(60) || (rogue.hp_max <= 30) || (rogue.hp_current < 10)) {
438                 return;
439         }
440         n = get_rand(1, 3);             /* 1 Hp, 2 Str, 3 both */
441
442         if ((n != 2) || (!sustain_strength)) {
443                 message("you feel weaker", 0);
444         }
445         if (n != 2) {
446                 rogue.hp_max--;
447                 rogue.hp_current--;
448                 less_hp++;
449         }
450         if (n != 1) {
451                 if ((rogue.str_current > 3) && (!sustain_strength)) {
452                         rogue.str_current--;
453                         if (coin_toss()) {
454                                 rogue.str_max--;
455                         }
456                 }
457         }
458         print_stats((STAT_STRENGTH | STAT_HP));
459 }
460
461 m_confuse(monster)
462 object *monster;
463 {
464         char msg[80];
465
466         if (!rogue_can_see(monster->row, monster->col)) {
467                 return(0);
468         }
469         if (rand_percent(45)) {
470                 monster->m_flags &= (~CONFUSES);        /* will not confuse the rogue */
471                 return(0);
472         }
473         if (rand_percent(55)) {
474                 monster->m_flags &= (~CONFUSES);
475                 sprintf(msg, "the gaze of the %s has confused you", mon_name(monster));
476                 message(msg, 1);
477                 cnfs();
478                 return(1);
479         }
480         return(0);
481 }
482
483 flame_broil(monster)
484 object *monster;
485 {
486         short row, col, dir;
487
488         if ((!mon_sees(monster, rogue.row, rogue.col)) || coin_toss()) {
489                 return(0);
490         }
491         row = rogue.row - monster->row;
492         col = rogue.col - monster->col;
493         if (row < 0) {
494                 row = -row;
495         }
496         if (col < 0) {
497                 col = -col;
498         }
499         if (((row != 0) && (col != 0) && (row != col)) ||
500                 ((row > 7) || (col > 7))) {
501                 return(0);
502         }
503         dir = get_dir(monster->row, monster->col, row, col);
504         bounce(FIRE, dir, monster->row, monster->col, 0);
505
506         return(1);
507 }
508
509 get_dir(srow, scol, drow, dcol)
510 short srow, scol, drow, dcol;
511 {
512         if (srow == drow) {
513                 if (scol < dcol) {
514                         return(RIGHT);
515                 } else {
516                         return(LEFT);
517                 }
518         }
519         if (scol == dcol) {
520                 if (srow < drow) {
521                         return(DOWN);
522                 } else {
523                         return(UPWARD);
524                 }
525         }
526         if ((srow > drow) && (scol > dcol)) {
527                 return(UPLEFT);
528         }
529         if ((srow < drow) && (scol < dcol)) {
530                 return(DOWNRIGHT);
531         }
532         if ((srow < drow) && (scol > dcol)) {
533                 return(DOWNLEFT);
534         }
535         /*if ((srow > drow) && (scol < dcol)) {*/
536                 return(UPRIGHT);
537         /*}*/
538 }