void remove_entity( const T& ent ) {
		all_entities.remove( ent );
	}
Esempio n. 2
0
GMEXPORT double CalculatePathFromXYtoXY(double x1, double y1, double x2, double y2)
{

	if (x1 != x2 || y1 != y2)
	{
	m_PathList.clear();

	std::map<int, std::map<int, MapSearchNode*> > grid;
	for (int i = 0; i < 42; i++)
	{
		for (int j = 0; j < 34; j++)
		{
			grid[i][j] = new MapSearchNode();
			grid[i][j]->x = i;
			grid[i][j]->y = j;
		}
	}

	ofstream fileStream;
	fileStream.open("level_paths.txt");
	// define the new nodes
	MapSearchNode* start = new MapSearchNode();
	start->x = x1;
	start->y = y1;

	fileStream << "START";
	fileStream << " START X: ";
	fileStream << start->x;
	fileStream << " START Y: ";
	fileStream << start->y;

	MapSearchNode* end = new MapSearchNode();
	end->x = x2;
	end->y = y2;

	fileStream << "END";
	fileStream << " END X: ";
	fileStream << end->x;
	fileStream << " END Y: ";
	fileStream << end->y;

	MapSearchNode* current = new MapSearchNode();
	MapSearchNode* child = new MapSearchNode();

	std::list<MapSearchNode*> openList;
	std::list<MapSearchNode*> closedList;
	list<MapSearchNode*>::iterator i;

	unsigned int n = 0;

	openList.push_back(start);
	start->opened = true;

	while (n == 0 || (current != end && n < 50))
	{
		// Look for the smallest f value in the openList
		for (i = openList.begin(); i != openList.end(); i++)
		{
			if (i == openList.begin() || (*i)->GetFScore() <= current->GetFScore())
			{
				current = (*i);
			}
		}

		fileStream << "searching";
		fileStream << " Current X: ";
		fileStream << current->x;
		fileStream << " Current Y: ";
		fileStream << current->y;

		// Stop if we've reached the end
		if (current->x == end->x && current->y == end->y)
		{
			fileStream << "end reached";
			break;
		}

		// Remove the current point from the open list
		openList.remove(current);
		current->opened = false;
		
		// Add the current point from the open list
		closedList.push_back(current);
		current->closed = true;

		// Get all the current adjacent walkable points
		for (int x = -1; x < 2; x++)
		{
			for (int y = -1; y < 2; y++)
			{
				if (x == 0 && y == 0)
				{
					// ignore current node, pass
					continue;
				}

				if (x == 0 || y == 0)
				{

					child = grid[current->x + x][current->y + y];

					// if it's closed or not walkable then pass
					if (child->closed || (spmap[child->x][child->y] != 0 && spmap[child->x][child->y] != 3 && spmap[child->x][child->y] != 4 && spmap[child->x][child->y] != 2 && spmap[child->x][child->y] != 9))
					{
						fileStream << "\n";
						fileStream << "closed or not walkable";
						continue;
					}

					// IF AT A CORNER?
	
					// if it's already in the opened list
					if (child->opened)
					{
						if (child->gScore > child->GetGScore(current))
						{
							child->parent = current;
							child->ComputeScores(end);
						}
					}
					else
					{
						openList.push_back(child);
						child->opened = true;
	
						// COMPUTE THE G
						child->parent = current;
						child->ComputeScores(end);
					}
				}
 			}
		}
		n++;
		fileStream << "\n";
	}

	// Reset
	for (i = openList.begin(); i != openList.end(); i++)
	{
		(*i)->opened = false;
	}
	for (i = closedList.begin(); i != closedList.end(); i++)
	{
		(*i)->closed = false;
	}
	fileStream.close();

	fileStream.open("level_path.txt");
	// resolve the path starting from the end point
	while (current->parent && current != start)
	{
		fileStream << "X ";
		fileStream << current->x;
		fileStream << " Y ";
		fileStream << current->y;
		fileStream << "\n";
		m_PathList.push_back(current);
		current = current->parent;
		n ++;
	}
	fileStream.close();
	return 0;
	}
	return 0;
}