抽取游戏礼包中的物品的常见逻辑

来源:互联网 发布:室内地图软件 编辑:程序博客网 时间:2024/06/11 14:41

这篇文章纯粹是为了整理我阅读的"礼包业务"代码。页游中很常见"双击一个礼包获取礼包中的物品"。常见的有如下逻辑:

1.打开礼包后获取礼包中的所有物品。 

2.打开礼包后获取礼包中的部分物品。在这些物品中,有些物品根据物品本身的概率来决定是否被抽取,有些物品则是从一组物品中抽取一个。比如"三级宝石"礼包,打开之后获取"三级攻击宝石"、"三级生命宝石"等其中之一。

3.打开礼包后获取礼包中的部分物品。这些物品是仅仅抽取每组中的一个物品,有概率决定。

  下面则是我写的逻辑Lua代码。代码很简单,因此直接贴出。

 

--[[日期:2014年3月24日文件:从礼包中选择满足条件的物品。]]--礼包配置表,配置各种礼包StaticAwardTable ={--grouptype = 0,获取此礼包中的所有物品。-----------------------------------------------------{    id = 11,grouptype = 0,award ={{ type = 0, count = 1, itemId = 110,  quality = 0, strong = 0, bind = 1,},{ type = 0, count = 1, itemId = 111,  quality = 0, strong = 0, bind = 1,},{ type = 0, count = 1, itemId = 112,  quality = 0, strong = 0, bind = 1,},{ type = 0, count = 1, itemId = 113,  quality = 0, strong = 0, bind = 1,},{ type = 0, count = 1, itemId = 114,  quality = 0, strong = 0, bind = 1,},},},--grouptype = 1,对礼包中的物品分组进行处理,相同组的物品应配置在一起。-----------------------------------------------------{    id = 22, --礼包idgrouptype = 1,award ={--group = 0,根据单个物品的rate决定是否抽取此物品,由于概率最大值是10000,配置10000表示此物品必出。{ type = 0, count = 1, itemId = 2201,  quality = 0, strong = 0, bind = 1, group = 0, rate = 10000,}, --必出{ type = 0, count = 1, itemId = 2202,  quality = 0, strong = 0, bind = 1, group = 0, rate = 10000,}, --必出{ type = 0, count = 1, itemId = 2203,  quality = 0, strong = 0, bind = 1, group = 0, rate = 2000,}, --20%概率--group ~= 0,仅给出此组中的一个物品,物品概率均等,随即抽取一个物品。{ type = 0, count = 1, itemId = 2211,  quality = 0, strong = 0, bind = 1, group = 1,},{ type = 0, count = 1, itemId = 2212,  quality = 0, strong = 0, bind = 1, group = 1,},{ type = 0, count = 1, itemId = 2213,  quality = 0, strong = 0, bind = 1, group = 1,},{ type = 0, count = 1, itemId = 2214,  quality = 0, strong = 0, bind = 1, group = 1,},},},--grouptype = 2,对礼包中的物品分组进行处理。相同组的物品不需配置在一起。每组仅给出一个物品,根据rate来决定。--相同组的物品不需要配置在一起,但需要按照从上到下的顺序rate值越小,就应该配置在两端。每组的概率之和是10000。-----------------------------------------------------{    id = 33, --礼包idgrouptype = 2,award ={--group值从1开始,否则ipairs无法遍历,pairs遍历时则会乱序{ type = 0, count = 1, itemId = 3311,  quality = 0, strong = 0, bind = 1 ,group = 1, rate = 1000,},{ type = 0, count = 1, itemId = 3312,  quality = 0, strong = 0, bind = 1 ,group = 1, rate = 2000,},{ type = 0, count = 1, itemId = 3313,  quality = 0, strong = 0, bind = 1 ,group = 1, rate = 3000,},{ type = 0, count = 1, itemId = 3314,  quality = 0, strong = 0, bind = 1 ,group = 1, rate = 3000,},{ type = 0, count = 1, itemId = 3315,  quality = 0, strong = 0, bind = 1 ,group = 1, rate = 1000,},{ type = 0, count = 1, itemId = 3321,  quality = 0, strong = 0, bind = 1 ,group = 2, rate = 1000,},{ type = 0, count = 1, itemId = 3322,  quality = 0, strong = 0, bind = 1 ,group = 2, rate = 2000,},{ type = 0, count = 1, itemId = 3323,  quality = 0, strong = 0, bind = 1 ,group = 2, rate = 3000,},{ type = 0, count = 1, itemId = 3324,  quality = 0, strong = 0, bind = 1 ,group = 2, rate = 3000,},{ type = 0, count = 1, itemId = 3325,  quality = 0, strong = 0, bind = 1 ,group = 2, rate = 1000,},{ type = 0, count = 1, itemId = 3331,  quality = 0, strong = 0, bind = 1 ,group = 3, rate = 1000,},{ type = 0, count = 1, itemId = 3332,  quality = 0, strong = 0, bind = 1 ,group = 3, rate = 2000,},{ type = 0, count = 1, itemId = 3333,  quality = 0, strong = 0, bind = 1 ,group = 3, rate = 3000,},{ type = 0, count = 1, itemId = 3334,  quality = 0, strong = 0, bind = 1 ,group = 3, rate = 3000,},{ type = 0, count = 1, itemId = 3335,  quality = 0, strong = 0, bind = 1 ,group = 3, rate = 1000,},},},}StaticAwardFunc = {}--函数:初始化随机值。StaticAwardFunc.InitRandom = function()local now = os.time()math.randomseed(now)end--函数:获取[1, max]的随机值。StaticAwardFunc.GetRandom = function(max)max = max or 10000local random = math.random(max)return randomend--函数:根据id获取此礼包的配置。StaticAwardFunc.GetGiftConf = function(id)local confInfo = StaticAwardTablefor _, oneGift in ipairs(confInfo) doif oneGift.id == id then return oneGift endendreturnend--[[函数:处理礼包中的物品。awardConf是礼包中的物品配置。说明:grouptype = 0 。]]StaticAwardFunc[0] = function(awardConf, result)print("In StaticAwardFunc[0]")for _, one in ipairs(awardConf) doresult[#result + 1] = oneendif #result == 0 then return endprint("End StaticAwardFunc[0]")return result --获得的物品tableend--函数:计算物品的平均概率,并添加。StaticAwardFunc.AddAverageItem = function(awardConf, groupStart, groupEnd, result)--print("StaticAwardFunc.AddAverageItem: groupStart, groupEnd ", groupStart, groupEnd)local count = groupEnd - groupStart + 1if count <= 0 then return endlocal offset = StaticAwardFunc.GetRandom(count) - 1local index = groupStart + offset--print("StaticAwardFunc.AddAverageItem: offset, index ", offset, index)if not awardConf[index] then return end--添加此物品result[#result + 1] = awardConf[index]end--[[函数:处理礼包中的物品。awardConf是礼包中的物品配置。说明:grouptype = 1 。]]StaticAwardFunc[1] = function(awardConf, result)print("In StaticAwardFunc[1]")local pass, random, one, groupStart, groupEnd, groupValue--程序假设相同组的物品被配置在一起for i = 1, #awardConf doif not pass or i > pass thenone = awardConf[i]--此组中只要有物品被抽中,则抽取此物品if one.group == 0 thenrandom = StaticAwardFunc.GetRandom()if one.rate >= random then --抽取此物品result[#result + 1] = oneendend--此组中的物品需要被统一处理--groupStart组的起始索引,groupEnd组的结束索引,groupValue组的值if one.group ~= 0 thengroupStart = igroupValue = one.group--计算这一组的结束索引for j = groupStart + 1, #awardConf doone = awardConf[j]if one.group == groupValue then groupEnd = j elsebreakendend--计算获得的物品StaticAwardFunc.AddAverageItem(awardConf, groupStart, groupEnd, result)--处理下一组,此时i可能是下一组的开头,也可能是无效的索引pass = groupEndendend --if pass and i > passendprint("End StaticAwardFunc[1]")end--函数:添加物品到组中。StaticAwardFunc.AddGroup = function(itemsGroup, one)if not itemsGroup[one.group] then itemsGroup[one.group] = {} endlocal oneGroup = itemsGroup[one.group]oneGroup[#oneGroup + 1] = oneend--[[函数:处理礼包中的物品。awardConf是礼包中的物品配置。说明:grouptype = 1 。]]StaticAwardFunc[2] = function(awardConf, result)print("In StaticAwardFunc[2]")local itemsGroup = {} --存放相同组中的物品--把物品分组for _, one in ipairs(awardConf) doStaticAwardFunc.AddGroup(itemsGroup, one)endlocal random, value--处理单个分组中的物品for _, oneGroup in ipairs(itemsGroup) dovalue = 0random = StaticAwardFunc.GetRandom()for _, oneItem in ipairs(oneGroup) dovalue = value + oneItem.rateif value >= random then --找到物品result[#result + 1] = oneItembreakendend --end forend -- end forprint("End StaticAwardFunc[2]")endStaticAwardFunc.PrintResult = function(result)print("StaticAwardFunc.PrintResult:")for _, one in ipairs(result) doprint("itemId, count ", one.itemId, one.count)endprint("")end--函数:处理礼包。giftConf是礼包配置。StaticAwardFunc.DealGift = function(giftConf)local result = {} --存放最终抽取的物品if StaticAwardFunc[giftConf.grouptype] thenStaticAwardFunc[giftConf.grouptype](giftConf.award, result)endStaticAwardFunc.PrintResult(result)endStaticAwardFunc.main = function()local word, id, giftConfwhile true doprint("StaticAwardFunc.main: please enter a gift id.")word = io.read()if word == "q" then break end --退出id = tonumber(word)print("StaticAwardFunc.main: id = "..id)giftConf = StaticAwardFunc.GetGiftConf(id)if not giftConf thenprint("StaticAwardFunc.main: invalid gift id")elseStaticAwardFunc.DealGift(giftConf)endprint("")endendStaticAwardFunc.main()





0 0
原创粉丝点击