Advertisement
PaleoCrafter

Untitled

Jun 13th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 9.56 KB | None | 0 0
  1. /*
  2.  * The MIT License (MIT)
  3.  *
  4.  * Copyright (c) 2014 MineFormers
  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. package de.mineformers.core.util.world
  25.  
  26. import scala.collection.mutable
  27. import com.google.common.base.Objects
  28. import java.lang.{Integer => JInt}
  29. import net.minecraft.nbt.NBTTagCompound
  30. import net.minecraft.util.{AxisAlignedBB, Vec3}
  31.  
  32. /**
  33.  * BlockPos
  34.  * Manages the pool of block positions
  35.  *
  36.  * @author PaleoCrafter
  37.  */
  38. object BlockPos {
  39.   private val cache = mutable.WeakHashMap[(Int, Int, Int), BlockPos]()
  40.  
  41.   val Zero = BlockPos(0, 0, 0)
  42.   val One = BlockPos(1, 1, 1)
  43.  
  44.   def apply(tag: NBTTagCompound): BlockPos = apply(tag.getInteger("x"), tag.getInteger("y"), tag.getInteger("z"))
  45.  
  46.   /**
  47.    * Create a new [[BlockPos]] based on the given coordinates
  48.    * @param x the X of the new block position
  49.    * @param y the Y of the new block position
  50.    * @param z the Z of the new block position
  51.    * @return a [[BlockPos]] instance, either a new one or one from the cache
  52.    */
  53.   def apply(x: Int, y: Int, z: Int): BlockPos = apply((x, y, z))
  54.  
  55.   /**
  56.    * Create a new [[BlockPos]] based on the given coordinates
  57.    * @param coords a tuple of the coordinates in the form (x, y, z)
  58.    * @return a [[BlockPos]] instance, either a new one or one from the cache
  59.    */
  60.   def apply(coords: (Int, Int, Int)): BlockPos = cache.getOrElseUpdate(coords, new BlockPos(coords))
  61.  
  62.   def unapply(pos: BlockPos): Option[(Int, Int, Int)] = Some((pos.x, pos.y, pos.z))
  63. }
  64.  
  65. /**
  66.  * Don't use this constructor, use BlockPos(x, y, z) for caching!
  67.  *
  68.  * @param coords a tuple representing the position's coordinates
  69.  */
  70. class BlockPos private(coords: (Int, Int, Int)) extends Ordered[BlockPos] {
  71.   val (x, y, z) = coords
  72.   private val _hashCode = Objects.hashCode(JInt.valueOf(x), JInt.valueOf(y), JInt.valueOf(z))
  73.  
  74.   /**
  75.    * Add the given coordinates to this block pos
  76.    * @param x the X component to add
  77.    * @param y the Y component to add
  78.    * @param z the Z component to add
  79.    * @return a new (cached) BlockPos with the sum of the coordinates
  80.    */
  81.   def +(x: Int, y: Int, z: Int): BlockPos = this + BlockPos(x, y, z)
  82.  
  83.   /**
  84.    * Add the given coordinates to this block pos
  85.    * @param coords a tuple representing the coordinates to add
  86.    * @return a new (cached) BlockPos with the sum of the coordinates
  87.    */
  88.   def +(coords: (Int, Int, Int)): BlockPos = this + BlockPos(coords)
  89.  
  90.   /**
  91.    * Add the given coordinates to this block pos
  92.    * @param pos another BlockPos to add
  93.    * @return a new (cached) BlockPos with the sum of the coordinates
  94.    */
  95.   def +(pos: BlockPos): BlockPos = BlockPos(this.x + pos.x, this.y + pos.y, this.z + pos.z)
  96.  
  97.   /**
  98.    * Subtract the given coordinates from this block pos
  99.    * @param x the X component to subtract
  100.    * @param y the Y component to subtract
  101.    * @param z the Z component to subtract
  102.    * @return a new (cached) BlockPos with the difference of the coordinates
  103.    */
  104.   def -(x: Int, y: Int, z: Int): BlockPos = this + BlockPos(x, y, z)
  105.  
  106.   /**
  107.    * Subtract the given coordinates from this block pos
  108.    * @param coords a tuple representing the coordinates to subtract
  109.    * @return a new (cached) BlockPos with the difference of the coordinates
  110.    */
  111.   def -(coords: (Int, Int, Int)): BlockPos = this + BlockPos(coords)
  112.  
  113.   /**
  114.    * Subtract the given coordinates from this block pos
  115.    * @param pos another BlockPos to subtract
  116.    * @return a new (cached) BlockPos with the difference of the coordinates
  117.    */
  118.   def -(pos: BlockPos): BlockPos = this + -pos
  119.  
  120.   /**
  121.    * Multiply the coordinates of this block pos
  122.    * @param scalar a plain value every component of the BlockPos will be multiplied with
  123.    * @return a new (cached) BlockPos with the product of this position with the scalar
  124.    */
  125.   def *(scalar: Int): BlockPos = BlockPos(x * scalar, y * scalar, z * scalar)
  126.  
  127.   /**
  128.    * Multiply the given coordinates with this block pos
  129.    * @param x the X coordinate to multiply with
  130.    * @param y the Y coordinate to multiply with
  131.    * @param z the Z coordinate to multiply with
  132.    * @return a new (cached) BlockPos with the product of the form
  133.    *         (this.x * x, this.y * y, this.z * z)
  134.    */
  135.   def *(x: Int, y: Int, z: Int): BlockPos = this * BlockPos(x, y, z)
  136.  
  137.   /**
  138.    * Multiply the given coordinates with this block pos
  139.    * @param coords a tuple representing the coordinates to multiply with
  140.    * @return a new (cached) BlockPos with the product of the form
  141.    *         (this.x * coords.x, this.y * coords.y, this.z * coords.z)
  142.    */
  143.   def *(coords: (Int, Int, Int)): BlockPos = this * BlockPos(coords)
  144.  
  145.   /**
  146.    * Multiply the given coordinates with this block pos
  147.    * @param pos a tuple representing the coordinates to multiply with
  148.    * @return a new (cached) BlockPos with the product of the form
  149.    *         (this.x * pos.x, this.y * pos.y, this.z * pos.z)
  150.    */
  151.   def *(pos: BlockPos): BlockPos = BlockPos(this.x * pos.x, this.y * pos.y, this.z * pos.z)
  152.  
  153.   def unary_+ = this
  154.  
  155.   def unary_- = BlockPos(-x, -y, -z)
  156.  
  157.   /**
  158.    * @return the magnitude (length) of this block pos
  159.    */
  160.   def mag = {
  161.     math.sqrt(magSq)
  162.   }
  163.  
  164.   /**
  165.    * @return the squared magnitude (length) of this block pos
  166.    */
  167.   def magSq = {
  168.     x * x + y * y + z * z
  169.   }
  170.  
  171.   /**
  172.    * The distance between this block pos and the given coordinates
  173.    * @param x the X coordinate to calculate the distance to
  174.    * @param y the Y coordinate to calculate the distance to
  175.    * @param z the Z coordinate to calculate the distance to
  176.    * @return the distance between this position and the given coordinates
  177.    *         (this - (x, y, z)).mag
  178.    */
  179.   def distance(x: Int, y: Int, z: Int) = {
  180.     (this -(x, y, z)).mag
  181.   }
  182.  
  183.   /**
  184.    * The distance between this block pos and the given coordinates
  185.    * @param coords the coordinates to calculate the distance to
  186.    * @return the distance between this position and the given coordinates
  187.    *         (this - coords).mag
  188.    */
  189.   def distance(coords: (Int, Int, Int)) = {
  190.     (this - coords).mag
  191.   }
  192.  
  193.   /**
  194.    * The distance between this block pos and the given coordinates
  195.    * @param pos the coordinates to calculate the distance to
  196.    * @return the distance between this position and the given coordinates
  197.    *         (this - pos).mag
  198.    */
  199.   def distance(pos: BlockPos) = {
  200.     (this - pos).mag
  201.   }
  202.  
  203.   /**
  204.    * The squared distance between this block pos and the given coordinates
  205.    * @param x the X coordinate to calculate the distance to
  206.    * @param y the Y coordinate to calculate the distance to
  207.    * @param z the Z coordinate to calculate the distance to
  208.    * @return the squared distance between this position and the given coordinates
  209.    *         (this - (x, y, z)).magSq
  210.    */
  211.   def distanceSq(x: Int, y: Int, z: Int) = {
  212.     (this -(x, y, z)).magSq
  213.   }
  214.  
  215.   /**
  216.    * The squared distance between this block pos and the given coordinates
  217.    * @param coords the coordinates to calculate the distance to
  218.    * @return the squared distance between this position and the given coordinates
  219.    *         (this - coords).magSq
  220.    */
  221.   def distanceSq(coords: (Int, Int, Int)) = {
  222.     (this - coords).magSq
  223.   }
  224.  
  225.   /**
  226.    * The squared distance between this block pos and the given coordinates
  227.    * @param pos the coordinates to calculate the distance to
  228.    * @return the squared distance between this position and the given coordinates
  229.    *         (this - pos).magSq
  230.    */
  231.   def distanceSq(pos: BlockPos) = {
  232.     (this - pos).magSq
  233.   }
  234.  
  235.   /**
  236.    * @return true if all components are 0
  237.    */
  238.   def isZero: Boolean = {
  239.     x == 0 && y == 0 && z == 0
  240.   }
  241.  
  242.   /**
  243.    * @return true if this block position is aligned axial
  244.    */
  245.   def isAxial: Boolean = {
  246.     if (x == 0) y == 0 || z == 0 else y == 0 && z == 0
  247.   }
  248.  
  249.   def toVec3 = Vec3.createVectorHelper(x, y, z)
  250.  
  251.   def containedBy(bounds: AxisAlignedBB): Boolean = bounds.minX <= x && bounds.minY <= y && bounds.minZ <= z && bounds.maxX > x && bounds.maxY > y && bounds.maxZ > z
  252.  
  253.   override def hashCode = _hashCode
  254.  
  255.   override def equals(that: Any): Boolean = {
  256.     that match {
  257.       case BlockPos(thatX, thatY, thatZ) => return this.x == thatX && this.y == thatY && this.z == thatZ
  258.       case (thatX, thatY, thatZ) => return this.x == thatX && this.y == thatY && this.z == thatZ
  259.     }
  260.     false
  261.   }
  262.  
  263.   override def compare(o: BlockPos): Int = {
  264.     if (x != o.x) return if (x < o.x) 1 else -1
  265.     if (y != o.y) return if (y < o.y) 1 else -1
  266.     if (z != o.z) return if (z < o.z) 1 else -1
  267.     0
  268.   }
  269.  
  270.   override def toString = "( " + x + ", " + y + ", " + z + " )"
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement