Walking man: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
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("[ | <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 | ||
==NPC side== | ==NPC side== | ||
2. Create npcscripts/walking_man.nut | 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){ | 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"+ | RFC(F("compilestring")("WalkTimer"+GetMyID()+"<-NewTimer(\"SetWalkingAnimation\",100,0,"+GetMyID()+")"))(); | ||
} | } | ||
} | } | ||
function onDestinationReached() | function onDestinationReached() | ||
{ | { | ||
if( GetDistanceFromMeToPoint( | if( GetDistanceFromMeToPoint( WALK_START ) < 2 ) | ||
WalkToPoint( | WalkToPoint( WALK_END ); | ||
else if( GetDistanceFromMeToPoint( | else if( GetDistanceFromMeToPoint( WALK_END ) < 2 ) | ||
{ | { | ||
WalkToPoint( | 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() | ||
{ | { | ||
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(" | 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> | |||
Revision as of 16:28, 13 May 2024
This script uses SetAnim every 100 ms.
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(); }