Browse Source

去掉AndNode和OrNode,使用行为树实现事件

tanghai 14 years ago
parent
commit
54a5d08a1d
36 changed files with 277 additions and 483 deletions
  1. 56 0
      Cpp/Game/BehaviorTree/BehaviorTree.cc
  2. 34 0
      Cpp/Game/BehaviorTree/BehaviorTree.h
  3. 18 0
      Cpp/Game/BehaviorTree/BehaviorTreeConf.proto
  4. 5 5
      Cpp/Game/BehaviorTree/BuffType.cc
  5. 5 5
      Cpp/Game/BehaviorTree/BuffType.h
  6. 6 8
      Cpp/Game/BehaviorTree/CMakeLists.txt
  7. 5 5
      Cpp/Game/BehaviorTree/ChangeHealth.cc
  8. 5 5
      Cpp/Game/BehaviorTree/ChangeHealth.h
  9. 1 1
      Cpp/Game/BehaviorTree/CombatContex.cc
  10. 4 4
      Cpp/Game/BehaviorTree/CombatContex.h
  11. 4 4
      Cpp/Game/BehaviorTree/ContexIf.h
  12. 6 8
      Cpp/Game/BehaviorTree/EventDefine.h
  13. 9 9
      Cpp/Game/BehaviorTree/GameEvents.cc
  14. 6 6
      Cpp/Game/BehaviorTree/GameEvents.h
  15. 9 9
      Cpp/Game/BehaviorTree/GameEventsTest.cc
  16. 46 0
      Cpp/Game/BehaviorTree/NodeFactories.cc
  17. 24 0
      Cpp/Game/BehaviorTree/NodeFactories.h
  18. 5 5
      Cpp/Game/BehaviorTree/NodeIf.h
  19. 2 2
      Cpp/Game/BehaviorTree/NotNode.cc
  20. 5 5
      Cpp/Game/BehaviorTree/NotNode.h
  21. 2 2
      Cpp/Game/BehaviorTree/SelectorNode.cc
  22. 5 5
      Cpp/Game/BehaviorTree/SelectorNode.h
  23. 2 2
      Cpp/Game/BehaviorTree/SequenceNode.cc
  24. 5 5
      Cpp/Game/BehaviorTree/SequenceNode.h
  25. 3 3
      Cpp/Game/BehaviorTree/SpellBuff.h
  26. 4 6
      Cpp/Game/BehaviorTree/Vampire.txt
  27. 1 1
      Cpp/Game/CMakeLists.txt
  28. 0 49
      Cpp/Game/Event/AndNode.cc
  29. 0 33
      Cpp/Game/Event/AndNode.h
  30. 0 69
      Cpp/Game/Event/Event.cc
  31. 0 37
      Cpp/Game/Event/Event.h
  32. 0 30
      Cpp/Game/Event/EventConf.proto
  33. 0 54
      Cpp/Game/Event/NodeFactories.cc
  34. 0 24
      Cpp/Game/Event/NodeFactories.h
  35. 0 49
      Cpp/Game/Event/OrNode.cc
  36. 0 33
      Cpp/Game/Event/OrNode.h

+ 56 - 0
Cpp/Game/BehaviorTree/BehaviorTree.cc

@@ -0,0 +1,56 @@
+#include <boost/format.hpp>
+#include <glog/logging.h>
+#include "Base/Typedef.h"
+#include "BehaviorTree/BehaviorTree.h"
+#include "BehaviorTree/NodeFactories.h"
+#include "BehaviorTree/BehaviorTreeConf.pb.h"
+
+namespace Egametang {
+
+BehaviorTree::BehaviorTree(NodeFactories& factories, const BehaviorTreeConf& tree_conf):
+		node(NULL)
+{
+	type = tree_conf.type();
+
+	const BehaviorNodeConf& node_conf = tree_conf.node();
+	if (tree_conf.has_node())
+	{
+		const BehaviorNodeConf& node_conf = tree_conf.node();
+		BuildTree(factories, node_conf, node);
+	}
+}
+
+void BehaviorTree::BuildTree(
+		NodeFactories& factories, const BehaviorNodeConf& node_conf,
+		NodeIf*& 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;
+		BuildTree(factories, logic_node_conf, logic_node);
+		node->AddChildNode(logic_node);
+	}
+}
+
+BehaviorTree::~BehaviorTree()
+{
+	delete node;
+}
+
+void BehaviorTree::Run(ContexIf* contex)
+{
+	node->Run(contex);
+}
+
+std::string BehaviorTree::ToString()
+{
+	boost::format format("type: %1%\node: %2%");
+	format % type % node->ToString();
+	return format.str();
+}
+
+} // namespace Egametang
+

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

