Loads a portion of a map's terrain.
136 char mapFileName[255];
137 sprintf(mapFileName,
"maps/%04u_%02u_%02u.map", mapID, tileY, tileX);
139 FILE* mapFile = fopen(mapFileName,
"rb");
148 printf(
"%s is the wrong version, please extract new .map files\n", mapFileName);
155 bool haveTerrain =
false;
156 bool haveLiquid =
false;
164 if (!haveTerrain && !haveLiquid)
172 memset(holes, 0,
sizeof(holes));
173 uint8 liquid_type[16][16];
174 memset(liquid_type, 0,
sizeof(liquid_type));
181 float heightMultiplier;
191 count += fread(v8,
sizeof(
uint8), V8_SIZE_SQ, mapFile);
192 if (count != expected)
193 printf(
"TerrainBuilder::loadMap: Failed to read some data expected %d, read %d\n", expected, count);
198 V9[i] = (
float)v9[i]*heightMultiplier + hheader.
gridHeight;
201 V8[i] = (
float)v8[i]*heightMultiplier + hheader.
gridHeight;
208 count += fread(v9,
sizeof(
uint16), V9_SIZE_SQ, mapFile);
209 count += fread(v8,
sizeof(
uint16), V8_SIZE_SQ, mapFile);
210 if (count != expected)
211 printf(
"TerrainBuilder::loadMap: Failed to read some data expected %d, read %d\n", expected, count);
216 V9[i] = (
float)v9[i]*heightMultiplier + hheader.
gridHeight;
219 V8[i] = (
float)v8[i]*heightMultiplier + hheader.
gridHeight;
224 count += fread(V9,
sizeof(
float), V9_SIZE_SQ, mapFile);
225 count += fread(V8,
sizeof(
float), V8_SIZE_SQ, mapFile);
226 if (count != expected)
227 printf(
"TerrainBuilder::loadMap: Failed to read some data expected %d, read %d\n", expected, count);
235 if (fread(holes, fheader.
holesSize, 1, mapFile) != 1)
236 printf(
"TerrainBuilder::loadMap: Failed to read some data expected 1, read 0\n");
239 int count = meshData.solidVerts.size() / 3;
240 float xoffset = (float(tileX)-32)*
GRID_SIZE;
241 float yoffset = (float(tileY)-32)*
GRID_SIZE;
248 meshData.solidVerts.append(coord[0]);
249 meshData.solidVerts.append(coord[2]);
250 meshData.solidVerts.append(coord[1]);
256 meshData.solidVerts.append(coord[0]);
257 meshData.solidVerts.append(coord[2]);
258 meshData.solidVerts.append(coord[1]);
261 int indices[] = { 0, 0, 0 };
262 int loopStart = 0, loopEnd = 0, loopInc = 0;
264 for (
int i = loopStart; i < loopEnd; i+=loopInc)
268 ttriangles.
append(indices[2] + count);
269 ttriangles.
append(indices[1] + count);
270 ttriangles.
append(indices[0] + count);
280 printf(
"TerrainBuilder::loadMap: Failed to read some data expected 1, read 0\n");
283 float* liquid_map =
NULL;
286 if (fread(liquid_type,
sizeof(liquid_type), 1, mapFile) != 1)
287 printf(
"TerrainBuilder::loadMap: Failed to read some data expected 1, read 0\n");
292 liquid_map =
new float [toRead];
293 if (fread(liquid_map,
sizeof(
float), toRead, mapFile) != toRead)
294 printf(
"TerrainBuilder::loadMap: Failed to read some data expected 1, read 0\n");
299 int count = meshData.liquidVerts.size() / 3;
300 float xoffset = (float(tileX)-32)*
GRID_SIZE;
301 float yoffset = (float(tileY)-32)*
GRID_SIZE;
315 if (row < lheader.offsetY || row >= lheader.
offsetY + lheader.
height ||
316 col < lheader.offsetX || col >= lheader.
offsetX + lheader.
width)
324 meshData.liquidVerts.append(coord[0]);
325 meshData.liquidVerts.append(coord[2]);
326 meshData.liquidVerts.append(coord[1]);
336 meshData.liquidVerts.append((xoffset+col*GRID_PART_SIZE)*-1, lheader.
liquidLevel, (yoffset+row*GRID_PART_SIZE)*-1);
340 delete [] liquid_map;
342 int indices[] = { 0, 0, 0 };
343 int loopStart = 0, loopEnd = 0, loopInc = 0, triInc =
BOTTOM-
TOP;
347 for (
int i = loopStart; i < loopEnd; i+=loopInc)
348 for (
int j = TOP; j <=
BOTTOM; j+= triInc)
351 ltriangles.
append(indices[2] + count);
352 ltriangles.
append(indices[1] + count);
353 ltriangles.
append(indices[0] + count);
362 int loopStart = 0, loopEnd = 0, loopInc = 0, tTriCount = 4;
363 bool useTerrain, useLiquid;
365 float* lverts = meshData.liquidVerts.getCArray();
368 float* tverts = meshData.solidVerts.getCArray();
371 if ((ltriangles.
size() + ttriangles.
size()) == 0)
376 float* lverts_copy =
NULL;
377 if (meshData.liquidVerts.size())
379 lverts_copy =
new float[meshData.liquidVerts.size()];
380 memcpy(lverts_copy, lverts,
sizeof(
float)*meshData.liquidVerts.size());
384 for (
int i = loopStart; i < loopEnd; i+=loopInc)
386 for (
int j = 0; j < 2; ++j)
395 if (!meshData.liquidVerts.size() || !ltriangles.
size())
425 if (!ttriangles.
size())
432 float quadHeight = 0;
434 for(
uint32 idx = 0; idx < 3; idx++)
436 float h = lverts_copy[ltris[idx]*3 + 1];
445 if (validCount > 0 && validCount < 3)
447 quadHeight /= validCount;
448 for(
uint32 idx = 0; idx < 3; idx++)
450 float h = lverts[ltris[idx]*3 + 1];
452 lverts[ltris[idx]*3 + 1] = quadHeight;
462 if (useTerrain && fheader.
holesSize != 0)
463 useTerrain = !
isHole(i, holes);
466 if (useTerrain && useLiquid)
472 float h = lverts[ltris[
x]*3 + 1];
484 float h = tverts[ttris[
x]*3 + 1];
493 if (minLLevel > maxTLevel)
497 if (minTLevel > maxLLevel)
504 meshData.liquidType.append(liquidType);
505 for (
int k = 0; k < 3; ++k)
506 meshData.liquidTris.append(ltris[k]);
510 for (
int k = 0; k < 3*tTriCount/2; ++k)
511 meshData.solidTris.append(ttris[k]);
515 ttris += 3*tTriCount/2;
520 delete [] lverts_copy;
522 return meshData.solidTris.size() || meshData.liquidTris.size();
Definition: TerrainBuilder.h:43
Definition: MapDefines.h:26
#define MAP_LIQUID_TYPE_OCEAN
Definition: TerrainBuilder.cpp:74
void getHeightCoord(int index, Grid grid, float xOffset, float yOffset, float *coord, float *v)
Get the vector coordinate for a specific position.
Definition: TerrainBuilder.cpp:526
Definition: MapDefines.h:28
#define MAP_LIQUID_TYPE_NO_WATER
Definition: TerrainBuilder.cpp:72
Definition: TerrainBuilder.h:42
#define MAP_HEIGHT_AS_INT16
Definition: TerrainBuilder.cpp:46
static const int V9_SIZE_SQ
Definition: TerrainBuilder.h:47
bool isHole(int square, uint8 const holes[16][16][8])
Determines if the specific position's triangles should be rendered.
Definition: TerrainBuilder.cpp:603
Definition: MapDefines.h:27
T * getCArray()
Definition: Array.h:256
arena_t NULL
Definition: jemalloc_internal.h:624
#define MAP_HEIGHT_NO_HEIGHT
Definition: TerrainBuilder.cpp:45
#define MAP_LIQUID_TYPE_MAGMA
Definition: TerrainBuilder.cpp:75
bool m_skipLiquid
Controls whether liquids are loaded.
Definition: TerrainBuilder.h:105
void getLiquidCoord(int index, int index2, float xOffset, float yOffset, float *coord, float *v)
Get the liquid vector coordinate for a specific position.
Definition: TerrainBuilder.cpp:593
uint8 getLiquidType(int square, const uint8 liquid_type[16][16])
Get the liquid type for a specific position.
Definition: TerrainBuilder.cpp:616
static const float INVALID_MAP_LIQ_HEIGHT_MAX
Definition: TerrainBuilder.h:55
std::string sprintf(CStringRef format, ArgList args)
Definition: format.h:3096
#define MAP_HEIGHT_AS_INT8
Definition: TerrainBuilder.cpp:47
static const int V9_SIZE
Definition: TerrainBuilder.h:46
uint32_t uint32
Definition: Define.h:150
uint16_t uint16
Definition: Define.h:151
#define MAP_LIQUID_NO_TYPE
Definition: TerrainBuilder.cpp:57
int size() const
Definition: Array.h:430
Definition: TerrainBuilder.h:33
#define SEEK_SET
Definition: zconf.h:475
void getLoopVars(Spot portion, int &loopStart, int &loopEnd, int &loopInc)
Sets loop variables for selecting only certain parts of a map's terrain.
Definition: TerrainBuilder.cpp:89
Spot
Definition: TerrainBuilder.h:31
static const float INVALID_MAP_LIQ_HEIGHT
Definition: TerrainBuilder.h:54
#define MAP_LIQUID_TYPE_DARK_WATER
Definition: TerrainBuilder.cpp:77
uint8_t uint8
Definition: Define.h:152
Definition: TerrainBuilder.h:36
static const int V8_SIZE_SQ
Definition: TerrainBuilder.h:49
G3D::int16 x
Definition: Vector2int16.h:37
static const float GRID_PART_SIZE
Definition: TerrainBuilder.h:51
#define MAP_LIQUID_TYPE_SLIME
Definition: TerrainBuilder.cpp:76
void append(const T &value)
Definition: Array.h:583
#define MAP_LIQUID_TYPE_WATER
Definition: TerrainBuilder.cpp:73
static const float GRID_SIZE
Definition: TerrainBuilder.h:50
#define MAP_LIQUID_NO_HEIGHT
Definition: TerrainBuilder.cpp:58
char const * MAP_VERSION_MAGIC
Definition: TerrainBuilder.cpp:83
void getHeightTriangle(int square, Spot triangle, int *indices, bool liquid=false)
Get the triangle's vector indices for a specific position.
Definition: TerrainBuilder.cpp:546
void printf(BasicWriter< Char > &w, BasicCStringRef< Char > format, ArgList args)
Definition: format.h:3083