Advertisement
Agent_Silence

Gladius

Dec 18th, 2014 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.21 KB | None | 0 0
  1. direction = "^" -- Arrow pointing towards direction.
  2. positions = {curX = 0, curZ = 0, curY = 0} -- Positions
  3. tMap = {} -- Map
  4. blockTable = {
  5. {name = "minecraft:stone", color = colors.lightGray, isOre = false, oreColor = colors.black},
  6. {name = "minecraft:planks", color = colors.brown, isOre = false, oreColor = colors.black},
  7. {name = "minecraft:grass", color = colors.green, isOre = false, oreColor = colors.black},
  8. {name = "minecraft:dirt", color = colors.brown, isOre = false, oreColor = colors.black},
  9. {name = "minecraft:cobblestone", color = colors.lightGray, isOre = false, oreColor = colors.black},
  10. {name = "minecraft:water", color = colors.blue, isOre = false, oreColor = colors.black},
  11. {name = "minecraft:flowing_water", color = colors.blue, isOre = false, oreColor = colors.black},
  12. {name = "minecraft:lava", color = colors.orange, isOre = false, oreColor = colors.black},
  13. {name = "minecraft:flowing_lava", color = colors.lightGray, isOre = false, oreColor = colors.black},
  14. {name = "minecraft:sand", color = colors.yellow, isOre = false, oreColor = colors.black},
  15. {name = "minecraft:gravel", color = colors.pink, isOre = false, oreColor = colors.black},
  16. {name = "minecraft:gold_ore", color = colors.lightGray, isOre = true, oreColor = colors.yellow},
  17. {name = "minecraft:iron_ore", color = colors.lightGray, isOre = true, oreColor = colors.white},
  18. {name = "minecraft:coal_ore", color = colors.lightGray, isOre = true, oreColor = colors.black},
  19. {name = "minecraft:lapis_ore", color = colors.lightGray, isOre = true, oreColor = colors.blue},
  20. {name = "minecraft:diamond_ore", color = colors.lightGray, isOre = true, oreColor = colors.lightBlue},
  21. {name = "minecraft:emerald_ore", color = colors.lightGray, isOre = true, oreColor = colors.lime},
  22. {name = "minecraft:redstone_ore", color = colors.lightGray, isOre = true, oreColor = colors.red},
  23. {name = "minecraft:lit_redstone_ore", color = colors.lightGray, isOre = true, oreColor = colors.red}} -- Table for all blocks
  24. lookingAt = "Unable to discern"
  25.  
  26.  
  27. rednet.open("top") -- Opens rednet
  28. map = window.create(term.native(),1,1,27,19) -- Creates seperate window
  29. term.setBackgroundColor(colors.lightGray)
  30. term.clear()
  31.  
  32.  
  33. function right() -- Turns the arrow right
  34.   if direction == "^" then
  35.     direction = ">"
  36.   elseif direction == ">" then
  37.     direction = "v"
  38.   elseif direction == "v" then
  39.     direction = "<"
  40.   elseif direction == "<" then
  41.     direction = "^"
  42.   end
  43. end
  44.  
  45. function left() -- Turns the arrow left
  46.   if direction == "^" then
  47.     direction = "<"
  48.   elseif direction == "<" then
  49.     direction = "v"
  50.   elseif direction == "v" then
  51.     direction = ">"
  52.   elseif direction == ">" then
  53.     direction = "^"
  54.   end
  55. end
  56.  
  57. function discernDir() -- Finds direction to move
  58.   if direction == "^" then
  59.     positions.curZ = positions.curZ - 1
  60.   elseif direction == ">" then
  61.     positions.curX = positions.curX + 1
  62.   elseif direction == "v" then
  63.     positions.curZ = positions.curZ + 1
  64.   elseif direction == "<" then
  65.     positions.curX = positions.curX - 1
  66.   end
  67. end
  68.  
  69. function lookingZ() -- Finds direction to move
  70.   if direction == "^" then
  71.     local a = positions.curZ + 1
  72.     return positions.curZ - a
  73.   elseif direction == "v" then
  74.     local a = positions.curZ - 1
  75.     return positions.curZ - a
  76.   else
  77.     return 0
  78.   end
  79. end
  80.  
  81. function lookingX() -- Finds direction to move
  82.   if direction == ">" then
  83.     local a = positions.curZ - 1
  84.     return positions.curZ - a
  85.   elseif direction == "<" then
  86.     local a = positions.curX + 1
  87.     return positions.curX - a
  88.   else
  89.     return 0
  90.   end
  91. end
  92.  
  93. function newBlock(X,Y,Color,Layer,isOre,oreColor) -- Creates or overides a block on the map
  94.   for i,v in pairs(tMap) do
  95.     if v.x == X and v.y == Y and v.layer == Layer and v.color ~= Color then
  96.       local template = {
  97.         x = X,
  98.         y = Y,
  99.         color = Color,
  100.         layer = Layer,
  101.         ore = isOre}
  102.         if isOre then
  103.           template.oreColor = oreColor
  104.         end
  105.         table.insert(tMap,template)
  106.     elseif v.x == X and v.y == Y and v.layer == Layer and v.color == Color then
  107.       marked = true
  108.     end
  109.   end
  110.   if not marked then
  111.     local template = {
  112.       x = X,
  113.       y = Y,
  114.       color = Color,
  115.       layer = Layer,
  116.       ore = isOre}
  117.       if isOre then
  118.         template.oreColor = oreColor
  119.       end
  120.     table.insert(tMap,template)
  121.   end
  122. end
  123.  
  124. function redrawMap() -- Redraws the map
  125.   term.setBackgroundColor(colors.lightGray)
  126.   term.clear()
  127.   term.setTextColor(colors.white)
  128.   term.setCursorPos(28,1)
  129.   term.write("X : "..positions.curX)
  130.   term.setCursorPos(28,2)
  131.   term.write("Z : "..positions.curZ)
  132.   term.setCursorPos(28,3)
  133.   term.write("Y : "..positions.curY)
  134.   term.setCursorPos(28,4)
  135.   term.write("Looking at : ")
  136.   term.setCursorPos(29,5)
  137.   term.write(lookingAt)
  138.   map.setBackgroundColor(colors.gray)
  139.   map.clear()
  140.   term.redirect(map)
  141.   for i,v in pairs(tMap) do
  142.     if v.x - positions.curX > -14 and v.x - positions.curX < 28 and v.y - positions.curZ  > -10 and v.y - positions.curZ < 20 and positions.curY == v.layer + 1 then
  143.       paintutils.drawPixel(v.x - positions.curX + 14, v.y - positions.curZ + 10, colors.black)
  144.     end
  145.     if v.x - positions.curX > -14 and v.x - positions.curX < 28 and v.y - positions.curZ  > -10 and v.y - positions.curZ < 20 and positions.curY == v.layer then
  146.       paintutils.drawPixel(v.x - positions.curX + 14, v.y - positions.curZ + 10, v.color)
  147.       if v.ore == true then
  148.         map.setCursorPos(v.x - positions.curX + 14, v.y - positions.curZ + 10)
  149.         map.setTextColor(v.oreColor)
  150.         map.write("#")
  151.       end
  152.     end
  153.   end
  154.   term.setCursorPos(14,10)
  155.   term.setBackgroundColor(colors.yellow)
  156.   term.setTextColor(colors.black)
  157.   term.write(direction)
  158.   term.redirect(term.native())
  159. end
  160.  
  161. function turtleError() -- Creates an error screen for the map
  162.   map.setBackgroundColor(colors.blue)
  163.   map.clear()
  164.   local text = "Turtle disconnected"
  165.   local w,h = map.getSize()
  166.   map.setCursorPos(math.floor(w / 2 - text:len() / 2 + .5), math.floor(h / 2 + .5)) -- Positions text in the middle of the map
  167.   map.setTextColor(colors.white)
  168.   map.write(text)
  169. end
  170.  
  171. function logBlock() -- Logs found block into the database
  172.   local id, msg = rednet.receive("gladius")
  173.   if msg ~= "Nothing" then
  174.     lookingAt = msg
  175.     for i,v in pairs(blockTable) do
  176.       if msg == v.name then
  177.         newBlock(positions.curX + lookingX(), positions.curZ + lookingZ(), v.color, positions.curY, v.isOre, v.oreColor)
  178.       end
  179.     end
  180.   else
  181.     lookingAt = "Nothing"
  182.   end
  183. redrawMap()
  184. end
  185. redrawMap()
  186. while true do
  187.   local event, key = os.pullEvent("key")
  188.   if key == keys.up then
  189.     rednet.broadcast("forward","gladius")
  190.     discernDir()
  191.     logBlock()
  192.   elseif key == keys.down then
  193.     rednet.broadcast("backward","gladius")
  194.     right()
  195.     right()
  196.     logBlock()
  197.   elseif key == keys.left then
  198.     rednet.broadcast("left","gladius")
  199.     left()
  200.     logBlock()
  201.   elseif key == keys.right then
  202.     rednet.broadcast("right","gladius")
  203.     right()
  204.     logBlock()
  205.   elseif key == keys.leftShift then
  206.     rednet.broadcast("down","gladius")
  207.     positions.curY = positions.curY - 1
  208.     logBlock()
  209.   elseif key == keys.space then
  210.     rednet.broadcast("up","gladius")
  211.     positions.curY = positions.curY + 1
  212.     logBlock()
  213.   end
  214.   redrawMap()
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement