Commits

Mihael Peklar committed 253454d

Some pathfinding modifications and fixes.

  • Participants
  • Parent commits 2b9e9c2

Comments (0)

Files changed (2)

 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;
+	y*=-1;
+	if(x<0)
+		x=0;
+	if(y<0)
+		y=0;
+	if(x>(this->tilesPerRow-1)*TILE_WIDTH)
+		x=(this->tilesPerRow-1)*TILE_WIDTH;
+	if(y>(this->tilesPerColumn-1)*TILE_HEIGHT)
+		y=(this->tilesPerColumn-1)*TILE_HEIGHT;
+	return x/TILE_WIDTH + (int)((y/TILE_HEIGHT))*this->tilesPerRow;
+}
+
+void Map::boundFix(float& x, float& y, float offX, float offY)
+{
+	x+=offX; y-=offY;
+	y*=-1;
+	if(x<0)
+		x=0;
+	if(y<0)
+		y=0;
+	if(x>this->tilesPerRow*TILE_WIDTH)
+		x=(this->tilesPerRow)*TILE_WIDTH;
+	if(y>this->tilesPerColumn*TILE_HEIGHT)
+		y=(this->tilesPerColumn)*TILE_HEIGHT;
+	y*=-1;
+	x-=offX; y+=offY;
 }
 
 /////////////////////////////////////////////////////////////
     }
 	//Commented out for testing purposes
 	blocked[s]=0;
-	blocked[e]=1;
+	//blocked[e]=1;
 	for(int i=mem.size()-1; mem[i].second>=0; i=mem[i].second)
 		res.push_back(mem[i].first);
 	res.push_back(s);
 
 std::deque<Order> Map::path_order(float px, float py, float ex, float ey)
 {
+	boundFix(ex, ey);
 	int s=this->findTile(px, py), e=this->findTile(ex, ey);
 	std::deque<Order> out;
 	std::vector<int> x=this->find_path(s, e);
 	drawThis=x;
 	float nx, ny;
 	Order o;
+	o.order=ORDER_MOVE;
+	o.details.move.srcx=px;
+	o.details.move.srcy=py;
+	if(s==e)
+		out.push_back(o);
 	for(std::vector<int>::iterator i=++x.begin(); i<x.end(); ++i)
 	{
 		nx=*i%this->tilesPerRow*TILE_WIDTH, ny=-*i/this->tilesPerRow*TILE_HEIGHT;
 		out.push_back(o);
 		px=nx; py=ny;
 	}
-	o.order=ORDER_MOVE;
-	out.back().details.move.dstx=ex;
-	out.back().details.move.dsty=ey;
+	if(!out.empty())
+	{
+		out.back().details.move.dstx=ex;
+		out.back().details.move.dsty=ey;
+	}
 //	o.details.move={nx, ny, ex, ey, 0.0}; //problem?
-	px=nx; py=ny;
 	return out;
 }
 
 #include <vector>
 #include<queue>
 #include <string>
-class Tile;
+#include "tileset.h"
 class Tileset;
 class Unit;
 class Order;
+class Tile;
 
 class Map
 {
     double tileSpeed(int x, int y);
     int getTilesPerRow() {return this->tilesPerRow;}
     int getTilesPerColumn() {return this->tilesPerColumn;}
+    void boundFix(float& x, float& y, float offX=TILE_WIDTH/2, float offY=TILE_HEIGHT/2);
     UnitContainer units;
     std::vector<int> find_path(int st, int et);
     std::deque<Order> path_order(float px, float py, float ex, float ey);