script_name("g-meter")
script_author("plasma")
script_version("2.0")
local mad = require 'MoonAdditions'
local m = require "memory"
--variaveis--
local gColor = 0x00000000 -- cor do centro do g-meter (alpha, red, green, blue)
local gBorderColor = 0xCCFF5544 -- cor da borda do g-meter
local gPointColor = 0xFF1100CC -- cor do ponto medidor do g-meter
local gPointBorderColor = 0xFFFFFFFF -- cor da borda do ponto
local gBorderSize = 4 -- tamanho da borda
local gx = 20 -- posição horizontal do g-meter
local gy = 320 -- posição vertical do g-meter
local gSizeX = 150 -- tamanho horizontal do g-meter
local gSizeY = 130 -- tamanho vertical do g-meter
local maxLongG = 3.0 -- maximo Gs longitudinal (frente/traseira)
local maxLatG = 6.0 -- maximo Gs lateral (direita/esquerda)
local sp1 = 0
local sp2 = 0
local acel = 0
local acelL = 0
local t1 = 0
local t2 = 0
local deltaTimeime = 0
local car = 0
local angZ = 0
local angZ2 = 0
local radian = 0
local function getTimeScale()
gameSpeed = m.getfloat(0xB7CB64, false)
return gameSpeed
end
function updateDeltaTime()
while true do
local time1 = gameClock()
wait(0)
deltaTime = gameClock() - time1
end
end
local function testSpeedDiff()
if sp1 ~= sp2 then
sp1 = sp2
end
end
local function acelLong()
sp2 = getCharSpeed(PLAYER_PED) -- velocidade atual do player
acel = (sp2 - sp1)/deltaTime
acel = acel/getTimeScale()
end
local function getZAngle(car)
return getCarHeading(car)
end
local function acelLat()
angZ = getZAngle(car)
radian = ((angZ-angZ2) / (deltaTime)) * 0.01745 -- transforma a variação de deg/s em rad/s
acelL = (radian * sp2) -- ao multiplicar rad/s por m/s temos m/s^2 = aceleração lateral
acelL = acelL/getTimeScale()
end
local function testAngleDiff()
if angZ2 ~= angZ then
angZ2 = angZ
end
end
function draw_text(str, x, y, r, g, b)
mad.draw_text(str, x, y, mad.font_style.MENU, 0.3, 0.6, mad.font_align.LEFT, 2000, true, false, r, g, b, 255, 1, 0, 30, 30, 30, 120)
end
local function drawGraphics()
local x, y = 0, 0
local maxX, maxY = gx+gSizeX, gy+gSizeY
local halfX, halfY = gSizeX/2, gSizeY/2
code
Assistant
User
Assistant
local pointCenterX = gx+halfX+gBorderSize/2
local pointCenterY = gy+halfY-gBorderSize
x = pointCenterX + (acelL/9.8) * ((maxX - pointCenterX) / maxLatG)
y = pointCenterY + (acel/9.8) * ((maxY - pointCenterY) / maxLongG)
renderDrawBoxWithBorder(gx, gy, gSizeX, gSizeY, gColor, gBorderSize, gBorderColor)
renderDrawLine(gx+1, gy+halfY-gBorderSize, gx+gSizeX, gy+halfY-gBorderSize, gBorderSize/2, gBorderColor) -- horizontal line
renderDrawLine(gx+halfX+gBorderSize, gy+1, gx+halfX+gBorderSize, gy+gSizeY, gBorderSize/2, gBorderColor)
x = x >= gx+gBorderSize and x or gx+gBorderSize
x = x <= maxX-gBorderSize and x or maxX-gBorderSize
y = y >= gy+gBorderSize and y or gy+gBorderSize
y = y <= maxY-gBorderSize and y or maxY-gBorderSize
renderDrawPolygon(x, y, 7, 7, 16, 0.0, gPointBorderColor) -- border
renderDrawPolygon(x, y, 5, 5, 16, 0.0, gPointColor)
draw_text("Lat: " .. string.format("%.2f", acelL/9.8) .. " g", gx+halfX/2, gy+gSizeY+gBorderSize, 255, 255, 255)
draw_text("Long: " .. string.format("%.2f", acel/9.8) .. " g", gx+gSizeX+gBorderSize*2, gy+halfY, 255, 255, 255)
end
function main()
local threadTime = lua_thread.create(updateDeltaTime)
while true do
wait(0)
if isPlayerPlaying(PLAYER_HANDLE) then
if not isCharOnFoot(PLAYER_PED) then --calculos secudarios sao feitos apenas quando dentro de um veiculo
car = getCarCharIsUsing(PLAYER_PED)
testSpeedDiff()
testAngleDiff()
acelLong()
acelLat()
drawGraphics()
end
code
end
end
return
end
Конечно! Вот рефакторинг кода с улучшенной структурой и ясностью:
lua
script_name("g-meter")
script_author("plasma")
script_version("2.0")
local mad = require 'MoonAdditions'
local m = require "memory"
-- Настройки
local gMeterConfig = {
color = 0x00000000,
borderColor = 0xCCFF5544,
pointColor = 0xFF1100CC,
pointBorderColor = 0xFFFFFFFF,
borderSize = 4,
positionX = 20,
positionY = 320,
sizeX = 150,
sizeY = 130,
maxLongG = 3.0,
maxLatG = 6.0
}
-- Переменные
local speedPrevious, speedCurrent = 0, 0
local acceleration, lateralAcceleration = 0, 0
local angleZ, angleZPrevious = 0, 0
local deltaTime = 0
local car = 0
local function getTimeScale()
return m.getfloat(0xB7CB64, false)
end
local function updateDeltaTime()
while true do
local startTime = gameClock()
wait(0)
deltaTime = gameClock() - startTime
end
end
local function updateSpeed()
speedCurrent = getCharSpeed(PLAYER_PED)
acceleration = (speedCurrent - speedPrevious) / deltaTime
acceleration = acceleration / getTimeScale()
speedPrevious = speedCurrent
end
local function getCarZAngle(car)
return getCarHeading(car)
end
local function updateLateralAcceleration()
angleZ = getCarZAngle(car)
local radian = ((angleZ - angleZPrevious) / deltaTime) * 0.01745
lateralAcceleration = (radian * speedCurrent) / getTimeScale()
angleZPrevious = angleZ
end
local function drawText(str, x, y, r, g, b)
mad.draw_text(str, x, y, mad.font_style.MENU, 0.3, 0.6, mad.font_align.LEFT, 2000, true, false, r, g, b, 255, 1, 0, 30, 30, 30, 120)
end
local function drawGraphics()
local config = gMeterConfig
local centerX, centerY = config.positionX + config.sizeX / 2, config.positionY + config.sizeY / 2
local pointCenterX = centerX + config.borderSize / 2
local pointCenterY = centerY - config.borderSize
local x = pointCenterX + (lateralAcceleration / 9.8) * ((config.sizeX - pointCenterX) / config.maxLatG)
local y = pointCenterY + (acceleration / 9.8) * ((config.sizeY - pointCenterY) / config.maxLongG)
renderDrawBoxWithBorder(config.positionX, config.positionY, config.sizeX, config.sizeY, config.color, config.borderSize, config.borderColor)
renderDrawLine(config.positionX + 1, centerY - config.borderSize, config.positionX + config.sizeX, centerY - config.borderSize, config.borderSize / 2, config.borderColor)
renderDrawLine(centerX + config.borderSize, config.positionY + 1, centerX + config.borderSize, config.positionY + config.sizeY, config.borderSize / 2, config.borderColor)
x = math.max(config.positionX + config.borderSize, math.min(x, config.positionX + config.sizeX - config.borderSize))
y = math.max(config.positionY + config.borderSize, math.min(y, config.positionY + config.sizeY - config.borderSize))
renderDrawPolygon(x, y, 7, 7, 16, 0.0, config.pointBorderColor)
renderDrawPolygon(x, y, 5, 5, 16, 0.0, config.pointColor)
drawText("Lat: " .. string.format("%.2f", lateralAcceleration / 9.8) .. " g", config.positionX + config.sizeX / 4, config.positionY + config.sizeY + config.borderSize, 255, 255, 255)
drawText("Long: " .. string.format("%.2f", acceleration / 9.8) .. " g", config.positionX + config.sizeX + config.borderSize * 2, config.positionY + config.sizeY / 2, 255, 255, 255)
end
function main()
local threadTime = lua_thread.create(updateDeltaTime)
while true do
wait(0)
if isPlayerPlaying(PLAYER_HANDLE) then
if not isCharOnFoot(PLAYER_PED) then
car = getCarCharIsUsing(PLAYER_PED)
updateSpeed()
updateLateralAcceleration()
drawGraphics()
end
end
end
end
Я разделил код на логические блоки, улучшил именование переменных и функций, а также сделал настройки более читаемыми и структурированными.