robot-input.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. local function intToPort(gate, port, len, v)
  2. for i = 1, len do
  3. local p = math.pow(2, i-1)
  4. Gate.setportstate(gate, port+i-1, math.floor(v/p)%2)
  5. end
  6. end
  7. return function(gate, argv)
  8. local arg = argv[1]:gsub("^ +", ""):gsub(" +$", "")
  9. for word in arg:gmatch("[^\t]+") do
  10. local first, rest = word:sub(1, 1), word:sub(2, #word)
  11. local vec = {}
  12. for a in rest:gmatch("[^ ]+") do table.insert(vec, tonumber(a) or error("invalid number "..a)) end
  13. if first=="P" then -- set position
  14. assert(#vec==4, "invalid position given to robot: "..word)
  15. gate.robotpos = {vec[1], vec[2], vec[3]}
  16. gate.robotdir = vec[4]
  17. elseif first=="B" then -- detected brick info
  18. assert(#vec==7, "invalid brick info given to robot: "..word)
  19. gate.brickexists = vec[1]
  20. gate.brickcolor = vec[2]
  21. gate.brickcolorfx = vec[3]
  22. gate.brickshapefx = vec[4]
  23. gate.brickray = vec[5]
  24. gate.brickcol = vec[6]
  25. gate.brickren = vec[7]
  26. intToPort(gate, 15, 6, gate.brickcolor)
  27. Gate.setportstate(gate, 21, gate.brickexists)
  28. else error("invalid control word given to robot: \""..word.."\".. (first \""..first.."\")") end
  29. end
  30. gate.waiting = false
  31. end