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算法)
博弈树 下过五子棋的人都应该知道,越厉害的人,对棋面的预测程度越深.换句话讲,就是当你下完一步棋,我就能在我的脑海里假设把我所有可能下的地方都下一遍,然后考虑我下完之后你又会下在哪里,最后我根据每次预 ...
随机推荐
- python manage.py shell 的增删改查
python manage.py shell 的增删改查 guoguo-MacBook-Pro:myblog guoguo$ python manage.py shell Python 3.5.1 ( ...
- CALL FUNCTION 'BAPI_GOODSMVT_CREATE'-(物料凭证创建)
*&---------------------------------------------------------------------* *& Report YTST_RAI ...
- spring mvc头
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- (转) 在linux网络UDP通信中,关于客户端是否绑定的理解
最近在做一个实例,是用RTSP协议完成.服务器已经有了,只需要把客户端做好就行了,在做的过程中发现了一些问题,就是关于UDP客户端是否绑定的问题. 也许大家在书上看到的大多都是说UDP客户端不需要绑定 ...
- Java for LeetCode 110 Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Wannafly挑战赛12 A 银行存款 【DP】【DFS】
链接:https://www.nowcoder.com/acm/contest/79/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- Machine Learning No.11: Recommender System
1. Content based Problem formulation Content Based Recommendations: 2. collaborative filtering algor ...
- webpack三种代码
在使用webpack时,主要有三种代码类型: 1.你或你的团队写的源码 2.第三方library或vendor代码 3.管理模块交互的runtime和manifest 什么是manifest文件? 通 ...
- POJ3693 Maximum repetition substring —— 后缀数组 重复次数最多的连续重复子串
题目链接:https://vjudge.net/problem/POJ-3693 Maximum repetition substring Time Limit: 1000MS Memory Li ...
- python二叉树的遍历,递归和非递归及相关其它
# encoding=utf-8class node(object): def __init__(self,data,left=None,right=None): self.data = data s ...