diff --git a/koptilnya/engine/eng_comf_config.txt b/koptilnya/engine/eng_comf_config.txt new file mode 100644 index 0000000..dca0e1f --- /dev/null +++ b/koptilnya/engine/eng_comf_config.txt @@ -0,0 +1,72 @@ +config = { + Name = "Jopa", + Engine = { + IdleRPM = 900, + MaxRPM = 7000, + FlywheelMass = 4.9, + FlywheelRadius = 0.378, + StartFriction = -50, + FrictionCoeff = 0.02, + LimiterDuration = 0.05, + TorqueMap = {240.29618749947, 251.33093929699, 262.3656910945, 273.40044289202, 284.43519468953, + 295.46994648705, 306.50469828456, 317.53945008208, 328.57420187959, 339.60895367711, + 350.64370547463, 361.67845727214, 372.71320906966, 383.74796086717, 394.78271266469, + 405.8174644622, 416.85221625972, 427.88696805724, 438.92171985475, 449.95647165227, 449.9557104607, + 449.95494926913, 449.95418807757, 449.953426886, 449.95266569443, 449.95190450287, 449.9511433113, + 449.95038211973, 449.94962092816, 449.9488597366, 449.94809854503, 449.94733735346, 449.9465761619, + 449.94581497033, 449.94505377876, 449.9442925872, 449.94353139563, 449.94277020406, + 449.94200901249, 449.94124782093, 449.94048662936, 449.93972543779, 449.93896424623, + 449.93820305466, 449.93744186309, 449.93668067153, 449.93591947996, 449.93515828839, + 449.93439709682, 449.93363590526, 449.93287471369, 449.93211352212, 449.93135233056, + 449.93059113899, 449.92982994742, 449.92906875586, 449.92830756429, 449.92754637272, + 449.92678518115, 449.92602398959, 449.92526279802, 449.92450160645, 449.92374041489, + 449.92297922332, 449.92221803175, 449.92145684019, 449.92069564862, 449.91993445705, + 449.91917326548, 449.91841207392, 449.91765088235, 449.91688969078, 449.91612849922, + 446.34504740794, 442.77396631665, 439.20288522537, 435.63180413409, 432.06072304281, + 428.48964195153, 424.91856086025, 421.34747976897, 417.77639867769, 414.2053175864, + 410.63423649512, 407.06315540384, 403.49207431256, 399.92099322128, 396.34991213, 392.77883103872, + 389.20774994744, 385.63666885615, 382.06558776487, 378.49450667359, 374.92342558231, + 371.35234449103, 367.78126339975, 364.21018230847, 360.63910121719, 357.0680201259, + 353.49693903462, 349.92585794334} + }, + Clutch = { + Stiffness = 22, + Damping = 0.8, + MaxTorque = 600 + }, + Gearbox = { + Type = "AUTO", + ShiftDuration = 0.2, + ShiftSmoothness = 0.3, + Ratios = {13.45, 8.12, 5.51, 4.16, 3.36, 2.83}, + Reverse = 14.41 + }, + Axles = {{ + Power = 1, + Coast = 1, + Preload = 10, + UsePowerBias = 10, + ViscousCoeff = 0.96, + Axle = Vector(1, 0, 0), + DistributionCoeff = 0.4, + FinalDrive = 1 + }, { + Power = 1, + Coast = 1, + Preload = 10, + UsePowerBias = 10, + ViscousCoeff = 0.96, + Axle = Vector(1, 0, 0), + DistributionCoeff = 0.6, + FinalDrive = 1 + }}, + Systems = {{ + Type = "LAUNCH", + RPMLimit = 3500 + }, { + Type = "TRACTION", + Slip = 1.5 + }} +} + +return config diff --git a/koptilnya/engine_remastered/builders/gearbox.txt b/koptilnya/engine_remastered/builders/gearbox.txt new file mode 100644 index 0000000..e69de29 diff --git a/koptilnya/engine_remastered/builders/systems.txt b/koptilnya/engine_remastered/builders/systems.txt new file mode 100644 index 0000000..e69de29 diff --git a/koptilnya/engine_remastered/configs/sample.txt b/koptilnya/engine_remastered/configs/sample.txt new file mode 100644 index 0000000..c7bf1a7 --- /dev/null +++ b/koptilnya/engine_remastered/configs/sample.txt @@ -0,0 +1,75 @@ +-- @name Sample vehicle config +-- @author Koptilnya1337 +-- @server +-- @include /koptilnya/engine_remastered/vehicle.txt +require('/koptilnya/engine_remastered/vehicle.txt') + +Vehicle:new({ + Engine = { + IdleRPM = 900, + MaxRPM = 7000, + FlywheelMass = 4.9, + FlywheelRadius = 0.378, + StartFriction = -50, + FrictionCoeff = 0.02, + LimiterDuration = 0.05, + TorqueMap = {240.29618749947, 251.33093929699, 262.3656910945, 273.40044289202, 284.43519468953, + 295.46994648705, 306.50469828456, 317.53945008208, 328.57420187959, 339.60895367711, + 350.64370547463, 361.67845727214, 372.71320906966, 383.74796086717, 394.78271266469, + 405.8174644622, 416.85221625972, 427.88696805724, 438.92171985475, 449.95647165227, 449.9557104607, + 449.95494926913, 449.95418807757, 449.953426886, 449.95266569443, 449.95190450287, 449.9511433113, + 449.95038211973, 449.94962092816, 449.9488597366, 449.94809854503, 449.94733735346, 449.9465761619, + 449.94581497033, 449.94505377876, 449.9442925872, 449.94353139563, 449.94277020406, + 449.94200901249, 449.94124782093, 449.94048662936, 449.93972543779, 449.93896424623, + 449.93820305466, 449.93744186309, 449.93668067153, 449.93591947996, 449.93515828839, + 449.93439709682, 449.93363590526, 449.93287471369, 449.93211352212, 449.93135233056, + 449.93059113899, 449.92982994742, 449.92906875586, 449.92830756429, 449.92754637272, + 449.92678518115, 449.92602398959, 449.92526279802, 449.92450160645, 449.92374041489, + 449.92297922332, 449.92221803175, 449.92145684019, 449.92069564862, 449.91993445705, + 449.91917326548, 449.91841207392, 449.91765088235, 449.91688969078, 449.91612849922, + 446.34504740794, 442.77396631665, 439.20288522537, 435.63180413409, 432.06072304281, + 428.48964195153, 424.91856086025, 421.34747976897, 417.77639867769, 414.2053175864, + 410.63423649512, 407.06315540384, 403.49207431256, 399.92099322128, 396.34991213, 392.77883103872, + 389.20774994744, 385.63666885615, 382.06558776487, 378.49450667359, 374.92342558231, + 371.35234449103, 367.78126339975, 364.21018230847, 360.63910121719, 357.0680201259, + 353.49693903462, 349.92585794334} + }, + Clutch = { + Stiffness = 22, + Damping = 0.8, + MaxTorque = 600 + }, + Gearbox = { + Type = "AUTO", + ShiftDuration = 0.2, + ShiftSmoothness = 0.3, + Ratios = {13.45, 8.12, 5.51, 4.16, 3.36, 2.83}, + Reverse = 14.41 + }, + Axles = {{ + Power = 1, + Coast = 1, + Preload = 10, + UsePowerBias = 10, + ViscousCoeff = 0.96, + Axle = Vector(1, 0, 0), + DistributionCoeff = 0.4, + FinalDrive = 1 + }, { + Power = 1, + Coast = 1, + Preload = 10, + UsePowerBias = 10, + ViscousCoeff = 0.96, + Axle = Vector(1, 0, 0), + DistributionCoeff = 0.6, + FinalDrive = 1 + }}, + Systems = {{ + Type = "LAUNCH", + Limit = 3500 + }, { + Type = "TRACTION", + Limit = 1.5 + }} +}) diff --git a/koptilnya/engine_remastered/engine.txt b/koptilnya/engine_remastered/engine.txt new file mode 100644 index 0000000..4b1d2b3 --- /dev/null +++ b/koptilnya/engine_remastered/engine.txt @@ -0,0 +1,86 @@ +-- @include ./wire_component.txt +-- @include /koptilnya/libs/constants.txt +require('./wire_component.txt') +require('/koptilnya/libs/constants.txt') + +Engine = class('Engine', WireComponent) + +function Engine:initialize(config, clutch) + self.idleRPM = config.IdleRPM + self.maxRPM = config.MaxRPM + + self.flywheelMass = config.FlywheelMass + self.flywheelRadius = config.FlywheelRadius + + self.startFriction = config.StartFriction + self.frictionCoeff = config.FrictionCoeff + + self.torque = 0 + self.rpmFrac = 0 + self.rpm = self.idleRPM + self.friction = 0 + + self.limiterDuration = config.LimiterDuration + + self.torqueMap = config.TorqueMap or {} + + self._fwInertia = self.flywheelMass * self.flywheelRadius ^ 2 / 2 + self._limiterTime = 0 + + self._clutch = clutch +end + +function Engine:getInputs() + return { + Throttle = 'number' + } +end + +function Engine:getOutputs() + return { + RPM = 'number', + Torque = 'number' + } +end + +function Engine:updateOutputs() + wire.ports.RPM = self.rpm + wire.ports.Torque = self.torque +end + +function Engine:getThrottle() + return wire.ports.Throttle +end + +function Engine:update() + local throttle = self:getThrottle() + + self.rpmFrac = math.clamp((self.rpm - self.idleRPM) / (self.maxRPM - self.idleRPM), 0, 1) + self.friction = self.startFriction - self.rpm * self.frictionCoeff + + local tqIdx = math.clamp(math.floor(self.rpmFrac * #self.torqueMap), 1, #self.torqueMap) + local maxInitialTorque = self.torqueMap[tqIdx] - self.friction + + local idleFadeStart = math.clamp(math.remap(self.rpm, self.idleRPM - 300, self.idleRPM, 1, 0), 0, 1) + local idleFadeEnd = math.clamp(math.remap(self.rpm, self.idleRPM, self.idleRPM + 600, 1, 0), 0, 1) + + local additionalEnergySupply = idleFadeEnd * (-self.friction / maxInitialTorque) + idleFadeStart + + if self.rpm > self.maxRPM then + throttle = 0 + self._limiterTime = timer.systime() + else + throttle = timer.systime() >= self._limiterTime + self.limiterDuration and throttle or 0 + end + + local masterThrottle = math.clamp(additionalEnergySupply + throttle, 0, 1) + + local realInitialTorque = maxInitialTorque * masterThrottle + + local loadTorque = self._clutch and self._clutch.torque or 0 + + self.torque = realInitialTorque + self.friction + + self.rpm = self.rpm + (self.torque - loadTorque) / self._fwInertia * RAD_TO_RPM * TICK_INTERVAL + self.rpm = math.max(self.rpm, 0) +end diff --git a/koptilnya/engine_remastered/vehicle.txt b/koptilnya/engine_remastered/vehicle.txt new file mode 100644 index 0000000..31289b6 --- /dev/null +++ b/koptilnya/engine_remastered/vehicle.txt @@ -0,0 +1,50 @@ +-- @include ./engine.txt +-- @include /koptilnya/libs/table.txt +require('./engine.txt') +require('/koptilnya/libs/table.txt') + +Vehicle = class('Vehicle') + +function Vehicle:initialize(config) + if config == nil then + throw('Vehicle config not provided') + end + + -- self.clutch = Clutch:new(config.Clutch) + self.engine = Engine:new(config.Engine, self.clutch) + -- self.gearbox = GearboxBuilder:create(config.Gearbox, self.clutch) + -- self.axles = table.map(config.Axles, function(config) + -- return Differential:new(config) + -- end) + -- self.systems = table.map(config.Systems, function(config) + -- return SystemsBuilder:create(config) + -- end) + + self.components = {self.engine} -- , self.gearbox, self.clutch} + -- self.components = table.add(self.components, self.axles) + -- self.components = table.add(self.components, self.systems) + + local inputs = {} + local outputs = {} + + for _, comp in ipairs(self.components) do + inputs = table.merge(inputs, comp:getInputs()) + outputs = table.merge(outputs, comp:getOutputs()) + end + + wire.adjustPorts(inputs, outputs) + + -- for _, ent in self.entities do + -- ent:createInputs() + -- ent:createOutputs() + -- end + + hook.add('tick', 'vehicle_update', function() + local outputs = {} + + for _, comp in pairs(self.components) do + comp:update() + comp:updateOutputs() + end + end) +end diff --git a/koptilnya/engine_remastered/wire_component.txt b/koptilnya/engine_remastered/wire_component.txt new file mode 100644 index 0000000..9f442c8 --- /dev/null +++ b/koptilnya/engine_remastered/wire_component.txt @@ -0,0 +1,12 @@ +WireComponent = class('WireComponent') + +function WireComponent:getInputs() + return {} +end + +function WireComponent:getOutputs() + return {} +end + +function WireComponent:updateOutputs() +end diff --git a/koptilnya/libs/table.txt b/koptilnya/libs/table.txt index 064db6b..d48056f 100644 --- a/koptilnya/libs/table.txt +++ b/koptilnya/libs/table.txt @@ -34,16 +34,6 @@ function table.deepcopy(orig) return copy end -function table.merge(table1, table2) - local newTable = table.deepcopy(table1) - - for k, v in pairs(table2) do - newTable[k] = v - end - - return newTable -end - function table.contains(tbl, ...) local argCount = #arg