]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - games/hack/hack.trap.c
This commit was generated by cvs2svn to compensate for changes in r69626,
[FreeBSD/FreeBSD.git] / games / hack / hack.trap.c
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.trap.c - version 1.0.3 */
3 /* $FreeBSD$ */
4
5 #include        "hack.h"
6
7 extern struct monst *makemon();
8
9 char vowels[] = "aeiou";
10
11 char *traps[] = {
12         " bear trap",
13         "n arrow trap",
14         " dart trap",
15         " trapdoor",
16         " teleportation trap",
17         " pit",
18         " sleeping gas trap",
19         " piercer",
20         " mimic"
21 };
22
23 struct trap *
24 maketrap(x,y,typ)
25 int x,y,typ;
26 {
27         struct trap *ttmp;
28
29         ttmp = newtrap();
30         ttmp->ttyp = typ;
31         ttmp->tseen = 0;
32         ttmp->once = 0;
33         ttmp->tx = x;
34         ttmp->ty = y;
35         ttmp->ntrap = ftrap;
36         ftrap = ttmp;
37         return(ttmp);
38 }
39
40 dotrap(trap) struct trap *trap; {
41         int ttype = trap->ttyp;
42
43         nomul(0);
44         if(trap->tseen && !rn2(5) && ttype != PIT)
45                 pline("You escape a%s.", traps[ttype]);
46         else {
47                 trap->tseen = 1;
48                 switch(ttype) {
49                 case SLP_GAS_TRAP:
50                         pline("A cloud of gas puts you to sleep!");
51                         nomul(-rnd(25));
52                         break;
53                 case BEAR_TRAP:
54                         if(Levitation) {
55                                 pline("You float over a bear trap.");
56                                 break;
57                         }
58                         u.utrap = 4 + rn2(4);
59                         u.utraptype = TT_BEARTRAP;
60                         pline("A bear trap closes on your foot!");
61                         break;
62                 case PIERC:
63                         deltrap(trap);
64                         if(makemon(PM_PIERCER,u.ux,u.uy)) {
65                           pline("A piercer suddenly drops from the ceiling!");
66                           if(uarmh)
67                                 pline("Its blow glances off your helmet.");
68                           else
69                                 (void) thitu(3,d(4,6),"falling piercer");
70                         }
71                         break;
72                 case ARROW_TRAP:
73                         pline("An arrow shoots out at you!");
74                         if(!thitu(8,rnd(6),"arrow")){
75                                 mksobj_at(ARROW, u.ux, u.uy);
76                                 fobj->quan = 1;
77                         }
78                         break;
79                 case TRAPDOOR:
80                         if(!xdnstair) {
81 pline("A trap door in the ceiling opens and a rock falls on your head!");
82 if(uarmh) pline("Fortunately, you are wearing a helmet!");
83                             losehp(uarmh ? 2 : d(2,10),"falling rock");
84                             mksobj_at(ROCK, u.ux, u.uy);
85                             fobj->quan = 1;
86                             stackobj(fobj);
87                             if(Invisible) newsym(u.ux, u.uy);
88                         } else {
89                             int newlevel = dlevel + 1;
90                                 while(!rn2(4) && newlevel < 29)
91                                         newlevel++;
92                                 pline("A trap door opens up under you!");
93                                 if(Levitation || u.ustuck) {
94                                 pline("For some reason you don't fall in.");
95                                         break;
96                                 }
97
98                                 goto_level(newlevel, FALSE);
99                         }
100                         break;
101                 case DART_TRAP:
102                         pline("A little dart shoots out at you!");
103                         if(thitu(7,rnd(3),"little dart")) {
104                             if(!rn2(6))
105                                 poisoned("dart","poison dart");
106                         } else {
107                                 mksobj_at(DART, u.ux, u.uy);
108                                 fobj->quan = 1;
109                         }
110                         break;
111                 case TELEP_TRAP:
112                         if(trap->once) {
113                                 deltrap(trap);
114                                 newsym(u.ux,u.uy);
115                                 vtele();
116                         } else {
117                                 newsym(u.ux,u.uy);
118                                 tele();
119                         }
120                         break;
121                 case PIT:
122                         if(Levitation) {
123                                 pline("A pit opens up under you!");
124                                 pline("You don't fall in!");
125                                 break;
126                         }
127                         pline("You fall into a pit!");
128                         u.utrap = rn1(6,2);
129                         u.utraptype = TT_PIT;
130                         losehp(rnd(6),"fall into a pit");
131                         selftouch("Falling, you");
132                         break;
133                 default:
134                         impossible("You hit a trap of type %u", trap->ttyp);
135                 }
136         }
137 }
138
139 mintrap(mtmp) struct monst *mtmp; {
140         struct trap *trap = t_at(mtmp->mx, mtmp->my);
141         int wasintrap = mtmp->mtrapped;
142
143         if(!trap) {
144                 mtmp->mtrapped = 0;     /* perhaps teleported? */
145         } else if(wasintrap) {
146                 if(!rn2(40)) mtmp->mtrapped = 0;
147         } else {
148             int tt = trap->ttyp;
149             int in_sight = cansee(mtmp->mx,mtmp->my);
150             extern char mlarge[];
151
152             if(mtmp->mtrapseen & (1 << tt)) {
153                 /* he has been in such a trap - perhaps he escapes */
154                 if(rn2(4)) return(0);
155             }
156             mtmp->mtrapseen |= (1 << tt);
157             switch (tt) {
158                 case BEAR_TRAP:
159                         if(index(mlarge, mtmp->data->mlet)) {
160                                 if(in_sight)
161                                   pline("%s is caught in a bear trap!",
162                                         Monnam(mtmp));
163                                 else
164                                   if(mtmp->data->mlet == 'o')
165                             pline("You hear the roaring of an angry bear!");
166                                 mtmp->mtrapped = 1;
167                         }
168                         break;
169                 case PIT:
170                         /* there should be a mtmp/data -> floating */
171                         if(!index("EywBfk'& ", mtmp->data->mlet)) { /* ab */
172                                 mtmp->mtrapped = 1;
173                                 if(in_sight)
174                                   pline("%s falls in a pit!", Monnam(mtmp));
175                         }
176                         break;
177                 case SLP_GAS_TRAP:
178                         if(!mtmp->msleep && !mtmp->mfroz) {
179                                 mtmp->msleep = 1;
180                                 if(in_sight)
181                                   pline("%s suddenly falls asleep!",
182                                         Monnam(mtmp));
183                         }
184                         break;
185                 case TELEP_TRAP:
186                         rloc(mtmp);
187                         if(in_sight && !cansee(mtmp->mx,mtmp->my))
188                                 pline("%s suddenly disappears!",
189                                         Monnam(mtmp));
190                         break;
191                 case ARROW_TRAP:
192                         if(in_sight) {
193                                 pline("%s is hit by an arrow!",
194                                         Monnam(mtmp));
195                         }
196                         mtmp->mhp -= 3;
197                         break;
198                 case DART_TRAP:
199                         if(in_sight) {
200                                 pline("%s is hit by a dart!",
201                                         Monnam(mtmp));
202                         }
203                         mtmp->mhp -= 2;
204                         /* not mondied here !! */
205                         break;
206                 case TRAPDOOR:
207                         if(!xdnstair) {
208                                 mtmp->mhp -= 10;
209                                 if(in_sight)
210 pline("A trap door in the ceiling opens and a rock hits %s!", monnam(mtmp));
211                                 break;
212                         }
213                         if(mtmp->data->mlet != 'w'){
214                                 fall_down(mtmp);
215                                 if(in_sight)
216                 pline("Suddenly, %s disappears out of sight.", monnam(mtmp));
217                                 return(2);      /* no longer on this level */
218                         }
219                         break;
220                 case PIERC:
221                         break;
222                 default:
223                         impossible("Some monster encountered a strange trap.");
224             }
225         }
226         return(mtmp->mtrapped);
227 }
228
229 selftouch(arg) char *arg; {
230         if(uwep && uwep->otyp == DEAD_COCKATRICE){
231                 pline("%s touch the dead cockatrice.", arg);
232                 pline("You turn to stone.");
233                 killer = objects[uwep->otyp].oc_name;
234                 done("died");
235         }
236 }
237
238 float_up(){
239         if(u.utrap) {
240                 if(u.utraptype == TT_PIT) {
241                         u.utrap = 0;
242                         pline("You float up, out of the pit!");
243                 } else {
244                         pline("You float up, only your leg is still stuck.");
245                 }
246         } else
247                 pline("You start to float in the air!");
248 }
249
250 float_down(){
251         struct trap *trap;
252         pline("You float gently to the ground.");
253         if(trap = t_at(u.ux,u.uy))
254                 switch(trap->ttyp) {
255                 case PIERC:
256                         break;
257                 case TRAPDOOR:
258                         if(!xdnstair || u.ustuck) break;
259                         /* fall into next case */
260                 default:
261                         dotrap(trap);
262         }
263         pickup(1);
264 }
265
266 vtele() {
267 #include "def.mkroom.h"
268         struct mkroom *croom;
269         for(croom = &rooms[0]; croom->hx >= 0; croom++)
270             if(croom->rtype == VAULT) {
271                 int x,y;
272
273                 x = rn2(2) ? croom->lx : croom->hx;
274                 y = rn2(2) ? croom->ly : croom->hy;
275                 if(teleok(x,y)) {
276                     teleds(x,y);
277                     return;
278                 }
279             }
280         tele();
281 }
282
283 tele() {
284         extern coord getpos();
285         coord cc;
286         int nux,nuy;
287
288         if(Teleport_control) {
289                 pline("To what position do you want to be teleported?");
290                 cc = getpos(1, "the desired position"); /* 1: force valid */
291                 /* possible extensions: introduce a small error if
292                    magic power is low; allow transfer to solid rock */
293                 if(teleok(cc.x, cc.y)){
294                         teleds(cc.x, cc.y);
295                         return;
296                 }
297                 pline("Sorry ...");
298         }
299         do {
300                 nux = rnd(COLNO-1);
301                 nuy = rn2(ROWNO);
302         } while(!teleok(nux, nuy));
303         teleds(nux, nuy);
304 }
305
306 teleds(nux, nuy)
307 int nux,nuy;
308 {
309         if(Punished) unplacebc();
310         unsee();
311         u.utrap = 0;
312         u.ustuck = 0;
313         u.ux = nux;
314         u.uy = nuy;
315         setsee();
316         if(Punished) placebc(1);
317         if(u.uswallow){
318                 u.uswldtim = u.uswallow = 0;
319                 docrt();
320         }
321         nomul(0);
322         if(levl[nux][nuy].typ == POOL && !Levitation)
323                 drown();
324         (void) inshop();
325         pickup(1);
326         if(!Blind) read_engr_at(u.ux,u.uy);
327 }
328
329 teleok(x,y) int x,y; {  /* might throw him into a POOL */
330         return( isok(x,y) && !IS_ROCK(levl[x][y].typ) && !m_at(x,y) &&
331                 !sobj_at(ENORMOUS_ROCK,x,y) && !t_at(x,y)
332         );
333         /* Note: gold is permitted (because of vaults) */
334 }
335
336 dotele() {
337         extern char pl_character[];
338
339         if(
340 #ifdef WIZARD
341            !wizard &&
342 #endif WIZARD
343                       (!Teleportation || u.ulevel < 6 ||
344                         (pl_character[0] != 'W' && u.ulevel < 10))) {
345                 pline("You are not able to teleport at will.");
346                 return(0);
347         }
348         if(u.uhunger <= 100 || u.ustr < 6) {
349                 pline("You miss the strength for a teleport spell.");
350                 return(1);
351         }
352         tele();
353         morehungry(100);
354         return(1);
355 }
356
357 placebc(attach) int attach; {
358         if(!uchain || !uball){
359                 impossible("Where are your chain and ball??");
360                 return;
361         }
362         uball->ox = uchain->ox = u.ux;
363         uball->oy = uchain->oy = u.uy;
364         if(attach){
365                 uchain->nobj = fobj;
366                 fobj = uchain;
367                 if(!carried(uball)){
368                         uball->nobj = fobj;
369                         fobj = uball;
370                 }
371         }
372 }
373
374 unplacebc(){
375         if(!carried(uball)){
376                 freeobj(uball);
377                 unpobj(uball);
378         }
379         freeobj(uchain);
380         unpobj(uchain);
381 }
382
383 level_tele() {
384 int newlevel;
385         if(Teleport_control) {
386             char buf[BUFSZ];
387
388             do {
389               pline("To what level do you want to teleport? [type a number] ");
390               getlin(buf);
391             } while(!digit(buf[0]) && (buf[0] != '-' || !digit(buf[1])));
392             newlevel = atoi(buf);
393         } else {
394             newlevel  = 5 + rn2(20);    /* 5 - 24 */
395             if(dlevel == newlevel) {
396                 if(!xdnstair) newlevel--; else newlevel++;
397             }
398         }
399         if(newlevel >= 30) {
400             if(newlevel > MAXLEVEL) newlevel = MAXLEVEL;
401             pline("You arrive at the center of the earth ...");
402             pline("Unfortunately it is here that hell is located.");
403             if(Fire_resistance) {
404                 pline("But the fire doesn't seem to harm you.");
405             } else {
406                 pline("You burn to a crisp.");
407                 dlevel = maxdlevel = newlevel;
408                 killer = "visit to the hell";
409                 done("burned");
410             }
411         }
412         if(newlevel < 0) {
413             newlevel = 0;
414             pline("You are now high above the clouds ...");
415             if(Levitation) {
416                 pline("You float gently down to earth.");
417                 done("escaped");
418             }
419             pline("Unfortunately, you don't know how to fly.");
420             pline("You fall down a few thousand feet and break your neck.");
421             dlevel = 0;
422             killer = "fall";
423             done("died");
424         }
425
426         goto_level(newlevel, FALSE); /* calls done("escaped") if newlevel==0 */
427 }
428
429 drown()
430 {
431         pline("You fall into a pool!");
432         pline("You can't swim!");
433         if(rn2(3) < u.uluck+2) {
434                 /* most scrolls become unreadable */
435                 struct obj *obj;
436
437                 for(obj = invent; obj; obj = obj->nobj)
438                         if(obj->olet == SCROLL_SYM && rn2(12) > u.uluck)
439                                 obj->otyp = SCR_BLANK_PAPER;
440                 /* we should perhaps merge these scrolls ? */
441
442                 pline("You attempt a teleport spell."); /* utcsri!carroll */
443                 (void) dotele();
444                 if(levl[u.ux][u.uy].typ != POOL) return;
445         }
446         pline("You drown ...");
447         killer = "pool of water";
448         done("drowned");
449 }