Walking man

From NPC for VCMP 0.4 Servers
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.

Introduction

This is a npcscript which remotely execute server functions. With the help of npc04 plugin, it dynamically creates as well as delete functions in the roottable of server. One of the function it creates on server is a timer which calls player.SetAnim(0,0) where player is the npc, at regular intervals. At the same time it updates position from npcscript. This creates the effect that the npc is walking. The npc04 plugin currently allow function calls through clientscriptdata only if the client is admin. Hence it is required to make the walking npc an admin (player.Admin=true) in the server.

function onPlayerJoin( player )
{
	if(player.IsNPC && player.IP=="127.0.0.1") //automatically make npc admin to execute commands. ip check is important.
		player.IsAdmin=true;
}

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

Map File

2. You need to place default.map in server directory. This map-file was previously referred as vicecity.map in this wiki

NPC side

3. Create npcscripts/walking_man.nut The following script expects "default.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("default.map"))
	{
		SendChat("My map is not loaded. Bye");
		QuitServer();
	}
}
function OnNPCScriptUnload()
{
	MiamiScale_Unload();
}

Download

You can download walking-man complete server here (mediafire ~10 MB)