2013-08-29 11:45:22 +09:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 yvt
|
2013-09-05 00:52:03 +09:00
|
|
|
based on code of pysnip (c) Mathias Kaerlev 2011-2012.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
This file is part of OpenSpades.
|
2016-12-03 18:23:47 +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-12-03 18:23:47 +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-12-03 18:23:47 +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-12-03 18:23:47 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
*/
|
2013-08-18 16:18:06 +09:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-12-03 18:23:47 +09:00
|
|
|
#include <cstdint>
|
2019-05-19 13:37:31 +09:00
|
|
|
#include <functional>
|
2016-12-03 18:23:47 +09:00
|
|
|
#include <list>
|
|
|
|
|
2016-12-03 18:49:07 +09:00
|
|
|
#include <Core/Debug.h>
|
|
|
|
#include <Core/Math.h>
|
2013-08-18 16:18:06 +09:00
|
|
|
|
|
|
|
#include "IGameMapListener.h"
|
2014-03-09 00:13:37 +09:00
|
|
|
#include <Core/AutoLocker.h>
|
2016-12-03 18:23:47 +09:00
|
|
|
#include <Core/Mutex.h>
|
|
|
|
#include <Core/RefCountedObject.h>
|
2013-08-18 16:18:06 +09:00
|
|
|
|
2016-12-03 18:23:47 +09:00
|
|
|
namespace spades {
|
2013-08-18 16:18:06 +09:00
|
|
|
class IStream;
|
|
|
|
namespace client {
|
2016-12-03 18:23:47 +09:00
|
|
|
class GameMap : public RefCountedObject {
|
2013-09-14 13:28:19 +09:00
|
|
|
protected:
|
|
|
|
~GameMap();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
public:
|
|
|
|
// fixed for now
|
|
|
|
enum {
|
|
|
|
DefaultWidth = 512,
|
|
|
|
DefaultHeight = 512,
|
|
|
|
DefaultDepth = 64 // should be <= 64
|
|
|
|
};
|
|
|
|
GameMap();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2019-05-19 13:37:31 +09:00
|
|
|
/**
|
|
|
|
* Construct a `GameMap` from VOXLAP5 terrain data supplied by the specified stream.
|
|
|
|
*
|
|
|
|
* @param onProgress Called whenever a new column (a set of voxels with the same X and Y
|
|
|
|
* coordinates) is loaded from the stream. The parameter indicates
|
|
|
|
* the number of columns loaded
|
|
|
|
* (up to `DefaultWidth * DefaultHeight`).
|
|
|
|
*/
|
|
|
|
static GameMap *Load(IStream *, std::function<void(int)> onProgress = {});
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-11-22 19:34:20 +09:00
|
|
|
void Save(IStream *);
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
int Width() { return DefaultWidth; }
|
|
|
|
int Height() { return DefaultHeight; }
|
|
|
|
int Depth() { return DefaultDepth; }
|
|
|
|
inline bool IsSolid(int x, int y, int z) {
|
2016-12-03 18:23:47 +09:00
|
|
|
SPAssert(x >= 0);
|
|
|
|
SPAssert(x < Width());
|
|
|
|
SPAssert(y >= 0);
|
|
|
|
SPAssert(y < Height());
|
|
|
|
SPAssert(z >= 0);
|
|
|
|
SPAssert(z < Depth());
|
2013-08-18 16:18:06 +09:00
|
|
|
return ((solidMap[x][y] >> (uint64_t)z) & 1ULL) != 0;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
/** @return 0xHHBBGGRR where HH is health (up to 100) */
|
2016-12-03 18:23:47 +09:00
|
|
|
inline uint32_t GetColor(int x, int y, int z) {
|
|
|
|
SPAssert(x >= 0);
|
|
|
|
SPAssert(x < Width());
|
|
|
|
SPAssert(y >= 0);
|
|
|
|
SPAssert(y < Height());
|
|
|
|
SPAssert(z >= 0);
|
|
|
|
SPAssert(z < Depth());
|
2013-08-18 16:18:06 +09:00
|
|
|
return colorMap[x][y][z];
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-12-22 04:55:30 +09:00
|
|
|
inline uint64_t GetSolidMapWrapped(int x, int y) {
|
|
|
|
return solidMap[x & (Width() - 1)][y & (Height() - 1)];
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
inline bool IsSolidWrapped(int x, int y, int z) {
|
|
|
|
if (z < 0)
|
2013-08-18 16:18:06 +09:00
|
|
|
return false;
|
2016-12-03 18:23:47 +09:00
|
|
|
if (z >= Depth())
|
2013-08-18 16:18:06 +09:00
|
|
|
return true;
|
2016-12-03 18:23:47 +09:00
|
|
|
return ((solidMap[x & (Width() - 1)][y & (Height() - 1)] >> (uint64_t)z) & 1ULL) !=
|
|
|
|
0;
|
2013-08-18 16:18:06 +09:00
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
inline uint32_t GetColorWrapped(int x, int y, int z) {
|
|
|
|
return colorMap[x & (Width() - 1)][y & (Height() - 1)][z & (Depth() - 1)];
|
2013-08-18 16:18:06 +09:00
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
inline void Set(int x, int y, int z, bool solid, uint32_t color, bool unsafe = false) {
|
|
|
|
SPAssert(x >= 0);
|
|
|
|
SPAssert(x < Width());
|
|
|
|
SPAssert(y >= 0);
|
|
|
|
SPAssert(y < Height());
|
|
|
|
SPAssert(z >= 0);
|
|
|
|
SPAssert(z < Depth());
|
2013-08-18 16:18:06 +09:00
|
|
|
uint64_t mask = 1ULL << z;
|
|
|
|
uint64_t value = solidMap[x][y];
|
|
|
|
bool changed = false;
|
2016-12-03 18:23:47 +09:00
|
|
|
if ((value & mask) != (solid ? mask : 0ULL)) {
|
2013-08-18 16:18:06 +09:00
|
|
|
changed = true;
|
|
|
|
value &= ~mask;
|
2016-12-03 18:23:47 +09:00
|
|
|
if (solid)
|
2013-08-18 16:18:06 +09:00
|
|
|
value |= mask;
|
|
|
|
solidMap[x][y] = value;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
if (solid) {
|
|
|
|
if (color != colorMap[x][y][z]) {
|
2013-08-18 16:18:06 +09:00
|
|
|
changed = true;
|
|
|
|
colorMap[x][y][z] = color;
|
|
|
|
}
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
if (!unsafe) {
|
|
|
|
if (changed) {
|
2014-03-09 00:13:37 +09:00
|
|
|
{
|
|
|
|
AutoLocker guard(&listenersMutex);
|
2016-12-03 18:23:47 +09:00
|
|
|
for (auto *l : listeners) {
|
2014-03-09 00:13:37 +09:00
|
|
|
l->GameMapChanged(x, y, z, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-18 16:18:06 +09:00
|
|
|
}
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2014-03-09 00:13:37 +09:00
|
|
|
void AddListener(IGameMapListener *);
|
|
|
|
void RemoveListener(IGameMapListener *);
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
bool ClipBox(int x, int y, int z);
|
|
|
|
bool ClipWorld(int x, int y, int z);
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
bool ClipBox(float x, float y, float z);
|
|
|
|
bool ClipWorld(float x, float y, float z);
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
// vanila compat
|
2016-12-03 18:23:47 +09:00
|
|
|
bool CastRay(Vector3 v0, Vector3 v1, float length, IntVector3 &vOut);
|
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
// accurate and slow ray casting
|
|
|
|
struct RayCastResult {
|
|
|
|
bool hit;
|
|
|
|
bool startSolid;
|
|
|
|
Vector3 hitPos;
|
|
|
|
IntVector3 hitBlock;
|
|
|
|
IntVector3 normal;
|
|
|
|
};
|
2016-12-03 18:23:47 +09:00
|
|
|
RayCastResult CastRay2(Vector3 v0, Vector3 dir, int maxSteps);
|
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
private:
|
|
|
|
uint64_t solidMap[DefaultWidth][DefaultHeight];
|
|
|
|
uint32_t colorMap[DefaultWidth][DefaultHeight][DefaultDepth];
|
2014-03-09 00:13:37 +09:00
|
|
|
std::list<IGameMapListener *> listeners;
|
|
|
|
Mutex listenersMutex;
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-11-22 19:34:20 +09:00
|
|
|
bool IsSurface(int x, int y, int z);
|
2013-08-18 16:18:06 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|