Skip to content

Commit 6592e66

Browse files
authored
Merge pull request #138 from YimMenu-Lua/manual_gearbox_and_fixes
Manual gearbox and fixes
2 parents 8d33ee2 + 79cde34 commit 6592e66

73 files changed

Lines changed: 4472 additions & 2091 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

SSV2/includes/backend.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,11 @@ function Backend:RegisterHandlers()
500500
Translator:OnTick()
501501

502502
yield()
503-
end)
503+
end, {
504+
exception_handler = function()
505+
Backend:Cleanup()
506+
end
507+
})
504508

505509
ThreadManager:RegisterLooped("SS_POOLMGR", function()
506510
self:PoolMgr()

SSV2/includes/classes/Set.lua

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,25 @@
1010
--------------------------------------
1111
-- Class: Set
1212
--------------------------------------
13-
---@generic T
1413
---@class Set<T> : { [T]: true }
15-
---@field protected m_data table<anyval, true>
14+
---@field protected m_data table<T, true>
1615
---@field protected m_data_type string
17-
---@overload fun(...): Set<...>
18-
Set = {}
16+
---@field new fun(...: T): Set<T>
17+
---@overload fun(...: T): Set<T>
18+
Set = { __type = "Set" }
1919
Set.__index = Set
20-
Set.__type = "Set"
20+
2121
---@diagnostic disable-next-line: param-type-mismatch
2222
setmetatable(Set, {
2323
__call = function(_, ...)
2424
return Set.new(...)
2525
end
2626
})
2727

28-
---@generic T
29-
---@param ... T
30-
---@return Set<T>
3128
function Set.new(...)
3229
---@diagnostic disable-next-line: param-type-mismatch
3330
local instance = setmetatable({ m_data = {} }, Set)
34-
local args = { ... }
31+
local args = { ... }
3532

3633
if (#args > 0) then
3734
instance.m_data_type = type(args[1])
@@ -43,7 +40,7 @@ function Set.new(...)
4340
return instance
4441
end
4542

46-
---@param element anyval
43+
---@param element T
4744
function Set:Push(element)
4845
if (element == nil) then
4946
return
@@ -67,20 +64,16 @@ function Set:Push(element)
6764
self.m_data[element] = true
6865
end
6966

70-
---@param element anyval
67+
---@param element T
7168
function Set:Pop(element)
72-
if (type(element) ~= self.m_data_type) then
73-
return
74-
end
75-
7669
self.m_data[element] = nil
7770
end
7871

7972
function Set:Clear()
8073
self.m_data = {}
8174
end
8275

83-
---@param element anyval
76+
---@param element any
8477
---@return boolean
8578
function Set:Contains(element)
8679
return self.m_data[element] == true
@@ -96,10 +89,14 @@ function Set:Size()
9689
return table.getlen(self.m_data)
9790
end
9891

92+
---@return fun(t: table<T, true>, index?: T): T, true
93+
---@return table<T, true>
9994
function Set:Iter()
10095
return pairs(self.m_data)
10196
end
10297

98+
---@return fun(t: table<T, true>, index?: T): T, true
99+
---@return table<T, true>
103100
function Set:__pairs()
104101
return pairs(self.m_data)
105102
end

SSV2/includes/classes/gta/CEntity.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ local CEntity = Class("CEntity", { symbolic_size = 0x028C })
4040
---@param entity handle
4141
---@return CEntity
4242
function CEntity:init(entity)
43-
if (not Game.IsScriptHandle(entity)) then
43+
if (not ENTITY.DOES_ENTITY_EXIST(entity)) then
4444
error("Invalid entity!")
4545
end
4646

SSV2/includes/classes/gta/CPed.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ local CPed = Class("CPed", { parent = CEntity, symbolic_size = 0x161C })
4141
---@param ped handle
4242
---@return CPed
4343
function CPed:init(ped)
44-
if (not Game.IsScriptHandle(ped) or not ENTITY.IS_ENTITY_A_PED(ped)) then
44+
if (not ENTITY.IS_ENTITY_A_PED(ped)) then
4545
error("Invalid entity!")
4646
end
4747

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
-- Copyright (C) 2026 SAMURAI (xesdoog) & Contributors.
2+
-- This file is part of Samurai's Scripts.
3+
--
4+
-- Permission is hereby granted to copy, modify, and redistribute
5+
-- this code as long as you respect these conditions:
6+
-- * Credit the owner and contributors.
7+
-- * Provide a copy of or a link to the original license (GPL-3.0 or later); see LICENSE.md or <https://www.gnu.org/licenses/>.
8+
9+
10+
local CStructView = require("includes.classes.gta.CStructView")
11+
12+
13+
--------------------------------------
14+
-- Class: CTransmission
15+
--------------------------------------
16+
---@class CTransmission
17+
---@field public m_current_gear pointer<uint8_t>
18+
---@field public m_previous_gear pointer<uint8_t>
19+
---@field public m_top_gear pointer<uint8_t>
20+
---@field public m_gear_ratios array<pointer<float>>
21+
---@field public m_drive_force pointer<float>
22+
---@field public m_drive_max_flat_velocity pointer<float>
23+
---@field public m_drive_max_velocity pointer<float>
24+
---@field public m_throttle_unk pointer<float> -- not sure what this does but I managed to force revs using only this offset. it worked once then stopped doing anything
25+
---@field public m_rpm pointer<float>
26+
---@field public m_rpm_2 pointer<float>
27+
---@field public m_clutch pointer<float> cvehicle + 0x08D4
28+
---@field public m_throttle pointer<float> these two might be flipped
29+
---@field public m_throttle_input pointer<float> //
30+
---@field public m_boost_pressure pointer<float> m_throttle_input + 16 ? TODO: fix this it's wrong. this keeps incrementing when throttle is held and resets to 0 when released. pressure does not infinitely accumulate but this does
31+
---@overload fun(ptr: pointer): CTransmission
32+
local CTransmission = CStructView("CTransmission")
33+
34+
---@param ptr pointer
35+
---@return CTransmission
36+
function CTransmission.new(ptr)
37+
---@diagnostic disable-next-line
38+
local instance = setmetatable({ m_ptr = ptr, }, CTransmission)
39+
instance.m_current_gear = ptr:add(0x0000)
40+
instance.m_previous_gear = ptr:add(0x0002)
41+
instance.m_top_gear = ptr:add(0x0006)
42+
instance.m_drive_force = ptr:add(0x0038)
43+
instance.m_drive_max_flat_velocity = ptr:add(0x003C)
44+
instance.m_drive_max_velocity = ptr:add(0x0040)
45+
instance.m_throttle_unk = ptr:add(0x0044)
46+
instance.m_rpm = ptr:add(0x0048)
47+
instance.m_rpm_2 = ptr:add(0x004C)
48+
instance.m_clutch = ptr:add(0x0054)
49+
instance.m_throttle = ptr:add(0x0058)
50+
instance.m_throttle_input = ptr:add(0x0060)
51+
instance.m_boost_pressure = ptr:add(0x0070)
52+
53+
54+
local gearRatios = {} ---@type array<pointer<float>>
55+
local pArrayStart = ptr:add(0x000C)
56+
for i = 0, 10 do -- reverse + 10 fwd gears
57+
gearRatios[i + 1] = pArrayStart:add(i * 0x4)
58+
end
59+
instance.m_gear_ratios = gearRatios
60+
61+
return instance
62+
end
63+
64+
---@param gear uint8_t
65+
---@return float
66+
function CTransmission:GetRatioForGear(gear)
67+
local ptr = self.m_gear_ratios[gear + 1]
68+
if (not ptr or ptr:is_null()) then
69+
return 0.0
70+
end
71+
72+
return ptr:get_float()
73+
end
74+
75+
---@param gear uint8_t
76+
---@param ratio float
77+
function CTransmission:SetRatioForGear(gear, ratio)
78+
local ptr = self.m_gear_ratios[gear + 1]
79+
if (not ptr or ptr:is_null()) then return end
80+
ptr:set_float(ratio)
81+
end
82+
83+
---@param gear uint8_t
84+
---@return float
85+
function CTransmission:GetMaxSpeedForGear(gear)
86+
local max_drive_vel = self.m_drive_max_velocity:get_float()
87+
local gear_ratio = math.abs(self:GetRatioForGear(gear))
88+
if (gear_ratio == 0) then
89+
return 0.0
90+
end
91+
return max_drive_vel / gear_ratio
92+
end
93+
94+
return CTransmission

SSV2/includes/classes/gta/CVehicle.lua

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ local CHandlingData = require("includes.classes.gta.CHandlingData")
1414
local CEntity = require("includes.classes.gta.CEntity")
1515
local CVehicleDrawData = require("includes.classes.gta.CVehicleDrawData")
1616
local CVehicleModelInfo = require("includes.classes.gta.CVehicleModelInfo")
17+
local CTransmission = require("includes.classes.gta.CTransmission")
1718
local CWheel = require("includes.classes.gta.CWheel")
1819
local fMatrix44 = require("includes.classes.gta.fMatrix44")
1920
local phFragInst = require("includes.classes.gta.phFragInst")
20-
local CCarHandlingData = require("includes.classes.gta.CCarHandlingData")
2121

2222

2323
---@class CAdvancedData
@@ -30,25 +30,26 @@ local CCarHandlingData = require("includes.classes.gta.CCarHandlingData")
3030
---@ignore
3131
---@class CVehicle : CEntity
3232
---@field protected m_ptr pointer
33-
---@field public m_physics_fragments phFragInst //0x30 `struct rage::phFragInst`
3433
---@field public m_draw_data CVehicleDrawData
3534
---@field public m_handling_data CHandlingData
3635
---@field public m_model_info CVehicleModelInfo
3736
---@field public m_vehicle_damage pointer<CVehicleDamage>
3837
---@field public m_can_boost_jump pointer<byte> `bool`
3938
---@field public m_velocity pointer<vec3>
39+
---@field public m_transmission CTransmission
4040
---@field public m_deform_god pointer<uint8_t>
41+
---@field public m_frag_inst phFragInst //0x09C0 `fragInstGTA`
42+
---@field public m_turbo pointer<float>
4143
---@field public m_water_damage pointer<uint32_t>
42-
---@field public m_next_gear pointer<int16_t>
43-
---@field public m_current_gear pointer<int16_t>
44-
---@field public m_top_gear pointer<int8_t>
4544
---@field public m_engine_health pointer<float>
46-
---@field public m_steering_input pointer<float> // 0xD4 name might not correctly reflect what this actually is but this seems to store controller input (value is between 0.99 (left) .. -0.99 (right))
47-
---@field public m_current_steering pointer<float> 0xDC // actual wheel steer. Wr'll use it to rewrite last known wheel steer after exiting a vehicle in IV-Style Exit so we'll no longer need to teleport outside or patch CTaskVehicleExit
45+
---@field public m_steering_input pointer<float> // 0x00D4 name might not correctly reflect what this actually is but this seems to store controller input (value is between 1.0 (left) .. -1.0 (right))
46+
---@field public m_steering_angle pointer<float> 0x00DC // steering angle?. We'll use it to rewrite last known steering value after exiting a vehicle in IV-Style Exit so we'll no longer have to teleport outside or patch CTaskVehicleExit
47+
---@field public m_throttle_power pointer<float> m_steering_angle + 0x8
48+
---@field public m_brake_power pointer<float>
4849
---@field public m_is_targetable pointer<byte> `bool`
4950
---@field public m_door_lock_status pointer<uint32_t>
50-
---@field public m_wheels atArray<CWheel> -- 0xC30
51-
---@field public m_num_wheels number -- 0xC38
51+
---@field public m_wheels atArray<CWheel> -- 0x0C30
52+
---@field public m_num_wheels integer -- 0x0C38
5253
---@field public m_ride_height pointer<float>
5354
---@field private DumpFlags fun(self: CVehicle, enum_flags: Enum, get_func: fun(self: CVehicle, flag: integer): boolean): nil
5455
---@overload fun(vehicle: integer): CVehicle|nil
@@ -57,7 +58,7 @@ local CVehicle = Class("CVehicle", { parent = CEntity, symbolic_size = 0xC40 })
5758
---@param vehicle handle
5859
---@return CVehicle
5960
function CVehicle:init(vehicle)
60-
if (not Game.IsScriptHandle(vehicle) or not ENTITY.IS_ENTITY_A_VEHICLE(vehicle)) then
61+
if (not ENTITY.IS_ENTITY_A_VEHICLE(vehicle)) then
6162
error("Invalid entity!")
6263
end
6364

@@ -66,30 +67,32 @@ function CVehicle:init(vehicle)
6667
local instance = setmetatable({}, CVehicle)
6768
instance:super().init(instance, vehicle)
6869

69-
local ptr = memory.handle_to_ptr(vehicle)
70-
instance.m_ptr = ptr
71-
instance.m_model_info = CVehicleModelInfo(ptr:add(0x0020):deref())
72-
instance.m_water_damage = ptr:add(0x00D8)
73-
instance.m_physics_fragments = phFragInst(ptr:add(0x0030):deref())
74-
instance.m_draw_data = CVehicleDrawData(ptr:add(0x0048):deref())
75-
instance.m_can_boost_jump = ptr:add(0x03A4)
76-
instance.m_vehicle_damage = ptr:add(0x0420)
77-
instance.m_velocity = ptr:add(0x07D0)
78-
instance.m_is_targetable = ptr:add(0x0AEE)
79-
instance.m_next_gear = ptr:add(0x0880)
80-
instance.m_current_gear = ptr:add(0x0882)
81-
instance.m_top_gear = ptr:add(0x0886)
82-
instance.m_engine_health = ptr:add(0x0910)
83-
instance.m_handling_data = CHandlingData(ptr:add(0x0960):deref(), instance.m_model_info:GetVehicleType())
84-
instance.m_deform_god = ptr:add(0x096C)
85-
instance.m_steering_input = ptr:add(0x09D4)
86-
instance.m_current_steering = ptr:add(0x09DC)
87-
instance.m_door_lock_status = ptr:add(0x13D0)
88-
89-
local array_ptr = ptr:add(0x0C30)
90-
instance.m_wheels = atArray(array_ptr, CWheel)
91-
instance.m_num_wheels = array_ptr:add(0x8):get_int()
92-
instance.m_ride_height = array_ptr:deref():add(0x007C)
70+
local ptr = memory.handle_to_ptr(vehicle)
71+
instance.m_ptr = ptr
72+
instance.m_model_info = CVehicleModelInfo(ptr:add(0x0020):deref())
73+
instance.m_draw_data = CVehicleDrawData(ptr:add(0x0048):deref())
74+
instance.m_turbo = ptr:add(0x007C)
75+
instance.m_water_damage = ptr:add(0x00D8)
76+
instance.m_can_boost_jump = ptr:add(0x03A4)
77+
instance.m_vehicle_damage = ptr:add(0x0420)
78+
instance.m_velocity = ptr:add(0x07D0)
79+
instance.m_transmission = CTransmission(ptr:add(0x0880))
80+
instance.m_engine_health = ptr:add(0x0910)
81+
instance.m_handling_data = CHandlingData(ptr:add(0x0960):deref(), instance.m_model_info:GetVehicleType())
82+
instance.m_deform_god = ptr:add(0x096C)
83+
instance.m_frag_inst = phFragInst(ptr:add(0x09C0):deref())
84+
instance.m_steering_input = ptr:add(0x09D4)
85+
instance.m_steering_angle = ptr:add(0x09DC)
86+
instance.m_throttle_power = ptr:add(0x09E4)
87+
instance.m_brake_power = ptr:add(0x09E8)
88+
instance.m_is_targetable = ptr:add(0x0AEE)
89+
90+
local array_ptr = ptr:add(0x0C30)
91+
instance.m_wheels = atArray(array_ptr, CWheel)
92+
instance.m_num_wheels = array_ptr:add(0x8):get_int()
93+
instance.m_ride_height = array_ptr:deref():add(0x007C)
94+
95+
instance.m_door_lock_status = ptr:add(0x13D0)
9396

9497
return instance
9598
end
@@ -115,14 +118,6 @@ function CVehicle:GetDeformMultiplier()
115118
end)
116119
end
117120

118-
-- ---@param handlingType eHandlingType
119-
-- ---@return CCarHandlingData|CBikeHandlingData|CFlyingHandlingData|any
120-
-- function CVehicle:GetSubHandlingData(handlingType)
121-
-- return self:__safecall(nil, function()
122-
-- return self.m_handling_data:GetSubHandlingData(handlingType)
123-
-- end)
124-
-- end
125-
126121
---@return pointer<(CCarHandlingData|CBikeHandlingData|CFlyingHandlingData)?>
127122
function CVehicle:GetSubHandlingData()
128123
return self.m_handling_data:GetSubHandlingData()
@@ -322,7 +317,7 @@ end
322317
---@param boneIndex integer
323318
---@return fMatrix44
324319
function CVehicle:GetBoneMatrix(boneIndex)
325-
local ph_frag_inst = self.m_physics_fragments
320+
local ph_frag_inst = self.m_frag_inst
326321
if not ph_frag_inst then
327322
return fMatrix44:zero()
328323
end
@@ -338,7 +333,7 @@ end
338333
---@param boneIndex integer
339334
---@param matrix fMatrix44
340335
function CVehicle:SetBoneMatrix(boneIndex, matrix)
341-
local ph_frag_inst = self.m_physics_fragments
336+
local ph_frag_inst = self.m_frag_inst
342337
if not ph_frag_inst then
343338
return
344339
end

SSV2/includes/classes/gta/CVehicleModelInfo.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ local CVehicleModelInfo = CStructView("CVehicleModelInfo", 0x08DC)
3232
---@return CVehicleModelInfo
3333
function CVehicleModelInfo.new(ptr)
3434
return setmetatable({
35-
m_ptr = ptr,
36-
m_vehicle_layout = ptr:add(0x00B0),
37-
m_vehicle_type = ptr:add(0x0340),
38-
m_wheel_scale = ptr:add(0x048C),
39-
m_wheel_scale_rear = ptr:add(0x0490),
40-
m_model_info_flags = ptr:add(0x057C),
41-
m_throttle_position = ptr:add(0x08D8),
35+
m_ptr = ptr,
36+
m_vehicle_layout = ptr:add(0x00B0),
37+
m_vehicle_type = ptr:add(0x0340),
38+
m_wheel_scale = ptr:add(0x048C),
39+
m_wheel_scale_rear = ptr:add(0x0490),
40+
m_model_info_flags = ptr:add(0x057C),
4241
---@diagnostic disable-next-line: param-type-mismatch
4342
}, CVehicleModelInfo)
4443
end

0 commit comments

Comments
 (0)