Esempio n. 1
0
/*
 * radius is in full res.
 * returns the path cost.
 */
float CPathFinder::MakePath(F3Vec& posPath, float3& startPos, float3& endPos, int radius) {
	ai->math->TimerStart();
	path.clear();

	ai->math->F3MapBound(startPos);
	ai->math->F3MapBound(endPos);

	float totalcost = 0.0f;

	int ex = int(endPos.x / (8 * resmodifier));
	int ey = int(endPos.z / (8 * resmodifier));
	int sy = int(startPos.z / (8 * resmodifier));
	int sx = int(startPos.x / (8 * resmodifier));

	radius /= int(8 * resmodifier);

	if (micropather->FindBestPathToPointOnRadius(XY2Node(sx, sy), XY2Node(ex, ey), &path, &totalcost, radius) == MicroPather::SOLVED) {
		posPath.reserve(path.size());

		for (unsigned i = 0; i < path.size(); i++) {
			float3 mypos = Node2Pos(path[i]);
			mypos.y = ai->cb->GetElevation(mypos.x, mypos.z);
			posPath.push_back(mypos);
		}
	}

	return totalcost;
}
/*
radius is in full res.
returns the path cost.
*/
float CPathFinder::MakePath(vector<float3> *posPath, float3 *startPos, float3 *endPos, int radius)
{
	////L("Makepath radius Started");
	ai->math->TimerStart();
	float totalcost;
	ClearPath();
	int sx,sy,ex,ey;
	ai->math->F3MapBound(startPos);
	ai->math->F3MapBound(endPos);
	ex = int(endPos->x / (8 *resmodifier));
	ey = int(endPos->z / (8 *resmodifier));
	sy = int(startPos->z / (8 *resmodifier));
	sx = int(startPos->x / (8 *resmodifier));
	radius /= int(8 *resmodifier);
	////L("StartPos : " << startPos->x << "," << startPos->z << " End Pos: " << endPos->x << "," << endPos->z);
		if(micropather->FindBestPathToPointOnRadius(XY2Node(sx,sy),XY2Node(ex,ey),&path,&totalcost, radius) == MicroPather::SOLVED){
			////L("attack solution solved! Path size = " << path.size());
            posPath->reserve(path.size());
			for(unsigned i = 0; i < path.size();i++)	{
					////L("adding path point");
					float3 mypos = Node2Pos(path[i]);
					mypos.y = ai->cb->GetElevation(mypos.x, mypos.z);
					posPath->push_back(mypos);
			}
			//char c[500];
			//sprintf(c,"Time Taken: %f Total Cost %i",ai->math->TimerSecs(),int(totalcost));
			//ai->cb->SendTextMsg(c,0);
		}
		else{
			//L("MakePath: path failed");
			//ai->cb->SendTextMsg("ATTACK FAILED!",0);
		}
	return totalcost;
}
Esempio n. 3
0
float CPathFinder::FindBestPath(F3Vec& posPath, float3& startPos, float myMaxRange, F3Vec& possibleTargets) {
	ai->math->TimerStart();
	path.clear();

	// make a list with the points that will count as end nodes
	float totalcost = 0.0f;
	const unsigned int radius = int(myMaxRange / (8 * resmodifier));
	int offsetSize = 0;

	std::vector<std::pair<int, int> > offsets;
	std::vector<int> xend;

	std::vector<void*> endNodes;
	endNodes.reserve(possibleTargets.size() * radius * 10);

	{
		const unsigned int DoubleRadius = radius * 2;
		const unsigned int SquareRadius = radius * radius;

		xend.resize(DoubleRadius + 1);
		offsets.resize(DoubleRadius * 5);

		for (size_t a = 0; a < DoubleRadius + 1; a++) {
			const float z = (int) (a - radius);
			const float floatsqrradius = SquareRadius;
			xend[a] = int(sqrt(floatsqrradius - z * z));
		}

		offsets[0].first = 0;
		offsets[0].second = 0;

		size_t index = 1;
		size_t index2 = 1;

		for (size_t a = 1; a < radius + 1; a++) {
			int endPosIdx = xend[a];
			int startPosIdx = xend[a - 1];

			while (startPosIdx <= endPosIdx) {
				assert(index < offsets.size());
				offsets[index].first = startPosIdx;
				offsets[index].second = a;
				startPosIdx++;
				index++;
			}

			startPosIdx--;
		}

		index2 = index;

		for (size_t a = 0; a < index2 - 2; a++) {
			assert(index < offsets.size());
			assert(a < offsets.size());
			offsets[index].first = offsets[a].first;
			offsets[index].second = DoubleRadius - (offsets[a].second);
			index++;
		}

		index2 = index;

		for (size_t a = 0; a < index2; a++) {
			assert(index < offsets.size());
			assert(a < offsets.size());
			offsets[index].first = -(offsets[a].first);
			offsets[index].second = offsets[a].second;
			index++;
		}

		for (size_t a = 0; a < index; a++) {
			assert(a < offsets.size());
			offsets[a].first = offsets[a].first; // ??
			offsets[a].second = offsets[a].second - radius;
		}

		offsetSize = index;
	}

	for (unsigned i = 0; i < possibleTargets.size(); i++) {
		float3& f = possibleTargets[i];
		int x, y;
		// TODO: make the circle here

		ai->math->F3MapBound(f);
		Node2XY(Pos2Node(f), &x, &y);

		for (int j = 0; j < offsetSize; j++) {
			const int sx = x + offsets[j].first;
			const int sy = y + offsets[j].second;

			if (sx >= 0 && sx < PathMapXSize && sy >= 0 && sy < PathMapYSize) {
				endNodes.push_back(XY2Node(sx, sy));
			}
		}
	}

	ai->math->F3MapBound(startPos);

	if (micropather->FindBestPathToAnyGivenPoint(Pos2Node(startPos), endNodes, &path, &totalcost) == MicroPather::SOLVED) {
        posPath.reserve(path.size());

		for (unsigned i = 0; i < path.size(); i++) {
			int x, y;

			Node2XY(path[i], &x, &y);
			float3 mypos = Node2Pos(path[i]);
			mypos.y = ai->cb->GetElevation(mypos.x, mypos.z);
			posPath.push_back(mypos);
		}
	}

	return totalcost;
}
float CPathFinder::FindBestPath(vector<float3> *posPath, float3 *startPos, float myMaxRange, vector<float3> *possibleTargets)
{
	////L("FindBestPath Started");
	////L("startPos: x: " << startPos->x << ", z: " << startPos->z);
	ai->math->TimerStart();
	float totalcost;
	ClearPath();
	// Make a list with the points that will count as end nodes.
	static vector<void*> endNodes;
	int radius = int(myMaxRange / (8 *resmodifier));
	int offsetSize = 0;
	endNodes.resize(0);
	endNodes.reserve(possibleTargets->size() * radius * 10);
	////L("possibleTargets->size(): " << possibleTargets->size());
	pair<int, int> * offsets;
	{
		
		////L("radius: " << radius);
		int DoubleRadius = radius * 2;
		int SquareRadius = radius * radius; //used to speed up loops so no recalculation needed
		int * xend = new int[DoubleRadius+1]; 
		////L("1");
		for (int a=0;a<DoubleRadius+1;a++){ 
			float z=a-radius;
			float floatsqrradius = SquareRadius;
			xend[a]=int(sqrt(floatsqrradius-z*z));
		}
		////L("2");
		offsets = new pair<int, int>[DoubleRadius*5];
		int index = 0;
		int startPos = 0;
		////L("3");
		offsets[index].first = 0;
		////L("offsets[index].first: " << offsets[index].first);
		offsets[index].second = 0;
		////L("offsets[index].second: " << offsets[index].second);
		index++;
		
		for (int a=1;a<radius+1;a++){ 
			////L("a: " << a);
			int endPos = xend[a];
			int startPos = xend[a-1];
			////L("endPos: " << endPos);
			while(startPos <= endPos)
			{
				////L("startPos: " << startPos);
				offsets[index].first = startPos;
				////L("offsets[index].first: " << offsets[index].first);
				offsets[index].second = a;
				////L("offsets[index].second: " << offsets[index].second);
				startPos++;
				index++;
			}
			startPos--;
		}
		int index2 = index;
		for (int a=0;a<index2-2;a++)
		{
			offsets[index].first = offsets[a].first;
			////L("offsets[index].first: " << offsets[index].first);
			offsets[index].second = DoubleRadius - ( offsets[a].second);
			////L("offsets[index].second: " << offsets[index].second);
			index++;
		}
		index2 = index;
		////L("3.7");
		for (int a=0;a<index2;a++)
		{
			offsets[index].first =  - ( offsets[a].first);
			////L("offsets[index].first: " << offsets[index].first);
			offsets[index].second = offsets[a].second;
			////L("offsets[index].second: " << offsets[index].second);
			index++;
		}
		for (int a=0;a<index;a++)
		{
			offsets[a].first = offsets[a].first;// + radius;
			////L("offsets[index].first: " << offsets[a].first);
			offsets[a].second = offsets[a].second - radius;
			////L("offsets[index].second: " << offsets[a].second);

		}
		offsetSize = index;
		delete [] xend;
	}

	for(unsigned i = 0; i < possibleTargets->size(); i++)
	{
		float3 f = (*possibleTargets)[i];
		int x, y;
		////L("Added: x: " << f.x << ", z: " << f.z);
		// TODO: Make the circle here:
		
		ai->math->F3MapBound(&f);
		void * node = Pos2Node(f);
		Node2XY(node, &x, &y);
		for(int j = 0; j < offsetSize; j++)
		{
			int sx = x + offsets[j].first; 
			int sy = y + offsets[j].second;
			if(sx >= 0 && sx < PathMapXSize && sy >= 0 && sy < PathMapYSize)
				endNodes.push_back(XY2Node(sx, sy));
		}
		
		
		////L("node: " << ((int) node) << ", x: " << x << ", y: " << y);
		//endNodes.push_back(node);
	}
	ai->math->F3MapBound(startPos);
	////L("endNodes.size(): " << endNodes.size());
	delete [] offsets;
	
	if(micropather->FindBestPathToAnyGivenPoint( Pos2Node(*startPos), endNodes, &path, &totalcost) == MicroPather::SOLVED)
	{
		// Solved
		////L("attack solution solved! Path size = " << path.size());
        posPath->reserve(path.size());
		for(unsigned i = 0; i < path.size(); i++) {
			////L("adding path point");
			int x, y;
			Node2XY(path[i], &x, &y);
			////L("node: " << ((int) path[i]) << ". x: " << x << ", y: " << y);
			float3 mypos = Node2Pos(path[i]);
			////L("mypos: x: " << mypos.x << ", z: " << mypos.z);
			mypos.y = ai->cb->GetElevation(mypos.x, mypos.z);
			posPath->push_back(mypos);
		}
		//char c[500];
		//sprintf(c,"Time Taken: %f Total Cost %i",ai->math->TimerSecs(), int(totalcost));
		////L(c);
	}
	else
		//L("FindBestPath: path failed!");
	return totalcost;
}
float CPathFinder::FindBestPath(vector<float3>* posPath, float3* startPos, float myMaxRange, vector<float3>* possibleTargets) {
	ai->math->TimerStart();
	float totalcost;
	ClearPath();

	// make a list with the points that will count as end nodes
	static vector<void*> endNodes;
	int radius = int(myMaxRange / (8 * resmodifier));
	int offsetSize = 0;

	endNodes.resize(0);
	endNodes.reserve(possibleTargets->size() * radius * 10);

	pair<int, int>* offsets;

	{
		// L("radius: " << radius);
		int DoubleRadius = radius * 2;
		// used to speed up loops so no recalculation needed
		int SquareRadius = radius * radius;
		int* xend = new int[DoubleRadius + 1];

		for (int a = 0; a < DoubleRadius + 1; a++) {
			float z = a - radius;
			float floatsqrradius = SquareRadius;
			xend[a] = int(sqrt(floatsqrradius - z * z));
		}

		offsets = new pair<int, int>[DoubleRadius * 5];
		int index = 0;

		offsets[index].first = 0;
		offsets[index].second = 0;
		index++;

		for (int a = 1; a < radius + 1; a++) {
			// L("a: " << a);
			int endPos = xend[a];
			int startPos = xend[a - 1];

			while (startPos <= endPos) {
				offsets[index].first = startPos;
				offsets[index].second = a;
				startPos++;
				index++;
			}

			startPos--;
		}

		int index2 = index;
		for (int a = 0; a < index2 - 2; a++) {
			offsets[index].first = offsets[a].first;
			offsets[index].second = DoubleRadius - ( offsets[a].second);
			index++;
		}

		index2 = index;

		for (int a = 0; a < index2; a++) {
			offsets[index].first =  -( offsets[a].first);
			offsets[index].second = offsets[a].second;
			index++;
		}

		for (int a = 0; a < index; a++) {
			offsets[a].first = offsets[a].first;
			offsets[a].second = offsets[a].second - radius;
		}

		offsetSize = index;
		delete[] xend;
	}

	for (unsigned i = 0; i < possibleTargets->size(); i++) {
		float3 f = (*possibleTargets)[i];
		int x, y;
		// L("Added: x: " << f.x << ", z: " << f.z);
		// TODO: make the circle here

		ai->math->F3MapBound(&f);
		void * node = Pos2Node(f);
		Node2XY(node, &x, &y);

		for (int j = 0; j < offsetSize; j++) {
			int sx = x + offsets[j].first;
			int sy = y + offsets[j].second
;
			if (sx >= 0 && sx < PathMapXSize && sy >= 0 && sy < PathMapYSize)
				endNodes.push_back(XY2Node(sx, sy));
		}
	}

	ai->math->F3MapBound(startPos);
	delete[] offsets;

	if (micropather->FindBestPathToAnyGivenPoint(Pos2Node(*startPos), endNodes, &path, &totalcost) == MicroPather::SOLVED) {
        posPath->reserve(path.size());

		for (unsigned i = 0; i < path.size(); i++) {
			int x, y;
			Node2XY(path[i], &x, &y);
			float3 mypos = Node2Pos(path[i]);
			mypos.y = ai->cb->GetElevation(mypos.x, mypos.z);
			posPath->push_back(mypos);
		}
	}

	return totalcost;
}