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

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 random
end--函数:根据id获取此礼包的配置。
StaticAwardFunc.GetGiftConf = function(id)local    confInfo = StaticAwardTablefor _, oneGift in ipairs(confInfo) doif oneGift.id == id then return oneGift endendreturn
end--[[
函数:处理礼包中的物品。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    --获得的物品table
end--函数:计算物品的平均概率,并添加。
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] = one
end--[[
函数:处理礼包中的物品。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("")end
endStaticAwardFunc.main()

抽取游戏礼包中的物品的常见逻辑相关推荐

  1. 3D游戏引擎中常见的三维场景管理方法

    对于一个有很多物体的3D场景来说,渲染这个场景最简单的方式就是用一个List将这些物体进行存储,并送入GPU进行渲染.当然,这种做法在效率上来说是相当低下的,因为真正需要渲染的物体应该是视椎体内的物体 ...

  2. 游戏美术中最常见的故障及其解决方案(1)

    游戏美术中最常见的故障及其解决方法 针对网格问题.光照 bug.粒子.透明度.物理的故障排除提示. 解决方案可以应用在 Unity,UE,Godot 和大多数其他游戏引擎. 几何图形 名称 症状 原因 ...

  3. 游戏服务器中常见的数据一致性问题分析

    什么是一致性问题 在游戏服务器的开发中,我们经常会碰到所谓"一致性"问题,以及碰到各种为了解决这种问题所做的"方案",那么,什么是一致性问题呢?其实非常简单,就 ...

  4. 【转】游戏编程中的人工智能技术--神经网络

    原文:http://blog.csdn.net/ecitnet/article/details/1799444 游戏编程中的人工智能技术 . > .  (连载之一) 用平常语言介绍神经网络(Ne ...

  5. 游戏社交不足怎么办? 游戏发行中的社交化运营经验分享

    http://www.gameres.com/687428.html 3 天前 上传 下载附件 (26.17 KB) 文/孙秀龙 今天很开心在这里和大家分享一下游戏发行中的社交化运营,这次分享目的是带 ...

  6. 在游戏策划中应用SCAMPER创新

    在这里我将谈谈对一个新游戏的诞生起到作用的常见火花,也就是如何去发散思维. 先看看同类型游戏 一个新游戏的创意通常是来源于一个游戏或者多个游戏的各个方面的组合.随着游戏的多年的发展,已经逐渐打造出了成 ...

  7. 关联规则算法在游戏行业中的应用

    关联规则算法在游戏行业中的应用 本文为学习<R语言游戏数据分析与挖掘>学习笔记. Apriori算法应用广泛,可用于消费市场价格分析,猜测顾客的消费习惯,比如较有名的"尿布和啤酒 ...

  8. spring 数组中随机取几个_游戏编程中需要掌握哪些数学物理知识

    一. 相似三角形知识的应用 在摇杆控制物体运动的游戏中,摇杆的手柄(下图黄色圆饼),不能移出摇杆所在的套(下图灰色圆环),也就是说摇杆偏离中心点的最大距离为max_R.一旦触摸移动过程中移动的点超出此 ...

  9. 游戏编程中的数学——随机数字生成(RNG)的黑暗秘密

    大家好,你们能听到我讲话吗?这个演讲的内容是介绍RNG(随机数字生成)的一些黑暗秘密.如你在大屏幕上看到的,Squirrel已经介绍了一些RNG的基础概念.首先,我想详细讲解几点.他的演讲更偏重理论, ...

最新文章

  1. python代码规范化_最流行的Python代码规范
  2. LC滤波器简单设计法 - 一文读懂LC滤波器简单设计方法及原理介绍,LC值计算方法...
  3. 绘制矩形php,PHP_php绘制一个矩形的方法,本文实例讲述了php绘制一个矩 - phpStudy...
  4. web前端开发面试题(答案)
  5. 一键安装zabbix监控redis
  6. ACM-Maximum Tape Utilization Ratio
  7. ubuntu python版本切换_ubuntu 16.04下python版本切换的方法
  8. 深度学习实践指南(二)—— 符号编程
  9. python语言的主网址-Python爬取网址中多个页面的信息
  10. windows传文件到linux
  11. Word2003中Visio2003图打印错误的解决方法
  12. 【Multisim仿真】数字电路仿真16路往复流水灯
  13. 谷歌开源 Embedding Projector 高维数据可视化--转自开源中国
  14. linux启动盘无法格式化u盘启动盘,U盘做了启动盘后无法格式化怎么办实测解决...
  15. 你必须知道的 NET(第2版)
  16. 语音预处理之分帧加窗
  17. Windows常见基本进程三:dumprep or dumprep进程(Dump Reporting Tool启动项)
  18. 属羊的几月出生最好命
  19. Inventor冲压加强筋_inventor 加强筋教程
  20. 【SSH框架】--Hibernate入门

热门文章

  1. Indexof与Substring的用法
  2. Web Vue IX
  3. 随记之 -- diy相册
  4. 内蒙古经贸学校计算机专业,内蒙古经贸学校招生专业|内蒙古经贸学校有哪些专业...
  5. 内蒙古大学892题解 (2019)
  6. 人工智能数学基础专栏目录
  7. Python h5py
  8. 软考 程序员教程-第二章 操作系统基础知识
  9. GAMES101作业6-BVH完成全过程
  10. android .9png聊天气泡,Android 关于点9图在气泡评论里使用的调研