ItemSynthesis.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 = ItemSynthesis.consum_add_rate(actor, splitConsume, special, assistantItem)
  63. successRateReal = successRateReal + addSuccessRateReal
  64. -- 最高概率
  65. local maxRate = tonumber(ConfigDataManager.getTableValue("cfg_synthesis", "maxRate", "id", synthesisCfgId) or 10000)
  66. successRateReal = math.min(maxRate, successRateReal)
  67. -- 计算合成成功次数
  68. local successCount = 0
  69. for i = 1, count, 1 do
  70. local successRate = math.random(0, 10000)
  71. if successRate <= tonumber(successRateReal) then
  72. successCount = successCount + 1
  73. end
  74. end
  75. if successCount == 0 then
  76. noticeTip.noticeinfo(actor, StringIdConst.TEXT358)
  77. if special then
  78. for index, value in pairs(special) do
  79. removeItemIndex[index] = value["itemIndex"]
  80. end
  81. removeitembyidxlist(actor, removeItemIndex, 9999, "物品合成")
  82. end
  83. return
  84. end
  85. local productId = ConfigDataManager.getTableValue("cfg_synthesis", "productId", "id", synthesisCfgId)
  86. local splitProduct = string.split(productId, "|")
  87. local productCfgId = {}
  88. local productRateLocal = {}
  89. local total = 0
  90. for index, value in pairs(splitProduct) do
  91. local productInfo = string.split(value, "#")
  92. local productItemCfgId = tonumber(productInfo[1])
  93. local productRate = tonumber(productInfo[2])
  94. table.insert(productCfgId, productItemCfgId)
  95. total = total + productRate
  96. table.insert(productRateLocal, total)
  97. end
  98. local result = {}
  99. local productItemInfo = {}
  100. for i = 1, successCount, 1 do
  101. local totalRate = math.random(0, total)
  102. local indexRate = 1
  103. local lastValue = 0
  104. for index, value in pairs(productRateLocal) do
  105. if lastValue < totalRate and totalRate < value then
  106. indexRate = index
  107. end
  108. lastValue = value
  109. end
  110. local productItem = productCfgId[indexRate]
  111. -- 特殊合成需要继承之前道具的卓越属性,追加属性,强化属性
  112. if special then
  113. local changeItemId = 0
  114. local removeIndex = 0
  115. for index, value in pairs(special) do
  116. changeItemId = value["itemId"]
  117. removeIndex = value["itemIndex"]
  118. end
  119. local itemId = itemcompound(actor, productItem, 1, removeIndex)
  120. ItemSynthesis.changeItemAtt(actor, itemId, changeItemId)
  121. local product = {
  122. itemId = itemId,
  123. itemCfgId = productItem,
  124. count = 1
  125. }
  126. table.insert(result, product)
  127. elseif synthesisType == SYNTHESIS_CFG_TYPE.special then
  128. local itemId = additemtobag(actor, productItem, 1, 0, 9999, "物品合成")
  129. local product = {
  130. itemId = itemId,
  131. itemCfgId = productItem,
  132. count = 1
  133. }
  134. table.insert(result, product)
  135. else
  136. local productItemCount = productItemInfo[productItem]
  137. if not productItemCount then
  138. productItemInfo[productItem] = 1
  139. else
  140. productItemInfo[productItem] = productItemInfo[productItem] + 1
  141. end
  142. end
  143. end
  144. if not table.isNullOrEmpty(productItemInfo) then
  145. for itemCfgId, count in pairs(productItemInfo) do
  146. local itemId = additemtobag(actor, itemCfgId, count, 0, 9999, "物品合成")
  147. local product = {
  148. itemId = itemId,
  149. itemCfgId = itemCfgId,
  150. count = count
  151. }
  152. table.insert(result, product)
  153. end
  154. end
  155. sendluamsg(actor, LuaMessageIdToClient.RES_ITEM_SYNTHESIS, result)
  156. ---触发合成任务
  157. local taskParam = {
  158. synthesisid = synthesisCfgId,
  159. num = count
  160. }
  161. TaskHandler.TriggerTaskGoal(actor, TaskTargetType.SYNTHESIS, taskParam)
  162. end
  163. -- 合成装备增加追加属性
  164. function ItemSynthesis.changeItemAtt(actor, itemId, changeItemId)
  165. local allequip = getplaydef(actor, "T$luaitemextdata")
  166. if not allequip then
  167. return
  168. end
  169. local equipext = allequip[changeItemId]
  170. if equipext == nil then
  171. return
  172. end
  173. allequip[itemId] = equipext
  174. EquipAndAppear.SetItemExtData(actor, itemId, equipext)
  175. allequip[changeItemId] = {}
  176. setplaydef(actor, "T$luaitemextdata", allequip)
  177. end
  178. -- 消耗增加概率
  179. function ItemSynthesis.consum_add_rate(actor, consumeItems, special, assistantItem)
  180. local confMaterialList = ConfigDataManager.getList("cfg_synthesis_material")
  181. local confAssistantList = ConfigDataManager.getList("cfg_synthesis_assistant")
  182. local allEquip = EquipAndAppear.getLuaItemExtData(actor)
  183. -- 获取合成增加概率
  184. local function _get_item_rate(actor, itemId, groupId)
  185. -- 装备强化等级
  186. local strengthlv = 0
  187. -- 装备追加等级
  188. local appendlv = 0
  189. local equip = getequipinfo(actor, itemId, 1)
  190. if equip and allEquip[itemId] then
  191. strengthlv = allEquip[itemId].strengthlv
  192. appendlv = allEquip[itemId].appendlv
  193. end
  194. -- 获取配置
  195. local conf = nil
  196. if groupId == nil then
  197. for _, v in ipairs(confMaterialList) do
  198. if v.materialId == itemId then
  199. -- 装备,增加等级判断
  200. if v.type == 2 then
  201. -- 是否符合等级要求
  202. if v.related == 1 then
  203. -- 同时满足强化等级以及追加等级
  204. if strengthlv >= v.minLevel and appendlv >= v.minAppend then
  205. conf = table.copy(v)
  206. break
  207. end
  208. end
  209. if v.related == 2 then
  210. -- 强化等级或追加等级满足其中一个
  211. if strengthlv >= v.minLevel or appendlv >= v.minAppend then
  212. conf = table.copy(v)
  213. break
  214. end
  215. end
  216. else
  217. conf = table.copy(v)
  218. break
  219. end
  220. end
  221. end
  222. else
  223. for _, v in ipairs(confMaterialList) do
  224. if v.groupId == groupId and v.itemId == itemId then
  225. -- 装备,增加等级判断
  226. if v.type == 2 then
  227. -- 是否符合等级要求
  228. if v.related == 1 then
  229. -- 同时满足强化等级以及追加等级
  230. if strengthlv >= v.minLevel and appendlv >= v.minAppend then
  231. conf = table.copy(v)
  232. break
  233. end
  234. end
  235. if v.related == 2 then
  236. -- 强化等级或追加等级满足其中一个
  237. if strengthlv >= v.minLevel or appendlv >= v.minAppend then
  238. conf = table.copy(v)
  239. break
  240. end
  241. end
  242. else
  243. conf = table.copy(v)
  244. break
  245. end
  246. end
  247. end
  248. end
  249. if conf then
  250. local addRate = conf.addRateBase or 0
  251. local addStrengthRate = (strengthlv - (conf.minLevel or 0)) * (conf.perAddRateLevel or 0)
  252. local addAppendRate = (appendlv - (conf.minAppend or 0)) * (conf.perAddRateAppend or 0)
  253. if conf.related == 1 then
  254. addRate = addRate + addStrengthRate + addAppendRate
  255. end
  256. if conf.related == 2 then
  257. addRate = addRate + math.max(addStrengthRate, addAppendRate)
  258. end
  259. return addRate
  260. end
  261. return 0
  262. end
  263. local addRate = 0
  264. -- 公式刚需消耗
  265. for _, v in ipairs(consumeItems) do
  266. local consumeInfo = string.split(v, "#")
  267. local consumeItemCfgId = tonumber(consumeInfo[1])
  268. local consumeItemCount = tonumber(consumeInfo[2])
  269. addRate = addRate + _get_item_rate(actor, consumeItemCfgId)
  270. end
  271. -- 公式特殊消耗道具
  272. if special then
  273. for index, value in pairs(special) do
  274. local itemId = value["itemId"]
  275. local itemIndex = value["itemIndex"]
  276. local groupId = value["groupId"]
  277. addRate = addRate + _get_item_rate(actor, itemId, groupId)
  278. end
  279. end
  280. -- 辅助消耗道具
  281. if assistantItem then
  282. for index, value in pairs(assistantItem) do
  283. local itemId = value["itemId"]
  284. local itemIndex = value["itemIndex"]
  285. local groupId = value["groupId"]
  286. addRate = addRate + _get_item_rate(actor, itemId, groupId)
  287. end
  288. end
  289. return addRate
  290. end