Walking man

From NPC for VCMP 0.4 Servers
Revision as of 16:28, 13 May 2024 by 127.0.0.1 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This script uses SetAnim every 100 ms.

This script requires modules: LibRPC and z-finder.

This script is made as of npcclient version 1.6 beta 4 patch 1.

Server Side

1. In scripts/main.nut, connect our npc using

ConnectNPC("[npc]captain", "walking_man.nut", false, "", "rpclib z-finder")

false - console off, put true - console on
"" - host IP, default to 127.0.0.1
rpclib - npc module (to be placed in npcscripts/plugins)

z-finder - another module to get z co-ordinates

NPC side

2. Create npcscripts/walking_man.nut The following script expects "vicecity.map" mapfile on server directory.

target<-null;timerid<-null;
WALK_START <- Vector(-872.146, -431.502, 11.5288);
WALK_END <- Vector( -870.825, -296.79, 11.173);
function WalkToPoint(point)
{
	target=point;
	if(timerid==null)
	{
		timerid=SetTimerEx("step_forward",100,0);
        //Remotely calling functions 'compilestring' of server.
		RFC(F("compilestring")("function SetWalkingAnimation(id){ local p=FindPlayer(id); if(p && IsPlayerNPC(id))p.SetAnim(0,0);else WalkTimer"+GetMyID()+".Delete()}"))();
		RFC(F("compilestring")("WalkTimer"+GetMyID()+"<-NewTimer(\"SetWalkingAnimation\",100,0,"+GetMyID()+")"))();
	}
}
function onDestinationReached()
{
	if( GetDistanceFromMeToPoint( WALK_START ) < 2 )
		WalkToPoint( WALK_END );
	else if( GetDistanceFromMeToPoint( WALK_END ) < 2 )
	{
		WalkToPoint( WALK_START );
        //print something on server console.
		RFC("print")("npc: i completed one round of patrol");
	}
}
function step_forward()
{
	local newpos=GetMyPos()+(target-GetMyPos()).Normalised()*0.14;
	newpos.z=FindZFor2DCoord(newpos.x, newpos.y)+1.0425;
	SetLocalValue(V_POS, newpos);
	SetLocalValue(F_ANGLE, atan2(-(target.x-GetMyPosX()),target.y-GetMyPosY()));
	if((target-GetMyPos()).Length()<0.8)//reached destination
	{
		if(timerid!=null)
			KillTimer(timerid);
		timerid=null;
		//stop timer of server
		RFC(F("compilestring")("WalkTimer"+GetMyName()+".Delete()"))();
	}
	else SendOnFootSyncDataLV();
	if(timerid==null)
		onDestinationReached();
}
function OnNPCSpawn() 
{ 
	SetTimerEx("SetMyPos",700, 1, WALK_START );
//	SetMyPos(WALK_START);
	print(GetMyPos());
	SetTimerEx("WalkToPoint", 1000, 1, WALK_END );
	//WalkToPoint( WALK_END );
	//Acquire weapon and skin
	RFC(F("compilestring")("FindPlayer("+GetMyID()+").SetWeapon(4,1);"))();
	RFC(F("compilestring")("FindPlayer("+GetMyID()+").Skin=1"))();
	SetTimerEx("SetLocalValue", 1000,1, I_CURWEP, 4);
}
function OnNPCScriptLoad(params)
{
	if(!MiamiScale_Init("vicecity.map"))
	{
		SendChat("My map is not loaded. Bye");
		QuitServer();
	}
}
function OnNPCScriptUnload()
{
	MiamiScale_Unload();
}