@@ -0,0 +1,34 @@
+#ifndef BEHAVIORTREE_BEHAVIORTREE_H
+#define BEHAVIORTREE_BEHAVIORTREE_H
+
+#include "BehaviorTree/NodeIf.h"
+
+namespace Egametang {
+
+class BehaviorTreeConf;
+class BehaviorNodeConf;
+class NodeFactories;
+
+class BehaviorTree
+{
+private:
+	int type;
+	NodeIf* node;
+
+	void BuildTree(NodeFactories& factories, const BehaviorNodeConf& node_conf,
+			NodeIf*& node);
+
+public:
+	BehaviorTree(NodeFactories& factories, const BehaviorTreeConf& tree_conf);
+
+	~BehaviorTree();
+
+	void Run(ContexIf* contex);
+
+	std::string ToString();
+};
+
+} // namespace Egametang
+
+
+#endif // BEHAVIORTREE_BEHAVIORTREE_H

+ 18 - 0
Cpp/Game/BehaviorTree/BehaviorTreeConf.proto

@@ -0,0 +1,18 @@
+package Egametang;
+
+message BehaviorNodeConf
+{
+	required int32 type = 1;
+	// 条件需要的参数
+	repeated int32 args = 2;
+	// 包含多个子节点
+	repeated BehaviorNodeConf node = 3;
+};
+
+message BehaviorTreeConf
+{
+	// 行为树类型
+	required int32 type = 1;
+	// 行为树节点
+	required BehaviorNodeConf node = 2;
+};

+ 5 - 5
Cpp/Game/Event/BuffType.cc → Cpp/Game/BehaviorTree/BuffType.cc

@@ -1,7 +1,7 @@
-#include "Event/BuffType.h"
-#include "Event/ContexIf.h"
-#include "Event/SpellBuff.h"
-#include "Event/EventConf.pb.h"
+#include "BehaviorTree/BuffType.h"
+#include "BehaviorTree/ContexIf.h"
+#include "BehaviorTree/SpellBuff.h"
+#include "BehaviorTree/BehaviorTreeConf.pb.h"
 
 namespace Egametang {
 
@@ -30,7 +30,7 @@ BuffTypeFactory::~BuffTypeFactory()
 {
 }
 
-NodeIf* BuffTypeFactory::GetInstance(const EventNode& conf)
+NodeIf* BuffTypeFactory::GetInstance(const BehaviorNodeConf& conf)
 {
 	return new BuffType(conf.args(0));
 }

+ 5 - 5
Cpp/Game/Event/BuffType.h → Cpp/Game/BehaviorTree/BuffType.h

@@ -1,7 +1,7 @@
-#ifndef EVENT_BUFFTYPE_H
-#define EVENT_BUFFTYPE_H
+#ifndef BEHAVIORTREE_BUFFTYPE_H
+#define BEHAVIORTREE_BUFFTYPE_H
 
-#include "Event/NodeIf.h"
+#include "BehaviorTree/NodeIf.h"
 
 namespace Egametang {
 
@@ -30,10 +30,10 @@ class BuffTypeFactory: public NodeFactoryIf
 public:
 	virtual ~BuffTypeFactory();
 
-	virtual NodeIf* GetInstance(const EventNode& conf);
+	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang
 
 
-#endif // EVENT_BUFFTYPE_H
+#endif // BEHAVIORTREE_BUFFTYPE_H

+ 6 - 8
Cpp/Game/Event/CMakeLists.txt → Cpp/Game/BehaviorTree/CMakeLists.txt

@@ -1,33 +1,31 @@
 PROTOBUF_GENERATE_CPP(proto_srcs proto_hdrs
-	EventConf.proto
+	BehaviorTreeConf.proto
 )
 
-SET(EventSrc
-	AndNode.cc
+SET(BehaviorTreeSrc
 	BuffType.cc
 	ChangeHealth.cc
 	CombatContex.cc
-	Event.cc
 	GameEvents.cc
 	NodeFactories.cc
 	NotNode.cc
-	OrNode.cc
 	SelectorNode.cc
 	SequenceNode.cc
+	BehaviorTree.cc
 	${proto_srcs}
 )
 
-ADD_LIBRARY(Event ${EventSrc})
+ADD_LIBRARY(BehaviorTree ${BehaviorTreeSrc})
 
 ADD_EXECUTABLE(GameEventsTest GameEventsTest.cc)
 
-SET(Excutes 
+SET(Excutes
 	GameEventsTest
 )
 
 FOREACH(Excute ${Excutes})
 	TARGET_LINK_LIBRARIES(${Excute}
-		Event
+		BehaviorTree
 		${ThirdPartyLibs}
 	)
 	ADD_TEST(${Excute} ${Excute})

+ 5 - 5
Cpp/Game/Event/ChangeHealth.cc → Cpp/Game/BehaviorTree/ChangeHealth.cc

@@ -1,8 +1,8 @@
 #include <glog/logging.h>
-#include "Event/ChangeHealth.h"
-#include "Event/CombatContex.h"
-#include "Event/EventConf.pb.h"
-#include "Event/SpellBuff.h"
+#include "BehaviorTree/ChangeHealth.h"
+#include "BehaviorTree/CombatContex.h"
+#include "BehaviorTree/BehaviorTreeConf.pb.h"
+#include "BehaviorTree/SpellBuff.h"
 
 namespace Egametang {
 
@@ -41,7 +41,7 @@ std::string ChangeHealth::ToString()
 	return s;
 }
 
-NodeIf* ChangeHealthFactory::GetInstance(const EventNode& conf)
+NodeIf* ChangeHealthFactory::GetInstance(const BehaviorNodeConf& conf)
 {
 	return new ChangeHealth(conf.args(0), conf.args(1));
 }

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

@@ -1,8 +1,8 @@
-#ifndef EVENT_CHANGEHEALTH_H
-#define EVENT_CHANGEHEALTH_H
+#ifndef BEHAVIORTREE_CHANGEHEALTH_H
+#define BEHAVIORTREE_CHANGEHEALTH_H
 
 #include "Base/Typedef.h"
-#include "Event/NodeIf.h"
+#include "BehaviorTree/NodeIf.h"
 
 namespace Egametang {
 
@@ -25,9 +25,9 @@ public:
 class ChangeHealthFactory: public NodeFactoryIf
 {
 public:
-	virtual NodeIf* GetInstance(const EventNode& conf);
+	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang
 
-#endif // EVENT_CHANGEHEALTH_H
+#endif // BEHAVIORTREE_CHANGEHEALTH_H

+ 1 - 1
Cpp/Game/Event/CombatContex.cc → Cpp/Game/BehaviorTree/CombatContex.cc

@@ -1,4 +1,4 @@
-#include "Event/CombatContex.h"
+#include "BehaviorTree/CombatContex.h"
 
 namespace Egametang {
 

+ 4 - 4
Cpp/Game/Event/CombatContex.h → Cpp/Game/BehaviorTree/CombatContex.h

@@ -1,7 +1,7 @@
-#ifndef EVENT_COMBATCONTEX_H
-#define EVENT_COMBATCONTEX_H
+#ifndef BEHAVIORTREE_COMBATCONTEX_H
+#define BEHAVIORTREE_COMBATCONTEX_H
 
-#include "Event/ContexIf.h"
+#include "BehaviorTree/ContexIf.h"
 
 namespace Egametang {
 
@@ -22,5 +22,5 @@ public:
 
 } // namespace Egametang
 
-#endif // EVENT_COMBATCONTEX_H
+#endif // BEHAVIORTREE_COMBATCONTEX_H
 

+ 4 - 4
Cpp/Game/Event/ContexIf.h → Cpp/Game/BehaviorTree/ContexIf.h

@@ -1,8 +1,8 @@
-#ifndef EVENT_CONTEXIF_H
-#define EVENT_CONTEXIF_H
+#ifndef BEHAVIORTREE_CONTEXIF_H
+#define BEHAVIORTREE_CONTEXIF_H
 
 #include <stddef.h>
-#include "Event/SpellBuff.h"
+#include "BehaviorTree/SpellBuff.h"
 
 namespace Egametang {
 
@@ -26,5 +26,5 @@ public:
 
 } // namespace Egametang
 
-#endif // EVENT_CONTEXIF_H
+#endif // BEHAVIORTREE_CONTEXIF_H
 

+ 6 - 8
Cpp/Game/Event/EventDefine.h → Cpp/Game/BehaviorTree/EventDefine.h

@@ -1,5 +1,5 @@
-#ifndef EVENT_EVENTDEFINE_H
-#define EVENT_EVENTDEFINE_H
+#ifndef BEHAVIORTREE_EVENTDEFINE_H
+#define BEHAVIORTREE_EVENTDEFINE_H
 
 enum EventType
 {
@@ -13,12 +13,10 @@ enum EventType
 
 enum NodeType
 {
-	AND                = 0,
-	OR                 = 1,
-	NOT                = 2,
+	SEQUENCE           = 1,
+	SELECTOR           = 2,
 
-	SEQUENCE           = 6,
-	SELECTOR           = 7,
+	NOT                = 11,
 
 	// 条件子节点100 - 1001
 	BUFF_TYPE          = 101,
@@ -33,4 +31,4 @@ enum SpellUnit
 	VICTIM = 1,
 };
 
-#endif // EVENT_EVENTDEFINE_H
+#endif // BEHAVIORTREE_EVENTDEFINE_H

+ 9 - 9
Cpp/Game/Event/GameEvents.cc → Cpp/Game/BehaviorTree/GameEvents.cc

@@ -2,8 +2,8 @@
 #include <glog/logging.h>
 #include "Base/Marcos.h"
 #include "Base/Typedef.h"
-#include "Event/GameEvents.h"
-#include "Event/EventConf.pb.h"
+#include "BehaviorTree/GameEvents.h"
+#include "BehaviorTree/BehaviorTreeConf.pb.h"
 
 namespace Egametang {
 
@@ -14,27 +14,27 @@ GameEvents::GameEvents(NodeFactories& factories):
 
 GameEvents::~GameEvents()
 {
-	foreach(std::list<Event*> list, events)
+	foreach(std::list<BehaviorTree*> list, events)
 	{
-		foreach(Event* event, list)
+		foreach(BehaviorTree* tree, list)
 		{
-			delete event;
+			delete tree;
 		}
 	}
 }
 
-void GameEvents::AddEvent(EventConf& conf)
+void GameEvents::AddEvent(const BehaviorTreeConf& conf)
 {
 	int32 type = conf.type();
-	Event* event = new Event(factories, conf);
+	BehaviorTree* event = new BehaviorTree(factories, conf);
 	events[type].push_back(event);
 }
 
 void GameEvents::Excute(int type, ContexIf* contex)
 {
-	std::list<Event*>& es = events[type];
+	std::list<BehaviorTree*>& es = events[type];
 
-	for (std::list<Event*>::iterator iter = es.begin(); iter != es.end(); ++iter)
+	for (std::list<BehaviorTree*>::iterator iter = es.begin(); iter != es.end(); ++iter)
 	{
 		CHECK(*iter);
 		(*iter)->Run(contex);

+ 6 - 6
Cpp/Game/Event/GameEvents.h → Cpp/Game/BehaviorTree/GameEvents.h

@@ -1,9 +1,9 @@
-#ifndef EVENT_GAMEEVENTS_H
-#define EVENT_GAMEEVENTS_H
+#ifndef BEHAVIORTREE_GAMEEVENTS_H
+#define BEHAVIORTREE_GAMEEVENTS_H
 
 #include <list>
 #include <vector>
-#include "Event/Event.h"
+#include "BehaviorTree/BehaviorTree.h"
 
 namespace Egametang {
 
@@ -13,18 +13,18 @@ class GameEvents
 {
 private:
 	NodeFactories& factories;
-	std::vector<std::list<Event*> > events;
+	std::vector<std::list<BehaviorTree*> > events;
 
 public:
 	GameEvents(NodeFactories& factories);
 
 	~GameEvents();
 
-	void AddEvent(EventConf& conf);
+	void AddEvent(const BehaviorTreeConf& conf);
 
 	void Excute(int type, ContexIf* contex);
 };
 
 } // namespace Egametang
 
-#endif // EVENT_GAMEEVENTS_H
+#endif // BEHAVIORTREE_GAMEEVENTS_H

+ 9 - 9
Cpp/Game/Event/GameEventsTest.cc → Cpp/Game/BehaviorTree/GameEventsTest.cc

@@ -4,12 +4,12 @@
 #include <gflags/gflags.h>
 #include <glog/logging.h>
 #include <google/protobuf/text_format.h>
-#include "Event/GameEvents.h"
-#include "Event/NodeFactories.h"
-#include "Event/EventConf.pb.h"
-#include "Event/SpellBuff.h"
-#include "Event/CombatContex.h"
-#include "Event/EventDefine.h"
+#include "BehaviorTree/GameEvents.h"
+#include "BehaviorTree/NodeFactories.h"
+#include "BehaviorTree/BehaviorTreeConf.pb.h"
+#include "BehaviorTree/SpellBuff.h"
+#include "BehaviorTree/CombatContex.h"
+#include "BehaviorTree/EventDefine.h"
 
 namespace Egametang {
 
@@ -38,12 +38,12 @@ static void FileToString(const std::string& file, std::string& string)
 	in.close();
 }
 
-TEST_F(GameEventsTest, DotChangeHealth)
+TEST_F(GameEventsTest, Vampire)
 {
-	std::string file = "../Cpp/Game/Event/Vampire.txt";
+	std::string file = "../Cpp/Game/BehaviorTree/Vampire.txt";
 	std::string string;
 	FileToString(file, string);
-	EventConf conf;
+	BehaviorTreeConf conf;
 	google::protobuf::TextFormat::ParseFromString(string, &conf);
 	game_events.AddEvent(conf);
 

+ 46 - 0
Cpp/Game/BehaviorTree/NodeFactories.cc

@@ -0,0 +1,46 @@
+#include <glog/logging.h>
+#include "Base/Typedef.h"
+#include "BehaviorTree/NotNode.h"
+#include "BehaviorTree/SequenceNode.h"
+#include "BehaviorTree/SelectorNode.h"
+#include "BehaviorTree/BuffType.h"
+#include "BehaviorTree/ChangeHealth.h"
+#include "BehaviorTree/NodeFactories.h"
+#include "BehaviorTree/BehaviorTreeConf.pb.h"
+#include "BehaviorTree/EventDefine.h"
+
+namespace Egametang {
+
+NodeFactories::NodeFactories(): factories(2000, (NodeFactoryIf*)(NULL))
+{
+	// 节点
+	factories[SEQUENCE] = new SequenceNodeFactory();
+	factories[SELECTOR] = new SelectorNodeFactory();
+	factories[NOT] = new NotNodeFactory();
+
+	// 叶子节点
+	factories[BUFF_TYPE] = new BuffTypeFactory();
+	factories[CHANGE_HEALTH] = new ChangeHealthFactory();
+}
+
+NodeFactories::~NodeFactories()
+{
+	for (std::size_t i = 0; i < factories.size(); ++i)
+	{
+		if (factories[i] == NULL)
+		{
+			continue;
+		}
+		delete factories[i];
+	}
+}
+
+NodeIf* NodeFactories::GetInstance(const BehaviorNodeConf& conf)
+{
+	int32 type = conf.type();
+	return factories[type]->GetInstance(conf);
+}
+
+}
+
+

+ 24 - 0
Cpp/Game/BehaviorTree/NodeFactories.h

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

+ 5 - 5
Cpp/Game/Event/NodeIf.h → Cpp/Game/BehaviorTree/NodeIf.h

@@ -1,12 +1,12 @@
-#ifndef EVENT_NODEIF_H
-#define EVENT_NODEIF_H
+#ifndef BEHAVIORTREE_NODEIF_H
+#define BEHAVIORTREE_NODEIF_H
 
 #include <string>
 
 namespace Egametang {
 
 class ContexIf;
-class EventNode;
+class BehaviorNodeConf;
 
 class NodeIf
 {
@@ -26,9 +26,9 @@ public:
 class NodeFactoryIf
 {
 public:
-	virtual NodeIf* GetInstance(const EventNode& conf) = 0;
+	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf) = 0;
 };
 
 } // namespace Egametang
 
-#endif // EVENT_NODEIF_H
+#endif // BEHAVIORTREE_NODEIF_H

+ 2 - 2
Cpp/Game/Event/NotNode.cc → Cpp/Game/BehaviorTree/NotNode.cc

@@ -1,5 +1,5 @@
 #include <stddef.h>
-#include "Event/NotNode.h"
+#include "BehaviorTree/NotNode.h"
 
 namespace Egametang {
 
@@ -30,7 +30,7 @@ std::string NotNode::ToString()
 	return s;
 }
 
-NodeIf* NotNodeFactory::GetInstance(const EventNode& conf)
+NodeIf* NotNodeFactory::GetInstance(const BehaviorNodeConf& conf)
 {
 	return new NotNode();
 }

+ 5 - 5
Cpp/Game/Event/NotNode.h → Cpp/Game/BehaviorTree/NotNode.h

@@ -1,7 +1,7 @@
-#ifndef EVENT_NOTNODE_H
-#define EVENT_NOTNODE_H
+#ifndef BEHAVIORTREE_NOTNODE_H
+#define BEHAVIORTREE_NOTNODE_H
 
-#include "Event/NodeIf.h"
+#include "BehaviorTree/NodeIf.h"
 
 namespace Egametang {
 
@@ -25,10 +25,10 @@ public:
 class NotNodeFactory: public NodeFactoryIf
 {
 public:
-	virtual NodeIf* GetInstance(const EventNode& conf);
+	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang
 
 
-#endif // EVENT_NOTNODE_H
+#endif // BEHAVIORTREE_NOTNODE_H

+ 2 - 2
Cpp/Game/Event/SelectorNode.cc → Cpp/Game/BehaviorTree/SelectorNode.cc

@@ -1,6 +1,6 @@
 #include <boost/foreach.hpp>
 #include "Base/Marcos.h"
-#include "Event/SelectorNode.h"
+#include "BehaviorTree/SelectorNode.h"
 
 namespace Egametang {
 
@@ -40,7 +40,7 @@ std::string SelectorNode::ToString()
 	return s;
 }
 
-NodeIf* SelectorNodeFactory::GetInstance(const EventNode& conf)
+NodeIf* SelectorNodeFactory::GetInstance(const BehaviorNodeConf& conf)
 {
 	return new SelectorNode();
 }

+ 5 - 5
Cpp/Game/Event/SelectorNode.h → Cpp/Game/BehaviorTree/SelectorNode.h

@@ -1,8 +1,8 @@
-#ifndef EVENT_SELECTORNODE_H
-#define EVENT_SELECTORNODE_H
+#ifndef BEHAVIORTREE_SELECTORNODE_H
+#define BEHAVIORTREE_SELECTORNODE_H
 
 #include <list>
-#include "Event/NodeIf.h"
+#include "BehaviorTree/NodeIf.h"
 
 namespace Egametang {
 
@@ -24,10 +24,10 @@ public:
 class SelectorNodeFactory: public NodeFactoryIf
 {
 public:
-	virtual NodeIf* GetInstance(const EventNode& conf);
+	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang
 
 
-#endif // EVENT_SELECTORNODE_H
+#endif // BEHAVIORTREE_SELECTORNODE_H

+ 2 - 2
Cpp/Game/Event/SequenceNode.cc → Cpp/Game/BehaviorTree/SequenceNode.cc

@@ -1,7 +1,7 @@
 #include <glog/logging.h>
 #include <boost/foreach.hpp>
 #include "Base/Marcos.h"
-#include "Event/SequenceNode.h"
+#include "BehaviorTree/SequenceNode.h"
 
 namespace Egametang {
 
@@ -41,7 +41,7 @@ std::string SequenceNode::ToString()
 	return s;
 }
 
-NodeIf* SequenceNodeFactory::GetInstance(const EventNode& conf)
+NodeIf* SequenceNodeFactory::GetInstance(const BehaviorNodeConf& conf)
 {
 	return new SequenceNode();
 }

+ 5 - 5
Cpp/Game/Event/SequenceNode.h → Cpp/Game/BehaviorTree/SequenceNode.h

@@ -1,8 +1,8 @@
-#ifndef EVENT_SEQUENCENODE_H
-#define EVENT_SEQUENCENODE_H
+#ifndef BEHAVIORTREE_SEQUENCENODE_H
+#define BEHAVIORTREE_SEQUENCENODE_H
 
 #include <list>
-#include "Event/NodeIf.h"
+#include "BehaviorTree/NodeIf.h"
 
 namespace Egametang {
 
@@ -24,10 +24,10 @@ public:
 class SequenceNodeFactory: public NodeFactoryIf
 {
 public:
-	virtual NodeIf* GetInstance(const EventNode& conf);
+	virtual NodeIf* GetInstance(const BehaviorNodeConf& conf);
 };
 
 } // namespace Egametang
 
 
-#endif // EVENT_SEQUENCENODE_H
+#endif // BEHAVIORTREE_SEQUENCENODE_H

+ 3 - 3
Cpp/Game/Event/SpellBuff.h → Cpp/Game/BehaviorTree/SpellBuff.h

@@ -1,5 +1,5 @@
-#ifndef EVENT_SPELLBUFF_H
-#define EVENT_SPELLBUFF_H
+#ifndef BEHAVIORTREE_SPELLBUFF_H
+#define BEHAVIORTREE_SPELLBUFF_H
 
 namespace Egametang {
 
@@ -30,4 +30,4 @@ public:
 } // namespace Egametang
 
 
-#endif // EVENT_SPELLBUFF_H
+#endif // BEHAVIORTREE_SPELLBUFF_H

+ 4 - 6
Cpp/Game/Event/Vampire.txt → Cpp/Game/BehaviorTree/Vampire.txt

@@ -1,18 +1,16 @@
 type: 5
-condition:
+node:
 {
+	type: 1
 	node:
 	{
 		type: 101
 		args: 2
 	}
-}
-action:
-{
 	node:
 	{
-		type: 6
-		node: 
+		type: 1
+		node:
 		{
 			type: 1001
 			args: 0

+ 1 - 1
Cpp/Game/CMakeLists.txt

@@ -1 +1 @@
-ADD_SUBDIRECTORY(Event)
+ADD_SUBDIRECTORY(BehaviorTree)

+ 0 - 49
Cpp/Game/Event/AndNode.cc

@@ -1,49 +0,0 @@
-#include <boost/foreach.hpp>
-#include "Base/Marcos.h"
-#include "Event/AndNode.h"
-
-namespace Egametang {
-
-AndNode::~AndNode()
-{
-	foreach(NodeIf* node, nodes)
-	{
-		delete node;
-	}
-}
-
-bool AndNode::Run(ContexIf* contex)
-{
-	foreach(NodeIf* node, nodes)
-	{
-		if (!node->Run(contex))
-		{
-			return false;
-		}
-	}
-	return true;
-}
-
-void AndNode::AddChildNode(NodeIf *node)
-{
-	nodes.push_back(node);
-}
-
-std::string AndNode::ToString()
-{
-	std::string s;
-	s += "AndNode: \n";
-	foreach(NodeIf* node, nodes)
-	{
-		s += "    " + node->ToString() + "\n";
-	}
-	return s;
-}
-
-NodeIf* AndNodeFactory::GetInstance(const EventNode& conf)
-{
-	return new AndNode();
-}
-
-} // namespace Egametang
-

+ 0 - 33
Cpp/Game/Event/AndNode.h

@@ -1,33 +0,0 @@
-#ifndef EVENT_ANDNODE_H
-#define EVENT_ANDNODE_H
-
-#include <list>
-#include "Event/NodeIf.h"
-
-namespace Egametang {
-
-class AndNode: public NodeIf
-{
-private:
-	std::list<NodeIf*> nodes;
-
-public:
-	virtual ~AndNode();
-
-	virtual bool Run(ContexIf* contex);
-
-	virtual void AddChildNode(NodeIf *node);
-
-	virtual std::string ToString();
-};
-
-class AndNodeFactory: public NodeFactoryIf
-{
-public:
-	virtual NodeIf* GetInstance(const EventNode& conf);
-};
-
-} // namespace Egametang
-
-
-#endif // EVENT_ANDNODE_H

+ 0 - 69
Cpp/Game/Event/Event.cc

@@ -1,69 +0,0 @@
-#include <boost/format.hpp>
-#include <glog/logging.h>
-#include "Base/Typedef.h"
-#include "Event/Event.h"
-#include "Event/NodeFactories.h"
-#include "Event/EventConf.pb.h"
-
-namespace Egametang {
-
-Event::Event(NodeFactories& factories, EventConf& conf):
-		action(NULL), condition(NULL)
-{
-	type = conf.type();
-
-	const ConditionConf& condition_conf = conf.condition();
-	if (condition_conf.has_node())
-	{
-		const EventNode& node_conf = condition_conf.node();
-		BuildTree(factories, node_conf, condition);
-	}
-
-	const ActionConf& action_conf = conf.action();
-	if (action_conf.has_node())
-	{
-		const EventNode& node_conf = action_conf.node();
-		BuildTree(factories, node_conf, action);
-	}
-}
-
-void Event::BuildTree(
-		NodeFactories& factories, const EventNode& conf,
-		NodeIf*& node)
-{
-	int32 type = conf.type();
-	node = factories.GetInstance(conf);
-	for (int i = 0; i < conf.node_size(); ++i)
-	{
-		const EventNode& logic_node_conf = conf.node(i);
-		NodeIf* logic_node = NULL;
-		BuildTree(factories, logic_node_conf, logic_node);
-		node->AddChildNode(logic_node);
-	}
-}
-
-Event::~Event()
-{
-	delete condition;
-	delete action;
-}
-
-void Event::Run(ContexIf* contex)
-{
-	if(condition->Run(contex))
-	{
-		// 执行动作
-		CHECK(action);
-		action->Run(contex);
-	}
-}
-
-std::string Event::ToString()
-{
-	boost::format format("type: %1%\ncondition: %2%\naction: %3%");
-	format % type % condition->ToString() % action->ToString();
-	return format.str();
-}
-
-} // namespace Egametang
-

+ 0 - 37
Cpp/Game/Event/Event.h

@@ -1,37 +0,0 @@
-#ifndef EVENT_EVENT_H
-#define EVENT_EVENT_H
-
-#include "Event/NodeIf.h"
-
-namespace Egametang {
-
-class EventNode;
-class NodeFactories;
-class ConditionConf;
-class EventConf;
-
-class Event
-{
-private:
-	int type;
-	NodeIf* condition;
-	NodeIf* action;
-
-	void BuildTree(
-			NodeFactories& factories, const EventNode& conf,
-			NodeIf*& condition);
-
-public:
-	Event(NodeFactories& factories, EventConf& conf);
-
-	~Event();
-
-	void Run(ContexIf* contex);
-
-	std::string ToString();
-};
-
-} // namespace Egametang
-
-
-#endif // EVENT_EVENT_H

+ 0 - 30
Cpp/Game/Event/EventConf.proto

@@ -1,30 +0,0 @@
-package Egametang;
-
-message EventNode
-{
-	required int32 type = 2;
-	// 条件需要的参数
-	repeated int32 args = 3;
-	// 包含多个子节点
-	repeated EventNode node = 4;
-};
-
-message ConditionConf
-{
-	optional EventNode node = 1;
-};
-
-message ActionConf
-{
-	optional EventNode node = 1;
-};
-
-message EventConf
-{
-	// 事件类型
-	required int32 type = 1;
-	// 条件
-	required ConditionConf condition = 2;
-	// 动作
-	required ActionConf action = 3;
-};

+ 0 - 54
Cpp/Game/Event/NodeFactories.cc

@@ -1,54 +0,0 @@
-#include <glog/logging.h>
-#include "Base/Typedef.h"
-#include "Event/AndNode.h"
-#include "Event/OrNode.h"
-#include "Event/NotNode.h"
-#include "Event/SequenceNode.h"
-#include "Event/SelectorNode.h"
-#include "Event/BuffType.h"
-#include "Event/ChangeHealth.h"
-#include "Event/NodeFactories.h"
-#include "Event/EventConf.pb.h"
-#include "Event/EventDefine.h"
-
-namespace Egametang {
-
-NodeFactories::NodeFactories(): factories(2000, NULL)
-{
-	// 条件节点
-	factories[AND] = new AndNodeFactory();
-	factories[OR] = new OrNodeFactory();
-	factories[NOT] = new NotNodeFactory();
-
-	// 动作节点
-	factories[SEQUENCE] = new SequenceNodeFactory();
-	factories[SELECTOR] = new SelectorNodeFactory();
-
-	// 条件叶子节点
-	factories[BUFF_TYPE] = new BuffTypeFactory();
-
-	// 动作叶子节点
-	factories[CHANGE_HEALTH] = new ChangeHealthFactory();
-}
-
-NodeFactories::~NodeFactories()
-{
-	for (std::size_t i = 0; i < factories.size(); ++i)
-	{
-		if (factories[i] == NULL)
-		{
-			continue;
-		}
-		delete factories[i];
-	}
-}
-
-NodeIf* NodeFactories::GetInstance(const EventNode& conf)
-{
-	int32 type = conf.type();
-	return factories[type]->GetInstance(conf);
-}
-
-}
-
-

+ 0 - 24
Cpp/Game/Event/NodeFactories.h

@@ -1,24 +0,0 @@
-#ifndef EVENT_NODEFACTORIES_H
-#define EVENT_NODEFACTORIES_H
-
-#include <vector>
-#include "Event/NodeIf.h"
-
-namespace Egametang {
-
-class NodeFactories
-{
-private:
-	std::vector<NodeFactoryIf*> factories;
-
-public:
-	NodeFactories();
-
-	virtual ~NodeFactories();
-
-	virtual NodeIf* GetInstance(const EventNode& conf);
-};
-
-} // namespace Egametang
-
-#endif // EVENT_NODEFACTORIES_H

+ 0 - 49
Cpp/Game/Event/OrNode.cc

@@ -1,49 +0,0 @@
-#include <boost/foreach.hpp>
-#include "Base/Marcos.h"
-#include "Event/OrNode.h"
-
-namespace Egametang {
-
-OrNode::~OrNode()
-{
-	foreach(NodeIf* node, nodes)
-	{
-		delete node;
-	}
-}
-
-bool OrNode::Run(ContexIf* contex)
-{
-	foreach(NodeIf* node, nodes)
-	{
-		if (!node->Run(contex))
-		{
-			return true;
-		}
-	}
-	return false;
-}
-
-void OrNode::AddChildNode(NodeIf *node)
-{
-	nodes.push_back(node);
-}
-
-std::string OrNode::ToString()
-{
-	std::string s;
-	s += "OrNode: \n";
-	foreach(NodeIf* node, nodes)
-	{
-		s += "    " + node->ToString() + "\n";
-	}
-	return s;
-}
-
-NodeIf* OrNodeFactory::GetInstance(const EventNode& conf)
-{
-	return new OrNode();
-}
-
-} // namespace Egametang
-

+ 0 - 33
Cpp/Game/Event/OrNode.h

@@ -1,33 +0,0 @@
-#ifndef EVENT_ORNODE_H
-#define EVENT_ORNODE_H
-
-#include <list>
-#include "Event/NodeIf.h"
-
-namespace Egametang {
-
-class OrNode: public NodeIf
-{
-private:
-	std::list<NodeIf*> nodes;
-
-public:
-	virtual ~OrNode();
-
-	virtual bool Run(ContexIf* contex);
-
-	virtual void AddChildNode(NodeIf *node);
-
-	virtual std::string ToString();
-};
-
-class OrNodeFactory: public NodeFactoryIf
-{
-public:
-	virtual NodeIf* GetInstance(const EventNode& conf);
-};
-
-} // namespace Egametang
-
-
-#endif // EVENT_ORNODE_H