Untitled conversation
- User
function BeginToPoint(x, y, z, radius, move_code, isSprint) bot = true repeat local posX, posY, posZ = GetCoordinates() local dist = getDistanceBetweenCoords3d(x, y, z, posX, posY, z) setAngle(x, y, dist, 0.1) MovePlayer(move_code, isSprint) wait(0) until not bot or dist < radius or findAllRandomCharsInSphere(x, y, z, 4, false, true) bot = false endМаксимально улучши код, как это только возможно
- Assistant
Вот улучшенная версия вашего кода на Lua. Я добавил несколько изменений, чтобы повысить читаемость, производительность и безопасность.
- Убрал глобальные переменные, предположив, что они не должны использоваться.
- Избежал вызова
wait(0)в каждом цикле, вместо этого поставил его после выполнения действий движения. - Добавил проверку на
nilдляGetCoordinates(), чтобы избежать возможных ошибок.
- User
function setAngle(x, y, distance, speed) local source_x = fix(representIntAsFloat(readMemory(0xB6F248, 4, false))) local source_z = fix(representIntAsFloat(readMemory(0xB6F258, 4, false))) + math.pi local angle = GetAngleBeetweenTwoPoints(x, y) - source_z - math.pi if distance > 1.8 then if angle > -0.1 and angle < 0.03 then setCameraPositionUnfixed(-0.3, GetAngleBeetweenTwoPoints(x, y)) elseif angle < -5.7 and angle > -5.93 then setCameraPositionUnfixed(-0.3, GetAngleBeetweenTwoPoints(x, y)) elseif angle < -6.0 and angle > -6.4 then setCameraPositionUnfixed(-0.3, GetAngleBeetweenTwoPoints(x, y)) elseif angle > 0.04 then setCameraPositionUnfixed(-0.3, fix(representIntAsFloat(readMemory(0xB6F258, 4, false))) + speed) elseif angle < -3.5 and angle > -5.67 then setCameraPositionUnfixed(-0.3, fix(representIntAsFloat(readMemory(0xB6F258, 4, false))) + speed) else setCameraPositionUnfixed(-0.3, fix(representIntAsFloat(readMemory(0xB6F258, 4, false))) - speed) end else setCameraPositionUnfixed(source_x, GetAngleBeetweenTwoPoints(x, y)) end endМаксимально улучши код, как это только возможно
- Assistant
Вот переписанная и улучшенная версия вашего кода на Lua. Я внес несколько изменений, чтобы улучшить читаемость, производительность и структуру.
Основные улучшения:
- Избежание повторения кода: Убрал повторяющиеся вызовы
fix(representIntAsFloat(readMemory(0xB6F258, 4, false))), сохранив результат в переменной.
- Избежание повторения кода: Убрал повторяющиеся вызовы
- User
function setAngle(targetX, targetY, distance, speed) local sourceX = fix(representIntAsFloat(readMemory(0xB6F248, 4, false))) local sourceZ = fix(representIntAsFloat(readMemory(0xB6F258, 4, false))) + math.pi local angle = GetAngleBeetweenTwoPoints(targetX, targetY) - sourceZ - math.pi local baseCameraPositionZ = fix(representIntAsFloat(readMemory(0xB6F258, 4, false))) if distance > 1.8 then if (angle > -0.1 and angle < 0.03) or (angle < -5.7 and angle > -5.93) or (angle < -6.0 and angle > -6.4) then setCameraPositionUnfixed(-0.3, GetAngleBeetweenTwoPoints(targetX, targetY)) elseif angle > 0.04 or (angle < -3.5 and angle > -5.67) then setCameraPositionUnfixed(-0.3, baseCameraPositionZ + speed) else setCameraPositionUnfixed(-0.3, baseCameraPositionZ - speed) end else setCameraPositionUnfixed(sourceX, GetAngleBeetweenTwoPoints(targetX, targetY)) end end function GetAngleBeetweenTwoPoints(x2, y2) local x1, y1, z1 = getCharCoordinates(playerPed) local plus = 0.0 local mode = 1 if x1 < x2 and y1 > y2 then plus = math.pi / 2 mode = 2 end if x1 < x2 and y1 < y2 then plus = math.pi end if x1 > x2 and y1 < y2 then plus = math.pi * 1.5 mode = 2 end local lx = x2 - x1 local ly = y2 - y1 lx = math.abs(lx) ly = math.abs(ly) if mode == 1 then ly = ly / lx else ly = lx / ly end ly = math.atan(ly) ly = ly + plus return ly end function fix(angle) while angle > math.pi do angle = angle - (math.pi * 2) end while angle < -math.pi do angle = angle + (math.pi * 2) end return angle end - Assistant
- User
- Assistant