一个简单的非线性叠加算法

在游戏中,有许多属性的叠加是非线性的,这里以闪避为例子进行展开。


单位目前的闪避为 a, 物品增加闪避 b, 单位得到物品的最终闪避为 c. (闪避的最大值为100%,这里用100来算,记为 limit)

c = a + b* ( (limit - a)/ f(limit)  )   |   a<limit, b<limit, f(limit) > limit

f(limit)为叠加衰减的系数,例:limit = 70, f(limit) = limit +30 = 100

若a = limit 或者  b = limit, 则c = limit. 
 

当单位丢失物品,减少闪避时。可以已知 c , b, limit, f(limit) 求上面方程的解

解得 a = ( c*f(limit) - b*limit ) / ( f(limit) - b )


例: a = 15, limit = 65, f(limit) = 87;

增加闪避:

第一次 b1 = 5;                                     c1 = 17.87

第二次b2 = 17, a = c1 = 17.87;           c2 = 27.08

第三次b3 = 24, a = c2 = 27.08;           c3 = 37.54

减少闪避:移去b2, 此时 c3 = 37.54; 得到 c2 = 30.81


如果第一次+b1, 第二次+b3

第一次b1 = 5;                                     c1 = 17.87

第二次b3 = 24                                    c2 = 30.81


两次算出的c2是相等的,所以上面的式子是成立的。下面是检测代码:

#include <stdio.h>

int main(){
	double a, b, c, t, s;
	s = 87;
	t = 65;
	a = 15;
	
	b = 5;
	double c1 = a + b*(t-a)/s;
	b = 17;
	double c2 = c1 + b*(t-c1)/s;
	b = 24;
	double c3 = c2 + b*(t-c2)/s;
	printf("%.2f %.2f %.2f\n", c1, c2, c3);
	
	b = 17;
	c2 = ( (c3*s) - b*t) / (s - b);
	b = 24;
	c = c1 + b*(t-c1)/s;
	printf("%.2f %.2f", c2, c);
	
	return 0;
}

非线性叠加(支持多次叠加):

c = a + b* ( (limit - a)/ f(limit)  )   |   a<limit, b<limit, f(limit) > limit

a = ( c*f(limit) - b*limit ) / ( f(limit) - b )   |   a<limit, b<limit, f(limit) > limit







猜你喜欢

转载自blog.csdn.net/lang_dye/article/details/80060606