open.mp forum
[Pawn] Pathfinding with ColAndreas and FCNPC - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
--- Thread: [Pawn] Pathfinding with ColAndreas and FCNPC (/showthread.php?tid=961)



Pathfinding with ColAndreas and FCNPC - [SF]kAn3 - 2019-12-23

I'm trying both to make npcs move around randomly and follow a player, yet there are 2 main problems.



1 - While randomly moving, npcs sometimes fall a bit under the ground surface, and then they start going up and down the surface real fast, making severe eye damage if you look at them

2 - When following someone, they will climb any object that is on their way, regardless of its height. For example, if there's a tree between me and the npc, he won't avoid the tree so you will see it going from the ground to the top of the tree while walking. Same thing happens for any other object.



Randomly move around:



PHP Code:
GoRandom(npcid){

?? ?if(!
FCNPC_IsValid(npcid)){

?? ??? ?
printf("[DEBUG] Invalid NPC: %d"npcid);

?? ??? ?return;

?? ?}

??? if(
FCNPC_IsMoving(npcid)){

?? ??? ?return;

?? ?}

?? ?if(
FCNPC_IsDead(npcid)){

?? ??? ?return;

?? ?}

? ? if(
FCNPC_IsAttacking(npcid)){

? ? ?? return;

? ? }

??? new 
FloatstartXFloatstartYFloatstartZFloatnxFloatnyFloatnzFloatrxFloatryFloatrzchance random(2), boolpositivecoord;

?? ?
FCNPC_GetPosition(npcidstartXstartYstartZ);

?? ?
positivecoord = (chance == 0) ? true false;

?? ?
startX = (positivecoord == true) ? startX  random(20)  15 startX random(20)  15;

?? ?
startY = (positivecoord == true) ? startY  random(20)  15 startY random(20)  15;

?? ?
FCNPC_GetPosition(npcidnxnynz);

?? ?
CA_FindZ_For2DCoord(startXstartYstartZ);

?? ?
CA_FindZ_For2DCoord(nxnynz);



?? ?if(
CA_RayCastLine(startXstartYstartZnxnynzrxryrz) == 0){

?? ??? ?
FCNPC_GoTo(npcidstartXstartYstartZFCNPC_MOVE_TYPE_WALKFCNPC_MOVE_SPEED_AUTOtrue);

?? ??? ?
FCNPC_ApplyAnimation(npcid"PED""WALK_DRUNK"4.111100);

?? ?}



?? ?return;





Follow the player:



PHP Code:
if(IsPlayerInRangeOfPoint(npcid,45.0,xa,ya,za) && CA_RayCastLine(xayazanxnynzrxryrz) == 0){ // if is near player and there's no object between them

??? FCNPC_GoTo(npcid,xa,ya,za,MOVE_TYPE_RUN,0.4true);

? ? return 
1;





Am I doing it wrong, there already is a pathfinding method in FCNPC or CA or anything similar? Also I found Dijkstra (https://pastebin.com/RwQZ1PWy) is it obsolete?

Any help will be appreciated.