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算法)
博弈树 下过五子棋的人都应该知道,越厉害的人,对棋面的预测程度越深.换句话讲,就是当你下完一步棋,我就能在我的脑海里假设把我所有可能下的地方都下一遍,然后考虑我下完之后你又会下在哪里,最后我根据每次预 ...
随机推荐
- BTC、BCH和BSV三者到底有什么区别?
比特币发展到今天已经有10个年头了,在这十年的发展中,比特币一共经历了两次重要的分裂,现在变成了三种货币,第一种是目前继承了比特币绝大多数遗产的BTC:第二种是BCH:第三种是BSV.那这三种货币到底 ...
- 怎样做大做强企业中的ERP?
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luozhonghua2014/article/details/37672409 ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- FZU1989 AntiAC —— 字符串
题目链接:https://vjudge.net/problem/FZU-1989 Problem 1989 AntiAC Accept: 79 Submit: 399Time Limit: 4 ...
- BZOJ 1605 [Usaco2008 Open]Crisis on the Farm 牧场危机:dp【找转移路径】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1605 题意: 平面直角坐标系中,有n个点,m个标记(坐标范围1~1000). 你可以发出口 ...
- URL过滤
URL过滤 就是网址过滤.把不安全的.少儿不宜的.政治的东西过滤掉,访问这些网址就会提示受限,不能访问. 一.url过滤简介 针对企业对员工上网行为的控制管理,可以采用URL过滤技术.如企业不允许研发 ...
- codeforces 658B B. Bear and Displayed Friends(优先队列)
题目链接: B. Bear and Displayed Friends time limit per test 2 seconds memory limit per test 256 megabyte ...
- hdu-3078 Network(lca+st算法+dfs)
题目链接: Network Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) P ...
- BZOJ-4003:城池攻占(可并堆+lazy标记)
小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 到 n 的整数表示.除 号城池外,城池 i 会受到另一座城池 fi 的管辖, 其中 fi <i.也就是 ...
- MySQL11月16-11月21日活动赠送的优惠券使用率_20161124
一.11.16到21号活动规则是 单笔订单最高的金额划分客户为399,799,1599元三档 达标的分别赠送对应的优惠券 优惠券ID有标号区间 THEN "1599档" ELSE ...