Use INT and VW in closest NPC util
This commit is contained in:
@@ -19,7 +19,7 @@ function initNPCScript() {
|
||||
* @return {NPCData} The NPC's data (class instancee)
|
||||
*/
|
||||
function getNPCData(npcId) {
|
||||
if(typeof getServerData().npcs[npcId] != "undefined") {
|
||||
if (typeof getServerData().npcs[npcId] != "undefined") {
|
||||
return getServerData().npcs[npcId];
|
||||
}
|
||||
return false;
|
||||
@@ -28,14 +28,14 @@ function getNPCData(npcId) {
|
||||
// ===========================================================================
|
||||
|
||||
function createNPCCommand(command, params, client) {
|
||||
if(areParamsEmpty(params)) {
|
||||
if (areParamsEmpty(params)) {
|
||||
messagePlayerSyntax(client, getCommandSyntaxText(command));
|
||||
return false;
|
||||
}
|
||||
|
||||
let skinIndex = getSkinModelIndexFromParams(params);
|
||||
|
||||
if(!skinIndex) {
|
||||
if (!skinIndex) {
|
||||
messagePlayerError(client, getLocaleString(client, "InvalidSkin"));
|
||||
return false;
|
||||
}
|
||||
@@ -53,11 +53,11 @@ function loadNPCsFromDatabase() {
|
||||
let dbConnection = connectToDatabase();
|
||||
let tempNPCs = [];
|
||||
let dbAssoc;
|
||||
if(dbConnection) {
|
||||
if (dbConnection) {
|
||||
let dbQueryString = `SELECT * FROM npc_main WHERE npc_server = ${getServerId()} AND npc_enabled = 1`;
|
||||
let dbQuery = queryDatabase(dbConnection, dbQueryString);
|
||||
if(dbQuery) {
|
||||
while(dbAssoc = fetchQueryAssoc(dbQuery)) {
|
||||
if (dbQuery) {
|
||||
while (dbAssoc = fetchQueryAssoc(dbQuery)) {
|
||||
let tempNPCData = new NPCData(dbAssoc);
|
||||
tempNPCData.triggers = loadNPCTriggersFromDatabase(tempNPCData.databaseId);
|
||||
tempNPCs.push(tempNPCData);
|
||||
@@ -78,11 +78,11 @@ function loadNPCTriggersFromDatabase(npcDatabaseId) {
|
||||
let dbConnection = connectToDatabase();
|
||||
let tempNPCTriggers = [];
|
||||
let dbAssoc;
|
||||
if(dbConnection) {
|
||||
if (dbConnection) {
|
||||
let dbQueryString = `SELECT * FROM npc_trig WHERE npc_trig_npc = ${npcDatabaseId} AND npc_trig_enabled = 1`;
|
||||
let dbQuery = queryDatabase(dbConnection, dbQueryString);
|
||||
if(dbQuery) {
|
||||
while(dbAssoc = fetchQueryAssoc(dbQuery)) {
|
||||
if (dbQuery) {
|
||||
while (dbAssoc = fetchQueryAssoc(dbQuery)) {
|
||||
let tempNPCTriggerData = new NPCTriggerData(dbAssoc);
|
||||
tempNPCTriggerData.conditions = loadNPCTriggerConditionsFromDatabase(tempNPCTriggerData.databaseId);
|
||||
tempNPCTriggerData.responses = loadNPCTriggerResponsesFromDatabase(tempNPCTriggerData.databaseId);
|
||||
@@ -104,11 +104,11 @@ function loadNPCTriggerConditionsFromDatabase(npcTriggerDatabaseId) {
|
||||
let dbConnection = connectToDatabase();
|
||||
let tempNPCTriggerConditions = [];
|
||||
let dbAssoc;
|
||||
if(dbConnection) {
|
||||
if (dbConnection) {
|
||||
let dbQueryString = `SELECT * FROM npc_cond WHERE npc_cond_trig = ${npcTriggerDatabaseId} AND npc_cond_enabled = 1`;
|
||||
let dbQuery = queryDatabase(dbConnection, dbQueryString);
|
||||
if(dbQuery) {
|
||||
while(dbAssoc = fetchQueryAssoc(dbQuery)) {
|
||||
if (dbQuery) {
|
||||
while (dbAssoc = fetchQueryAssoc(dbQuery)) {
|
||||
let tempNPCTriggerConditionData = new NPCTriggerConditionData(dbAssoc);
|
||||
tempNPCTriggerConditions.push(tempNPCTriggerConditionData);
|
||||
}
|
||||
@@ -128,11 +128,11 @@ function loadNPCTriggerResponsesFromDatabase(npcTriggerDatabaseId) {
|
||||
let dbConnection = connectToDatabase();
|
||||
let tempNPCTriggerResponses = [];
|
||||
let dbAssoc;
|
||||
if(dbConnection) {
|
||||
if (dbConnection) {
|
||||
let dbQueryString = `SELECT * FROM npc_resp WHERE npc_resp_trig = ${npcTriggerDatabaseId} AND npc_resp_enabled = 1`;
|
||||
let dbQuery = queryDatabase(dbConnection, dbQueryString);
|
||||
if(dbQuery) {
|
||||
while(dbAssoc = fetchQueryAssoc(dbQuery)) {
|
||||
if (dbQuery) {
|
||||
while (dbAssoc = fetchQueryAssoc(dbQuery)) {
|
||||
let tempNPCTriggerResponseData = new NPCTriggerResponseData(dbAssoc);
|
||||
tempNPCTriggerResponses.push(tempNPCTriggerResponseData);
|
||||
}
|
||||
@@ -148,11 +148,11 @@ function loadNPCTriggerResponsesFromDatabase(npcTriggerDatabaseId) {
|
||||
// ===========================================================================
|
||||
|
||||
function saveAllNPCsToDatabase() {
|
||||
if(getServerConfig().devServer) {
|
||||
if (getServerConfig().devServer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for(let i in getServerData().npcs) {
|
||||
for (let i in getServerData().npcs) {
|
||||
saveNPCToDatabase(i);
|
||||
}
|
||||
}
|
||||
@@ -160,34 +160,34 @@ function saveAllNPCsToDatabase() {
|
||||
// ===========================================================================
|
||||
|
||||
function saveNPCToDatabase(npcDataId) {
|
||||
if(getServerConfig().devServer) {
|
||||
if (getServerConfig().devServer) {
|
||||
logToConsole(LOG_VERBOSE, `[VRR.NPC]: NPC ${npcDataId} can't be saved because server is running as developer only. Aborting save ...`);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(getNPCData(npcDataId) == false) {
|
||||
if (getNPCData(npcDataId) == false) {
|
||||
logToConsole(LOG_VERBOSE, `[VRR.NPC]: NPC ${npcDataId} data is invalid. Aborting save ...`);
|
||||
return false;
|
||||
}
|
||||
|
||||
let tempNPCData = getNPCData(npcDataId);
|
||||
|
||||
if(tempNPCData.databaseId == -1) {
|
||||
if (tempNPCData.databaseId == -1) {
|
||||
logToConsole(LOG_VERBOSE, `[VRR.NPC]: NPC ${npcDataId} is a temp NPC. Aborting save ...`);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!tempNPCData.needsSaved) {
|
||||
if (!tempNPCData.needsSaved) {
|
||||
logToConsole(LOG_VERBOSE, `[VRR.NPC]: NPC ${npcDataId} hasn't changed data. Aborting save ...`);
|
||||
return false;
|
||||
}
|
||||
|
||||
logToConsole(LOG_VERBOSE, `[VRR.NPC]: Saving NPC ${tempNPCData.databaseId} to database ...`);
|
||||
let dbConnection = connectToDatabase();
|
||||
if(dbConnection) {
|
||||
if(tempNPCData.ped != false) {
|
||||
if(!tempNPCData.spawnLocked) {
|
||||
if(areServerElementsSupported()) {
|
||||
if (dbConnection) {
|
||||
if (tempNPCData.ped != false) {
|
||||
if (!tempNPCData.spawnLocked) {
|
||||
if (areServerElementsSupported()) {
|
||||
tempNPCData.position = tempNPCData.ped.position;
|
||||
tempNPCData.heading = tempNPCData.ped.heading;
|
||||
} else {
|
||||
@@ -224,7 +224,7 @@ function saveNPCToDatabase(npcDataId) {
|
||||
];
|
||||
|
||||
let dbQuery = null;
|
||||
if(tempNPCData.databaseId == 0) {
|
||||
if (tempNPCData.databaseId == 0) {
|
||||
let queryString = createDatabaseInsertQuery("npc_main", data);
|
||||
dbQuery = queryDatabase(dbConnection, queryString);
|
||||
tempNPCData.databaseId = getDatabaseInsertId(dbConnection);
|
||||
@@ -247,19 +247,19 @@ function saveNPCToDatabase(npcDataId) {
|
||||
// ===========================================================================
|
||||
|
||||
function setNPCDataIndexes() {
|
||||
for(let i in getServerData().npcs) {
|
||||
for (let i in getServerData().npcs) {
|
||||
getServerData().npcs[i].index = i;
|
||||
|
||||
for(let j in getServerData().npcs[i].triggers) {
|
||||
for (let j in getServerData().npcs[i].triggers) {
|
||||
getServerData().npcs[i].triggers[j].index = j;
|
||||
getServerData().npcs[i].triggers[j].npcIndex = i;
|
||||
|
||||
for(let k in getServerData().npcs[i].triggers[j].conditions) {
|
||||
for (let k in getServerData().npcs[i].triggers[j].conditions) {
|
||||
getServerData().npcs[i].triggers[j].conditions[k].index = k;
|
||||
getServerData().npcs[i].triggers[j].conditions[m].triggerIndex = j;
|
||||
}
|
||||
|
||||
for(let m in getServerData().npcs[i].triggers[j].responses) {
|
||||
for (let m in getServerData().npcs[i].triggers[j].responses) {
|
||||
getServerData().npcs[i].triggers[j].responses[m].index = m;
|
||||
getServerData().npcs[i].triggers[j].responses[m].triggerIndex = j;
|
||||
}
|
||||
@@ -272,12 +272,12 @@ function setNPCDataIndexes() {
|
||||
function spawnNPC(npcIndex) {
|
||||
let npcData = getNPCData(npcIndex);
|
||||
let ped = createGamePed(npcData.skin, npcData.position, npcData.rotation.z);
|
||||
if(ped) {
|
||||
if (ped) {
|
||||
getNPCData(npcIndex).ped = ped;
|
||||
setEntityData(ped, "vrr.dataIndex", npcIndex, false);
|
||||
if(npcData.animationName != "") {
|
||||
if (npcData.animationName != "") {
|
||||
let animationId = getAnimationFromParams(npcData.animationName);
|
||||
if(animationId != false) {
|
||||
if (animationId != false) {
|
||||
setEntityData(ped, "vrr.anim", animationId, true);
|
||||
}
|
||||
}
|
||||
@@ -289,7 +289,7 @@ function spawnNPC(npcIndex) {
|
||||
// ===========================================================================
|
||||
|
||||
function spawnAllNPCs() {
|
||||
for(let i in getServerData().npcs) {
|
||||
for (let i in getServerData().npcs) {
|
||||
spawnNPC(i);
|
||||
}
|
||||
}
|
||||
@@ -297,9 +297,9 @@ function spawnAllNPCs() {
|
||||
// ===========================================================================
|
||||
|
||||
function deleteNPCCommand(command, params, client) {
|
||||
let closestNPC = getClosestNPC(getPlayerPosition(client));
|
||||
let closestNPC = getClosestNPC(getPlayerPosition(client), getPlayerDimension(client), getPlayerInterior(client));
|
||||
|
||||
if(!getNPCData(closestNPC)) {
|
||||
if (!getNPCData(closestNPC)) {
|
||||
messagePlayerError(client, getLocaleString(client, "InvalidNPC"));
|
||||
return false;
|
||||
}
|
||||
@@ -315,8 +315,8 @@ function deleteNPCCommand(command, params, client) {
|
||||
function deleteNPC(npcId) {
|
||||
quickDatabaseQuery(`DELETE FROM npc_main WHERE npc_id=${getNPCData(npcId).databaseId}`);
|
||||
|
||||
if(getNPCData(npcId)) {
|
||||
if(getNPCData(npcId).ped != false) {
|
||||
if (getNPCData(npcId)) {
|
||||
if (getNPCData(npcId).ped != false) {
|
||||
deleteEntity(getNPCData(npcId).ped);
|
||||
}
|
||||
getServerData().npcs.splice(npcId, 1);
|
||||
@@ -328,27 +328,27 @@ function deleteNPC(npcId) {
|
||||
// ===========================================================================
|
||||
|
||||
function setNPCAnimationCommand(command, params, client) {
|
||||
if(areParamsEmpty(params)) {
|
||||
if (areParamsEmpty(params)) {
|
||||
messagePlayerSyntax(client, getCommandSyntaxText(command));
|
||||
return false;
|
||||
}
|
||||
|
||||
let closestNPC = getClosestNPC(getPlayerPosition(client));
|
||||
let closestNPC = getClosestNPC(getPlayerPosition(client), getPlayerDimension(client), getPlayerInterior(client));
|
||||
let animationId = getAnimationFromParams(getParam(params, " ", 1));
|
||||
let animationPositionOffset = 1;
|
||||
|
||||
if(!getNPCData(closestNPC)) {
|
||||
if (!getNPCData(closestNPC)) {
|
||||
messagePlayerError(client, getLocaleString(client, "InvalidNPC"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!getAnimationData(animationId)) {
|
||||
if (!getAnimationData(animationId)) {
|
||||
messagePlayerError(client, getLocaleString(client, "InvalidAnimation"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if(areThereEnoughParams(params, 2, " ")) {
|
||||
if(toInteger(animationPositionOffset) < 0 || toInteger(animationPositionOffset) > 3) {
|
||||
if (areThereEnoughParams(params, 2, " ")) {
|
||||
if (toInteger(animationPositionOffset) < 0 || toInteger(animationPositionOffset) > 3) {
|
||||
messagePlayerError(client, getLocaleString(client, "InvalidAnimationDistance"));
|
||||
return false;
|
||||
}
|
||||
@@ -365,15 +365,15 @@ function setNPCAnimationCommand(command, params, client) {
|
||||
// ===========================================================================
|
||||
|
||||
function setNPCNameCommand(command, params, client) {
|
||||
if(areParamsEmpty(params)) {
|
||||
if (areParamsEmpty(params)) {
|
||||
messagePlayerSyntax(client, getCommandSyntaxText(command));
|
||||
return false;
|
||||
}
|
||||
|
||||
let closestNPC = getClosestNPC(getPlayerPosition(client));
|
||||
let closestNPC = getClosestNPC(getPlayerPosition(client), getPlayerDimension(client), getPlayerInterior(client));
|
||||
let name = params;
|
||||
|
||||
if(!getNPCData(closestNPC)) {
|
||||
if (!getNPCData(closestNPC)) {
|
||||
messagePlayerError(client, getLocaleString(client, "InvalidNPC"));
|
||||
return false;
|
||||
}
|
||||
@@ -389,9 +389,9 @@ function setNPCNameCommand(command, params, client) {
|
||||
// ===========================================================================
|
||||
|
||||
function toggleNPCLookAtClosestPlayerCommand(command, params, client) {
|
||||
let closestNPC = getClosestNPC(getPlayerPosition(client));
|
||||
let closestNPC = getClosestNPC(getPlayerPosition(client), getPlayerDimension(client), getPlayerInterior(client));
|
||||
|
||||
if(!getNPCData(closestNPC)) {
|
||||
if (!getNPCData(closestNPC)) {
|
||||
messagePlayerError(client, getLocaleString(client, "InvalidNPC"));
|
||||
return false;
|
||||
}
|
||||
@@ -406,14 +406,14 @@ function toggleNPCLookAtClosestPlayerCommand(command, params, client) {
|
||||
// ===========================================================================
|
||||
|
||||
function getNPCInfoCommand(command, params, client) {
|
||||
if(areParamsEmpty(params)) {
|
||||
if (areParamsEmpty(params)) {
|
||||
messagePlayerSyntax(client, getCommandSyntaxText(command));
|
||||
return false;
|
||||
}
|
||||
|
||||
let closestNPC = getClosestNPC(getPlayerPosition(client));
|
||||
|
||||
if(!getNPCData(closestNPC)) {
|
||||
if (!getNPCData(closestNPC)) {
|
||||
messagePlayerError(client, getLocaleString(client, "InvalidNPC"));
|
||||
return false;
|
||||
}
|
||||
@@ -422,7 +422,7 @@ function getNPCInfoCommand(command, params, client) {
|
||||
|
||||
let ownerName = "Nobody";
|
||||
let ownerType = "None";
|
||||
switch(npcData.ownerType) {
|
||||
switch (npcData.ownerType) {
|
||||
case VRR_NPCOWNER_CLAN:
|
||||
ownerName = getClanData(getClanIdFromDatabaseId(npcData.ownerId)).name;
|
||||
ownerType = "clan";
|
||||
@@ -464,22 +464,19 @@ function getNPCInfoCommand(command, params, client) {
|
||||
|
||||
messagePlayerNormal(client, makeChatBoxSectionHeader(getLocaleString(client, "HeaderNPCInfo")));
|
||||
let chunkedList = splitArrayIntoChunks(stats, 6);
|
||||
for(let i in chunkedList) {
|
||||
for (let i in chunkedList) {
|
||||
messagePlayerInfo(client, chunkedList[i].join(", "));
|
||||
}
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
|
||||
function getClosestNPC(position) {
|
||||
function getClosestNPC(position, interior, dimension) {
|
||||
let npcs = getServerData().npcs;
|
||||
|
||||
let interior = getPlayerInterior(client);
|
||||
let dimension = getPlayerDimension(client);
|
||||
|
||||
let closest = 0;
|
||||
for(let i in npcs) {
|
||||
if(getDistance(npcs[i].ped.position, position) < getDistance(npcs[closest].ped.position, position) && npcs[closest].interior == interior && npcs[closest].dimension == dimension) {
|
||||
for (let i in npcs) {
|
||||
if (getDistance(npcs[i].ped.position, position) < getDistance(npcs[closest].ped.position, position) && npcs[closest].interior == interior && npcs[closest].dimension == dimension) {
|
||||
closest = i;
|
||||
}
|
||||
}
|
||||
@@ -502,9 +499,9 @@ function createNPC(skinIndex, position, heading, interior, dimension) {
|
||||
let npcIndex = getServerData().npcs.push(tempNPCData);
|
||||
setNPCDataIndexes();
|
||||
|
||||
spawnNPC(npcIndex-1);
|
||||
spawnNPC(npcIndex - 1);
|
||||
|
||||
return npcIndex-1;
|
||||
return npcIndex - 1;
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
Reference in New Issue
Block a user