Added jump to position for ship default LUA scripts for CC & OC

Inspired by nonenothing pull request #365
This commit is contained in:
Unknown 2020-05-08 06:17:18 +02:00
parent e8b54bb2a2
commit 17f1944253
2 changed files with 80 additions and 32 deletions

View file

@ -227,7 +227,7 @@ function ship_warp()
-- rs.setOutput(alarm_side, false) -- rs.setOutput(alarm_side, false)
end end
function ship_page_setMovement() function ship_page_setMovement(isByPosition)
-- force manual jump so we get proper max jump distance -- force manual jump so we get proper max jump distance
ship.command("MANUAL", false) ship.command("MANUAL", false)
@ -243,30 +243,49 @@ function ship_page_setMovement()
ship_writeMovement("Current movement is ") ship_writeMovement("Current movement is ")
w.setCursorPos(1, 5) w.setCursorPos(1, 5)
ship_movement[1] = ship_page_setDistanceAxis(4, "Forward/back", "Forward", "Backward", ship_movement[1], math.abs(ship_front + ship_back + 1), maxJumpDistance) local lenFB = math.abs(ship_front + ship_back + 1)
ship_movement[2] = ship_page_setDistanceAxis(6, "Up/down" , "Up" , "Down" , ship_movement[2], math.abs(ship_up + ship_down + 1), maxJumpDistance) local lenUD = math.abs(ship_up + ship_down + 1)
ship_movement[3] = ship_page_setDistanceAxis(8, "Right/left" , "Right" , "Left" , ship_movement[3], math.abs(ship_left + ship_right + 1), maxJumpDistance) local lenLR = math.abs(ship_left + ship_right + 1)
if (isByPosition) then
local dx, dy, dz = ship.getOrientation()
if dx == nil then
dx, dy, dz = 0, 0, 0
end
if dx == 0 then
ship_movement[3] = -dz * ship_page_setDistanceAxis(4, "X" , "East" , "West" , ship_movement[3], lenLR, maxJumpDistance, ship_x)
ship_movement[1] = dz * ship_page_setDistanceAxis(6, "Z" , "South" , "North" , ship_movement[1], lenFB, maxJumpDistance, ship_z)
else
ship_movement[1] = dx * ship_page_setDistanceAxis(4, "X" , "East" , "West" , ship_movement[1], lenFB, maxJumpDistance, ship_x)
ship_movement[3] = dx * ship_page_setDistanceAxis(6, "Z" , "South" , "North" , ship_movement[3], lenLR, maxJumpDistance, ship_z)
end
ship_movement[2] = ship_page_setDistanceAxis(8, "Y" , "Up" , "Down" , ship_movement[2], lenUD, maxJumpDistance, ship_y)
else
ship_movement[1] = ship_page_setDistanceAxis(4, "Forward/back", "Forward", "Backward", ship_movement[1], lenFB, maxJumpDistance, 0)
ship_movement[2] = ship_page_setDistanceAxis(6, "Up/down" , "Up" , "Down" , ship_movement[2], lenUD, maxJumpDistance, 0)
ship_movement[3] = ship_page_setDistanceAxis(8, "Right/left" , "Right" , "Left" , ship_movement[3], lenLR, maxJumpDistance, 0)
end
ship_movement = { ship.movement(ship_movement[1], ship_movement[2], ship_movement[3]) } ship_movement = { ship.movement(ship_movement[1], ship_movement[2], ship_movement[3]) }
ship_updateMovementStats() ship_updateMovementStats()
end end
function ship_page_setDistanceAxis(line, axis, positive, negative, userEntry, shipLength, maxJumpDistance) function ship_page_setDistanceAxis(line, axis, positive, negative, userEntry, shipLength, maxJumpDistance, offset)
local maximumDistance = math.floor(shipLength + maxJumpDistance) local maximumDistance = math.floor(shipLength + maxJumpDistance)
w.setCursorPos(1, line + 2) w.setCursorPos(1, line + 2)
w.setColorHelp() w.setColorHelp()
w.writeFullLine(" Enter between " .. math.floor( shipLength + 1) .. " and " .. maximumDistance .. " to move " .. positive .. ".") w.writeFullLine(" Enter between " .. w.format_integer(offset + math.floor( shipLength + 1)) .. " and " .. w.format_integer(offset + maximumDistance) .. " to move " .. positive .. ".")
w.writeFullLine(" Enter 0 to keep position on this axis.") w.writeFullLine(" Enter " .. w.format_integer(offset) .. " to keep position on this axis.")
w.writeFullLine(" Enter between " .. -maximumDistance .. " and " .. math.floor(-shipLength - 1) .. " to move " .. negative .. ".") w.writeFullLine(" Enter between " .. w.format_integer(offset - maximumDistance) .. " and " .. w.format_integer(offset + math.floor(-shipLength - 1)) .. " to move " .. negative .. ".")
repeat repeat
w.setCursorPos(1, line) w.setCursorPos(1, line)
w.setColorNormal() w.setColorNormal()
w.write(axis .. " movement: ") w.write(axis .. " movement: ")
userEntry = w.input_readInteger(userEntry) userEntry = w.input_readInteger(offset + userEntry)
if userEntry ~= 0 and (math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance) then if math.abs(userEntry - offset) > maximumDistance then
w.status_showWarning("Wrong distance. Try again.") w.status_showWarning("Wrong distance. Try again.")
end end
until userEntry == 0 or (math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance) until math.abs(userEntry - offset) <= maximumDistance
w.setCursorPos(1, line + 2) w.setCursorPos(1, line + 2)
w.clearLine() w.clearLine()
w.setCursorPos(1, line + 3) w.setCursorPos(1, line + 3)
@ -274,7 +293,7 @@ function ship_page_setDistanceAxis(line, axis, positive, negative, userEntry, sh
w.setCursorPos(1, line + 4) w.setCursorPos(1, line + 4)
w.clearLine() w.clearLine()
return userEntry return userEntry - offset
end end
function ship_page_setRotation() function ship_page_setRotation()
@ -461,17 +480,22 @@ function ship_page_controls()
w.setCursorPos(1, 16) w.setCursorPos(1, 16)
w.setColorControl() w.setColorControl()
w.writeFullLine(" set ship Name (N), dImensions (I), Movement (M)") w.writeFullLine(" set ship Name (N), dImensions (I), Movement (M/P)")
if ship_isInHyper then if ship_isInHyper then
w.writeFullLine(" Jump to move ship (M/J), exit Hyperspace (H)") w.writeFullLine(" Jump to move ship (J), exit Hyperspace (H)")
else else
w.writeFullLine(" Jump to move ship (M/J), enter Hyperspace (H)") w.writeFullLine(" Jump to move ship (J), enter Hyperspace (H)")
end end
end end
function ship_key_controls(character, keycode) function ship_key_controls(character, keycode)
if character == 'm' or character == 'M' then if character == 'm' or character == 'M' then
ship_page_setMovement() ship_page_setMovement(false)
ship_page_setRotation()
ship_warp()
return true
elseif character == 'p' or character == 'P' then
ship_page_setMovement(true)
ship_page_setRotation() ship_page_setRotation()
ship_warp() ship_warp()
return true return true

View file

@ -228,7 +228,7 @@ function ship_warp()
-- rs.setOutput(alarm_side, false) -- rs.setOutput(alarm_side, false)
end end
function ship_page_setMovement() function ship_page_setMovement(isByPosition)
-- force manual jump so we get proper max jump distance -- force manual jump so we get proper max jump distance
ship.command("MANUAL", false) ship.command("MANUAL", false)
@ -244,30 +244,49 @@ function ship_page_setMovement()
ship_writeMovement("Current movement is ") ship_writeMovement("Current movement is ")
w.setCursorPos(1, 5) w.setCursorPos(1, 5)
ship_movement[1] = ship_page_setDistanceAxis(4, "Forward/back", "Forward", "Backward", ship_movement[1], math.abs(ship_front + ship_back + 1), maxJumpDistance) local lenFB = math.abs(ship_front + ship_back + 1)
ship_movement[2] = ship_page_setDistanceAxis(6, "Up/down" , "Up" , "Down" , ship_movement[2], math.abs(ship_up + ship_down + 1), maxJumpDistance) local lenUD = math.abs(ship_up + ship_down + 1)
ship_movement[3] = ship_page_setDistanceAxis(8, "Right/left" , "Right" , "Left" , ship_movement[3], math.abs(ship_left + ship_right + 1), maxJumpDistance) local lenLR = math.abs(ship_left + ship_right + 1)
if (isByPosition) then
local dx, dy, dz = ship.getOrientation()
if dx == nil then
dx, dy, dz = 0, 0, 0
end
if dx == 0 then
ship_movement[3] = -dz * ship_page_setDistanceAxis(4, "X" , "East" , "West" , ship_movement[3], lenLR, maxJumpDistance, ship_x)
ship_movement[1] = dz * ship_page_setDistanceAxis(6, "Z" , "South" , "North" , ship_movement[1], lenFB, maxJumpDistance, ship_z)
else
ship_movement[1] = dx * ship_page_setDistanceAxis(4, "X" , "East" , "West" , ship_movement[1], lenFB, maxJumpDistance, ship_x)
ship_movement[3] = dx * ship_page_setDistanceAxis(6, "Z" , "South" , "North" , ship_movement[3], lenLR, maxJumpDistance, ship_z)
end
ship_movement[2] = ship_page_setDistanceAxis(8, "Y" , "Up" , "Down" , ship_movement[2], lenUD, maxJumpDistance, ship_y)
else
ship_movement[1] = ship_page_setDistanceAxis(4, "Forward/back", "Forward", "Backward", ship_movement[1], lenFB, maxJumpDistance, 0)
ship_movement[2] = ship_page_setDistanceAxis(6, "Up/down" , "Up" , "Down" , ship_movement[2], lenUD, maxJumpDistance, 0)
ship_movement[3] = ship_page_setDistanceAxis(8, "Right/left" , "Right" , "Left" , ship_movement[3], lenLR, maxJumpDistance, 0)
end
ship_movement = { ship.movement(ship_movement[1], ship_movement[2], ship_movement[3]) } ship_movement = { ship.movement(ship_movement[1], ship_movement[2], ship_movement[3]) }
ship_updateMovementStats() ship_updateMovementStats()
end end
function ship_page_setDistanceAxis(line, axis, positive, negative, userEntry, shipLength, maxJumpDistance) function ship_page_setDistanceAxis(line, axis, positive, negative, userEntry, shipLength, maxJumpDistance, offset)
local maximumDistance = math.floor(shipLength + maxJumpDistance) local maximumDistance = math.floor(shipLength + maxJumpDistance)
w.setCursorPos(1, line + 2) w.setCursorPos(1, line + 2)
w.setColorHelp() w.setColorHelp()
w.writeFullLine(" Enter between " .. math.floor( shipLength + 1) .. " and " .. maximumDistance .. " to move " .. positive .. ".") w.writeFullLine(" Enter between " .. w.format_integer(offset + math.floor( shipLength + 1)) .. " and " .. w.format_integer(offset + maximumDistance) .. " to move " .. positive .. ".")
w.writeFullLine(" Enter 0 to keep position on this axis.") w.writeFullLine(" Enter " .. w.format_integer(offset) .. " to keep position on this axis.")
w.writeFullLine(" Enter between " .. -maximumDistance .. " and " .. math.floor(-shipLength - 1) .. " to move " .. negative .. ".") w.writeFullLine(" Enter between " .. w.format_integer(offset - maximumDistance) .. " and " .. w.format_integer(offset + math.floor(-shipLength - 1)) .. " to move " .. negative .. ".")
repeat repeat
w.setCursorPos(1, line) w.setCursorPos(1, line)
w.setColorNormal() w.setColorNormal()
w.write(axis .. " movement: ") w.write(axis .. " movement: ")
userEntry = w.input_readInteger(userEntry) userEntry = w.input_readInteger(offset + userEntry)
if userEntry ~= 0 and (math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance) then if math.abs(userEntry - offset) > maximumDistance then
w.status_showWarning("Wrong distance. Try again.") w.status_showWarning("Wrong distance. Try again.")
end end
until userEntry == 0 or (math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance) until math.abs(userEntry - offset) <= maximumDistance
w.setCursorPos(1, line + 2) w.setCursorPos(1, line + 2)
w.clearLine() w.clearLine()
w.setCursorPos(1, line + 3) w.setCursorPos(1, line + 3)
@ -275,7 +294,7 @@ function ship_page_setDistanceAxis(line, axis, positive, negative, userEntry, sh
w.setCursorPos(1, line + 4) w.setCursorPos(1, line + 4)
w.clearLine() w.clearLine()
return userEntry return userEntry - offset
end end
function ship_page_setRotation() function ship_page_setRotation()
@ -465,17 +484,22 @@ function ship_page_controls()
w.setCursorPos(1, 20) w.setCursorPos(1, 20)
w.setColorControl() w.setColorControl()
w.writeFullLine(" set ship Name (N), dImensions (I), Movement (M)") w.writeFullLine(" set ship Name (N), dImensions (I), Movement (M/P)")
if ship_isInHyper then if ship_isInHyper then
w.writeFullLine(" Jump to move ship (M/J), exit Hyperspace (H)") w.writeFullLine(" Jump to move ship (J), exit Hyperspace (H)")
else else
w.writeFullLine(" Jump to move ship (M/J), enter Hyperspace (H)") w.writeFullLine(" Jump to move ship (J), enter Hyperspace (H)")
end end
end end
function ship_key_controls(character, keycode) function ship_key_controls(character, keycode)
if character == 'm' or character == 'M' then if character == 'm' or character == 'M' then
ship_page_setMovement() ship_page_setMovement(false)
ship_page_setRotation()
ship_warp()
return true
elseif character == 'p' or character == 'P' then
ship_page_setMovement(true)
ship_page_setRotation() ship_page_setRotation()
ship_warp() ship_warp()
return true return true