Commits

Mihael Peklar committed 3cef633

Pathfinding alogrithm outputs results to stdout.
make rebuild now works properly.

Comments (0)

Files changed (6)

 rebuild: clean all
 
 clean:
-	rm $(OBJECTS) thecombat
+	rm $(OBJECTS) thecombat || true
 						Unit *unit = *it;
 						if(unit->isSelected())
 						{
-							printf("%d\n", this->map->find_path(0, 7).size());
+							printf("%d\n", this->map->find_path(this->map->findTile(unit->getX(), unit->getY()), this->map->findTile(adjustedMouseX, adjustedMouseY)).size());
 							unit->defaultOrder(adjustedMouseX, adjustedMouseY);
 						}
 					}
 	}
 }
 
+int Map::findTile(float x, float y)
+{
+	x+=TILE_WIDTH/2; y-=TILE_HEIGHT/2;
+	return x/TILE_WIDTH + (int)((-y/TILE_HEIGHT))*this->tilesPerRow;
+}
+
 /////////////////////////////////////////////////////////////
 //Pathfinding
 class Map::_pnode //used in pathfinding (Priority queue NODE)
 
 std::vector<int> Map::find_path(int s, int e)
 {
+	std::vector<int> res;
 	std::vector< pair<int, int> > mem;
 	std::vector<bool> visited; visited.resize(tiles.size(), 0);
 	std::priority_queue< pnode > dot;
-	dot.push(pnode(0.0, 0, -1));
+	if(s<0 || e<0 || s>=this->tiles.size() || e>=this->tiles.size())
+		return res;
+	dot.push(pnode(0.0, s, -1));
 	int c=-1, p;
 	double d;
 	bool b=1;
 			dot.push(pnode(d+distance(c, c+tilesPerRow), c+tilesPerRow, mem.size()-1));
 		//printf("STEP\n");
 	}
-	blocked[s]=1;
-	std::vector<int> res;
+	//blocked[s]=1;
 	if(b)
 		return res;
 	//Commented out for testing purposes
     int getTilesPerColumn() {return this->tilesPerColumn;}
     UnitContainer units;
     std::vector<int> find_path(int st, int et);
+    int findTile(float x, float y);
 
     private:
     Tileset* tileset;
 
 void Tileset::paintTile(int tileId, float spaceX, float spaceY)
 {
-#define TILE_WIDTH 64
-#define TILE_HEIGHT 64
     int tilesPerRow = texture->getPixelWidth() / TILE_WIDTH;
 
     int x = (tileId % tilesPerRow) * TILE_WIDTH;
 class Texture;
 class TileTypeDescription;
 
+const float TILE_WIDTH=64;
+const float TILE_HEIGHT=64;
+
 class Tileset
 {
     public: