Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local event = require("event")
- local component=require("component")
- local os =require("os")
- local io =require("io")
- local sides=require("sides")
- local robot=component.robot
- local navi=component.navigation
- local inv=component.inventory_controller
- local zm=sides.north
- local zp=sides.south
- local xm=sides.west
- local xp=sides.east
- local forward=sides.forward
- local ym=0
- local yp=1
- local up=1
- local down=0
- local char_space = string.byte(" ")
- local char_q = string.byte("q")
- local running = true
- local base={-9.5,120.5,16.5}
- local places=
- {
- {{1,2,0},up},
- {{1,2,1},up},
- {{1,2,2},up},
- {{1,2,3},up},
- {{1,2,4},up},
- {{1,2,5},up},
- {{1,2,6},up},
- {{1,2,7},up},
- {{1,2,8},up},
- {{1,2,9},up},
- {{1,2,10},up},
- {{1,2,11},up},
- {{1,2,12},up},
- {{1,2,13},up},
- {{1,2,14},up},
- [25]={{1,0,0},down},
- }
- local recipes={}
- function moveToPlace(placenumber)
- local x,y,z
- local n=tonumber(placenumber)
- local pos=places[n][1]
- if (pos) then
- x,y,z=table.unpack(pos)
- moveToPos(x,y,z)
- else
- print("wrong place number check your setting")
- end
- end
- function split(str,delimiter)
- local dLen = string.len(delimiter)
- local newDeli = ''
- for i=1,dLen,1 do
- newDeli = newDeli .. "["..string.sub(delimiter,i,i).."]"
- end
- local locaStart,locaEnd = string.find(str,newDeli)
- local arr = {}
- local n = 1
- while locaStart ~= nil
- do
- if locaStart>0 then
- arr[n] = string.sub(str,1,locaStart-1)
- n = n + 1
- end
- str = string.sub(str,locaEnd+1,string.len(str))
- locaStart,locaEnd = string.find(str,newDeli)
- end
- if str ~= nil then
- arr[n] = str
- end
- return arr
- end
- function goBack()
- moveToPos(0,0,0)
- end
- function getPos()
- local x,y,z=navi.getPosition()
- local dx=x-base[1]
- local dy=y-base[2]
- local dz=z-base[3]
- return dx,dy,dz
- end
- function moveToPos(x,y,z)
- local ox,oy,oz=getPos()
- local dx=x-ox
- local dy=y-oy
- local dz=z-oz
- if dx>0 then
- while navi.getFacing()~=xp do
- robot.turn(true)
- end
- elseif dx<0 then
- while navi.getFacing()~=xm do
- robot.turn(true)
- end
- end
- for i=1,math.abs(dx),1 do
- robot.move(forward)
- end
- if dz>0 then
- while navi.getFacing()~=zp do
- robot.turn(true)
- end
- elseif dz<0 then
- while navi.getFacing()~=zm do
- robot.turn(true)
- end
- end
- for i=1,math.abs(dz),1 do
- robot.move(forward)
- end
- if dy>0 then
- for i=1,math.abs(dy),1 do
- robot.move(yp)
- end
- elseif dy<0 then
- for i=1,math.abs(dy),1 do
- robot.move(ym)
- end
- end
- end
- function isItem(side,slot,name,label)
- local iname,ilabel,isize=table.unpack(GetItem(side,slot))
- if name==iname and label==ilabel then
- return isize
- end
- return 0
- end
- function GetItem(side,slot)
- local info
- info=inv.getStacksInSlot(side,slot)
- if info then
- return {info["name"],info["label"],info["size"]}
- end
- return nil
- end
- function isEnough(name,label,allinfo,offset)
- if offset==nil then offset=0 end
- local numberneed=size
- local toget={}
- for i,e in pairs(allinfo) do
- if e["name"]==name and e["label"]==label then
- local sizehere=e["size"]
- if sizehere>=numberneed then
- table.insert(toget,{i+offset,numberneed})
- return toget
- else
- table.insert(toget,{i+offset,sizehere})
- numberneed=numberneed-sizehere
- end
- end
- end
- if numberneed>0 then
- return nil
- else
- return toget
- end
- end
- function learnRecipe(name)
- goBack()
- local recipe={}
- local maxslot=inv.getInventorySize(0)
- for i=1,maxslot,1 do
- local c=GetItem(0,i)
- if c then
- recipe["name"]={i,c}
- end
- end
- end
- function unknownEvent()
- -- do nothing if the event wasn't relevant
- end
- function unknownCommand()
- print("Unknown Command")
- end
- local myEventHandlers = setmetatable({}, { __index = function() return unknownEvent end })
- local commandHandlers = setmetatable({}, { __index = function() return unknownCommand end })
- function myEventHandlers.key_up(adress, char, code, playerName)
- if (char == char_q) then
- running = false
- end
- if (char == char_space) then
- print("what's your command?")
- cmd=split(io.read(),' ')
- handleCommand(table.unpack(cmd))
- end
- end
- function commandHandlers.moveto(x,y,z)
- if x==nil then
- print("moveto [x] [y] [z]")
- else
- moveToPos(x,y,z)
- end
- end
- function commandHandlers.movetoplace(n)
- if n=nil then
- print("movetoplace [slotnum]")
- else
- moveToPlace(n)
- end
- end
- function commandHandlers.regmachine(slot,x,y,z,face)
- if slot=nil then
- print("regmachine [slotnum] [x] [y] [z] [face](up=1 down=0)")
- else
- local nslot=tonumber(slot)
- local nx=tonumber(x)
- local ny=tonumber(y)
- local nz=tonumber(z)
- local nf=tonumber(face)
- place[nslot][1]={nx,ny,nz}
- place[nslot][2]=nf
- end
- end
- function commandHandlers.learn()
- learnRecipe()
- end
- function handleCommand(name, ...)
- print("name="..name)
- if (name) then
- commandHandlers[name](...)
- end
- end
- function handleEvent(eventID, ...)
- if (eventID) then
- myEventHandlers[eventID](...)
- end
- end
- function idle()
- end
- while running do
- handleEvent(event.pull())
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement