Advertisement
Guest User

CityRole

a guest
Dec 1st, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.01 KB | None | 0 0
  1. --Requires
  2. local comp = require("component")
  3. local robot = require("robot")
  4. local nav = comp.navigation
  5. local sides = require("sides")
  6. --Vars
  7. Roles = {"Citizen","Miner","Shopkeeper"}
  8. Role = Roles[2]
  9. Citizen = {}
  10. Citizen.Wander = false
  11. Miner = {}
  12. Miner.Ready = false
  13. Shopkeeper = {}
  14. Shopkeeper.Trade = false
  15. --Functions
  16. function Move()
  17.  while robot.detect() do
  18.   if Role == "Miner" and Miner.Ready == true then
  19.    robot.swing()
  20.    os.sleep(0.5)
  21.   else
  22.    break
  23. --   robot.turnRight()
  24.   end
  25.   os.sleep(0)
  26.  end
  27.  while robot.detectUp() do
  28.   if Role == "Miner" and Miner.Ready == true then
  29.    robot.swingUp()
  30.    os.sleep(0.5)
  31.   else
  32.    break
  33.   end
  34.   os.sleep(0)
  35.  end
  36.  -- South = +Z
  37.  -- West  = -X
  38.  -- North = -Z
  39.  -- East  = +X
  40.  if robot.forward() then
  41.   if     nav.getFacing() == sides.south then
  42.    BotZ = BotZ + 1
  43.   elseif nav.getFacing() == sides.west then
  44.    BotX = BotX - 1
  45.   elseif nav.getFacing() == sides.north then
  46.    BotZ = BotZ - 1
  47.   elseif nav.getFacing() == sides.east then
  48.    BotX = BotX + 1
  49.   end
  50.   print("Can move")
  51.   return true
  52.  else
  53.   print("Can not move")
  54.   return false
  55.  end
  56. end
  57.  
  58. function SetDir(dd)
  59.  io.write("HEE")
  60.  -- turning to direction
  61.  if     dd == "South" then
  62.   dd = sides.south
  63.  elseif dd == "West"  then
  64.   dd = sides.west
  65.  elseif dd == "North" then
  66.   dd = sides.north
  67.  elseif dd == "East"  then
  68.   dd = sides.east
  69.  end
  70.  while nav.getFacing() ~= dd do
  71.   robot.turnRight()
  72.   os.sleep(0)
  73.  end
  74.  io.write("HOO!!")
  75. end
  76.  
  77. function PathFind(dx,dy,dz,dd)
  78.  while BotX ~= dx and BotY ~=dy and BotZ ~= dz do
  79.   print(">>",0)
  80.   os.sleep(0)
  81.   i = 1
  82.   Distances = {
  83.   {math.sqrt(math.pow((dx-(BotX+1)),2) + math.pow((dy- BotY   ),2) + math.pow((dz- BotZ  ),2)),"+x"},
  84.   {math.sqrt(math.pow((dx-(BotX-1)),2) + math.pow((dy- BotY   ),2) + math.pow((dz- BotZ  ),2)),"-x"},
  85.   {math.sqrt(math.pow((dx- BotX   ),2) + math.pow((dy-(BotY+1)),2) + math.pow((dz- BotZ  ),2)),"+y"},
  86.   {math.sqrt(math.pow((dx- BotX   ),2) + math.pow((dy-(BotY-1)),2) + math.pow((dz- BotZ  ),2)),"-y"},
  87.   {math.sqrt(math.pow((dx- BotX   ),2) + math.pow((dy- BotY   ),2) + math.pow( dz-(BotZ+1),2)),"+z"},
  88.   {math.sqrt(math.pow((dx- BotX   ),2) + math.pow((dy- BotY   ),2) + math.pow( dz-(BotZ-1),2)),"-z"}
  89.   }
  90.   table.sort(Distances, function(a,b) return a[1]<b[1] end)
  91.   -- South = +Z
  92.   -- West  = -X
  93.   -- North = -Z
  94.   -- East  = +X
  95.   print("Cords: ["..tostring(BotX)..", "..tostring(BotY)..", "..tostring(BotZ).."]")
  96.   print("The best direction is: "..tostring(Distances[i][2]))
  97.   print(">>",1)
  98.   while true do
  99.    print(">>",2)
  100.    os.sleep(0)
  101.    print("Distance: "..tostring(Distances[i][1]).." | ",i,Distances[i][2])
  102.    if (BotX >= CityMinX and BotX <= CityMaxX) or (BotZ >= CityMinZ and BotZ <= CityMaxZ) then
  103.     Miner.Ready = false
  104.    else
  105.     Miner.Ready = true
  106.    end
  107.    print(">>",3)
  108.    if     Distances[i][2] == "+x" then
  109.     print(">>",4)
  110.     SetDir("East")
  111.     if not Move() then
  112.      print(">>","did not move")
  113.      i = i + 1
  114.     else
  115.      print(">>","moved")
  116.      break
  117.     end
  118.    elseif Distances[i][2] == "-x" then
  119.     print(">>",5)
  120.     SetDir("West")
  121.     if not Move() then
  122.      print(">>","did not move")
  123.      i = i + 1
  124.     else
  125.      print(">>","moved")
  126.      break
  127.     end
  128.    elseif Distances[i][2] == "+y" then
  129.     print(">>",6)
  130.     while robot.detectUp() do
  131.      robot.swingUp()
  132.      os.sleep(0.5)
  133.     end
  134.     print(">>",7)
  135.     if robot.up() then
  136.      print(">>","moved")
  137.      BotY = BotY + 1
  138.      break
  139.     else
  140.      print(">>","did not move")
  141.      i = i + 1
  142.     end
  143.    elseif Distances[i][2] == "-y" then
  144.     print(">>",8)
  145.     while robot.detectDown() do
  146.      robot.swingDown()
  147.      os.sleep(0)
  148.     end
  149.     print(">>",9)
  150.     if robot.down() then
  151.      print(">>","moved")
  152.      BotY = BotY - 1
  153.      break
  154.     else
  155.      print(">>","did not move")
  156.      i = i + 1
  157.     end
  158.    elseif Distances[i][2] == "+z" then
  159.     print(">>",10)
  160.     SetDir("South")
  161.     if not Move() then
  162.      print(">>","did not move")
  163.      i = i + 1
  164.     else
  165.      print(">>","moved")
  166.      break
  167.     end
  168.    elseif Distances[i][2] == "-z" then
  169.     print(">>",11)
  170.     SetDir("North")
  171.     if not Move() then
  172.      print(">>","did not move")
  173.      i = i + 1
  174.     else
  175.      print(">>","moved")
  176.      break
  177.     end
  178.    end
  179.    print(i)
  180.   end
  181.   print(12)
  182.  end
  183.  if BotX == dx and BotY == dy and BotZ == dz then
  184.   SetDir(dd)
  185.   return true
  186.  end
  187. end
  188. --Main Code
  189. CityMinX = 214
  190. CityMaxX = 296
  191. CityMinZ = 215
  192. CityMaxZ = 297
  193. if     Role == "Citizen"    then
  194.  BotX = 0
  195.  BotY = 0
  196.  BotZ = 0
  197.  ChargingStationX = 0
  198.  ChargingStationY = 0
  199.  ChargingStationZ = 0
  200.  ChargingStationD = 0
  201.  PathFind(0,0,0,"North")
  202. elseif Role == "Miner"      then
  203.  BotX = 220
  204.  BotY = 101
  205.  BotZ = 260
  206.  MineX = 210
  207.  MineY = 102
  208.  MineZ = 261
  209.  MineD = "West"
  210.  BedX = 0
  211.  BedY = 0
  212.  BedZ = 0
  213.  BedD = 0
  214.  Miner.TravelToMine = true
  215.  Miner.BookmarkMine = {0,0,0,0}
  216.  while true do
  217.   if Miner.TravelToMine then --Pathfinds to Mine
  218.    if PathFind(MineX,MineY,MineZ,MineD) then
  219.     Miner.TravelToMine = false
  220.     break
  221.    end
  222.   end
  223.   -- StripMine
  224.  
  225.  end
  226. elseif Role == "Shopkeeper" then
  227.  BotX = 0
  228.  BotY = 0
  229.  BotZ = 0
  230.  ChargingStationX = 0
  231.  ChargingStationY = 0
  232.  ChargingStationZ = 0
  233.  ChargingStationD = 0
  234. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement