Difference between revisions of "AI grid"

From Divinity Engine Wiki
Jump to: navigation, search
Line 6: Line 6:
 
[[File:AIGridMenuIcon.png|none]]
 
[[File:AIGridMenuIcon.png|none]]
 
take a look at [[AI_grid_panel|for more information about how to use this panel]]
 
take a look at [[AI_grid_panel|for more information about how to use this panel]]
 +
 +
Take the Gheto of dort joy as an example:
 +
<br/>[[File:AiWorld1.PNG|800px]]
 +
<br/>If you take a look the AI grid, it will look somewhat like this
 +
<br/>[[File:AiWorldAI.PNG|800px]]<br/>
 +
You can make a distinction between 2 main colors: <font color='red'>Red</font> and <font color='Green'>Green</font>. You can think of the AI grid as a board game. You are allowed to move from a green square to a neighboring green square, but you are not allowed to stand on red squares. As you can see in the example, you can walk on the ground and over the gratings (green cells), but you can not stand inside of a wall, or walk through the broken pillar or through the cages (red cells). It also prevents you from walking off steep slopes (as you can see in the bottom left and top left corner of the picture).<br/>
 +
 +
=AI Grid Generator - Behind the scenes=
 +
The AI Grid generator is an optimistic peace of machinery. In the beginning, the world is flat and you are allowed to stand on every tile. It then tries to figure out the height of every cell, and looks for any reason why you should not be standing there. It decides this by looking at 3 things:
 +
 +
# The objects on top of the cell
 +
# The angle of the cell
 +
# Is the cell reachable
 +
 +
==The objects on top of the cell==
 +
What the AIGridGenerator does is do a raycast on every tile of this grid to calculate two things: The height of the cell and whether you can walk on it or not. Think of it like a rain cloud that drops drops of rain on each and every tile. It is stopped when it hits either the terrain or an object that does not allow you to stand on top of it. It still goes trough every object, even if it should not stop the raindrop and if it does hit an object along the way, it detects what kind of object it is, and figures out whether it should prevent you from moving to this tile. If this object prevents you from moving, it turns the drop red so it can mark the tile as red when it finally hits the terrain, or an item that you can stand on. This property is then copied over to the entire tile.
 +
<br/><br/>Take this little scene as an example
 +
<br/>[[File:AiObjects1.PNG|800px]][[File:AiObjects2.PNG|800px]]<br/>
 +
You can see that some objects are treated differently by the AIGridGenerator. The plant does not raise the AI grid, and it does not make the cell red. The barricade makes the cells red, but it does not raise the AI grid up to its level. The platform does not make the cells red, but it does raise the AI grid so you can walk on top of it. Who decides these magical properties? Well, the generator looks at the properties of the template<br/>
 +
<br/>[[File:AiObjects3.PNG]]<br/>
 +
There are two properties that are imprtant for the AI grid: <b>WalkOn</b> and <b>CanWalkThrough</b>
 +
<br/>In this example the plant is set to CanWalkThrough, the platform is set to WalkOn, and the barricade is set to neither.
 +
<br/>
 +
So the raindrops are stopped by the platform, but not by the plant and the barricade, causing them use the height of the terrain for those tiles instead of the height of the objects on top of it. Of these objects, only the barricade prevents you from moving, so when the raindrops go though the physics of the object before it hits the terrain, it turns the drop red, so the tile knows it not be stepped on.
 +
<br/>The tile now has a height and is either marked as <b><font color='red'>WalkBlocked</font></b> or <b><font color='green'>WalkOn</font></b>
 +
==The Angle of the cell==
 +
Since every tile has its own height now, it can now compare its height to the heights of the neighboring tiles. The bigger the height difference, the steeper the angle will be. If the angle is too steep, the cell be forced to a <b><font color='red'>WalkBlocked</font></b> cell. This prevents you from running off a wall of a cliff. Easy enough huh?
 +
 +
==Is the cell Reachable==
 +
