Kaikaku

bDrone

Mar 6th, 2021 (edited)
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 32.04 KB | None | 0 0
  1. --{program="bDrone",version="1.01",date="2021-03-15"}
  2. ---------------------------------------
  3. -- bDrone           by Kaikaku
  4. -- 2021-03-15, v1.01   desription
  5. -- 2021-03-13, v1.00   initial
  6. -- A35d84ge
  7. ---------------------------------------
  8.  
  9. ---------------------------------------
  10. ---- DESCRIPTION ----------------------
  11. ---------------------------------------
  12. -- Builds at build height limit either
  13. --   a prank drone that drops every
  14. --   morning an item or a rapid fire
  15. --   drone that shoots via wireless
  16. --   commands up to 2880 arrows.
  17. -- For more details see info screens
  18. --   in program and YouTube video.
  19.  
  20.  
  21. ---------------------------------------
  22. ---- ASSUMPTIONS ----------------------
  23. ---------------------------------------
  24. -- Standard turtle is okay.
  25. -- Needs crafty turtle for RFD.
  26. -- Needs modem to send fire command.
  27.  
  28.  
  29. ---------------------------------------
  30. ---- PARAMETERS -----------------------
  31. ---------------------------------------
  32. local cVersion  ="v1.01"      
  33. local cPrgName  ="bDrone"      
  34. local cMinFuel  =600          
  35. local cSleepTime=60+5          
  36. local cMaxTurns =-1            
  37. local turnCounter =0            
  38. local turnsRemaining =cMaxTurns
  39. local harvestedStuff=0          
  40. local harvestedStuffTotal=0          
  41. local blnAskForParameters =true        
  42. local blnDebugPrint       =false--true--  
  43. local blnRefill=false
  44. local blnFire=false
  45. local amountFire=0
  46. local blnRFD=false
  47.  
  48.  
  49. ---------------------------------------
  50. ---- VARIABLES ------------------------
  51. ---------------------------------------
  52. local slotDropper=1
  53. local slotSensor=2
  54. local slotTrapdoor=3
  55. local slotCarpet=4
  56. local slotJunkStart=5
  57. local slotJunkEnd=13
  58. local slotModem=14
  59. local slotDiskDrive=15
  60. local slotDisk=16
  61. local slotFuel=16
  62.  
  63. local upSteps=0
  64.  
  65. local blockDropper = {}
  66. blockDropper[1]="minecraft:dropper"
  67. blockDropper[2]="minecraft:dispenser"
  68.  
  69. local isComputer=false
  70.  
  71. ---------------------------------------
  72. ---- Early UI functions ---------------
  73. ---------------------------------------
  74. local function swapColors()
  75. local backColor=term.getBackgroundColor()
  76. local textColor=term.getTextColor()
  77.  
  78. term.setBackgroundColor(textColor)
  79. term.setTextColor(backColor)
  80. end
  81.  
  82. local function printUI(strInput)
  83. if strInput==ni then strInput="" end
  84.  
  85.   if strInput=="header" then
  86.     term.write("—ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ") swapColors() print("”") swapColors()  
  87.   elseif strInput=="line" then
  88.      term.write("ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒ") swapColors() print("‘") swapColors()
  89.   elseif strInput=="footer" then
  90.     term.write("ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŽ") print()
  91.   else
  92.     term.write("•")
  93.     strInput=strInput.."                                     "
  94.     term.write(string.sub(strInput,1,37))
  95.     swapColors() print("•") swapColors()
  96.   end
  97. end
  98.  
  99. local function coloredTextAt(inputText,outputRow,outputCol,textColor,backColor)
  100. -- writes and colors text to coordinates
  101. local oldRow, oldCol = term.getCursorPos()
  102. local oldTextColor=term.getTextColor()
  103. local oldBackColor=term.getBackgroundColor()
  104. if textColor==nil then textColor=term.getTextColor() end
  105. if backColor==nil then backColor=term.getBackgroundColor() end
  106.  
  107.   term.setTextColor(textColor)
  108.   term.setBackgroundColor(backColor)
  109.   term.setCursorPos(outputRow,outputCol)
  110.   term.write(inputText)
  111.   term.setCursorPos(oldRow, oldCol)
  112.   term.setTextColor(oldTextColor)
  113.   term.setBackgroundColor(oldBackColor)
  114. end
  115.  
  116.  
  117. ---------------------------------------
  118. ---- tArgs ----------------------------
  119. ---------------------------------------
  120. local tArgs = {...}  
  121. local paraIdentified=false            
  122. local paraNumberCount=0              
  123. term.clear() term.setCursorPos(1,1)
  124.  
  125. -- header
  126. if #tArgs~=0 then
  127.   printUI("header")
  128.   printUI(""..cPrgName..", "..cVersion..", by Kaikaku. Enjoy!")
  129.   printUI("line")
  130.   print("Starting...")
  131. end
  132.  
  133. -- check parameters
  134. for i=1,#tArgs do
  135.   blnAskForParameters=false
  136.   paraIdentified=false
  137.   -- rfd-rapid fire drone?
  138.   if not paraIdentified  then
  139.     if string.lower(tArgs[i])=="rfd" then
  140.       paraIdentified=true blnRFD=true
  141.       blnAskForParameters=false
  142.       print("Option: build Rapid Fire Drone!")
  143.     end
  144.   end
  145.   -- fire?
  146.   if not paraIdentified  then
  147.     if string.lower(tArgs[i])=="fire" then
  148.       if #tArgs<i+1 then error("Parameter 'fire' requires number (e.g., "..cPrgName.." fire 100)") end
  149.       amountFire=tonumber(tArgs[i+1])
  150.       if amountFire==nil then error("Parameter 'fire' requires number (e.g., "..cPrgName.." fire 100)") end
  151.       if amountFire<0 then amountFire=1 end
  152.       paraIdentified=true blnFire=true
  153.       blnAskForParameters=false
  154.       print("Option: fire drone!")
  155.       break
  156.     end
  157.   end
  158.   -- refill?
  159.   if not paraIdentified  then
  160.     if string.lower(tArgs[i])=="refill" or string.lower(tArgs[i])=="refil" then
  161.       paraIdentified=true blnRefill=true
  162.       blnAskForParameters=false
  163.       print("Option: refill a drone!")
  164.     end
  165.   end
  166.   -- max movement up
  167.   if not paraIdentified  then
  168.     if paraNumberCount==0 then
  169.       cMaxTurns=tonumber(tArgs[i]) -- used to cap hight
  170.       paraIdentified=true
  171.       paraNumberCount=paraNumberCount+1
  172.       blnAskForParameters=false
  173.       print("Option: cap movement up at "..cMaxTurns)
  174.     end
  175.   end
  176.   if not paraIdentified then
  177.     error("Error: Unknown parameter "..i..":'"..tArgs[i].."'")
  178.   end
  179. sleep(0.3)
  180. end
  181. if blnFire then
  182.   -- fire has high priority
  183.   if cMaxTurns>0 then cMaxTurns=-1 print("Info: fire overrides max up movement.") end
  184.   if blnRefill then blnRefill=false print("Info: fire overrides refill.") end
  185. end
  186. if cMaxTurns>0 and blnRefill then
  187.   -- refill has no hight max
  188.   cMaxTurns=-1
  189.   print("Info: refill overrides max up movement.")
  190. end
  191.  
  192.  
  193. ---------------------------------------
  194. -- basic functions for turtle control -
  195. ---------------------------------------
  196. -- 2021-03-05 stuff                   -
  197. -- 2021-02-06 select+place            -
  198. -- 2021-01-29 save turtle go func     -
  199. --            checkInventory w/ exit  -
  200. --            refuelManager           -
  201. ---------------------------------------
  202. local function gf(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.forward()   do end end end
  203. local function gb(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.back()      do end end end
  204. local function gu(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.up()        do end end end
  205. local function gd(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.down()      do end end end
  206. local function gl(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.turnLeft()  do end end end
  207. local function gr(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.turnRight() do end end end
  208.  
  209. local function df()  turtle.dig()       end
  210. local function du()  turtle.digUp()     end
  211. local function dd()  turtle.digDown()   end
  212.  
  213. local function pf()  return turtle.place()     end
  214. local function pu()  return turtle.placeUp()   end
  215. local function pd()  return turtle.placeDown() end
  216.  
  217. local function sf()  turtle.suck()      end
  218. local function su()  turtle.suckUp()    end
  219. local function sd()  turtle.suckDown()  end
  220. local function Df()  turtle.drop()      end
  221. local function Du(n) turtle.dropUp(n)  end
  222. local function Dd()  turtle.dropDown()  end
  223. local function ss(s) turtle.select(s)   end
  224. local function gic(s) return turtle.getItemCount(s) end
  225.  
  226. local function gfs(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.forward() do df() end end end
  227. local function gbs(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.back()    do gl() gl() df() gr() gr() end end end
  228. local function gus(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.up()      do du() end end end
  229. local function gds(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.down()    do dd() end end end
  230. local function pfs() df() turtle.place()     end
  231. local function pus() du() turtle.placeUp()   end
  232. local function pds() dd() turtle.placeDown() end
  233.  
  234. local function gfr() return turtle.forward() end  -- <<-- add to template
  235. local function gbr() return turtle.back()    end
  236. local function gur() return turtle.up()      end
  237. local function gdr() return turtle.down()    end
  238.  
  239. local function spd(s,blnForward)
  240.   if s==nil then s=turtle.currentSlot() end
  241.   if blnForward==nil then blnForward=true end
  242.   ss(s) pd() if blnForward then gf() end
  243. end
  244. local function spu(s,blnForward)
  245.   if s==nil then s=turtle.currentSlot() end
  246.   if blnForward==nil then blnForward=true end
  247.   ss(s) pu() if blnForward then gf() end
  248. end
  249. local function spf(s,blnBack)
  250.   if s==nil then s=turtle.currentSlot() end
  251.   if blnBack==nil then blnBack=true end
  252.   ss(s) pf() if blnBack then gb() end
  253. end
  254.  
  255. local function waitKey(strText)
  256.   local event, scancode
  257.   write(strText)
  258.   event, scancode = os.pullEvent("key")
  259.   print()
  260. end
  261.  
  262. local function askForInputText(textt)
  263.   local at=""
  264.   -- check prompting texts
  265.   if textt==nil then textt="Enter text:" end
  266.  
  267.   -- ask for input
  268.   write(textt)
  269.   at=read()
  270.   return at
  271. end
  272.  
  273. local function askForNumber(askText, minValue, maxValue)
  274. -- gets entered data, ensures it's a number and returns it
  275. -- keeps asking if entry is not a number
  276. -- adapts to min and max values
  277. -- allways writes in screen line 13 (last for turtles)
  278. -- calls askForInputText
  279. local blnReask=true
  280. local returnNumber=nil
  281. if minValue==nil then minValur=1 end
  282. if maxValue==nil then maxValue=100 end
  283. if askText==nil then askText="Key in number and press Enter: " end
  284.   while blnReask do
  285.    term.setCursorPos(1,13)
  286.     returnNumber=askForInputText(askText)
  287.     if returnNumber==nil then
  288.       blnReask=true
  289.     else  
  290.       returnNumber=tonumber(returnNumber)
  291.       if returnNumber==nil then
  292.         blnReask=true
  293.       else
  294.         returnNumber=math.floor(returnNumber)
  295.         if returnNumber>maxValue then returnNumber=maxValue end
  296.         if returnNumber<minValue then returnNumber=minValue end
  297.         blnReask=false
  298.       end
  299.     end
  300.   end
  301.   return returnNumber
  302. end
  303.  
  304. local function pressKeyNoSpecials(askText)
  305. -- excludes ctrl / alt / shifts
  306. -- catches windows
  307. -- retruns the key number (if needed at all)
  308. local tmpEvent, tmpKey
  309. if askText==nil then askText="Press key to START! (stop w/ ctrl+t)   " end
  310.   tmpKey=341
  311.   while tmpKey>=340 and tmpKey<=346 do -- ctrls, alts, shifts
  312.     term.write("Press key to START! (stop w/ ctrl+t)   ") tmpEvent, tmpKey = os.pullEvent("key")
  313.     if tmpKey==nil then tmpKey=341 end -- win
  314.   end
  315.   return tmpKey
  316. end
  317.  
  318. local function checkFuel()
  319.   local tmp=turtle.getFuelLevel()
  320.   return tmp
  321. end
  322.  
  323. local function checkTurtle()
  324. -- turtle?
  325.   local turtleOk, turtleVal = pcall(checkFuel)
  326.   if not turtleOk then
  327.     term.clear() term.setCursorPos(1,1)
  328.    
  329.     if blnFire then isComputer=true return end
  330.    
  331.     printUI("header")                                            
  332.     printUI(""..cPrgName..", "..cVersion..", by Kaikaku")
  333.     printUI("line")
  334.     printUI("This is mainly a turtle program.")
  335.     printUI("  Please, execute it with a turtle!")
  336.     printUI("")
  337.     printUI("However, you can use computers to")
  338.     printUI("  send fire commands to rapid fire")
  339.     printUI("  drones. Do this with the command:")
  340.     printUI("")
  341.     printUI("  "..cPrgName.." fire xxx")
  342.     printUI("")
  343.     printUI("  with xxx=number of redstone pulses")
  344.     printUI("")
  345.     printUI("Enjoy! ")
  346.     printUI("footer")
  347.    
  348.     coloredTextAt(cPrgName,2,2,colors.red)
  349.     coloredTextAt("fire commands",9,8,nil,colors.red)
  350.     coloredTextAt(cPrgName.." fire xxx",4,11,colors.red)
  351.     coloredTextAt("",9,15,colors.red)
  352.     error()
  353.   end
  354. end
  355.  
  356.  
  357. local function sleepDots(sec)  
  358. if sec==nil then sec=10 end
  359. if sec<1 then return end
  360.  
  361.   for i=1,sec-1 do
  362.     sleep(1)
  363.     term.write(".")
  364.   end
  365.  
  366.   sleep(1)
  367.   print(".")
  368. end
  369.  
  370. local function checkInventory(s, nMin, nMax, textt, blnExitOnFalse, blnRepeat)
  371. -- checks if in slot s are not less than nMin and not more than nMax items
  372. -- returns true if okay
  373. -- if not displays textt
  374. -- blnExitOnFalse=true raises an error
  375. -- blnRepeat=true repeatedly sks to put in the right number of items (overrides blnExitOnFalse)
  376. local oldSlot = turtle.getSelectedSlot()
  377. if s==nil    then s=turtle.getSelectedSlot() end
  378. if nMin==nil then nMin=0  end
  379. if nMax==nil then nMax=64 end
  380. if blnExitOnFalse==nil then blnExitOnFalse=false end
  381. if blnRepeat==nil then blnRepeat=false end
  382. if blnRepeat~=true then blnRepeat=false end
  383.  
  384.   while true do
  385.  
  386.     if turtle.getItemCount(s)<nMin or turtle.getItemCount(s)>nMax then
  387.       print(textt)
  388.       if not blnRepeat then
  389.         -- single check ends with this
  390.         if blnExitOnFalse then
  391.           error()
  392.         else
  393.           ss(oldSlot) return false
  394.         end
  395.       end
  396.       -- repeated check
  397.       ss(s)
  398.       sleepDots(3)
  399.     else
  400.       -- everything is fine
  401.       ss(oldSlot) return true
  402.     end
  403.   end
  404. end
  405.  
  406. local function refuelFromSlot(s, n) -- slot, amount to consume
  407. if s==nil then s=16 end
  408. if n==nil then n=64 end
  409. local currentSlot = turtle.getSelectedSlot()
  410. local fuelItems = turtle.getItemCount(s)
  411. local returnValue = false
  412.  
  413.   if fuelItems>0 then
  414.     ss(s)
  415.     returnValue=turtle.refuel(n)
  416.     ss(currentSlot)
  417.   end
  418.   return returnValue
  419. end
  420.  
  421. local function refuelManager(setMinFuel,setSlotFuel,waitTime)
  422.   local currentSlotSelected=turtle.getSelectedSlot()
  423.   ss(setSlotFuel)
  424.   while turtle.getFuelLevel()<setMinFuel do
  425.     print("Need more fuel ("..turtle.getFuelLevel().."/"..setMinFuel..").")
  426.     if not refuelFromSlot(setSlotFuel) then
  427.       -- unsucessfull try
  428.       print("  Please, put fuel items in slot "..setSlotFuel.."!")
  429.       term.write("  Sleeping "..waitTime.." seconds")
  430.       sleepDots(waitTime)
  431.     else
  432.       print("Refueled...")
  433.     end
  434.   end
  435.   ss(currentSlotSelected)
  436. end
  437.  
  438. local function debugPrint(str1,str2,str3,str4)
  439. if blnDebugPrint then
  440.   if str1==nil then str1="" end term.write(str1)
  441.   if str2==nil then str2="" end term.write(str2)
  442.   if str3==nil then str3="" end term.write(str3)
  443.   if str4==nil then str4="" end print(str4)
  444. end
  445. end
  446.  
  447. local function inspectFor(blockArray, strDirection)
  448. if strDirection==nil then strDirection="f" end
  449. local blnOk, data
  450.  
  451.   -- inspect
  452.   if strDirection=="d" then blnOk, data = turtle.inspectDown()
  453.   elseif strDirection=="u" then blnOk, data = turtle.inspectUp()
  454.   elseif strDirection=="f" then blnOk, data = turtle.inspect()
  455.   else
  456.     print("Warning: Unknown direction '",strDirection,"' in inspectFor, taking (f)orward instead.")
  457.     strDirection="f"
  458.     blnOk, data = turtle.inspect()
  459.   end
  460.   if data.name~=nil then debugPrint("Found:"..string.lower(data.name)) end
  461.   -- compare
  462.   local i=1
  463.   while blockArray[i]~=nil do
  464.   debugPrint("Compare to:"..string.lower(blockArray[i]))
  465.     if data.name~=nil then
  466.       if string.lower(data.name) == string.lower(blockArray[i]) then return true end
  467.     end
  468.     i=i+1
  469.   end
  470.  
  471.   return false -- reached a nil value
  472. end
  473.  
  474. local function prepareStartup()
  475.   -- step 1: startup program for disk/startup
  476.   fs.delete("disk/startup")
  477.   file = fs.open("disk/startup","w")
  478.   file.write(
  479.   [[
  480.   -- copy prg. to computer as startup
  481.   fs.delete("startup")
  482.   fs.copy("disk/aDroneFire","startup")
  483.   fs.delete("disk/startup")
  484.   print("reboot in 10 seconds") sleep(10)
  485.   os.reboot()
  486.   ]]
  487.   )
  488.   file.close()
  489.  
  490.   -- step 2: program to be installed as startup on computer
  491.   fs.delete("disk/aDroneFire")
  492.   file = fs.open("disk/aDroneFire","w")
  493.   file.write([[
  494. ---------------------------------------
  495. -- bDrone_listener     by Kaikaku
  496. -- 2021-03-13, v1.00   initial
  497. ---------------------------------------
  498.  
  499. ---------------------------------------
  500. ---- DESCRIPTION ----------------------
  501. ---------------------------------------
  502. -- This program is installed by bDrone
  503. --   to enable the computer on a RFD
  504. --   to receive fire commands
  505.  
  506. shell.run("label set RapidFireDrone")
  507. term.clear() term.setCursorPos(1,1)
  508. print("Rapid Fire Drone: Listener active")
  509. local numShots = 10
  510.  
  511. local tArgs = {...}
  512. if #tArgs >= 1 then -- no error check
  513.   if tonumber(tArgs[1])~=nil then
  514.     numShots=tonumber(tArgs[1])
  515.   end
  516. end
  517.  
  518. local function setRedstone(blnValue)
  519.   rs.setOutput("bottom",blnValue)
  520.   rs.setOutput("left",blnValue)
  521.   rs.setOutput("right",blnValue)
  522.   rs.setOutput("back",blnValue)
  523.   rs.setOutput("front",blnValue)
  524. end
  525.  
  526. local function shoot(number)
  527. if number==nil then number = 1 elseif number <1 then number = 1 end
  528.   for i=1,number do
  529.     setRedstone(true)
  530.     os.sleep(0.1)
  531.     setRedstone(false)
  532.     os.sleep(0.1)
  533.   end
  534. end
  535.  
  536. -- MAIN
  537. local modem = peripheral.wrap("top")
  538. modem.open(1234)
  539.  
  540. local a,b,c,d,e,f
  541. while true do
  542.   while true do
  543.     a,b,c,d,e,f = os.pullEvent("modem_message")
  544.     --if a~=nil then print("a:",a) end
  545.     --if b~=nil then print("b:",b) end
  546.     --if c~=nil then print("c:",c) end
  547.     --if d~=nil then print("d:",d) end
  548.     --if e~=nil then print("e:",e) end
  549.     --if f~=nil then print("f:",f) end
  550.  
  551.     if c==1234 and e=="abcefd_fire" then numShots=d print("Fire "..d.." times! ") break end
  552.   end
  553.   shoot(numShots)
  554. end
  555. ]])
  556. end
  557.  
  558. ------------------------------------------------------------------------------
  559. -- main: description ---------------------------------------------------------
  560. ------------------------------------------------------------------------------
  561. checkTurtle()
  562.  
  563. if blnAskForParameters then
  564. term.clear() term.setCursorPos(1,1)
  565. local iPage=0
  566. local iPageMax=2  
  567. local event, key, isHeld  
  568. local blnLoop=true                              
  569.  
  570. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  571. printUI("header")
  572. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  573. printUI("line")
  574. --       1234567890123456789012345678901234567
  575. printUI("This program builds and operates a ")
  576. printUI("  prank drone. Select mode:")
  577. printUI("  ")
  578. printUI("  1 = build regular drone")
  579. printUI("  2 = build rapid fire drone")
  580. printUI("  3 = refill drone")
  581. printUI("  4 = fire rapid fire drone")
  582. printUI("  ")
  583. printUI("footer")
  584. term.write("Press key 1,2,3,4 to select mode")
  585.  
  586. coloredTextAt(cPrgName,2,2,colors.red)
  587. coloredTextAt("Select mode",17,5,colors.red)
  588. coloredTextAt("1",4,7,colors.red)
  589. coloredTextAt("2",4,8,colors.red)
  590. coloredTextAt("3",4,9,colors.red)
  591. coloredTextAt("4",4,10,colors.red)
  592. while blnLoop do -- option selection example --<< put in template
  593.   event, key, isHeld = os.pullEvent("key")
  594.   if key~=nil then
  595.     if keys.getName(key)=="one" or keys.getName(key)=="two" or keys.getName(key)=="three" or keys.getName(key)=="four" then
  596.       blnLoop=false
  597.     end
  598.   end
  599. end
  600.  
  601.  
  602. if keys.getName(key)=="one" then
  603. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  604. printUI("header")
  605. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  606. printUI("line")
  607. --       1234567890123456789012345678901234567
  608. printUI("Build drone, inventory set-up:")
  609. printUI("  sl.1: 1+ dispenser/dropper")
  610. printUI("  sl.2: 1+ daylight detector")
  611. printUI("  sl.3: 4+ iron trapdoors (opt.)")
  612. printUI("  sl.4: 4+ carpets (optional)")
  613. printUI("  sl.5-13: junk items the drone")
  614. printUI("    will drop/dispens every morning.")
  615. printUI("    Slots 6-12 are optional.")
  616. printUI("footer")
  617.  
  618. coloredTextAt(cPrgName,2,2,colors.red)
  619. coloredTextAt("Build drone",2,4,nil,colors.red)
  620. coloredTextAt("1",10,5,colors.red) coloredTextAt("dispenser/dropper",13,5,colors.red)
  621. coloredTextAt("1",10,6,colors.red) coloredTextAt("daylight detector",13,6,colors.red)
  622.                                    coloredTextAt("junk items",13,9,colors.red)
  623.  
  624. pressKeyNoSpecials("Press key to START! (stop w/ ctrl+t)   ")
  625. end
  626.  
  627.  
  628. if keys.getName(key)=="two" then
  629. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  630. printUI("header")
  631. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  632. printUI("line")
  633. --       1234567890123456789012345678901234567
  634. printUI("Build rapid fire drone, inventory:")
  635. printUI("  sl.1: 5+ dispensers/droppers")
  636. printUI("  sl.2: 1  computer")
  637. printUI("  sl.3: 4+ iron trapdoors (opt.)")
  638. printUI("  sl.4: 4+ carpets (optional)")
  639. printUI("  sl.5-13: arrows/junk items")
  640. printUI("  sl.14: 1 wireless/ender modem")
  641. printUI("  sl.15: 1 disk drive | sl.16: 1 disk")
  642. printUI("footer")
  643.  
  644. coloredTextAt(cPrgName,2,2,colors.red)
  645. coloredTextAt("Build rapid fire drone",2,4,nil,colors.red)
  646. coloredTextAt("5",10,5,colors.red) coloredTextAt("dispensers",13,5,colors.red)
  647. coloredTextAt("1",10,6,colors.red) coloredTextAt("computer",13,6,colors.red)
  648.                                    coloredTextAt("arrows",13,9,colors.red)
  649. coloredTextAt("1",11,10,colors.red) coloredTextAt("modem",28,10,colors.red)
  650. coloredTextAt("1",11,11,colors.red) coloredTextAt("disk drive",13,11,colors.red)
  651. coloredTextAt("1",33,11,colors.red) coloredTextAt("disk",35,11,colors.red)
  652.  
  653. pressKeyNoSpecials("Press key to START! (stop w/ ctrl+t)   ")
  654. blnRFD=true
  655. end
  656.  
  657.  
  658. if keys.getName(key)=="three" then
  659. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  660. printUI("header")
  661. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  662. printUI("line")
  663. --       1234567890123456789012345678901234567
  664. printUI("Refill existing drone, inventory:")
  665. printUI("A) Fill turtle with items to")
  666. printUI("   be put in drone (junk/arrows)")
  667. printUI("B) And/or put chest with items ")
  668. printUI("   in front of turtle")
  669. printUI("Info: For rapid fire drones you may")
  670. printUI("   use up to 3 turtles for quicker")
  671. printUI("   refill (give head start).")
  672. printUI("footer")
  673.  
  674. coloredTextAt(cPrgName,2,2,colors.red)
  675. coloredTextAt("Refill",2,4,nil,colors.red)
  676. coloredTextAt("A)",2,5,colors.red)
  677. coloredTextAt("B)",2,7,colors.red)
  678.  
  679. pressKeyNoSpecials("Press key to START! (stop w/ ctrl+t)   ")
  680. blnRefill=true
  681. end
  682.  
  683.  
  684. if keys.getName(key)=="four" then
  685. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  686. printUI("header")
  687. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  688. printUI("line")
  689. --       1234567890123456789012345678901234567
  690. printUI("Fire rapid fire drone:")
  691. printUI("  ")
  692. printUI("Enter number (1-576) of redstone")
  693. printUI("  pulses. Eachs pulse will dispense")
  694. printUI("  5 items (e.g., shot 5 arrows).")
  695. printUI("  ")
  696. printUI("If drone runs dry use refill option.")
  697. printUI("  ")
  698. printUI("footer")
  699. term.write("Key in number and press Enter: ") -- required to catch option number from 1st screen
  700.  
  701. coloredTextAt(cPrgName,2,2,colors.red)
  702. coloredTextAt("Fire",2,4,nil,colors.red)
  703. coloredTextAt("Enter number",2,6,colors.red)
  704.  
  705. event, key, isHeld = os.pullEvent("key")
  706.  
  707. amountFire=askForNumber("Key in number and press Enter: ",1,576)
  708. blnFire=true
  709. end
  710.  
  711. end
  712.  
  713. local function getFirstEmptySlot(startAt)  -- <<-- put in template
  714. if startAt==nil then startAt=1 end
  715. if startAt>16 or startAt<1 then return nil end
  716.  
  717.   for i=startAt,16,1 do
  718.     if gic(i)==0 then return i end  
  719.   end
  720.   for i=1,startAt,1 do
  721.     if gic(i)==0 then return i end  
  722.   end
  723.   return nil
  724. end
  725.  
  726. local function identifyTool(toolSide)           --   <<-- add to template
  727. -- returns name of tool at side toolSide
  728. -- returns no_tool if there is none
  729. -- requires at least one empty slot for tool check (throws error)
  730. if toolSide==nil then toolSide="right" end
  731. if toolSide~="right" and toolSide~="r" then toolSide="left" end
  732. local slotNumber = getFirstEmptySlot()
  733. local slotSelected=turtle.getSelectedSlot()
  734. local toolName="no_tool"
  735.  
  736.   if slotNumber==nil then error("Couldn't find empty slot to check tool on side '"..toolSide.."'!") end
  737.   ss(slotNumber)
  738.   -- step 1: get name
  739.   if toolSide=="right" or toolSide=="r" then
  740.     turtle.equipRight()
  741.   else
  742.     turtle.equipLeft()  
  743.   end
  744.   -- step 2: get name
  745.   local data = turtle.getItemDetail()
  746.   if data~=nil then toolName=data.name end
  747.   -- step 3: re-equipget name
  748.   if toolSide=="right" or toolSide=="r" then
  749.     turtle.equipRight()
  750.   else
  751.     turtle.equipLeft()  
  752.   end
  753.   ss(slotSelected)
  754.   return toolName
  755. end
  756.  
  757.  
  758. local function findModem() -- delete if no modem useage                      <<-- add to template
  759.   for _, p in pairs( rs.getSides() ) do
  760.     if peripheral.isPresent(p) and peripheral.getType(p) == "modem" then return true,p end
  761.   end
  762.   return false,nil
  763. end
  764.  
  765.  
  766. local function ensureModem() -- delete if no modem useage                      <<-- add to template
  767. -- Equips modem from slotFuel either right or left, depending which solt is free.
  768. -- Throws an error if both sides are already taken.
  769. -- Return modem side
  770. local modemExists, modemSide = findModem()
  771.  
  772.   if isComputer then
  773.     while not modemExists do
  774.       print("No modem found, attach a modem to computer!")
  775.       sleepDots(3)
  776.       modemExists, modemSide = findModem()
  777.     end
  778.    
  779.   else -- isTurtle
  780.   local slotSelected=turtle.getSelectedSlot()
  781.     while not modemExists do
  782.       ss(slotFuel)
  783.       print("Turtle has no modem, put one wireless modem in slot "..slotFuel.."!")
  784.       sleepDots(3)
  785.       if gic()== 1 then
  786.         -- check right side
  787.         if identifyTool("right")=="no_tool" then
  788.           turtle.equipRight()
  789.         elseif identifyTool("left")=="no_tool" then
  790.           turtle.equipLeft()
  791.         else
  792.           error("Could find empty tool slot to equip modem! Please, manually remove one tool. E.g., in lua editor with 'turtle.equipLeft()'")
  793.         end
  794.       end  
  795.       modemExists, modemSide = findModem()
  796.     end
  797.     ss(slotSelected)
  798.   end
  799.   return modemSide
  800. end
  801.  
  802. ------------------------------------------------------------------------------
  803. -- main: pre-main options ----------------------------------------------------
  804. ------------------------------------------------------------------------------
  805.  
  806. if blnFire then
  807.   term.clear() term.setCursorPos(1,1)
  808.   local modem = peripheral.wrap(ensureModem())
  809.   modem.transmit(1234,amountFire,"abcefd_fire")
  810.   print("Fire ("..amountFire..") command sent! :)")
  811.   return
  812. end
  813.  
  814. ------------------------------------------------------------------------------
  815. -- main: program -------------------------------------------------------------
  816. ------------------------------------------------------------------------------
  817. term.clear() term.setCursorPos(1,1)
  818.  
  819. ---- step 0: check starting stuff ----  
  820. print("step 0: checking inventory...")
  821.                      
  822. if not blnRefill then
  823.   if not blnRFD then
  824.     -- normal drone inventory
  825.     checkInventory(slotDropper,   1,nil,"Put 1+ dispenser/dropper in slot "..slotDropper.."!", true, true)
  826.     checkInventory(slotSensor,    1,nil,"Put 1+ daylight detector in slot "..slotSensor.."!", true, true)
  827.     checkInventory(slotTrapdoor,  4,nil,"Option: Put 4+ trapdoorse in slot "..slotTrapdoor.."!", false)
  828.     checkInventory(slotCarpet,    4,nil,"Option: Put 4+ carpets in slot "..slotCarpet.."!", false)
  829.     checkInventory(slotJunkStart, 1,nil,"Put some items to be dropped in slot "..slotJunkStart.."!", true, true)
  830.     sleep(1)
  831.   else
  832.     -- RFD inventory
  833.     checkInventory(slotDropper,   5,nil,"Put 5+ dispenser/dropper in slot "..slotDropper.."!", true, true)
  834.     checkInventory(slotSensor,    1,  1,"Put 1+ computer in slot "..slotSensor.."!", true, true)
  835.     checkInventory(slotTrapdoor,  4,nil,"Option: Put 4+ trapdoorse in slot "..slotTrapdoor.."!", false)
  836.     checkInventory(slotCarpet,    4,nil,"Option: Put 4+ carpets in slot "..slotCarpet.."!", false)
  837.     checkInventory(slotJunkStart, 1,nil,"Put some items to be dropped in slot "..slotJunkStart.."!", true, true)
  838.     checkInventory(slotModem,     1,  1,"Put 1 wireless/ender modem in slot "..slotModem.."!", true, true)
  839.     checkInventory(slotDiskDrive, 1,  1,"Put 1 disk drive in slot "..slotDiskDrive.."!", true, true)
  840.     checkInventory(slotDisk,      1,  1,"Put 1 floppy disk in slot "..slotDisk.."!", true, true)
  841.     sleep(1)
  842.   end
  843. else
  844.   blnLoop=true
  845.   while blnLoop do
  846.     for i=1,16 do sf() end
  847.     blnLoop=true
  848.     for i=1,16 do
  849.       if gic(i)>0 then blnLoop=false break end
  850.     end
  851.    
  852.     if blnLoops then
  853.       print("Put some items into turtle or chest in front of it!")
  854.       sleepDots(5)
  855.     end
  856.   end
  857. end
  858.  
  859. -- fuel
  860. refuelManager(cMinFuel,slotFuel,10)  
  861. -- recheck floppy disk?
  862. if blnRFD then checkInventory(slotDisk,      1,  1,"Put 1 floppy disk back in slot "..slotDisk.."!", true, true) end
  863.  
  864. ---- step 1: go up ----
  865. term.clear() term.setCursorPos(1,1) -- clean up in case of warnings
  866. print("step 0: checking inventory...")
  867. print("... done")
  868.  
  869. print("step 1: go up...")
  870.  
  871. upSteps=0
  872. while turtle.up() do
  873.   upSteps=upSteps+1
  874.   term.setCursorPos(19, 3)
  875.   term.write("moved up "..upSteps.." blocks")
  876.   if cMaxTurns>0 and upSteps>=cMaxTurns then break end
  877. end
  878.  
  879. term.setCursorPos(19, 3)
  880. print()
  881. print("... done")
  882.  
  883.  
  884. ---- step 2: check build space ----
  885. local blnBlocked=false
  886. local blnFirstBlocked=false
  887. term.write("step 2: check build space...")
  888. if not blnRefill then
  889.   repeat
  890.     blnBlocked=false
  891.     blnFirstBlocked=false
  892.     for i=1,2 do
  893.       -- RFD?
  894.       if blnRFD then
  895.         if not blnBlocked then
  896.           gl() if not gfr() then blnBlocked=true gr() end
  897.         end
  898.         if not blnBlocked and turtle.inspectDown() then
  899.           blnBlocked=true gb() gr()
  900.         else
  901.           if not blnBlocked then gb() gr() end
  902.         end
  903.       end
  904.      
  905.       -- fd
  906.       if not blnBlocked and not gfr() then blnBlocked=true end
  907.       -- left fd
  908.       if not blnBlocked then
  909.         gl()
  910.         if not gfr() then blnBlocked=true gr() gb() end
  911.         if not blnBlocked and turtle.inspectDown() then blnBlocked=true gb() gr() gb() end
  912.       end
  913.       -- right fd
  914.       if not blnBlocked then
  915.         gb()
  916.         if not gbr() then blnBlocked=true gr() gb() end
  917.         if not blnBlocked and turtle.inspectDown() then blnBlocked=true gf() gr() gb() end
  918.       end
  919.       -- return after one half
  920.       if not blnBlocked then
  921.         gf() gl() gf()
  922.       end
  923.       -- original orientation
  924.      
  925.       if i==1 and blnBlocked then blnFirstBlocked=true end
  926.       if i==2 then
  927.         if not blnFirstBlocked and blnBlocked then gl(2) end
  928.         --if blnFirstBlocked then gl(2) end
  929.       end
  930.       debugPrint("i="..i.."|blnFirstBlocked=",blnFirstBlocked,"|blnBlocked=",blnBlocked)
  931.     end
  932.     if blnBlocked then
  933.       print("Layer blocked, try 2 deeper...")
  934.       if upSteps<4 then
  935.         gd(upSteps)
  936.         error("Couldn't find place to build the drone :(")
  937.       end
  938.       gd(2) upSteps=upSteps-2
  939.     else
  940.       print(" done")
  941.     end
  942.   until not blnBlocked
  943. else
  944.   print(" no")
  945. end
  946.  
  947. ---- step 3: build drone ----
  948. term.write("step 3: build drone...")
  949. if not blnRefill then
  950.   -- assumption: T's at build limit, at center w/ original orientation
  951.  
  952.   -- step 3.1 build trap doors and carpets
  953.   for i=1,2 do
  954.     if blnRFD then gl() gf() ss(slotDropper) pd() gb() gr() end
  955.     gf()
  956.     if blnRFD and i~=2 then pd() end
  957.     gl() gf() ss(slotTrapdoor) pd() gb(2) pd() gf() ss(slotCarpet) pf() gr(2) pf() gr() gf()
  958.   end
  959.   -- step 3.2 build core
  960.   if not blnRFD then
  961.     gd() ss(slotSensor) pu() gd() ss(slotDropper) pu()
  962.     upSteps=upSteps-2
  963.   else
  964.     ss(slotSensor) pd() gb() ss(slotModem) pf()
  965.     gd(2) ss(slotDiskDrive) pf() ss(slotDisk) Df()
  966.     prepareStartup()
  967.     gu() peripheral.call("front","turnOn")
  968.     gu() ss(slotDropper) pd() gb() gd(2) gf()
  969.     ss(slotDisk) sf() ss(slotDiskDrive) df()
  970.     gd() gf() ss(slotDropper) pu()
  971.     upSteps=upSteps-3
  972.   end
  973.  
  974.   print(" done")
  975. else
  976.   print(" no")
  977. end
  978.  
  979. ---- step 4: fill up with junk ----  
  980. term.write("step 4: fill up with junk...")
  981. if not blnRefill then
  982.   for i=slotJunkStart,slotJunkEnd do
  983.     if gic(i)>0 then ss(i) Du(64) end
  984.   end
  985.   print(" inital fill done")
  986. else
  987.   -- basic dropper/dispenser
  988.   for i=1,16 do if gic(i)>0 then ss(i) Du(64) end end
  989.   -- other droppers/dispensers
  990.   gf() gu() gr()
  991.   for iDropper=1,4 do
  992.     if inspectFor(blockDropper,"u") then
  993.       for i=1,16 do
  994.         if gic(i)>0 then
  995.           ss(i) Du(64)
  996.         end
  997.       end
  998.     else
  999.       print("No Dropper/Dispensor here to refill.")
  1000.     end
  1001.     gf() gr() gf()
  1002.   end
  1003.   gl()
  1004.   print(" done")
  1005. end
  1006.  
  1007. ---- step 5: return, finishing stuff / report ----  
  1008. term.write("step 5: return...")
  1009.   gd(upSteps)
  1010. if blnRefill then gb() gd() end
  1011.  
  1012. print(" done")
  1013. print("Prank prepared !") ss(1)                
  1014. sleep(0.4)
  1015. print("***************************************")
  1016. print("* Check out YouTube for more videos   *")
  1017. print("* and turtle programs by Kaikaku :)   *")
  1018. print("***************************************")
Add Comment
Please, Sign In to add comment