diff --git a/koptilnya/libs/constants.txt b/koptilnya/libs/constants.txt new file mode 100644 index 0000000..fee1b0f --- /dev/null +++ b/koptilnya/libs/constants.txt @@ -0,0 +1,3 @@ +NULL_ENTITY = entity(0) +CURRENT_PLAYER = player() +OWNER = owner() \ No newline at end of file diff --git a/koptilnya/libs/table.txt b/koptilnya/libs/table.txt index 66af9b1..6e0e583 100644 --- a/koptilnya/libs/table.txt +++ b/koptilnya/libs/table.txt @@ -20,6 +20,31 @@ function table.chunk(tbl, size) return result end +function table.deepcopy(orig) + local orig_type = type(orig) + local copy + if orig_type == 'table' then + copy = {} + for orig_key, orig_value in next, orig, nil do + copy[table.deepcopy(orig_key)] = table.deepcopy(orig_value) + end + setmetatable(copy, table.deepcopy(getmetatable(orig))) + else -- number, string, boolean, etc + copy = orig + end + 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 diff --git a/koptilnya/steering/input.txt b/koptilnya/steering/input.txt new file mode 100644 index 0000000..c3dfd9e --- /dev/null +++ b/koptilnya/steering/input.txt @@ -0,0 +1,5 @@ +Input = class("Input") + +function Input:initialize(keyMap) + +end diff --git a/koptilnya/steering/main.txt b/koptilnya/steering/main.txt new file mode 100644 index 0000000..b0188e4 --- /dev/null +++ b/koptilnya/steering/main.txt @@ -0,0 +1,74 @@ +-- @name Steering +-- @author DarkSupah +-- @server +-- @include ./steering_controller.txt +-- @include ./steer_axle.txt +-- @include ../libs/constants.txt +-- @include ../libs/table.txt +require("./steering_controller.txt") +require("./steer_axle.txt") +require("../libs/constants.txt") +require("../libs/table.txt") + +local frontConfig = { + Camber = -5, + Caster = 5, + Ackermann = 1.1, + Lock = 50, + CorrectionOn = 0.8, + CorrectionOff = 0.2 +} +local rearConfig = { + Camber = -5, + Caster = -5, + Ackermann = 1.2, + Lock = 5, + CorrectionOn = 0.8, + CorrectionOff = 0.2 +} + +-- Fucking slaves, get you ass back here +local _slaves = { + E1 = "entity", + E2 = "entity", + E3 = "entity", + E4 = "entity" +} + +local _axles = {SteerAxle:new(frontConfig, wire.ports.E1, wire.ports.E2), + SteerAxle:new(rearConfig, wire.ports.E3, wire.ports.E4)} + +local _inputs = { + Base = "entity", + Seat = "entity" +} + +local _outputs = { + SteerNormalized = "number", + Driver = "entity" +} + +local _allInputs = table.merge(_inputs, _slaves) + +wire.adjustPorts(_allInputs, _outputs) + +local steeringController = SteeringController:new(wire.ports.Base) + +hook.add("PlayerEnteredVehicle", "onEnter", function(ply, veh) + if veh == steeringController.seat then + steeringController:setDriver(ply) + end +end) + +hook.add("PlayerLeaveVehicle", "onLeave", function(ply, veh) + if veh == steeringController.seat then + steeringController:setDriver(NULL_ENTITY) + end +end) + +hook.add("think", "update", function() + steeringController.seat = wire.ports.Seat + wire.ports.Driver = steeringController.driver + + steeringController:update() +end) diff --git a/koptilnya/steering/steer_axle.txt b/koptilnya/steering/steer_axle.txt new file mode 100644 index 0000000..842df67 --- /dev/null +++ b/koptilnya/steering/steer_axle.txt @@ -0,0 +1,23 @@ +SteerAxle = class("SteerAxle") + +function SteerAxle:initialize(config, leftWheel, rightWheel) + self.steer = 0 + + self.config = config + self.leftWheel = leftWheel + self.rightWheel = rightWheel +end + +function SteerAxle:update() + self:_updateSlave(self.leftWheel) + self:_updateSlave(self.rightWheel) +end + +function SteerAxle:_updateSlave(slave) + if slave:isValid() and not slave:isPlayerHolding() then + slave:setAngles(base:localToWorldAngles(Angle(0, 0, 0))) + if not slave:isFrozen() then + slave:setFrozen() + end + end +end diff --git a/koptilnya/steering/steering_controller.txt b/koptilnya/steering/steering_controller.txt new file mode 100644 index 0000000..bcdd279 --- /dev/null +++ b/koptilnya/steering/steering_controller.txt @@ -0,0 +1,19 @@ +-- @include ../libs/constants.txt +require("../libs/constants.txt") + +SteeringController = class("SteeringController") + +function SteeringController:initialize(base) + self.seat = NULL_ENTITY + self.driver = NULL_ENTITY + + self.base = base +end + +function SteeringController:update() + +end + +function SteeringController:setDriver(ply) + self.driver = ply +end