文章编辑人员:雪舞
内容测试时间:2023-4-23 16:32:36
老的函数用了很多的if
--**********************************
-- 心法加成
--**********************************
function x808234_XinFaAddition( sceneId, selfId )
local nXinfaLevel = LuaFnGetXinFaLevel(sceneId, selfId, 70)
local nAdditon = 0
if nXinfaLevel>=150 then
nAdditon = 15
elseif nXinfaLevel>=140 and nXinfaLevel<150 then
nAdditon = 14
elseif nXinfaLevel>=130 and nXinfaLevel<140 then
nAdditon = 13
elseif nXinfaLevel>=120 and nXinfaLevel<130 then
nAdditon = 12
elseif nXinfaLevel>=110 and nXinfaLevel<120 then
nAdditon = 11
elseif nXinfaLevel>=100 and nXinfaLevel<110 then
nAdditon = 10
elseif nXinfaLevel>=90 and nXinfaLevel<100 then
nAdditon = 9
elseif nXinfaLevel>=80 and nXinfaLevel<90 then
nAdditon = 8
elseif nXinfaLevel>=70 and nXinfaLevel<80 then
nAdditon = 7
elseif nXinfaLevel>=60 and nXinfaLevel<70 then
nAdditon = 6
elseif nXinfaLevel>=50 and nXinfaLevel<60 then
nAdditon = 5
elseif nXinfaLevel>=40 and nXinfaLevel<50 then
nAdditon = 4
elseif nXinfaLevel>=30 and nXinfaLevel<40 then
nAdditon = 4
elseif nXinfaLevel>=20 and nXinfaLevel<30 then
nAdditon = 3
elseif nXinfaLevel>=10 and nXinfaLevel<20 then
nAdditon = 2
elseif nXinfaLevel<10 then
nAdditon = 1
end
return nAdditon
end
新优化过的:
function x808234_XinFaAddition(sceneId, selfId)
local nXinfaLevel = LuaFnGetXinFaLevel(sceneId, selfId, 70)
local nAddition = max(min(floor((nXinfaLevel - 10) / 10) + 1, 15), 1)
return nAddition
end
这个代码用了更少的if语句,更短的代码行数,并且使用了内置的数学函数来进行计算。max()和min()函数用于将nAddition的值限制在1到15之间。(nXinfaLevel - 10) / 10计算出nXinfaLevel比10大多少个十,例如,如果nXinfaLevel为136,则该公式的结果为12.6。floor()将这个值向下取整为最近的整数。对于上面的例子,这将给出一个值为12。将这个值加1即得到最终的nAddition。
|