一個模塊都往往需要統一的接口支持,特別是對於非常大型的模塊,基礎結構的統一性非常重要,它往往決定了其擴展對象的通用性。昨天說了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 (1 == newton(&x, iteration)) { printf("the value nearby root is: %f\n", x); } else { printf("iteration failed!\n"); } return 0; } double f(double x) { return x * x * x * x - 3 * x * x * x + 1.5 * x * x - 4.0; } double f1(double x) { return 4 * x * x * x - 9 * x * x + 3 * x; } int32_t newton(double *x, int32_t iteration) { double x0, x1; int32_t i; x0 = *x; //初始方程的近似根 for (i = 0; i < iteration; ++i) { //iteration 是迭代次數 if (0.0 == f1(x0)) { //如果倒數為0,則返回0(該方法失效) printf("the process derivative is 0!\n"); return 0; } x1 = x0 - f(x0) / f1(x0); //開始牛頓迭代計算 if (fabs(x1 - x0) < EPS || fabs(f(x1)) < EPS) { //達到結束條件 *x = x1; //返回結果 return 1; } else { //沒有達到結束條件,准備下一次迭代 x0 = x1; } } printf("more than iteration count\n"); return 0; }
result.
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 0; } 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 = 1; i < n; ++i) s = s + f(a + i * h) * h; return s; }