-- ========================================================== -- Строитель: клиент -- ========================================================== local KEY_E = 38 -- INPUT_CONTEXT local CTRL_GROW = 38 -- E (удерживаем, чтобы расти) local CTRL_CANCEL = 177 -- Backspace local npc = nil local npcPos = vector3(Config.Npc.coords.x, Config.Npc.coords.y, Config.Npc.coords.z) local jobBlip = nil local siteBlip = nil local employed = false local savedOutfit = nil local dialogOpen = false local task = nil -- { idx, target, expires } local minigame = nil -- { size, target, wasHolding } -- ---------- Помощники ---------- local function help(text, duration) BeginTextCommandDisplayHelp('STRING') AddTextComponentSubstringPlayerName(text) EndTextCommandDisplayHelp(0, false, true, duration or -1) end local function nui(action, data) data = data or {} data.action = action SendNUIMessage(data) end local function makeBlip(coords, sprite, color, scale, label) local b = AddBlipForCoord(coords.x, coords.y, coords.z) SetBlipSprite(b, sprite) SetBlipColour(b, color) SetBlipScale(b, scale) SetBlipAsShortRange(b, false) BeginTextCommandSetBlipName('STRING') AddTextComponentSubstringPlayerName(label) EndTextCommandSetBlipName(b) return b end local function getOutfit(ped) local data = { components = {}, props = {} } for i = 0, 11 do data.components[i] = { drawable = GetPedDrawableVariation(ped, i), texture = GetPedTextureVariation(ped, i), palette = GetPedPaletteVariation(ped, i), } end for i = 0, 2 do data.props[i] = { drawable = GetPedPropIndex(ped, i), texture = GetPedPropTextureIndex(ped, i), } end return data end local function restoreOutfit(ped, data) for i, c in pairs(data.components) do SetPedComponentVariation(ped, i, c.drawable, c.texture, c.palette) end for i, p in pairs(data.props) do if p.drawable == -1 then ClearPedProp(ped, i) else SetPedPropIndex(ped, i, p.drawable, p.texture, true) end end end local function applyWorkOutfit(ped) for i, c in pairs(Config.WorkOutfit.components) do SetPedComponentVariation(ped, i, c.drawable, c.texture, 0) end for i, p in pairs(Config.WorkOutfit.props) do if p.drawable < 0 then ClearPedProp(ped, i) else SetPedPropIndex(ped, i, p.drawable, p.texture, true) end end pcall(function() exports.grandrp_fixclothes:applyForTop(ped) end) end local function spawnNpc() local model = GetHashKey(Config.Npc.model) RequestModel(model) local t = GetGameTimer() while not HasModelLoaded(model) and GetGameTimer() - t < 10000 do Wait(10) end local c = Config.Npc.coords local groundZ = nil local t2 = GetGameTimer() while not groundZ and GetGameTimer() - t2 < 15000 do RequestCollisionAtCoord(c.x, c.y, c.z) local found, z = GetGroundZFor_3dCoord(c.x, c.y, c.z + 5.0, false) if found then groundZ = z end if not groundZ then Wait(250) end end local z = groundZ or c.z npc = CreatePed(4, model, c.x, c.y, z, c.w, false, true) SetEntityInvincible(npc, true) SetBlockingOfNonTemporaryEvents(npc, true) FreezeEntityPosition(npc, true) TaskStartScenarioInPlace(npc, Config.Npc.scenario, 0, true) SetModelAsNoLongerNeeded(model) end -- ---------- Задание ---------- local function removeTask() task = nil if siteBlip and DoesBlipExist(siteBlip) then RemoveBlip(siteBlip) end siteBlip = nil end local function stopMinigame() if minigame then minigame = nil nui('build', { show = false }) end end -- Кубик падает. Если размер попал в яму, задание сдано. local function dropBlock(m) local ok = math.abs(m.size - m.target) <= Config.Task.tolerance nui('build', { drop = true, ok = ok }) Wait(900) if ok then local idx = task and task.idx stopMinigame() removeTask() if idx then TriggerServerEvent('redlio:build:complete', idx) end else m.size = Config.Task.minSize m.wasHolding = false nui('build', { size = math.floor(m.size), reset = true }) help('Не подошло по размеру. Попробуйте ещё раз.', 3000) end end local function startMinigame() if minigame or not task then return end local m = { size = Config.Task.minSize, target = task.target, wasHolding = false } minigame = m nui('build', { show = true, target = m.target, size = math.floor(m.size) }) CreateThread(function() local last = GetGameTimer() while minigame == m and task do Wait(0) local now = GetGameTimer() local dt = math.min((now - last) / 1000.0, 0.1) last = now DisableControlAction(0, 24, true) DisableControlAction(0, CTRL_GROW, true) for _, c in ipairs({ 30, 31, 32, 33, 34, 35 }) do DisableControlAction(0, c, true) end if IsControlJustPressed(0, CTRL_CANCEL) then stopMinigame() help('Работа отменена. Чтобы вернуться, подойдите к метке.', 3000) break end local holding = IsDisabledControlPressed(0, CTRL_GROW) if holding then m.size = math.min(Config.Task.maxSize, m.size + Config.Task.grow * dt) nui('build', { size = math.floor(m.size) }) elseif m.wasHolding then dropBlock(m) end m.wasHolding = holding end end) end -- ---------- Диалог с прорабом ---------- local function openDialog() dialogOpen = true SetNuiFocus(true, true) nui('open', { npcName = Config.Npc.name, employed = employed, pay = Config.Task.pay, }) end local function closeDialog() dialogOpen = false SetNuiFocus(false, false) nui('close') end RegisterNUICallback('close', function(_, cb) closeDialog() cb('ok') end) RegisterNUICallback('hire', function(_, cb) closeDialog() TriggerServerEvent('redlio:build:hire') cb('ok') end) RegisterNUICallback('quit', function(_, cb) closeDialog() TriggerServerEvent('redlio:build:quit') cb('ok') end) -- ---------- События с сервера ---------- RegisterNetEvent('redlio:build:hired', function() employed = true local ped = PlayerPedId() savedOutfit = getOutfit(ped) applyWorkOutfit(ped) help('Вы устроились строителем. Ждите задание: появится метка на карте.', 5000) end) RegisterNetEvent('redlio:build:fired', function() employed = false removeTask() stopMinigame() if savedOutfit then restoreOutfit(PlayerPedId(), savedOutfit) savedOutfit = nil end help('Вы уволились. Ваша одежда возвращена.', 5000) end) RegisterNetEvent('redlio:build:task', function(idx, target, ms) removeTask() local s = Config.Sites[idx] if not s then return end task = { idx = idx, target = target, expires = GetGameTimer() + (ms or 180000) } siteBlip = makeBlip(s.coords, Config.SiteBlip.sprite, Config.SiteBlip.color, 0.9, Config.SiteBlip.label) SetNewWaypoint(s.coords.x, s.coords.y) help('Новое задание на карте. Подойдите к метке и нажмите ~INPUT_CONTEXT~.', 5000) end) RegisterNetEvent('redlio:build:taskEnd', function() stopMinigame() removeTask() help('Время задания вышло. Ждите следующую метку.', 4000) end) RegisterNetEvent('redlio:build:paid', function(amount) TriggerEvent('grandrp_chat:system', 'Строитель: вам начислено $' .. amount) nui('pay', { amount = amount, duration = 5000 }) end) RegisterNetEvent('redlio:build:levelup', function(lvl) TriggerEvent('grandrp_chat:system', 'Навык строителя вырос: уровень ' .. lvl) end) RegisterNetEvent('redlio:build:denied', function(msg) help('~r~' .. msg, 4000) end) -- ---------- Основной цикл ---------- CreateThread(function() spawnNpc() jobBlip = makeBlip(npcPos, Config.Blip.sprite, Config.Blip.color, Config.Blip.scale, Config.Blip.label) while true do local sleep = 500 local pos = GetEntityCoords(PlayerPedId()) if not dialogOpen and not minigame then -- прораб local dNpc = #(pos - npcPos) if dNpc < 15.0 then sleep = 0 end if dNpc < Config.InteractDistance then help('Нажмите ~INPUT_CONTEXT~, чтобы поговорить с прорабом') if IsControlJustPressed(0, KEY_E) then openDialog() end end -- метка активного задания if task then local s = Config.Sites[task.idx] local d = #(pos - s.coords) if d < 30.0 then sleep = 0 DrawMarker(1, s.coords.x, s.coords.y, s.coords.z - 0.95, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 0.6, 255, 200, 40, 150, false, true, 2, false, nil, nil, false) end if d < Config.InteractDistance then help('Нажмите ~INPUT_CONTEXT~, чтобы начать работу') if IsControlJustPressed(0, KEY_E) then startMinigame() end end end end Wait(sleep) end end) -- Координаты для настройки: встаньте на место и введите /build_coords RegisterCommand('build_coords', function() local c = GetEntityCoords(PlayerPedId()) local h = GetEntityHeading(PlayerPedId()) local text = string.format('vector4(%.2f, %.2f, %.2f, %.2f)', c.x, c.y, c.z, h) print('[builder] ' .. text) help('Координаты: ~y~' .. text .. '~s~ (также в F8)', 6000) end, false) AddEventHandler('onResourceStop', function(res) if res ~= GetCurrentResourceName() then return end SetNuiFocus(false, false) if npc and DoesEntityExist(npc) then DeleteEntity(npc) end if jobBlip and DoesBlipExist(jobBlip) then RemoveBlip(jobBlip) end removeTask() if savedOutfit then restoreOutfit(PlayerPedId(), savedOutfit) end end)