Advertisement
blunty666

location

Sep 5th, 2015
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 KB | None | 0 0
  1. --[[
  2. The MIT License (MIT)
  3.  
  4. Copyright (c) 2012 Lyqyd
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. --]]
  24.  
  25. -- Lyqyds location API (https://github.com/lyqyd/location)
  26. -- the headings have been altered to use the Minecraft directions
  27. -- have also made some simplifications to suit the needed use case
  28.  
  29. local location = {
  30.     add = function(self, o)
  31.         return vector.new(
  32.             self.x + o.x,
  33.             self.y + o.y,
  34.             self.z + o.z
  35.         )
  36.     end,
  37.     sub = function(self, o)
  38.         return vector.new(
  39.             self.x - o.x,
  40.             self.y - o.y,
  41.             self.z - o.z
  42.         )
  43.     end,
  44.     mul = function(self, m)
  45.         return vector.new(
  46.             self.x * m,
  47.             self.y * m,
  48.             self.z * m
  49.         )
  50.     end,
  51.     div = function(self, d)
  52.         return vector.new(
  53.             self.x / d,
  54.             self.y / d,
  55.             self.z / d
  56.         )
  57.     end,
  58.     left = function(self)
  59.         if turtle.turnLeft() then
  60.             self.h = (self.h - 1) % 4
  61.             return true
  62.         end
  63.         return false
  64.     end,
  65.     right = function(self)
  66.         if turtle.turnRight() then
  67.             self.h = (self.h + 1) % 4
  68.             return true
  69.         end
  70.         return false
  71.     end,
  72.     forward = function(self)
  73.         if turtle.forward() then
  74.             self.x = self.x + (self.h - 2) * (self.h % 2)
  75.             self.z = self.z + (1 - self.h) * ((self.h + 1) % 2)
  76.             return true
  77.         end
  78.         return false
  79.     end,
  80.     back = function(self)
  81.         if turtle.back() then
  82.             self.x = self.x - (self.h - 2) * (self.h % 2)
  83.             self.z = self.z - (1 - self.h) * ((self.h + 1) % 2)
  84.             return true
  85.         end
  86.         return false
  87.     end,
  88.     up = function(self)
  89.         if turtle.up() then
  90.             self.y = self.y + 1
  91.             return true
  92.         end
  93.         return false
  94.     end,
  95.     down = function(self)
  96.         if turtle.down() then
  97.             self.y = self.y - 1
  98.             return true
  99.         end
  100.         return false
  101.     end,
  102.     moveDeltas = function(self)
  103.         return (self.h - 2) * (self.h % 2), (1 - self.h) * ((self.h + 1) % 2)
  104.     end,
  105.     setHeading = function(self, heading)
  106.         if not heading or heading < 0 or heading > 3 then return nil, "Heading Not in Range" end
  107.         while self.h ~= heading do
  108.             if (self.h + 1) % 4 == heading % 4 then
  109.                 self:right()
  110.             else
  111.                 self:left()
  112.             end
  113.         end
  114.         return true
  115.     end,
  116.     getHeading = function(self)
  117.         return self.h
  118.     end,
  119.     tovector = function(self)
  120.         if vector then
  121.             return vector.new(
  122.                 self.x,
  123.                 self.y,
  124.                 self.z
  125.             )
  126.         else
  127.             return nil
  128.         end
  129.     end,
  130.     tostring = function(self)
  131.         return self.x..","..self.y..","..self.z..","..self.h
  132.     end,
  133.     value = function(self)
  134.         return self.x, self.y, self.z, self.h
  135.     end,
  136. }
  137.  
  138. local lmetatable = {
  139.     __index = location,
  140.     __add = location.add,
  141.     __sub = location.sub,
  142.     __mul = location.mul,
  143.     __div = location.div,
  144.     __unm = function(l) return l:mul(-1) end,
  145.     __tostring = function(l) return l:tostring() end,
  146. }
  147.  
  148. function new( x, y, z, h )
  149.     local l = {
  150.         x = x or 0,
  151.         y = y or 0,
  152.         z = z or 0,
  153.         h = h or 1
  154.     }
  155.     setmetatable( l, lmetatable )
  156.     return l
  157. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement