diff --git a/inferium-harvester.lua b/inferium-harvester.lua index fee71c4..fe82260 100644 --- a/inferium-harvester.lua +++ b/inferium-harvester.lua @@ -2,11 +2,13 @@ local utils = require('libs/utils') local turtleUtils = require('libs/turtle-utils') local config = require('config/harvesting') -local VERSION = "0.5.0" +local VERSION = "0.6.0" local IDLE_TIME = 2 local WAIT_ITEM_IDLE_TIME = 5 local MIN_FUEL_NEEDED = (100 + config.firstCropZ + config.length) * 2 local MIN_FREE_SLOTS_BEFORE_COMPACT = 4 +local INFERIUM_QUERY_PORT = 111 +local INFERIUM_REPLY_PORT = 112 -- a table with the list of current crops local localPlan = nil @@ -256,9 +258,39 @@ local function retrieveLocalPlan() goBackToHome() end +-- TODO: fetch timeout ? local function fetchRemotePlan() - -- TODO - return config.defaultRemotePlan or {} + local fallbackPlan = config.defaultRemotePlan or {} + + local modem = peripheral.find("modem") + + if not modem then + return fallbackPlan + end + + modem.open(INFERIUM_REPLY_PORT) + + local message = { type = 'getplan', payload = { computerId = os.getComputerID() } } + modem.transmit(INFERIUM_QUERY_PORT, INFERIUM_REPLY_PORT, textutils.serialize(message)) + + local channel, replyRawMessage + repeat + _, _, channel, _, replyRawMessage = os.pullEvent("modem_message") + until channel == INFERIUM_REPLY_PORT + + modem.close(INFERIUM_REPLY_PORT) + + if not replyRawMessage then + return fallbackPlan + end + + local replyMessage = textutils.unserialize(replyRawMessage) + + if replyMessage and replyMessage.payload then + return replyMessage.payload + end + + return fallbackPlan end local function removeSeeds(seeds)