[BACK]Return to misc.c CVS log [TXT][DIR] Up to [contributed] / early-roguelike / rogue4

Annotation of early-roguelike/rogue4/misc.c, Revision 1.1.1.1

1.1       rubenllo    1: /*
                      2:  * All sorts of miscellaneous routines
                      3:  *
                      4:  * @(#)misc.c  4.30 (Berkeley) 4/6/82
                      5:  *
                      6:  * Rogue: Exploring the Dungeons of Doom
                      7:  * Copyright (C) 1980, 1981, 1982 Michael Toy, Ken Arnold and Glenn Wichman
                      8:  * All rights reserved.
                      9:  *
                     10:  * See the file LICENSE.TXT for full copyright and licensing information.
                     11:  */
                     12:
                     13: #include <stdlib.h>
                     14: #include <curses.h>
                     15: #include <ctype.h>
                     16: #include <string.h>
                     17: #include "rogue.h"
                     18:
                     19: /*
                     20:  * tr_name:
                     21:  *     Print the name of a trap
                     22:  */
                     23: char *
                     24: tr_name(char type)
                     25: {
                     26:     switch (type)
                     27:     {
                     28:        case T_DOOR:
                     29:            return terse ? "a trapdoor" : "you found a trapdoor";
                     30:        case T_BEAR:
                     31:            return terse ? "a beartrap" : "you found a beartrap";
                     32:        case T_SLEEP:
                     33:            return terse ? "a sleeping gas trap":"you found a sleeping gas trap";
                     34:        case T_ARROW:
                     35:            return terse ? "an arrow trap" : "you found an arrow trap";
                     36:        case T_TELEP:
                     37:            return terse ? "a teleport trap" : "you found a teleport trap";
                     38:        case T_DART:
                     39:            return terse ? "a dart trap" : "you found a poison dart trap";
                     40:     }
                     41:     msg("wierd trap: %d", type);
                     42:     return NULL;
                     43: }
                     44:
                     45: /*
                     46:  * look:
                     47:  *     A quick glance all around the player
                     48:  */
                     49: void
                     50: look(bool wakeup)
                     51: {
                     52:     register int x, y;
                     53:     register unsigned char ch;
                     54:     register int index;
                     55:     register THING *tp;
                     56:     register struct room *rp;
                     57:     register int ey, ex;
                     58:     register int passcount = 0;
                     59:     register char pfl, *fp, pch;
                     60:     register int sy, sx, sumhero = 0, diffhero = 0;
                     61:     register int oldx, oldy;
                     62:
                     63:     getyx(stdscr, oldy, oldx);
                     64:     rp = proom;
                     65:     if (!ce(oldpos, hero))
                     66:     {
                     67:        if ((oldrp->r_flags & (ISGONE|ISDARK)) == ISDARK && !on(player,ISBLIND))
                     68:        {
                     69:            ey = oldpos.y + 1;
                     70:            ex = oldpos.x + 1;
                     71:            sy = oldpos.y - 1;
                     72:            for (x = oldpos.x - 1; x <= ex; x++)
                     73:                for (y = sy; y <= ey; y++)
                     74:                {
                     75:                    if (y == hero.y && x == hero.x)
                     76:                        continue;
                     77:                    move(y, x);
                     78:                    if (inch() == FLOOR)
                     79:                        addch(' ');
                     80:                }
                     81:        }
                     82:        oldpos = hero;
                     83:        oldrp = rp;
                     84:     }
                     85:     ey = hero.y + 1;
                     86:     ex = hero.x + 1;
                     87:     sx = hero.x - 1;
                     88:     sy = hero.y - 1;
                     89:     if (door_stop && !firstmove && running)
                     90:     {
                     91:        sumhero = hero.y + hero.x;
                     92:        diffhero = hero.y - hero.x;
                     93:     }
                     94:     index = INDEX(hero.y, hero.x);
                     95:     pfl = _flags[index];
                     96:     pch = _level[index];
                     97:     for (y = sy; y <= ey; y++)
                     98:        if (y > 0 && y < LINES - 1) for (x = sx; x <= ex; x++)
                     99:        {
                    100:            if (x <= 0 || x >= COLS)
                    101:                continue;
                    102:            if (!on(player, ISBLIND))
                    103:            {
                    104:                if (y == hero.y && x == hero.x)
                    105:                    continue;
                    106:            }
                    107:            else if (y != hero.y || x != hero.x)
                    108:                continue;
                    109:
                    110:            index = INDEX(y, x);
                    111:            /*
                    112:             * THIS REPLICATES THE moat() MACRO.  IF MOAT IS CHANGED,
                    113:             * THIS MUST BE CHANGED ALSO
                    114:             */
                    115:            fp = &_flags[index];
                    116:            ch = _level[index];
                    117:            if (pch != DOOR && ch != DOOR)
                    118:                if ((pfl & F_PASS) != (*fp & F_PASS))
                    119:                    continue;
                    120:                else if ((*fp & F_PASS) && (*fp & F_PNUM) != (pfl & F_PNUM))
                    121:                    continue;
                    122:
                    123:            if ((tp = _monst[index]) != NULL)
                    124:                if (on(player, SEEMONST) && on(*tp, ISINVIS))
                    125:                {
                    126:                    if (door_stop && !firstmove)
                    127:                        running = FALSE;
                    128:                    continue;
                    129:                }
                    130:                else
                    131:                {
                    132:                    if (wakeup)
                    133:                        wake_monster(y, x);
                    134:                    if (tp->t_oldch != ' ' ||
                    135:                        (!(rp->r_flags & ISDARK) && !on(player, ISBLIND)))
                    136:                            tp->t_oldch = _level[index];
                    137:                    if (see_monst(tp))
                    138:                        ch = tp->t_disguise;
                    139:                }
                    140:
                    141:            move(y, x);
                    142:            if (ch != inch())
                    143:                addch(ch);
                    144:
                    145:            if (door_stop && !firstmove && running)
                    146:            {
                    147:                switch (runch)
                    148:                {
                    149:                    case 'h':
                    150:                        if (x == ex)
                    151:                            continue;
                    152:                    when 'j':
                    153:                        if (y == sy)
                    154:                            continue;
                    155:                    when 'k':
                    156:                        if (y == ey)
                    157:                            continue;
                    158:                    when 'l':
                    159:                        if (x == sx)
                    160:                            continue;
                    161:                    when 'y':
                    162:                        if ((y + x) - sumhero >= 1)
                    163:                            continue;
                    164:                    when 'u':
                    165:                        if ((y - x) - diffhero >= 1)
                    166:                            continue;
                    167:                    when 'n':
                    168:                        if ((y + x) - sumhero <= -1)
                    169:                            continue;
                    170:                    when 'b':
                    171:                        if ((y - x) - diffhero <= -1)
                    172:                            continue;
                    173:                }
                    174:                switch (ch)
                    175:                {
                    176:                    case DOOR:
                    177:                        if (x == hero.x || y == hero.y)
                    178:                            running = FALSE;
                    179:                        break;
                    180:                    case PASSAGE:
                    181:                        if (x == hero.x || y == hero.y)
                    182:                            passcount++;
                    183:                        break;
                    184:                    case FLOOR:
                    185:                    case '|':
                    186:                    case '-':
                    187:                    case ' ':
                    188:                        break;
                    189:                    default:
                    190:                        running = FALSE;
                    191:                        break;
                    192:                }
                    193:            }
                    194:        }
                    195:     if (door_stop && !firstmove && passcount > 1)
                    196:        running = FALSE;
                    197:     move(hero.y, hero.x);
                    198:     addch(PLAYER);
                    199: }
                    200:
                    201: /*
                    202:  * find_obj:
                    203:  *     Find the unclaimed object at y, x
                    204:  */
                    205: THING *
                    206: find_obj(int y, int x)
                    207: {
                    208:     register THING *op;
                    209:
                    210:     for (op = lvl_obj; op != NULL; op = next(op))
                    211:     {
                    212:        if (op->o_pos.y == y && op->o_pos.x == x)
                    213:                return op;
                    214:     }
                    215: #ifdef WIZARD
                    216:     sprintf(prbuf, "Non-object %d,%d", y, x);
                    217:     debug(prbuf);
                    218: #endif
                    219:     return NULL;
                    220: }
                    221:
                    222: /*
                    223:  * eat:
                    224:  *     She wants to eat something, so let her try
                    225:  */
                    226: void
                    227: eat(void)
                    228: {
                    229:     register THING *obj;
                    230:
                    231:     if ((obj = get_item("eat", FOOD)) == NULL)
                    232:        return;
                    233:     if (obj->o_type != FOOD)
                    234:     {
                    235:        if (!terse)
                    236:            msg("ugh, you would get ill if you ate that");
                    237:        else
                    238:            msg("that's Inedible!");
                    239:        return;
                    240:     }
                    241:     inpack--;
                    242:
                    243:     if (food_left < 0)
                    244:        food_left = 0;
                    245:     if ((food_left += HUNGERTIME - 200 + rnd(400)) > STOMACHSIZE)
                    246:        food_left = STOMACHSIZE;
                    247:     hungry_state = 0;
                    248:     if (obj == cur_weapon)
                    249:        cur_weapon = NULL;
                    250:     if (obj->o_which == 1)
                    251:        msg("my, that was a yummy %s", fruit);
                    252:     else
                    253:        if (rnd(100) > 70)
                    254:        {
                    255:            pstats.s_exp++;
                    256:            msg("yuk, this food tastes awful");
                    257:            check_level();
                    258:        }
                    259:        else
                    260:            msg("yum, that tasted good");
                    261:
                    262:     if (--obj->o_count < 1)
                    263:     {
                    264:        detach(pack, obj);
                    265:        discard(obj);
                    266:     }
                    267: }
                    268:
                    269: /*
                    270:  * chg_str:
                    271:  *     Used to modify the playes strength.  It keeps track of the
                    272:  *     highest it has been, just in case
                    273:  */
                    274: void
                    275: chg_str(int amt)
                    276: {
                    277:     str_t comp;
                    278:
                    279:     if (amt == 0)
                    280:        return;
                    281:     add_str(&pstats.s_str, amt);
                    282:     comp = pstats.s_str;
                    283:     if (ISRING(LEFT, R_ADDSTR))
                    284:        add_str(&comp, -cur_ring[LEFT]->o_ac);
                    285:     if (ISRING(RIGHT, R_ADDSTR))
                    286:        add_str(&comp, -cur_ring[RIGHT]->o_ac);
                    287:     if (comp > max_stats.s_str)
                    288:        max_stats.s_str = comp;
                    289: }
                    290:
                    291: /*
                    292:  * add_str:
                    293:  *     Perform the actual add, checking upper and lower bound limits
                    294:  */
                    295: void
                    296: add_str(str_t *sp, int amt)
                    297: {
                    298:     if ((*sp += amt) < 3)
                    299:        *sp = 3;
                    300:     else if (*sp > 31)
                    301:        *sp = 31;
                    302: }
                    303:
                    304: /*
                    305:  * add_haste:
                    306:  *     Add a haste to the player
                    307:  */
                    308: bool
                    309: add_haste(bool potion)
                    310: {
                    311:     if (on(player, ISHASTE))
                    312:     {
                    313:        no_command += rnd(8);
                    314:        player.t_flags &= ~ISRUN;
                    315:        extinguish(nohaste);
                    316:        player.t_flags &= ~ISHASTE;
                    317:        msg("you faint from exhaustion");
                    318:        return FALSE;
                    319:     }
                    320:     else
                    321:     {
                    322:        player.t_flags |= ISHASTE;
                    323:        if (potion)
                    324:            fuse(nohaste, 0, rnd(4)+4, AFTER);
                    325:        return TRUE;
                    326:     }
                    327: }
                    328:
                    329: /*
                    330:  * aggravate:
                    331:  *     Aggravate all the monsters on this level
                    332:  */
                    333: void
                    334: aggravate(void)
                    335: {
                    336:     register THING *mi;
                    337:
                    338:     for (mi = mlist; mi != NULL; mi = next(mi))
                    339:        runto(&mi->t_pos, &hero);
                    340: }
                    341:
                    342: /*
                    343:  * vowelstr:
                    344:  *      For printfs: if string starts with a vowel, return "n" for an
                    345:  *     "an".
                    346:  */
                    347: char *
                    348: vowelstr(const char *str)
                    349: {
                    350:     switch (*str)
                    351:     {
                    352:        case 'a': case 'A':
                    353:        case 'e': case 'E':
                    354:        case 'i': case 'I':
                    355:        case 'o': case 'O':
                    356:        case 'u': case 'U':
                    357:            return "n";
                    358:        default:
                    359:            return "";
                    360:     }
                    361: }
                    362:
                    363: /*
                    364:  * is_current:
                    365:  *     See if the object is one of the currently used items
                    366:  */
                    367: bool
                    368: is_current(THING *obj)
                    369: {
                    370:     if (obj == NULL)
                    371:        return FALSE;
                    372:     if (obj == cur_armor || obj == cur_weapon || obj == cur_ring[LEFT]
                    373:        || obj == cur_ring[RIGHT])
                    374:     {
                    375:        if (!terse)
                    376:            addmsg("That's already ");
                    377:        msg("in use");
                    378:        return TRUE;
                    379:     }
                    380:     return FALSE;
                    381: }
                    382:
                    383: /*
                    384:  * get_dir:
                    385:  *      Set up the direction co_ordinate for use in varios "prefix"
                    386:  *     commands
                    387:  */
                    388: bool
                    389: get_dir(void)
                    390: {
                    391:     register char *prompt;
                    392:     register bool gotit;
                    393:
                    394:     if (!terse)
                    395:        msg(prompt = "which direction? ");
                    396:     else
                    397:        prompt = "direction: ";
                    398:     do
                    399:     {
                    400:        gotit = TRUE;
                    401:        switch (readchar())
                    402:        {
                    403:            case 'h': case'H': delta.y =  0; delta.x = -1;
                    404:            when 'j': case'J': delta.y =  1; delta.x =  0;
                    405:            when 'k': case'K': delta.y = -1; delta.x =  0;
                    406:            when 'l': case'L': delta.y =  0; delta.x =  1;
                    407:            when 'y': case'Y': delta.y = -1; delta.x = -1;
                    408:            when 'u': case'U': delta.y = -1; delta.x =  1;
                    409:            when 'b': case'B': delta.y =  1; delta.x = -1;
                    410:            when 'n': case'N': delta.y =  1; delta.x =  1;
                    411:            when ESCAPE: return FALSE;
                    412:            otherwise:
                    413:                mpos = 0;
                    414:                msg(prompt);
                    415:                gotit = FALSE;
                    416:        }
                    417:     } until (gotit);
                    418:     if (on(player, ISHUH) && rnd(5) == 0)
                    419:        do
                    420:        {
                    421:            delta.y = rnd(3) - 1;
                    422:            delta.x = rnd(3) - 1;
                    423:        } while (delta.y == 0 && delta.x == 0);
                    424:     mpos = 0;
                    425:     return TRUE;
                    426: }
                    427:
                    428: /*
                    429:  * sign:
                    430:  *     Return the sign of the number
                    431:  */
                    432: int
                    433: sign(int nm)
                    434: {
                    435:     if (nm < 0)
                    436:        return -1;
                    437:     else
                    438:        return (nm > 0);
                    439: }
                    440:
                    441: /*
                    442:  * spread:
                    443:  *     Give a spread around a given number (+/- 10%)
                    444:  */
                    445: int
                    446: spread(int nm)
                    447: {
                    448:     return nm - nm / 10 + rnd(nm / 5);
                    449: }
                    450:
                    451: /*
                    452:  * call_it:
                    453:  *     Call an object something after use.
                    454:  */
                    455: void
                    456: call_it(bool know, char **guess)
                    457: {
                    458:     if (know && *guess)
                    459:     {
                    460:        free(*guess);
                    461:        *guess = NULL;
                    462:     }
                    463:     else if (!know && askme && *guess == NULL)
                    464:     {
                    465:        msg(terse ? "call it: " : "what do you want to call it? ");
                    466:        if (get_str(prbuf, stdscr) == NORM)
                    467:        {
                    468:            *guess = malloc((unsigned int) strlen(prbuf) + 1);
                    469:            strcpy(*guess, prbuf);
                    470:        }
                    471:     }
                    472: }

CVSweb