Last active
October 4, 2024 19:01
-
-
Save thiemok/bb38634b60e6a240243ea488c73e17a1 to your computer and use it in GitHub Desktop.
Stormworks Scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upShiftPoint = property.getNumber("up_shift_point") | |
downShiftPoint = property.getNumber("down_shift_point") | |
numGears = property.getNumber("number_of_gears") | |
gearings = { | |
property.getNumber("base_gearing"), | |
property.getNumber("gearbox_1_gearing"), | |
property.getNumber("gearbox_2_gearing"), | |
property.getNumber("gearbox_3_gearing"), | |
property.getNumber("gearbox_4_gearing"), | |
property.getNumber("gearbox_5_gearing"), | |
property.getNumber("gearbox_6_gearing"), | |
property.getNumber("gearbox_7_gearing") | |
} | |
gearRanges = {} | |
for i = 1, numGears, 1 do | |
local hi = upShiftPoint | |
local low = downShiftPoint | |
local g = gearings[i] | |
if i > 1 then | |
low = gearRanges[i - 1][1] | |
hi = gearRanges[i - 1][2] | |
end | |
gearRanges[i] = { low * g, hi * g } | |
end | |
currentGear = 0 | |
awaitLockout = false | |
function onTick() | |
shifted = doLogic() | |
output.setBool(1, shifted) | |
output.setNumber(2, currentGear) | |
end | |
function doLogic() | |
local rps = input.getNumber(1) | |
local dRps = input.getNumber(2) | |
local w = input.getNumber(3) | |
local manualShiftUp = input.getBool(9) | |
local manualShiftDown = input.getBool(10) | |
local shiftLockout = input.getBool(11) | |
local disableAutomatic = input.getBool(12) | |
if awaitLockout == true and shiftLockout == true then | |
awaitLockout = false | |
end | |
if manualShiftDown == true then | |
currentGear = math.max(currentGear - 1, -1) | |
awaitLockout = true | |
return true | |
end | |
if manualShiftUp == true then | |
currentGear = math.min(currentGear + 1, numGears) | |
awaitLockout = true | |
return true | |
end | |
if shiftLockout == true or awaitLockout == true then | |
return false | |
end | |
if disableAutomatic == false and shouldShift(rps) == true then | |
currentGear = findCorrectGear(rps) | |
awaitLockout = true | |
return true | |
end | |
return false | |
end | |
function shouldShift(rps) | |
if currentGear <= 0 then | |
return false | |
end | |
range = gearRanges[currentGear] | |
if currentGear == numGears and rps >= range[2] then | |
return false | |
end | |
return rps <= range[1] or rps >= range[2] | |
end | |
function findCorrectGear(rps) | |
for gear, range in ipairs(gearRanges) do | |
if rps > range[1] and rps < range[2] then | |
return gear | |
end | |
end | |
return 1 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foregroundR = property.getNumber("foregroundR") | |
foregroundG = property.getNumber("foregroundG") | |
foregroundB = property.getNumber("foregroundB") | |
backgroundR = property.getNumber("backgroundR") | |
backgroundG = property.getNumber("backgroundG") | |
backgroundB = property.getNumber("backgroundB") | |
lidarR = property.getNumber("lidarR") | |
lidarG = property.getNumber("lidarG") | |
lidarB = property.getNumber("lidarB") | |
range = 0 | |
fov = 0 | |
yaw = 0 | |
targets = {} | |
lidarM = {} | |
function onTick() | |
local r = input.getNumber(12) | |
local f = input.getNumber(13) | |
if string.format("%f", r) ~= string.format("%f", range) or string.format("%f", f) ~= string.format("%f", fov) then | |
targets = {} | |
lidarM = {} | |
end | |
range = r | |
fov = f | |
doStep() | |
end | |
function doStep() | |
doRadarStep() | |
doLidarStep() | |
prepareNextTick() | |
end | |
function doRadarStep() | |
local hasTarget = input.getBool(7) | |
if hasTarget == true then | |
local distance = input.getNumber(10) | |
if distance <= range then | |
local target = {} | |
target["rssi"] = input.getNumber(8) | |
target["distance"] = distance | |
target["angle"] = input.getNumber(9) | |
targets[string.format("%f", yaw)] = target | |
else | |
targets[string.format("%f", yaw)] = nil | |
end | |
else | |
targets[string.format("%f", yaw)] = nil | |
end | |
end | |
function doLidarStep() | |
local distance = input.getNumber(15) | |
local yaw = input.getNumber(14) | |
local m = nil | |
if distance <= range then | |
m = distance | |
end | |
lidarM[string.format("%f", yaw)] = m | |
end | |
function prepareNextTick() | |
output.setNumber(2, fov) | |
yaw = input.getNumber(14) | |
end | |
function onDraw() | |
local width = screen.getWidth() | |
local height = screen.getHeight() | |
screenW = width | |
screenH = height | |
local centerX = width / 2 | |
local centerY = height / 2 | |
drawScreen(centerX, centerY, width * 0.7, height * 0.7) | |
end | |
function drawScreen(centerX, centerY, width, height) | |
local radius = (width + height) / 4 | |
screen.setColor(backgroundR, backgroundG, backgroundB) | |
screen.drawCircleF(centerX, centerY, radius) | |
drawLidar(centerX, centerY, radius - 1) | |
drawRadar(centerX, centerY, radius - 1) | |
screen.setColor(foregroundR, foregroundG, foregroundB) | |
screen.drawCircle(centerX, centerY, radius) | |
end | |
function drawRadar(centerX, centerY, radius) | |
drawRadarBeam(centerX, centerY, radius, yaw, fov) | |
for targetYaw, targetData in pairs(targets) do | |
drawRadarTarget( | |
centerX, | |
centerY, | |
radius, | |
fov, | |
targetYaw, | |
targetData | |
) | |
end | |
end | |
function drawLidar(centerX, centerY, radius) | |
screen.setColor(lidarR, lidarG, lidarB) | |
for mYaw, mDist in pairs(lidarM) do | |
local r = scale(mDist, range, radius) | |
local angle = turnsToRadians(mYaw) | |
local p1 = pointOnCircle(centerX, centerY, r, angle) | |
local p2 = pointOnCircle(centerX, centerY, radius, angle) | |
local p3 = pointOnCircle(centerX, centerY, radius, angle - turnsToRadians(fov)) | |
screen.drawTriangleF(p1[1], p1[2], p2[1], p2[2], p3[1], p3[2]) | |
end | |
end | |
function drawRadarBeam(centerX, centerY, radius, beamTurns, beamWidth) | |
screen.setColor(foregroundR, foregroundG, foregroundB) | |
local angle1 = turnsToRadians(beamTurns) | |
local angle2 = turnsToRadians(beamTurns - beamWidth) | |
local beamPoint1 = pointOnCircle( | |
centerX, | |
centerY, | |
radius, | |
angle1 | |
) | |
local beamPoint2 = pointOnCircle( | |
centerX, | |
centerY, | |
radius, | |
angle2 | |
) | |
screen.drawTriangleF(centerX, centerY, beamPoint1[1], beamPoint1[2], beamPoint2[1], beamPoint2[2]) | |
end | |
function drawRadarTarget(centerX, centerY, radius, beamWidth, turns, data) | |
local r = scale(data["distance"], range, radius) | |
local angle1 = turnsToRadians(turns) | |
local angle2 = turnsToRadians(turns - beamWidth) | |
local p = pointOnCircle( | |
centerX, | |
centerY, | |
r, | |
(angle1 + angle2) / 2 | |
) | |
screen.setColor(0, 255, 0) | |
screen.drawCircleF(p[1], p[2], 1) | |
end | |
function turnsToRadians(t) | |
return t * (2 * math.pi) | |
end | |
function pointOnCircle(centerX, centerY, radius, angle) | |
local x = centerX + radius * math.cos(angle) | |
local y = centerY + radius * math.sin(angle) | |
return {x, y} | |
end | |
function scale(v, vMax, scaleMax) | |
return v * (scaleMax / vMax) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment