예제 #1
0
파일: Utils.cs 프로젝트: Chlour/ShopC
        /**
         * 以铜币为单位从背包中扣除金币
         */
        public static int[] TakeMoneyFromPlayer(this TSPlayer plr, int price)
        {
            int[] result = new int[5];
            plr.DelItemFromInventoryByIndex(51, 10);
            int copper_coin   = 0;
            int silver_coin   = 0;
            int gold_coin     = 0;
            int platinum_coin = 0;
            //铜币为单位的总数
            int total = 0;

            //获取买家的货币数量
            for (int i = 50; i < 54; i++)
            {
                if (plr.TPlayer.inventory[i].type == 71)
                {
                    copper_coin = copper_coin + plr.TPlayer.inventory[i].stack;
                }
                else if (plr.TPlayer.inventory[i].type == 72)
                {
                    silver_coin = silver_coin + plr.TPlayer.inventory[i].stack;
                }
                else if (plr.TPlayer.inventory[i].type == 73)
                {
                    gold_coin = gold_coin + plr.TPlayer.inventory[i].stack;
                }
                else if (plr.TPlayer.inventory[i].type == 74)
                {
                    platinum_coin = platinum_coin + plr.TPlayer.inventory[i].stack;
                }
            }
            total = copper_coin + silver_coin * 100 + gold_coin * 10000 + platinum_coin * 1000000;

            if (price > total)
            {
                //货币数不够
                result[0] = 1;
                return(result);
            }

            //采用先删除后添加的方式
            for (int i = 50; i < 54; i++)
            {
                if (plr.TPlayer.inventory[i] != null)
                {
                    plr.DelItemFromInventoryByIndex(i, 1000);
                }
            }

            total = total - price;
            int total_p = total / 1000000;
            int total_g = (total / 10000) % 100;
            int total_s = (total / 100) % 100;
            int total_c = total % 100;

            if (total_p != 0)
            {
                plr.GiveItemEX(74, total_p, 0);
            }
            if (total_g != 0)
            {
                plr.GiveItemEX(73, total_g, 0);
            }
            if (total_s != 0)
            {
                plr.GiveItemEX(72, total_s, 0);
            }
            if (total_c != 0)
            {
                plr.GiveItemEX(71, total_c, 0);
            }

            result[0] = 0;
            result[1] = price / 1000000;
            result[2] = (price / 10000) % 100;
            result[3] = (price / 100) % 100;
            result[4] = price % 100;
            return(result);
        }