ItemSynthesis.lua 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. -- 辅助消耗道具
  27. local assistantItem = msgData[3]
  28. local synthesisType =
  29. tonumber(ConfigDataManager.getTableValue("cfg_synthesis", "synthesisType", "id", synthesisCfgId))
  30. jprint("synthesisType", synthesisType)
  31. if synthesisType == SYNTHESIS_CFG_TYPE.special and count > 10 then
  32. return
  33. end
  34. local needLevel = ConfigDataManager.getTableValue("cfg_synthesis", "level", "id", synthesisCfgId)
  35. local currentLevel = getbaseinfo(actor, "level")
  36. if tonumber(needLevel) > tonumber(currentLevel) then
  37. noticeTip.noticeinfo(actor, StringIdConst.TEXT353)
  38. return
  39. end
  40. local consumeItem = ConfigDataManager.getTableValue("cfg_synthesis", "consumeItem", "id", synthesisCfgId)
  41. local splitConsume = string.split(consumeItem, "|")
  42. -- 查询需要消耗的道具信息
  43. local consumeLocal = {}
  44. for index, value in pairs(splitConsume) do
  45. local consumeInfo = string.split(value, "#")
  46. local consumeItemCfgId = tonumber(consumeInfo[1])
  47. local consumeItemCount = tonumber(consumeInfo[2])
  48. local haveCount = getbagitemcountbyid(actor, consumeItemCfgId)
  49. if consumeItemCount * count > haveCount then
  50. noticeTip.noticeinfo(actor, StringIdConst.TEXT357)
  51. return
  52. end
  53. consumeLocal[consumeItemCfgId] = consumeItemCount * count
  54. end
  55. -- 清理需要消耗道具
  56. for index, value in pairs(consumeLocal) do
  57. removeitemfrombag(actor, index, value, 0, 9999, "物品合成")
  58. end
  59. local removeItemIndex = {}
  60. local successRateReal = ConfigDataManager.getTableValue("cfg_synthesis", "successRateReal", "id", synthesisCfgId)
  61. -- 装备等级概率
  62. local addSuccessRateReal =
  63. ItemSynthesis.consum_add_rate(actor, synthesisCfgId, splitConsume, special, assistantItem)
  64. successRateReal = successRateReal + addSuccessRateReal
  65. -- 最高概率
  66. local maxRate = tonumber(ConfigDataManager.getTableValue("cfg_synthesis", "maxRate", "id", synthesisCfgId) or 10000)
  67. successRateReal = math.min(maxRate, successRateReal)
  68. -- 计算合成成功次数
  69. local successCount = 0
  70. for i = 1, count, 1 do
  71. local successRate = math.random(0, 10000)
  72. if successRate <= tonumber(successRateReal) then
  73. successCount = successCount + 1
  74. end
  75. end
  76. if successCount == 0 then
  77. noticeTip.noticeinfo(actor, StringIdConst.TEXT358)
  78. if special then
  79. for index, value in pairs(special) do
  80. removeItemIndex[index] = value["itemIndex"]
  81. end
  82. removeitembyidxlist(actor, removeItemIndex, 9999, "物品合成")
  83. end
  84. return
  85. end
  86. local productId = ConfigDataManager.getTableValue("cfg_synthesis", "productId", "id", synthesisCfgId)
  87. local splitProduct = string.split(productId, "|")
  88. local productCfgId = {}
  89. local productRateLocal = {}
  90. local total = 0
  91. for index, value in pairs(splitProduct) do
  92. local productInfo = string.split(value, "#")
  93. local productItemCfgId = tonumber(productInfo[1])
  94. local productRate = tonumber(productInfo[2])
  95. table.insert(productCfgId, productItemCfgId)
  96. total = total + productRate
  97. table.insert(productRateLocal, total)
  98. end
  99. local result = {}
  100. local productItemInfo = {}
  101. for i = 1, successCount, 1 do
  102. local totalRate = math.random(0, total)
  103. local indexRate = 1
  104. local lastValue = 0
  105. for index, value in pairs(productRateLocal) do
  106. if lastValue < totalRate and totalRate < value then
  107. indexRate = index
  108. end
  109. lastValue = value
  110. end
  111. local productItem = productCfgId[indexRate]
  112. -- 特殊合成需要继承之前道具的卓越属性,追加属性,强化属性
  113. if special then
  114. local changeItemId = 0
  115. local removeIndex = 0
  116. for index, value in pairs(special) do
  117. changeItemId = value["itemId"]
  118. removeIndex = value["itemIndex"]
  119. end
  120. local itemId = itemcompound(actor, productItem, 1, removeIndex)
  121. ItemSynthesis.changeItemAtt(actor, itemId, changeItemId)
  122. local product = {
  123. itemId = itemId,
  124. itemCfgId = productItem,
  125. count = 1
  126. }
  127. table.insert(result, product)
  128. elseif synthesisType == SYNTHESIS_CFG_TYPE.special then
  129. local itemId = additemtobag(actor, productItem, 1, 0, 9999, "物品合成")
  130. local product = {
  131. itemId = itemId,
  132. itemCfgId = productItem,
  133. count = 1
  134. }
  135. table.insert(result, product)
  136. else
  137. local productItemCount = productItemInfo[productItem]
  138. if not productItemCount then
  139. productItemInfo[productItem] = 1
  140. else
  141. productItemInfo[productItem] = productItemInfo[productItem] + 1
  142. end
  143. end
  144. end
  145. if not table.isNullOrEmpty(productItemInfo) then
  146. for itemCfgId, count in pairs(productItemInfo) do
  147. local itemId = additemtobag(actor, itemCfgId, count, 0, 9999, "物品合成")
  148. local product = {
  149. itemId = itemId,
  150. itemCfgId = itemCfgId,
  151. count = count
  152. }
  153. table.insert(result, product)
  154. end
  155. end
  156. sendluamsg(actor, LuaMessageIdToClient.RES_ITEM_SYNTHESIS, result)
  157. ---触发合成任务
  158. local taskParam = {
  159. synthesisid = synthesisCfgId,
  160. num = count
  161. }
  162. TaskHandler.TriggerTaskGoal(actor, TaskTargetType.SYNTHESIS, taskParam)
  163. end
  164. -- 合成装备增加追加属性
  165. function ItemSynthesis.changeItemAtt(actor, itemId, changeItemId)
  166. local allequip = getplaydef(actor, "T$luaitemextdata")
  167. if not allequip then
  168. return
  169. end
  170. local equipext = allequip[changeItemId]
  171. if equipext == nil then
  172. return
  173. end
  174. allequip[itemId] = equipext
  175. EquipAndAppear.SetItemExtData(actor, itemId, equipext)
  176. allequip[changeItemId] = {}
  177. setplaydef(actor, "T$luaitemextdata", allequip)
  178. end
  179. -- 消耗增加概率
  180. function ItemSynthesis.consum_add_rate(actor, synthesisCfgId, consumeItems, special, assistantItem)
  181. local configList = ConfigDataManager.getList("cfg_synthesis_material")
  182. local allEquip = EquipAndAppear.getLuaItemExtData(actor)
  183. -- 获取合成增加概率
  184. local function _get_item_rate(actor, sId, itemId)
  185. local equip = getequipinfo(actor, itemId, 1)
  186. if equip == nil or allEquip[itemId] == nil then
  187. return 0
  188. end
  189. for _, v in ipairs(configList) do
  190. if v.synthesisCfgId == sId and v.materialId == itemId then
  191. -- 装备强化等级
  192. local strengthlv = allEquip[itemId].strengthlv
  193. -- 装备追加等级
  194. local appendlv = allEquip[itemId].appendlv
  195. -- 是否符合等级要求
  196. local isMatch = false
  197. if v.related == 1 then
  198. -- 同时满足强化等级以及追加等级
  199. if strengthlv >= v.minLevel and appendlv >= v.minAppend then
  200. isMatch = true
  201. end
  202. end
  203. if v.related == 2 then
  204. -- 强化等级或追加等级满足其中一个
  205. if strengthlv >= v.minLevel or appendlv >= v.minAppend then
  206. isMatch = true
  207. end
  208. end
  209. if isMatch then
  210. local addRate = v.addRateBase or 0
  211. local addStrengthRate = (strengthlv - v.minLevel) * (v.perAddRateLevel or 0)
  212. local addAppendRate = (appendlv - v.minAppend) * (v.perAddRateAppend or 0)
  213. if v.related == 1 then
  214. addRate = addRate + addStrengthRate + addAppendRate
  215. end
  216. if v.related == 2 then
  217. addRate = addRate + math.max(addStrengthRate, addAppendRate)
  218. end
  219. return addRate
  220. end
  221. end
  222. end
  223. return 0
  224. end
  225. local addRate = 0
  226. -- 公式刚需消耗
  227. for _, v in ipairs(consumeItems) do
  228. local consumeInfo = string.split(v, "#")
  229. local consumeItemCfgId = tonumber(consumeInfo[1])
  230. local consumeItemCount = tonumber(consumeInfo[2])
  231. addRate = addRate + _get_item_rate(actor, synthesisCfgId, consumeItemCfgId)
  232. end
  233. -- 公式特殊消耗道具
  234. if special then
  235. for index, value in pairs(special) do
  236. local itemId = value["itemId"]
  237. local itemIndex = value["itemIndex"]
  238. addRate = addRate + _get_item_rate(actor, synthesisCfgId, itemId)
  239. end
  240. end
  241. -- 辅助消耗道具
  242. if assistantItem then
  243. for index, value in pairs(assistantItem) do
  244. local itemId = value["itemId"]
  245. local itemIndex = value["itemIndex"]
  246. addRate = addRate + _get_item_rate(actor, synthesisCfgId, itemId)
  247. end
  248. end
  249. return addRate
  250. end