Advertisement
Kaikaku

tSend

Apr 10th, 2021 (edited)
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.32 KB | None | 0 0
  1. --{program="tSend",version="1.01",date="2021-04-18"}
  2. ---------------------------------------
  3. -- tSend               by Kaikaku
  4. -- 2021-04-18, v1.01   added info about ""
  5. --                     and os.getComputerID()
  6. -- 2021-04-10, v1.00   initial
  7. ---------------------------------------
  8.  
  9. ---------------------------------------
  10. ---- DESCRIPTION ----------------------
  11. ---------------------------------------
  12. -- A short helper program to send
  13. --   rednet messages, e.g.
  14. --   tSend 1 "my message"
  15.  
  16.  
  17. ---------------------------------------
  18. ---- ASSUMPTIONS ----------------------
  19. ---------------------------------------
  20. -- Turtle or computer
  21. -- If there is no modem attached the
  22. --   program will ask for providing/
  23. --   placing one.
  24.  
  25.  
  26. ---------------------------------------
  27. ---- VARIABLES: template --------------
  28. ---------------------------------------
  29. local cVersion  ="v1.01"              
  30. local cPrgName  ="tSend"          
  31. local blnAskForParameters =true      
  32. local blnDebugPrint       =true
  33. local blnTurtle                
  34. local baseColor=colors.blue    
  35.  
  36.  
  37. ---------------------------------------
  38. ---- VARIABLES: specific --------------
  39. ---------------------------------------
  40. local modem -- set later in code
  41. local slotFuel = 16 -- for modem
  42.  
  43.  
  44. ---------------------------------------
  45. ---- Early UI functions ---------------
  46. ---------------------------------------
  47. local function swapColors()
  48.   local backColor=term.getBackgroundColor()
  49.   local textColor=term.getTextColor()
  50.  
  51.   term.setBackgroundColor(textColor)
  52.   term.setTextColor(backColor)
  53. end
  54.  
  55. local function printUI(strInput)
  56. if strInput==ni then strInput="" end
  57.  
  58.   if strInput=="header" then
  59.     term.write("—ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ") swapColors() print("”") swapColors()  
  60.   elseif strInput=="line" then
  61.      term.write("ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒ") swapColors() print("‘") swapColors()
  62.   elseif strInput=="footer" then
  63.     term.write("ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŽ") print()
  64.   else
  65.     term.write("•")
  66.     strInput=strInput.."                                     "
  67.     term.write(string.sub(strInput,1,37))
  68.     swapColors() print("•") swapColors()
  69.   end
  70. end
  71.  
  72. local function coloredTextAt(inputText,outputRow,outputCol,textColor,backColor)
  73. -- writes and colors text to coordinates
  74. local oldRow, oldCol = term.getCursorPos()
  75. local oldTextColor=term.getTextColor()
  76. local oldBackColor=term.getBackgroundColor()
  77. if textColor==nil then textColor=term.getTextColor() end
  78. if backColor==nil then backColor=term.getBackgroundColor() end
  79.  
  80.   term.setTextColor(textColor)
  81.   term.setBackgroundColor(backColor)
  82.   term.setCursorPos(outputRow,outputCol)
  83.   term.write(inputText)
  84.   term.setCursorPos(oldRow, oldCol)
  85.   term.setTextColor(oldTextColor)
  86.   term.setBackgroundColor(oldBackColor)
  87. end
  88.  
  89. local function debugPrint(str)              
  90.  if blnDebugPrint then print(str) end
  91. end
  92.  
  93. local function checkFuel()                  
  94.   local tmp=turtle.getFuelLevel()
  95.   return tmp
  96. end
  97.  
  98. local function checkTurtle(blnOnlyIdentify)  
  99. if blnOnlyIdentify==nil then blnOnlyIdentify=false end
  100. -- turtle?
  101.   local turtleOk, turtleVal = pcall(checkFuel)
  102.   if not turtleOk then
  103.     blnTurtle=false
  104.     if not blnOnlyIdentify then
  105.       term.clear() term.setCursorPos(1,1)
  106.       printUI("header")
  107.       printUI(""..cPrgName..", "..cVersion..", by Kaikaku")
  108.       printUI("line")
  109.       printUI("This is a turtle program.")
  110.       printUI("  Please, execute it with a turtle!")
  111.       printUI("footer")
  112.    
  113.       coloredTextAt(cPrgName,2,2,baseColor)
  114.       error()
  115.     end
  116.   else
  117.     blnTurtle=true
  118.   end
  119. end
  120.  
  121. local function sleepDots(sec, duration)              
  122. if sec==nil then sec=10 end
  123. if sec<1 then return end
  124. if duration==nil then duration=1 end -- shorten durtation for more dots
  125.  
  126.   for i=1,sec-1 do
  127.     sleep(1*duration)
  128.     term.write(".")
  129.   end
  130.   sleep(1*duration)
  131.   print(".")
  132. end
  133.  
  134. local function getFirstEmptySlot(startAt)              
  135. if startAt==nil then startAt=1 end
  136. if startAt>16 or startAt<1 then return nil end
  137.  
  138.   for i=startAt,16,1 do
  139.     if turtle.getItemCount(i)==0 then return i end  
  140.   end
  141.   for i=1,startAt,1 do
  142.     if turtle.getItemCount(i)==0 then return i end  
  143.   end
  144.   return nil
  145. end
  146.  
  147. local function identifyTool(toolSide)                
  148. -- returns name of tool at side toolSide
  149. -- returns no_tool if there is none
  150. -- requires at least one empty slot for tool check (throws error)
  151. if toolSide==nil then toolSide="right" end
  152. if toolSide~="right" and toolSide~="r" then toolSide="left" end
  153. local slotNumber = getFirstEmptySlot()
  154. local slotSelected=turtle.getSelectedSlot()
  155. local toolName="no_tool"
  156.  
  157.   if slotNumber==nil then error("Couldn't find empty slot to check tool on side '"..toolSide.."'!") end
  158.   turtle.select(slotNumber)
  159.   -- step 1: get name
  160.   if toolSide=="right" or toolSide=="r" then
  161.     turtle.equipRight()
  162.   else
  163.     turtle.equipLeft()  
  164.   end
  165.   -- step 2: get name
  166.   local data = turtle.getItemDetail()
  167.   if data~=nil then toolName=data.name end
  168.   -- step 3: re-equipget name
  169.   if toolSide=="right" or toolSide=="r" then
  170.     turtle.equipRight()
  171.   else
  172.     turtle.equipLeft()  
  173.   end
  174.   turtle.select(slotSelected)
  175.   return toolName
  176. end
  177.  
  178. local function findModem()    
  179.   for _, p in pairs( rs.getSides() ) do
  180.     if peripheral.isPresent(p) and peripheral.getType(p) == "modem" then return true,p end
  181.   end
  182.   return false,nil
  183. end
  184.  
  185.  
  186. local function ensureModem()                
  187. -- Equips modem from slotFuel either right or left, depending which solt is free.
  188. -- Throws an error if both sides are already taken.
  189. -- Return modem side
  190. local modemExists, modemSide = findModem()
  191.  
  192.   if not blnTurtle then                                        
  193.     while not modemExists do
  194.       print("No modem found, attach a modem to computer!")
  195.       sleepDots(3)
  196.       modemExists, modemSide = findModem()
  197.     end
  198.    
  199.   else -- isTurtle
  200.   local slotSelected=turtle.getSelectedSlot()
  201.     while not modemExists do
  202.       turtle.select(slotFuel)
  203.       print("Turtle has no modem, put one wireless modem in slot "..slotFuel.."!")
  204.       sleepDots(3)
  205.       if turtle.getItemCount()== 1 then
  206.         -- check right side
  207.         if identifyTool("right")=="no_tool" then
  208.           turtle.equipRight()
  209.         elseif identifyTool("left")=="no_tool" then
  210.           turtle.equipLeft()
  211.         else
  212.           error("Couldn't find empty tool slot to equip modem! Please, manually remove one tool. E.g., in lua editor with 'turtle.equipLeft()'")
  213.         end
  214.       end  
  215.       modemExists, modemSide = findModem()
  216.     end
  217.     turtle.select(slotSelected)
  218.   end
  219.   return modemSide
  220. end
  221.  
  222.  
  223. ---------------------------------------
  224. ---- tArgs ----------------------------
  225. ---------------------------------------
  226. local tArgs = {...}  
  227.  
  228. -- header
  229. if #tArgs==2 then
  230.   blnAskForParameters=false
  231.   -- pre-checks
  232.   tArgs[1]=tonumber(tArgs[1])  
  233.   if tArgs[1]==nil then
  234.     error("Invalid paramter("..(1)..")='"..(tArgs[1] or "").."'. Only numbers (0-65535) can be used for channel numbers!")
  235.   end  
  236.   tArgs[1]=math.floor(tArgs[1])
  237.   if tArgs[1]<0 or tArgs[1]>65535 then
  238.     error("Invalid paramter("..(1)..")='"..(tArgs[1] or "").."'. Only numbers (0-65535) can be used for channel numbers!")
  239.   end
  240.   -- turtle?
  241.   checkTurtle(true)
  242.   -- modem?
  243.   if modem==nil then modem = peripheral.wrap(ensureModem()) end
  244.   -- send
  245.   modem.transmit(tArgs[1],os.getComputerID(),tArgs[2])
  246.   --modem.close()?
  247. else
  248.   blnAskForParameters=true
  249. end
  250.  
  251.  
  252. ---------------------------------------
  253. -- basic functions for turtle control -
  254. ---------------------------------------
  255.  
  256. local function pressKeyNoSpecials(askText)
  257. -- excludes ctrl / alt / shifts
  258. -- catches windows
  259. -- retruns the key number (if needed at all)
  260. local tmpEvent, tmpKey
  261. if askText==nil then askText="Press key to START! (stop w/ ctrl+t)   " end
  262.   tmpKey=341
  263.   while tmpKey>=340 and tmpKey<=346 do -- ctrls, alts, shifts
  264.     term.write(askText) tmpEvent, tmpKey = os.pullEvent("key")
  265.     if tmpKey==nil then tmpKey=341 end -- win
  266.   end
  267.   return tmpKey
  268. end
  269.  
  270.  
  271. ------------------------------------------------------------------------------
  272. -- main: description ---------------------------------------------------------
  273. ------------------------------------------------------------------------------
  274.  
  275. if blnAskForParameters then
  276. term.clear() term.setCursorPos(1,1)
  277. local iPage=0
  278. local iPageMax=2                                    
  279. local event, key, isHeld  
  280. local blnLoop=true                              
  281.  
  282. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  283. printUI("header")
  284. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  285. printUI("line")
  286. --       1234567890123456789012345678901234567
  287. printUI("This small helper program sends a ")
  288. printUI("  rednet message and can be used with")
  289. printUI("  turtles and computers.")
  290. printUI("If there is no modem attached, the")
  291. printUI("  program will ask for providing /")
  292. printUI("  placing one.")
  293. printUI("The program is very similar to the ")
  294. printUI("  regular rednet.send() command.")
  295. printUI("footer")
  296.  
  297. coloredTextAt(cPrgName,2,2,baseColor)
  298. coloredTextAt("modem",7,17,baseColor)
  299.                              
  300. pressKeyNoSpecials("Press key for next. (stop w/ ctrl+t)   ")  
  301.  
  302.  
  303. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  304. printUI("header")
  305. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  306. printUI("line")
  307. --       1234567890123456789012345678901234567
  308. printUI("Program must be called with ")
  309. printUI("  2 parameters, like: ")
  310. printUI("  ")
  311. printUI("  tSend channel message")
  312. printUI('E.g., tSend 1 Hello')
  313. printUI('  tSend 44 "Sesame open!"')
  314. printUI("  shell.run('tSend 10 \"Time: Noon\"')")
  315. printUI('  (hint: message needs to be in ""!)')
  316. printUI("footer")
  317.  
  318. coloredTextAt(cPrgName,2,2,baseColor)
  319. coloredTextAt("tSend channel message",4,7,baseColor)
  320.                              
  321. pressKeyNoSpecials("Press key to exit.                     ")
  322.  
  323. end
  324.  
  325.  
  326. ------------------------------------------------------------------------------
  327. -- main: program -------------------------------------------------------------
  328. ------------------------------------------------------------------------------
  329.  
  330. -- "The truth is, there is no main program" ;)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement