Guest User

Untitled

a guest
May 14th, 2023
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.06 KB | None | 0 0
  1. --- Developed using LifeBoatAPI - Stormworks Lua plugin for VSCode - https://code.visualstudio.com/download (search "Stormworks Lua with LifeboatAPI" extension)
  2. --- If you have any issues, please report them here: https://github.com/nameouschangey/STORMWORKS_VSCodeExtension/issues - by Nameous Changey
  3. --- Remember to set your Author name etc. in the settings: CTRL+COMMA
  4.  
  5. require("_build._simulator_config") -- default simulator config, CTRL+CLICK it or F12 to goto this file and edit it. Its in a separate file just for convenience.
  6. require("LifeBoatAPI") -- Type 'LifeBoatAPI.' and use intellisense to checkout the new LifeBoatAPI library functions; such as the LBVec vector maths library
  7.  
  8. -- By Hendrik, please contact me if you need the non-minified source code
  9. function dist(x1, y1, x2, y2) return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2) end
  10.  
  11. function ease(a, b, t) return a + ((b - a) * math.sin((t * math.pi) / 2)) end
  12.  
  13. function getNumbers(...) local a = {} for b, c in ipairs({ ... }) do a[b] = input.getNumber(c) end  return table.unpack(a) end
  14.  
  15. function getBooleans(...) local a = {} for b, c in ipairs({ ... }) do a[b] = input.getBool(c) end  return table.unpack(a) end
  16.  
  17. function inRect(x, y, a, b, w, h) return x >= a and y >= b and x < a + w and y < b + h end
  18.  
  19.  
  20. function color(r, g, b, a)
  21.     a = a or 255
  22.     return {255*(0.8*r/255)^2.6, 255*(0.8*g/255)^2.6, 255*(0.8*b/255)^2.6, a}
  23. end
  24.  
  25.  
  26. -- colors: light, dark
  27. colors = {
  28.     -- text
  29.     {{240, 240, 240}, {200, 200, 200}},
  30.    
  31.     -- button normal
  32.     {color(120, 120, 153), color(120, 120, 153)},
  33.    
  34.     -- button active
  35.     {color(106, 106, 181), color(106, 106, 181)},
  36.    
  37.     -- waypoints normal
  38.     {color(219, 30, 30), color(183, 25, 25)},
  39.  
  40.     -- waypoints selected
  41.     {color(183, 25, 25), color(142, 19, 19)},
  42. }
  43.  
  44. function setPaint(index)
  45.     screen.setColor(table.unpack(colors[index][buttons[1][6] and 2 or 1]))
  46. end
  47.  
  48.  
  49. animationStep = 0
  50. ANIMATION_DURATION = 30
  51. mapX = 0
  52. mapY = 0
  53. mapStartX = 0
  54. mapStartY = 0
  55. mapTargetX = 0
  56. mapTargetY = 0
  57.  
  58. zoom = 1
  59. touchWasActive = false
  60. waypoints = {}
  61. distance = 0
  62. focusedWaypoint = 0
  63.  
  64. -- label, x, y, w, type[0=hold, 1=click, 2=toggle], value
  65. buttons = {
  66.     {"DRK", 1, 1, 16,      2, false},
  67.     {"FLW", 1, 9, 16,      2, true},
  68.     {"+",   1, 17, 7,      0, false},
  69.     {"-",   10, 17, 7,     0, false},
  70.     {"ADD", -17, 9, 16,    1, false},
  71.     {"DEL", -17, 17, 16,   1, false},
  72.     {"AP", -17, 25, 16,    2, false},
  73.  
  74.     {"<", -17, 1, 7,       1, false},
  75.     {">", -8, 1, 7,      1, false},
  76. }
  77.  
  78.  
  79. function onTick()
  80.     animationStep = math.min(animationStep + 1, ANIMATION_DURATION)
  81.     screenWidth, screenHeight, touchX, touchY, gpsx, gpsy, heading, keypadA, keypadB = getNumbers(1, 2, 3, 4, 7, 8, 9, 10, 11)
  82.     heading = heading * -1
  83.     mapX = ease(mapStartX, mapTargetX, animationStep / ANIMATION_DURATION)
  84.     mapY = ease(mapStartY, mapTargetY, animationStep / ANIMATION_DURATION)
  85.     touchActive, doRemoveCurrentWaypoint, keypadPulse = getBooleans(1, 3, 4)
  86.     touchDown = touchActive and not touchWasActive
  87.     touchWasActive = touchActive
  88.  
  89.     -- Output next waypoint if AP enabled and there is a waypoint
  90.     output.setBool(1, buttons[7][6])
  91.     if buttons[7][6] then
  92.         -- Disable AP when no more waypoints
  93.         if #waypoints == 0 then
  94.             buttons[7][6] = false
  95.         else
  96.             output.setNumber(1, waypoints[1][1])
  97.             output.setNumber(2, waypoints[1][2])
  98.             -- Remove first waypoint if reached (< 100m)
  99.             if dist(gpsx, gpsy, waypoints[1][1], waypoints[1][2]) < 100 then
  100.                 doRemoveCurrentWaypoint = true
  101.             end
  102.         end
  103.     end
  104.  
  105.     -- Handle button touch
  106.     for i, b in ipairs(buttons) do
  107.         if b[5] ~= 2 then
  108.             b[6] = false
  109.         end
  110.         bx = b[2] > 0 and b[2] or (screenWidth + b[2])
  111.         if touchActive and inRect(touchX, touchY, bx, b[3], b[4], 7) then
  112.             if b[5] == 2 then
  113.                 if touchDown then
  114.                     b[6] = not b[6]
  115.                 end
  116.             elseif b[5] == 1 then
  117.                 b[6] = touchDown
  118.             else
  119.                 b[6] = touchActive
  120.             end
  121.         end
  122.     end
  123.  
  124.     if buttons[2][6] then
  125.         mapStartX = mapX
  126.         mapStartY = mapY
  127.         mapTargetX = gpsx
  128.         mapTargetY = gpsy
  129.         animationStep = 0
  130.     end
  131.     if buttons[3][6] then
  132.         zoom = zoom - zoom / 50
  133.     end
  134.     if buttons[4][6] then
  135.         zoom = zoom + zoom / 50
  136.     end
  137.  
  138.     -- handle cycle through waypoints
  139.     if buttons[8][6] or buttons[9][6] then
  140.         if buttons[8][6] and focusedWaypoint > 1 then
  141.             focusedWaypoint = focusedWaypoint - 1
  142.         end
  143.         if buttons[9][6] and focusedWaypoint < #waypoints then
  144.             focusedWaypoint = focusedWaypoint + 1
  145.         end
  146.  
  147.         if focusedWaypoint > 0 then
  148.             buttons[2][6] = false
  149.             mapStartX = mapX
  150.             mapStartY = mapY
  151.             mapTargetX = waypoints[focusedWaypoint][1]
  152.             mapTargetY = waypoints[focusedWaypoint][2]
  153.             animationStep = 0
  154.         end
  155.     end
  156.  
  157.     addWp = nil
  158.     -- Add waypoint by button
  159.     if buttons[5][6] then
  160.         x,y = map.screenToMap(mapX, mapY, zoom, screenWidth, screenHeight, screenWidth / 2, screenHeight / 2)
  161.         addWp = {x, y}
  162.     end
  163.  
  164.     -- Add waypoint when set by attached large keypad
  165.     if keypadPulse then
  166.         addWp = { keypadA, keypadB }
  167.     end
  168.  
  169.     -- Insert waypoint after focused waypoint
  170.     if addWp then
  171.         i = focusedWaypoint or 0
  172.         -- Only add if different from previous
  173.         if i == 0 or #waypoints == 0 or waypoints[i][1] ~= addWp[1] or waypoints[i][2] ~= addWp[2] then
  174.             table.insert(waypoints, i + 1, addWp)
  175.             focusedWaypoint = i + 1
  176.         end
  177.     end
  178.  
  179.     -- Remove waypoint by bool input signal
  180.     if doRemoveCurrentWaypoint and #waypoints > 0 then
  181.         table.remove(waypoints, 1)
  182.         focusedWaypoint = 0
  183.     end
  184.  
  185.     -- Remove focused waypoint by button
  186.     if buttons[6][6] and #waypoints > 0 and focusedWaypoint > 0 then
  187.         table.remove(waypoints, focusedWaypoint)
  188.         focusedWaypoint = math.max(0, math.min(focusedWaypoint - 1, #waypoints))
  189.     end
  190.  
  191.     -- Calculate distance
  192.     d = 0
  193.     lx = gpsx
  194.     ly = gpsy
  195.     for i, wy in ipairs(waypoints) do
  196.         x, y = table.unpack(wy)
  197.         d = d + dist(lx, ly, wy[1], y)
  198.         lx = x
  199.         ly = y
  200.     end
  201.     distance = math.floor(d)
  202.     output.setNumber(3, distance)
  203.  
  204.  
  205.     -- Map clicked
  206.     if touchDown and touchX >= 18 and touchX < w - 18 and touchY < h - 7 then
  207.         focusX = touchX
  208.         focusY = touchY
  209.         buttons[2][6] = false
  210.         -- Check if waypoint was clicked
  211.         for i, wy in ipairs(waypoints) do
  212.             px, py = map.mapToScreen(mapX, mapY, zoom, w, h, wy[1], wy[2])
  213.             d = dist(touchX, touchY, px, py)
  214.             if d < 6 then
  215.                 focusX = px
  216.                 focusY = py
  217.                 focusedWaypoint = i
  218.             end
  219.         end
  220.  
  221.         -- Pan
  222.         wx, wy = map.screenToMap(mapX, mapY, zoom, screenWidth, screenHeight, focusX, focusY)
  223.         mapStartX = mapX
  224.         mapStartY = mapY
  225.         mapTargetX = wx
  226.         mapTargetY = wy
  227.         animationStep = 0
  228.     end
  229.  
  230. end
  231.  
  232. function onDraw()
  233.     if touchActive == nil then return true end
  234.     w = screen.getWidth()
  235.     h = screen.getHeight()
  236.  
  237.     if buttons[1][6] then
  238.         screen.setMapColorGrass(3, 6, 3)
  239.         screen.setMapColorLand(8, 8, 4)
  240.         screen.setMapColorOcean(2, 2, 5)
  241.         screen.setMapColorSand(12, 12, 7)
  242.         screen.setMapColorShallows(4, 4, 7)
  243.         screen.setMapColorSnow(15, 15, 15)
  244.     else
  245.         screen.setMapColorOcean(14, 46, 48)
  246.         screen.setMapColorShallows(24, 68, 72)
  247.         screen.setMapColorLand(90, 90, 90)
  248.         screen.setMapColorGrass(64, 85, 48)
  249.         screen.setMapColorSand(100, 93, 41)
  250.         screen.setMapColorSnow(200, 200, 200)
  251.     end
  252.  
  253.     screen.drawMap(mapX, mapY, zoom)
  254.  
  255.     px, py = map.mapToScreen(mapX, mapY, zoom, w, h, gpsx, gpsy)
  256.     prevX = px
  257.     prevY = py
  258.     setPaint(4)
  259.     for i, wp in ipairs(waypoints) do
  260.         x, y = map.mapToScreen(mapX, mapY, zoom, w, h, wp[1], wp[2])
  261.         screen.drawLine(prevX, prevY, x, y)
  262.         screen.drawCircleF(x+0.5, y+0.5, 2)
  263.         prevX = x
  264.         prevY = y
  265.     end
  266.  
  267.     if waypoints[focusedWaypoint] ~= nil then
  268.         setPaint(5)
  269.         x, y = map.mapToScreen(mapX, mapY, zoom, w, h, waypoints[focusedWaypoint][1], waypoints[focusedWaypoint][2])
  270.         screen.drawCircleF(x + 0.5, y + 0.5, 3)
  271.     end
  272.  
  273.     pi2 = math.pi * 2
  274.     headingSize = 6
  275.     setPaint(4)
  276.     screen.drawTriangleF(px + math.cos((heading - 0.25) * pi2) * headingSize, py + math.sin((heading - 0.25) * pi2) * headingSize, px + math.cos((heading + 0.35) * pi2) * headingSize, py + math.sin((heading + 0.35) * pi2) * headingSize, px + math.cos((heading + 1.15) * pi2) * headingSize, py + math.sin((heading + 1.15) * pi2) * headingSize)
  277.  
  278.     -- Crosshair if not following
  279.     if not buttons[2][6] then
  280.         screen.setColor(20, 20, 20)
  281.         cx = w / 2
  282.         cy = h / 2
  283.         screen.drawLine(cx - 3, cy, cx + 4, cy)
  284.         screen.drawLine(cx, cy - 3, cx, cy + 4)
  285.     end
  286.  
  287.     screen.setColor(10, 10, 10, 200)
  288.     screen.drawRectF(0, 0, 18, h-7)
  289.     screen.drawRectF(w-18, 0, 18, h-7)
  290.     screen.drawRectF(0, h-7, w, 7)
  291.     for i, b in ipairs(buttons) do
  292.         bx = b[2] > 0 and b[2] or (w + b[2])
  293.         setPaint(b[6] and 3 or 2)
  294.         screen.drawRectF(bx, b[3], b[4], 7)
  295.         setPaint(1)
  296.         -- label is moved 1px more in case of small button of width=7
  297.         screen.drawText(bx + (b[4] == 7 and 2 or 1), b[3] + 1, b[1])
  298.     end
  299.  
  300.     setPaint(1)
  301.     if distance > 0 and w >= 96 then
  302.         distanceString = tostring(distance) .. "m"
  303.         screen.drawText(w - (string.len(distanceString) * 5) - 1, h - 6, distanceString)
  304.     end
  305.     if w >= 96 then
  306.         screen.drawText(2, h - 6, tostring(math.ceil(mapX)) .. " " .. tostring(math.ceil(mapY)))
  307.     end
  308.  
  309. end
Advertisement
Add Comment
Please, Sign In to add comment