Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Program : Rednet server
- --- Author : LightKnight51
- --- Last modification : 18/04/2023
- --- Dependency for work : Rednet client (muA3ykGt)
- --- You can start any program (with arguments) with this remote control tool
- --- Variables
- local sSeparator = "|"
- local computerId = os.getComputerID()
- local libraryName = "MarquitoLuaUtils"
- local libraryCode = "mVTSwvw1"
- local deviceType = "COMPUTER"
- --- Functions
- ---- Order functions
- -- Send order to one to many turtles
- function SendOrder(bAll, receiverId, turtleType, typeOrder, messageContent)
- local protocol = "TurtleProgram." .. turtleType
- local data
- if messageContent ~= nil then
- data = typeOrder .. sSeparator .. messageContent
- else
- data = typeOrder
- end
- if bAll then
- MarquitoLuaUtils.BroadcastDataWithRednet(data, protocol)
- else
- if MarquitoLuaUtils.IsAnArray(receiverId) then
- MarquitoLuaUtils.SendDataWithRednetForMultiplesDevices(receiverId, data, protocol)
- else
- MarquitoLuaUtils.SendDataWithRednetForOneDevice(receiverId, data, protocol)
- end
- end
- end
- -- Send exit order to turtle
- function SendExitOrder(bAll, receiverId, turtleType)
- if bAll then
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtles on this network must stop")
- else
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtle " .. textutils.serialise(receiverId) .. " must stop")
- end
- SendOrder(bAll, receiverId, turtleType, "exit", nil)
- end
- -- Send program download order to turtle (in fact, it's download the programe here, and send the code to the turtle)
- function SendDownloadProgramOrder(bAll, receiverId, turtleType, programName, programContent)
- if bAll then
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtles on this network receive program " .. programName)
- else
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtle " .. textutils.serialise(receiverId) .. " receive program" .. programName)
- end
- local data = programName .. sSeparator .. programContent
- SendOrder(bAll, receiverId, turtleType, "download", data)
- end
- -- Send program execution order
- function SendExecuteProgramOrder(bAll, receiverId, turtleType, programName)
- if bAll then
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtles on this network must execute " .. programName)
- else
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtle " .. textutils.serialise(receiverId) .. " must execute " .. programName)
- end
- SendOrder(bAll, receiverId, turtleType, "execute", programName)
- end
- -- Send program delete order
- function SendDeleteProgramOrder(bAll, receiverId, turtleType, programName)
- if bAll then
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtles on this network must delete " .. programName)
- else
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtle " .. textutils.serialise(receiverId) .. " must delete " .. programName)
- end
- SendOrder(bAll, receiverId, turtleType, "delete", programName)
- end
- -- Send program coordinates order
- function SendCordinatesOrder(receiverId)
- SendOrder(false, receiverId, "coordinates", nil)
- end
- ---- Ask user input functions
- -- Ask if all turtles
- function AskIfAllTurtles()
- local nOption = nil
- print("Do you want to perform this action on specific(s) turtle(s) or all ?")
- print("0 -> One turtle")
- print("1 -> A set of turtles (ids delimited with \",\")")
- print("2 -> Every turtle in the network")
- while true do
- nOption = tonumber(read())
- if nOption == 0 or nOption == 1 or nOption == 2 then
- break
- else
- print("Incorrect code")
- end
- end
- return nOption
- end
- -- Ask turtle type
- function AskTurtleType()
- local turtleType = MarquitoLuaUtils.TurtleType.NONE
- local nOption = nil
- print("Which turtle need to get this order ?")
- print("0 -> Mining turtle")
- print("1 -> Melee turtle")
- print("2 -> Farming turtle")
- print("3 -> Felling turtle")
- print("4 -> Crafting turtle")
- print("5 -> Diging turtle")
- print("6 -> Noisy turtle")
- while true do
- nOption = tonumber(read())
- if nOption == 0 then
- turtleType = MarquitoLuaUtils.TurtleType.MINING
- break
- elseif nOption == 1 then
- turtleType = MarquitoLuaUtils.TurtleType.MELEE
- break
- elseif nOption == 2 then
- turtleType = MarquitoLuaUtils.TurtleType.FARM
- break
- elseif nOption == 3 then
- turtleType = MarquitoLuaUtils.TurtleType.FELLING
- break
- elseif nOption == 4 then
- turtleType = MarquitoLuaUtils.TurtleType.CRAFT
- break
- elseif nOption == 5 then
- turtleType = MarquitoLuaUtils.TurtleType.DIG
- break
- elseif nOption == 6 then
- turtleType = MarquitoLuaUtils.TurtleType.NOISY
- break
- else
- print("Incorrect code")
- end
- end
- return turtleType
- end
- -- Ask turtle id
- function AskTurtleId()
- print("Turtle ID ?")
- local turtleId = tonumber(read())
- return turtleId
- end
- -- Ask turtle id
- function AskTurtleIds()
- print("Turtle IDs (ids delimited with \",\") ?")
- local turtleIds = tostring(read())
- return MarquitoLuaUtils.Split(turtleIds, ",")
- end
- -- Ask program name
- function AskProgramName()
- print("Program's name ?")
- program = tostring(read())
- return program
- end
- -- Ask pastebin code to download a program
- function AskPastebinCode()
- print("Pastebin program's code ?")
- pastebinCd = tostring(read())
- return pastebinCd
- end
- -- Ask if we need to download again and update the program
- function AsIfWeNeedUpdate()
- print("Do you want to update this program ? - no(0) yes(1)")
- local nOption = 0
- while true do
- nOption = tonumber(read())
- if nOption == 0 or nOption == 1 then
- break
- else
- print("Incorrect code")
- end
- end
- return nOption == 1
- end
- -- Receive data from client
- function ReceiveData()
- while true do
- senderId, message, protocol = rednet.receive()
- if protocol == "turtleSendCoords" then
- x, y, z = returnCoords(message)
- print("Cordonnées de la turtle n" .. tostring(senderId) .. " : " .. tostring(x) .. " " .. tostring(y) .. " " .. tostring(z))
- end
- end
- end
- function returnCoords(message)
- x, y, z = 0
- i = 0
- for value in string.gmatch(message, "%a+") do
- if i == 0 then
- x = tonumber(value)
- elseif i == 1 then
- y = tonumber(value)
- elseif i == 2 then
- z = tonumber(value)
- end
- i = i + 1
- end
- return x, y, z
- end
- -- Download program
- function DownloadProgram(pastebinCode, programName)
- if fs.exists(programName) then
- fs.delete(programName)
- end
- shell.run("pastebin get " .. pastebinCode .. " " .. programName)
- end
- -- Exit the program
- function ExitProgram()
- print("End of program")
- os.sleep(1)
- MarquitoLuaUtils.EndLog()
- os.shutdown()
- end
- --sendOrder()
- function MainProgram()
- term.clear()
- if MarquitoLuaUtils.OpenWirelessRednetModem() then
- print("Id of computer / tablet : " .. tostring(computerId))
- print("Welcome to the program allow you to send order to turtles")
- while true do
- -- The turtle type
- local turtleType = MarquitoLuaUtils.TurtleType.NONE
- local nOption = nil
- print("0 -> Exit this program")
- print("1 -> Download program for the turtle")
- print("2 -> Execute turtle program")
- print("3 -> Delete turtle program")
- print("4 -> Stop one or many turtle(s)")
- print("What do you want to do ?")
- while true do
- nOption = tonumber(read())
- if nOption == 0 or nOption == 1 or nOption == 2 or nOption == 3 or nOption == 4 or nOption == 5 then
- break
- else
- print("Incorrect code")
- end
- end
- -- Exit current program
- if nOption == 0 then
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Exit RednetServer")
- break
- -- Download a program
- elseif nOption == 1 then
- -- Ask turtle type
- turtleType = AskTurtleType()
- -- Ask if one, a set of, or all turtle(s)
- turtleChoice = AskIfAllTurtles()
- turtleId = nil
- if turtleChoice == 0 then
- turtleId = AskTurtleId()
- elseif turtleChoice == 1 then
- turtleId = AskTurtleIds()
- end
- -- Ask the program name
- programName = AskProgramName()
- -- Check if the program exist, or if we want to update it before send to turtle(s)
- if not MarquitoLuaUtils.ProgramExistInLocal(programName) or AsIfWeNeedUpdate() then
- pastebinCode = AskPastebinCode()
- DownloadProgram(pastebinCode, programName)
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Download program : " .. programName .. " with pastebin code : " .. pastebinCode)
- end
- -- The program content
- programContent = MarquitoLuaUtils.GetProgramContent(programName)
- SendDownloadProgramOrder(turtleChoice == 2, turtleId, turtleType, programName, programContent)
- -- Execute a program
- elseif nOption == 2 then
- -- Ask turtle type
- turtleType = AskTurtleType()
- -- Ask if one, a set of, or all turtle(s)
- turtleChoice = AskIfAllTurtles()
- turtleId = nil
- if turtleChoice == 0 then
- turtleId = AskTurtleId()
- elseif turtleChoice == 1 then
- turtleId = AskTurtleIds()
- end
- -- Ask the programe name
- programName = AskProgramName()
- SendExecuteProgramOrder(turtleChoice == 2, turtleId, turtleType, programName)
- -- Delete a program
- elseif nOption == 3 then
- -- Ask turtle type
- turtleType = AskTurtleType()
- -- Ask if one, a set of, or all turtle(s)
- turtleChoice = AskIfAllTurtles()
- turtleId = nil
- if turtleChoice == 0 then
- turtleId = AskTurtleId()
- elseif turtleChoice == 1 then
- turtleId = AskTurtleIds()
- end
- -- Ask the programe name
- programName = AskProgramName()
- SendDeleteProgramOrder(turtleChoice == 2, turtleId, turtleType, programName)
- -- exit the rednetClient
- elseif nOption == 4 then
- -- Ask turtle type
- turtleType = AskTurtleType()
- -- Ask if one, a set of, or all turtle(s)
- turtleChoice = AskIfAllTurtles()
- turtleId = nil
- if turtleChoice == 0 then
- turtleId = AskTurtleId()
- elseif turtleChoice == 1 then
- turtleId = AskTurtleIds()
- end
- SendExitOrder(turtleChoice == 2, turtleId, turtleType)
- end
- nOption = nil
- os.sleep(0.5)
- term.clear()
- end
- MarquitoLuaUtils.CloseWirelessRednetModem()
- end
- end
- -- We need this library for other programs work
- DownloadProgram(libraryCode, libraryName)
- os.loadAPI("MarquitoLuaUtils")
- -- Run the rednet client
- parallel.waitForAny(MainProgram, ReceiveData)
- ExitProgram()
Advertisement
Add Comment
Please, Sign In to add comment