Rnpc

From SA-MP Wiki

Jump to: navigation, search

This page explains the functions provided by the RNPC plugin by User:Mauzen RNPC (Recordfree NPCs) is a server plugin. Its purpose is to create the NPC record files in runtime, so they dont need to be recorded manually. This way the NPCs can be controlled dynamically from the script-side. It is based on the knowledge about the Rec-files.

It can be found in this forum thread: http://forum.sa-mp.com/showthread.php?t=355849

Contents

Scripting functions

General

ConnectRNPC

Connects a new RNPC to the server. This, or at least using the RNPC npcscript is needed for any other functions.
Parameters:
(name[])
name[]Name of the NPC.
ReturnsID of the connected NPC


MoveRNPC

Simply moves the NPC to the given position. This works without entering build mode first.
Parameters:
(npcid, Float:x, Float:y, Float:z, Float:speed)
npcidID of the NPC to move.
x,y,zTarget coordinates.
speedMovement speed. For OnFoot-Running 0.006 is a good value.
Returns1 on success, 0 on fail


Record generation

RNPC_CreateBuild

Use this function to enter "build mode".

It creates the record file for the given NPC and so allows the other function to write to it.

It is important to stop the current playback of a NPC before building a new one, or the NPC will crash. To avoid this you can create the build in another slot.
Parameters:
(npcid, type, slot)
npcidThe ID of the NPC who should be controlled.
typeThe recording type, either PLAYER_RECORDING_TYPE_ONFOOT or PLAYER_RECORDING_TYPE_VEHICLE
slotThe recording slot that should be created. Every slot got its own file, so you can keep multiple recordings for a NPC at once, and later choose which one should be played back. Default value is 0
Returns1 on success, 0 if build mode is already active.


RNPC_FinishBuild

Finishes the currently active build and ends the "build mode". This is needed before playing the generated record back.
Parameters:
(clear)
npcidDetermines if a last segment should be added at the end to clear things like velocity and movement, so the NPC just stands still after the playback. Default value is 1
Returns1 on success, 0 if there is no active build.


RNPC_AddMovement

Appends a movement from (x1,y1,z1) to (x2,y2,z2) to the current build. Good to set the NPCs initial position
Parameters:
(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2, Float:speed)
x1,y1,z1The coordinates where the NPC should start to move.
x1,y1,z1The target coordinates.
speedMovement speed. For OnFoot-Running 0.006 is a good value.
Returns1 on success, 0 on fail


RNPC_AddMovementAlt

Appends a movement from (x1,y1,z1) to (x2,y2,z2) to the current build. Also supports previously set acceleration.
Parameters:
(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2, Float:speed)
x1,y1,z1The coordinates where the NPC should start to move.
x1,y1,z1The target coordinates.
speedMovement speed. For OnFoot-Running 0.006 is a good value.
lockLock the vehicle to the ground, if MapAndreas was initalized.
Returns1 on success, 0 on fail


RNPC_ConcatMovement

Appends a movement from the NPCs last position to (x,y,z) to the current build.
Parameters:
(Float:x, Float:y, Float:z, Float:speed)
x,y,zThe target coordinates.
speedMovement speed. For OnFoot-Running 0.006 is a good value.
Returns1 on success, 0 on fail


RNPC_AddPause

Adds a pause to the current build. The NPC wont move, but continue with his current attributes (e.g. keeps shooting).
Parameters:
(Ftime)
timeTime [ms] to pause.
Returns1 on success, 0 on fail


Playback control

RNPC_StartBuildPlayback

Starts playing a preciously generated recording.
Parameters:
(npcid, slot)
npcidThe ID of the NPC who should play his recording.
slotThe slot of the build that should be played back. Default value is 0
vehicleidIf this is set, the NPC will be forced to enter the vehicle twice, to fix issues with vehicles being far away from their recorded position.
ReturnsThis function doesn't return a specific value


RNPC_StartPlayback

Makes the NPC play back any recording file in the npcmodes directory.
Parameters:
(npcid, rec[])
npcidThe ID of the NPC who should play the recording.
slotThe name of the recording that should be played.
ReturnsThis function doesn't return a specific value


RNPC_StopPlayback

