Esempio n. 1
0
int exec(int op, int a, int b)
{
	int c;
	if (op < 0)
	{
		op = abs(op);
		c = a;
		a = b;
		b = c;
	}
	switch (op)
	{
	case 0:
	{
			  return -a; 
			  break;
	}
	case 1:
	{
			  return a + b;
			  break;
	}
	case 2:
	{
			  return a - b;
			  break;
	}
	case 3:
	{
			  return a*b;
			  break;
	}
	case 4:
	{
			  if (b == 0)
				  return 0;
			  else
				  return a/b;
			  break;
	}
	case 5:
	{
			  return abs(a);
			  break;
	}
	case 6:
	{
			  return _Pow_int(a, b);
			  break;
	}
	case 7:
	case 13:
	case 77:
	{
			   if (b == 0)
				   return 0;
			   else
			   return a%b;
			   break;
	}
	case 8:
	{
			  return __max(a, b);
			  break;
	}
	case 9:
	{
			  return __min(a, b);
			  break;
	}
	case 10:
	{
			   switch (abs(b) % 8)
			   {
			   case 0: return abs(a)*sizeof(char); break;
			   case 1: return abs(a)*sizeof(signed char); break;
			   case 2: return abs(a)*sizeof(short); break;
			   case 3: return abs(a)*sizeof(unsigned int); break;
			   case 4: return abs(a)*sizeof(long); break;
			   case 5: return abs(a)*sizeof(unsigned long long); break;
			   case 6: return abs(a)*sizeof(float); break;
			   case 7: return abs(a)*sizeof(double); break;
			   }
	}
	case 11:
	{
			   if (a == 0)
				   return 0;
			   else
			   return 9 * M_PI*cosf(a*b) / a;
			   break;

	}
	default:
	{
			   if (op < 100)
			   {
				   return op % abs(a + 1) + op % abs(b + 1);
			   }
			   else
			   {
				   return -1;
			   }
	}
	}
}
Esempio n. 2
0
static int __int_pow(int base, int p)
{
	return _Pow_int(base, p);
}
int ASTAR_FindPathWithTimeLimit(int from, int to, int *pathlist)
{
	int PathPointCount = 0;
	std::vector<int>OpenList;
	std::vector<int>PathList;
	int			startTime = trap_Milliseconds();

	// UQ1: Because this A* is only used to hunt enemies, limit the over-all range on requests to save CPU time...
	if (Distance(gWPArray[from]->origin, gWPArray[to]->origin) > 4096.0f) return -1;

	if (!PATHING_IGNORE_FRAME_TIME && trap_Milliseconds() - FRAME_TIME > 300)
	{// Never path on an already long frame time...
		return -1;
	}

	// Initialize...
	ASTAR_InitSASNodes();

	OpenList.clear();
	PathList.clear();
	int i=0;

	pASNode[from].bIsOpen=true;
	pASNode[from].G=0;
	OpenList.push_back(from);
		
	for(i = 0; i < gWPNum; i++)
	{
		pASNode[i].H=(int)sqrtl(_Pow_int(gWPArray[i]->origin[0] - gWPArray[to]->origin[0], 2) 
			+ _Pow_int(gWPArray[i]->origin[1] - gWPArray[to]->origin[1], 2)
			+ _Pow_int(gWPArray[i]->origin[2] - gWPArray[to]->origin[2], 2));
	}

	while(1)
	{
		bool bFoundPath=false;
		bool HaveNotClosed = false;

		for(i = 0; i < OpenList.size(); i++)
		{
			if(!pASNode[OpenList[i]].bIsClosed)
			{
				if(OpenList[i]==to)
				{
					bFoundPath=true;
					break;
				}

				HaveNotClosed = true;
				break;
			}
		}

		if(bFoundPath)
			break;

		if(!HaveNotClosed || trap_Milliseconds() - startTime > 500) // UQ1: Try limiting by timer, if we dont have one by now, we shouldn't bother!
		{
			//G_Printf("JKG A*: Failed to find a normal path - trying backup A*\n");
			return -1; // Failed...
			//return DOM_FindIdealPathtoWP(NULL, from, to, -1, pathlist);
		}

		int MinFIndex=-1;
		int MinF=99999999;

		for(i=0;i<OpenList.size();i++)
		{
			if(!pASNode[OpenList[i]].bIsClosed)
			{
				if(OpenList[i]==to)
				{
					bFoundPath=true;
					break;
				}
			}

			int tempI=OpenList[i];
			if(!pASNode[tempI].bIsClosed)
			{
				int tempF=pASNode[tempI].G+pASNode[to].H;
				if(tempF<MinF)
				{
					MinF=tempF;
					MinFIndex=tempI;
				}
			}
		}

		if(bFoundPath)
			break;

		for(int j=0;j<pASNode[MinFIndex].NeighborCount;j++)
		{
			SASNode *pTempChild=&pASNode[pASNode[MinFIndex].Neighbor[j]];

			if(pTempChild->bIsOpen)
			{
				if(pTempChild->G>pASNode[MinFIndex].G+pASNode[MinFIndex].NeighborDistance[j])
				{
					pTempChild->G=pASNode[MinFIndex].G+pASNode[MinFIndex].NeighborDistance[j];
					pTempChild->ParentIndex=MinFIndex;
				}
			}
			else
			{
				pTempChild->bIsOpen=true;
				OpenList.push_back(pASNode[MinFIndex].Neighbor[j]);
				pTempChild->ParentIndex=MinFIndex;
				pTempChild->G=pASNode[MinFIndex].G+pASNode[MinFIndex].NeighborDistance[j];
			}
		}

		pASNode[MinFIndex].bIsClosed=true;
	}

	int tempIndex = to;
	PathList.push_back(tempIndex);
		
	while(tempIndex != from)
	{
		pathlist[PathPointCount] = tempIndex;
		PathPointCount++;

		tempIndex=pASNode[tempIndex].ParentIndex;
		PathList.push_back(tempIndex);
	}

	pathlist[PathPointCount] = from;
	PathPointCount++;

	/*
	G_Printf("=====================================================================");
	G_Printf("Path (length %i) found was:\n", PathPointCount);

	for (i = 0; i < PathPointCount; i++)
	{
		if (i == PathPointCount - 1)
			G_Printf("%i (goal) ", pathlist[i]);
		else
			G_Printf("%i (dist to next %f) ", pathlist[i], Distance(gWPArray[pathlist[i]]->origin, gWPArray[pathlist[i+1]]->origin));
	}

	G_Printf("\n");
	G_Printf("=====================================================================");
	*/

	//PathPointCount=PathList.size();
	return PathPointCount;
}
int ASTAR_ShortenPath(int old_pathlist_size, int *old_pathlist, int *pathlist)
{
	int PathPointCount = 0;
	std::vector<int>OpenList;
	std::vector<int>PathList;
	int startTime = trap_Milliseconds();

	ORIGINAL_SIZE = old_pathlist_size;

	// Initialize...
	ASTAR_InitSASNodesShorten(old_pathlist_size, old_pathlist);

	//G_Printf("Path is %i nodes from node %i to node %i.\n", old_pathlist_size, old_pathlist[old_pathlist_size-1], old_pathlist[0]);

	OpenList.clear();
	PathList.clear();
	int i=0;

	pASNode[old_pathlist[old_pathlist_size-1]].bIsOpen=true;
	pASNode[old_pathlist[old_pathlist_size-1]].G=0;
	OpenList.push_back(old_pathlist[old_pathlist_size-1]);
		
	for(i = 0; i < old_pathlist_size; i++)
	{
		pASNode[old_pathlist[i]].H=(int)sqrtl(_Pow_int(gWPArray[i]->origin[0] - gWPArray[old_pathlist[0]]->origin[0], 2) 
			+ _Pow_int(gWPArray[i]->origin[1] - gWPArray[old_pathlist[0]]->origin[1], 2)
			+ _Pow_int(gWPArray[i]->origin[2] - gWPArray[old_pathlist[0]]->origin[2], 2));
	}

	while(1)
	{
		bool bFoundPath=false;
		int NotClosedCount=0;

		for(i = 0; i < OpenList.size(); i++)
		{
			if(!pASNode[OpenList[i]].bIsClosed)
			{
				if(OpenList[i]==old_pathlist[0])
				{
					bFoundPath=true;
					break;
				}

				NotClosedCount++;
			}
		}

		if(bFoundPath)
			break;

		if(NotClosedCount==0 || trap_Milliseconds()-startTime > 1000)
		{
			//G_Printf("JKG A*: Failed to find a normal path - trying backup A*\n");
			return -1; // Failed...
			//return DOM_FindIdealPathtoWP(NULL, from, to, -1, pathlist);
		}

		int MinFIndex=-1;
		int MinF=99999999;

		for(i=0;i<OpenList.size();i++)
		{
			int tempI=OpenList[i];
			if(!pASNode[tempI].bIsClosed)
			{
				int tempF=pASNode[tempI].G+pASNode[old_pathlist[0]].H;
				if(tempF<MinF)
				{
					MinF=tempF;
					MinFIndex=tempI;
				}
			}
		}

		for(int j=0;j<pASNode[MinFIndex].NeighborCount;j++)
		{
			SASNode *pTempChild=&pASNode[pASNode[MinFIndex].Neighbor[j]];

			if(pTempChild->bIsOpen)
			{
				if(pTempChild->G>pASNode[MinFIndex].G+pASNode[MinFIndex].NeighborDistance[j])
				{
					pTempChild->G=pASNode[MinFIndex].G+pASNode[MinFIndex].NeighborDistance[j];
					pTempChild->ParentIndex=MinFIndex;
				}
			}
			else
			{
				pTempChild->bIsOpen=true;
				OpenList.push_back(pASNode[MinFIndex].Neighbor[j]);
				pTempChild->ParentIndex=MinFIndex;
				pTempChild->G=pASNode[MinFIndex].G+pASNode[MinFIndex].NeighborDistance[j];
			}
		}

		pASNode[MinFIndex].bIsClosed=true;

		if(bFoundPath)
			break;
	}

	int tempIndex = old_pathlist[0];
	PathList.push_back(tempIndex);
		
	while(tempIndex != old_pathlist[old_pathlist_size-1])
	{
		pathlist[PathPointCount] = tempIndex;
		PathPointCount++;

		tempIndex=pASNode[tempIndex].ParentIndex;
		PathList.push_back(tempIndex);
	}

	pathlist[PathPointCount] = old_pathlist[old_pathlist_size-1];
	PathPointCount++;

	/*
	G_Printf("=====================================================================");
	G_Printf("Path (length %i) found was:\n", PathPointCount);

	for (i = 0; i < PathPointCount; i++)
	{
		if (i == PathPointCount - 1)
			G_Printf("%i (goal) ", pathlist[i]);
		else
			G_Printf("%i (dist to next %f) ", pathlist[i], Distance(gWPArray[pathlist[i]]->origin, gWPArray[pathlist[i+1]]->origin));
	}

	G_Printf("\n");
	G_Printf("=====================================================================");
	*/

	//PathPointCount=PathList.size();
	return PathPointCount;
}
int ASTAR_FindPath(int from, int to, int *pathlist)
{
	int PathPointCount = 0;
	std::vector<int>OpenList;
	std::vector<int>PathList;

	//
	// This version is completely unrestricted. ONLY use it if a path is EXTREMELY important! Pussible huge CPU spikes...
	//

	// Initialize...
	ASTAR_InitSASNodes();

	OpenList.clear();
	PathList.clear();
	int i=0;

	pASNode[from].bIsOpen=true;
	pASNode[from].G=0;
	OpenList.push_back(from);
		
	for(i = 0; i < gWPNum; i++)
	{
		pASNode[i].H=(int)sqrtl(_Pow_int(gWPArray[i]->origin[0] - gWPArray[to]->origin[0], 2) 
			+ _Pow_int(gWPArray[i]->origin[1] - gWPArray[to]->origin[1], 2)
			+ _Pow_int(gWPArray[i]->origin[2] - gWPArray[to]->origin[2], 2));
	}

	while(1)
	{
		bool bFoundPath=false;
		int NotClosedCount=0;

		for(i = 0; i < OpenList.size(); i++)
		{
			if(!pASNode[OpenList[i]].bIsClosed)
			{
				if(OpenList[i]==to)
				{
					bFoundPath=true;
					break;
				}

				NotClosedCount++;
			}
		}

		if(bFoundPath)
			break;

		if(NotClosedCount==0)
		{
			//G_Printf("JKG A*: Failed to find a normal path - trying backup A*\n");
			return -1; // Failed...
			//return DOM_FindIdealPathtoWP(NULL, from, to, -1, pathlist);
		}

		int MinFIndex=-1;
		int MinF=99999999;

		for(i=0;i<OpenList.size();i++)
		{
			int tempI=OpenList[i];
			if(!pASNode[tempI].bIsClosed)
			{
				int tempF=pASNode[tempI].G+pASNode[to].H;
				if(tempF<MinF)
				{
					MinF=tempF;
					MinFIndex=tempI;
				}
			}
		}

		for(int j=0;j<pASNode[MinFIndex].NeighborCount;j++)
		{
			SASNode *pTempChild=&pASNode[pASNode[MinFIndex].Neighbor[j]];

			if(pTempChild->bIsOpen)
			{
				if(pTempChild->G>pASNode[MinFIndex].G+pASNode[MinFIndex].NeighborDistance[j])
				{
					pTempChild->G=pASNode[MinFIndex].G+pASNode[MinFIndex].NeighborDistance[j];
					pTempChild->ParentIndex=MinFIndex;
				}
			}
			else
			{
				pTempChild->bIsOpen=true;
				OpenList.push_back(pASNode[MinFIndex].Neighbor[j]);
				pTempChild->ParentIndex=MinFIndex;
				pTempChild->G=pASNode[MinFIndex].G+pASNode[MinFIndex].NeighborDistance[j];
			}
		}

		pASNode[MinFIndex].bIsClosed=true;

		if(bFoundPath)
			break;
	}

	int tempIndex = to;
	PathList.push_back(tempIndex);
		
	while(tempIndex != from)
	{
		pathlist[PathPointCount] = tempIndex;
		PathPointCount++;

		tempIndex=pASNode[tempIndex].ParentIndex;
		PathList.push_back(tempIndex);
	}

	pathlist[PathPointCount] = from;
	PathPointCount++;

	/*
	G_Printf("=====================================================================");
	G_Printf("Path (length %i) found was:\n", PathPointCount);

	for (i = 0; i < PathPointCount; i++)
	{
		if (i == PathPointCount - 1)
			G_Printf("%i (goal) ", pathlist[i]);
		else
			G_Printf("%i (dist to next %f) ", pathlist[i], Distance(gWPArray[pathlist[i]]->origin, gWPArray[pathlist[i+1]]->origin));
	}

	G_Printf("\n");
	G_Printf("=====================================================================");
	*/

	//PathPointCount=PathList.size();
	return PathPointCount;
}