alesandreo

tests/turtle.test.lua

Aug 11th, 2021 (edited)
1,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.62 KB | None | 0 0
  1. -- https://pastebin.com/tphvJG4e
  2.  
  3. turtle = nil
  4. if not textutils then
  5.   require('lib.ale.mock.textutils')
  6. end
  7. require ('lib.ale.ale')
  8. function Assert(booltest, message)
  9.   message = message or "Test failed."
  10.   if not booltest then
  11.     error(message)
  12.   end
  13. end
  14.  
  15.  
  16. t = Miner:new(1,1,1, "South")
  17. t:printPosition()
  18. Assert(t.position:compare({x = 1, y = 1, z = 1, face = 2}), "Miner didn't start in the correct starting position.")
  19. t:save_position("start")
  20. print(t.saved_positions.start:toString())
  21. Assert(t.position:compare(t.saved_positions.start), "Saved position doesn't match starting turtle position.")
  22. t:goToPosition({x = 0, y = 0, z = 0, face = 0})
  23. Assert(t.position:compare({x = 0, y = 0, z = 0, face = 0}), "Turtle failed to find it's way to 0,0,0")
  24. print("Located: "..t.position:toString())
  25. print("Saved: "..t.saved_positions.start:toString())
  26. t:save_position("end")
  27. Assert(t.saved_positions['start']:compare({x = 1, y = 1, z = 1, face = 2}), "Turtle lost his saved \"start\" position.")
  28. Assert(t.saved_positions['end']:compare({x = 0, y = 0, z = 0, face = 0}), "Turtle lost his saved \"end\" position.")
  29. t:rewindToSavedPosition("start")
  30. t:printPosition()
  31. Assert(t.position:compare({x = 1, y = 1, z = 1, face = 2}), "Turtle failed to rewind to start.")
  32. t:replay(3)
  33. t:printPosition()
  34. Assert(t.position:compare({x = 1, y = 0, z = 1, face = 0}), "Replay 3 didn't get where it was going.")
  35. t:replayToSavedPosition("end")
  36. t:printPosition()
  37. Assert(t.position:compare({x = 0, y = 0, z = 0, face = 0}), "Replayed to save position end didn't work.")
  38.  
  39. t:forward()
  40. Assert(t.position:compare(Position:new(0, 0, -1, 0)), "North didn't decrement z properly.")
  41. t:back()
  42. Assert(t.position:compare(Position:new(0, 0, 0, 0)), "North back didn't increment z properly.")
  43. t:turnRight()
  44. Assert(t.position:compare(Position:new(0, 0, 0, 1)), "Turning right didn't increment face properly.")
  45. t:turnLeft()
  46. Assert(t.position:compare(Position:new(0, 0, 0, 0)), "Turning left didn't decrement face properly.")
  47. t:turnRight()
  48. t:forward()
  49. Assert(t.position:compare(Position:new(1, 0, 0, 1)), "East didn't increment x properly.")
  50. t:back()
  51. Assert(t.position:compare(Position:new(0, 0, 0, 1)), "East back didn't decrement x properly.")
  52. t:forward()
  53. t:turnRight()
  54. t:forward()
  55. Assert(t.position:compare(Position:new(1, 0, 1, 2)), "South didn't increment z properly.")
  56. t:back()
  57. Assert(t.position:compare(Position:new(1, 0, 0, 2)), "South didn't increment z properly.")
  58. t:turnRight()
  59. t:turnRight(false)
  60. Assert(t.position:compare(Position:new(1, 0, 0, 0)), "Turning right past west didn't reset to 0 properly.")
  61. t:turnLeft(false)
  62. t:turnLeft()
  63. Assert(t.position:compare(Position:new(1, 0, 0, 2)), "Turning left past north didn't reset to 3 properly.")
  64. t:forward()
  65. Assert(t.position:compare(Position:new(1, 0, 1, 2)), "South forward didn't increment z.")
  66. t:up()
  67. Assert(t.position:compare(Position:new(1, 1, 1, 2)), "Up didn't increment y.")
  68. t:turnRight()
  69. t:back()
  70. Assert(t.position:compare(Position:new(2, 1, 1, 3)), "West back didn't increment x.")
  71. t:forward()
  72. Assert(t.position:compare(Position:new(1, 1, 1, 3)), "West didn't decrement x.")
  73. t:rewind(11)
  74. Assert(t.position:compare(Position:new(0, 0, 0, 1)), "Failed to rewind 11 positions to where I expected.")
  75. t:turnAround()
  76. Assert(t.position:compare(Position:new(0, 0, 0, 3)), "Failed to turnAround.")
  77. t:forward()
  78. t:forward()
  79. t:back()
  80. t:back()
  81. t:back()
  82. t:down()
  83. Assert(t.position:compare(Position:new(1, -1, 0, 3)), "After dancing, I'm not where I expected to be.")
  84.  
  85.  
  86. Slot = t.inventory:getSlot("minecraft:dirt")
  87. Slot:select()
  88. t.whitelist:addBlock('minecraft:iron_ore')
  89. t.whitelist:addBlock('minecraft:oak_log')
  90. t.whitelist:addTag('forge:ores')
  91. Assert(t:isValuable({name = 'minecraft:iron_ore', tags = {}}), "Iron ore isn't marked as valuable.")
  92. Assert(not t:isValuable({name = 'minecraft:god_mode', tags = {}}), "Untagged ore is whitelisting.")
  93. Assert(t:isValuable({name = 'minecraft:diamond', tags = {['forge:ores'] = true}}), "Tag forge ores isn't marked as valuable.")
  94. Assert(not t:isValuable({name = 'minecraft:diamond', tags = {['forge:not_ore'] = true}}), "Untagged ore is whitelisting.")
  95. t.filler:addBlock('minecraft:dirt')
  96. t.blacklist:addBlock('minecraft:dirt')
  97. t:delete_position("start")
  98. t:save_position("start")
  99. t:mineBlock()
  100. t:rewindToSavedPosition("start")
  101. Assert(t.position:compare(t.saved_positions["start"]), "Didn't rewind properly.")
  102.  
  103. T = Miner:new(217, 4, -69, 1)
  104. print(T.position:toString())
  105. Target = Position:new(32, 15, 16, "North")
  106. Target:offsetByPosition(T.position)
  107. T:goToPosition(Target)
  108. -- for x = 0, 16, 1 do
  109. --   print("Mining")
  110. --   t:mineBlock()
  111. --   t:printPosition()
  112. -- end
  113.  
  114.  
Add Comment
Please, Sign In to add comment