Join Sean on
Bookmark and Share
BOXHEAD'S NAVIGATION SYSTEM
Sean Cooper
November 3rd, 2008
Subject: Boxhead's Navigation System

AI PT 3: BOXHEAD'S Navigation SYSTEM.  I have been asked a lot of times how the navigation and object avoidance is achieved in Boxhead

Before reading this, it is really important to read the AI PT 2: THE MAP SYSTEM article below, as this is the basis for the navigation system.
Ok let's get started. The Navigation is fairly basic, but it is fast and can handle as many enemies as you want, basically each enemy (in this case a Zombie) will access a pre-defined navigation map, see fig 1.1

Navigation_0001 Navigation_0002

 

How do we do this? Well it is pretty simple, we flood-fill the map, if you are unfamiliar with this, then please read http://en.wikipedia.org/wiki/Flood_fill, this explains it really nicely. However, we want to make sure we fill the map one step at time from the Players position because we want the numbers to count away from the destination (the player), the Player is 0 and the cells away from the player are > 0.

Let's look at how we do this:

class NavCell extends Cell

Let's take the original Map cell class and extend it to make a new Class, NavCell. Now we have the NavCell we can create some properties that I think we will need to perform a flood fill. These properties will give us the result in Fig 1.1

{
    public var NavIndex:
int;
    public var NavID:int;
    public var MapX:int;
    public var MapY:int;

    public function NavCell( ix:int, iy:int)
    {
        super();
        MapX = ix;
        MapY = iy;
    }
}

Now instead of creating new Cells we now create new NavCells for the map.

for ( var iy:int = 0; iy < MapHeight; iy++)
{
    MapCells[iy] = new Array();
    for ( var ix:int = 0; ix < MapWidth; ix++)
        MapCells[iy][ix] = new NavCell(ix,iy);
}

Now let's fill the Map from the Players position:

// Create a list of NavCells to work with to start the fill
var NavCells:Array = [MapCells[ int(player.y)][ int(player.x)]];

// Set The index to 0 - as 0 is the destination
for each ( var cell:NavCell in NavCells)
    cell.mCellIndex = 0;

// ??? Remember this, we will go over it later
var NavID:int    = Math.floor( Math.random()*1000000000);
while (NavCells.length) //More than one NavCell
    NavCells = FloodMap_Step( NavCells, NavTime);

You can see here that the Player's cell is added to the NavCells and the call to FloodMap_Step is only called then there is a cell in the NavCell. Let's write this function and mark the map.

function FloodMap_Step( cells:Array, id:int):Array
{
    var rCells:Array = new Array()
    for each ( var cell:NavCell in cells)
    {
        // Get all the cells surrounding cells
        // Make sure you handle any overlap i.e. 0-1 = -1

        var northCell:NavCell = MapCells[cell.MapY-1][cell.MapX];
        var southCell:NavCell = MapCells[cell.MapY+1][cell.MapX];
        var eastCell:NavCell = MapCells[cell.MapY][cell.MapX+1];
        var westCell:NavCell = MapCells[cell.MapY][cell.MapX-1];

  HINT: You could cache these different directions with in the class NavCell and create them when you create the map.

        // has the cell already been processed?
        if ( northCell.NavID != id)
        {
            northCell.NavIndex = cell.NavIndex+1;
            northCell.NavID = id;
            rCells.push( northCell);
        }

        // same for South/West/East
        ...           
    }


    // Return all the newly acquired cells
   
return rCells;    
}

Now we said we would look at what the NavID is, well this ID allows us to check whether a cell has been processed or not, we don't want to check the same cell twice as the Flood fill will never end. Why not just clear the NavIndex? Well this makes it so you don't have to, you can just process the Navigation every time the player moves (or things change) and not have the overhead of the clear.

Now the Navigation becomes really simple for the Zombie, move to the next lowest cell surrounding the Cell it is on. When there is a ZERO in the adjacent cell then attack or do something...

Another Scenario:

Well this is a little more complex but the same thing applies except you add all the cells that the player's barricades are on.

// Add all the barricades to the NavCells
for each ( var b:Barricade in Barricades)
    NavCells.push( MapCells[ int(b.y)][ int(b.x)]];

Now continue with same as before and the NavMap will end up like FIG 2.1.

Hope this helps to progress your game. Any questions let me know on the email below,

Sean Cooper
develop@seantcooper.com


Bartemis
August 20th, 2010
Subject: AI that avoids walls
Do you also have a way to avoid walls so that the zombies don't stop if they are near walls. I see in your codes that the zombies stop if they are near a wall, but what if the zombies doesn't have wallbreaking powers?
Jesse
July 25th, 2010
Subject: Boxhead Custom Maps
Yes! a boxhead map maker! but it would hopefully not be online (but an online multiplayer game), as in a downloadable .exe file. Drag and drop program (hopefully). You would export the map as whatever file you decide, and on the online game, you would click a load button, load the map, and that's where you would play it too. I think that's a good idea, then you wouldnt have to create a server thingy where you upload your maps online. Think about it (please). I could make you a little demo thing in Scratch, and show it to you and you could decide if you liked it!
emb
July 07th, 2010
Subject: pt 1 & 2
Are part 1 and 2 of this article available somewhere?
Yaw Asabere
June 24th, 2010
Subject: game designer
This has been really helpful to me. With this i've been able to design an efficient ai and terrain system. I never thought about using grids for such things until I read this. Thank you
Alex
June 09th, 2010
Subject: I agree with the posts above...
And I love your work! Keep it up! :D 5/5
Kevin
June 02nd, 2010
Subject: Boxhead
OR HAVE SPLITSCREEN ZOMBIE WAR. :D the epicness continues
Steven Rhodes
May 13th, 2010
Subject: New Boxhead Designer
Im a big fan of the boxhead series and was wondering if you would be able to make a whole new boxhead, however this time let the users make the maps and then be able to play them using a variety of objects, for ex: walls barricades rocks wood trees etc. Also think of maybe putting like 5 or 6 different kinds of turrets in it. Where as Boxhead: The Zombie Wars has just the turret gun and the turret morter. So just take that into consideration
Add your comment/rating
Name:
*required
E-mail: *required (but private)
Subject: *required
Comment:
Your Rating:
 


DEVELOP TOPICS
SIGN UP FOR



© 2010 seantcooper.com | Copyright | About us | Contact us