Walking man: Difference between revisions

From NPC for VCMP 0.4 Servers
Jump to navigation Jump to search
(Created page with "<poem style="border: 2px solid #d6d2c5; background-color: #f9f4e6; padding: 1em;"> This script uses SetAnim every 100 ms. </poem> <poem style="border: 2px solid #d6d2c5; background-color: #f9f4e6; padding: 1em;"> This script requires modules: LibRPC and z-finder. </poem> <poem style="border: 2px solid #d6d2c5; background-color: #f9f4e6; padding: 1em;"> This script is made as of npcclient version 1.6 beta 4 patch 1. </poem> ==Server Side== 1. In scripts/main.nut...")
 
No edit summary
 
(One intermediate revision by one other user not shown)
Line 10: Line 10:
==Server Side==
==Server Side==
1. In scripts/main.nut, connect our npc using  
1. In scripts/main.nut, connect our npc using  
<source >ConnectNPC("[NPC]apj33", "walking_man.nut", false, "", "rpclib z-finder")</source>
<source >ConnectNPC("[npc]captain", "walking_man.nut", false, "", "rpclib z-finder")</source>
<poem>
<poem>
false - console off
false - console off, put true - console on
"" - host IP, default to 127.0.0.1
"" - host IP, default to 127.0.0.1
rpclib - npc module (to be placed in npcscripts/plugins) </poem>
rpclib - npc module (to be placed in npcscripts/plugins) </poem>
z-finder - another module to get z co-ordinates
z-finder - another module to get z co-ordinates
==Other side==
==NPC side==
2. Create npcscripts/walking_man.nut and
2. Create npcscripts/walking_man.nut
The following script expects "vicecity.map" mapfile on server directory.
<source>
<source>
target<-null;timerid<-null;
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)
function WalkToPoint(point)
{
{
Line 27: Line 30:
timerid=SetTimerEx("step_forward",100,0);
timerid=SetTimerEx("step_forward",100,0);
         //Remotely calling functions 'compilestring' of server.
         //Remotely calling functions 'compilestring' of server.
RFC(F("compilestring")("function SetWalkingAnimation(id){if(FindPlayer(id))FindPlayer(id).SetAnim(0,0);}"))();
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"+GetMyName()+"<-NewTimer(\"SetWalkingAnimation\",100,0,"+GetMyID()+")"))();
RFC(F("compilestring")("WalkTimer"+GetMyID()+"<-NewTimer(\"SetWalkingAnimation\",100,0,"+GetMyID()+")"))();
}
}
}
}
function onDestinationReached()
function onDestinationReached()
{
{
if( GetDistanceFromMeToPoint( Vector(-872.146, -431.502, 11.5288) ) < 2 )
if( GetDistanceFromMeToPoint( WALK_START ) < 2 )
WalkToPoint( Vector( -870.825, -296.79, 11.173) );
WalkToPoint( WALK_END );
else if( GetDistanceFromMeToPoint( Vector(-870.825, -296.79, 11.173) ) < 2 )
else if( GetDistanceFromMeToPoint( WALK_END ) < 2 )
{
{
WalkToPoint( Vector( -872.146, -431.502, 11.5288) );
WalkToPoint( WALK_START );
         //print something on server console.
         //print something on server console.
RFC("print")("npc: i completed one round of patrol");
RFC("print")("npc: i completed one round of patrol");
Line 62: Line 65:
function OnNPCSpawn()  
function OnNPCSpawn()  
{  
{  
WalkToPoint( Vector(-872.146, -431.502, 11.5288) );
SetTimerEx("SetMyPos",700, 1, WALK_START );
//Acquire weapon
// 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()+").SetWeapon(4,1);"))();
RFC(F("compilestring")("FindPlayer("+GetMyID()+").Skin=1"))();
SetTimerEx("SetLocalValue", 1000,1, I_CURWEP, 4);
SetTimerEx("SetLocalValue", 1000,1, I_CURWEP, 4);
}
}
function OnNPCScriptLoad(params)
function OnNPCScriptLoad(params)
{
{
if(!MiamiScale_Init("default.map"))
if(!MiamiScale_Init("vicecity.map"))
{
{
SendChat("My map is not loaded. Bye");
SendChat("My map is not loaded. Bye");
Line 78: Line 86:
{
{
MiamiScale_Unload();
MiamiScale_Unload();
}
}</source>
</source>
Notes: The NPC

Latest revision as of 16:28, 13 May 2024

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();
}