diff --git a/gps/gpsServer.lua b/gps/gpsServer.lua index 5dc278f..dc2d354 100644 --- a/gps/gpsServer.lua +++ b/gps/gpsServer.lua @@ -3,9 +3,11 @@ local GPS = require('gps') local Util = require('util') local args = { ... } +local fs = _G.fs local gps = _G.gps local os = _G.os local peripheral = _G.peripheral +local read = _G.read local term = _G.term local turtle = _G.turtle local vector = _G.vector @@ -14,11 +16,13 @@ local WIRED_MODEM = 'computercraft:wired_modem_full' local CABLE = 'computercraft:cable' local ENDER_MODEM = 'computercraft:advanced_modem' +local STARTUP_FILE = 'usr/autorun/gpsServer.lua' + local positions = { } local function build() - if not turtle.has(WIRED_MODEM, 8) or - not turtle.has(CABLE, 4) or + if not turtle.has(WIRED_MODEM, 5) or + not turtle.has(CABLE, 8) or not turtle.has(ENDER_MODEM, 4) then error([[Place into inventory: * 5 Wired modem (blocks) @@ -26,6 +30,18 @@ local function build() * 4 Ender modems]]) end + term.clear() + term.setCursorPos(1, 2) + term.setTextColor(colors.yellow) + print(' Turtle must be facing east\n') + term.setTextColor(colors.white) + print(' Enter to continue or ctrl-t to abort') + read() + + term.clear() + term.setCursorPos(1, 2) + print('building...') + local blocks = { { x = 0, y = 0, z = 0, b = WIRED_MODEM }, @@ -55,6 +71,41 @@ local function build() end end +local function configure() + local function getOption(prompt) + while true do + term.write(prompt) + local value = read() + if tonumber(value) then + return tonumber(value) + end + print('Invalid value, try again.\n') + end + end + + print('Server configuration\n\n') + + Config.update('gpsServer', { + x = getOption('Turtle x: '), + y = getOption('Turtle y: '), + z = getOption('Turtle z: '), + east = getOption('East modem: modem_'), + south = getOption('South modem: modem_'), + west = getOption('West modem: modem_'), + north = getOption('North modem: modem_'), + }) + + print('Make sure all wired modems are activated') + print('Enter to continue') + read() + + if not fs.exists(STARTUP_FILE) then + Util.writeFile(STARTUP_FILE, + [[shell.openForegroundTab('gpsServer.lua server')]]) + print('Autorun program created: ' .. STARTUP_FILE) + end +end + local function memoize(t, k, fn) local e = t[k] if not e then @@ -65,19 +116,32 @@ local function memoize(t, k, fn) end local function server() - local modems = Config.load('gpsServer') local computers = { } - if Util.empty(modems) then - error('Missing usr/config/gpsServer configuration file') + if not fs.exists('usr/config/gpsServer') then + configure() end + local config = Config.load('gpsServer') + + local modems = { } + modems['modem_' .. config.east] = { x = config.x + 2, y = config.y + 2, z = config.z } + modems['modem_' .. config.west] = { x = config.x - 2, y = config.y + 2, z = config.z } + modems['modem_' .. config.south] = { x = config.x, y = config.y, z = config.z + 2 } + modems['modem_' .. config.north] = { x = config.x, y = config.y, z = config.z - 2 } + for k, modem in pairs(modems) do - Util.merge(modem, peripheral.wrap(k)) + Util.merge(modem, peripheral.wrap(k) or { }) + Util.print('%s: %d %d %d', k, modem.x, modem.y, modem.z) + if not modem.open then + error('Modem is not activated or connected: ' .. k) + end modem.open(gps.CHANNEL_GPS) --modem.open(999) end + print('\nStarting GPS Server') + local function getPosition(computerId, modem, distance) local computer = memoize(computers, computerId, function() return { } end) table.insert(computer, { @@ -123,23 +187,9 @@ if args[1] == 'build' then build() turtle._goto({ x = 0, y = 1, z = 0, heading = 0 }) - print('Activate all modems') - print('Press enter when ready') - _G.read() + configure() - local modems = { } - peripheral.find('modem', function(name, modem) - if modem.isWireless() then - modems[name] = { x = 0, y = 0, z = 0 } - end - end) - - Config.update('gpsServer', modems) - - print([[ -Configuration file usr/config/gpsServer created. -Add coordinates for each modem. -Use the position of the wired modem below the wireless modem]]) + server() elseif args[1] == 'server' then server()