源码地址: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. Linux搭建FTP服务器实战

    首先准备一台Linux系统机器(虚拟机也可), 检测出是否安装了vsftpd软件: rpm -qa |grep vsftpd 如果没有输出结果,就是没有安装. 使用命令安装,安装过程中会有提示,直接输 ...

  2. 应用索引技术优化SQL 语句(转)

    原文出处 一.前言 很多数据库系统性能不理想是因为系统没有经过整体优化,存在大量性能低下的SQL 语句.这类SQL语句性能不好的首要原因是缺乏高效的索引.没有索引除了导致语句本身运行速度慢外,更是导致 ...

  3. requests不加代理

    requests里的proxies不加代理可以设置为空,就会使用本机IP proxies={}

  4. Why use async requests instead of using a larger threadpool?(转载)

    问: During the Techdays here in the Netherlands Steve Sanderson gave a presentation about C#5, ASP.NE ...

  5. 从mediaserver入手快速理解binder机制(最简单理解binder)【转】

    本文转载自;https://blog.csdn.net/u010164190/article/details/53015194 Android的binder机制提供一种进程间通信的方法,使一个进程可以 ...

  6. Dubbo之生产者

    环境步骤: 安装Zookeepr启动 创建Maven项目搭建生产者和消费者 安装DubboAdmin平台,实现监控 Dubbo注册中心采用的是Zookeeper.为什么采用Zookeeper呢? Zo ...

  7. smokeping高级配置

    摘自: http://mayulin.blog.51cto.com/1628315/514367 自定义报警 http://www.cnblogs.com/thatsit/p/6395506.html

  8. apache-tomcat 及对应eclipse下载地址for mac

    tomcat 7.0.42http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.42/bin/apache-tomcat-7.0.42.zip ...

  9. ORA-00600: internal error code, arguments: [6749], [3], [12602196]

    环境信息:Linux5.8 oracle10.2.0.4 问题现象: 现象1:alert日志有大量下面的错误信息: Wed Aug 27 21:01:27 2014Errors in file /u0 ...

  10. 最近火狐浏览器 总是“插件 adobe flash 已崩溃”

    原因和解决方案:在地址栏中输入:about:addons>在如下地方发现firefox已经在警告该插件的安全性了>选择“总不激活”