EquipRefined.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. -- 涉及到的相关属性在属性表配置
  2. EquipRefined = {}
  3. local this = EquipRefined;
  4. local filename = "EquipRefined";
  5. local luckAttrs = {305012, -- 幸运一击伤害加成
  6. 900 -- 幸运(灵魂宝石成功率)
  7. }
  8. function EquipRefined.onLoginEnd(actor)
  9. end
  10. -- 根据权重随机词条数量
  11. function EquipRefined.random_weighted(weight_str, count)
  12. local result = {}
  13. if count <= 0 then
  14. return result
  15. end
  16. local num = string.split(weight_str, "&")
  17. local weight_total = 0
  18. for _, value in pairs(num) do
  19. local sum = string.split(value, "#")
  20. weight_total = weight_total + tonumber(sum[2])
  21. end
  22. local random_weight = math.random(1, weight_total)
  23. local cumulative_weight = 0
  24. for _, value in pairs(num) do
  25. local sum = string.split(value, "#")
  26. local weight = tonumber(sum[2])
  27. cumulative_weight = cumulative_weight + weight
  28. if cumulative_weight >= random_weight then
  29. table.insert(result, tonumber(sum[1]))
  30. if table.count(result) == count then
  31. return result
  32. end
  33. end
  34. end
  35. end
  36. function EquipRefined.random_weighted2(weight_str, count)
  37. local result = {}
  38. if count <= 0 then
  39. return result
  40. end
  41. local num = string.split(weight_str, "|")
  42. local weight_total = 0
  43. for _, value in pairs(num) do
  44. local sum = string.split(value, "#")
  45. weight_total = weight_total + tonumber(sum[2])
  46. end
  47. local random_weight = math.random(1, weight_total)
  48. local cumulative_weight = 0
  49. for _, value in pairs(num) do
  50. local sum = string.split(value, "#")
  51. local weight = tonumber(sum[2])
  52. cumulative_weight = cumulative_weight + weight
  53. if cumulative_weight >= random_weight then
  54. table.insert(result, tonumber(sum[1]))
  55. if table.count(result) == count then
  56. return result
  57. end
  58. end
  59. end
  60. end
  61. function EquipRefined.luarefinedequip(actor, msgData)
  62. local itemid = msgData.itemId
  63. local equip = getequipinfo(actor, itemid, 1)
  64. if equip == nil then
  65. return
  66. end
  67. local cfgRefined = ConfigDataManager.getTable("cfg_equip_refined", "id", equip.cfgid)
  68. if #cfgRefined > 0 then
  69. cfgRefined = cfgRefined[1]
  70. else
  71. return
  72. end
  73. local isMax = EquipRefined.isMax(equip)
  74. local costs = isMax and cfgRefined.refinedattrcost or cfgRefined.refinednumcost
  75. local cost_map = {}
  76. if not string.isNullOrEmpty(costs) then
  77. string.putIntIntMap(cost_map, costs, "#", "|")
  78. end
  79. -- 校验道具是否足够
  80. local enough = API.CheckItemMap(actor, cost_map)
  81. if enough == false then
  82. sendluamsg(actor, LuaMessageIdToClient.TIPS, "道具不足")
  83. return
  84. end
  85. local num = 0
  86. if isMax then
  87. num = tonumber(cfgRefined.count)
  88. else
  89. num = EquipRefined.random_weighted(cfgRefined.againnum, 1)[1]
  90. end
  91. if EquipRefined.resetEquipEntryWithRandomAttrs(actor, itemid, num) == false then
  92. return
  93. end
  94. API.TakeItemMap(actor, cost_map, "洗练消耗")
  95. sendluamsg(actor, LuaMessageIdToClient.REFINED_EQUIP_RESULT, {
  96. itemid = itemid
  97. })
  98. end
  99. function EquipRefined.isMax(equip)
  100. local cfgRefined = ConfigDataManager.getTable("cfg_equip_refined", "id", equip.cfgid)
  101. if #cfgRefined > 0 then
  102. cfgRefined = cfgRefined[1]
  103. else
  104. return
  105. end
  106. local entries = (equip ~= nil and equip.entries ~= nil) and equip.entries or {}
  107. local num = 0
  108. for key, value in pairs(entries) do
  109. local isLuck = false;
  110. for _, v in pairs(luckAttrs) do
  111. if v == value.attrid then
  112. isLuck = true
  113. break
  114. end
  115. end
  116. if isLuck == false then
  117. num = num + 1
  118. end
  119. end
  120. return num >= tonumber(cfgRefined.count);
  121. end
  122. function EquipRefined.resetEquipEntryWithRandomAttrs(actor, equipId, num)
  123. -- 获取装备信息
  124. local equipInfo = getequipinfo(actor, equipId, 1)
  125. if not equipInfo or equipInfo == nil then
  126. sendluamsg(actor, LuaMessageIdToClient.TIPS, "无法获取装备信息")
  127. return false
  128. end
  129. local itemCfg = ConfigDataManager.getTable("cfg_equip_entryLib", "id", equipInfo.cfgid)
  130. if #itemCfg > 0 then
  131. itemCfg = itemCfg[1]
  132. end
  133. if itemCfg == nil then
  134. sendluamsg(actor, LuaMessageIdToClient.TIPS, "无法获取词条配置")
  135. return
  136. end
  137. local attrGroups = {}
  138. attrGroups = string.split(itemCfg.att1, "|")
  139. -- info("装备信息:", equipInfo)
  140. -- 获取所有词条配置
  141. local allEntries = ConfigDataManager.getTable("cfg_equip_att")
  142. -- local allEntries = ConfigDataManager.getTable("cfg_att_info")
  143. -- info("所有词条配置数量:", allEntries and #allEntries or 0)
  144. if not allEntries or allEntries == nil then
  145. sendluamsg(actor, LuaMessageIdToClient.TIPS, "无法获取词条配置")
  146. return false
  147. end
  148. local tmpEntries = (equipInfo ~= nil and equipInfo.entries ~= nil) and equipInfo.entries or {}
  149. local tmpLuckAttrs = {}
  150. for key, value in pairs(tmpEntries) do
  151. local isLuck = false;
  152. for _, v in pairs(luckAttrs) do
  153. if v == value.attrid then
  154. table.insert(tmpLuckAttrs, value)
  155. break
  156. end
  157. end
  158. end
  159. -- 定义减伤属性ID(需要排除)
  160. local excludedEntryId = 26
  161. info("总词条数量:", #allEntries)
  162. -- 过滤可用的词条(排除减伤属性)并构建带权重的列表
  163. local availableEntries = {}
  164. local totalWeight = 0
  165. for _, entry in ipairs(allEntries) do
  166. local isGroup = false
  167. for key, v in pairs(attrGroups) do
  168. if v == entry.group then
  169. isGroup = true
  170. break
  171. end
  172. end
  173. -- 确保不包含减伤属性
  174. if entry.id ~= excludedEntryId and entry.id ~= 36 and entry.id ~= 37 and entry.id ~= 38 and isGroup then
  175. local attrParts = string.split(entry.att, "#")
  176. local weight = 1 -- 默认权重
  177. if #attrParts >= 4 then
  178. weight = tonumber(attrParts[4]) or 1 -- 获取权重,如果不存在则为1
  179. end
  180. table.insert(availableEntries, {
  181. entry = entry,
  182. weight = weight
  183. })
  184. totalWeight = totalWeight + weight
  185. end
  186. end
  187. if #availableEntries == 0 then
  188. sendluamsg(actor, LuaMessageIdToClient.TIPS, "没有可用的词条")
  189. return false
  190. end
  191. local entryCount = num;
  192. info("选择的词条数量:", entryCount)
  193. local selectedEntries = {}
  194. -- 生成新的词条列表(使用加权随机选择)
  195. local tempAvailableEntries = {}
  196. for i, weightedEntry in ipairs(availableEntries) do
  197. tempAvailableEntries[i] = {
  198. entry = weightedEntry.entry,
  199. weight = weightedEntry.weight
  200. }
  201. end
  202. local tempTotalWeight = totalWeight
  203. -- 根据权重随机选择词条
  204. for i = 1, entryCount do
  205. if #tempAvailableEntries <= 0 or tempTotalWeight <= 0 then
  206. break
  207. end
  208. -- 生成一个基于当前总权重的随机数
  209. local randomValue = math.random(1, tempTotalWeight)
  210. local currentWeight = 0
  211. local selectedIndex = 1
  212. -- 找到对应的词条
  213. for j, weightedEntry in ipairs(tempAvailableEntries) do
  214. currentWeight = currentWeight + weightedEntry.weight
  215. if randomValue <= currentWeight then
  216. selectedIndex = j
  217. break
  218. end
  219. end
  220. local selectedWeightedEntry = tempAvailableEntries[selectedIndex]
  221. local entryCfg = selectedWeightedEntry.entry
  222. info(string.format("选择的第%d条词条: ID=%d, 权重=%d", i, entryCfg.id, selectedWeightedEntry.weight))
  223. local attrParts = string.split(entryCfg.att, "#")
  224. local attrId = attrParts[1]
  225. local minValue = attrParts[2]
  226. local maxValue = attrParts[3]
  227. info(string.format("原始属性值: attrId=%s, minValue=%s, maxValue=%s", tostring(attrId), tostring(minValue),
  228. tostring(maxValue)))
  229. -- 转换为数字
  230. minValue = tonumber(minValue)
  231. maxValue = tonumber(maxValue)
  232. -- 创建词条对象
  233. local entry = {
  234. entryid = entryCfg.id,
  235. attrid = attrId,
  236. value = math.random(minValue, maxValue)
  237. }
  238. info("生成的词条对象:", entry)
  239. table.insert(selectedEntries, entry)
  240. -- 从可选列表中移除已选的词条,避免重复
  241. tempTotalWeight = tempTotalWeight - selectedWeightedEntry.weight
  242. table.remove(tempAvailableEntries, selectedIndex)
  243. end
  244. info("随机生成的词条:", selectedEntries)
  245. for key, value in pairs(tmpLuckAttrs) do
  246. table.insert(selectedEntries, value)
  247. end
  248. -- 应用新的词条到装备
  249. local result = resetequipentry(actor, equipId, selectedEntries)
  250. if not result then
  251. sendluamsg(actor, LuaMessageIdToClient.TIPS, "词条重置失败")
  252. return false
  253. end
  254. -- 发送成功消息
  255. APISendCenterMsg(actor, "装备词条重置成功!", "#00ff00")
  256. return true
  257. end
  258. function EquipRefined.equipEntryRandomAttrOne(equipInfo, cfgEquip, cfgEquipAtt)
  259. local attrGroups = string.split(cfgEquip.att1, "|")
  260. -- 定义减伤属性ID(需要排除)
  261. local excludedEntryId = 26
  262. -- 过滤可用的词条(排除减伤属性)并构建带权重的列表
  263. local availableEntries = {}
  264. local totalWeight = 0
  265. for _, entry in ipairs(cfgEquipAtt) do
  266. local isGroup = false
  267. for key, v in pairs(attrGroups) do
  268. if v == entry.group then
  269. isGroup = true
  270. break
  271. end
  272. end
  273. -- 确保不包含减伤属性
  274. if entry.id ~= excludedEntryId and entry.id ~= 36 and entry.id ~= 37 and entry.id ~= 38 and isGroup then
  275. local attrParts = string.split(entry.att, "#")
  276. local weight = 1 -- 默认权重
  277. if #attrParts >= 4 then
  278. weight = tonumber(attrParts[4]) or 1 -- 获取权重,如果不存在则为1
  279. end
  280. table.insert(availableEntries, {
  281. entry = entry,
  282. weight = weight
  283. })
  284. totalWeight = totalWeight + weight
  285. end
  286. end
  287. if #availableEntries == 0 then
  288. return nil
  289. end
  290. local entryCount = 1;
  291. local selectedEntries = {}
  292. -- 生成新的词条列表(使用加权随机选择)
  293. local tempAvailableEntries = {}
  294. for i, weightedEntry in ipairs(availableEntries) do
  295. tempAvailableEntries[i] = {
  296. entry = weightedEntry.entry,
  297. weight = weightedEntry.weight
  298. }
  299. end
  300. local tempTotalWeight = totalWeight
  301. -- 根据权重随机选择词条
  302. for i = 1, entryCount do
  303. if #tempAvailableEntries <= 0 or tempTotalWeight <= 0 then
  304. break
  305. end
  306. -- 生成一个基于当前总权重的随机数
  307. local randomValue = math.random(1, tempTotalWeight)
  308. local currentWeight = 0
  309. local selectedIndex = 1
  310. -- 找到对应的词条
  311. for j, weightedEntry in ipairs(tempAvailableEntries) do
  312. currentWeight = currentWeight + weightedEntry.weight
  313. if randomValue <= currentWeight then
  314. selectedIndex = j
  315. break
  316. end
  317. end
  318. local selectedWeightedEntry = tempAvailableEntries[selectedIndex]
  319. local entryCfg = selectedWeightedEntry.entry
  320. local attrParts = string.split(entryCfg.att, "#")
  321. local attrId = attrParts[1]
  322. local minValue = attrParts[2]
  323. local maxValue = attrParts[3]
  324. -- 转换为数字
  325. minValue = tonumber(minValue)
  326. maxValue = tonumber(maxValue)
  327. -- 创建词条对象
  328. local entry = {
  329. entryid = entryCfg.id,
  330. attrid = attrId,
  331. value = math.random(minValue, maxValue)
  332. }
  333. table.insert(selectedEntries, entry)
  334. -- 从可选列表中移除已选的词条,避免重复
  335. tempTotalWeight = tempTotalWeight - selectedWeightedEntry.weight
  336. table.remove(tempAvailableEntries, selectedIndex)
  337. end
  338. if table.count(selectedEntries) <= 0 then
  339. return nil
  340. end
  341. return selectedEntries[1]
  342. end