To prevent you from teleporting on cliffs where you are not supposed to get, the AI grid generator does a third and final step: it checks if the cell can be reached through other cell. If this is not the case, you are probably not supposed to get there, so it will prevent you from ever getting there by turning all those tiles <b><font color='red'>WalkBlocked</font></b>. I can hear you think "But what about all those isolated places where I am ablea to get to?" Well, this is where the [[Triggers#Ai_Seeder|AI Seeder triggers]] come in.<br/>
 +
[[File:AiTower.PNG]][[File:AiTowerTrigger.PNG]]
 +
<br/>
 +
As you can see in the image above, the top of the tower is not connected with any other walkable tile, yet you can still stand there. This is because someone added an [[Triggers#Ai_Seeder|AI Seeder triggers]] on top of the platform. AI Seeders are trigger from where the AI grid generator starts deciding whether a tile is reachable. So for every isolated patch of AI where you should still be able to get one way or another, you need to place an AI seeder.<br/>(sidenote: [[Triggers#Start_Point|Start Point triggers]] also function as AI seeders so the place where they player starts is always walkable)<br/>
 +
 +
=Workarounds=
 +
There is also a downside of this rule: "What if non of these conditions fails, but I still don't want the players to walk somewhere?" Well, all you have to do is invalidate one of the rules.<br/>
 +
The easiest solution would be to decorate the place with walk blocked objects, or force some some tiles to red by [[AI_grid_panel#The_Paint_Movement_Tab|painting them]]. But want if you want to make entire sections walkblocked, like ocean floors? All you have to do is make sure the tiles are no longer connected to other walkable tiles.<br/>
 +
[[File:AiWorld2generated.PNG]]<br/>
 +
As you can see here, the AI grid generator generated the sections behind the rock and in the water as walkable as well. We don't want players to walk in the ocean. Our solution for this is a stroke of 1 tile of unwalkable cells that we paint using the walk block painter.<br/>
 +
[[File:AiWorld2painted.PNG]]<br/>
 +
If we generate the grid again after drawing these tiles, the result will look like this:
 +
[[File:AiWorld2Ai.PNG]]<br/>
 +
We have now effectively made the entire ocean non walkable.

Revision as of 14:53, 12 September 2017

Introduction

The world of Rivellon is a beautiful place. It has trees, rocks, beaches, swamps, and all kind of nasty places where you don't want to find yourself. Obviously, this world also has some physical rules all objects and creates have to follow. Just like humans, dwarves, lizards and elves can't walk trough objects in the real world, they are also not able to do this in the magical world of Rivellon. But exactly what mystical force is stopping them from wandering though solid objects? That is where the AI grid comes in!

AI Grid

The AI grid is a 2D representation of the world, where every patch of 0.5m² gets a property that either allows creates to stand on it, or prevents them from doing so. To view the AI grid, open up the AiGrid panel by pressing the AI button on the top bar

AIGridMenuIcon.png

take a look at for more information about how to use this panel

Take the Gheto of dort joy as an example:
AiWorld1.PNG
If you take a look the AI grid, it will look somewhat like this
AiWorldAI.PNG
You can make a distinction between 2 main colors: Red and Green. You can think of the AI grid as a board game. You are allowed to move from a green square to a neighboring green square, but you are not allowed to stand on red squares. As you can see in the example, you can walk on the ground and over the gratings (green cells), but you can not stand inside of a wall, or walk through the broken pillar or through the cages (red cells). It also prevents you from walking off steep slopes (as you can see in the bottom left and top left corner of the picture).

AI Grid Generator - Behind the scenes

The AI Grid generator is an optimistic peace of machinery. In the beginning, the world is flat and you are allowed to stand on every tile. It then tries to figure out the height of every cell, and looks for any reason why you should not be standing there. It decides this by looking at 3 things:

  1. The objects on top of the cell
  2. The angle of the cell
  3. Is the cell reachable

The objects on top of the cell

What the AIGridGenerator does is do a raycast on every tile of this grid to calculate two things: The height of the cell and whether you can walk on it or not. Think of it like a rain cloud that drops drops of rain on each and every tile. It is stopped when it hits either the terrain or an object that does not allow you to stand on top of it. It still goes trough every object, even if it should not stop the raindrop and if it does hit an object along the way, it detects what kind of object it is, and figures out whether it should prevent you from moving to this tile. If this object prevents you from moving, it turns the drop red so it can mark the tile as red when it finally hits the terrain, or an item that you can stand on. This property is then copied over to the entire tile.

Take this little scene as an example
AiObjects1.PNGAiObjects2.PNG
You can see that some objects are treated differently by the AIGridGenerator. The plant does not raise the AI grid, and it does not make the cell red. The barricade makes the cells red, but it does not raise the AI grid up to its level. The platform does not make the cells red, but it does raise the AI grid so you can walk on top of it. Who decides these magical properties? Well, the generator looks at the properties of the template

AiObjects3.PNG
There are two properties that are imprtant for the AI grid: WalkOn and CanWalkThrough
In this example the plant is set to CanWalkThrough, the platform is set to WalkOn, and the barricade is set to neither.
So the raindrops are stopped by the platform, but not by the plant and the barricade, causing them use the height of the terrain for those tiles instead of the height of the objects on top of it. Of these objects, only the barricade prevents you from moving, so when the raindrops go though the physics of the object before it hits the terrain, it turns the drop red, so the tile knows it not be stepped on.
The tile now has a height and is either marked as WalkBlocked or WalkOn

The Angle of the cell

Since every tile has its own height now, it can now compare its height to the heights of the neighboring tiles. The bigger the height difference, the steeper the angle will be. If the angle is too steep, the cell be forced to a WalkBlocked cell. This prevents you from running off a wall of a cliff. Easy enough huh?

Is the cell Reachable

To prevent you from teleporting on cliffs where you are not supposed to get, the AI grid generator does a third and final step: it checks if the cell can be reached through other cell. If this is not the case, you are probably not supposed to get there, so it will prevent you from ever getting there by turning all those tiles WalkBlocked. I can hear you think "But what about all those isolated places where I am ablea to get to?" Well, this is where the AI Seeder triggers come in.
AiTower.PNGAiTowerTrigger.PNG
As you can see in the image above, the top of the tower is not connected with any other walkable tile, yet you can still stand there. This is because someone added an AI Seeder triggers on top of the platform. AI Seeders are trigger from where the AI grid generator starts deciding whether a tile is reachable. So for every isolated patch of AI where you should still be able to get one way or another, you need to place an AI seeder.
(sidenote: Start Point triggers also function as AI seeders so the place where they player starts is always walkable)

Workarounds

There is also a downside of this rule: "What if non of these conditions fails, but I still don't want the players to walk somewhere?" Well, all you have to do is invalidate one of the rules.
The easiest solution would be to decorate the place with walk blocked objects, or force some some tiles to red by painting them. But want if you want to make entire sections walkblocked, like ocean floors? All you have to do is make sure the tiles are no longer connected to other walkable tiles.
AiWorld2generated.PNG
As you can see here, the AI grid generator generated the sections behind the rock and in the water as walkable as well. We don't want players to walk in the ocean. Our solution for this is a stroke of 1 tile of unwalkable cells that we paint using the walk block painter.
AiWorld2painted.PNG
If we generate the grid again after drawing these tiles, the result will look like this: AiWorld2Ai.PNG
We have now effectively made the entire ocean non walkable.