Ejemplo n.º 1
0
        /*
         * Buy an object from a store
         */
        static bool store_purchase(int item)
        {
            int amt, num;
            int price;

            Object.Object o_ptr;

            //object_type object_type_body;
            Object.Object i_ptr = new Object.Object();

            //char o_name[80];
            string o_name;

            Store store = current_store();

            if (store == null) {
                Utilities.msg("You cannot purchase items when not in a store.");
                return false;
            }

            /* Get the actual object */
            o_ptr = store.stock[item];
            if (item < 0) return false;

            /* Clear all current messages */
            Term.msg_flag = false;
            Utilities.prt("", 0, 0);

            if (store.sidx == STORE.HOME) {
                amt = o_ptr.number;
            } else {
                /* Price of one */
                price = price_item(o_ptr, false, 1);

                /* Check if the player can afford any at all */
                if ((uint)Misc.p_ptr.au < (uint)price)
                {
                    /* Tell the user */
                    Utilities.msg("You do not have enough gold for this item.");

                    /* Abort now */
                    return false;
                }

                /* Work out how many the player can afford */
                amt = Misc.p_ptr.au / price;
                if (amt > o_ptr.number) amt = o_ptr.number;

                /* Double check for wands/staves */
                if ((Misc.p_ptr.au >= price_item(o_ptr, false, amt+1)) && (amt < o_ptr.number))
                    amt++;

            }

            /* Find the number of this item in the inventory */
            if (!o_ptr.flavor_is_aware())
                num = 0;
            else
                num = find_inven(o_ptr);

            o_name = String.Format("{0} how many{1}? (max {2}) ",
                    (store.sidx == STORE.HOME) ? "Take" : "Buy",
                    num != 0 ? String.Format(" (you have {0})", num) : "", amt);

            /* Get a quantity */
            amt = Utilities.get_quantity(o_name, amt);

            /* Allow user abort */
            if (amt <= 0) return false;

            /* Get desired object */
            Object.Object.copy_amt(ref i_ptr, o_ptr, amt);

            /* Ensure we have room */
            if (!i_ptr.inven_carry_okay())
            {
                Utilities.msg("You cannot carry that many items.");
                return false;
            }

            /* Describe the object (fully) */
            o_name = i_ptr.object_desc(Object.Object.Detail.PREFIX | Object.Object.Detail.FULL);

            /* Attempt to buy it */
            if (store.sidx != STORE.HOME)
            {
                bool response;

                /* Extract the price for the entire stack */
                price = price_item(i_ptr, false, i_ptr.number);

                Utilities.screen_save();

                /* Show price */
                Utilities.prt(String.Format("Price: {0}", price), 1, 0);

                /* Confirm purchase */
                response = store_get_check(String.Format("Buy {0}? [ESC, any other key to accept]", o_name));
                Utilities.screen_load();

                /* Negative response, so give up */
                if (!response) return false;

                Game_Command.insert(Command_Code.BUY);
                Game_Command.get_top().set_arg_choice(0, item);
                Game_Command.get_top().set_arg_number(1, amt);
            }

            /* Home is much easier */
            else
            {
                Game_Command.insert(Command_Code.RETRIEVE);
                Game_Command.get_top().set_arg_choice(0, item);
                Game_Command.get_top().set_arg_number(1, amt);
            }

            /* Not kicked out */
            return true;
        }
Ejemplo n.º 2
0
        /*
         * Buy the item with the given index from the current store's inventory.
         */
        public static void buy(Command_Code code, cmd_arg[] args)
        {
            int item = args[0].value;
            int amt = args[1].value;

            Object.Object o_ptr;
            //object_type object_type_body;
            Object.Object i_ptr = new Object.Object();//&object_type_body;

            //char o_name[80];
            string o_name;
            int price, item_new;

            Store store = Store.current_store();

            if (store == null) {
                Utilities.msg("You cannot purchase items when not in a store.");
                return;
            }

            /* Get the actual object */
            o_ptr = store.stock[item];

            /* Get desired object */
            Object.Object.copy_amt(ref i_ptr, o_ptr, amt);

            /* Ensure we have room */
            if (!i_ptr.inven_carry_okay())
            {
                Utilities.msg("You cannot carry that many items.");
                return;
            }

            /* Describe the object (fully) */
            o_name = i_ptr.object_desc(Object.Object.Detail.PREFIX | Object.Object.Detail.FULL);

            /* Extract the price for the entire stack */
            price = Store.price_item(i_ptr, false, i_ptr.number);

            if (price > Misc.p_ptr.au)
            {
                Utilities.msg("You cannot afford that purchase.");
                return;
            }

            /* Spend the money */
            Misc.p_ptr.au -= price;

            /* Update the display */
            Store.store_flags |= Store.STORE_GOLD_CHANGE;

            /* ID objects on buy */
            i_ptr.notice_everything();

            /* Combine / Reorder the pack (later) */
            Misc.p_ptr.notice |= (Misc.PN_COMBINE | Misc.PN_REORDER | Misc.PN_SORT_QUIVER | Misc.PN_SQUELCH);

            /* The object no longer belongs to the store */
            i_ptr.ident &= ~(Object.Object.IDENT_STORE);

            /* Message */
            if (Random.one_in_(3)) Utilities.msgt(Message_Type.MSG_STORE5, "{0}", Store.comment_accept[Random.randint0(Store.comment_accept.Length)]);
            Utilities.msg("You bought {0} for {1} gold.", o_name, (long)price);

            /* Erase the inscription */
            i_ptr.note = null;

            /* Give it to the player */
            item_new = i_ptr.inven_carry(Misc.p_ptr);

            /* Message */
            o_name = Misc.p_ptr.inventory[item_new].object_desc(Object.Object.Detail.PREFIX | Object.Object.Detail.FULL);
            Utilities.msg("You have {0} ({1}).", o_name, Object.Object.index_to_label(item_new));

            /* Hack - Reduce the number of charges in the original stack */
            if (o_ptr.tval == TVal.TV_WAND || o_ptr.tval == TVal.TV_STAFF)
            {
                o_ptr.pval[Misc.DEFAULT_PVAL] -= i_ptr.pval[Misc.DEFAULT_PVAL];
            }

            /* Handle stuff */
            Misc.p_ptr.handle_stuff();

            /* Remove the bought objects from the store */

            store.item_increase(item, -amt);
            store.item_optimize(item);

            /* Store is empty */
            if (store.stock_num == 0)
            {
                int i;

                /* Shuffle */
                if (Random.one_in_(Store.SHUFFLE))
                {
                    /* Message */
                    Utilities.msg("The shopkeeper retires.");

                    /* Shuffle the store */
                    store.store_shuffle();
                    Store.store_flags |= Store.STORE_FRAME_CHANGE;
                }

                /* Maintain */
                else
                {
                    /* Message */
                    Utilities.msg("The shopkeeper brings out some new stock.");
                }

                /* New inventory */
                for (i = 0; i < 10; ++i)
                    store.store_maint();
            }

            Game_Event.signal(Game_Event.Event_Type.INVENTORY);
            Game_Event.signal(Game_Event.Event_Type.EQUIPMENT);
        }