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

Annotation of early-roguelike/arogue5/trader.c, Revision 1.1

1.1     ! rubenllo    1: /*
        !             2:  * Anything to do with trading posts
        !             3:  *
        !             4:  * Advanced Rogue
        !             5:  * Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka and AT&T
        !             6:  * All rights reserved.
        !             7:  *
        !             8:  * Based on "Super-Rogue"
        !             9:  * Copyright (C) 1984 Robert D. Kindelberger
        !            10:  * All rights reserved.
        !            11:  *
        !            12:  * See the file LICENSE.TXT for full copyright and licensing information.
        !            13:  */
        !            14:
        !            15: #include "curses.h"
        !            16: #include "rogue.h"
        !            17: #include <ctype.h>
        !            18: #include <string.h>
        !            19:
        !            20: bool open_market(void);
        !            21: void trans_line(void);
        !            22: char *typ_name(struct object *obj);
        !            23:
        !            24: 
        !            25: /*
        !            26:  * buy_it:
        !            27:  *     Buy the item on which the hero stands
        !            28:  */
        !            29: void
        !            30: buy_it(void)
        !            31: {
        !            32:        reg int wh;
        !            33:        struct linked_list *item;
        !            34:
        !            35:        if (purse <= 0) {
        !            36:            msg("You have no money.");
        !            37:            return;
        !            38:        }
        !            39:        if (curprice < 0) {             /* if not yet priced */
        !            40:            wh = price_it();
        !            41:            if (!wh)                    /* nothing to price */
        !            42:                return;
        !            43:            msg("Do you want to buy it? ");
        !            44:            do {
        !            45:                wh = tolower(readchar());
        !            46:                if (wh == ESCAPE || wh == 'n') {
        !            47:                    msg("");
        !            48:                    return;
        !            49:                }
        !            50:            } until(wh == 'y');
        !            51:        }
        !            52:        mpos = 0;
        !            53:        if (curprice > purse) {
        !            54:            msg("You can't afford to buy that %s !",curpurch);
        !            55:            return;
        !            56:        }
        !            57:        /*
        !            58:         * See if the hero has done all his transacting
        !            59:         */
        !            60:        if (!open_market())
        !            61:            return;
        !            62:        /*
        !            63:         * The hero bought the item here
        !            64:         */
        !            65:        item = find_obj(hero.y, hero.x);
        !            66:        mpos = 0;
        !            67:        if (add_pack(NULL,TRUE,&item)) {        /* try to put it in his pack */
        !            68:            purse -= curprice;          /* take his money */
        !            69:            ++trader;                   /* another transaction */
        !            70:            trans_line();               /* show remaining deals */
        !            71:            curprice = -1;              /* reset stuff */
        !            72:            curpurch[0] = 0;
        !            73:            whatis (item);              /* identify it after purchase */
        !            74:            (OBJPTR(item))->o_flags &= ~ISPOST; /* turn off ISPOST */
        !            75:            msg("%s", inv_name(OBJPTR(item), TRUE));
        !            76:        }
        !            77: }
        !            78: 
        !            79: /*
        !            80:  * do_post:
        !            81:  *     Put a trading post room and stuff on the screen
        !            82:  */
        !            83: void
        !            84: do_post(void)
        !            85: {
        !            86:        coord tp;
        !            87:        reg int i;
        !            88:        reg struct room *rp;
        !            89:        reg struct object *op;
        !            90:        reg struct linked_list *ll;
        !            91:
        !            92:        o_free_list(lvl_obj);           /* throw old items away */
        !            93:
        !            94:        for (rp = rooms; rp < &rooms[MAXROOMS]; rp++)
        !            95:            rp->r_flags = ISGONE;               /* kill all rooms */
        !            96:
        !            97:        rp = &rooms[0];                         /* point to only room */
        !            98:        rp->r_flags = 0;                        /* this room NOT gone */
        !            99:        rp->r_max.x = 40;
        !           100:        rp->r_max.y = 10;                       /* 10 * 40 room */
        !           101:        rp->r_pos.x = (COLS - rp->r_max.x) / 2; /* center horizontal */
        !           102:        rp->r_pos.y = 1;                        /* 2nd line */
        !           103:        draw_room(rp);                          /* draw the only room */
        !           104:        i = roll(4,10);                         /* 10 to 40 items */
        !           105:        for (; i > 0 ; i--) {                   /* place all the items */
        !           106:            ll = new_thing(ALL);                /* get something */
        !           107:            attach(lvl_obj, ll);
        !           108:            op = OBJPTR(ll);
        !           109:            op->o_flags |= ISPOST;              /* object in trading post */
        !           110:            do {
        !           111:                rnd_pos(rp,&tp);
        !           112:            } until (mvinch(tp.y, tp.x) == FLOOR);
        !           113:            op->o_pos = tp;
        !           114:            mvaddch(tp.y,tp.x,op->o_type);
        !           115:        }
        !           116:        trader = 0;
        !           117:        wmove(cw,12,0);
        !           118:        waddstr(cw,"Welcome to Friendly Fiend's Flea Market\n\r");
        !           119:        waddstr(cw,"=======================================\n\r");
        !           120:        waddstr(cw,"$: Prices object that you stand upon.\n\r");
        !           121:        waddstr(cw,"#: Buys the object that you stand upon.\n\r");
        !           122:        waddstr(cw,"%: Trades in something in your pack for gold.\n\r");
        !           123:        trans_line();
        !           124: }
        !           125:
        !           126: 
        !           127: /*
        !           128:  * get_worth:
        !           129:  *     Calculate an objects worth in gold
        !           130:  */
        !           131: int
        !           132: get_worth(struct object *obj)
        !           133: {
        !           134:        reg int worth, wh;
        !           135:
        !           136:        worth = 0;
        !           137:        wh = obj->o_which;
        !           138:        switch (obj->o_type) {
        !           139:            case FOOD:
        !           140:                worth = 2;
        !           141:            when WEAPON:
        !           142:                if (wh < MAXWEAPONS) {
        !           143:                    worth = weaps[wh].w_worth;
        !           144:                    worth += s_magic[S_ALLENCH].mi_worth *
        !           145:                                 (obj->o_hplus + obj->o_dplus);
        !           146:                }
        !           147:            when ARMOR:
        !           148:                if (wh < MAXARMORS) {
        !           149:                    worth = armors[wh].a_worth;
        !           150:                    worth += s_magic[S_ALLENCH].mi_worth *
        !           151:                                (armors[wh].a_class - obj->o_ac);
        !           152:                }
        !           153:            when SCROLL:
        !           154:                if (wh < MAXSCROLLS)
        !           155:                    worth = s_magic[wh].mi_worth;
        !           156:            when POTION:
        !           157:                if (wh < MAXPOTIONS)
        !           158:                    worth = p_magic[wh].mi_worth;
        !           159:            when RING:
        !           160:                if (wh < MAXRINGS) {
        !           161:                    worth = r_magic[wh].mi_worth;
        !           162:                    worth += obj->o_ac * 40;
        !           163:                }
        !           164:            when STICK:
        !           165:                if (wh < MAXSTICKS) {
        !           166:                    worth = ws_magic[wh].mi_worth;
        !           167:                    worth += 20 * obj->o_charges;
        !           168:                }
        !           169:            when MM:
        !           170:                if (wh < MAXMM) {
        !           171:                    worth = m_magic[wh].mi_worth;
        !           172:                    switch (wh) {
        !           173:                        case MM_BRACERS:        worth += 40  * obj->o_ac;
        !           174:                        when MM_PROTECT:        worth += 60  * obj->o_ac;
        !           175:                        when MM_DISP:           /* ac already figured in price*/
        !           176:                        otherwise:              worth += 20  * obj->o_ac;
        !           177:                    }
        !           178:                }
        !           179:            when RELIC:
        !           180:                if (wh < MAXRELIC) {
        !           181:                    worth = rel_magic[wh].mi_worth;
        !           182:                    if (wh == quest_item) worth *= 10;
        !           183:                }
        !           184:            otherwise:
        !           185:                worth = 0;
        !           186:        }
        !           187:        if (obj->o_flags & ISPROT)      /* 300% more for protected */
        !           188:            worth *= 3;
        !           189:        if (obj->o_flags &  ISBLESSED)  /* 50% more for blessed */
        !           190:            worth = worth * 3 / 2;
        !           191:        if (obj->o_flags & ISCURSED)    /* half for cursed */
        !           192:            worth /= 2;
        !           193:        if (worth < 0)
        !           194:            worth = 0;
        !           195:        return worth;
        !           196: }
        !           197: 
        !           198: /*
        !           199:  * open_market:
        !           200:  *     Retruns TRUE when ok do to transacting
        !           201:  */
        !           202: bool
        !           203: open_market(void)
        !           204: {
        !           205:        if (trader >= MAXPURCH && !wizard) {
        !           206:            msg("The market is closed. The stairs are that-a-way.");
        !           207:            return FALSE;
        !           208:        }
        !           209:        else {
        !           210:            return TRUE;
        !           211:        }
        !           212: }
        !           213: 
        !           214: /*
        !           215:  * price_it:
        !           216:  *     Price the object that the hero stands on
        !           217:  */
        !           218: bool
        !           219: price_it(void)
        !           220: {
        !           221:        reg struct linked_list *item;
        !           222:        reg struct object *obj;
        !           223:        reg int worth;
        !           224:        reg char *str;
        !           225:
        !           226:        if (!open_market())             /* after buying hours */
        !           227:            return FALSE;
        !           228:        if ((item = find_obj(hero.y,hero.x)) == NULL)
        !           229:            return FALSE;
        !           230:        obj = OBJPTR(item);
        !           231:        worth = get_worth(obj);
        !           232:        if (worth < 0) {
        !           233:            msg("That's not for sale.");
        !           234:            return FALSE;
        !           235:        }
        !           236:        if (worth < 25)
        !           237:            worth = 25;
        !           238:        worth *= 3;                     /* slightly expensive */
        !           239:        str = inv_name(obj, TRUE);
        !           240:        sprintf(outstring,"%s for only %d pieces of gold", str, worth);
        !           241:        msg(outstring);
        !           242:        curprice = worth;               /* save price */
        !           243:        strcpy(curpurch,str);           /* save item */
        !           244:        return TRUE;
        !           245: }
        !           246:
        !           247:
        !           248: 
        !           249: /*
        !           250:  * sell_it:
        !           251:  *     Sell an item to the trading post
        !           252:  */
        !           253: void
        !           254: sell_it(void)
        !           255: {
        !           256:        reg struct linked_list *item;
        !           257:        reg struct object *obj;
        !           258:        reg int wo, ch;
        !           259:
        !           260:        if (!open_market())             /* after selling hours */
        !           261:            return;
        !           262:
        !           263:        if ((item = get_item(pack, "sell", ALL)) == NULL)
        !           264:            return;
        !           265:        obj = OBJPTR(item);
        !           266:        wo = get_worth(obj);
        !           267:        if (wo <= 0) {
        !           268:            mpos = 0;
        !           269:            msg("We don't buy those.");
        !           270:            return;
        !           271:        }
        !           272:        if (wo < 25)
        !           273:            wo = 25;
        !           274:        sprintf(outstring,"Your %s is worth %d pieces of gold.",typ_name(obj),wo);
        !           275:        msg(outstring);
        !           276:        msg("Do you want to sell it? ");
        !           277:        do {
        !           278:            ch = tolower(readchar());
        !           279:            if (ch == ESCAPE || ch == 'n') {
        !           280:                msg("");
        !           281:                return;
        !           282:            }
        !           283:        } until (ch == 'y');
        !           284:        mpos = 0;
        !           285:        if (drop(item) == TRUE) {               /* drop this item */
        !           286:            purse += wo;                        /* give him his money */
        !           287:            ++trader;                           /* another transaction */
        !           288:            wo = obj->o_count;
        !           289:            if (obj->o_group == 0)              /* dropped one at a time */
        !           290:                obj->o_count = 1;
        !           291:            msg("Sold %s",inv_name(obj,TRUE));
        !           292:            obj->o_count = wo;
        !           293:            trans_line();                       /* show remaining deals */
        !           294:        }
        !           295: }
        !           296: 
        !           297: /*
        !           298:  * trans_line:
        !           299:  *     Show how many transactions the hero has left
        !           300:  */
        !           301: void
        !           302: trans_line(void)
        !           303: {
        !           304:        if (!wizard)
        !           305:            sprintf(prbuf,"You have %d transactions remaining.",
        !           306:                    MAXPURCH - trader);
        !           307:        else
        !           308:            sprintf(prbuf,
        !           309:                "You have infinite transactions remaining oh great wizard");
        !           310:        mvwaddstr(cw,LINES - 3,0,prbuf);
        !           311: }
        !           312:
        !           313:
        !           314: 
        !           315: /*
        !           316:  * typ_name:
        !           317:  *     Return the name for this type of object
        !           318:  */
        !           319: char *
        !           320: typ_name(struct object *obj)
        !           321: {
        !           322:        static char buff[20];
        !           323:        reg int wh;
        !           324:
        !           325:        switch (obj->o_type) {
        !           326:                case POTION:  wh = TYP_POTION;
        !           327:                when SCROLL:  wh = TYP_SCROLL;
        !           328:                when STICK:   wh = TYP_STICK;
        !           329:                when RING:    wh = TYP_RING;
        !           330:                when ARMOR:   wh = TYP_ARMOR;
        !           331:                when WEAPON:  wh = TYP_WEAPON;
        !           332:                when MM:      wh = TYP_MM;
        !           333:                when FOOD:    wh = TYP_FOOD;
        !           334:                when RELIC:   wh = TYP_RELIC;
        !           335:                otherwise:    wh = -1;
        !           336:        }
        !           337:        if (wh < 0)
        !           338:                strcpy(buff,"unknown");
        !           339:        else
        !           340:                strcpy(buff,things[wh].mi_name);
        !           341:        return (buff);
        !           342: }
        !           343:
        !           344:

CVSweb