2015-09-23 22:03:31 +02:00
|
|
|
/*
|
|
|
|
|
Copyright (C) 2015 erik
|
|
|
|
|
|
|
|
|
|
This program 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 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-09-26 20:37:31 +02:00
|
|
|
#include <rulesets/mind/SharedTerrain.h>
|
2015-09-23 22:03:31 +02:00
|
|
|
|
|
|
|
|
#include <Mercator/Segment.h>
|
|
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2016-03-31 21:08:12 +02:00
|
|
|
SharedTerrain::SharedTerrain() :
|
|
|
|
|
m_terrain(new Mercator::Terrain())
|
2015-09-23 22:03:31 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SharedTerrain::~SharedTerrain()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 21:08:12 +02:00
|
|
|
std::vector<SharedTerrain::BasePointDefinition> SharedTerrain::setBasePoints(const std::vector<BasePointDefinition>& basepoints)
|
2015-09-23 22:03:31 +02:00
|
|
|
{
|
2016-03-31 21:08:12 +02:00
|
|
|
std::vector<BasePointDefinition> changedPoints;
|
2015-09-23 22:03:31 +02:00
|
|
|
for (auto& basepointDef : basepoints) {
|
|
|
|
|
Mercator::BasePoint existingPoint;
|
2016-03-31 21:08:12 +02:00
|
|
|
if (!m_terrain->getBasePoint(basepointDef.x, basepointDef.y, existingPoint)
|
|
|
|
|
|| (existingPoint.height() != basepointDef.basePoint.height() || existingPoint.falloff() != basepointDef.basePoint.falloff()
|
|
|
|
|
|| existingPoint.roughness() != basepointDef.basePoint.roughness())) {
|
2015-09-23 22:03:31 +02:00
|
|
|
m_terrain->setBasePoint(basepointDef.x, basepointDef.y, basepointDef.basePoint);
|
2016-03-31 21:08:12 +02:00
|
|
|
changedPoints.push_back(basepointDef);
|
2015-09-23 22:03:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-03-31 21:08:12 +02:00
|
|
|
return std::move(changedPoints);
|
2015-09-23 22:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SharedTerrain::blitHeights(int xMin, int xMax, int yMin, int yMax, std::vector<float>& heights) const
|
|
|
|
|
{
|
|
|
|
|
int segmentResolution = m_terrain->getResolution();
|
|
|
|
|
int xSize = xMax - xMin;
|
|
|
|
|
|
|
|
|
|
int segmentXMin = std::lround(floor(xMin / (double)segmentResolution));
|
|
|
|
|
int segmentXMax = std::lround(floor(xMax / (double)segmentResolution));
|
|
|
|
|
int segmentYMin = std::lround(floor(yMin / (double)segmentResolution));
|
|
|
|
|
int segmentYMax = std::lround(floor(yMax / (double)segmentResolution));
|
|
|
|
|
|
|
|
|
|
for (int segmentX = segmentXMin; segmentX <= segmentXMax; ++segmentX) {
|
|
|
|
|
for (int segmentY = segmentYMin; segmentY <= segmentYMax; ++segmentY) {
|
|
|
|
|
|
|
|
|
|
int segmentXStart = segmentX * segmentResolution;
|
|
|
|
|
int segmentYStart = segmentY * segmentResolution;
|
|
|
|
|
int dataXOffset = segmentXStart - xMin;
|
|
|
|
|
int dataYOffset = segmentYStart - yMin;
|
|
|
|
|
|
|
|
|
|
int xStart = std::max(xMin - segmentXStart, 0);
|
|
|
|
|
int yStart = std::max(yMin - segmentYStart, 0);
|
|
|
|
|
int xEnd = std::min<int>(xMax - segmentXStart, segmentResolution);
|
|
|
|
|
int yEnd = std::min<int>(yMax - segmentYStart, segmentResolution);
|
|
|
|
|
|
2016-03-31 21:08:12 +02:00
|
|
|
Mercator::Segment* segment = m_terrain->getSegment(segmentX, segmentY);
|
|
|
|
|
if (segment) {
|
|
|
|
|
if (!segment->isValid()) {
|
|
|
|
|
segment->populate();
|
2015-09-23 22:03:31 +02:00
|
|
|
}
|
2016-03-31 21:08:12 +02:00
|
|
|
|
|
|
|
|
for (int x = xStart; x < xEnd; ++x) {
|
|
|
|
|
for (int y = yStart; y < yEnd; ++y) {
|
|
|
|
|
heights[((dataYOffset + y) * xSize) + (dataXOffset + x)] = segment->get(x, y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//No valid segment found; fill with default value of -10.
|
|
|
|
|
for (int x = xStart; x < xEnd; ++x) {
|
|
|
|
|
for (int y = yStart; y < yEnd; ++y) {
|
|
|
|
|
heights[((dataYOffset + y) * xSize) + (dataXOffset + x)] = -10;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 22:03:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 21:08:12 +02:00
|
|
|
const Mercator::Terrain& SharedTerrain::getTerrain() const
|
|
|
|
|
{
|
|
|
|
|
return *m_terrain.get();
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 22:03:31 +02:00
|
|
|
|