From c0ca7102fa09ba01e9c52d1ebe7cd5dc5ad60d6e Mon Sep 17 00:00:00 2001 From: swkeep <49286776+swkeep@users.noreply.github.com> Date: Sat, 22 Jun 2024 14:58:39 +0330 Subject: [PATCH] feat: separate network entities --- interactionMenu/lua/client/interact.lua | 6 +- interactionMenu/lua/client/menuContainer.lua | 29 ++- interactionMenu/lua/examples/onEntites.lua | 191 +++++++++++++++++++ 3 files changed, 220 insertions(+), 6 deletions(-) diff --git a/interactionMenu/lua/client/interact.lua b/interactionMenu/lua/client/interact.lua index da6ff5d..4ce78b3 100644 --- a/interactionMenu/lua/client/interact.lua +++ b/interactionMenu/lua/client/interact.lua @@ -16,9 +16,6 @@ MenuTypes = Util.ENUM { } local Wait = Wait -local SetDrawOrigin = SetDrawOrigin -local DrawSprite = DrawSprite -local ClearDrawOrigin = ClearDrawOrigin local PlayerPedId = PlayerPedId local GetEntityCoords = GetEntityCoords local SetScriptGfxDrawBehindPausemenu = SetScriptGfxDrawBehindPausemenu @@ -29,6 +26,7 @@ local pause = false local scaleform_initialized = false local scaleform local SpatialHashGrid = Util.SpatialHashGrid +-- local grid_zone = SpatialHashGrid:new('zone', 100) local grid_position = SpatialHashGrid:new('position', 100) @@ -114,7 +112,7 @@ function Interact:setVisibility(id, value) scaleform.send("interactionMenu:menu:setVisibility", { id = id, visibility = value }) end -function Interact:SetDarkMode(value) +function Interact:setDarkMode(value) scaleform.send("interactionMenu:darkMode", value) end diff --git a/interactionMenu/lua/client/menuContainer.lua b/interactionMenu/lua/client/menuContainer.lua index 6c0b60b..c39cc41 100644 --- a/interactionMenu/lua/client/menuContainer.lua +++ b/interactionMenu/lua/client/menuContainer.lua @@ -41,6 +41,7 @@ Container = { indexes = { models = {}, entities = {}, + netIds = {}, players = {}, bones = {}, globals = { @@ -209,6 +210,12 @@ local function classifyMenuInstance(instance) models[model] = models[model] or {} table.insert(models[model], instance.id) + elseif instance.type == 'netId' then + local netIds = indexes.netIds + local netId = instance.netId + netIds[netId] = netIds[netId] or {} + + table.insert(netIds[netId], instance.id) elseif instance.player then local players = indexes.players players[instance.player] = players[instance.player] or {} @@ -308,6 +315,9 @@ function Container.create(t) elseif t.model then instance.type = 'model' instance.model = t.model + elseif t.netId then + instance.type = 'netId' + instance.netId = t.netId elseif t.bone then instance.type = 'bone' instance.bone = t.bone @@ -578,6 +588,14 @@ function Container.getMenu(model, entity, menuId) Util.table_merge(combinedIds, Container.indexes.players[playerId] or {}) end + local networked = NetworkGetEntityIsNetworked(entity) + local netId + + if networked then + netId = NetworkGetNetworkIdFromEntity(entity) + Util.table_merge(combinedIds, Container.indexes.netIds[netId] or {}) + end + -- bone local bones = Container.indexes.bones[entity] if bones and closestBoneName then @@ -621,14 +639,21 @@ function Container.getMenuType(t) local entities = Container.indexes.entities local models = Container.indexes.models local players = Container.indexes.players + local netIds = Container.indexes.netIds local playerId = IdentifyPlayerServerId(entityType, entity) + local networked = NetworkGetEntityIsNetworked(entity) + local netId + + if networked then + netId = NetworkGetNetworkIdFromEntity(entity) + end if t.zone then return MenuTypes['ON_ZONE'] elseif t.closestPoint and next(t.closestPoint) then -- onPosition return MenuTypes['ON_POSITION'] - elseif (entityType == 3 or entityType == 2) and models[model] or entities[entity] or players[playerId] or globalsExistsCheck(entity, entityType) then + elseif (entityType == 3 or entityType == 2) and models[model] or entities[entity] or players[playerId] or globalsExistsCheck(entity, entityType) or netIds[netId] then -- onModel / onEntity / onBone return MenuTypes['ON_ENTITY'] else @@ -1038,7 +1063,7 @@ function Container.syncData(scaleform, menuData, refreshUI) if Config.features.timeBasedTheme and is_daytime() ~= previous_daytime then previous_daytime = is_daytime() - Interact:SetDarkMode(previous_daytime) + Interact:setDarkMode(previous_daytime) end end diff --git a/interactionMenu/lua/examples/onEntites.lua b/interactionMenu/lua/examples/onEntites.lua index 492ad5f..def60d4 100644 --- a/interactionMenu/lua/examples/onEntites.lua +++ b/interactionMenu/lua/examples/onEntites.lua @@ -722,4 +722,195 @@ CreateThread(function() SetTimeout(4000, function() exports['interactionMenu']:remove(remove_test) end) + + ped_position = vector4(-2006.71, 3167.59, 31.81, 355.17) + ped_ = Util.spawnPed(GetHashKey('a_c_husky'), ped_position) + + FreezeEntityPosition(ped_, false) + + local function loadAnim(animation) + RequestAnimDict(animation) + while not HasAnimDictLoaded(animation) do + Citizen.Wait(100) + end + return true + end + + local function faceMe(entity1, entity2) + local p1 = GetEntityCoords(entity1, true) + local p2 = GetEntityCoords(entity2, true) + + local dx = p2.x - p1.x + local dy = p2.y - p1.y + + local heading = GetHeadingFromVector_2d(dx, dy) + TaskAchieveHeading(entity1, heading, 1500) + Wait(1000) + end + + + local function goThere(ped) + local target = vector3(-2022.15, 3176.92, 32.81) + local start_position = GetEntityCoords(ped) + + TaskGoToCoordAnyMeans(ped, target, 10.0, 0, 0, 0, 0) + while true do + local current_position = GetEntityCoords(ped) + if #(current_position - target) < 3 then + break + end + + Wait(100) + end + TaskGoToCoordAnyMeans(ped, start_position, 10.0, 0, 0, 0, 0) + while true do + local current_position = GetEntityCoords(ped) + if #(current_position - start_position) < 3 then + break + end + + Wait(100) + end + faceMe(ped, PlayerPedId()) + end + + local function SetRelationshipBetweenPed(ped) + if not ped then + return + end + -- note: if we don't do this they will star fighting among themselves! + RemovePedFromGroup(ped) + SetPedRelationshipGroupHash(ped, GetHashKey(ped)) + SetCanAttackFriendly(ped, false, false) + end + + local function AttackTargetedPed(AttackerPed, targetPed) + ClearPedTasks(AttackerPed) + if not AttackerPed and not targetPed then + return false + end + SetPedCombatAttributes(AttackerPed, 46, 1) + TaskGoToEntityWhileAimingAtEntity(AttackerPed, targetPed, targetPed, 8.0, 1, 0, 15, 1, 1, 1566631136) + TaskCombatPed(AttackerPed, targetPed, 0, 16) + SetRelationshipBetweenPed(AttackerPed) + SetPedCombatMovement(AttackerPed, 3) + end + + local sit = false + local lookat_me = false + local previousPos = nil + + exports['interactionMenu']:Create { + entity = ped_, + offset = vector3(0, 0, 0.4), + options = { + { + label = "Health", + progress = { + type = "info", + value = 0, + percent = true + }, + bind = { + func = function(entity, distance, coords, name, bone) + local max_hp = 100 - GetPedMaxHealth(entity) + local current_hp = 100 - GetEntityHealth(entity) + return math.floor(current_hp * 100 / max_hp) + end + } + }, + { + label = 'Show them who\'s boss', + action = { + type = 'async', + func = function(entity) + local target = vector4(-2021.57, 3181.07, 31.81, 247.2) + -- local myTarget = Util.spawnPed(GetHashKey('A_C_shepherd'), target) + local myTarget = Util.spawnPed(GetHashKey('cs_brad'), target) + local start_position = GetEntityCoords(entity) + FreezeEntityPosition(myTarget, false) + AttackTargetedPed(myTarget, entity) + AttackTargetedPed(entity, myTarget) + + while not IsPedDeadOrDying(myTarget, true) do + Wait(1000) + end + TaskGoToCoordAnyMeans(entity, start_position, 10.0, 0, 0, 0, 0) + Wait(2000) + + DeleteEntity(myTarget) + end + } + }, + { + label = 'Face Me', + action = { + type = 'sync', + func = function(entity) + faceMe(entity, PlayerPedId()) + end + } + }, + { + label = 'Face me until I tell you to stop', + action = { + type = 'sync', + func = function(entity) + if lookat_me then + print('stop') + lookat_me = false + return + end + print('start') + lookat_me = true + + CreateThread(function() + while lookat_me do + local currentPos = GetEntityCoords(PlayerPedId(), true) + + if not previousPos or (currentPos.x ~= previousPos.x or currentPos.y ~= previousPos.y or currentPos.z ~= previousPos.z) then + faceMe(entity, PlayerPedId()) + previousPos = currentPos + end + + Wait(600) + end + end) + end + } + }, + { + label = 'Sit/Stand', + action = { + type = 'sync', + func = function(entity) + local dict = 'creatures@retriever@amb@world_dog_sitting@idle_a' + local anim = 'idle_c' + if not sit then + loadAnim(dict) + local flag = 0 + TaskPlayAnim(entity, dict, anim, 8.0, 0, -1, flag or 1, 0, 0, 0, 0) + print('play') + Wait(1000) + sit = true + else + StopAnimTask(entity, dict, anim, 1.0) + print('stop') + Wait(1000) + sit = false + end + end + } + }, + { + label = 'Move', + action = { + type = 'sync', + func = function(entity) + goThere(entity) + end + } + }, + } + } end)