Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- local turtle = require("turtle")
- -- local math = require("math")
- -- ### World ###
- -- structure of a block:
- -- {
- -- name = "minecraft:oak_log",
- -- state = { axis = "x" },
- -- tags = { ["minecraft:logs"] = true, ... },
- -- }
- local block_map = {}
- function ClearMap()
- block_map = {}
- end
- function GetBlock(pos)
- return block_map[tostring(pos)]
- end
- function RegisterBlock(pos, block)
- if block == nil then
- block_map[tostring(pos)] = {name="minecraft:air"}
- else
- block_map[tostring(pos)] = block
- end
- end
- -- ### Vector
- Vector = {__type = "Vector"}
- VectorMT = {__index = Vector}
- function VectorMT.__add(a, b)
- if type(a) == "number" then
- if type(b) == "Vector" then
- return Vector.new(a + b.x, a + b.y)
- else
- return Vector.new()
- end
- elseif type(a) == "Vector" then
- if type(b) == "Vector" then
- return Vector.new(a.x + b.x, a.y + b.y)
- elseif type(b) == "number" then
- return Vector.new(a.x + b, a.y + b)
- else
- return Vector.new()
- end
- end
- return Vector.new()
- end
- function VectorMT.__tostring(t)
- return t.x .. ", " .. t.y
- end
- function Vector.new(x, y)
- local self = setmetatable({}, VectorMT)
- self.x = x or 0
- self.y = y or 0
- return self
- end
- function Vector:copy()
- return Vector.new(self.x, self.y)
- end
- -- ### Movement ###
- ORIENTATION = {
- [0] = Vector.new(1, 0),
- [1] = Vector.new(0, 1),
- [2] = Vector.new(-1, 0),
- [3] = Vector.new(0, -1)}
- local position = Vector.new(0, 0)
- local orientation = 0
- function Move(distance)
- RegisterLeft()
- local is_block, block_infront = RegisterFront()
- RegisterRight()
- while is_block == false and distance > 0 do
- turtle.forward()
- print("Before move - Position: " .. tostring(position))
- print("Current orientation: " .. orientation)
- print("Adding vector: " .. tostring(ORIENTATION[orientation]))
- position = position + ORIENTATION[orientation]
- print("After move - Position: " .. tostring(position))
- RegisterLeft()
- is_block, block_infront = RegisterFront()
- RegisterRight()
- distance = distance - 1
- end
- end
- function RegisterFront()
- local front_position = position + ORIENTATION[orientation]
- local is_block, block_infront = turtle.inspect()
- RegisterBlock(front_position, block_infront)
- return is_block, block_infront
- end
- function RegisterLeft()
- turtle.turnLeft()
- local o = orientation - 1
- if o == 0 then
- o = 4
- end
- local is_block, block = RegisterFront()
- turtle.turnRight()
- return is_block, block
- end
- function RegisterRight()
- turtle.turnRight()
- local o = orientation + 1
- if o == 5 then
- o = 1
- end
- local is_block, block = RegisterFront()
- turtle.turnLeft()
- return is_block, block
- end
- ClearMap()
- -- local block = {name = "computercraft:advanced_turtle"}
- -- RegisterBlock(Vector.new(0,0), block)
- Move(10)
- print("Now printing moves")
- local count = 0
- for k, v in pairs(block_map) do
- count = count + 1
- print(" - Block at "..k.." is "..textutils.serialise(v.name))
- end
- print("Block Map length:" .. count)
Advertisement
Add Comment
Please, Sign In to add comment