Stormworks: Build and Rescue

Stormworks: Build and Rescue

Zizo Radar & Sonar System (Deprecated)
 This topic has been pinned, so it's probably important
Zizo  [developer] 21 Jul, 2020 @ 10:18am
LUA script (full length)
Hello, due to the 4096 characters limitation ingame, the lua script had to contain a lot of abbreviations and completely ignore any layout format.
Here is a more enjoyable script from an older version of this radar

This script may not be updated to the ingame one
-- settings values used at the first tick w=0 h=0 zoom=17 --zoom value at start -- abbreviations list (not used in this version) s=screen mc=math.cos ms=math.sin mp=math.pi ma=math.abs mq=math.sqrt mstm=map.screenToMap mmts=map.mapToScreen drf=screen.drawRectF dtb=screen.drawTextBox dt=screen.drawText dtf=screen.drawTriangleF sc=screen.setColor iN=input.getNumber iB=input.getBool pB=property.getBool pN=property.getNumber oN=output.setNumber oB=output.setBool f=false u=true -- getting the settings radarRange = property.getNumber("Radar Type") radarSpeed = property.getNumber("Radar Speed") radarDefinition = 45 / radarSpeed if property.getBool("Buzzer") then radarMute = true end -- each tick calculations function onTick() --getting the inputs touch2 = input.getBool(1) --touch boolean (long press sensitive) touch = input.getBool(2) --touch boolean (just a 1 tick pulse every touch) touchX = input.getNumber(3) --x coords in pixel where the touch input is touchY = input.getNumber(4) --y coords in pixel where the touch input is gpsX = input.getNumber(5) gpsY = input.getNumber(6) angle = (input.getNumber(7) * (-1)) + 0.25 --change this "0.25" depending on how you install the radar (facing) radarAngle = input.getNumber(8) - input.getNumber(7) radarDistance = input.getNumber(9) radarHeight = input.getNumber(10) radarElevationAngle = input.getNumber(11) -- target height from sealevel calculation targetHeight = ((math.tan(radarElevationAngle * math.pi * 2))* radarDistance) + radarHeight --adapting the zoom speed for a smooter feeling zoomSpeed = zoom / 170 if zoom > 0 and zoomDecrease then zoom = zoom - zoomSpeed elseif zoom < 50 and zoomIncrease then zoom = zoom + zoomSpeed end --listening to the touch inputs on the possible buttons and on the map if isPointInRectangle(touchX, touchY, w - 6, h - 34,7,7) and touch2 then --zoom business zoomIncrease = false zoomDecrease = true elseif isPointInRectangle(touchX, touchY, w - 6, h - 26, 7, 7) and touch2 then zoomIncrease = true zoomDecrease = false else zoomIncrease = false zoomDecrease = false end if isPointInRectangle(touchX, touchY, w - 8, h - 19, 7, 7) and touch2 then resetWaypoint = true else resetWaypoint = false end if isPointInRectangle(touchX, touchY, w - 17, h - 19, 7, 7) and touch then gpsToggle = true else gpsToggle = false end if isPointInRectangle(touchX, touchY, w - 17, h - 10, 7, 7) and touch then Targets=nil if radarToggle then radarToggle = false else radarToggle = true end end if isPointInRectangle(touchX, touchY, w - 8, h - 10, 7, 7) and touch then if radarMute then radarMute = false else radarMute = true end end if isPointInRectangle(touchX, touchY, 0, 12, w - 23, h) and touch then touchInMap = true else touchInMap = false end if input.getBool(3) then gpsToggle = true waypoint = true end if touchInMap then waypointX, waypointY = map.screenToMap(mapX, mapY, zoom, w, h, touchX, touchY) waypoint = true waypointXOutput, waypointYOutput = waypointX, waypointY elseif gpsToggle and waypoint then waypoint = false elseif gpsToggle and not waypoint and waypointX ~= nil then waypoint = true end if resetWaypoint then waypointX, waypointY = nil waypoint = false mapX = gpsX mapY = gpsY end if waypoint then mapX, mapY = waypointX, waypointY else mapX = gpsX mapY = gpsY end toLineX, toLineY = map.mapToScreen(mapX, mapY, zoom, w, h, waypointX, waypointY) x, y = map.mapToScreen(mapX, mapY, zoom, w, h, gpsX, gpsY) -- calculating all the radar screen coords to draw it if radarToggle then outputSpeed = radarSpeed radarRealAngle = (radarAngle * math.pi * 2) - (angle * math.pi * 2) radarX1 = gpsX + radarRange * math.cos(radarRealAngle) radarY1 = gpsY + radarRange * math.sin(radarRealAngle) radarX11, radarY11 = map.mapToScreen(mapX, mapY, zoom, w, h, radarX1, radarY1) radarX2 = gpsX + radarRange * math.cos(radarRealAngle + 0.15) radarY2 = gpsY + radarRange * math.sin (radarRealAngle + 0.15) radarX21, radarY21 = map.mapToScreen(mapX, mapY, zoom, w, h, radarX2, radarY2) radarX3 = gpsX + radarRange * math.cos(radarRealAngle + 0.3) radarY3 = gpsY + radarRange * math.sin(radarRealAngle + 0.3) radarX31, radarY31 = map.mapToScreen(mapX, mapY, zoom, w, h, radarX3, radarY3) radarX4 = gpsX + radarRange * math.cos(radarRealAngle + 0.45) radarY4 = gpsY + radarRange * math.sin(radarRealAngle + 0.45) radarX41, radarY41 = map.mapToScreen(mapX, mapY, zoom, w, h, radarX4, radarY4) --detection counting if Targets==nil then Targets={} count=0 end count = count + 1 if count - radarDefinition >= 10 then count = 0 end -- saving the detection informations in an array if radarDistance == 0 then Targets[count] = {q ='e'} -- only save if the detection is a target else tosaveX = gpsX + radarDistance * math.cos(radarRealAngle) tosaveY = gpsY + radarDistance * math.sin(radarRealAngle) Targets[count] = {q = tosaveX, w = tosaveY, z = targetHeight} end else outputSpeed = 0 end --setting outputs output.setBool(1, radarMute) output.setBool(2, radarToggle) output.setNumber(1, radarSpeed) output.setNumber(2, outputSpeed) output.setNumber(5, waypointXOutput) output.setNumber(6, waypointYOutput) end -- takes care of drawing all the stuff function onDraw() w = screen.getWidth() h = screen.getHeight() screen.setMapColorOcean(10, 10, 10) screen.setMapColorShallows(15, 15, 15) screen.setMapColorLand(50, 50, 50) screen.setMapColorGrass(70, 70, 70) screen.setMapColorSand(60, 60, 60) screen.setMapColorSnow(80, 80, 80) screen.drawMap(mapX, mapY, zoom) if waypointX ~= nil then screen.setColor(250, 234, 0) screen.drawText(5, 7, 'Dist:' ..round(math.sqrt((math.abs(waypointX - gpsX) ^ 2) + (math.abs(waypointY - gpsY) ^ 2)), 0).. 'm') screen.drawLine(x, y, toLineX, toLineY) end --calls a custim function to draw the position drawPosition(x, y, angle) radarCenterX, radarCenterY = map.screenToMap(mapX, mapY, zoom, w, h, x, y) if radarToggle then screen.setColor(0, 255, 0, 10) screen.drawCircle(x, y, math.sqrt((math.abs(x - radarX11) ^ 2) + (math.abs(y - radarY11) ^ 2))) screen.setColor(0, 255, 0, 50) screen.drawTriangleF(x, y, radarX21, radarY21, radarX11, radarY11) screen.setColor(0, 255, 0, 20) screen.drawTriangleF(x, y, radarX21, radarY21, radarX31, radarY31) screen.setColor(0, 255, 0, 10) screen.drawTriangleF(x, y, radarX41, radarY41, radarX31, radarY31) screen.setColor(0, 255, 0) screen.drawLine(x, y, radarX11, radarY11) drawObjects() end screen.setColor(0, 0, 0) screen.drawRectF(-1, -1, w - 17, 7) screen.drawRectF(w-18, 0, 20, h) screen.setColor(255, 255, 255) screen.drawText(5, h-5, 50 - round(z, 1).. 'x') screen.drawText(5, 0, 'GPS:' ..round(gpsX, 1).. ', ' ..round(gpsY, 1)) screen.drawTriangle(w - 9, 0, w - 15, 15, w - 3, 15) screen.setColor(250, 234, 0) screen.drawCircle(w - 9, 9, 9) screen.setColor(255, 0, 0) screen.drawText(w - 11, 9, 'N') screen.setColor(255, 255, 255, 10) --draw button backgrounds when pressed if resetWaypoint then screen.drawRectF(w - 8, h - 19, 7, 7) end if gpsToggle then screen.drawRectF(w - 17, h - 19, 7, 7) end if radarMute then screen.drawRectF(w - 8, h - 10, 7, 7) end if radarToggle then screen.drawRectF(w - 17, h - 10, 7, 7) end if day then screen.drawRectF(w - 17, h - 28, 7, 7) end --draw buttons themself screen.setColor(255, 255, 255) screen.drawTextBox(w - 6, h - 34, 7, 7, '+', 0, 0) screen.drawTextBox(w - 6, h - 26, 7, 7, '-', 0, 0) screen.setColor(250, 234, 0) screen.drawTextBox(w - 8, h - 19, 7, 7, 'W', 0, 0) screen.setColor(255, 0, 0) screen.drawTextBox(w - 17, h - 19, 7, 7, 'C', 0, 0) screen.setColor(0, 255, 0) screen.drawTextBox(w - 17, h - 10, 7, 7, 'R', 0, 0) screen.setColor(255, 255, 255) screen.drawTextBox(w - 8, h - 10, 7, 7, 'S', 0 , 0) end function isPointInRectangle(x, y, rectX, rectY, rectW, rectH) return x > rectX and y > rectY and x < rectX + rectW and y < rectY + rectH end function round(num,nDP) local mu=10 ^ (nDP or 0) return math.floor(num * mu + 0.5) / mu end function drawPosition(x,y,a) if not isPointInRectangle(x, y, 0, 12, w - 18, h) then --if the gps location is not on screen, draws the position just to show direction centerX = w - 9 centerY = 27 screen.setColor(255, 255, 255) screen.drawCircle(centerX, centerY, 9) x = centerX y = centerY end boatRealAngle = (angle * math.pi * 2) x1 = x + (-5) * math.cos(boatRealAngle) y1 = y + (-5) * math.sin(boatRealAngle) x2 = x + (-5) * math.cos(boatRealAngle - 10) y2 = y + (-5) * math.sin(boatRealAngle - 10) x3 = x + (-5) * math.cos(boatRealAngle + 10) y3 = y + (-5) * math.sin(boatRealAngle + 10) screen.setColor(255, 255, 255) screen.drawTriangleF(x1, y1, x2, y2, x3, y3) end function drawObjects() for i,oc in pairs(Targets)do if oc.q ~= 'e' then objectCoordinateX, objectCoordinateY = map.mapToScreen(mapX, mapY, zoom, w, h, oc.q, oc.w) alpha = 255 - (count - i) if oc.z > 0 then screen.setColor(255, 0, 0, alpha) else screen.setColor(0, 0, 255, alpha) end screen.drawCircleF(objectCoordinateX, objectCoordinateY, 1) end end end
Last edited by Zizo; 17 Jan, 2021 @ 7:10pm