Fixed OC script ship controller

Fixed data saving
Fixed connection lost after jump
This commit is contained in:
LemADEC 2016-02-07 22:52:05 +01:00
parent a17b40d9aa
commit e58b3191a0

View file

@ -3,6 +3,7 @@ local computer = require("computer")
local term = require("term") local term = require("term")
local event = require("event") local event = require("event")
local fs = require("filesystem") local fs = require("filesystem")
local serialization = require("serialization")
Style = { Style = {
CDefault = 0xFFFFFF, CDefault = 0xFFFFFF,
@ -326,20 +327,23 @@ end
function data_save() function data_save()
local file = fs.open("shipdata.txt", "w") local file = fs.open("shipdata.txt", "w")
if file ~= nil then if file ~= nil then
file.writeLine(textutils.serialize(data)) file:write(serialization.serialize(data))
file.close() file:close()
else else
ShowWarning("No file system") ShowWarning("No file system")
end end
end end
function data_read() function data_read()
data = { }
if fs.exists("shipdata.txt") then if fs.exists("shipdata.txt") then
local file = fs.open("shipdata.txt", "r") local file = fs.open("shipdata.txt", "r")
data = textutils.unserialize(file.readAll()) local size = fs.size("shipdata.txt")
file.close() local rawData = file:read(size)
else if rawData ~= nil then
data = { } data = serialization.unserialize(rawData)
end
file:close()
end end
if data.core_summon == nil then data.core_summon = false; end if data.core_summon == nil then data.core_summon = false; end
if data.core_distance == nil then data.core_distance = 0; end if data.core_distance == nil then data.core_distance = 0; end
@ -435,7 +439,7 @@ function core_computeRealDistance()
if core_isInHyper then if core_isInHyper then
core_shipLength = 0 core_shipLength = 0
core_realDistance = data.core_distance * 100 + core_shipLength core_realDistance = data.core_distance * 100 + core_shipLength
core_jumpCost = (1000 * core_shipSize) + (1000 * data.core_distance) ship.mode(2)
else else
if data.core_direction == 1 or data.core_direction == 2 then if data.core_direction == 1 or data.core_direction == 2 then
core_shipLength = core_up + core_down + 1 core_shipLength = core_up + core_down + 1
@ -445,8 +449,9 @@ function core_computeRealDistance()
core_shipLength = core_left + core_right + 1 core_shipLength = core_left + core_right + 1
end end
core_realDistance = data.core_distance + core_shipLength - 1 core_realDistance = data.core_distance + core_shipLength - 1
core_jumpCost = (10 * core_shipSize) + (100 * data.core_distance) ship.mode(1)
end end
core_jumpCost = ship.getEnergyRequired(core_realDistance)
end end
function core_computeNewCoordinates(cx, cy, cz) function core_computeNewCoordinates(cx, cy, cz)
@ -493,7 +498,7 @@ function core_warp()
ship.mode(1) ship.mode(1)
end end
ship.jump() ship.jump()
ship = nil -- ship = nil
end end
-- rs.setOutput(alarm_side, false) -- rs.setOutput(alarm_side, false)
end end
@ -623,7 +628,7 @@ function core_page_jumpToBeacon()
ship.mode(4) ship.mode(4)
ship.beaconFrequency(freq) ship.beaconFrequency(freq)
ship.jump() ship.jump()
ship = nil -- ship = nil
end end
-- rs.setOutput(alarm_side, false) -- rs.setOutput(alarm_side, false)
end end
@ -639,7 +644,7 @@ function core_page_jumpToGate()
ship.mode(6) ship.mode(6)
ship.targetJumpgate(name) ship.targetJumpgate(name)
ship.jump() ship.jump()
ship = nil -- ship = nil
end end
-- rs.setOutput(alarm_side, false) -- rs.setOutput(alarm_side, false)
end end
@ -721,7 +726,7 @@ function core_key(char, keycode)
rs.setOutput(alarm_side, false) rs.setOutput(alarm_side, false)
ship.mode(5) ship.mode(5)
ship.jump() ship.jump()
ship = nil -- ship = nil
end end
-- rs.setOutput(alarm_side, false) -- rs.setOutput(alarm_side, false)
return true return true