ItemSynthesis.lua 12 KB

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