the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 189 lines 4.7 kB view raw
1#include "stdafx.h" 2#include "BoundingBox.h" 3#include "Direction.h" 4#include "JavaMath.h" 5 6BoundingBox::BoundingBox() 7{ 8 // 4J added initialisers 9 x0 = 0; 10 y0 = 0; 11 z0 = 0; 12 x1 = 0; 13 y1 = 0; 14 z1 = 0; 15} 16 17BoundingBox::BoundingBox(intArray sourceData) 18{ 19 if (sourceData.length == 6) 20 { 21 x0 = sourceData[0]; 22 y0 = sourceData[1]; 23 z0 = sourceData[2]; 24 x1 = sourceData[3]; 25 y1 = sourceData[4]; 26 z1 = sourceData[5]; 27 } 28} 29 30BoundingBox *BoundingBox::getUnknownBox() 31{ 32 return new BoundingBox(INT_MAX, INT_MAX, INT_MAX, INT_MIN, INT_MIN, INT_MIN ); 33} 34 35BoundingBox *BoundingBox::orientBox(int footX, int footY, int footZ, int offX, int offY, int offZ, int width, int height, int depth, int orientation) 36{ 37 switch (orientation) 38 { 39 default: 40 return new BoundingBox(footX + offX, footY + offY, footZ + offZ, footX + width - 1 + offX, footY + height - 1 + offY, footZ + depth - 1 + offZ); 41 case Direction::NORTH: 42 // foot is at x0, y0, z1 43 return new BoundingBox(footX + offX, footY + offY, footZ - depth + 1 + offZ, footX + width - 1 + offX, footY + height - 1 + offY, footZ + offZ); 44 case Direction::SOUTH: 45 // foot is at x0, y0, z0 46 return new BoundingBox(footX + offX, footY + offY, footZ + offZ, footX + width - 1 + offX, footY + height - 1 + offY, footZ + depth - 1 + offZ); 47 case Direction::WEST: 48 // foot is at x1, y0, z0, but width and depth are flipped 49 return new BoundingBox(footX - depth + 1 + offZ, footY + offY, footZ + offX, footX + offZ, footY + height - 1 + offY, footZ + width - 1 + offX); 50 case Direction::EAST: 51 // foot is at x0, y0, z0, but width and depth are flipped 52 return new BoundingBox(footX + offZ, footY + offY, footZ + offX, footX + depth - 1 + offZ, footY + height - 1 + offY, footZ + width - 1 + offX); 53 } 54} 55 56BoundingBox::BoundingBox(BoundingBox *other) 57{ 58 x0 = other->x0; 59 y0 = other->y0; 60 z0 = other->z0; 61 x1 = other->x1; 62 y1 = other->y1; 63 z1 = other->z1; 64} 65 66BoundingBox::BoundingBox(int x0, int y0, int z0, int x1, int y1, int z1) 67{ 68 this->x0 = x0; 69 this->y0 = y0; 70 this->z0 = z0; 71 this->x1 = x1; 72 this->y1 = y1; 73 this->z1 = z1; 74} 75 76BoundingBox::BoundingBox(int x0, int z0, int x1, int z1) 77{ 78 this->x0 = x0; 79 this->z0 = z0; 80 this->x1 = x1; 81 this->z1 = z1; 82 83 // the bounding box for this constructor is limited to world size, 84 // excluding bedrock level 85 y0 = 1; 86 y1 = 512; 87} 88 89bool BoundingBox::intersects(BoundingBox *other) 90{ 91 return !(this->x1 < other->x0 || this->x0 > other->x1 || this->z1 < other->z0 || this->z0 > other->z1 || this->y1 < other->y0 || this->y0 > other->y1); 92} 93 94bool BoundingBox::intersects(int x0, int y0, int z0, int x1, int y1, int z1) 95{ 96 return !(this->x1 < x0 || this->x0 > x1 || this->z1 < z0 || this->z0 > z1 || this->y1 < y0 || this->y0 > y1); 97} 98 99bool BoundingBox::intersects(int x0, int z0, int x1, int z1) 100{ 101 return !(this->x1 < x0 || this->x0 > x1 || this->z1 < z0 || this->z0 > z1); 102} 103 104void BoundingBox::expand(BoundingBox *other) 105{ 106 x0 = Math::_min(x0, other->x0); 107 y0 = Math::_min(y0, other->y0); 108 z0 = Math::_min(z0, other->z0); 109 x1 = Math::_max(x1, other->x1); 110 y1 = Math::_max(y1, other->y1); 111 z1 = Math::_max(z1, other->z1); 112} 113 114BoundingBox *BoundingBox::getIntersection(BoundingBox *other) 115{ 116 if (!intersects(other)) 117 { 118 return NULL; 119 } 120 BoundingBox *result = new BoundingBox(); 121 result->x0 = Math::_max(x0, other->x0); 122 result->y0 = Math::_max(y0, other->y0); 123 result->z0 = Math::_max(z0, other->z0); 124 result->x1 = Math::_min(x1, other->x1); 125 result->y1 = Math::_min(y1, other->y1); 126 result->z1 = Math::_min(z1, other->z1); 127 128 return result; 129} 130 131void BoundingBox::move(int dx, int dy, int dz) 132{ 133 x0 += dx; 134 y0 += dy; 135 z0 += dz; 136 x1 += dx; 137 y1 += dy; 138 z1 += dz; 139} 140 141bool BoundingBox::isInside(int x, int y, int z) 142{ 143 return (x >= x0 && x <= x1 && z >= z0 && z <= z1 && y >= y0 && y <= y1); 144} 145 146int BoundingBox::getXSpan() 147{ 148 return x1 - x0 + 1; 149} 150 151int BoundingBox::getYSpan() 152{ 153 return y1 - y0 + 1; 154} 155 156int BoundingBox::getZSpan() 157{ 158 return z1 - z0 + 1; 159} 160 161int BoundingBox::getXCenter() 162{ 163 return x0 + (x1 - x0 + 1) / 2; 164} 165 166int BoundingBox::getYCenter() 167{ 168 return y0 + (y1 - y0 + 1) / 2; 169} 170 171int BoundingBox::getZCenter() 172{ 173 return z0 + (z1 - z0 + 1) / 2; 174} 175 176wstring BoundingBox::toString() 177{ 178 return L"(" + _toString<int>(x0) + L", " + _toString<int>(y0) + L", " + _toString<int>(z0) + L"; " + _toString<int>(x1) + L", " + _toString<int>(y1) + L", " + _toString<int>(z1) + L")"; 179} 180 181IntArrayTag *BoundingBox::createTag(const wstring &name) 182{ 183 // 4J-JEV: If somebody knows a better way to do this, please tell me. 184 int *data = new int[6](); 185 data[0] = x0; data[1] = y0; data[2] = z0; 186 data[3] = x1; data[4] = y1; data[5] = z1; 187 188 return new IntArrayTag( name, intArray(data,6) ); 189}