Stops any running playback for the given NPC.
Parameters:
(npcid)
npcidThe ID of the NPC who should stop.
ReturnsThis function doesn't return a specific value


RNPC_SetAutorepeat

Sets wether the NPC should restart his playback after finishing it.


Parameters:
(npcid, repeat)
npcidThe ID of the NPC to control
repeat1/0 to set repeating.
ReturnsThis function doesn't return a specific value


Attribute setters

Theres a whole bunch of simple setter functions. They are meant to be used in build mode, and affect all following actions of the NPC until changed again. They should be quite self-explaining, so heres just a plain list of all OnFoot-Setters:

native RNPC_SetLRKeys(lr);
native RNPC_SetUDKeys(ud);
native RNPC_SetKeys(keys);
native RNPC_SetQuat1(Float:w); // Better use SetAngleQuats than this
native RNPC_SetQuat2(Float:x);
native RNPC_SetQuat3(Float:y);
native RNPC_SetQuat4(Float:z);
native RNPC_SetHealth(hp);
native RNPC_SetArmour(arm);
native RNPC_SetSpecialAction(sp);
native RNPC_SetWeaponID(weaponid);
native RNPC_SetAnimID(anim);
native RNPC_SetAnimParams(params); // Not sure how the params work yet
native RNPC_SetAngleQuats(Float:a, Float:h, Float:b); // a, h and b are degree angles, where h is the facing angle
 
native RNPC_SetSirenState(siren);
native RNPC_SetDriverHealth(health);
native RNPC_SetInternalPos(Float:x, Float:y, Float:z); // For debugging
native RNPC_SetAcceleration(Float:acc); // Accleration in speed/second for AddMovementAlt
 
native RNPC_SetUpdateRate(rate); // The interval in ms in which segments are written. Default of 100ms should be fine.
native RNPC_GetBuildLength(); // Returns the lentgh of the currently active, or last finished build in ms

Example code

The "build mode-thingy" might be hard to understand just from explaining the single functions. So heres some example code that should make it more clear.

Walk a bit and rage-shoot

SetPlayerPos(npcid, 0.0, 0.0, 0.0);
RNPC_CreateBuild(npcid, PLAYER_RECORDING_ONFOOT); // start build mode
RNPC_SetWeaponID(32); // Set weapon
RNPC_AddPause(500); // wait a bit
RNPC_AddMovement(0.0, 0.0, 0.0, 20.0, 20.0, 0.0); // start moving
RNPC_ConcatMovement(0.0, 20.0, 0.0);  // Continue walking
RNPC_AddPause(200); // wait a bit again
RNPC_SetKeys(128); // aim straight forward
RNPC_AddPause(500);
RNPC_SetKeys(128 + 4); // Start shooting straight forward
RNPC_AddPause(500);
for (new i = 0; i < 360; i+=20) {
    // Makes the NPC rotate slowly
    RNPC_SetAngleQuats(0.0, i, 0.0);
    RNPC_AddPause(150);
}
RNPC_SetKeys(128); // stop shooting
RNPC_AddPause(500);
RNPC_SetKeys(0); // stop aiming
RNPC_FinishBuild(); // end the build mode and finish the build
 
RNPC_StartBuildPlayback(npcid); // start playback

Simple following script

stock FollowPlayer(npcid, targetid)
{
    SetTimerEx("Follower", 500, 1, "ii", npcid, targetid);
}
 
public Follower(npcid, targetid)
{
    new Float:x, Float:y, Float:z;
    GetPlayerPos(targetid, x, y, z);
    MoveRNPC(npcid, x, y, z, 0.006);
}

Flying drag race

new npc = ConnectRNPC("Racer");
new veh = CreateVehicle(420, 0.0, 0.0, 25.0, 0.0, -1, -1, 0);
PutPlayerInVehicle(npc, veh, 0);
 
RNPC_CreateBuild(npc, PLAYER_RECORDING_TYPE_DRIVER);
RNPC_SetAcceleration(0.003);
RNPC_AddMovementAlt(0.0, 0.0, 25.0, 0.0, 500.0, 25.0, 0.15, false);
RNPC_FinishBuild();
 
RNPC_StartBuildPlayback(npc);
Personal tools