Advertisement
NanoBob

Warturtle mobs

Mar 4th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.59 KB | None | 0 0
  1. local sendChannel=666
  2. local pingLimit=5
  3.  
  4. if fs.exists("events")==false then shell.run("pastebin get L7KYC10V events") end
  5. if os.loadAPI("events")==false then error("Failed to load events API") end
  6.  
  7. if fs.exists("data")==false then shell.run("pastebin get 5FpayqrQ data") end
  8. if os.loadAPI("data")==false then error("Failed to load data API") end
  9.  
  10. if fs.exists("utils")==false then shell.run("pastebin get c4CBkgKV utils") end
  11. if os.loadAPI("utils")==false then error("Failed to load utils API") end
  12.  
  13. local self={
  14.     facing="north",
  15.     pos={x=0,y=0,z=0},
  16. }
  17.  
  18. local whitelist={
  19.     ["NanoBob_"]=true,
  20.     ["DDerek"]=true,
  21.     ["bas260"]=true,
  22. }
  23.  
  24. local sensor
  25. local mainTimer
  26.  
  27. local facing={
  28.     [0]="west","north","east","south","west","north",
  29.     north=1,east=2,south=3,west=4,
  30. }
  31.  
  32. function self.turn(direction,ammount)
  33.     for i=1,ammount do
  34.         if direction=="right" then
  35.             turtle.turnRight()
  36.             self.facing=facing[ facing[self.facing]+1 ]
  37.         else
  38.             turtle.turnLeft()
  39.             self.facing=facing[ facing[self.facing]-1 ]
  40.         end
  41.     end
  42.     data.set("facing",self.facing,"db/position")
  43. end
  44.  
  45. function self.setFacing(targetFacing)
  46.     local targetFacing=string.lower(targetFacing)
  47.     local currentFacing=string.lower(self.facing)
  48.     if targetFacing==currentFacing then
  49.         return
  50.     else
  51.         local currentFacingIndex=facing[currentFacing]
  52.         local facingIndex=facing[targetFacing]
  53.         local turns=facingIndex-currentFacingIndex
  54.         if turns<0 then
  55.             turns=turns+4
  56.         end
  57.         if turns==3 then
  58.             self.turn("left",1)
  59.         else
  60.             for i=1,turns do
  61.                 self.turn("right",1)
  62.             end
  63.         end
  64.     end
  65. end
  66.  
  67. function initPos()
  68.     local facing=data.get("facing","db/position")
  69.     if facing==nil then
  70.         print("Please input facing:")
  71.         io.write("facing: ")
  72.         facing=read()
  73.         data.set("facing",facing,"db/position")
  74.     end
  75.     self.facing=facing
  76. end
  77.  
  78. function initSensor()
  79.     sensor=peripheral.wrap("left")
  80. end
  81.  
  82. function init()
  83.     initPos()
  84.     initSensor()
  85. end
  86. init()
  87.  
  88. function sensorHandle()
  89.    
  90. end
  91.  
  92. local messageHandles={
  93.    
  94. }
  95.  
  96. function handleMessages(side,channel,replyChannel,message)
  97.     if replyChannel==nil then return end
  98.     local messageType=utils.split(message,"|",1)
  99.     if messageHandles[messageType]==nil then return end
  100.     local returnMessage=messageHandles[messageType](replyChannel,message)
  101.     if returnMessage==nil then return end
  102.     wireless.transmit(replyChannel,localChannel,returnMessage)
  103. end
  104. events.addHandler("modem_message",handleMessages)
  105.  
  106. function searchForPlayers(id)
  107.     if id==mainTimer then
  108.         local players=sensor.getMobIds()
  109.         for _,player in ipairs(players) do
  110.             --if whitelist[player.name]==nil then
  111.                 local data=sensor.getMobData(player)
  112.                 if data==nil then break end
  113.                 local data=data.all()
  114.                 local pos=data.position
  115.                 pos.y=pos.y+1
  116.                 if pos.x>0.5 then
  117.                     self.setFacing("east")
  118.                     turtle.attack()
  119.                     if pos.x>1 then
  120.                         turtle.forward()
  121.                     end
  122.                 elseif pos.x<-0.5 then
  123.                     self.setFacing("west")
  124.                     turtle.attack()
  125.                     if pos.x<-1 then
  126.                         turtle.forward()
  127.                     end
  128.                 end
  129.                 if pos.y>0.5 then
  130.                     turtle.attackUp()
  131.                     if pos.y>-0.6 then
  132.                         turtle.up()
  133.                     end
  134.                 elseif pos.y<0 then
  135.                     turtle.attackDown()
  136.                     if pos.y<=-1.5 then
  137.                         turtle.down()
  138.                     end
  139.                 end
  140.                 if pos.z>0.5 then
  141.                     self.setFacing("south")
  142.                     turtle.attack()
  143.                     if pos.z>1 then
  144.                         turtle.forward()
  145.                     end
  146.                 elseif pos.z<-0.5 then
  147.                     self.setFacing("north")
  148.                     turtle.attack()
  149.                     if pos.z<-1 then
  150.                         turtle.forward()
  151.                     end
  152.                 end
  153.                 break
  154.             --end
  155.         end
  156.     end
  157. end
  158. events.addHandler("timer",timerExpired)
  159.  
  160. while true do
  161.     searchForPlayers()
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement