Saturday, June 9, 2012

Isometric 3d Game part 3

I have used an AI to generate the optimal path for movements of the tank. I have adopted A* algorithm for that. Cost of 1 second will be accounted for both turning the tank and moving the tank to a new block.  Path is generated after each movement of the tank. Then only tank can pickup newly appeared coins or life packs near to current position of the tank..
using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace TileMap
{
class AI
{
public Stack generatePath(Map map,int Sx,int Sy,int Dx,int Dy)
{
PriorityQueue working = new PriorityQueue();
PriorityQueue finished=new PriorityQueue();
Stack st = new Stack();
working.enqueue(new GridSquare(0,Sx,Sy,-1,-1,0));
GridSquare temp,tempworking;
int tempcost;
while (true)
{
temp = working.dequeue();
finished.enqueue(temp);
if ((temp.x == Dx) && (temp.y == Dy))
{
st.Push(temp);
while ((temp.parX != -1) && (temp.parY != -1))
{
temp = finished.get(temp.parX,temp.parY);
st.Push(temp);
}
break;
}
//north
if ((temp.y - 1 >= 0) && ((map.Blocks[temp.y - 1, temp.x].Tile == 0) || (map.Blocks[temp.y - 1, temp.x].Tile == 4) || (map.Blocks[temp.y - 1, temp.x].Tile == 5)))
{
if (finished.get(temp.x, temp.y - 1) == null)
{
tempworking = working.get(temp.x, temp.y - 1);
if (temp.dir == 0) tempcost = temp.cost + 1; else tempcost = temp.cost + 2;
if (tempworking == null)
{
working.enqueue(new GridSquare(tempcost,temp.x,temp.y-1,temp.x,temp.y,0));
}
else if (tempworking.cost > tempcost)
{
working.remove(tempworking.x,tempworking.y);
working.enqueue(new GridSquare(tempcost, temp.x, temp.y - 1, temp.x, temp.y, 0));
}
}
}
//east
if ((temp.x + 1 < Constants.MAP_SIZE) && ((map.Blocks[temp.y, temp.x + 1].Tile == 0) || (map.Blocks[temp.y, temp.x + 1].Tile == 4) || (map.Blocks[temp.y, temp.x + 1].Tile == 5)))
{
if (finished.get(temp.x+1, temp.y) == null)
{
tempworking = working.get(temp.x+1, temp.y);
if (temp.dir == 1) tempcost = temp.cost + 1; else tempcost = temp.cost + 2;
if (tempworking == null)
{
working.enqueue(new GridSquare(tempcost, temp.x+1, temp.y, temp.x, temp.y, 1));
}
else if (tempworking.cost > tempcost)
{
working.remove(tempworking.x, tempworking.y);
working.enqueue(new GridSquare(tempcost, temp.x+1, temp.y, temp.x, temp.y, 1));
}
}
}
//south
if ((temp.y + 1 < Constants.MAP_SIZE) && ((map.Blocks[temp.y + 1, temp.x].Tile == 0) || (map.Blocks[temp.y + 1, temp.x].Tile == 4) || (map.Blocks[temp.y + 1, temp.x].Tile == 5)))
{
if (finished.get(temp.x, temp.y + 1) == null)
{
tempworking = working.get(temp.x, temp.y + 1);
if (temp.dir == 2) tempcost = temp.cost + 1; else tempcost = temp.cost + 2;
if (tempworking == null)
{
working.enqueue(new GridSquare(tempcost, temp.x, temp.y + 1, temp.x, temp.y, 2));
}
else if (tempworking.cost > tempcost)
{
working.remove(tempworking.x, tempworking.y);
working.enqueue(new GridSquare(tempcost, temp.x, temp.y + 1, temp.x, temp.y, 2));
}
}
}
//west
if ((temp.x - 1 >= 0) && ((map.Blocks[temp.y, temp.x - 1].Tile == 0) || (map.Blocks[temp.y, temp.x - 1].Tile == 4) || (map.Blocks[temp.y, temp.x - 1].Tile == 5)))
{
if (finished.get(temp.x-1, temp.y) == null)
{
tempworking = working.get(temp.x-1, temp.y);
if (temp.dir == 0) tempcost = temp.cost + 1; else tempcost = temp.cost + 2;
if (tempworking == null)
{
working.enqueue(new GridSquare(tempcost, temp.x-1, temp.y, temp.x, temp.y, 3));
}
else if (tempworking.cost > tempcost)
{
working.remove(tempworking.x, tempworking.y);
working.enqueue(new GridSquare(tempcost, temp.x-1, temp.y, temp.x, temp.y, 3));
}
}
}
}
return st;
}
}
}
view raw isogame3.java hosted with ❤ by GitHub

No comments:

Post a Comment