From 50208c76c828cdb851e8dab7611cbdf5935f6c04 Mon Sep 17 00:00:00 2001 From: Vortrex <3858226+VortrexFTW@users.noreply.github.com> Date: Thu, 7 Apr 2022 04:53:13 -0500 Subject: [PATCH] Add NPC anim command --- scripts/server/command.js | 4 +-- scripts/server/npc.js | 62 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/scripts/server/command.js b/scripts/server/command.js index aa982f0a..929aa37b 100644 --- a/scripts/server/command.js +++ b/scripts/server/command.js @@ -388,8 +388,8 @@ function loadCommands() { npc: [ new CommandData("addnpc", createNPCCommand, "", getStaffFlagValue("ManageNPCs"), true, false, "Creates an NPC with the specified skin"), new CommandData("delnpc", deleteNPCCommand, "", getStaffFlagValue("ManageNPCs"), true, false, "Deletes the nearest NPC"), - //new CommandData("npcinfo", getNPCInfoCommand, "", getStaffFlagValue("ManageNPCs"), true, false, "Shows info about the nearest NPC"), - //new CommandData("npcanim", npcPlayAnimationCommand, "", getStaffFlagValue("ManageNPCs"), true, false, "Plays the specified animation on the nearest NPC"), + new CommandData("npcinfo", getNPCInfoCommand, "", getStaffFlagValue("ManageNPCs"), true, false, "Shows info about the nearest NPC"), + new CommandData("npcanim", setNPCAnimationCommand, "", getStaffFlagValue("ManageNPCs"), true, false, "Plays the specified animation on the nearest NPC"), //new CommandData("npcrespawnall", respawnAllNPCsCommand, "", getStaffFlagValue("ManageNPCs"), true, false, "Respawns all NPCs"), //new CommandData("npcrespawn", respawnNPCCommand, "", getStaffFlagValue("ManageNPCs"), true, false, "Respawns the nearest NPC"), ], diff --git a/scripts/server/npc.js b/scripts/server/npc.js index 20050614..8f34166a 100644 --- a/scripts/server/npc.js +++ b/scripts/server/npc.js @@ -277,6 +277,7 @@ function spawnNPC(npcIndex) { let civilian = createGameCivilian(getNPCData(npcIndex).model, getNPCData(npcIndex).spawnPosition, getNPCData(npcIndex).spawnRotation); if(civilian) { civilian.setData("vrr.dataIndex", npcIndex); + civilian.setData("vrr.animation", getNPCData(npcIndex).animationName); getNPCData(npcIndex).ped = civilian; } } @@ -297,7 +298,7 @@ function deleteNPCCommand(command, params, client) { return false; } - let closestNPC = getClosestNPC(client); + let closestNPC = getClosestNPC(getPlayerPosition(client)); if(!getNPCData(closestNPC)) { messagePlayerError(client, getLocaleString(client, "InvalidNPC")); @@ -307,9 +308,11 @@ function deleteNPCCommand(command, params, client) { let npcName = getNPCData(closestNPC).name; deleteNPC(closestNPC); - messageAdmins(`${client.name} deleted NPC ${npcName}`) + messageAdmins(`${client.name}{MAINCOLOUR} deleted NPC {npcPink}${npcName}`) } +// =========================================================================== + function deleteNPC(npcId) { quickDatabaseQuery(`DELETE FROM npc_main WHERE npc_id=${getNPCData(npcId).databaseId}`); @@ -317,8 +320,59 @@ function deleteNPC(npcId) { if(getNPCData(npcId).ped != false) { deleteEntity(getNPCData(npcId).ped); } - delete getServerData().npcs[npcId]; + getServerData().npcs.splice(npcId, 1); } setAllNPCDataIndexes(); -} \ No newline at end of file +} + +// =========================================================================== + +function setNPCAnimationCommand(command, params, client) { + if(areParamsEmpty(params)) { + messagePlayerSyntax(client, command); + return false; + } + + let closestNPC = getClosestNPC(getPlayerPosition(client)); + let animationId = getAnimationFromParams(getParam(params, " ", 1)); + let animationPositionOffset = 1; + + if(!getNPCData(closestNPC)) { + messagePlayerError(client, getLocaleString(client, "InvalidNPC")); + return false; + } + + if(!getAnimationData(animationId)) { + messagePlayerError(client, getLocaleString(client, "InvalidAnimation")); + return false; + } + + if(areThereEnoughParams(params, 2, " ")) { + if(toInteger(animationPositionOffset) < 0 || toInteger(animationPositionOffset) > 3) { + messagePlayerError(client, getLocaleString(client, "InvalidAnimationDistance")); + return false; + } + animationPositionOffset = getParam(params, " ", 2); + } + + getNPCData(closestNPC).animationName = animation; + makePedPlayAnimation(getNPCData(closestNPC).ped, animationId, animationPositionOffset); +} + + +// =========================================================================== + +function getClosestHospital(position) { + let npcs = getServerData().npcs; + let closest = 0; + for(let i in npcs) { + if(getDistance(npcs[i].ped.position, position) < getDistance(npcs[i].ped.position, position)) { + closest = i; + } + } + + return closest; +} + +// =========================================================================== \ No newline at end of file