AI决策算法 之 GOAP (三)
源码地址:http://pan.baidu.com/s/1dFwzmfB
这篇我们使用上篇文章写的GOAP框架来完成一个实例:
实例内容:
AI有10HP, 需要去站岗,站岗完成扣5HP
当HP<=5必须去补充HP,找到HP球补充HP,每个HP球补充5HP
根据GOAP框架逻辑推断出需要:AI数据提供者,站岗Action,补充HPAction,HP点脚本,站岗点脚本, AI属性脚本
主要脚本:
AI数据提供者
- public class Worker : MonoBehaviour,IGoap{
- private PropertyComponent property; //属性脚本
- public float moveSpeed = 3; //移动速度
- void Start()
- {
- property = GetComponent<PropertyComponent>();
- }
- public System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> GetState()
- {
- HashSet<KeyValuePair<string, object>> state = new HashSet<KeyValuePair<string, object>>();
- //当前状态HP是否足够
- state.Add(new KeyValuePair<string, object>("EnoughHP", property.HP > 5));
- return state;
- }
- public System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> CreateGoalState()
- {
- HashSet<KeyValuePair<string, object>> goal = new HashSet<KeyValuePair<string, object>>();
- //站岗完成目标
- goal.Add(new KeyValuePair<string, object>("SentryComplete", true));
- return goal;
- }
- public void PlanFailed(System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> failedGoal)
- {
- }
- public void PlanFound(System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> goal, System.Collections.Generic.Queue<Action> actions)
- {
- }
- public void ActionsFinished()
- {
- Debug.LogError("FinishedSentry");
- }
- public void PlanAborted(Action aborterAction)
- {
- }
- public bool MoveAgent(Action tagetAction)
- {
- //移动
- float step = moveSpeed * Time.deltaTime;
- gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, tagetAction.target.transform.position, step);
- if (gameObject.transform.position.Equals(tagetAction.target.transform.position))
- {
- tagetAction.IsInRange = true;
- return true;
- }
- else
- return false;
- }
- }
站岗Action
- public class SentryAction : Action {
- private SentryComponent targetSentry; //站岗目标脚本
- private float SentryTimer = 0; //站岗计时
- public float SentryTime = 3;
- bool isDone = false; //是否完成
- public SentryAction()
- {
- AddPrecondition("EnoughHP", true); //前置条件:必须HP足够
- AddEffect("SentryComplete", true); //完成效果:站岗完成
- }
- public override void Reset()
- {
- targetSentry = null;
- SentryTimer = 0;
- isDone = false;
- }
- public override bool IsDone()
- {
- return isDone;
- }
- public override bool CheckProcedualPrecondition(GameObject agent)
- {
- //得到最近的站岗目标
- SentryComponent[] sentryComponents = GameObject.FindObjectsOfType<SentryComponent>();
- SentryComponent temp = null;
- foreach(var v in sentryComponents)
- {
- if (temp == null)
- {
- temp = v;
- continue;
- }
- if (Vector3.Distance(agent.transform.position, v.transform.position) < Vector3.Distance(agent.transform.position, temp.transform.position))
- temp = v;
- }
- targetSentry = temp;
- target = temp.gameObject;
- return temp != null;
- }
- public override bool Perform(GameObject agent)
- {
- //站岗执行
- SentryTimer += Time.deltaTime;
- if(SentryTimer > SentryTime)
- {
- //站岗完成消耗HP
- agent.GetComponent<PropertyComponent>().HP -= 5;
- isDone = true;
- }
- return true;
- }
- public override bool RequiresInRange()
- {
- return true;
- }
- }
补充HPAction
- public class GetHPAction : Action {
- private HPPointComponent targetHPPoint; //HP点目标脚本
- bool isDone = false;
- void Start()
- {
- AddEffect("EnoughHP", true); //完成效果:HP补充到足够
- }
- public override void Reset()
- {
- targetHPPoint = null;
- isDone = false;
- }
- public override bool IsDone()
- {
- return isDone;
- }
- public override bool CheckProcedualPrecondition(GameObject agent)
- {
- HPPointComponent[] tempComponents = GameObject.FindObjectsOfType<HPPointComponent>();
- HPPointComponent temp = null;
- foreach (var v in tempComponents)
- {
- if (temp == null)
- {
- temp = v;
- continue;
- }
- if (Vector3.Distance(agent.transform.position, v.transform.position) < Vector3.Distance(agent.transform.position, temp.transform.position))
- temp = v;
- }
- targetHPPoint = temp;
- target = temp.gameObject;
- return temp != null;
- }
- public override bool Perform(GameObject agent)
- {
- DestroyImmediate(targetHPPoint.gameObject);
- isDone = true;
- agent.GetComponent<PropertyComponent>().HP += 5;
- return true;
- }
- public override bool RequiresInRange()
- {
- return true;
- }
- }
AI决策算法 之 GOAP (三)的更多相关文章
- AI决策算法 之 GOAP (一)
http://blog.csdn.net/lovethrain/article/details/67632033 本系列文章内容部分参考自:http://gamerboom.com/archives/ ...
- AI决策算法 之 GOAP (二)
http://blog.csdn.net/lovethRain/article/details/67634803 GOAP 的主要逻辑: 1.Agent的状态机初始化Idle状态 2.Idel状态根据 ...
- 贝叶斯公式由浅入深大讲解—AI基础算法入门
1 贝叶斯方法 长久以来,人们对一件事情发生或不发生的概率,只有固定的0和1,即要么发生,要么不发生,从来不会去考虑某件事情发生的概率有多大,不发生的概率又是多大.而且概率虽然未知,但最起码是一个确定 ...
- 贝叶斯公式由浅入深大讲解—AI基础算法入门【转】
本文转载自:https://www.cnblogs.com/zhoulujun/p/8893393.html 1 贝叶斯方法 长久以来,人们对一件事情发生或不发生的概率,只有固定的0和1,即要么发生, ...
- 2018科大讯飞AI营销算法大赛全面来袭,等你来战!
AI技术已成为推动营销迭代的重要驱动力.AI营销高速发展的同时,积累了海量的广告数据和用户数据.如何有效应用这些数据,是大数据技术落地营销领域的关键,也是检测智能营销平台竞争力的标准. 讯飞AI营销云 ...
- 多维算法思考(三):AB组合问题
多维算法思考(三):AB组合问题 题目:x个A,y个B可以组合成多少个不同排列的问题. 首先,我们用数学的方式思考,这个问题属于<组合数学>的问题,我们的第一种方法可以用组合思路来求解. ...
- 算法 排序lowB三人组 冒泡排序 选择排序 插入排序
参考博客:基于python的七种经典排序算法 [经典排序算法][集锦] 经典排序算法及python实现 首先明确,算法的实质 是 列表排序.具体就是操作的列表,将无序列表变成有序列表! 一 ...
- 实践案例丨基于ModelArts AI市场算法MobileNet_v2实现花卉分类
概述 MobileNetsV2是基于一个流线型的架构,它使用深度可分离的卷积来构建轻量级的深层神经网,此模型基于 MobileNetV2: Inverted Residuals and Linear ...
- 五子棋 AI(AIpha-beta算法)
博弈树 下过五子棋的人都应该知道,越厉害的人,对棋面的预测程度越深.换句话讲,就是当你下完一步棋,我就能在我的脑海里假设把我所有可能下的地方都下一遍,然后考虑我下完之后你又会下在哪里,最后我根据每次预 ...
随机推荐
- php依据地理坐标获取国家、省份、城市,及周边数据类
功能:当App获取到用户的地理坐标时,能够依据坐标知道用户当前在那个国家.省份.城市.及周边有什么数据. 原理:基于百度Geocoding API 实现.须要先注冊百度开发人员.然后申请百度AK(密钥 ...
- ZookeeperclientAPI之创建会话(六)
Zookeeper对外提供了一套Java的clientAPI. 本篇博客主要讲一下创建会话. 创建项目 首选,创建一个基于maven管理的简单javaproject.在pom文件里引入zookeepe ...
- 【shell】判断一个变量是否为空
#!/bin/bash argv=" if [ -z "$argv" ] then echo "argv is empty" else echo &q ...
- eclipse显示adb is down错误,无法真机调试
cmd进入adb目录下,运行adb kill-server 和 adb start-server还是不能正常调试时, 在360的网络连接列表中找到占用端口5037的adb.exe,全部关闭,重启ecl ...
- POJ 3744 Scout YYF I:概率dp
题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问 ...
- 分享知识-快乐自己:Hibernate中的 quert.list() 与 quert.iterate() 方法区别
区别如下: quert.list() : 1):每次都是通过一条语句直接操作数据库取出所有的数据返回(并且将对象存入hibernate缓存中): 2):不会从一二级缓存中查询数据: 3):之执行一条S ...
- 改善C#程序的建议10:用Parallel简化Task
在命名空间System.Threading.Tasks下,有一个静态类Parallel简化了在同步状态下的Task的操作.Parallel主要提供了3个有用的方法:For.ForEach.Invoke ...
- struts2的结果类型
1.从struts-default.xml入手,得到结果类型列表以及对应的处理类: <result-types> <!-- 转发到action --> <result-t ...
- 局域网扫描IP
今天有朋友去面试,被问到一个“如何扫描局域网IP”的问题(即找出局域网中当前已使用的IP),朋友回答的不好,回来问我,我首先想到的就是使用ping命令将局域网可分配的IP地址逐个遍历一遍,能ping通 ...
- animation-delay负值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...