Răsfoiți Sursa

1.NodeIf重命名为BehaviorNodeIf
2.增加了dota ai行为树图

tanghai 14 ani în urmă
părinte
comite
8fc0f0674e

+ 48 - 0
Cpp/Game/BehaviorTree/BehaviorNodeIf.h

@@ -0,0 +1,48 @@
+#ifndef BEHAVIORTREE_BEHAVIORNODEIF_H
+#define BEHAVIORTREE_BEHAVIORNODEIF_H
+
+#include <string>
+#include "Base/Typedef.h"
+
+namespace Egametang {
+
+class ContexIf;
+class BehaviorNodeConf;
+
+class BehaviorNodeIf
+{
+private:
+	int32 type;
+
+public:
+	BehaviorNodeIf(int32 type): type(type)
+	{
+	}
+
+	virtual ~BehaviorNodeIf()
+	{
+	}
+
+	int32 Type() const
+	{
+		return type;
+	}
+
+	virtual void AddChildNode(BehaviorNodeIf *node)
+	{
+	}
+
+	virtual bool Run(ContexIf* contex) = 0;
+
+	virtual std::string ToString() = 0;
+};
+
+class BehaviorNodeFactoryIf
+{
+public:
+	virtual BehaviorNodeIf* GetInstance(const BehaviorNodeConf& conf) = 0;
+};
+
+} // namespace Egametang
+
+#endif // BEHAVIORTREE_BEHAVIORNODEIF_H

+ 2 - 2
Cpp/Game/BehaviorTree/BehaviorTree.cc

