Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --{program="tSend",version="1.01",date="2021-04-18"}
- ---------------------------------------
- -- tSend by Kaikaku
- -- 2021-04-18, v1.01 added info about ""
- -- and os.getComputerID()
- -- 2021-04-10, v1.00 initial
- ---------------------------------------
- ---------------------------------------
- ---- DESCRIPTION ----------------------
- ---------------------------------------
- -- A short helper program to send
- -- rednet messages, e.g.
- -- tSend 1 "my message"
- ---------------------------------------
- ---- ASSUMPTIONS ----------------------
- ---------------------------------------
- -- Turtle or computer
- -- If there is no modem attached the
- -- program will ask for providing/
- -- placing one.
- ---------------------------------------
- ---- VARIABLES: template --------------
- ---------------------------------------
- local cVersion ="v1.01"
- local cPrgName ="tSend"
- local blnAskForParameters =true
- local blnDebugPrint =true
- local blnTurtle
- local baseColor=colors.blue
- ---------------------------------------
- ---- VARIABLES: specific --------------
- ---------------------------------------
- local modem -- set later in code
- local slotFuel = 16 -- for modem
- ---------------------------------------
- ---- Early UI functions ---------------
- ---------------------------------------
- local function swapColors()
- local backColor=term.getBackgroundColor()
- local textColor=term.getTextColor()
- term.setBackgroundColor(textColor)
- term.setTextColor(backColor)
- end
- local function printUI(strInput)
- if strInput==ni then strInput="" end
- if strInput=="header" then
- term.write("") swapColors() print("") swapColors()
- elseif strInput=="line" then
- term.write("") swapColors() print("") swapColors()
- elseif strInput=="footer" then
- term.write("") print()
- else
- term.write("")
- strInput=strInput.." "
- term.write(string.sub(strInput,1,37))
- swapColors() print("") swapColors()
- end
- end
- local function coloredTextAt(inputText,outputRow,outputCol,textColor,backColor)
- -- writes and colors text to coordinates
- local oldRow, oldCol = term.getCursorPos()
- local oldTextColor=term.getTextColor()
- local oldBackColor=term.getBackgroundColor()
- if textColor==nil then textColor=term.getTextColor() end
- if backColor==nil then backColor=term.getBackgroundColor() end
- term.setTextColor(textColor)
- term.setBackgroundColor(backColor)
- term.setCursorPos(outputRow,outputCol)
- term.write(inputText)
- term.setCursorPos(oldRow, oldCol)
- term.setTextColor(oldTextColor)
- term.setBackgroundColor(oldBackColor)
- end
- local function debugPrint(str)
- if blnDebugPrint then print(str) end
- end
- local function checkFuel()
- local tmp=turtle.getFuelLevel()
- return tmp
- end
- local function checkTurtle(blnOnlyIdentify)
- if blnOnlyIdentify==nil then blnOnlyIdentify=false end
- -- turtle?
- local turtleOk, turtleVal = pcall(checkFuel)
- if not turtleOk then
- blnTurtle=false
- if not blnOnlyIdentify then
- term.clear() term.setCursorPos(1,1)
- printUI("header")
- printUI(""..cPrgName..", "..cVersion..", by Kaikaku")
- printUI("line")
- printUI("This is a turtle program.")
- printUI(" Please, execute it with a turtle!")
- printUI("footer")
- coloredTextAt(cPrgName,2,2,baseColor)
- error()
- end
- else
- blnTurtle=true
- end
- end
- local function sleepDots(sec, duration)
- if sec==nil then sec=10 end
- if sec<1 then return end
- if duration==nil then duration=1 end -- shorten durtation for more dots
- for i=1,sec-1 do
- sleep(1*duration)
- term.write(".")
- end
- sleep(1*duration)
- print(".")
- end
- local function getFirstEmptySlot(startAt)
- if startAt==nil then startAt=1 end
- if startAt>16 or startAt<1 then return nil end
- for i=startAt,16,1 do
- if turtle.getItemCount(i)==0 then return i end
- end
- for i=1,startAt,1 do
- if turtle.getItemCount(i)==0 then return i end
- end
- return nil
- end
- local function identifyTool(toolSide)
- -- returns name of tool at side toolSide
- -- returns no_tool if there is none
- -- requires at least one empty slot for tool check (throws error)
- if toolSide==nil then toolSide="right" end
- if toolSide~="right" and toolSide~="r" then toolSide="left" end
- local slotNumber = getFirstEmptySlot()
- local slotSelected=turtle.getSelectedSlot()
- local toolName="no_tool"
- if slotNumber==nil then error("Couldn't find empty slot to check tool on side '"..toolSide.."'!") end
- turtle.select(slotNumber)
- -- step 1: get name
- if toolSide=="right" or toolSide=="r" then
- turtle.equipRight()
- else
- turtle.equipLeft()
- end
- -- step 2: get name
- local data = turtle.getItemDetail()
- if data~=nil then toolName=data.name end
- -- step 3: re-equipget name
- if toolSide=="right" or toolSide=="r" then
- turtle.equipRight()
- else
- turtle.equipLeft()
- end
- turtle.select(slotSelected)
- return toolName
- end
- local function findModem()
- for _, p in pairs( rs.getSides() ) do
- if peripheral.isPresent(p) and peripheral.getType(p) == "modem" then return true,p end
- end
- return false,nil
- end
- local function ensureModem()
- -- Equips modem from slotFuel either right or left, depending which solt is free.
- -- Throws an error if both sides are already taken.
- -- Return modem side
- local modemExists, modemSide = findModem()
- if not blnTurtle then
- while not modemExists do
- print("No modem found, attach a modem to computer!")
- sleepDots(3)
- modemExists, modemSide = findModem()
- end
- else -- isTurtle
- local slotSelected=turtle.getSelectedSlot()
- while not modemExists do
- turtle.select(slotFuel)
- print("Turtle has no modem, put one wireless modem in slot "..slotFuel.."!")
- sleepDots(3)
- if turtle.getItemCount()== 1 then
- -- check right side
- if identifyTool("right")=="no_tool" then
- turtle.equipRight()
- elseif identifyTool("left")=="no_tool" then
- turtle.equipLeft()
- else
- error("Couldn't find empty tool slot to equip modem! Please, manually remove one tool. E.g., in lua editor with 'turtle.equipLeft()'")
- end
- end
- modemExists, modemSide = findModem()
- end
- turtle.select(slotSelected)
- end
- return modemSide
- end
- ---------------------------------------
- ---- tArgs ----------------------------
- ---------------------------------------
- local tArgs = {...}
- -- header
- if #tArgs==2 then
- blnAskForParameters=false
- -- pre-checks
- tArgs[1]=tonumber(tArgs[1])
- if tArgs[1]==nil then
- error("Invalid paramter("..(1)..")='"..(tArgs[1] or "").."'. Only numbers (0-65535) can be used for channel numbers!")
- end
- tArgs[1]=math.floor(tArgs[1])
- if tArgs[1]<0 or tArgs[1]>65535 then
- error("Invalid paramter("..(1)..")='"..(tArgs[1] or "").."'. Only numbers (0-65535) can be used for channel numbers!")
- end
- -- turtle?
- checkTurtle(true)
- -- modem?
- if modem==nil then modem = peripheral.wrap(ensureModem()) end
- -- send
- modem.transmit(tArgs[1],os.getComputerID(),tArgs[2])
- --modem.close()?
- else
- blnAskForParameters=true
- end
- ---------------------------------------
- -- basic functions for turtle control -
- ---------------------------------------
- local function pressKeyNoSpecials(askText)
- -- excludes ctrl / alt / shifts
- -- catches windows
- -- retruns the key number (if needed at all)
- local tmpEvent, tmpKey
- if askText==nil then askText="Press key to START! (stop w/ ctrl+t) " end
- tmpKey=341
- while tmpKey>=340 and tmpKey<=346 do -- ctrls, alts, shifts
- term.write(askText) tmpEvent, tmpKey = os.pullEvent("key")
- if tmpKey==nil then tmpKey=341 end -- win
- end
- return tmpKey
- end
- ------------------------------------------------------------------------------
- -- main: description ---------------------------------------------------------
- ------------------------------------------------------------------------------
- if blnAskForParameters then
- term.clear() term.setCursorPos(1,1)
- local iPage=0
- local iPageMax=2
- local event, key, isHeld
- local blnLoop=true
- term.clear() term.setCursorPos(1,1) iPage=iPage+1
- printUI("header")
- printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
- printUI("line")
- -- 1234567890123456789012345678901234567
- printUI("This small helper program sends a ")
- printUI(" rednet message and can be used with")
- printUI(" turtles and computers.")
- printUI("If there is no modem attached, the")
- printUI(" program will ask for providing /")
- printUI(" placing one.")
- printUI("The program is very similar to the ")
- printUI(" regular rednet.send() command.")
- printUI("footer")
- coloredTextAt(cPrgName,2,2,baseColor)
- coloredTextAt("modem",7,17,baseColor)
- pressKeyNoSpecials("Press key for next. (stop w/ ctrl+t) ")
- term.clear() term.setCursorPos(1,1) iPage=iPage+1
- printUI("header")
- printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
- printUI("line")
- -- 1234567890123456789012345678901234567
- printUI("Program must be called with ")
- printUI(" 2 parameters, like: ")
- printUI(" ")
- printUI(" tSend channel message")
- printUI('E.g., tSend 1 Hello')
- printUI(' tSend 44 "Sesame open!"')
- printUI(" shell.run('tSend 10 \"Time: Noon\"')")
- printUI(' (hint: message needs to be in ""!)')
- printUI("footer")
- coloredTextAt(cPrgName,2,2,baseColor)
- coloredTextAt("tSend channel message",4,7,baseColor)
- pressKeyNoSpecials("Press key to exit. ")
- end
- ------------------------------------------------------------------------------
- -- main: program -------------------------------------------------------------
- ------------------------------------------------------------------------------
- -- "The truth is, there is no main program" ;)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement