MMORPG大型游戏设计与开发(服务器 AI 基础接口)

时间:2023-03-08 22:44:11

一个模块都往往需要统一的接口支持,特别是对于非常大型的模块,基础结构的统一性非常重要,它往往决定了其扩展对象的通用性。昨天说了AI的基本概述以及组成,作为与场景模块中核心一样重要的地位,基础部分的设计尽量的统一、详细、通用、精简。

游戏截图

MMORPG大型游戏设计与开发(服务器 AI 基础接口)

基础接口(base)

1、管理方法

初始化(init)、释放(release)、获得NPC队伍指针(get npc team)、内部逻辑循环函数(activate)。

2、状态方法(ing)

休闲(idle)、闲逛(wander)、巡逻(patrol)、警戒(alert)、跟随(follow)、追击(pursuit)、保持距离(keep away)、逃跑(escape)、返回(return)、等待(wait)。

3、状态切换(do)

休闲(idle)、闲逛(wander)、巡逻(patrol)、警戒(alert)、跟随(follow)、追击(pursuit)、保持距离(keep away)、逃跑(escape)、返回(return)、等待(wait)。

4、事件

尝试移动(try move)、攻击(attacked)、威胁清除(clear threat)、寻路结果(path result)。

5、技能选择评估

初始化技能CD(init skill cd)、检查攻击目标(check attack target)、期望的目标(period target)、期望的自身(period self)、期望的友人(period firend)、被动响应(passive respond)、中途中断(channeling break)、目标数量(target count)、自身血量(self hp)。

6、工具函数

获得点的极坐标偏移点(get adjust point)、跟随(follow)、清除目标(clear target)、保存返回点(save return point)、闲逛(wander)、逃跑(escape)、寻路(patrol)、警戒(alert)、请求帮助(call help)、请求治疗(call heal)、转到战斗(turn to fight)、转到休闲(turn to idle)、检查事件(check event)、执行事件(fire event)、返回检查(check return)、请求治疗检查(check call heal)、闲逛范围检查(check wander range)、攻击检查(check attacked)、检查目标是否进入警戒范围(check is in alert range)、检查目标是否进入攻击范围(check is in attck range)、保持攻击范围(keep attck range)、保持闲逛范围(keep wander range)。

算法(近似迭代法)

1、牛顿迭代法

code.

#include <stdio.h>
#include <inttypes.h>
#include <math.h> /**
* 牛顿迭代法
* 该方法是用于求方程或方程组近似根的一种常用算法。
*/ #define EPS 1e-6 double f(double x);
double f1(double x);
int32_t newton(double *x, int32_t iteration); int32_t main(int32_t argc, char *argv[]) {
double x;
int32_t _x, iteration;
printf("please input init iteration value x0: ");
scanf("%d", &_x);
x = static_cast<double>(_x);
printf("please input max iteration count: ");
scanf("%d", &iteration);
if ( == newton(&x, iteration)) {
printf("the value nearby root is: %f\n", x);
} else {
printf("iteration failed!\n");
}
return ;
} double f(double x) {
return x * x * x * x - * x * x * x + 1.5 * x * x - 4.0;
} double f1(double x) {
return * x * x * x - * x * x + * x;
} int32_t newton(double *x, int32_t iteration) {
double x0, x1;
int32_t i;
x0 = *x; //初始方程的近似根
for (i = ; i < iteration; ++i) { //iteration 是迭代次数
if (0.0 == f1(x0)) { //如果倒数为0,则返回0(该方法失效)
printf("the process derivative is 0!\n");
return ;
}
x1 = x0 - f(x0) / f1(x0); //开始牛顿迭代计算
if (fabs(x1 - x0) < EPS || fabs(f(x1)) < EPS) { //达到结束条件
*x = x1; //返回结果
return ;
} else { //没有达到结束条件,准备下一次迭代
x0 = x1;
}
}
printf("more than iteration count\n");
return ;
}

result.

MMORPG大型游戏设计与开发(服务器 AI 基础接口)

2、求定积分

code.

#include <stdio.h>
#include <inttypes.h>
#include <math.h> /**
* 求定积分
* 利用梯田法求定积分,需注意以下3个方面的工作:
* 1 确定迭代变量。
* 2 建立迭代关系。
* 3 对迭代过程进行控制。
*/ #define N 100 double f(double x);
double integral(double a, double b, int32_t n); int32_t main(int32_t argc, char *argv[]) {
double a, b, value;
int32_t _a, _b;
printf("please input definite integral max and min value: ");
scanf("%d,%d", &_a, &_b);
a = static_cast<double>(_a);
b = static_cast<double>(_b);
value = integral(a, b, N);
printf("sin(x) in range[%d,%d] definite integral is: %f\n", _a, _b, value);
return ;
} double f(double x) {
return sin(x);
} double integral(double a, double b, int32_t n) {
double s, h;
int32_t i;
h = (b - a) / n;
s = 0.5 * h * (f(a) + f(b));
for (i = ; i < n; ++i)
s = s + f(a + i * h) * h;
return s;
}

result.

MMORPG大型游戏设计与开发(服务器 AI 基础接口)