源码地址:http://pan.baidu.com/s/1dFwzmfB

这篇我们使用上篇文章写的GOAP框架来完成一个实例:

实例内容:

AI有10HP, 需要去站岗,站岗完成扣5HP

当HP<=5必须去补充HP,找到HP球补充HP,每个HP球补充5HP

根据GOAP框架逻辑推断出需要:AI数据提供者,站岗Action,补充HPAction,HP点脚本,站岗点脚本, AI属性脚本

主要脚本:

AI数据提供者

[csharp] view
plain
 copy

  1. public class Worker : MonoBehaviour,IGoap{
  2. private PropertyComponent property;    //属性脚本
  3. public float moveSpeed = 3;            //移动速度
  4. void Start()
  5. {
  6. property = GetComponent<PropertyComponent>();
  7. }
  8. public System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> GetState()
  9. {
  10. HashSet<KeyValuePair<string, object>> state = new HashSet<KeyValuePair<string, object>>();
  11. //当前状态HP是否足够
  12. state.Add(new KeyValuePair<string, object>("EnoughHP", property.HP > 5));
  13. return state;
  14. }
  15. public System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> CreateGoalState()
  16. {
  17. HashSet<KeyValuePair<string, object>> goal = new HashSet<KeyValuePair<string, object>>();
  18. //站岗完成目标
  19. goal.Add(new KeyValuePair<string, object>("SentryComplete", true));
  20. return goal;
  21. }
  22. public void PlanFailed(System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> failedGoal)
  23. {
  24. }
  25. public void PlanFound(System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> goal, System.Collections.Generic.Queue<Action> actions)
  26. {
  27. }
  28. public void ActionsFinished()
  29. {
  30. Debug.LogError("FinishedSentry");
  31. }
  32. public void PlanAborted(Action aborterAction)
  33. {
  34. }
  35. public bool MoveAgent(Action tagetAction)
  36. {
  37. //移动
  38. float step = moveSpeed * Time.deltaTime;
  39. gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, tagetAction.target.transform.position, step);
  40. if (gameObject.transform.position.Equals(tagetAction.target.transform.position))
  41. {
  42. tagetAction.IsInRange = true;
  43. return true;
  44. }
  45. else
  46. return false;
  47. }
  48. }

站岗Action

[csharp] view
plain
 copy

  1. public class SentryAction : Action {
  2. private SentryComponent targetSentry;      //站岗目标脚本
  3. private float SentryTimer = 0;             //站岗计时
  4. public float SentryTime = 3;
  5. bool isDone = false;                       //是否完成
  6. public SentryAction()
  7. {
  8. AddPrecondition("EnoughHP", true);  //前置条件:必须HP足够
  9. AddEffect("SentryComplete", true);  //完成效果:站岗完成
  10. }
  11. public override void Reset()
  12. {
  13. targetSentry = null;
  14. SentryTimer = 0;
  15. isDone = false;
  16. }
  17. public override bool IsDone()
  18. {
  19. return isDone;
  20. }
  21. public override bool CheckProcedualPrecondition(GameObject agent)
  22. {
  23. //得到最近的站岗目标
  24. SentryComponent[] sentryComponents = GameObject.FindObjectsOfType<SentryComponent>();
  25. SentryComponent temp = null;
  26. foreach(var v in sentryComponents)
  27. {
  28. if (temp == null)
  29. {
  30. temp = v;
  31. continue;
  32. }
  33. if (Vector3.Distance(agent.transform.position, v.transform.position) < Vector3.Distance(agent.transform.position, temp.transform.position))
  34. temp = v;
  35. }
  36. targetSentry = temp;
  37. target = temp.gameObject;
  38. return temp != null;
  39. }
  40. public override bool Perform(GameObject agent)
  41. {
  42. //站岗执行
  43. SentryTimer += Time.deltaTime;
  44. if(SentryTimer > SentryTime)
  45. {
  46. //站岗完成消耗HP
  47. agent.GetComponent<PropertyComponent>().HP -= 5;
  48. isDone = true;
  49. }
  50. return true;
  51. }
  52. public override bool RequiresInRange()
  53. {
  54. return true;
  55. }
  56. }

补充HPAction

[csharp] view
plain
 copy

  1. public class GetHPAction : Action {
  2. private HPPointComponent targetHPPoint;     //HP点目标脚本
  3. bool isDone = false;
  4. void Start()
  5. {
  6. AddEffect("EnoughHP", true);   //完成效果:HP补充到足够
  7. }
  8. public override void Reset()
  9. {
  10. targetHPPoint = null;
  11. isDone = false;
  12. }
  13. public override bool IsDone()
  14. {
  15. return isDone;
  16. }
  17. public override bool CheckProcedualPrecondition(GameObject agent)
  18. {
  19. HPPointComponent[] tempComponents = GameObject.FindObjectsOfType<HPPointComponent>();
  20. HPPointComponent temp = null;
  21. foreach (var v in tempComponents)
  22. {
  23. if (temp == null)
  24. {
  25. temp = v;
  26. continue;
  27. }
  28. if (Vector3.Distance(agent.transform.position, v.transform.position) < Vector3.Distance(agent.transform.position, temp.transform.position))
  29. temp = v;
  30. }
  31. targetHPPoint = temp;
  32. target = temp.gameObject;
  33. return temp != null;
  34. }
  35. public override bool Perform(GameObject agent)
  36. {
  37. DestroyImmediate(targetHPPoint.gameObject);
  38. isDone = true;
  39. agent.GetComponent<PropertyComponent>().HP += 5;
  40. return true;
  41. }
  42. public override bool RequiresInRange()
  43. {
  44. return true;
  45. }
  46. }

AI决策算法 之 GOAP (三)的更多相关文章

  1. AI决策算法 之 GOAP (一)

    http://blog.csdn.net/lovethrain/article/details/67632033 本系列文章内容部分参考自:http://gamerboom.com/archives/ ...

  2. AI决策算法 之 GOAP (二)

    http://blog.csdn.net/lovethRain/article/details/67634803 GOAP 的主要逻辑: 1.Agent的状态机初始化Idle状态 2.Idel状态根据 ...

  3. 贝叶斯公式由浅入深大讲解—AI基础算法入门

    1 贝叶斯方法 长久以来,人们对一件事情发生或不发生的概率,只有固定的0和1,即要么发生,要么不发生,从来不会去考虑某件事情发生的概率有多大,不发生的概率又是多大.而且概率虽然未知,但最起码是一个确定 ...

  4. 贝叶斯公式由浅入深大讲解—AI基础算法入门【转】

    本文转载自:https://www.cnblogs.com/zhoulujun/p/8893393.html 1 贝叶斯方法 长久以来,人们对一件事情发生或不发生的概率,只有固定的0和1,即要么发生, ...

  5. 2018科大讯飞AI营销算法大赛全面来袭,等你来战!

    AI技术已成为推动营销迭代的重要驱动力.AI营销高速发展的同时,积累了海量的广告数据和用户数据.如何有效应用这些数据,是大数据技术落地营销领域的关键,也是检测智能营销平台竞争力的标准. 讯飞AI营销云 ...

  6. 多维算法思考(三):AB组合问题

    多维算法思考(三):AB组合问题 题目:x个A,y个B可以组合成多少个不同排列的问题. 首先,我们用数学的方式思考,这个问题属于<组合数学>的问题,我们的第一种方法可以用组合思路来求解. ...

  7. 算法 排序lowB三人组 冒泡排序 选择排序 插入排序

    参考博客:基于python的七种经典排序算法   [经典排序算法][集锦]     经典排序算法及python实现 首先明确,算法的实质 是 列表排序.具体就是操作的列表,将无序列表变成有序列表! 一 ...

  8. 实践案例丨基于ModelArts AI市场算法MobileNet_v2实现花卉分类

    概述 MobileNetsV2是基于一个流线型的架构,它使用深度可分离的卷积来构建轻量级的深层神经网,此模型基于 MobileNetV2: Inverted Residuals and Linear ...

  9. 五子棋 AI(AIpha-beta算法)

    博弈树 下过五子棋的人都应该知道,越厉害的人,对棋面的预测程度越深.换句话讲,就是当你下完一步棋,我就能在我的脑海里假设把我所有可能下的地方都下一遍,然后考虑我下完之后你又会下在哪里,最后我根据每次预 ...

随机推荐

  1. 辛星跟您玩转vim第四节之操作文本内容

    首先值得一提的是.我的vim教程pdf版本号已经写完了.大家能够去下载,这里是csdn的下载地址:csdn下载,假设左边的下载地址挂掉了,也能够自行在浏览器以下输入例如以下地址进行下载:http:// ...

  2. Gemini.Workflow 双子工作流入门教程一:定义流程:流程图属性

    简介: Gemini.Workflow 双子工作流,是一套功能强大,使用简单的工作流,简称双子流,目前配套集成在Aries框架中. 下面介绍本篇教程:流程定义:流程图属性. 步骤一:在流程管理的流程定 ...

  3. cmd 环境变量设置方法详细解释

    cmd设置环境变量可以方便我们bat脚本的运行,但是要注意的是变量只在当前的cmd窗口有作用(局部生效),如果想要设置持久的环境变量需要我们通过两种手段进行设置:一种是直接修改注册表,另一种是通过我的 ...

  4. BZOJ 1609 [Usaco2008 Feb]Eating Together麻烦的聚餐:LIS & LDS (nlogn)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1609 题意: 给你一个只由数字"1,2,3"组成的序列a[i],共n个 ...

  5. 【转】PHP生成器 (generator)和协程的实现

    原文地址:https://phphub.org/topics/1430 1.一切从 Iterator 和 Generator 开始 为便于新入门开发者理解,本文一半篇幅是讲述迭代器接口(Iterato ...

  6. 在CI框架中的配置整合amfphp

    之前做的项目用到CI框架和amfphp的整合,主要用于php与flex的交互,在此做一下记录: 一. 安装CI框架: 1.  搭建PHP运行环境,本人在WIN7下用WAMP作测试,安装目录:d:/wa ...

  7. 强制浏览器下载PDF文件

    if(empty($filename)) { return FALSE; } // http headers header('Content-Type: application-x/force-dow ...

  8. html5--2.7新的布局元素(4)-time

    html5--2.7新的布局元素(4)-time 学习要点 了解微格式的概念 掌握time元素的用法 微格式的概念 HTML5中的微格式,是一种利用HTML5中的新标签对网页添加附加信息的方法,附加信 ...

  9. 一些rtsp实现的开源代码

    * live.com   C/S   C++   http://www.live555.com     * darwin     S     C++   http://www.opensource.a ...

  10. Servlet_03_部署描述符

    二.参考文档 1.Servlet 3.0 之 部署描述符 2.web.xml配置详解 部署描述符文件