--- 宝箱读表随机生成道具 RandomChest = {} local this = {} chestReward = "T$chestReward" --- 使用宝箱生成道具 ---@param actor any 玩家对象 ---@param itemConfigId number 道具配置ID ---@param count number 数量 function RandomChest.generateChestItem(actor, itemConfigId, count) local itemRow = ConfigDataManager.getTable("cfg_item", "id", itemConfigId) if table.isNullOrEmpty(itemRow) then gameDebug.print("RandomChest.generateChestItem itemConfigId error") return end local allItem = {} local tipItem = {} if tonumber(itemRow[1].type) == ItemType.BOX and tonumber(itemRow[1].subtype) == ItemSubType.EQUIP_BOX then local rewardRecord = getplaydef(actor, chestReward) for i = 1, count do local res = this.getItemAndCount(actor, itemRow[1].useparam) if table.isNullOrEmpty(res) then gameDebug.print("RandomChest.generateChestItem getItemAndCount res is nil") else for itemId, itemCount in pairs(res) do if table.isNullOrEmpty(rewardRecord) then local temp = {} temp[itemId] = 1 setplaydef(actor, chestReward, temp) else if rewardRecord[itemId] then rewardRecord[itemId] = rewardRecord[itemId] + 1 else rewardRecord[itemId] = 1 end setplaydef(actor, chestReward, rewardRecord) end table.mergeAdd(allItem, { [itemId] = itemCount }) end end end end if table.count(allItem) > 0 then -- additemmaptobag(actor, allItem) Bag.sendRewards(actor, allItem) this.excHorseLamp(actor, allItem) end end function this.excHorseLamp(actor, itemMap) if table.isNullOrEmpty(itemMap) then return end local name = getbaseinfo(actor, "rolename") for cfgId, count in pairs(itemMap) do local item = ConfigDataManager.getById("cfg_item", cfgId) if not table.isNullOrEmpty(item) then local itemName = item.name local noticeId = item.runninghorselamp if #noticeId > 0 then noticeTip.noticeinfo(actor, noticeId, name, itemName) end end end end --- 获取随机生成的道具数量 ---@param actor any 玩家对象 ---@param tableName string 配置表名 function this.getItemAndCount(actor, tableName) local res = {} local boxConfig = ConfigDataManager.getList(tableName) if table.isNullOrEmpty(boxConfig) then gameDebug.print("RandomChest.generateChestItem useParam error") return res end local idMap = {} -- 职业限制判断 local playerCareer = tonumber(getbaseinfo(actor, "getbasecareer")) -- 等级限制判断 local playerLevel = getbaseinfo(actor, "level") local rewardRecord = getplaydef(actor, chestReward) for _, config in pairs(boxConfig) do if config.career ~= "" and tonumber(playerCareer) ~= tonumber(string.split(config.career, "#")[1]) then goto tag end local lv_split = string.split(config.level, "#") if config.level ~= "" and (tonumber(playerLevel) < tonumber(lv_split[1]) or tonumber(playerLevel) > tonumber(lv_split[2])) then goto tag end local id = config.id local item = config.item local probability = string.split(config.probability, "|") local pro1 = string.split(probability[1], "#") if tonumber(config.type) == 1 then if not rewardRecord or not rewardRecord[item] then idMap[id] = pro1[2] else for _, v in pairs(probability) do local strs = string.split(v, "#") idMap[id] = tonumber(strs[1]) <= tonumber(rewardRecord[item]) and strs[2] or pro1[2] end end else local weight = pro1[2] if rewardRecord and rewardRecord[item] then for _, v in pairs(probability) do local v_split = string.split(v, "#") if tonumber(v_split[1]) ~= 0 then weight = v_split[1] >= rewardRecord[item] and v_split[2] or pro1[2] end end end if tonumber(randomex(actor, weight, 10000)) == 1 then local num_split = string.split(config.num, "#") res[item] = math.random(num_split[1], num_split[2]) end end :: tag :: end if table.isNullOrEmpty(idMap) then gameDebug.print("RandomChest.generateChestItem itemMap is nil") return res end if tonumber(boxConfig[1].type) == 1 then local id = randombyweight(actor, idMap, 1)[1] local num = ConfigDataManager.getTableValue(tableName, "num", "id", id) local itemId = ConfigDataManager.getTableValue(tableName, "item", "id", id) local num_split = string.split(num, "#") res[itemId] = math.random(num_split[1], num_split[2]) return res else return res end end