tanghai 14 жил өмнө
parent
commit
d9f0c6f9fa

+ 2 - 2
Cpp/CMakeLists.txt

@@ -36,8 +36,8 @@ INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
 FIND_PACKAGE(PythonLibs REQUIRED)
 INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIR})
 
-FIND_PACKAGE(Qt4 REQUIRED)
-INCLUDE_DIRECTORIES(${QT_INCLUDE_DIR})
+#FIND_PACKAGE(Qt4 REQUIRED)
+#INCLUDE_DIRECTORIES(${QT_INCLUDE_DIR})
 
 FIND_PACKAGE(Perftools REQUIRED)
 INCLUDE_DIRECTORIES(${PERFTOOLS_INCLUDE_DIR})

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

@@ -29,6 +29,17 @@ 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();

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

@@ -17,6 +17,8 @@ public:
 	virtual bool Run(ContexIf* contex);
 
 	virtual void AddChildNode(NodeIf *node);
+
+	virtual std::string ToString();
 };
 
 class AndNodeFactory: public NodeFactoryIf

+ 7 - 0
Cpp/Game/Event/BuffType.cc

@@ -15,6 +15,13 @@ bool BuffType::Run(ContexIf* contex)
 	return buff->buff_type == buff_type;
 }
 
+std::string BuffType::ToString()
+{
+	std::string s;
+	s += "BuffType: \n";
+	return s;
+}
+
 BuffTypeFactory::~BuffTypeFactory()
 {
 }

+ 2 - 0
Cpp/Game/Event/BuffType.h

@@ -19,6 +19,8 @@ public:
 	BuffType(int buff_type);
 
 	virtual bool Run(ContexIf* contex);
+
+	virtual std::string ToString();
 };
 
 class BuffTypeFactory: public NodeFactoryIf

+ 9 - 1
Cpp/Game/Event/ChangeHealth.cc

@@ -1,3 +1,4 @@
+#include <glog/logging.h>
 #include "Event/ChangeHealth.h"
 #include "Event/CombatContex.h"
 #include "Event/EventConf.pb.h"
@@ -33,7 +34,14 @@ bool ChangeHealth::Run(ContexIf *contex)
 	return true;
 }
 
-NodeIf *ChangeHealthFactory::GetInstance(const EventNode& conf)
+std::string ChangeHealth::ToString()
+{
+	std::string s;
+	s += "ChangeHealth: \n";
+	return s;
+}
+
+NodeIf* ChangeHealthFactory::GetInstance(const EventNode& conf)
 {
 	return new ChangeHealth(conf.args(0), conf.args(1));
 }

+ 2 - 0
Cpp/Game/Event/ChangeHealth.h

@@ -18,6 +18,8 @@ public:
 	virtual ~ChangeHealth();
 
 	virtual bool Run(ContexIf* contex);
+
+	virtual std::string ToString();
 };
 
 class ChangeHealthFactory: public NodeFactoryIf

+ 4 - 0
Cpp/Game/Event/CombatContex.cc

@@ -7,6 +7,10 @@ CombatContex::CombatContex(Spell* spell, Buff* buff):
 {
 }
 
+CombatContex::~CombatContex()
+{
+}
+
 Spell* CombatContex::GetSpell()
 {
 	return spell;

+ 1 - 0
Cpp/Game/Event/CombatContex.h

@@ -13,6 +13,7 @@ private:
 
 public:
 	CombatContex(Spell* spell, Buff* buff);
+	~CombatContex();
 
 	virtual Spell* GetSpell();
 

+ 3 - 1
Cpp/Game/Event/ContexIf.h

@@ -9,7 +9,9 @@ namespace Egametang {
 class ContexIf
 {
 public:
-	virtual ~ContexIf();
+	virtual ~ContexIf()
+	{
+	}
 
 	virtual Spell* GetSpell()
 	{

+ 15 - 11
Cpp/Game/Event/DotFirstDamage.txt

@@ -1,14 +1,18 @@
 type: 5
-condition: {
-EventNode: {
-  type: 1
-  args: 2
-}
-}
-action: {
-EventNode: {
-  type: 1
-  args: 1
-  args: 100
+condition:
+{
+	node:
+	{
+		type: 101
+		args: 2
+	}
 }
+action:
+{
+	node: 
+	{
+		type: 1001
+		args: 1
+		args: 100
+	}
 }

+ 20 - 6
Cpp/Game/Event/Event.cc

@@ -1,3 +1,5 @@
+#include <boost/format.hpp>
+#include <glog/logging.h>
 #include "Base/Typedef.h"
 #include "Event/Event.h"
 #include "Event/NodeFactories.h"
@@ -8,15 +10,24 @@ 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();
-		BuildCondition(factories, node_conf, condition);
+		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::BuildCondition(
+void Event::BuildTree(
 		NodeFactories& factories, const EventNode& conf,
 		NodeIf*& node)
 {
@@ -26,7 +37,7 @@ void Event::BuildCondition(
 	{
 		const EventNode& logic_node_conf = conf.nodes(i);
 		NodeIf* logic_node = NULL;
-		BuildCondition(factories, logic_node_conf, logic_node);
+		BuildTree(factories, logic_node_conf, logic_node);
 		node->AddChildNode(logic_node);
 	}
 }
@@ -41,14 +52,17 @@ void Event::Run(ContexIf* contex)
 {
 	if(condition->Run(contex))
 	{
-		// 执行动作
+		// 执行动作
+		CHECK(action);
 		action->Run(contex);
 	}
 }
 
-int Event::Type() const
+std::string Event::ToString()
 {
-	return type;
+	boost::format format("type: %1%\ncondition: %2%\naction: %3%");
+	format % type % condition->ToString() % action->ToString();
+	return format.str();
 }
 
 } // namespace Egametang

+ 3 - 7
Cpp/Game/Event/Event.h

@@ -17,22 +17,18 @@ private:
 	NodeIf* condition;
 	NodeIf* action;
 
-	void BuildCondition(
+	void BuildTree(
 			NodeFactories& factories, const EventNode& conf,
 			NodeIf*& condition);
 
-	void BuildAction(
-			NodeFactories& factories, const ConditionConf& conf,
-			NodeIf*& action);
-
 public:
 	Event(NodeFactories& factories, EventConf& conf);
 
 	~Event();
 
-	int Type() const;
-
 	void Run(ContexIf* contex);
+
+	std::string ToString();
 };
 
 } // namespace Egametang

+ 10 - 10
Cpp/Game/Event/EventConf.proto

@@ -9,16 +9,6 @@ message EventNode
 	repeated EventNode nodes = 4;
 };
 
-message EventConf
-{
-	// 事件类型
-	required int32 type = 1;
-	// 条件
-	required ConditionConf condition = 2;
-	// 动作
-	required ActionConf action = 3;
-};
-
 message ConditionConf
 {
 	optional EventNode node = 1;
@@ -27,4 +17,14 @@ message ConditionConf
 message ActionConf
 {
 	optional EventNode node = 1;
+};
+
+message EventConf
+{
+	// 事件类型
+	required int32 type = 1;
+	// 条件
+	required ConditionConf condition = 2;
+	// 动作
+	required ActionConf action = 3;
 };

+ 14 - 15
Cpp/Game/Event/GameEvents.cc

@@ -1,3 +1,6 @@
+#include <boost/foreach.hpp>
+#include <glog/logging.h>
+#include "Base/Marcos.h"
 #include "Base/Typedef.h"
 #include "Event/GameEvents.h"
 #include "Event/EventConf.pb.h"
@@ -11,33 +14,29 @@ GameEvents::GameEvents(NodeFactories& factories):
 
 GameEvents::~GameEvents()
 {
+	foreach(std::list<Event*> list, events)
+	{
+		foreach(Event* event, list)
+		{
+			delete event;
+		}
+	}
 }
 
 void GameEvents::AddEvent(EventConf& conf)
 {
 	int32 type = conf.type();
-	Event event(factories, conf);
+	Event* event = new Event(factories, conf);
 	events[type].push_back(event);
 }
 
 void GameEvents::Excute(int type, ContexIf* contex)
 {
-	std::list<Event>& es = events[type];
+	std::list<Event*>& es = events[type];
 
-	for (std::list<Event>::iterator iter = es.begin(); iter != es.end();)
+	for (std::list<Event*>::iterator iter = es.begin(); iter != es.end(); ++iter)
 	{
-		// 暂未考虑event删除情况,每个event都会对应到一个buff
-		// 所以可以在这里遍历的时候查看相应buff是否删除,如果
-		// 删除就删除相应event
-		if (false)
-		{
-			std::list<Event>::iterator current = iter;
-			++iter;
-			es.erase(current);
-			continue;
-		}
-		iter->Run(contex);
-		++iter;
+		(*iter)->Run(contex);
 	}
 }
 

+ 1 - 1
Cpp/Game/Event/GameEvents.h

@@ -13,7 +13,7 @@ class GameEvents
 {
 private:
 	NodeFactories& factories;
-	std::vector<std::list<Event> > events;
+	std::vector<std::list<Event*> > events;
 
 public:
 	GameEvents(NodeFactories& factories);

+ 35 - 7
Cpp/Game/Event/GameEventsTest.cc

@@ -1,11 +1,14 @@
 #include <fcntl.h>
+#include <fstream>
 #include <gtest/gtest.h>
 #include <gflags/gflags.h>
 #include <glog/logging.h>
-#include <iosfwd>
+#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"
 
 namespace Egametang {
 
@@ -25,15 +28,40 @@ public:
 	}
 };
 
+static void FileToString(const std::string& file, std::string& string)
+{
+	std::ifstream in(file.c_str());
+	std::ostringstream os;
+	os << in.rdbuf();
+	string = os.str();
+	in.close();
+}
+
 TEST_F(GameEventsTest, DotChangeHealth)
 {
-	std::string conf_file = "../../../Cpp/Game/Event/DotFirstDamage.txt";
+	std::string file = "../Cpp/Game/Event/DotFirstDamage.txt";
+	std::string string;
+	FileToString(file, string);
 	EventConf conf;
-	int fd = -1;
-	fd = open(conf_file.c_str(), O_RDONLY);
-	conf.ParseFromFileDescriptor(fd);
-	VLOG(2) << "conf: " << conf.DebugString();
-	close(fd);
+	google::protobuf::TextFormat::ParseFromString(string, &conf);
+	game_events.AddEvent(conf);
+
+	Unit caster;
+	Unit victim;
+	caster.health = 1000;
+	victim.health = 2000;
+	Spell spell;
+	Buff buff;
+	spell.caster = &caster;
+	spell.victim = &victim;
+	CombatContex contex(&spell, &buff);
+
+	game_events.Excute(5, &contex);
+	ASSERT_EQ(2000, victim.health);
+
+	buff.buff_type = 2;
+	game_events.Excute(5, &contex);
+	ASSERT_EQ(1900, victim.health);
 }
 
 } // namespace Egametang

+ 6 - 1
Cpp/Game/Event/NodeFactories.cc

@@ -1,3 +1,4 @@
+#include <glog/logging.h>
 #include "Base/Typedef.h"
 #include "Event/AndNode.h"
 #include "Event/OrNode.h"
@@ -9,7 +10,7 @@
 
 namespace Egametang {
 
-NodeFactories::NodeFactories(): factories(2000)
+NodeFactories::NodeFactories(): factories(2000, NULL)
 {
 	factories[0] = new AndNodeFactory();
 	factories[1] = new OrNodeFactory();
@@ -26,6 +27,10 @@ NodeFactories::~NodeFactories()
 {
 	for (std::size_t i = 0; i < factories.size(); ++i)
 	{
+		if (factories[i] == NULL)
+		{
+			continue;
+		}
 		delete factories[i];
 	}
 }

+ 4 - 0
Cpp/Game/Event/NodeIf.h

@@ -1,6 +1,8 @@
 #ifndef EVENT_NODEIF_H
 #define EVENT_NODEIF_H
 
+#include <string>
+
 namespace Egametang {
 
 class ContexIf;
@@ -14,6 +16,8 @@ public:
 	virtual void AddChildNode(NodeIf *node)
 	{
 	}
+
+	virtual std::string ToString() = 0;
 };
 
 class NodeFactoryIf

+ 8 - 0
Cpp/Game/Event/NotNode.cc

@@ -22,6 +22,14 @@ void NotNode::AddChildNode(NodeIf *node)
 	this->node = node;
 }
 
+std::string NotNode::ToString()
+{
+	std::string s;
+	s += "NotNode: \n";
+	s += "    " + node->ToString() + "\n";
+	return s;
+}
+
 NodeIf* NotNodeFactory::GetInstance(const EventNode& conf)
 {
 	return new NotNode();

+ 2 - 0
Cpp/Game/Event/NotNode.h

@@ -18,6 +18,8 @@ public:
 	virtual bool Run(ContexIf* contex);
 
 	virtual void AddChildNode(NodeIf *node);
+
+	virtual std::string ToString();
 };
 
 class NotNodeFactory: public NodeFactoryIf

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

@@ -29,6 +29,17 @@ 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();

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

@@ -17,6 +17,8 @@ public:
 	virtual bool Run(ContexIf* contex);
 
 	virtual void AddChildNode(NodeIf *node);
+
+	virtual std::string ToString();
 };
 
 class OrNodeFactory: public NodeFactoryIf

+ 5 - 0
Cpp/Game/Event/SpellBuff.h

@@ -20,6 +20,11 @@ class Buff
 {
 public:
 	int buff_type;
+
+public:
+	Buff(): buff_type(0)
+	{
+	}
 };
 
 } // namespace Egametang