openspades/Sources/Client/GameMapWrapper.cpp

368 lines
11 KiB
C++
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
2016-11-20 19:13:00 +09:00
2013-08-29 11:45:22 +09:00
This file is part of OpenSpades.
2016-11-20 19:13:00 +09:00
2013-08-29 11:45:22 +09:00
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2016-11-20 19:13:00 +09:00
2013-08-29 11:45:22 +09:00
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2016-11-20 19:13:00 +09:00
2013-08-29 11:45:22 +09:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
2016-11-20 19:13:00 +09:00
2013-08-29 11:45:22 +09:00
*/
2013-08-18 16:18:06 +09:00
#include <cstring>
2013-08-18 16:18:06 +09:00
#include <deque>
#include <set>
#include <vector>
#include <Core/Debug.h>
#include <Core/Debug.h>
#include <Core/Deque.h>
#include <Core/Stopwatch.h>
#include "GameMap.h"
#include "GameMapWrapper.h"
2013-08-18 16:18:06 +09:00
namespace spades {
namespace client {
2016-11-20 19:13:00 +09:00
GameMapWrapper::GameMapWrapper(GameMap *mp) : map(mp) {
2013-08-18 16:18:06 +09:00
SPADES_MARK_FUNCTION();
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
width = mp->Width();
height = mp->Height();
depth = mp->Depth();
linkMap = new uint8_t[width * height * depth];
2013-08-18 16:18:06 +09:00
memset(linkMap, 0, width * height * depth);
}
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
GameMapWrapper::~GameMapWrapper() {
SPADES_MARK_FUNCTION();
delete[] linkMap;
}
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
void GameMapWrapper::Rebuild() {
SPADES_MARK_FUNCTION();
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
Stopwatch stopwatch;
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
GameMap *m = map;
memset(linkMap, 0, width * height * depth);
2016-11-20 19:13:00 +09:00
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
2013-08-18 16:18:06 +09:00
SetLink(x, y, depth - 1, Root);
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
Deque<CellPos> queue(width * height * 2);
2016-11-20 19:13:00 +09:00
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
if (m->IsSolid(x, y, depth - 2)) {
SetLink(x, y, depth - 2, PositiveZ);
2013-08-18 16:18:06 +09:00
queue.Push(CellPos(x, y, depth - 2));
}
2016-11-20 19:13:00 +09:00
while (!queue.IsEmpty()) {
2013-08-18 16:18:06 +09:00
CellPos p = queue.Front();
queue.Shift();
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
int x = p.x, y = p.y, z = p.z;
2016-11-20 19:13:00 +09:00
if (p.x > 0 && m->IsSolid(x - 1, y, z) && GetLink(x - 1, y, z) == Invalid) {
SetLink(x - 1, y, z, PositiveX);
queue.Push(CellPos(x - 1, y, z));
2013-08-18 16:18:06 +09:00
}
if (p.x < width - 1 && m->IsSolid(x + 1, y, z) && GetLink(x + 1, y, z) == Invalid) {
SetLink(x + 1, y, z, NegativeX);
queue.Push(CellPos(x + 1, y, z));
2013-08-18 16:18:06 +09:00
}
if (p.y > 0 && m->IsSolid(x, y - 1, z) && GetLink(x, y - 1, z) == Invalid) {
SetLink(x, y - 1, z, PositiveY);
queue.Push(CellPos(x, y - 1, z));
2013-08-18 16:18:06 +09:00
}
if (p.y < height - 1 && m->IsSolid(x, y + 1, z) &&
GetLink(x, y + 1, z) == Invalid) {
SetLink(x, y + 1, z, NegativeY);
queue.Push(CellPos(x, y + 1, z));
2013-08-18 16:18:06 +09:00
}
if (p.z > 0 && m->IsSolid(x, y, z - 1) && GetLink(x, y, z - 1) == Invalid) {
SetLink(x, y, z - 1, PositiveZ);
queue.Push(CellPos(x, y, z - 1));
2013-08-18 16:18:06 +09:00
}
if (p.z < depth - 1 && m->IsSolid(x, y, z + 1) && GetLink(x, y, z + 1) == Invalid) {
SetLink(x, y, z + 1, NegativeZ);
queue.Push(CellPos(x, y, z + 1));
2013-08-18 16:18:06 +09:00
}
}
2016-11-20 19:13:00 +09:00
SPLog("%.3f msecs to rebuild", stopwatch.GetTime() * 1000.);
2013-08-18 16:18:06 +09:00
}
2016-11-20 19:13:00 +09:00
void GameMapWrapper::AddBlock(int x, int y, int z, uint32_t color) {
2013-08-18 16:18:06 +09:00
SPADES_MARK_FUNCTION();
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
GameMap *m = map;
2016-11-20 19:13:00 +09:00
if (GetLink(x, y, z) != Invalid) {
2013-08-18 16:18:06 +09:00
SPAssert(m->IsSolid(x, y, z));
return;
}
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
m->Set(x, y, z, true, color);
2016-11-20 19:13:00 +09:00
if (GetLink(x, y, z) != Invalid) {
2013-08-18 16:18:06 +09:00
return;
}
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
LinkType l = Invalid;
if (x > 0 && m->IsSolid(x - 1, y, z) && GetLink(x - 1, y, z) != Invalid) {
2013-08-18 16:18:06 +09:00
l = NegativeX;
SPAssert(GetLink(x - 1, y, z) != PositiveX);
2013-08-18 16:18:06 +09:00
}
if (x < width - 1 && m->IsSolid(x + 1, y, z) && GetLink(x + 1, y, z) != Invalid) {
2013-08-18 16:18:06 +09:00
l = PositiveX;
SPAssert(GetLink(x + 1, y, z) != NegativeX);
2013-08-18 16:18:06 +09:00
}
if (y > 0 && m->IsSolid(x, y - 1, z) && GetLink(x, y - 1, z) != Invalid) {
2013-08-18 16:18:06 +09:00
l = NegativeY;
SPAssert(GetLink(x, y - 1, z) != PositiveY);
2013-08-18 16:18:06 +09:00
}
if (y < height - 1 && m->IsSolid(x, y + 1, z) && GetLink(x, y + 1, z) != Invalid) {
2013-08-18 16:18:06 +09:00
l = PositiveY;
SPAssert(GetLink(x, y + 1, z) != NegativeY);
2013-08-18 16:18:06 +09:00
}
if (z > 0 && m->IsSolid(x, y, z - 1) && GetLink(x, y, z - 1) != Invalid) {
2013-08-18 16:18:06 +09:00
l = NegativeZ;
SPAssert(GetLink(x, y, z - 1) != PositiveZ);
2013-08-18 16:18:06 +09:00
}
if (z < depth - 1 && m->IsSolid(x, y, z + 1) && GetLink(x, y, z + 1) != Invalid) {
2013-08-18 16:18:06 +09:00
l = PositiveZ;
SPAssert(GetLink(x, y, z + 1) != NegativeZ);
2013-08-18 16:18:06 +09:00
}
SetLink(x, y, z, l);
2016-11-20 19:13:00 +09:00
if (l == Invalid)
2013-08-18 16:18:06 +09:00
return;
// if there's invalid block around this block,
// rebuild tree
Deque<CellPos> queue(1024);
queue.Push(CellPos(x, y, z));
while (!queue.IsEmpty()) {
2013-08-18 16:18:06 +09:00
CellPos p = queue.Front();
queue.Shift();
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
int x = p.x, y = p.y, z = p.z;
SPAssert(m->IsSolid(x, y, z));
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
LinkType thisLink = GetLink(x, y, z);
2016-11-20 19:13:00 +09:00
if (p.x > 0 && m->IsSolid(x - 1, y, z) && GetLink(x - 1, y, z) == Invalid &&
thisLink != NegativeX) {
SetLink(x - 1, y, z, PositiveX);
queue.Push(CellPos(x - 1, y, z));
2013-08-18 16:18:06 +09:00
}
if (p.x < width - 1 && m->IsSolid(x + 1, y, z) && GetLink(x + 1, y, z) == Invalid &&
thisLink != PositiveX) {
SetLink(x + 1, y, z, NegativeX);
queue.Push(CellPos(x + 1, y, z));
2013-08-18 16:18:06 +09:00
}
if (p.y > 0 && m->IsSolid(x, y - 1, z) && GetLink(x, y - 1, z) == Invalid &&
thisLink != NegativeY) {
SetLink(x, y - 1, z, PositiveY);
queue.Push(CellPos(x, y - 1, z));
2013-08-18 16:18:06 +09:00
}
if (p.y < height - 1 && m->IsSolid(x, y + 1, z) &&
GetLink(x, y + 1, z) == Invalid && thisLink != PositiveY) {
SetLink(x, y + 1, z, NegativeY);
queue.Push(CellPos(x, y + 1, z));
2013-08-18 16:18:06 +09:00
}
if (p.z > 0 && m->IsSolid(x, y, z - 1) && GetLink(x, y, z - 1) == Invalid &&
thisLink != NegativeZ) {
SetLink(x, y, z - 1, PositiveZ);
queue.Push(CellPos(x, y, z - 1));
2013-08-18 16:18:06 +09:00
}
if (p.z < depth - 1 && m->IsSolid(x, y, z + 1) && GetLink(x, y, z + 1) == Invalid &&
thisLink != PositiveZ) {
SetLink(x, y, z + 1, NegativeZ);
queue.Push(CellPos(x, y, z + 1));
2013-08-18 16:18:06 +09:00
}
}
2016-11-20 19:13:00 +09:00
}
template <typename T> static inline bool EqualTwoCond(T a, T b, T c, bool cond) {
2016-11-20 19:13:00 +09:00
return a == b || (cond && a == c);
2013-08-18 16:18:06 +09:00
}
2016-11-20 19:13:00 +09:00
std::vector<CellPos> GameMapWrapper::RemoveBlocks(const std::vector<CellPos> &cells) {
2013-08-18 16:18:06 +09:00
SPADES_MARK_FUNCTION();
2016-11-20 19:13:00 +09:00
if (cells.empty())
2013-08-18 16:18:06 +09:00
return std::vector<CellPos>();
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
GameMap *m = map;
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
// solid, but unlinked cells
std::vector<CellPos> unlinkedCells;
Deque<CellPos> queue(1024);
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
// unlink children
for (size_t i = 0; i < cells.size(); i++) {
2013-08-18 16:18:06 +09:00
CellPos pos = cells[i];
m->Set(pos.x, pos.y, pos.z, false, 0);
2013-12-06 15:51:14 +09:00
// if(GetLink(pos.x, pos.y, pos.z) == Invalid){
// this block is already disconnected.
2016-11-20 19:13:00 +09:00
// }
if (GetLink(pos.x, pos.y, pos.z) == Marked) {
2016-11-20 19:13:00 +09:00
continue;
}
2013-08-18 16:18:06 +09:00
SPAssert(GetLink(pos.x, pos.y, pos.z) != Root);
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
SetLink(pos.x, pos.y, pos.z, Invalid);
queue.Push(pos);
2016-11-20 19:13:00 +09:00
while (!queue.IsEmpty()) {
2013-08-18 16:18:06 +09:00
pos = queue.Front();
queue.Shift();
2016-11-20 19:13:00 +09:00
if (m->IsSolid(pos.x, pos.y, pos.z))
2013-08-18 16:18:06 +09:00
unlinkedCells.push_back(pos);
// don't "continue;" when non-solid
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
int x = pos.x, y = pos.y, z = pos.z;
if (x > 0 && EqualTwoCond(GetLink(x - 1, y, z), PositiveX, Invalid,
m->IsSolid(x - 1, y, z))) {
SetLink(x - 1, y, z, Marked);
queue.Push(CellPos(x - 1, y, z));
2013-08-18 16:18:06 +09:00
}
if (x < width - 1 && EqualTwoCond(GetLink(x + 1, y, z), NegativeX, Invalid,
m->IsSolid(x + 1, y, z))) {
SetLink(x + 1, y, z, Marked);
queue.Push(CellPos(x + 1, y, z));
2013-08-18 16:18:06 +09:00
}
if (y > 0 && EqualTwoCond(GetLink(x, y - 1, z), PositiveY, Invalid,
m->IsSolid(x, y - 1, z))) {
SetLink(x, y - 1, z, Marked);
queue.Push(CellPos(x, y - 1, z));
2013-08-18 16:18:06 +09:00
}
if (y < height - 1 && EqualTwoCond(GetLink(x, y + 1, z), NegativeY, Invalid,
m->IsSolid(x, y + 1, z))) {
SetLink(x, y + 1, z, Marked);
queue.Push(CellPos(x, y + 1, z));
2013-08-18 16:18:06 +09:00
}
if (z > 0 && EqualTwoCond(GetLink(x, y, z - 1), PositiveZ, Invalid,
m->IsSolid(x, y, z - 1))) {
SetLink(x, y, z - 1, Marked);
queue.Push(CellPos(x, y, z - 1));
2013-08-18 16:18:06 +09:00
}
if (z < depth - 1 && EqualTwoCond(GetLink(x, y, z + 1), NegativeZ, Invalid,
m->IsSolid(x, y, z + 1))) {
SetLink(x, y, z + 1, Marked);
queue.Push(CellPos(x, y, z + 1));
2013-08-18 16:18:06 +09:00
}
}
}
2016-11-20 19:13:00 +09:00
// remove "visited" mark
for (size_t i = 0; i < unlinkedCells.size(); i++) {
const CellPos &pos = unlinkedCells[i];
if (GetLink(pos.x, pos.y, pos.z) == Marked)
2016-11-20 19:13:00 +09:00
SetLink(pos.x, pos.y, pos.z, Invalid);
}
2013-08-18 16:18:06 +09:00
SPAssert(queue.IsEmpty());
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
// start relinking
for (size_t i = 0; i < unlinkedCells.size(); i++) {
const CellPos &pos = unlinkedCells[i];
2013-08-18 16:18:06 +09:00
int x = pos.x, y = pos.y, z = pos.z;
if (!m->IsSolid(x, y, z)) {
2013-08-18 16:18:06 +09:00
// notice: (x,y,z) may be air, so
// don't use SPAssert()
continue;
}
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
LinkType newLink = Invalid;
if (z < depth - 1 && GetLink(x, y, z + 1) != Invalid) {
2013-08-18 16:18:06 +09:00
newLink = PositiveZ;
} else if (x > 0 && GetLink(x - 1, y, z) != Invalid) {
2013-08-18 16:18:06 +09:00
newLink = NegativeX;
} else if (x < width - 1 && GetLink(x + 1, y, z) != Invalid) {
2013-08-18 16:18:06 +09:00
newLink = PositiveX;
} else if (y > 0 && GetLink(x, y - 1, z) != Invalid) {
2013-08-18 16:18:06 +09:00
newLink = NegativeY;
} else if (y < height - 1 && GetLink(x, y + 1, z) != Invalid) {
2013-08-18 16:18:06 +09:00
newLink = PositiveY;
} else if (z > 0 && GetLink(x, y, z - 1) != Invalid) {
2013-08-18 16:18:06 +09:00
newLink = NegativeZ;
}
2016-11-20 19:13:00 +09:00
if (newLink != Invalid) {
2013-08-18 16:18:06 +09:00
SetLink(x, y, z, newLink);
queue.Push(pos);
}
}
2016-11-20 19:13:00 +09:00
while (!queue.IsEmpty()) {
2013-08-18 16:18:06 +09:00
CellPos p = queue.Front();
queue.Shift();
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
int x = p.x, y = p.y, z = p.z;
LinkType thisLink = GetLink(x, y, z);
2016-11-20 19:13:00 +09:00
if (p.x > 0 && m->IsSolid(x - 1, y, z) && GetLink(x - 1, y, z) == Invalid &&
thisLink != NegativeX) {
SetLink(x - 1, y, z, PositiveX);
queue.Push(CellPos(x - 1, y, z));
2013-08-18 16:18:06 +09:00
}
if (p.x < width - 1 && m->IsSolid(x + 1, y, z) && GetLink(x + 1, y, z) == Invalid &&
thisLink != PositiveX) {
SetLink(x + 1, y, z, NegativeX);
queue.Push(CellPos(x + 1, y, z));
2013-08-18 16:18:06 +09:00
}
if (p.y > 0 && m->IsSolid(x, y - 1, z) && GetLink(x, y - 1, z) == Invalid &&
thisLink != NegativeY) {
SetLink(x, y - 1, z, PositiveY);
queue.Push(CellPos(x, y - 1, z));
2013-08-18 16:18:06 +09:00
}
if (p.y < height - 1 && m->IsSolid(x, y + 1, z) &&
GetLink(x, y + 1, z) == Invalid && thisLink != PositiveY) {
SetLink(x, y + 1, z, NegativeY);
queue.Push(CellPos(x, y + 1, z));
2013-08-18 16:18:06 +09:00
}
if (p.z > 0 && m->IsSolid(x, y, z - 1) && GetLink(x, y, z - 1) == Invalid &&
thisLink != NegativeZ) {
SetLink(x, y, z - 1, PositiveZ);
queue.Push(CellPos(x, y, z - 1));
2013-08-18 16:18:06 +09:00
}
if (p.z < depth - 1 && m->IsSolid(x, y, z + 1) && GetLink(x, y, z + 1) == Invalid &&
thisLink != PositiveZ) {
SetLink(x, y, z + 1, NegativeZ);
queue.Push(CellPos(x, y, z + 1));
2013-08-18 16:18:06 +09:00
}
}
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
std::vector<CellPos> floatingBlocks;
floatingBlocks.reserve(unlinkedCells.size());
2016-11-20 19:13:00 +09:00
for (size_t i = 0; i < unlinkedCells.size(); i++) {
const CellPos &p = unlinkedCells[i];
if (!m->IsSolid(p.x, p.y, p.z))
2013-08-18 16:18:06 +09:00
continue;
if (GetLink(p.x, p.y, p.z) == Invalid) {
2013-08-18 16:18:06 +09:00
floatingBlocks.push_back(p);
}
}
2016-11-20 19:13:00 +09:00
2013-08-18 16:18:06 +09:00
return floatingBlocks;
}
}
}