Advertisement
Guest User

Untitled

a guest
May 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. local matrix = {};
  2. matrix.new = function(v1, v2, v3)
  3. local self = {};
  4. self.x = vector.new(v1.x, v1.y, v1.z);
  5. self.y = vector.new(v2.x, v2.y, v2.z);
  6. self.z = vector.new(v3.x, v3.y, v3.z);
  7.  
  8. self.multVec = function(self, v)
  9. return self.x * v.x + self.y * v.y + self.z * v.z;
  10. end
  11.  
  12. self.multMat = function(self, m)
  13. return matrix.new(self:multVec(m.x), self:multVec(m.y), self:multVec(m.z));
  14. end
  15.  
  16. self.transpose = function(self)
  17. return matrix.new( vector.new(self.x.x, self.y.x, self.z.x), vector.new(self.x.y, self.y.y, self.z.y), vector.new(self.x.z, self.y.z, self.z.z));
  18. end
  19.  
  20. self.multTransVec = function (self, v)
  21. return vector.new(v.dot(self.x), v.dot(self.Y), v.dot(self.Z));
  22. end
  23. end
  24.  
  25. local turtleState = {}
  26. turtleState.new = function()
  27. local self = {};
  28.  
  29. self.ensureWeHaveModem = function(self, right)
  30. if right == nil then
  31. right = true;
  32. end
  33.  
  34. local p = peripheral.find("modem");
  35. if p == nil then
  36. for slot = 1, 16 do
  37. local itemDetail = turtle.getItemDetail(slot);
  38. if itemDetail ~= nil and itemDetail.name == "computercraft:peripheral" and itemDetail.damage == 1 then
  39. turtle.select(slot);
  40. if right then
  41. turtle.equipRight()
  42. else
  43. turtle.equipLeft()
  44. end
  45. p = peripheral.find("modem");
  46. break;
  47. end
  48. end
  49. end
  50. return p;
  51. end
  52.  
  53. self.ensureWeHaveCompass = function(self, right)
  54. if right == nil then
  55. right = true;
  56. end
  57.  
  58. local p = peripheral.find("compass");
  59. if p == nil then
  60. for slot = 1, 16 do
  61. local itemDetail = turtle.getItemDetail(slot);
  62. if itemDetail ~= nil and itemDetail.name == "minecraft:compass" then
  63. local oldSlot = turtle.getSelectedSlot();
  64. turtle.select(slot);
  65. if right then
  66. turtle.equipRight()
  67. else
  68. turtle.equipLeft()
  69. end
  70. turtle.select(oldSlot);
  71. p = peripheral.find("compass");
  72. break;
  73. end
  74. end
  75. end
  76. return p;
  77. end
  78. return self;
  79. end
  80.  
  81. local area = {};
  82. area.new = function(x, y, z, rotation)
  83. local self = {};
  84. self.globalPosition = vector.new(x, y, z);
  85. self.globalRotationMatrix = rotationToMatrix(rotation)
  86.  
  87. self.getLocalPosition = function(self, state)
  88. state:ensureWeHaveModem();
  89. local globalPosition;
  90. while globalPosition == nil do
  91. globalPosition = vector.new(gps.locate(10));
  92. end
  93. globalPosition = globalPosition - self.globalPosition;
  94. return self.rotationToMatrix:multVec(globalPosition);
  95. end
  96.  
  97. self.getLocalRotationMatrix = function(self, state)
  98. local matrix = rotationToMatrix(state:ensureWeHaveCompass().getFacing());
  99. return self.rotationToMatrix:multMat(matrix);
  100. end
  101.  
  102. return self;
  103. end
  104.  
  105. local function rotationToMatrix(rotation)
  106. if rotation == "north" then
  107. return matrix.new(vector.new(-1, 0, 0), vector.new(0, 1, 0), vector.new( 0, 0, -1));
  108. elseif rotation == "east" then
  109. return matrix.new(vector.new( 0, 0, 1), vector.new(0, 1, 0), vector.new( -1, 0, 0));
  110. elseif rotation == "south" then
  111. return matrix.new(vector.new( 1, 0, 0), vector.new(0, 1, 0), vector.new( 0, 0, 1));
  112. elseif rotation == "west" then
  113. return matrix.new(vector.new( 0, 0, -1), vector.new(0, 1, 0), vector.new( 1, 0, 0));
  114. else
  115. return nil;
  116. end
  117. end
  118.  
  119. local state = turtleState.new();
  120. local a = area.new(-272, 12, 244, "west");
  121. print(a:getLocalPosition(state));
  122. local forward = vector.new(0, 0, 1);
  123. print(a:getLocalRotationMatrix(state):multVec(forward));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement