소스 검색

* a bit of pathfinding (still needs testing and implementation of something like BFS (I consider Dijkstra usless in this case))
* some minor changes

mateuszb 18 년 전
부모
커밋
0c17ec03a8
5개의 변경된 파일128개의 추가작업 그리고 1개의 파일을 삭제
  1. 25 1
      CDefObjInfoHandler.cpp
  2. 2 0
      CDefObjInfoHandler.h
  3. 64 0
      CPathfinder.cpp
  4. 36 0
      CPathfinder.h
  5. 1 0
      StartInfo.h

+ 25 - 1
CDefObjInfoHandler.cpp

@@ -18,7 +18,31 @@ void CDefObjInfoHandler::load()
 		DefObjInfo nobj;
 		std::string dump;
 		inp>>nobj.defName;
-		for(int yy=0; yy<4; ++yy)
+
+		for(int o=0; o<6; ++o)
+		{
+			nobj.blockMap[o] = 0xff;
+			nobj.visitMap[o] = 0x00;
+		}
+		std::string mapStr;
+		inp>>mapStr;
+		for(int v=0; v<mapStr.size(); ++v)
+		{
+			if(mapStr[v]=='0')
+			{
+				nobj.blockMap[v/6] ^= (128 >> (v%6));
+			}
+		}
+		inp>>mapStr;
+		for(int v=0; v<mapStr.size(); ++v)
+		{
+			if(mapStr[v]=='1')
+			{
+				nobj.visitMap[v/6] ^= (128 >> (v%6));
+			}
+		}
+
+		for(int yy=0; yy<2; ++yy)
 			inp>>dump;
 		inp>>nobj.type;
 		inp>>nobj.subtype;

+ 2 - 0
CDefObjInfoHandler.h

@@ -9,6 +9,8 @@ struct DefObjInfo
 	int priority;
 	int type, subtype;
 	int objType;
+	unsigned char visitMap[6];
+	unsigned char blockMap[6];
 	bool operator==(const std::string & por) const;
 };
 

+ 64 - 0
CPathfinder.cpp

@@ -0,0 +1,64 @@
+#include "global.h"
+#include "CPathfinder.h"
+
+#define CGI (CGameInfo::mainObj)
+
+CPath * CPathfinder::getPath(int3 &src, int3 &dest)
+{
+	if(src.z!=dest.z) //first check
+		return NULL;
+
+	//graph initialization
+	graph.resize(CGI->ac->map.width);
+	for(int i=0; i<graph.size(); ++i)
+	{
+		graph[i].resize(CGI->ac->map.height);
+		for(int j=0; j<graph[i].size(); ++j)
+		{
+			graph[i][j].accesible = true;
+			graph[i][j].dist = -1;
+			graph[i][j].theNodeBefore = NULL;
+			graph[i][j].x = i;
+			graph[i][j].y = j;
+		}
+	}
+
+	for(int h=0; h<CGI->objh->objInstances.size(); ++h)
+	{
+		if(CGI->objh->objInstances[h].pos.z == src.z)
+		{
+			unsigned char blockMap[6];
+			std::string ourName = CGI->ac->map.defy[CGI->objh->objInstances[h].defNumber].name;
+			std::transform(ourName.begin(), ourName.end(), ourName.begin(), (int(*)(int))toupper);
+			for(int y=0; y<CGI->dobjinfo->objs.size(); ++y)
+			{
+				std::string cName = CGI->dobjinfo->objs[y].defName;
+				std::transform(cName.begin(), cName.end(), cName.begin(), (int(*)(int))toupper);
+				if(cName==ourName)
+				{
+					for(int u=0; u<6; ++u)
+					{
+						blockMap[u] = CGI->dobjinfo->objs[y].blockMap[u];
+					}
+					break;
+				}
+			}
+			for(int i=0; i<6; ++i)
+			{
+				for(int j=0; j<8; ++j)
+				{
+					int cPosX = CGI->objh->objInstances[h].pos.x - j;
+					int cPosY = CGI->objh->objInstances[h].pos.y - i;
+					if(cPosX>0 && cPosY>0)
+					{
+						graph[cPosX][cPosY].accesible = blockMap[i] & (128 >> j);
+					}
+				}
+			}
+		}
+	}
+	//graph initialized
+
+
+	return NULL;
+}

+ 36 - 0
CPathfinder.h

@@ -0,0 +1,36 @@
+#ifndef CPATHFINDER_H
+#define CPATHFINDER_H
+#include "CGameInfo.h"
+#include "int3.h"
+#include <queue>
+#include <vector>
+
+struct CPathNode
+{
+	bool v1, v2, v3,
+		 v4,     v5,
+		 v6, v7, v8; //true if we can pass, false if not
+	bool accesible; //true if a hero can be on this node
+	int dist; //distance from the first node of searching; -1 is infinity
+	CPathNode * theNodeBefore;
+	int x, y; //coordiantes
+};
+
+struct CPath
+{
+	std::queue<CPathNode> nodes; //just get node by node
+};
+
+/**
+ * main pathfinder class
+ */
+class CPathfinder
+{
+private:
+	std::vector< std::vector<CPathNode> > graph;
+public:
+	CPath * getPath(int3 & src, int3 & dest); //calculates path between src and dest; returns pointer to CPath or NULL if path does not exists
+	CPath * getPath(int3 & src, int3 & dest, int (*getDist)(int3 & a, int3 b)); //calculates path between src and dest; returns pointer to CPath or NULL if path does not exists; uses getDist to calculate distance
+};
+
+#endif //CPATHFINDER_H

+ 1 - 0
StartInfo.h

@@ -2,6 +2,7 @@
 #define STARTINFO_H
 
 #include "global.h"
+#include <vector>
 
 enum Ebonus {brandom=-1,bartifact, bgold, bresource};