Cliffs work and some map cleanup; ( !!currently old maps are broken in editor!! )

This commit is contained in:
Titus Tscharntke
2011-02-08 03:50:59 +00:00
parent e856d5e4f0
commit e03b029bea
12 changed files with 105 additions and 1033 deletions

View File

@@ -36,6 +36,7 @@ enum MapSurfaceType {
static const int MAX_TITLE_LENGTH = 128;
static const int MAX_AUTHOR_LENGTH = 128;
static const int MAX_DESCRIPTION_LENGTH = 256;
static const int MAX_DESCRIPTION_LENGTH_VERSION2 = 128;
static const int MIN_MAP_CELL_DIMENSION = 16;
static const int MAX_MAP_CELL_DIMENSION = 1024;
@@ -55,17 +56,33 @@ static const MapSurfaceType DEFAULT_MAP_CELL_SURFACE_TYPE = st_Grass;
static const int DEFAULT_MAP_CELL_HEIGHT_FACTOR = 3;
static const int DEFAULT_MAP_WATER_DEPTH = 4;
static const int DEFAULT_CLIFF_HEIGHT = 0;
static const int MAP_FORMAT_VERSION=2;
typedef union {
int8 description[MAX_DESCRIPTION_LENGTH];
struct {
int8 short_desc[MAX_DESCRIPTION_LENGTH_VERSION2];
int32 magic; // 0x01020304 for meta
int32 cliffLevel;
int8 meta[120];
} version2;
} uniondata;
struct MapFileHeader {
int32 version;
int32 maxFactions;
int32 width;
int32 height;
int32 altFactor;
int32 heightFactor;
int32 waterLevel;
int8 title[MAX_TITLE_LENGTH];
int8 author[MAX_AUTHOR_LENGTH];
int8 description[MAX_DESCRIPTION_LENGTH];
uniondata extension_data;
};
// ===============================================
@@ -98,8 +115,9 @@ private:
int type;
int h;
int w;
int altFactor;
int heightFactor;
int waterLevel;
int cliffLevel;
//Cell **cells;
std::vector<std::vector<Cell> > cells;
@@ -114,17 +132,19 @@ public:
MapPreview();
~MapPreview();
float getHeight(int x, int y) const;
bool isCliff(int x,int y);
MapSurfaceType getSurface(int x, int y) const;
int getObject(int x, int y) const;
int getResource(int x, int y) const;
int getStartLocationX(int index) const;
int getStartLocationY(int index) const;
int getHeightFactor() const;
int getWaterLevel() const;
int getHeightFactor() const{return heightFactor;}
int getWaterLevel() const{return waterLevel;}
int getCliffLevel() const{return cliffLevel;}
bool inside(int x, int y);
void setRefAlt(int x, int y);
void setAdvanced(int altFactor, int waterLevel);
void setAdvanced(int heightFactor, int waterLevel, int cliffLevel);
void setTitle(const string &title);
void setDesc(const string &desc);
void setAuthor(const string &author);

View File

@@ -61,7 +61,7 @@ void BaseRenderer::renderMap(MapPreview *map, int x, int y,
&& i * cellSize + x < clientW
&& clientH - cellSize - j * cellSize + y > -cellSize
&& clientH - cellSize - j * cellSize + y < clientH) {
bool isCliff=false; // needed to speedup things
//surface
alt = map->getHeight(i, j) / 20.f;
showWater = map->getWaterLevel()/ 20.f - alt;
@@ -74,7 +74,13 @@ void BaseRenderer::renderMap(MapPreview *map, int x, int y,
case st_Stone: surfColor = Vec3f(0.7f * alt, 0.7f * alt, 0.7f * alt + showWater); break;
case st_Ground: surfColor = Vec3f(0.7f * alt, 0.5f * alt, 0.3f * alt + showWater); break;
}
if(map->getCliffLevel()>0)
{// we maybe need to render cliff surfColor
if(map->isCliff(i, j)){
surfColor = Vec3f(0.95f * alt, 0.8f * alt, 0.0f * alt + showWater);
isCliff=true;
}
}
glColor3fv(surfColor.ptr());
glBegin(GL_TRIANGLE_STRIP);
@@ -99,7 +105,7 @@ void BaseRenderer::renderMap(MapPreview *map, int x, int y,
case 10: glColor3f(1.f, 0.2f, 0.8f); break;
}
if (map->getObject(i, j) != 0) {
if (map->getObject(i, j) != 0 || isCliff ) {
glPointSize(cellSize / 2.f);
glBegin(GL_POINTS);
glVertex2i(i * cellSize + cellSize / 2, clientH - j * cellSize - cellSize / 2);

View File

@@ -29,8 +29,9 @@ namespace Shared { namespace Map {
MapPreview::MapPreview() {
fileLoaded = false;
altFactor = DEFAULT_MAP_CELL_HEIGHT_FACTOR;
heightFactor = DEFAULT_MAP_CELL_HEIGHT_FACTOR;
waterLevel = DEFAULT_MAP_WATER_DEPTH;
cliffLevel = DEFAULT_CLIFF_HEIGHT;
//cells = NULL;
cells.clear();
//startLocations = NULL;
@@ -60,6 +61,24 @@ float MapPreview::getHeight(int x, int y) const {
return cells[x][y].height;
}
bool MapPreview::isCliff(int x, int y){
if(cliffLevel == 0)
return false;
for(int k= -1; k <= 1; ++k){
for(int l= -1; l <= 1; ++l){
int xToCheck= x + l;
int yToCheck= y + k;
if(xToCheck < 0 || yToCheck < 0 || xToCheck >= w || yToCheck >= h){
//ignore
}
else if(cliffLevel <= abs(getHeight(x, y) - getHeight(xToCheck, yToCheck))){
return true;
}
}
return false;
}
}
MapSurfaceType MapPreview::getSurface(int x, int y) const {
return static_cast<MapSurfaceType>(cells[x][y].surface);
}
@@ -588,17 +607,10 @@ void MapPreview::setAuthor(const string &author) {
this->author = author;
}
void MapPreview::setAdvanced(int altFactor, int waterLevel) {
this->altFactor = altFactor;
void MapPreview::setAdvanced(int heightFactor, int waterLevel, int cliffLevel) {
this->heightFactor = heightFactor;
this->waterLevel = waterLevel;
}
int MapPreview::getHeightFactor() const {
return altFactor;
}
int MapPreview::getWaterLevel() const {
return waterLevel;
this->cliffLevel = cliffLevel;
}
void MapPreview::randomizeHeights() {
@@ -651,11 +663,18 @@ void MapPreview::loadFromFile(const string &path) {
MapFileHeader header;
size_t bytes = fread(&header, sizeof(MapFileHeader), 1, f1);
altFactor = header.altFactor;
heightFactor = header.heightFactor;
waterLevel = header.waterLevel;
title = header.title;
author = header.author;
desc = header.description;
cliffLevel = 0;
if(header.version==1){
desc = header.extension_data.description;
}
else if(header.version==2){
desc = header.extension_data.version2.short_desc;
cliffLevel=header.extension_data.version2.cliffLevel;
}
//read start locations
resetFactions(header.maxFactions);
@@ -711,15 +730,18 @@ void MapPreview::saveToFile(const string &path) {
//write header
MapFileHeader header;
header.version = 1;
header.version = MAP_FORMAT_VERSION;
header.maxFactions = maxFactions;
header.width = w;
header.height = h;
header.altFactor = altFactor;
header.heightFactor = heightFactor;
header.waterLevel = waterLevel;
strncpy(header.title, title.c_str(), 128);
strncpy(header.author, author.c_str(), 128);
strncpy(header.description, desc.c_str(), 256);
strncpy(header.title, title.c_str(), MAX_TITLE_LENGTH);
strncpy(header.author, author.c_str(), MAX_AUTHOR_LENGTH);
strncpy(header.extension_data.version2.short_desc, desc.c_str(), MAX_DESCRIPTION_LENGTH_VERSION2);
header.extension_data.version2.magic= 0x01020304;
header.extension_data.version2.cliffLevel= cliffLevel;
fwrite(&header, sizeof(MapFileHeader), 1, f1);