ItemSynthesis.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ItemSynthesis = {}
  2. local this = {}
  3. local SYNTHESIS_CFG_TYPE = {
  4. normal = 1,
  5. special = 2
  6. }
  7. function testsynthesis(actor)
  8. local msgData = {30001}
  9. ItemSynthesis.synthesis(actor,msgData)
  10. end
  11. -- 道具合成
  12. function ItemSynthesis.synthesis(actor,msgData)
  13. local synthesisCfgId = msgData[1]
  14. local arbitrarily = ConfigDataManager.getTableValue("cfg_synthesis","arbitrarily","id",synthesisCfgId)
  15. local special = nil
  16. local count = 1
  17. if arbitrarily == "" then
  18. count = msgData[2]
  19. else
  20. special = msgData[2]
  21. if not special then
  22. noticeTip.noticeinfo(actor, StringIdConst.TEXT356)
  23. return
  24. end
  25. end
  26. local synthesisType = tonumber(ConfigDataManager.getTableValue("cfg_synthesis","synthesisType","id",synthesisCfgId))
  27. jprint("synthesisType",synthesisType)
  28. if synthesisType == SYNTHESIS_CFG_TYPE.special and count > 10 then
  29. return
  30. end
  31. local needLevel = ConfigDataManager.getTableValue("cfg_synthesis","level","id",synthesisCfgId)
  32. local currentLevel = getbaseinfo(actor,"level")
  33. if tonumber(needLevel) > tonumber(currentLevel) then
  34. noticeTip.noticeinfo(actor, StringIdConst.TEXT353)
  35. return
  36. end
  37. local consumeItem = ConfigDataManager.getTableValue("cfg_synthesis","consumeItem","id",synthesisCfgId)
  38. local splitConsume = string.split(consumeItem, "|")
  39. -- 查询需要消耗的道具信息
  40. local consumeLocal = {}
  41. for index, value in pairs(splitConsume) do
  42. local consumeInfo = string.split(value, "#")
  43. local consumeItemCfgId = tonumber(consumeInfo[1])
  44. local consumeItemCount = tonumber(consumeInfo[2])
  45. local haveCount = getbagitemcountbyid(actor,consumeItemCfgId)
  46. if consumeItemCount * count > haveCount then
  47. noticeTip.noticeinfo(actor, StringIdConst.TEXT357)
  48. return
  49. end
  50. consumeLocal[consumeItemCfgId] = consumeItemCount * count
  51. end
  52. -- 清理需要消耗道具
  53. for index, value in pairs(consumeLocal) do
  54. removeitemfrombag(actor,index,value,0,9999,'物品合成')
  55. end
  56. local removeItemIndex = {}
  57. local successRateReal = ConfigDataManager.getTableValue("cfg_synthesis","successRateReal","id",synthesisCfgId)
  58. -- 计算合成成功次数
  59. local successCount = 0
  60. for i = 1,count,1 do
  61. local successRate = math.random(0, 10000)
  62. if successRate <= tonumber(successRateReal) then
  63. successCount = successCount + 1
  64. end
  65. end
  66. if successCount == 0 then
  67. noticeTip.noticeinfo(actor, StringIdConst.TEXT358)
  68. if special then
  69. for index, value in pairs(special) do
  70. removeItemIndex[index] = value["itemIndex"]
  71. end
  72. removeitembyidxlist(actor,removeItemIndex,9999,'物品合成')
  73. end
  74. return
  75. end
  76. local productId = ConfigDataManager.getTableValue("cfg_synthesis","productId","id",synthesisCfgId)
  77. local splitProduct = string.split(productId, "|")
  78. local productCfgId = {}
  79. local productRateLocal = {}
  80. local total = 0
  81. for index, value in pairs(splitProduct) do
  82. local productInfo = string.split(value, "#")
  83. local productItemCfgId = tonumber(productInfo[1])
  84. local productRate = tonumber(productInfo[2])
  85. table.insert(productCfgId,productItemCfgId)
  86. total = total + productRate
  87. table.insert(productRateLocal,total)
  88. end
  89. local result= {}
  90. local productItemInfo = {}
  91. for i = 1,successCount,1 do
  92. local totalRate = math.random(0, total)
  93. local indexRate = 1
  94. local lastValue = 0
  95. for index, value in pairs(productRateLocal) do
  96. if lastValue < totalRate and totalRate < value then
  97. indexRate = index
  98. end
  99. lastValue = value
  100. end
  101. local productItem = productCfgId[indexRate]
  102. -- 特殊合成需要继承之前道具的卓越属性,追加属性,强化属性
  103. if special then
  104. local changeItemId = 0
  105. local removeIndex = 0
  106. for index, value in pairs(special) do
  107. changeItemId = value["itemId"]
  108. removeIndex = value["itemIndex"]
  109. end
  110. local itemId = itemcompound(actor,productItem,1,removeIndex)
  111. ItemSynthesis.changeItemAtt(actor,itemId,changeItemId)
  112. local product = {
  113. itemId = itemId,
  114. itemCfgId = productItem,
  115. count = 1
  116. }
  117. table.insert(result,product)
  118. elseif synthesisType == SYNTHESIS_CFG_TYPE.special then
  119. local itemId = additemtobag(actor,productItem,1, 0, 9999, '物品合成')
  120. local product = {
  121. itemId = itemId,
  122. itemCfgId = productItem,
  123. count = 1
  124. }
  125. table.insert(result,product)
  126. else
  127. local productItemCount = productItemInfo[productItem]
  128. if not productItemCount then
  129. productItemInfo[productItem] = 1
  130. else
  131. productItemInfo[productItem] = productItemInfo[productItem] + 1
  132. end
  133. end
  134. end
  135. if not table.isNullOrEmpty(productItemInfo) then
  136. for itemCfgId, count in pairs(productItemInfo) do
  137. local itemId = additemtobag(actor,itemCfgId,count, 0, 9999, '物品合成')
  138. local product = {
  139. itemId = itemId,
  140. itemCfgId = itemCfgId,
  141. count = count
  142. }
  143. table.insert(result,product)
  144. end
  145. end
  146. sendluamsg(actor,LuaMessageIdToClient.RES_ITEM_SYNTHESIS,result)
  147. ---触发合成任务
  148. local taskParam = {
  149. synthesisid = synthesisCfgId,
  150. num = count
  151. }
  152. TaskHandler.TriggerTaskGoal(actor, TaskTargetType.SYNTHESIS, taskParam)
  153. end
  154. -- 合成装备增加追加属性
  155. function ItemSynthesis.changeItemAtt(actor,itemId,changeItemId)
  156. local allequip = getplaydef(actor,"T$luaitemextdata")
  157. if not allequip then
  158. return
  159. end
  160. local equipext = allequip[changeItemId]
  161. if equipext == nil then
  162. return
  163. end
  164. allequip[itemId] = equipext
  165. EquipAndAppear.SetItemExtData(actor,itemId,equipext)
  166. allequip[changeItemId] = {}
  167. setplaydef(actor,"T$luaitemextdata",allequip)
  168. end