Quote:
Originally Posted by thezolotoi
Hello, how can I get my npc to use this include for moving? Tried to understand the example, but could not.
Checking the forum, I found a function so that the entity could jump over small obstacles, but there was still a question about avoiding obstacles.
|
There are design choices you can make along the way that will make the code more or less efficient.
The most important one is when is the pathing "out of date" and need to be updated?
Another one might be, how is it going to know when to switch point to the next one. You can do it by touching an entity which you keep moving to the next point. Another way is by simply calculating if the distance to the next point is close enough.
But for a simple example:
Create your path between two points, for example an entity chasing a player:
Code:
new Float:fStart[3], Float:fEnd[3];
pev(myent, pev_origin, fStart);
pev(id, pev_origin, fEnd);
gMyPathIndex = AStarThreaded(fStart, fEnd, "PathDone");
And when the path is done:
Code:
new Array:gPath;
public PathDone(Index, Array:hPath, Float:Distance, NodesAdded, NodesValidated, NodesCleared)
{
if ( hPath == Invalid_Array )
{
client_print(0, print_chat, "Pathfinding failed.");
return;
}
gPath = hPath;
}
If you have several pathfindings you might want to check if gMyPathIndex equals Index.
If there is a path, make your ent follow each step of it.
Code:
public myentThink(ent)
{
if ( gPath != Invalid_Array )
{
if ( ArraySize(gPath) )
{
new Float:vOrigin[3];
entity_get_vector(ent, EV_VEC_origin, vOrigin);
new iNextPoint[3], Float:fNextPoint[3];
ArrayGetArray(gPath, 0, iNextPoint); // Get the next point in the path.
IVecFVec(iNextPoint, fNextPoint); // Convert it to a float for distance calculation.
if ( get_distance_f(fNextPoint, vOrigin) >= 5 ) // Check if the entity is close enough to the point it's moving towards for it to get a new point to move towards.
ArrayDeleteItem(gPath, 0); // The new point won't be the target until next think but it shouldn't really matter. Otherwise you have to implement the same code twice for several steps.
// Do movement towards iNextPoint/fNextPoint.
}
else
ArrayDestroy(gPath); // The end was reached, destroy the array.
}
// Update nextthink...
}
When it comes to ent parameters or how to actually move it in the correct way I'm of no help.
__________________