From aa4574b5ebbbdf24682a07720536eb80c30c19ec Mon Sep 17 00:00:00 2001 From: 1 <1@72wo.cn> Date: Sun, 23 Nov 2025 09:59:34 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=A9=BA=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/blazing/model/pet.go | 61 ++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/modules/blazing/model/pet.go b/modules/blazing/model/pet.go index 0489208a9..ab0e1bd88 100644 --- a/modules/blazing/model/pet.go +++ b/modules/blazing/model/pet.go @@ -117,62 +117,69 @@ func (pet *PetInfo) AddEV(evadd []uint32) (bool, error) { return false, errors.New("pet.Ev未初始化或长度不为6") } - // 2. 预计算:当前EV总和 + 增量后的临时值(先不修改原数据,避免边改边算) - var ( - totalCurrent uint32 // 当前EV总和 - tempEV [evFieldCount]uint32 // 增量后的临时EV值(避免修改原数组导致计算错误) - ) - // 一次性遍历:计算当前总和 + 初始化临时EV(减少遍历次数) + // 2. 第一步:直接添加并限制单项最大值(按索引顺序处理) + var tempEV [evFieldCount]uint32 for i := 0; i < evFieldCount; i++ { - totalCurrent += pet.Ev[i] - // 先计算增量后的值,同时限制单个不超过maxSingleEV + // 直接累加增量 tempEV[i] = pet.Ev[i] + evadd[i] + // 单项不超过255 if tempEV[i] > maxSingleEV { tempEV[i] = maxSingleEV } } - // 3. 计算增量后的临时总和,检查是否超过maxTotalEV + // 3. 计算增量后的总和,检查是否超过510 totalTemp := uint32(0) for _, v := range tempEV { totalTemp += v } - // 4. 若总和超额,执行削减逻辑(优先削减数值大的字段,保证公平性) + // 4. 若总和超额,按索引顺序(0→5)削减(优先削减前面的字段) hasCut := false if totalTemp > maxTotalEV { overTotal := totalTemp - maxTotalEV // 需要削减的总量 hasCut = true - // 循环削减直到超额量为0(优先削减数值大的字段,避免小值被过度削减) - for overTotal > 0 { - // 找到当前最大的EV索引 - maxIdx := 0 - for i := 1; i < evFieldCount; i++ { - if tempEV[i] > tempEV[maxIdx] { - maxIdx = i - } + // 按索引顺序遍历削减(从第0个字段开始,依次处理) + for i := 0; i < evFieldCount && overTotal > 0; i++ { + // 可削减的最大值:最多削减到原始值(不触碰添加前的基础EV) + cutAble := tempEV[i] - pet.Ev[i] + if cutAble <= 0 { + continue // 该字段无增量可削减,跳过 } - // 计算该字段可削减的最大值(至少留到原数值,或削减到能覆盖overTotal) - cutAble := tempEV[maxIdx] - pet.Ev[maxIdx] // 最多削减增量部分,不低于原值 - if cutAble == 0 { - cutAble = tempEV[maxIdx] // 若增量为0,可削减当前值(根据业务调整,也可panic) - } - - // 实际削减量:取可削减量和剩余超额量的较小值 + // 实际削减量:取"可削减量"和"剩余需削减量"的较小值 cut := cutAble if cut > overTotal { cut = overTotal } // 执行削减 - tempEV[maxIdx] -= cut + tempEV[i] -= cut overTotal -= cut } + + // 极端情况:即使削减所有增量后仍超额(如原始EV总和已超510),继续按顺序削减原始值 + if overTotal > 0 { + for i := 0; i < evFieldCount && overTotal > 0; i++ { + // 此时可削减到0(根据业务需求调整,也可返回错误) + cutAble := tempEV[i] + if cutAble <= 0 { + continue + } + + cut := cutAble + if cut > overTotal { + cut = overTotal + } + + tempEV[i] -= cut + overTotal -= cut + } + } } - // 5. 将处理后的临时值赋值给原EV(批量赋值,减少零散操作) + // 5. 将处理后的结果赋值给原EV数组 copy(pet.Ev[:], tempEV[:]) return hasCut, nil