Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Developed using LifeBoatAPI - Stormworks Lua plugin for VSCode - https://code.visualstudio.com/download (search "Stormworks Lua with LifeboatAPI" extension)
- --- If you have any issues, please report them here: https://github.com/nameouschangey/STORMWORKS_VSCodeExtension/issues - by Nameous Changey
- --- Remember to set your Author name etc. in the settings: CTRL+COMMA
- 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.
- require("LifeBoatAPI") -- Type 'LifeBoatAPI.' and use intellisense to checkout the new LifeBoatAPI library functions; such as the LBVec vector maths library
- -- By Hendrik, please contact me if you need the non-minified source code
- function dist(x1, y1, x2, y2) return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2) end
- function ease(a, b, t) return a + ((b - a) * math.sin((t * math.pi) / 2)) end
- function getNumbers(...) local a = {} for b, c in ipairs({ ... }) do a[b] = input.getNumber(c) end return table.unpack(a) end
- function getBooleans(...) local a = {} for b, c in ipairs({ ... }) do a[b] = input.getBool(c) end return table.unpack(a) end
- function inRect(x, y, a, b, w, h) return x >= a and y >= b and x < a + w and y < b + h end
- function color(r, g, b, a)
- a = a or 255
- return {255*(0.8*r/255)^2.6, 255*(0.8*g/255)^2.6, 255*(0.8*b/255)^2.6, a}
- end
- -- colors: light, dark
- colors = {
- -- text
- {{240, 240, 240}, {200, 200, 200}},
- -- button normal
- {color(120, 120, 153), color(120, 120, 153)},
- -- button active
- {color(106, 106, 181), color(106, 106, 181)},
- -- waypoints normal
- {color(219, 30, 30), color(183, 25, 25)},
- -- waypoints selected
- {color(183, 25, 25), color(142, 19, 19)},
- }
- function setPaint(index)
- screen.setColor(table.unpack(colors[index][buttons[1][6] and 2 or 1]))
- end
- animationStep = 0
- ANIMATION_DURATION = 30
- mapX = 0
- mapY = 0
- mapStartX = 0
- mapStartY = 0
- mapTargetX = 0
- mapTargetY = 0
- zoom = 1
- touchWasActive = false
- waypoints = {}
- distance = 0
- focusedWaypoint = 0
- -- label, x, y, w, type[0=hold, 1=click, 2=toggle], value
- buttons = {
- {"DRK", 1, 1, 16, 2, false},
- {"FLW", 1, 9, 16, 2, true},
- {"+", 1, 17, 7, 0, false},
- {"-", 10, 17, 7, 0, false},
- {"ADD", -17, 9, 16, 1, false},
- {"DEL", -17, 17, 16, 1, false},
- {"AP", -17, 25, 16, 2, false},
- {"<", -17, 1, 7, 1, false},
- {">", -8, 1, 7, 1, false},
- }
- function onTick()
- animationStep = math.min(animationStep + 1, ANIMATION_DURATION)
- screenWidth, screenHeight, touchX, touchY, gpsx, gpsy, heading, keypadA, keypadB = getNumbers(1, 2, 3, 4, 7, 8, 9, 10, 11)
- heading = heading * -1
- mapX = ease(mapStartX, mapTargetX, animationStep / ANIMATION_DURATION)
- mapY = ease(mapStartY, mapTargetY, animationStep / ANIMATION_DURATION)
- touchActive, doRemoveCurrentWaypoint, keypadPulse = getBooleans(1, 3, 4)
- touchDown = touchActive and not touchWasActive
- touchWasActive = touchActive
- -- Output next waypoint if AP enabled and there is a waypoint
- output.setBool(1, buttons[7][6])
- if buttons[7][6] then
- -- Disable AP when no more waypoints
- if #waypoints == 0 then
- buttons[7][6] = false
- else
- output.setNumber(1, waypoints[1][1])
- output.setNumber(2, waypoints[1][2])
- -- Remove first waypoint if reached (< 100m)
- if dist(gpsx, gpsy, waypoints[1][1], waypoints[1][2]) < 100 then
- doRemoveCurrentWaypoint = true
- end
- end
- end
- -- Handle button touch
- for i, b in ipairs(buttons) do
- if b[5] ~= 2 then
- b[6] = false
- end
- bx = b[2] > 0 and b[2] or (screenWidth + b[2])
- if touchActive and inRect(touchX, touchY, bx, b[3], b[4], 7) then
- if b[5] == 2 then
- if touchDown then
- b[6] = not b[6]
- end
- elseif b[5] == 1 then
- b[6] = touchDown
- else
- b[6] = touchActive
- end
- end
- end
- if buttons[2][6] then
- mapStartX = mapX
- mapStartY = mapY
- mapTargetX = gpsx
- mapTargetY = gpsy
- animationStep = 0
- end
- if buttons[3][6] then
- zoom = zoom - zoom / 50
- end
- if buttons[4][6] then
- zoom = zoom + zoom / 50
- end
- -- handle cycle through waypoints
- if buttons[8][6] or buttons[9][6] then
- if buttons[8][6] and focusedWaypoint > 1 then
- focusedWaypoint = focusedWaypoint - 1
- end
- if buttons[9][6] and focusedWaypoint < #waypoints then
- focusedWaypoint = focusedWaypoint + 1
- end
- if focusedWaypoint > 0 then
- buttons[2][6] = false
- mapStartX = mapX
- mapStartY = mapY
- mapTargetX = waypoints[focusedWaypoint][1]
- mapTargetY = waypoints[focusedWaypoint][2]
- animationStep = 0
- end
- end
- addWp = nil
- -- Add waypoint by button
- if buttons[5][6] then
- x,y = map.screenToMap(mapX, mapY, zoom, screenWidth, screenHeight, screenWidth / 2, screenHeight / 2)
- addWp = {x, y}
- end
- -- Add waypoint when set by attached large keypad
- if keypadPulse then
- addWp = { keypadA, keypadB }
- end
- -- Insert waypoint after focused waypoint
- if addWp then
- i = focusedWaypoint or 0
- -- Only add if different from previous
- if i == 0 or #waypoints == 0 or waypoints[i][1] ~= addWp[1] or waypoints[i][2] ~= addWp[2] then
- table.insert(waypoints, i + 1, addWp)
- focusedWaypoint = i + 1
- end
- end
- -- Remove waypoint by bool input signal
- if doRemoveCurrentWaypoint and #waypoints > 0 then
- table.remove(waypoints, 1)
- focusedWaypoint = 0
- end
- -- Remove focused waypoint by button
- if buttons[6][6] and #waypoints > 0 and focusedWaypoint > 0 then
- table.remove(waypoints, focusedWaypoint)
- focusedWaypoint = math.max(0, math.min(focusedWaypoint - 1, #waypoints))
- end
- -- Calculate distance
- d = 0
- lx = gpsx
- ly = gpsy
- for i, wy in ipairs(waypoints) do
- x, y = table.unpack(wy)
- d = d + dist(lx, ly, wy[1], y)
- lx = x
- ly = y
- end
- distance = math.floor(d)
- output.setNumber(3, distance)
- -- Map clicked
- if touchDown and touchX >= 18 and touchX < w - 18 and touchY < h - 7 then
- focusX = touchX
- focusY = touchY
- buttons[2][6] = false
- -- Check if waypoint was clicked
- for i, wy in ipairs(waypoints) do
- px, py = map.mapToScreen(mapX, mapY, zoom, w, h, wy[1], wy[2])
- d = dist(touchX, touchY, px, py)
- if d < 6 then
- focusX = px
- focusY = py
- focusedWaypoint = i
- end
- end
- -- Pan
- wx, wy = map.screenToMap(mapX, mapY, zoom, screenWidth, screenHeight, focusX, focusY)
- mapStartX = mapX
- mapStartY = mapY
- mapTargetX = wx
- mapTargetY = wy
- animationStep = 0
- end
- end
- function onDraw()
- if touchActive == nil then return true end
- w = screen.getWidth()
- h = screen.getHeight()
- if buttons[1][6] then
- screen.setMapColorGrass(3, 6, 3)
- screen.setMapColorLand(8, 8, 4)
- screen.setMapColorOcean(2, 2, 5)
- screen.setMapColorSand(12, 12, 7)
- screen.setMapColorShallows(4, 4, 7)
- screen.setMapColorSnow(15, 15, 15)
- else
- screen.setMapColorOcean(14, 46, 48)
- screen.setMapColorShallows(24, 68, 72)
- screen.setMapColorLand(90, 90, 90)
- screen.setMapColorGrass(64, 85, 48)
- screen.setMapColorSand(100, 93, 41)
- screen.setMapColorSnow(200, 200, 200)
- end
- screen.drawMap(mapX, mapY, zoom)
- px, py = map.mapToScreen(mapX, mapY, zoom, w, h, gpsx, gpsy)
- prevX = px
- prevY = py
- setPaint(4)
- for i, wp in ipairs(waypoints) do
- x, y = map.mapToScreen(mapX, mapY, zoom, w, h, wp[1], wp[2])
- screen.drawLine(prevX, prevY, x, y)
- screen.drawCircleF(x+0.5, y+0.5, 2)
- prevX = x
- prevY = y
- end
- if waypoints[focusedWaypoint] ~= nil then
- setPaint(5)
- x, y = map.mapToScreen(mapX, mapY, zoom, w, h, waypoints[focusedWaypoint][1], waypoints[focusedWaypoint][2])
- screen.drawCircleF(x + 0.5, y + 0.5, 3)
- end
- pi2 = math.pi * 2
- headingSize = 6
- setPaint(4)
- 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)
- -- Crosshair if not following
- if not buttons[2][6] then
- screen.setColor(20, 20, 20)
- cx = w / 2
- cy = h / 2
- screen.drawLine(cx - 3, cy, cx + 4, cy)
- screen.drawLine(cx, cy - 3, cx, cy + 4)
- end
- screen.setColor(10, 10, 10, 200)
- screen.drawRectF(0, 0, 18, h-7)
- screen.drawRectF(w-18, 0, 18, h-7)
- screen.drawRectF(0, h-7, w, 7)
- for i, b in ipairs(buttons) do
- bx = b[2] > 0 and b[2] or (w + b[2])
- setPaint(b[6] and 3 or 2)
- screen.drawRectF(bx, b[3], b[4], 7)
- setPaint(1)
- -- label is moved 1px more in case of small button of width=7
- screen.drawText(bx + (b[4] == 7 and 2 or 1), b[3] + 1, b[1])
- end
- setPaint(1)
- if distance > 0 and w >= 96 then
- distanceString = tostring(distance) .. "m"
- screen.drawText(w - (string.len(distanceString) * 5) - 1, h - 6, distanceString)
- end
- if w >= 96 then
- screen.drawText(2, h - 6, tostring(math.ceil(mapX)) .. " " .. tostring(math.ceil(mapY)))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment