Advertisement
NoelGu

Asline

Jun 26th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.87 KB | None | 0 0
  1. local event = require("event")
  2. local component=require("component")
  3. local os =require("os")
  4. local io =require("io")
  5.  
  6. local sides=require("sides")
  7. local robot=component.robot
  8. local navi=component.navigation
  9. local inv=component.inventory_controller
  10.  
  11. local zm=sides.north
  12. local zp=sides.south
  13. local xm=sides.west
  14. local xp=sides.east
  15. local forward=sides.forward
  16. local ym=0
  17. local yp=1
  18.  
  19. local up=1
  20. local down=0
  21.  
  22. local char_space = string.byte(" ")
  23. local char_q = string.byte("q")
  24. local running = true
  25. local base={-9.5,120.5,16.5}
  26. local places=
  27.     {
  28.         {{1,2,0},up},
  29.         {{1,2,1},up},
  30.         {{1,2,2},up},
  31.         {{1,2,3},up},
  32.         {{1,2,4},up},
  33.         {{1,2,5},up},
  34.         {{1,2,6},up},
  35.         {{1,2,7},up},
  36.         {{1,2,8},up},
  37.         {{1,2,9},up},
  38.         {{1,2,10},up},
  39.         {{1,2,11},up},
  40.         {{1,2,12},up},
  41.         {{1,2,13},up},
  42.         {{1,2,14},up},
  43.         [25]={{1,0,0},down},
  44. }
  45.  
  46. local recipes={}
  47.  
  48. function moveToPlace(placenumber)
  49.     local x,y,z
  50.     local n=tonumber(placenumber)
  51.     local pos=places[n][1]
  52.     if (pos) then
  53.         x,y,z=table.unpack(pos)
  54.         moveToPos(x,y,z)
  55.     else
  56.         print("wrong place number check your setting")
  57.     end
  58. end
  59.  
  60. function split(str,delimiter)
  61.     local dLen = string.len(delimiter)
  62.     local newDeli = ''
  63.     for i=1,dLen,1 do
  64.         newDeli = newDeli .. "["..string.sub(delimiter,i,i).."]"
  65.     end
  66.  
  67.     local locaStart,locaEnd = string.find(str,newDeli)
  68.     local arr = {}
  69.     local n = 1
  70.     while locaStart ~= nil
  71.     do
  72.         if locaStart>0 then
  73.             arr[n] = string.sub(str,1,locaStart-1)
  74.             n = n + 1
  75.         end
  76.  
  77.         str = string.sub(str,locaEnd+1,string.len(str))
  78.         locaStart,locaEnd = string.find(str,newDeli)
  79.     end
  80.     if str ~= nil then
  81.         arr[n] = str
  82.     end
  83.     return arr
  84. end
  85. function goBack()
  86.     moveToPos(0,0,0)
  87. end
  88. function getPos()
  89.     local x,y,z=navi.getPosition()
  90.     local dx=x-base[1]
  91.     local dy=y-base[2]
  92.     local dz=z-base[3]
  93.     return dx,dy,dz
  94. end
  95.  
  96. function moveToPos(x,y,z)
  97.     local ox,oy,oz=getPos()
  98.     local dx=x-ox
  99.     local dy=y-oy
  100.     local dz=z-oz
  101.  
  102.     if dx>0 then
  103.         while navi.getFacing()~=xp do
  104.             robot.turn(true)
  105.         end
  106.     elseif dx<0 then
  107.         while navi.getFacing()~=xm do
  108.             robot.turn(true)
  109.         end
  110.     end
  111.  
  112.     for i=1,math.abs(dx),1 do
  113.         robot.move(forward)
  114.     end
  115.  
  116.     if dz>0 then
  117.         while navi.getFacing()~=zp do
  118.             robot.turn(true)
  119.         end
  120.     elseif dz<0 then
  121.         while navi.getFacing()~=zm do
  122.             robot.turn(true)
  123.         end
  124.     end
  125.  
  126.     for i=1,math.abs(dz),1 do
  127.         robot.move(forward)
  128.     end
  129.  
  130.     if dy>0 then
  131.         for i=1,math.abs(dy),1 do
  132.             robot.move(yp)
  133.         end
  134.     elseif dy<0 then
  135.         for i=1,math.abs(dy),1 do
  136.             robot.move(ym)
  137.         end
  138.     end
  139. end
  140.  
  141. function isItem(side,slot,name,label)
  142.     local iname,ilabel,isize=table.unpack(GetItem(side,slot))
  143.     if name==iname and label==ilabel then
  144.         return isize
  145.     end
  146.     return 0
  147. end
  148.  
  149. function GetItem(side,slot)
  150.     local info
  151.     info=inv.getStacksInSlot(side,slot)
  152.     if info then
  153.         return {info["name"],info["label"],info["size"]}
  154.     end
  155.     return nil
  156. end
  157.  
  158. function isEnough(name,label,allinfo,offset)
  159.     if offset==nil then offset=0 end
  160.     local numberneed=size
  161.     local toget={}
  162.     for i,e in pairs(allinfo) do
  163.         if e["name"]==name and e["label"]==label then
  164.             local sizehere=e["size"]
  165.             if sizehere>=numberneed then
  166.                 table.insert(toget,{i+offset,numberneed})
  167.                 return toget
  168.             else
  169.                 table.insert(toget,{i+offset,sizehere})
  170.                 numberneed=numberneed-sizehere
  171.             end
  172.         end        
  173.     end
  174.     if numberneed>0 then
  175.         return nil
  176.     else
  177.         return toget
  178.     end
  179. end
  180.  
  181. function learnRecipe(name)
  182.     goBack()
  183.     local recipe={}    
  184.     local maxslot=inv.getInventorySize(0)
  185.     for i=1,maxslot,1 do
  186.         local c=GetItem(0,i)
  187.         if c then
  188.             recipe["name"]={i,c}
  189.         end
  190.     end
  191. end
  192.  
  193.  
  194.  
  195. function unknownEvent()
  196.   -- do nothing if the event wasn't relevant
  197. end
  198.  
  199. function unknownCommand()
  200.     print("Unknown Command")
  201. end
  202. local myEventHandlers = setmetatable({}, { __index = function() return unknownEvent end })
  203.  
  204. local commandHandlers = setmetatable({}, { __index = function() return unknownCommand end })
  205.  
  206. function myEventHandlers.key_up(adress, char, code, playerName)
  207.     if (char == char_q) then
  208.         running = false
  209.     end
  210.     if (char == char_space) then
  211.         print("what's your command?")
  212.         cmd=split(io.read(),' ')
  213.         handleCommand(table.unpack(cmd))
  214.     end
  215. end
  216.  
  217. function commandHandlers.moveto(x,y,z)
  218.     if x==nil then
  219.         print("moveto [x] [y] [z]")
  220.     else
  221.         moveToPos(x,y,z)
  222.     end
  223. end
  224.  
  225. function commandHandlers.movetoplace(n)
  226.     if n=nil then
  227.         print("movetoplace [slotnum]")
  228.     else
  229.         moveToPlace(n)
  230.     end
  231. end
  232.  
  233. function commandHandlers.regmachine(slot,x,y,z,face)
  234.     if slot=nil then
  235.         print("regmachine [slotnum] [x] [y] [z] [face](up=1 down=0)")
  236.     else
  237.         local nslot=tonumber(slot)
  238.         local nx=tonumber(x)
  239.         local ny=tonumber(y)
  240.         local nz=tonumber(z)      
  241.         local nf=tonumber(face)
  242.         place[nslot][1]={nx,ny,nz}
  243.         place[nslot][2]=nf
  244.     end
  245. end
  246.  
  247. function commandHandlers.learn()
  248.     learnRecipe()
  249. end
  250.  
  251.  
  252. function handleCommand(name, ...)
  253.     print("name="..name)
  254.     if (name) then
  255.         commandHandlers[name](...)
  256.     end
  257. end
  258.  
  259. function handleEvent(eventID, ...)
  260.     if (eventID) then
  261.         myEventHandlers[eventID](...)
  262.     end
  263. end
  264.  
  265. function idle()
  266.    
  267. end
  268.  
  269.  
  270. while running do
  271.     handleEvent(event.pull())
  272. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement