[Tutorial:NPC] #3 Recording vehicle drivings

Started by habi, Apr 09, 2022, 05:27 AM

Previous topic - Next topic

habi

Yes, you can record driving a car, bike, boat or helicopter. The last two i have not tested but working is same. Vehicle recordings were created with highest difficulty, because apart from their health, damage etc which are easily to figure out, i got stuck on the rotations, speed, ID etc. However all these were figured out in due time and it is one of the highly developed area in this npc application. Imagine the movements of the turret on the top of a firetruck or rhino being recorded/manipulated.

Now a few things. Vehicle recordings are currently independent on the ID or  model ID of the vehicle. This means you sit in one car, accelerate, drift, brake (or a stunt jump) and the actions can be repeated by npc in a different car/model.

You must be in a vehicle when the recording start. The recording will not continue if you exit vehicle. So use /stoprecord when you are done and before exiting vehicle.

Like you did in Tutorial#2, make a recording of a vehicle. I will once again repeat the steps. The main.nut is same as last tutorial.
1. Get in a vehicle and type /recordme car123
2. If message appears, then recording started. Now drive through a path. When you are finished, type /stoprecord.
3. You may exit the vehicle now.

Now, go to your server directory and open recordings and you will see a file car123.rec.
Move this file to npcscripts/recordings.
Now open a notepad file and save below file to npcscripts/drivecar.nut

//------------------------------------------

function NextPlayback()
{
StartRecordingPlayback(PLAYER_RECORDING_TYPE_DRIVER,"car123");
}

//------------------------------------------

function OnNPCEnterVehicle(vehicleid, seatid)
{
    NextPlayback();
}

//------------------------------------------

function OnNPCExitVehicle()
{
    StopRecordingPlayback();
}

//------------------------------------------


Now edit your gamemode script main.nut as follows.
function onScriptLoad ()
{
ConnectNPC ("driver", "drivecar.nut");
}
function onPlayerSpawn(player)
{
if(IsPlayerNPC(player.ID) )
{
local vehicle=FindVehicle(114);
if(vehicle) player.Vehicle=vehicle;
}
}
Two things must be noted.
1. NPC must be put in a vehicle to start the vehicle recording. npc cannot access a vehicle by itself.
2. Once the npc is given a vehicle via player.Vehicle=vehicle, the npcscripts take care of the rest. The latter have an event OnNPCEnterVehicle which will be triggered and the playback is started. You may need to do some tests to understand it.
Also we have given the npc a random vehicle, a vehicle with id 114 in this case. You can change / must change 114 to that vehicle's ID in your server which you want the npc to drive.

Connect to your server and check if someone is driving around.

See also: taxi_SI_test.nut, an npcscript which have a taxi driver driving around a path.

A small addition
If you are a c plus plus programmer, i invite you to look at the following piece of code i write in relation to vehicle data.
//NPCClient.cpp
void DecodeIDandKeys(uint8_t nibble, uint8_t byte, uint16_t wKeys, INCAR_SYNC_DATA* m_pIcSyncData)
{
m_pIcSyncData->dwKeys = wKeys;
uint16_t wVehId = 0;
wVehId |= nibble >> 2;
wVehId |= ( (nibble & 3) * 256 );
if (byte & 0x40)
{
m_pIcSyncData->dwKeys |= 65536;
byte = byte ^ 0x40;
}
wVehId |= ( byte << 2 );
m_pIcSyncData->VehicleID = wVehId;
}

Mikimon