Walking npc

Started by habi2, Oct 29, 2024, 01:28 PM

Previous topic - Next topic

habi2

npcscripts/test.nut

MiamiScale_Init("default.map")
t<-GetTickCount();

lastpos<-Vector(0,0,0);

function Walk()
{
local lastpos=GetMyPos();
local newpos=move2(lastpos,GetMyFacingAngle(), 0.22,0)  //pos,pangle,dis,angle)
local z=FindZFor2DCoord(newpos.x, newpos.y);
if(!z)
{
Stop();
return;
}
else newpos.z=z+ 0.5; //Update z co-ordinate so that npc do not float in air/fall down.

//Check wall, etc.
if(newpos.z-lastpos.z > 0.5)
{
//cannot move forward
Stop();
SendChat("I can't go further");
print("the jump is "+(newpos.z-lastpos.z)+" to "+newpos+" from "+lastpos+"\n");
return
}
SetLocalValue(F_POSX,newpos.x);
SetLocalValue(F_POSY,newpos.y);
SetLocalValue(F_POSZ,newpos.z);
SetLocalValue(I_KEYS, KEY_ONFOOT_FORWARD)
SetLocalValue(V_ONFOOT_SPEED, (newpos-lastpos).Normalised()*0.12688)
SendOnFootSyncDataLV()
}
function Stop()
{
if(timer!=null)
KillTimer(timer)
//Send stationary-npc packet (stop walking)
SetLocalValue(I_KEYS, 0)
SetLocalValue(V_ONFOOT_SPEED, Vector(0,0,0))
SendOnFootSyncDataLV()
}
timer<-null
function MainFunc()
{
if(timer!=null)
KillTimer(timer)
timer<-SetTimerEx("Walk", 50, 0);
}

function OnPlayerText(pid,text)
{
if(text=="run"||text=="walk")MainFunc();
else if(text=="stop")Stop()
else if(text=="come"){Stop();SetMyPos(GetPlayerPos(pid));}
else if(text=="turn around"||text=="ta")SetMyFacingAngle(GetMyFacingAngle()+PI)
else if(text=="update")dofile("npcscripts/test.nut");
}

//Functions used to get the values...
/*
function OnPlayerUpdate(pid, utype)
{
if(utype==PLAYERUPDATE_NORMAL  )
{
local speed=GetPlayerSpeed(pid).Length();
if(speed > 0)
{
local now=GetTickCount();
//print(now-t+" "+speed+"\n");

local pos=GetPlayerPos(pid);
//print(now-t+" "+(pos-lastpos).Length()+"\n");
local s=50*(pos-lastpos).Length()/(now-t);
//print(s+"\n");
t=now;
lastpos=pos;
}
}
}*/
//speed=0.12688, 50 ms, s=0.334, dis 0.22

Command-line
//Assuming current working directory is that of server32.exe as well as npcclient_0471.exe
//Connect to 127.0.0.1 8192
npcclient_0471.exe -n habi0 -c -q z-finder
dofile("npcscripts/test.nut")
Note that we have loaded the npc additional plugin z-finder located in npcscripts/plugins.

Type come in chat and npc teleport to you. Then use walk and it will run straight. stop to stop it. Type ta or turn around and npc change angle by 180 degree.



PSL

#1
Thanks for this tutorial, very useful.
This script allows the NPC to move in a face-facing direction.

PSL

I made a script for NPCS walking freely last December, but it didn't work very well, it used random points and random angles, it was like walking around, it didn't work very well at all.

[TDA]Speed

hey habi can you make a one with path finding or ai something like that?

habi2

Hi [TDA]Speed, we can definitely think about making a path-finding npc. In fact from an IPL file, the path can be found. Ah yes, when given command to go to a location, it will walk through the road making left turn or right turn. it is possible now.

vitovc

it is possible but he wants you to code pathfinding for him xD
actually there is alot of AI-stuff can be done (and needed for DM-gamemods). I feel fear when I imagine how much work should be done to make npc to act like real players in terms of DM.
...::: vice city :::...
Useful things for vcmp: Relative position and finding an angle (in 3d), 3d line (like laser)