Advertisement
electronic_steve

robot particler

Feb 3rd, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. --robot
  2. component    = require('component')
  3. event        = require('event')
  4. robot        = require('robot')
  5. computer     = require('computer')
  6. shell        = require("shell")
  7. filesystem   = require("filesystem")
  8. modem        = component.modem
  9. particle     = component.particle
  10. nav          = component.navigation
  11. particle_map = {}
  12. sides        = {[2]={0,-1},[3]={0,1},[4]={-1,0},[5]={1,0}}
  13. arg          = {...}
  14. pos          = {x=arg[1] or 0,y=arg[2] or 0,z=arg[2] or 0}
  15. send         = component.tunnel.send
  16. filesystem.setAutorunEnabled(true)
  17. modem.setWakeMessage("ROBOTSTART")
  18.  
  19. function getN()
  20.     return #particle_map
  21. end
  22. function up(self)
  23.     for i=1,3 do
  24.         local out=robot.up()
  25.         if out  then pos.y=pos.y+1 return true end
  26.         os.sleep(0.5)
  27.     end
  28.     return false
  29. end
  30. function down(self)
  31.     for i=1,3 do
  32.         local out=robot.down()
  33.         if out  then pos.y=pos.y-1 return true end
  34.         os.sleep(0.5)
  35.     end
  36.     return false
  37. end
  38. function left(self)
  39.     return robot.turnLeft()
  40. end
  41. function right(self)
  42.     return robot.turnRight()
  43. end
  44. function go(self)
  45.     for i=1,3 do
  46.         local out=robot.forward()
  47.         if out then
  48.             pos.x=pos.x+sides[nav.getFacing()][1]
  49.             pos.z=pos.z+sides[nav.getFacing()][2]
  50.             return true
  51.         end
  52.         os.sleep(0.5)
  53.     end
  54.     return false
  55. end
  56.  
  57.  
  58. function back(self)
  59.     for i=1,3 do
  60.         local out=robot.back()
  61.         if out  then
  62.             pos.x=pos.x-sides[nav.getFacing()][1]
  63.             pos.z=pos.z-sides[nav.getFacing()][2]
  64.             return true
  65.         end
  66.         os.sleep(0.5)
  67.     end
  68.     return false
  69. end
  70. function add(x,y,z,name,random_speed,random_pos_radius)
  71.     table.insert(particle_map,{name=name,x=x,y=y,z=z,rand=random_speed,rand_rad=random_pos_radius*2})
  72.     return #particle_map
  73. end
  74. function del(self)
  75.     particle_map[#particle_map]=nil
  76.     return #particle_map
  77. end
  78. function editpos(x,y,z)
  79.     pos.x=x
  80.     pos.y=y
  81.     pos.z=z
  82.     return true
  83. end
  84. function chat(_,_,_,_,_,msg)
  85.     print("s:"..msg)
  86.     local text=tostring(load("return "..msg)())
  87.     print("c:"..text)
  88.     send(text)
  89. end
  90. event.listen("modem_message",chat)
  91.  
  92. function particle_update()
  93.     for i=1,#particle_map do
  94.         local self     = particle_map[i]
  95.         local vx,vy,vz = 0,0,0
  96.         local name     = self.name
  97.         local x        = self.x-pos.x+self.rand_rad*(math.random()-0.5)
  98.         local y        = self.y-pos.y+math.max(math.min(self.rand_rad,256),0)*math.random()
  99.         local z        = self.z-pos.z+self.rand_rad*(math.random()-0.5)
  100.         if self.rand then vx,vy,vz=math.random()-0.5,math.random()-0.5,math.random()-0.5 end
  101.         particle.spawn(name,x,y,z,vx,vy,vz)
  102.     end
  103. end
  104.  
  105. while true do
  106.     os.sleep(0) -- too long without yielding не дремлет!
  107.     pcall(particle_update)
  108. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement