Насрал типами, но еще не до конца

This commit is contained in:
Никита Круглицкий
2025-05-16 07:11:48 +06:00
parent 9b50dbe33d
commit 84f591db59
18 changed files with 1629 additions and 1217 deletions

View File

@@ -5,66 +5,68 @@ local PowertrainComponent = require('../powertrain_component.txt')
local Gearbox = class('Gearbox', PowertrainComponent)
function Gearbox:initialize(vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config)
PowertrainComponent.initialize(self, vehicle, name, config)
if CLIENT then return end
if CLIENT then
return
end
self.wireInputs = {
Upshift = 'number',
Downshift = 'number'
}
self.wireOutputs = {
[string.format('%s_RPM', self.name)] = 'number',
[string.format('%s_Torque', self.name)] = 'number',
[string.format('%s_Ratio', self.name)] = 'number'
}
self.wireInputs = {
Upshift = 'number',
Downshift = 'number',
}
self.wireOutputs = {
[string.format('%s_RPM', self.name)] = 'number',
[string.format('%s_Torque', self.name)] = 'number',
[string.format('%s_Ratio', self.name)] = 'number',
}
self.type = config.Type or 'MANUAL'
self.inertia = config.Inertia or 1000
self.type = config.Type or 'MANUAL'
self.inertia = config.Inertia or 1000
self.ratio = 0
self.ratio = 0
end
function Gearbox:updateWireOutputs()
PowertrainComponent.updateWireOutputs(self)
wire.ports[string.format('%s_RPM', self.name)] = self:getRPM()
wire.ports[string.format('%s_Torque', self.name)] = self.torque
wire.ports[string.format('%s_Ratio', self.name)] = self.ratio
PowertrainComponent.updateWireOutputs(self)
wire.ports[string.format('%s_RPM', self.name)] = self:getRPM()
wire.ports[string.format('%s_Torque', self.name)] = self.torque
wire.ports[string.format('%s_Ratio', self.name)] = self.ratio
end
function Gearbox:queryInertia()
if self.output == nil or self.ratio == 0 then
return self.inertia
end
if self.output == nil or self.ratio == 0 then
return self.inertia
end
return self.inertia + self.output:queryInertia() / math.pow(self.ratio, 2)
return self.inertia + self.output:queryInertia() / math.pow(self.ratio, 2)
end
function Gearbox:queryAngularVelocity(angularVelocity)
self.angularVelocity = angularVelocity
self.angularVelocity = angularVelocity
if self.output == nil or self.ratio == 0 then
return angularVelocity
end
if self.output == nil or self.ratio == 0 then
return angularVelocity
end
return self.output:queryAngularVelocity(angularVelocity) * self.ratio
return self.output:queryAngularVelocity(angularVelocity) * self.ratio
end
function Gearbox:forwardStep(torque, inertia)
self.torque = torque * self.ratio
self.torque = torque * self.ratio
if self.output == nil then
return torque
end
if self.output == nil then
return torque
end
if self.ratio == 0 then
self.output:forwardStep(0, self.inertia * 0.5)
if self.ratio == 0 then
self.output:forwardStep(0, self.inertia * 0.5)
return torque
end
return torque
end
return self.output:forwardStep(self.torque, (inertia + self.inertia) * math.pow(self.ratio, 2)) / self.ratio
return self.output:forwardStep(self.torque, (inertia + self.inertia) * math.pow(self.ratio, 2)) / self.ratio
end
return Gearbox

View File

@@ -10,74 +10,78 @@ require('/koptilnya/libs/utils.txt')
local ManualGearbox = class('ManualGearbox', BaseGearbox)
function ManualGearbox:initialize(vehicle, name, config)
BaseGearbox.initialize(self, vehicle, name, config)
BaseGearbox.initialize(self, vehicle, name, config)
if CLIENT then return end
if CLIENT then
return
end
table.merge(self.wireOutputs, {
[string.format('%s_Gear', self.name)] = 'number'
})
table.merge(self.wireOutputs, {
[string.format('%s_Gear', self.name)] = 'number',
})
self.ratios = config.Ratios or { 3.6, 2.2, 1.5, 1.2, 1.0, 0.8}
self.reverse = config.Reverse or 3.4
self.ratios = config.Ratios or { 3.6, 2.2, 1.5, 1.2, 1.0, 0.8 }
self.reverse = config.Reverse or 3.4
self.gear = 0
self.gear = 0
function shiftFunc()
if wire.ports.Clutch == 0 then return 0 end
function shiftFunc()
if wire.ports.Clutch == 0 then
return 0
end
local upshift = wire.ports.Upshift or 0
local downshift = wire.ports.Downshift or 0
local upshift = wire.ports.Upshift or 0
local downshift = wire.ports.Downshift or 0
return upshift - downshift
end
return upshift - downshift
end
self.shiftWatcher = watcher(shiftFunc, function(val)
if val ~= 0 then
self:shift(val)
end
end)
self.shiftWatcher = watcher(shiftFunc, function(val)
if val ~= 0 then
self:shift(val)
end
end)
self:recalcRatio()
self:recalcRatio()
end
function ManualGearbox:updateWireOutputs()
BaseGearbox.updateWireOutputs(self)
BaseGearbox.updateWireOutputs(self)
wire.ports[string.format('%s_Gear', self.name)] = self.gear
wire.ports[string.format('%s_Gear', self.name)] = self.gear
end
function ManualGearbox:setGear(gear)
if gear >= -1 and gear <= #self.ratios then
self.gear = gear
self:recalcRatio()
if gear >= -1 and gear <= #self.ratios then
self.gear = gear
self:recalcRatio()
net.start('GEARBOX_GEAR')
net.writeInt(gear, 5)
net.send(self.vehicle.playersConnectedToHUD, true)
end
net.start('GEARBOX_GEAR')
net.writeInt(gear, 5)
net.send(self.vehicle.playersConnectedToHUD, true)
end
end
function ManualGearbox:recalcRatio()
if self.gear == -1 then
self.ratio = -self.reverse
elseif self.gear == 0 then
self.ratio = 0
else
self.ratio = self.ratios[self.gear]
end
if self.gear == -1 then
self.ratio = -self.reverse
elseif self.gear == 0 then
self.ratio = 0
else
self.ratio = self.ratios[self.gear]
end
end
function ManualGearbox:shift(dir)
self:setGear(self.gear + dir)
self:setGear(self.gear + dir)
end
function ManualGearbox:forwardStep(torque, inertia)
self.shiftWatcher()
local result = BaseGearbox.forwardStep(self, torque, inertia)
self.shiftWatcher()
return result
local result = BaseGearbox.forwardStep(self, torque, inertia)
return result
end
return ManualGearbox