升级自动填充算法

像原神给角色升级吃经验的时候,要省吃俭用的样子

--- 填充材料_可溢出且溢出最少_递归
---@param materialList array @材料列表
---@param nextIndex number @下一个材料的index
---@param GetItemEnough function @查询材料是否足够
---@param need number 升满级需要的经验
---@param selectedMap HashMap 选中的材料
---@return boolean @IsEnough 当前材料是否足够
local function GetUseNextMaterialCount(materialList, selectedMap, nextIndex, need, parentIsEnough, GetItemEnough,GetItemCount)
    if nextIndex == 0 then -- 边界1
        return false
    end

    local materialBean = materialList[nextIndex]
    local templateId = materialBean.id
    local pExp = materialBean.exp

    local count = need // pExp
    if count > 0 then
        if not GetItemEnough(templateId, count) then
            local bagCount = bagMgr:GetItemCount(templateId)
            count = bagCount
        end
    end
    if count > 0 then
        need = need - count * pExp
        -- selectedMap:Add(templateId, count)
    end
    if need <= 0 then -- 边界2 此材料刚好够
        selectedMap:Add(templateId, count)
        return true
    end

    local myIsEnough = GetItemEnough(templateId, count + 1)

    nextIndex = nextIndex - 1 -- 倒序 从经验多的开吃
    local childIsEnough = self:GetUseNextMaterialCount(materialList,
        selectedMap,
        nextIndex, need, myIsEnough or parentIsEnough,
        GetItemEnough,GetItemCount
    )

    if childIsEnough then
        if count > 0 then
            selectedMap:Add(templateId, count)
        end
        return true -- 低级材料已填充满
    elseif myIsEnough then
        count = count + 1
        selectedMap:Add(templateId, count)
        return true
    elseif not parentIsEnough then -- 高级材料也不够就用自己把
        if count > 0 then
            selectedMap:Add(templateId, count)
        end
    end

    return false
end

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值