C++ 版本的 行为树的简单实现

//回到主人身边
class AINodeGotoOwnerSide : public AINodeBase
{
private:
static AINodeRegister<AINodeGotoOwnerSide> reg; public:
virtual bool invoke(int level = , bool isLog = false);
};
AINodeRegister<AINodeGotoOwnerSide> AINodeGotoOwnerSide::reg(ANT_GOTO_OWNER_SIDE, "AINodeGotoOwnerSide");
bool AINodeGotoOwnerSide::invoke(int level, bool isLog)
{
return rand() % > ;
}
AINodeDescribe des[] = {
AINodeDescribe(, , ANBT_SELECT, "根节点"),
AINodeDescribe(, , ANBT_SEQUENCE, "是否拾取金币的判定节点"),
AINodeDescribe(, , ANT_RELEASE_SKILL, "附近是否存在金币"),
AINodeDescribe(, , ANT_PICKING_UP_COINS, "捡取金币节点"),
AINodeDescribe(, , ANBT_SEQUENCE, "是否回到主人身边的判定节点"),
AINodeDescribe(, , ANT_RELEASE_SKILL, "是不是很长时间没有见到金币了"),
AINodeDescribe(, , ANT_PICKING_UP_COINS, "是不是很长时间没有回到主人身边了"),
AINodeDescribe(, , ANT_PICKING_UP_COINS, "回到主人身边的执行节点"),
AINodeDescribe(, , ANT_PICKING_UP_COINS, "没事随便逛逛吧"),
};
int desCount = sizeof(des) / sizeof(AINodeDescribe);
vector<AINodeDescribe> des_vtr;
for (int i = ; i < desCount; ++i)
{
des_vtr.push_back(des[i]);
}
AINodeBase * rootNode = AINodeHelper::sharedHelper()->CreateNodeTree(des_vtr);
cout << "\n状态结构组织图 \n" << endl;
AINodeHelper::sharedHelper()->printAITree(rootNode); cout << "\n状态结构组织图 \n" << endl;
for (int i = ; i < ; ++i)
{
cout << "调用树开始" << endl; rootNode->invoke(, true); cout << "调用树结束" << endl;
}
AITree.h
//
// AITree.h
// KPGranny2
//
// Created by bianchx on 15/9/15.
//
// #ifndef __KPGranny2__AITree__
#define __KPGranny2__AITree__ #include <stdio.h>
#include <vector>
#include <map>
#include <string> #include "AITreeNodeType.h" #pragma mark =============== public Helper Action ================== class AINodeBase; typedef AINodeBase * (* BaseNodeCreate)(); struct AINodeDescribe
{
AINodeDescribe()
{
memset(this, 0, sizeof(AINodeDescribe));
} AINodeDescribe(int id, int pId, int typeId, char * describe = NULL)
:Id(id)
,ParentId(pId)
,AINodeTypeId(typeId)
{
memset(Describe, 0, sizeof(Describe)); if(describe != NULL && strlen(describe) < sizeof(Describe) / sizeof(char))
{
strcpy(Describe, describe);
}
} int Id; //当期节点Id
int ParentId; //父节点Id
int AINodeTypeId; //智能节点类型Id
char Describe[256]; //节点名称
}; class AINodeHelper
{
private:
static AINodeHelper * m_nodeHlper; std::map<int, BaseNodeCreate> m_type2Create;
std::map<int, std::string> m_type2Name; public:
static AINodeHelper * sharedHelper(); void registerNodeCreate(int type, BaseNodeCreate create);
void registerNodeName(int type, std::string name); AINodeBase * CreateNode(int type); //创建节点 AINodeBase * CreateNodeTree(std::vector<AINodeDescribe> des, void * host = NULL); void printAITree(AINodeBase * node, int level = 0);
}; template <class T>
class AINodeRegister
{
public:
static AINodeBase * CreateT()
{
return new T();
} AINodeRegister(int type, std::string name = "")
{
AINodeHelper * helper = AINodeHelper::sharedHelper(); helper->registerNodeCreate(type, &AINodeRegister::CreateT); if(name != "")
helper->registerNodeName(type, name);
}
}; #pragma mark ================== 具体的内容 ================= enum AINodeBaseType
{
ANBT_SELECT, //选择节点
ANBT_SEQUENCE, //顺序节点
ANBT_NOT, //取反节点
}; class MemoryManagementObject
{
private:
int m_mmo_referenceCount; public:
MemoryManagementObject()
:m_mmo_referenceCount(1)
{
} virtual ~MemoryManagementObject()
{ } int getReferenceCount();
void retain();
void release();
}; class AINodeBase : public MemoryManagementObject
{
protected:
std::string m_nodeName;
std::string m_nodeDescribe; public:
AINodeBase()
:m_host(NULL)
,m_nodeName("AINodeBase")
,m_nodeDescribe("")
{
} virtual ~AINodeBase() { } void * m_host; //AI的宿主 virtual bool invoke(int level = 0, bool isLog = false) { return false; } virtual void destroy(); virtual void setDescribe(std::string describe);
virtual std::string getDescribe(); virtual void setName(std::string name);
virtual std::string getName();
}; //列表节点
class AIListNode : public AINodeBase
{
protected:
std::vector<AINodeBase *> m_childNodes; public:
virtual void addChildNode(AINodeBase * node);
virtual std::vector<AINodeBase *> & getChildNodes();
virtual void destroy();
}; //单条节点
class AISingleNode : public AINodeBase
{
protected:
AINodeBase * m_childNode; public:
AISingleNode()
:m_childNode(NULL)
{ } virtual void setChildNode(AINodeBase * node);
virtual AINodeBase * getChildNode();
virtual void destroy();
}; //选择节点
class AISelectNode : public AIListNode
{
private:
static AINodeRegister<AISelectNode> reg; public:
virtual bool invoke(int level = 0, bool isLog = false);
}; //顺序节点
class AISequenceNode : public AIListNode
{
private:
static AINodeRegister<AISequenceNode> reg; public:
virtual bool invoke(int level = 0, bool isLog = false);
}; //取反节点
class AINotNode : public AISingleNode
{
private:
static AINodeRegister<AINotNode> reg; public:
virtual bool invoke(int level = 0, bool isLog = false);
}; #endif /* defined(__KPGranny2__AITree__) */
AITree.cpp
//
// AITree.cpp
// KPGranny2
//
// Created by bianchx on 15/9/15.
//
// #include "AITree.h" #include <iostream>
#include <sstream> #define COMMAND_LINE 0
#define COCOS2D 1
#define AI_DEBUG 1 #if COCOS2D
#include "cocos2d.h"
#endif using namespace std; AINodeHelper * AINodeHelper::m_nodeHlper(NULL); AINodeHelper * AINodeHelper::sharedHelper()
{
if(m_nodeHlper == NULL)
m_nodeHlper = new AINodeHelper(); return m_nodeHlper;
} void AINodeHelper::registerNodeCreate(int type, BaseNodeCreate create)
{
m_type2Create[type] = create;
} void AINodeHelper::registerNodeName(int type, std::string name)
{
m_type2Name[type] = name;
} AINodeBase * AINodeHelper::CreateNode(int type)
{
AINodeBase * nodeBase = NULL; do
{
map<int, BaseNodeCreate>::iterator iter = m_type2Create.find(type); if(iter == m_type2Create.end())
break; nodeBase = (*iter).second(); if(nodeBase == NULL)
break; map<int, string>::iterator iter_name = m_type2Name.find(type);
if(iter_name != m_type2Name.end())
{
string & name = (*iter_name).second;
nodeBase->setName(name);
}
}while(0); return nodeBase;
} AINodeBase * AINodeHelper::CreateNodeTree(vector<AINodeDescribe> des, void * host)
{
if(des.size() == 0)
return NULL; #if COMMAND_LINE && AI_DEBUG
cout << "CreateNodeTree all count = " << des.size() << endl;
#endif #if COCOS2D && AI_DEBUG
CCLOG("CreateNodeTree all count = %d", (int)des.size());
#endif map<int, AINodeBase *> m_type2Create; AINodeBase * rootNode = NULL; for(vector<AINodeDescribe>::iterator iter = des.begin(); iter != des.end(); ++iter)
{
AINodeDescribe &item = (*iter);
AINodeBase * node = CreateNode(item.AINodeTypeId); #if COMMAND_LINE && AI_DEBUG
cout << "CreateNodeTree " << item.AINodeTypeId << endl;
#endif #if COCOS2D && AI_DEBUG
CCLOG("CreateNodeTree %d", item.AINodeTypeId);
#endif if(node == NULL)
continue; node->m_host = host; //注入宿主 if(strlen(item.Describe) != 0)
{
node->setDescribe(item.Describe);
} m_type2Create[item.Id] = node; if(item.ParentId == 0)
{
rootNode = node;
}
else
{
do
{
AINodeBase * parentNode = m_type2Create[item.ParentId];
if(parentNode == NULL)
break; AIListNode * listParentNode = dynamic_cast<AIListNode *>(parentNode);
if(listParentNode != NULL)
{
listParentNode->addChildNode(node);
break;
} AISingleNode * singleNode = dynamic_cast<AISingleNode *>(parentNode);
if(singleNode != NULL)
{
singleNode->setChildNode(node);
break;
} } while (0);
}
} return rootNode;
} void AINodeHelper::printAITree(AINodeBase * node, int level)
{
ostringstream oss; for (int i = 0; i < level; ++i)
{
oss << "\t";
} oss << node->getName() << " " << node->getDescribe() << " " << node; #if COMMAND_LINE
cout << oss.str().c_str() << endl;;
#endif #if COCOS2D
CCLOG(oss.str().c_str());
#endif do
{
AIListNode * listNode = dynamic_cast<AIListNode *>(node);
if(listNode != NULL)
{
vector<AINodeBase *> & childs = listNode->getChildNodes();
if(childs.size() > 0)
{
for (std::vector<AINodeBase *>::iterator i = childs.begin(); i != childs.end(); ++i)
{
printAITree(*i, level + 1);
}
} break;
} AISingleNode * singleNode = dynamic_cast<AISingleNode *>(node);
if(singleNode != NULL)
{
AINodeBase * child = singleNode->getChildNode();
if(child != NULL)
{
printAITree(child, level + 1);
}
}
} while (0); } int MemoryManagementObject::getReferenceCount()
{
return m_mmo_referenceCount;
} void MemoryManagementObject::retain()
{
++m_mmo_referenceCount;
} void MemoryManagementObject::release()
{
--m_mmo_referenceCount; if(m_mmo_referenceCount <= 0)
{
delete this;
}
} //最根层节点
void AINodeBase::destroy()
{
#if COMMAND_LINE && AI_DEBUG
cout << "destroy " << getName() << " " << this << endl;
#endif #if COCOS2D && AI_DEBUG
CCLOG("destroy %s %p", getName().c_str(), this);
#endif release();
} void AINodeBase::setDescribe(std::string describe)
{
m_nodeDescribe = describe;
} string AINodeBase::getDescribe()
{
return m_nodeDescribe;
} void AINodeBase::setName(string name)
{
m_nodeName = name;
} string AINodeBase::getName()
{
return m_nodeName;
} //列表节点
void AIListNode::addChildNode(AINodeBase * node)
{
m_childNodes.push_back(node);
} std::vector<AINodeBase *> & AIListNode::getChildNodes()
{
return m_childNodes;
} void AIListNode::destroy()
{
if(m_childNodes.size() > 0)
{
for(vector<AINodeBase *>::iterator iter = m_childNodes.begin(); iter != m_childNodes.end(); ++iter)
{
(*iter)->destroy();
}
} AINodeBase::destroy();
} //单条节点
void AISingleNode::setChildNode(AINodeBase * node)
{
if(m_childNode != node)
{
if(m_childNode != NULL)
m_childNode->destroy(); m_childNode = node;
}
} AINodeBase * AISingleNode::getChildNode()
{
return m_childNode;
} void AISingleNode::destroy()
{
if(m_childNode != NULL)
{
m_childNode->destroy();
} AINodeBase::destroy();
} //选择节点
AINodeRegister<AISelectNode> AISelectNode::reg(ANBT_SELECT, "AISelectNode"); bool AISelectNode::invoke(int level, bool isLog)
{
bool success = false; do
{
if(m_childNodes.size() == 0)
break; for(vector<AINodeBase *>::iterator iter = m_childNodes.begin(); iter != m_childNodes.end(); ++iter)
{
AINodeBase * node = (*iter);
bool inv = node->invoke(level + 1, isLog); #if (COMMAND_LINE || COCOS2D) && AI_DEBUG
ostringstream oss; for (int i = 0; i < level; ++i) {
oss << " ";
} oss << node->getName() << " invoke " << inv;
#endif #if COMMAND_LINE && AI_DEBUG
if(isLog)
{
cout << oss.str().c_str() << endl;
}
#endif #if COCOS2D && AI_DEBUG
if(isLog)
{
CCLOG("%s", oss.str().c_str());
}
#endif if(inv)
{
success = true;
break;
}
}
} while (false); return success;
} //顺序节点
AINodeRegister<AISequenceNode> AISequenceNode::reg(ANBT_SEQUENCE, "AISequenceNode"); bool AISequenceNode::invoke(int level, bool isLog)
{
bool success = true; do
{
for(vector<AINodeBase *>::iterator iter = m_childNodes.begin(); iter != m_childNodes.end(); ++iter)
{
AINodeBase * node = (*iter);
bool inv = node->invoke(level + 1, isLog); #if (COMMAND_LINE || COCOS2D) && AI_DEBUG
ostringstream oss; for (int i = 0; i < level; ++i) {
oss << " ";
} oss << node->getName() << " invoke " << inv;
#endif #if COMMAND_LINE && AI_DEBUG
if(isLog)
{
cout << oss.str() << endl;
}
#endif #if COCOS2D && AI_DEBUG
if(isLog)
{
CCLOG("%s", oss.str().c_str());
}
#endif if(inv == false)
{
success = false;
break;
}
}
} while (false); return success;
} //取反节点
AINodeRegister<AINotNode> AINotNode::reg(ANBT_NOT, "AINotNode"); bool AINotNode::invoke(int level, bool isLog)
{
bool success = false; do
{
if(m_childNode == NULL)
break; success = !(m_childNode->invoke(level + 1, isLog));
} while (false); #if (COMMAND_LINE || COCOS2D) && AI_DEBUG
ostringstream oss; for (int i = 0; i < level; ++i) {
oss << " ";
} if(m_childNode != NULL)
{
oss << m_childNode->getName() << " invoke " << !success;
}
else
{
oss << "no child";
}
#endif #if COMMAND_LINE && AI_DEBUG
if(isLog)
{
cout << oss.str() << endl;
}
#endif #if COCOS2D && AI_DEBUG
if(isLog)
{
CCLOG("no child");
}
#endif return success;
}
C++ 版本的 行为树的简单实现的更多相关文章
- 『zkw线段树及其简单运用』
阅读本文前,请确保已经阅读并理解了如下两篇文章: 『线段树 Segment Tree』 『线段树简单运用』 引入 这是一种由\(THU-zkw\)大佬发明的数据结构,本质上是经典的线段树区间划分思想, ...
- 【ARM-Linux开发】内核3.x版本之后设备树机制
内核3.x版本之后设备树机制 Based on Linux 3.10.24 source code 参考/documentation/devicetree/Booting-without- ...
- 《机器学习Python实现_10_10_集成学习_xgboost_原理介绍及回归树的简单实现》
一.简介 xgboost在集成学习中占有重要的一席之位,通常在各大竞赛中作为杀器使用,同时它在工业落地上也很方便,目前针对大数据领域也有各种分布式实现版本,比如xgboost4j-spark,xgbo ...
- RDIFramework.NET V2.7 Web版本升手风琴+树型目录(2级+)方法
RDIFramework.NET V2.7 Web版本升手风琴+树型目录(2级+)方法 手风琴风格在Web应用非常的普遍,越来越多的Web应用都是采用这种方式来体现各个功能模块,传统的手风琴风格只支持 ...
- LA、Remember the Word (字典树, 简单dp)
传送门 题意: 给你一个初始串 S,strlen(s) <= 3e5 然后给你 n 个单词. n <= 4000, 每个单词的长度不超过 100 : 问你这个初始串,分割成若干个单词的 ...
- 动态树(Link-Cut-Tree)简单总结(指针版本)
Link-Cut-Tree(动态树 LCT) 1.定义 1. 偏爱子节点: 一颗子树内最后访问的点若在该子树根节点 X 的某个儿子节点 P 的子树中,则称 P 为 X 的偏爱子节点. 偏爱边:连向偏爱 ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 用户权限树的实现 -- 权限递归树
业务系统里经常会需要计算类似的树形权限树的业务需求 1:往往会有一些需求,a 对 b 有权限, b对c 有权限, 等等. 2:还需要很直观的看到,整个权限的树形关系,一目了然的那种. 3:程序调用简单 ...
- POJ 3630 Phone List(trie树的简单应用)
题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...
- 比特币区块结构Merkle树及简单支付验证分析
在比特币网络中,不是每个节点都有能力储存完整的区块链数据,受限于存储空间的的限制,很多节点是以SPV(Simplified Payment Verification简单支付验证)钱包接入比特币网络,通 ...
随机推荐
- .Net客户端监听ZooKeeper节点数据变化
一个很简单的例子,用途是监听zookeeper中某个节点数据的变化,具体请参见代码中的注释 using System; using System.Collections.Generic; using ...
- 电子商务网站SQL注入项目实战一例
故事A段:发现整站SQL对外输出: 有个朋友的网站,由于是外包项目,深圳某公司开发的,某天我帮他检测了一下网站相关情况. 我查看了页面源代码,发现了个惊人的事情,竟然整站打印SQL到Html里,着实吓 ...
- Asp.Net MVC 分页、检索、排序整体实现
很多时候需要这样的功能,对表格进行分页.排序和检索.这个有很多实现的方式,有现成的表格控件.用前端的mvvm,用户控件.但很多时候看着很漂亮的东西你想进一步控制的时候却不那么如意.这里自己实现一次,功 ...
- HTTPS那些事(二)SSL证书(转载)
原创地址:http://www.guokr.com/post/116169/ 从第一部分HTTP工作原理中,我们可以了解到HTTPS核心的一个部分是数据传输之前的握手,握手过程中确定了数据加密的密 ...
- Redis 发布订阅用法
一.发布订阅模型发布订阅其作用是为了减少依赖关系,通常也叫观察者模式.主要是把耦合点单独抽离出来作为第三方,隔离易变化的发送方和接收方. 发送方:只负责向第三方发送消息.(杂志社把读者杂志交给邮局)接 ...
- Atitit图像处理的用途
Atitit图像处理的用途 1.1. 分类识别 (人脸检测,肤色识别,人类检测:1 1.2. 炫丽的动态按钮生成:色相旋转+自己的草书等图片合成,图片自动裁剪1 1.3. 集成调用自绘gui接口:识别 ...
- slave IO流程之二:注册slave请求和dump请求
slave IO流程已经在http://www.cnblogs.com/onlyac/p/5815566.html中有介绍 这次我们要探索注册slave请求和dump请求的报文格式和主要流程. 一.注 ...
- jarsigner签名报错Invalid keystore format
由于之前在魅族市场的APK包都不是自己上传的,而是魅族从其他安卓市场帮拉去过来了. 所以需要我们自己去认领APK包. 这个时候就需要按照魅族给的未签名测试包给重新签名然后提交审核了. 1:看完以下说明 ...
- Struts框架
Struts是最早的Java开源框架之一,它是MVC设计模式的一个优秀实现. Struts定义了通用的Controller(控制器),通过配置文件(通常是 Struts -config.xml) Ec ...
- HTTP学习一:HTTP基础知识
1 HTTP介绍 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传送协议. 它的发展是万维网协会(World Wid ...