抽取游戏礼包中的物品的常见逻辑
来源:互联网 发布:室内地图软件 编辑:程序博客网 时间: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
- 抽取游戏礼包中的物品的常见逻辑
- 深入分析游戏中的物品系统
- 游戏中的逻辑
- ListView中抽取ViewHolder的逻辑操作
- 乱侃游戏的物品生成(Part!)
- Python 2.7 好玩游戏的物品清单
- 游戏礼包激活码案例分析
- 漫谈游戏中的交易逻辑
- 游戏中的刷兵逻辑
- 2048游戏的逻辑
- 游戏的主要逻辑
- 从重复逻辑中抽取新的数据组件
- 面试 笔试中的常见智力题 逻辑题
- 面试 笔试中的常见智力题 逻辑题
- 《游戏王》中的一道逻辑题
- Lua游戏逻辑开发中的优化思路
- qq会员礼包接入的坑
- 实现抽取java中的注释的代码
- [C/C++]浮点数在内存中的表示
- Android面试笔试集锦
- ubuntu快捷键总结
- angular form validation
- MOSS 2010:Visual Studio 2010开发体验(14)——列表开发之事件接收器
- 抽取游戏礼包中的物品的常见逻辑
- 【物联网智能网关-18】多通道远程安全升级
- MFC各类自定义消息
- 单文档多视图获取视图指针
- MFC获取文档 视图 程序 框架类的指…
- 女人为什么喜欢抱着男人睡觉,一定…
- 基于CAS实现单点登录(SSO):登录成功后,cas client如何返回更多用户信息
- C语言的单链表在开创内存空间时为…
- 编译和链接的区别