1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
#include "StdAfx.h"
// ReadMap.cpp: implementation of the CReadMap class.
//
//////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <string>
#include "mmgr.h"
#include "ReadMap.h"
#include "Rendering/Textures/Bitmap.h"
#include "Ground.h"
#include "ConfigHandler.h"
#include "MapDamage.h"
#include "MapInfo.h"
#include "MetalMap.h"
#include "Sim/Path/PathManager.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/Unit.h"
#include "SM3/Sm3Map.h"
#include "SMF/SmfReadMap.h"
#include "FileSystem/FileHandler.h"
#include "FileSystem/ArchiveScanner.h"
#include "LoadSaveInterface.h"
#include "LogOutput.h"
#include "Platform/errorhandler.h"
#include "Exceptions.h"
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CReadMap* readmap=0;
CR_BIND_INTERFACE(CReadMap)
CR_REG_METADATA(CReadMap, (
CR_SERIALIZER(Serialize)
));
CReadMap* CReadMap::LoadMap (const std::string& mapname)
{
if (mapname.length() < 3)
throw std::runtime_error("CReadMap::LoadMap(): mapname '" + mapname + "' too short");
string extension = mapname.substr(mapname.length()-3);
CReadMap* rm = 0;
if (extension == "sm3") {
rm = new CSm3ReadMap;
((CSm3ReadMap*)rm)->Initialize (mapname.c_str());
} else
rm = new CSmfReadMap(mapname);
if (!rm)
return 0;
/* Read metal map */
MapBitmapInfo mbi;
unsigned char* metalmapPtr = rm->GetInfoMap("metal", &mbi);
if (metalmapPtr && mbi.width == (rm->width >> 1) && mbi.height == (rm->height >> 1)) {
int size = mbi.width * mbi.height;
unsigned char* mtlMap = new unsigned char[size];
memcpy(mtlMap, metalmapPtr, size);
rm->metalMap = new CMetalMap(mtlMap, mbi.width, mbi.height, mapInfo->map.maxMetal);
}
if (metalmapPtr)
rm->FreeInfoMap("metal", metalmapPtr);
if (!rm->metalMap) {
unsigned char* mtlMap = new unsigned char[rm->width * rm->height / 4];
memset(mtlMap, 0, rm->width * rm->height / 4);
rm->metalMap = new CMetalMap(mtlMap, rm->width / 2,rm->height / 2, 1.0f);
}
/* Read type map */
MapBitmapInfo tbi;
unsigned char* typemapPtr = rm->GetInfoMap("type", &tbi);
if (typemapPtr && tbi.width == (rm->width >> 1) && tbi.height == (rm->height >> 1)) {
assert(gs->hmapx == tbi.width && gs->hmapy == tbi.height);
rm->typemap = new unsigned char[tbi.width * tbi.height];
memcpy(rm->typemap, typemapPtr, tbi.width * tbi.height);
} else
throw content_error("Bad/no terrain type map.");
if (typemapPtr)
rm->FreeInfoMap("type", typemapPtr);
return rm;
}
void CReadMap::Serialize(creg::ISerializer& s)
{
// remove the const
float* hm = (float*) GetHeightmap();
s.Serialize(hm, 4 * (gs->mapx + 1) * (gs->mapy + 1));
if (!s.IsWriting())
mapDamage->RecalcArea(2, gs->mapx - 3, 2, gs->mapy - 3);
}
CReadMap::CReadMap() :
orgheightmap(NULL),
centerheightmap(NULL),
slopemap(NULL),
facenormals(NULL),
centernormals(NULL),
typemap(NULL),
metalMap(NULL)
{
memset(mipHeightmap, 0, sizeof(mipHeightmap));
}
CReadMap::~CReadMap()
{
delete metalMap;
delete[] typemap;
delete[] slopemap;
delete[] facenormals;
delete[] centernormals;
delete[] centerheightmap;
for(int i=1; i<numHeightMipMaps; i++) //don't delete first pointer since it points to centerheightmap
delete[] mipHeightmap[i];
delete[] orgheightmap;
}
void CReadMap::Initialize()
{
PrintLoadMsg("Loading Map");
orgheightmap=new float[(gs->mapx+1)*(gs->mapy+1)];
facenormals=new float3[gs->mapx*gs->mapy*2];
centernormals=new float3[gs->mapx*gs->mapy];
centerheightmap=new float[gs->mapx*gs->mapy];
mipHeightmap[0] = centerheightmap;
for(int i=1; i<numHeightMipMaps; i++)
mipHeightmap[i] = new float[(gs->mapx>>i)*(gs->mapy>>i)];
slopemap = new float[gs->hmapx * gs->hmapy];
CalcHeightmapChecksum();
HeightmapUpdated(0, 0, gs->mapx, gs->mapy);
}
void CReadMap::CalcHeightmapChecksum()
{
const float* heightmap = GetHeightmap();
minheight = +123456.0f;
maxheight = -123456.0f;
mapChecksum = 0;
for (int i = 0; i < ((gs->mapx + 1) * (gs->mapy + 1)); ++i) {
orgheightmap[i] = heightmap[i];
if (heightmap[i] < minheight) { minheight = heightmap[i]; }
if (heightmap[i] > maxheight) { maxheight = heightmap[i]; }
mapChecksum += (unsigned int) (heightmap[i] * 100);
mapChecksum ^= *(unsigned int*) &heightmap[i];
}
currMinHeight = minheight;
currMaxHeight = maxheight;
}
void CReadMap::UpdateHeightmapSynced(int x1, int y1, int x2, int y2)
{
const float* heightmap = GetHeightmap();
x1 = std::max( 0, x1 - 1);
y1 = std::max( 0, y1 - 1);
x2 = std::min(gs->mapx - 1, x2 + 1);
y2 = std::min(gs->mapy - 1, y2 + 1);
for (int y = y1; y <= y2; y++) {
for (int x = x1; x <= x2; x++) {
float height = heightmap[(y) * (gs->mapx + 1) + x];
height += heightmap[(y ) * (gs->mapx + 1) + x + 1];
height += heightmap[(y + 1) * (gs->mapx + 1) + x ];
height += heightmap[(y + 1) * (gs->mapx + 1) + x + 1];
centerheightmap[y * gs->mapx + x] = height * 0.25f;
}
}
for (int i = 0; i < numHeightMipMaps - 1; i++) {
int hmapx = gs->mapx >> i;
for (int y = ((y1 >> i) & (~1)); y < (y2 >> i); y += 2) {
for (int x = ((x1 >> i) & (~1)); x < (x2 >> i); x += 2) {
float height = mipHeightmap[i][(x) + (y) * hmapx];
height += mipHeightmap[i][(x ) + (y + 1) * hmapx];
height += mipHeightmap[i][(x + 1) + (y ) * hmapx];
height += mipHeightmap[i][(x + 1) + (y + 1) * hmapx];
mipHeightmap[i + 1][(x / 2) + (y / 2) * hmapx / 2] = height * 0.25f;
}
}
}
const int decy = std::max( 0, y1 - 1);
const int incy = std::min(gs->mapy - 1, y2 + 1);
const int decx = std::max( 0, x1 - 1);
const int incx = std::min(gs->mapx - 1, x2 + 1);
//! create the surface normals
for (int y = decy; y <= incy; y++) {
for (int x = decx; x <= incx; x++) {
int idx0 = (y ) * (gs->mapx + 1) + x;
int idx1 = (y + 1) * (gs->mapx + 1) + x;
float3 e1(-SQUARE_SIZE, heightmap[idx0] - heightmap[idx0 + 1], 0);
float3 e2( 0, heightmap[idx0] - heightmap[idx1 ], -SQUARE_SIZE);
const float3 n1 = e2.cross(e1).Normalize();
//! triangle topright
facenormals[(y * gs->mapx + x) * 2] = n1;
e1 = float3( SQUARE_SIZE, heightmap[idx1 + 1] - heightmap[idx1 ], 0);
e2 = float3( 0, heightmap[idx1 + 1] - heightmap[idx0 + 1], SQUARE_SIZE);
const float3 n2 = e2.cross(e1).Normalize();
//! triangle bottomleft
facenormals[(y * gs->mapx + x) * 2 + 1] = n2;
//! face normal
centernormals[y * gs->mapx + x] = (n1 + n2).Normalize();
}
}
for (int y = std::max(0, (y1/2)-1); y <= std::min(gs->hmapy - 1, (y2/2)+1); y++) {
for (int x = std::max(0, (x1/2)-1); x <= std::min(gs->hmapx - 1, (x2/2)+1); x++) {
const int idx0 = (y*2 ) * (gs->mapx) + x*2;
const int idx1 = (y*2 + 1) * (gs->mapx) + x*2;
float avgslope = 0;
avgslope += facenormals[(idx0 ) * 2 ].y;
avgslope += facenormals[(idx0 ) * 2 + 1].y;
avgslope += facenormals[(idx0 + 1) * 2 ].y;
avgslope += facenormals[(idx0 + 1) * 2 + 1].y;
avgslope += facenormals[(idx1 ) * 2 ].y;
avgslope += facenormals[(idx1 ) * 2 + 1].y;
avgslope += facenormals[(idx1 + 1) * 2 ].y;
avgslope += facenormals[(idx1 + 1) * 2 + 1].y;
avgslope /= 8;
float maxslope = facenormals[(idx0 ) * 2 ].y;
maxslope = std::min(maxslope, facenormals[(idx0 ) * 2 + 1].y);
maxslope = std::min(maxslope, facenormals[(idx0 + 1) * 2 ].y);
maxslope = std::min(maxslope, facenormals[(idx0 + 1) * 2 + 1].y);
maxslope = std::min(maxslope, facenormals[(idx1 ) * 2 ].y);
maxslope = std::min(maxslope, facenormals[(idx1 ) * 2 + 1].y);
maxslope = std::min(maxslope, facenormals[(idx1 + 1) * 2 ].y);
maxslope = std::min(maxslope, facenormals[(idx1 + 1) * 2 + 1].y);
//! smooth it a bit, so small holes don't block huge tanks
float lerp = maxslope / avgslope;
float slope = maxslope * (1.0f - lerp) + avgslope * lerp;
slopemap[y * gs->hmapx + x] = 1.0f - slope;
}
}
}
void CReadMap::UpdateDraw() {
GML_STDMUTEX_LOCK(map); // UpdateDraw
for (std::vector<HeightmapUpdate>::iterator i = heightmapUpdates.begin(); i != heightmapUpdates.end(); ++i)
UpdateHeightmapUnsynced(i->x1, i->y1, i->x2, i->y2);
heightmapUpdates.clear();
}
void CReadMap::HeightmapUpdated(const int& x1, const int& y1, const int& x2, const int& y2) {
GML_STDMUTEX_LOCK(map); // HeightmapUpdated
// only update the heightmap if the affected area has a size > 0
if ((x1 < x2) && (y1 < y2)) {
//! synced
UpdateHeightmapSynced(x1, y1, x2, y2);
//! unsynced
heightmapUpdates.push_back(HeightmapUpdate(x1, x2, y1, y2));
}
}
CReadMap::IQuadDrawer::~IQuadDrawer() {
}
|