Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Requires
- local comp = require("component")
- local robot = require("robot")
- local nav = comp.navigation
- local sides = require("sides")
- --Vars
- Roles = {"Citizen","Miner","Shopkeeper"}
- Role = Roles[2]
- Citizen = {}
- Citizen.Wander = false
- Miner = {}
- Miner.Ready = false
- Shopkeeper = {}
- Shopkeeper.Trade = false
- --Functions
- function Move()
- while robot.detect() do
- if Role == "Miner" and Miner.Ready == true then
- robot.swing()
- os.sleep(0.5)
- else
- break
- -- robot.turnRight()
- end
- os.sleep(0)
- end
- while robot.detectUp() do
- if Role == "Miner" and Miner.Ready == true then
- robot.swingUp()
- os.sleep(0.5)
- else
- break
- end
- os.sleep(0)
- end
- -- South = +Z
- -- West = -X
- -- North = -Z
- -- East = +X
- if robot.forward() then
- if nav.getFacing() == sides.south then
- BotZ = BotZ + 1
- elseif nav.getFacing() == sides.west then
- BotX = BotX - 1
- elseif nav.getFacing() == sides.north then
- BotZ = BotZ - 1
- elseif nav.getFacing() == sides.east then
- BotX = BotX + 1
- end
- print("Can move")
- return true
- else
- print("Can not move")
- return false
- end
- end
- function SetDir(dd)
- io.write("HEE")
- -- turning to direction
- if dd == "South" then
- dd = sides.south
- elseif dd == "West" then
- dd = sides.west
- elseif dd == "North" then
- dd = sides.north
- elseif dd == "East" then
- dd = sides.east
- end
- while nav.getFacing() ~= dd do
- robot.turnRight()
- os.sleep(0)
- end
- io.write("HOO!!")
- end
- function PathFind(dx,dy,dz,dd)
- while BotX ~= dx and BotY ~=dy and BotZ ~= dz do
- print(">>",0)
- os.sleep(0)
- i = 1
- Distances = {
- {math.sqrt(math.pow((dx-(BotX+1)),2) + math.pow((dy- BotY ),2) + math.pow((dz- BotZ ),2)),"+x"},
- {math.sqrt(math.pow((dx-(BotX-1)),2) + math.pow((dy- BotY ),2) + math.pow((dz- BotZ ),2)),"-x"},
- {math.sqrt(math.pow((dx- BotX ),2) + math.pow((dy-(BotY+1)),2) + math.pow((dz- BotZ ),2)),"+y"},
- {math.sqrt(math.pow((dx- BotX ),2) + math.pow((dy-(BotY-1)),2) + math.pow((dz- BotZ ),2)),"-y"},
- {math.sqrt(math.pow((dx- BotX ),2) + math.pow((dy- BotY ),2) + math.pow( dz-(BotZ+1),2)),"+z"},
- {math.sqrt(math.pow((dx- BotX ),2) + math.pow((dy- BotY ),2) + math.pow( dz-(BotZ-1),2)),"-z"}
- }
- table.sort(Distances, function(a,b) return a[1]<b[1] end)
- -- South = +Z
- -- West = -X
- -- North = -Z
- -- East = +X
- print("Cords: ["..tostring(BotX)..", "..tostring(BotY)..", "..tostring(BotZ).."]")
- print("The best direction is: "..tostring(Distances[i][2]))
- print(">>",1)
- while true do
- print(">>",2)
- os.sleep(0)
- print("Distance: "..tostring(Distances[i][1]).." | ",i,Distances[i][2])
- if (BotX >= CityMinX and BotX <= CityMaxX) or (BotZ >= CityMinZ and BotZ <= CityMaxZ) then
- Miner.Ready = false
- else
- Miner.Ready = true
- end
- print(">>",3)
- if Distances[i][2] == "+x" then
- print(">>",4)
- SetDir("East")
- if not Move() then
- print(">>","did not move")
- i = i + 1
- else
- print(">>","moved")
- break
- end
- elseif Distances[i][2] == "-x" then
- print(">>",5)
- SetDir("West")
- if not Move() then
- print(">>","did not move")
- i = i + 1
- else
- print(">>","moved")
- break
- end
- elseif Distances[i][2] == "+y" then
- print(">>",6)
- while robot.detectUp() do
- robot.swingUp()
- os.sleep(0.5)
- end
- print(">>",7)
- if robot.up() then
- print(">>","moved")
- BotY = BotY + 1
- break
- else
- print(">>","did not move")
- i = i + 1
- end
- elseif Distances[i][2] == "-y" then
- print(">>",8)
- while robot.detectDown() do
- robot.swingDown()
- os.sleep(0)
- end
- print(">>",9)
- if robot.down() then
- print(">>","moved")
- BotY = BotY - 1
- break
- else
- print(">>","did not move")
- i = i + 1
- end
- elseif Distances[i][2] == "+z" then
- print(">>",10)
- SetDir("South")
- if not Move() then
- print(">>","did not move")
- i = i + 1
- else
- print(">>","moved")
- break
- end
- elseif Distances[i][2] == "-z" then
- print(">>",11)
- SetDir("North")
- if not Move() then
- print(">>","did not move")
- i = i + 1
- else
- print(">>","moved")
- break
- end
- end
- print(i)
- end
- print(12)
- end
- if BotX == dx and BotY == dy and BotZ == dz then
- SetDir(dd)
- return true
- end
- end
- --Main Code
- CityMinX = 214
- CityMaxX = 296
- CityMinZ = 215
- CityMaxZ = 297
- if Role == "Citizen" then
- BotX = 0
- BotY = 0
- BotZ = 0
- ChargingStationX = 0
- ChargingStationY = 0
- ChargingStationZ = 0
- ChargingStationD = 0
- PathFind(0,0,0,"North")
- elseif Role == "Miner" then
- BotX = 220
- BotY = 101
- BotZ = 260
- MineX = 210
- MineY = 102
- MineZ = 261
- MineD = "West"
- BedX = 0
- BedY = 0
- BedZ = 0
- BedD = 0
- Miner.TravelToMine = true
- Miner.BookmarkMine = {0,0,0,0}
- while true do
- if Miner.TravelToMine then --Pathfinds to Mine
- if PathFind(MineX,MineY,MineZ,MineD) then
- Miner.TravelToMine = false
- break
- end
- end
- -- StripMine
- end
- elseif Role == "Shopkeeper" then
- BotX = 0
- BotY = 0
- BotZ = 0
- ChargingStationX = 0
- ChargingStationY = 0
- ChargingStationZ = 0
- ChargingStationD = 0
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement