源码地址: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. 流畅python学习笔记:第十四章:迭代器和生成器

    迭代器和生成器是python中的重要特性,本章作者花了很大的篇幅来介绍迭代器和生成器的用法. 首先来看一个单词序列的例子: import re re_word=re.compile(r'\w+') c ...

  2. Docker实践中遇到的坑

    1.docker容器中后台运行退出执行curl+p+q,再次进入执行命令docker attach 容器id. 2.容器中exit退出后,还原方法为docker ps -a 查看历史运行容器,dock ...

  3. angularJs 购物车模型

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel= ...

  4. Flask中的内置session

    Flask中的Session非常的奇怪,他会将你的SessionID存放在客户端的Cookie中,使用起来也非常的奇怪 1. Flask 中 session 是需要 secret_key 的 from ...

  5. HDU - 1541 Stars 【树状数组】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...

  6. JavaMail发送和接收邮件

    一.JavaMail概述:        JavaMail是由Sun定义的一套收发电子邮件的API,不同的厂商可以提供自己的实现类.但它并没有包含在JDK中,而是作为JavaEE的一部分. 厂商所提供 ...

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

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

  8. 2018.3.3 How too much fructose may cause liver damage

    Fructose is the sweetest of the natural sugars. As its name suggests, it is found mainly in fruits. ...

  9. 前端多媒体(1)——获取摄像头&麦克风

    捕获视频/音频 PPT地址 长久以来,音频/视频捕获都是网络开发中的"圣杯".多年来,我们总是依赖于浏览器插件(Flash 或 Silverlight)实现这一点. 依靠 WebR ...

  10. Linux下Fork与Exec

    一.引言 对于没有接触过Unix/Linux操作系统的人来说,fork是最难理解的概念之一:它执行一次却返回两个值.fork函数是Unix系统最杰出的成就之一,它是七十年代UNIX早期的开发者经过长期 ...