EquipRefined.lua 11 KB

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