MMORPG大型遊戲設計與開發(服務器 AI 基礎接口)

一個模塊都每每須要統一的接口支持,特別是對於很是大型的模塊,基礎結構的統一性很是重要,它每每決定了其擴展對象的通用性。昨天說了AI的基本概述以及組成,做爲與場景模塊中核心同樣重要的地位,基礎部分的設計儘可能的統1、詳細、通用、精簡。算法

遊戲截圖

基礎接口(base)

  一、管理方法

    初始化(init)、釋放(release)、得到NPC隊伍指針(get npc team)、內部邏輯循環函數(activate)。函數

  二、狀態方法(ing)

    休閒(idle)、閒逛(wander)、巡邏(patrol)、警惕(alert)、跟隨(follow)、追擊(pursuit)、保持距離(keep away)、逃跑(escape)、返回(return)、等待(wait)。工具

  三、狀態切換(do)

     休閒(idle)、閒逛(wander)、巡邏(patrol)、警惕(alert)、跟隨(follow)、追擊(pursuit)、保持距離(keep away)、逃跑(escape)、返回(return)、等待(wait)。ui

  四、事件

    嘗試移動(try move)、攻擊(attacked)、威脅清除(clear threat)、尋路結果(path result)。spa

  五、技能選擇評估

    初始化技能CD(init skill cd)、檢查攻擊目標(check attack target)、指望的目標(period target)、指望的自身(period self)、指望的友人(period firend)、被動響應(passive respond)、中途中斷(channeling break)、目標數量(target count)、自身血量(self hp)。設計

  六、工具函數

    得到點的極座標偏移點(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)。3d

算法(近似迭代法)

  一、牛頓迭代法

    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.

  二、求定積分

    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;
}

    result.

相關文章
相關標籤/搜索