| 58 | } |
| 59 | |
| 60 | void VoxelMap::calculateCentre() |
| 61 | { |
| 62 | this->centreChanged = false; |
| 63 | |
| 64 | // This calculates the 'centre' of the voxel map by finding |
| 65 | // the average position of all 'filled' voxels |
| 66 | // An 'int' should be more than enough to keep the sum |
| 67 | // of even the largest completely filled voxel map |
| 68 | Vec3<int> sum = {0, 0, 0}; |
| 69 | int numFilled = 0; |
| 70 | |
| 71 | for (int z = 0; z < this->size.z; z++) |
| 72 | { |
| 73 | // The following would figure out whether a pixel is filled or not |
| 74 | // However, vanilla always aims at map's centre |
| 75 | // This provides for small vehicles to "dodge shots", which actually |
| 76 | // are just misaligned voxelmaps assigned to them |
| 77 | // Therefore, we should not check this to recreate vanilla behavior |
| 78 | /* |
| 79 | for (int y = 0; y < this->size.y; y++) |
| 80 | { |
| 81 | for (int x = 0; x < this->size.x; x++) |
| 82 | { |
| 83 | |
| 84 | if (this->getBit({x, y, z})) |
| 85 | { |
| 86 | sum += Vec3<int>{x, y, z}; |
| 87 | numFilled++; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | */ |
| 92 | // Instead, we consider layer filled if one bit is |
| 93 | if (this->slices[z] && !this->slices[z]->isEmpty()) |
| 94 | { |
| 95 | sum += Vec3<int>{this->size.x / 2, this->size.y / 2, z}; |
| 96 | numFilled++; |
| 97 | } |
| 98 | } |
| 99 | if (numFilled == 0) |
| 100 | { |
| 101 | // Special case 'empty' voxel maps to be the middle of the bounds? |
| 102 | this->centre = this->size / 2; |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | this->centre = sum / numFilled; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | bool VoxelMap::operator==(const VoxelMap &other) const |
| 111 | { |