@@ -22,14 +22,14 @@ BehaviorTree::BehaviorTree(NodeFactories& factories, const BehaviorTreeConf& tre
 
 void BehaviorTree::BuildTree(
 		NodeFactories& factories, const BehaviorNodeConf& node_conf,
-		NodeIf*& node)
+		BehaviorNodeIf*& node)
 {
 	int32 type = node_conf.type();
 	node = factories.GetInstance(node_conf);
 	for (int i = 0; i < node_conf.node_size(); ++i)
 	{
 		const BehaviorNodeConf& logic_node_conf = node_conf.node(i);
-		NodeIf* logic_node = NULL;
+		BehaviorNodeIf* logic_node = NULL;
 		BuildTree(factories, logic_node_conf, logic_node);
 		node->AddChildNode(logic_node);
 	}

+ 3 - 3
Cpp/Game/BehaviorTree/BehaviorTree.h

@@ -1,7 +1,7 @@
 #ifndef BEHAVIORTREE_BEHAVIORTREE_H
 #define BEHAVIORTREE_BEHAVIORTREE_H
 
-#include "BehaviorTree/NodeIf.h"
+#include "BehaviorTree/BehaviorNodeIf.h"
 
 namespace Egametang {
 
@@ -13,10 +13,10 @@ class BehaviorTree
 {
 private:
 	int type;
-	NodeIf* node;
+	BehaviorNodeIf* node;
 
 	void BuildTree(NodeFactories& factories, const BehaviorNodeConf& node_conf,
-			NodeIf*& node);
+			BehaviorNodeIf*& node);
 
 public:
 	BehaviorTree(NodeFactories& factories, const BehaviorTreeConf& tree_conf);

+ 1 - 1
Cpp/Game/BehaviorTree/BehaviorTreeConf.proto

@@ -11,7 +11,7 @@ message BehaviorNodeConf
 
 message BehaviorTreeConf
 {
-	// 行为树类型
+	// 行为树类型: AI, ON_HIT, ON_HITTED ...
 	required int32 type = 1;
 	// 行为树节点
 	required BehaviorNodeConf node = 2;

+ 4 - 3
Cpp/Game/BehaviorTree/BuffType.cc

@@ -5,7 +5,8 @@
 
 namespace Egametang {
 
-BuffType::BuffType(int buff_type): buff_type(buff_type)
+BuffType::BuffType(int32 type, int32 buff_type):
+		BehaviorNodeIf(type), buff_type(buff_type)
 {
 }
 
@@ -30,9 +31,9 @@ BuffTypeFactory::~BuffTypeFactory()
 {
 }
 
-NodeIf* BuffTypeFactory::GetInstance(const BehaviorNodeConf& conf)
+BehaviorNodeIf* BuffTypeFactory::GetInstance(const BehaviorNodeConf& conf)
 {
-	return new BuffType(conf.args(0));
+	return new BuffType(conf.type(), conf.args(0));
 }
 
 } // namespace Egametang

+ 6 - 6
Cpp/Game/BehaviorTree/BuffType.h

@@ -1,7 +1,7 @@
 #ifndef BEHAVIORTREE_BUFFTYPE_H
 #define BEHAVIORTREE_BUFFTYPE_H
 
-#include "BehaviorTree/NodeIf.h"
+#include "BehaviorTree/BehaviorNodeIf.h"
 
 namespace Egametang {
 
@@ -10,13 +10,13 @@ class ContexIf;
 // 条件节点还可以预绑定一些配置参数,
 // 例如下面的buff_type字段由策划配置
 // 可配置成dot hot之类的, 由工厂类设置
-class BuffType: public NodeIf
+class BuffType: public BehaviorNodeIf
 {
 private:
-	int buff_type;
+	int32 buff_type;
 
 public:
-	BuffType(int buff_type);
+	BuffType(int32 type, int buff_type);
 
 	virtual ~BuffType();
 
@@ -25,12 +25,12 @@ public:
 	virtual std::string ToString();
 };
 
-class BuffTypeFactory: public NodeFactoryIf
+class BuffTypeFactory: public BehaviorNodeFactoryIf
 {
 public:
 	virtual ~BuffTypeFactory();
 
-	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
+	virtual BehaviorNodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang

+ 4 - 4
Cpp/Game/BehaviorTree/ChangeHealth.cc

@@ -6,8 +6,8 @@
 
 namespace Egametang {
 
-ChangeHealth::ChangeHealth(int32 unit, int32 value):
-		unit(unit), value(value)
+ChangeHealth::ChangeHealth(int32 type, int32 unit, int32 value):
+		BehaviorNodeIf(type), unit(unit), value(value)
 {
 }
 
@@ -41,9 +41,9 @@ std::string ChangeHealth::ToString()
 	return s;
 }
 
-NodeIf* ChangeHealthFactory::GetInstance(const BehaviorNodeConf& conf)
+BehaviorNodeIf* ChangeHealthFactory::GetInstance(const BehaviorNodeConf& conf)
 {
-	return new ChangeHealth(conf.args(0), conf.args(1));
+	return new ChangeHealth(conf.type(), conf.args(0), conf.args(1));
 }
 
 } // namespace Egametang

+ 5 - 5
Cpp/Game/BehaviorTree/ChangeHealth.h

@@ -2,18 +2,18 @@
 #define BEHAVIORTREE_CHANGEHEALTH_H
 
 #include "Base/Typedef.h"
-#include "BehaviorTree/NodeIf.h"
+#include "BehaviorTree/BehaviorNodeIf.h"
 
 namespace Egametang {
 
-class ChangeHealth: public NodeIf
+class ChangeHealth: public BehaviorNodeIf
 {
 private:
 	int32 unit;
 	int32 value;
 
 public:
-	ChangeHealth(int32 unit, int32 value);
+	ChangeHealth(int32 type, int32 unit, int32 value);
 
 	virtual ~ChangeHealth();
 
@@ -22,10 +22,10 @@ public:
 	virtual std::string ToString();
 };
 
-class ChangeHealthFactory: public NodeFactoryIf
+class ChangeHealthFactory: public BehaviorNodeFactoryIf
 {
 public:
-	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
+	virtual BehaviorNodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang

+ 2 - 2
Cpp/Game/BehaviorTree/NodeFactories.cc

@@ -11,7 +11,7 @@
 
 namespace Egametang {
 
-NodeFactories::NodeFactories(): factories(2000, (NodeFactoryIf*)(NULL))
+NodeFactories::NodeFactories(): factories(2000, (BehaviorNodeFactoryIf*)(NULL))
 {
 	// 节点
 	factories[SEQUENCE] = new SequenceNodeFactory();
@@ -35,7 +35,7 @@ NodeFactories::~NodeFactories()
 	}
 }
 
-NodeIf* NodeFactories::GetInstance(const BehaviorNodeConf& conf)
+BehaviorNodeIf* NodeFactories::GetInstance(const BehaviorNodeConf& conf)
 {
 	int32 type = conf.type();
 	return factories[type]->GetInstance(conf);

+ 3 - 3
Cpp/Game/BehaviorTree/NodeFactories.h

@@ -2,21 +2,21 @@
 #define BEHAVIORTREE_NODEFACTORIES_H
 
 #include <vector>
-#include "BehaviorTree/NodeIf.h"
+#include "BehaviorTree/BehaviorNodeIf.h"
 
 namespace Egametang {
 
 class NodeFactories
 {
 private:
-	std::vector<NodeFactoryIf*> factories;
+	std::vector<BehaviorNodeFactoryIf*> factories;
 
 public:
 	NodeFactories();
 
 	virtual ~NodeFactories();
 
-	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
+	virtual BehaviorNodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang

+ 0 - 34
Cpp/Game/BehaviorTree/NodeIf.h

@@ -1,34 +0,0 @@
-#ifndef BEHAVIORTREE_NODEIF_H
-#define BEHAVIORTREE_NODEIF_H
-
-#include <string>
-
-namespace Egametang {
-
-class ContexIf;
-class BehaviorNodeConf;
-
-class NodeIf
-{
-public:
-	virtual ~NodeIf()
-	{
-	}
-	virtual bool Run(ContexIf* contex) = 0;
-
-	virtual void AddChildNode(NodeIf *node)
-	{
-	}
-
-	virtual std::string ToString() = 0;
-};
-
-class NodeFactoryIf
-{
-public:
-	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf) = 0;
-};
-
-} // namespace Egametang
-
-#endif // BEHAVIORTREE_NODEIF_H

+ 5 - 4
Cpp/Game/BehaviorTree/NotNode.cc

@@ -1,9 +1,10 @@
 #include <stddef.h>
 #include "BehaviorTree/NotNode.h"
+#include "BehaviorTree/BehaviorTreeConf.pb.h"
 
 namespace Egametang {
 
-NotNode::NotNode(): node(NULL)
+NotNode::NotNode(int32 type): BehaviorNodeIf(type), node(NULL)
 {
 }
 
@@ -17,7 +18,7 @@ bool NotNode::Run(ContexIf* contex)
 	return !node->Run(contex);
 }
 
-void NotNode::AddChildNode(NodeIf *node)
+void NotNode::AddChildNode(BehaviorNodeIf *node)
 {
 	this->node = node;
 }
@@ -30,9 +31,9 @@ std::string NotNode::ToString()
 	return s;
 }
 
-NodeIf* NotNodeFactory::GetInstance(const BehaviorNodeConf& conf)
+BehaviorNodeIf* NotNodeFactory::GetInstance(const BehaviorNodeConf& conf)
 {
-	return new NotNode();
+	return new NotNode(conf.type());
 }
 
 } // namespace Egametang

+ 7 - 7
Cpp/Game/BehaviorTree/NotNode.h

@@ -1,31 +1,31 @@
 #ifndef BEHAVIORTREE_NOTNODE_H
 #define BEHAVIORTREE_NOTNODE_H
 
-#include "BehaviorTree/NodeIf.h"
+#include "BehaviorTree/BehaviorNodeIf.h"
 
 namespace Egametang {
 
-class NotNode: public NodeIf
+class NotNode: public BehaviorNodeIf
 {
 private:
-	NodeIf* node;
+	BehaviorNodeIf* node;
 
 public:
-	NotNode();
+	NotNode(int32 type);
 
 	virtual ~NotNode();
 
 	virtual bool Run(ContexIf* contex);
 
-	virtual void AddChildNode(NodeIf *node);
+	virtual void AddChildNode(BehaviorNodeIf *node);
 
 	virtual std::string ToString();
 };
 
-class NotNodeFactory: public NodeFactoryIf
+class NotNodeFactory: public BehaviorNodeFactoryIf
 {
 public:
-	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
+	virtual BehaviorNodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang

+ 11 - 6
Cpp/Game/BehaviorTree/SelectorNode.cc

@@ -1,12 +1,17 @@
 #include <boost/foreach.hpp>
 #include "Base/Marcos.h"
 #include "BehaviorTree/SelectorNode.h"
+#include "BehaviorTree/BehaviorTreeConf.pb.h"
 
 namespace Egametang {
 
+SelectorNode::SelectorNode(int32 type): BehaviorNodeIf(type)
+{
+}
+
 SelectorNode::~SelectorNode()
 {
-	foreach(NodeIf* node, nodes)
+	foreach(BehaviorNodeIf* node, nodes)
 	{
 		delete node;
 	}
@@ -14,7 +19,7 @@ SelectorNode::~SelectorNode()
 
 bool SelectorNode::Run(ContexIf* contex)
 {
-	foreach(NodeIf* node, nodes)
+	foreach(BehaviorNodeIf* node, nodes)
 	{
 		if (node->Run(contex))
 		{
@@ -24,7 +29,7 @@ bool SelectorNode::Run(ContexIf* contex)
 	return false;
 }
 
-void SelectorNode::AddChildNode(NodeIf *node)
+void SelectorNode::AddChildNode(BehaviorNodeIf *node)
 {
 	nodes.push_back(node);
 }
@@ -33,16 +38,16 @@ std::string SelectorNode::ToString()
 {
 	std::string s;
 	s += "SelectorNode: \n";
-	foreach(NodeIf* node, nodes)
+	foreach(BehaviorNodeIf* node, nodes)
 	{
 		s += "    " + node->ToString() + "\n";
 	}
 	return s;
 }
 
-NodeIf* SelectorNodeFactory::GetInstance(const BehaviorNodeConf& conf)
+BehaviorNodeIf* SelectorNodeFactory::GetInstance(const BehaviorNodeConf& conf)
 {
-	return new SelectorNode();
+	return new SelectorNode(conf.type());
 }
 
 } // namespace Egametang

+ 8 - 6
Cpp/Game/BehaviorTree/SelectorNode.h

@@ -2,29 +2,31 @@
 #define BEHAVIORTREE_SELECTORNODE_H
 
 #include <list>
-#include "BehaviorTree/NodeIf.h"
+#include "BehaviorTree/BehaviorNodeIf.h"
 
 namespace Egametang {
 
-class SelectorNode: public NodeIf
+class SelectorNode: public BehaviorNodeIf
 {
 private:
-	std::list<NodeIf*> nodes;
+	std::list<BehaviorNodeIf*> nodes;
 
 public:
+	SelectorNode(int32 type);
+
 	virtual ~SelectorNode();
 
 	virtual bool Run(ContexIf* contex);
 
-	virtual void AddChildNode(NodeIf *node);
+	virtual void AddChildNode(BehaviorNodeIf *node);
 
 	virtual std::string ToString();
 };
 
-class SelectorNodeFactory: public NodeFactoryIf
+class SelectorNodeFactory: public BehaviorNodeFactoryIf
 {
 public:
-	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
+	virtual BehaviorNodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang

+ 11 - 6
Cpp/Game/BehaviorTree/SequenceNode.cc

@@ -2,12 +2,17 @@
 #include <boost/foreach.hpp>
 #include "Base/Marcos.h"
 #include "BehaviorTree/SequenceNode.h"
+#include "BehaviorTree/BehaviorTreeConf.pb.h"
 
 namespace Egametang {
 
+SequenceNode::SequenceNode(int32 type): BehaviorNodeIf(type)
+{
+}
+
 SequenceNode::~SequenceNode()
 {
-	foreach(NodeIf* node, nodes)
+	foreach(BehaviorNodeIf* node, nodes)
 	{
 		delete node;
 	}
@@ -15,7 +20,7 @@ SequenceNode::~SequenceNode()
 
 bool SequenceNode::Run(ContexIf* contex)
 {
-	foreach(NodeIf* node, nodes)
+	foreach(BehaviorNodeIf* node, nodes)
 	{
 		if (!node->Run(contex))
 		{
@@ -25,7 +30,7 @@ bool SequenceNode::Run(ContexIf* contex)
 	return true;
 }
 
-void SequenceNode::AddChildNode(NodeIf *node)
+void SequenceNode::AddChildNode(BehaviorNodeIf *node)
 {
 	nodes.push_back(node);
 }
@@ -34,16 +39,16 @@ std::string SequenceNode::ToString()
 {
 	std::string s;
 	s += "SequenceNode: \n";
-	foreach(NodeIf* node, nodes)
+	foreach(BehaviorNodeIf* node, nodes)
 	{
 		s += "    " + node->ToString() + "\n";
 	}
 	return s;
 }
 
-NodeIf* SequenceNodeFactory::GetInstance(const BehaviorNodeConf& conf)
+BehaviorNodeIf* SequenceNodeFactory::GetInstance(const BehaviorNodeConf& conf)
 {
-	return new SequenceNode();
+	return new SequenceNode(conf.type());
 }
 
 } // namespace Egametang

+ 8 - 6
Cpp/Game/BehaviorTree/SequenceNode.h

@@ -2,29 +2,31 @@
 #define BEHAVIORTREE_SEQUENCENODE_H
 
 #include <list>
-#include "BehaviorTree/NodeIf.h"
+#include "BehaviorTree/BehaviorNodeIf.h"
 
 namespace Egametang {
 
-class SequenceNode: public NodeIf
+class SequenceNode: public BehaviorNodeIf
 {
 private:
-	std::list<NodeIf*> nodes;
+	std::list<BehaviorNodeIf*> nodes;
 
 public:
+	SequenceNode(int32 type);
+
 	virtual ~SequenceNode();
 
 	virtual bool Run(ContexIf* contex);
 
-	virtual void AddChildNode(NodeIf *node);
+	virtual void AddChildNode(BehaviorNodeIf *node);
 
 	virtual std::string ToString();
 };
 
-class SequenceNodeFactory: public NodeFactoryIf
+class SequenceNodeFactory: public BehaviorNodeFactoryIf
 {
 public:
-	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
+	virtual BehaviorNodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang

+ 1 - 1
Cpp/Game/BehaviorTree/Vampire.txt

@@ -23,4 +23,4 @@ node:
 			args: -100
 		}
 	}
-}
+}

BIN
Doc/Dota英雄AI.vsd