#include "map.h" #include #include using namespace Shared::Util; using namespace std; namespace Glest{ namespace MapEditor{ // =============================================== // class Map // =============================================== // ================== PUBLIC ===================== Map::Map(){ altFactor= 3; waterLevel= 4; cells= NULL; startLocations= NULL; reset(64, 64, 10.f, 1); resetPlayers(4); title=""; desc=""; author=""; refAlt= 10; } Map::~Map(){ delete [] startLocations; for(int i=0; i (sqrt(dx * dx + dy * dy)); } void Map::changeHeight(int x, int y, int height, int radius){ for (int i=x-radius+1; idist){ int oldAlt= static_cast(cells[i][j].height); int altInc= height * (radius-dist-1)/radius; if(height>0){ altInc++; } if(height<0){ altInc--; } int newAlt= refAlt + altInc; if((height>0 && newAlt>oldAlt) || (height<0 && newAlt=0 && newAlt<=20){ cells[i][j].height= static_cast(newAlt); } } } } } } } void Map::setRefAlt(int x, int y){ if(inside(x, y)){ refAlt= static_cast(cells[x][y].height); } } void Map::flipX(){ Cell **oldCells= cells; cells= new Cell*[w]; for (int i=0; i=dist){ cells[i][j].surface= surface; } } } } } void Map::changeObject(int x, int y, int object, int radius){ int i, j; int dist; for (i=x-radius+1; i=dist){ cells[i][j].object= object; cells[i][j].resource= 0; } } } } } void Map::changeResource(int x, int y, int resource, int radius){ int i, j; int dist; for (i=x-radius+1; i=dist){ cells[i][j].resource= resource; cells[i][j].object= 0; } } } } } void Map::changeStartLocation(int x, int y, int player){ if ((player-1)=0 && x=0 && y1024 || h>1024){ throw runtime_error("Size of map can be at most 1024x1024"); return; } if (alt<0 || alt>20){ throw runtime_error("Height must be in the range 0-20"); return; } if (surf<1 || surf>5){ throw runtime_error("Surface must be in the range 1-5"); return; } if (cells!=NULL){ for(int i=0; iw; i++){ delete cells[i]; } delete cells; } this->w= w; this->h= h; this->maxPlayers= maxPlayers; cells= new Cell*[w]; for (int i=0; i1024 || h>1024){ throw runtime_error("Size of map can be at most 1024x1024"); return; } if (alt<0 || alt>20){ throw runtime_error("Height must be in the range 0-20"); return; } if (surf<1 || surf>5){ throw runtime_error("Surface must be in the range 1-5"); return; } int oldW= this->w; int oldH= this->h; this->w= w; this->h= h; this->maxPlayers= maxPlayers; //create new cells Cell **oldCells= cells; cells= new Cell*[w]; for (int i=0; i4){ throw runtime_error("Max Players must be in the range 1-4"); return; } if (startLocations!=NULL) delete startLocations; this->maxPlayers= maxPlayers; startLocations= new StartLocation[maxPlayers]; for (int i=0; ititle= title; } void Map::setDesc(const string &desc){ this->desc= desc; } void Map::setAuthor(const string &author){ this->author= author; } void Map::setAdvanced(int altFactor, int waterLevel){ this->altFactor= altFactor; this->waterLevel= waterLevel; } int Map::getHeightFactor() const{ return altFactor; } int Map::getWaterLevel() const{ return waterLevel; } void Map::randomizeHeights(){ resetHeights(random.randRange(8, 10)); sinRandomize(0); decalRandomize(4); sinRandomize(1); } void Map::randomize(){ randomizeHeights(); int slPlaceFactorX= random.randRange(0, 1); int slPlaceFactorY= random.randRange(0, 1)*2; for(int i=0; i(w*slNoiseFactor * ((i+slPlaceFactorX)%2) + w*(1.f-slNoiseFactor)/2.f); sl.y= static_cast(h*slNoiseFactor * (((i+slPlaceFactorY)/2) % 2)+ h*(1.f-slNoiseFactor)/2.f); startLocations[i]= sl; } } void Map::switchSurfaces(int surf1, int surf2){ if(surf1>0 && surf1<=5 && surf2>0 && surf2<=5){ for(int i=0; i(height); } } } void Map::sinRandomize(int strenght){ float sinH1= random.randRange(5.f, 40.f); float sinH2= random.randRange(5.f, 40.f); float sinV1= random.randRange(5.f, 40.f); float sinV2= random.randRange(5.f, 40.f); float ah= static_cast(10 + random.randRange(-2, 2)); float bh= static_cast((maxHeight-minHeight)/random.randRange(2, 3)); float av= static_cast(10 + random.randRange(-2, 2)); float bv= static_cast((maxHeight-minHeight)/random.randRange(2, 3)); for(int i=0; i(i)/w; float normV= static_cast(j)/h; float sh= (sin(normH*sinH1) + sin(normH*sinH2))/2.f; float sv= (sin(normV*sinV1) + sin(normV*sinV2))/2.f; float newHeight= (ah+bh*sh+av+bv*sv)/2.f; applyNewHeight(newHeight, i, j, strenght); } } } void Map::decalRandomize(int strenght){ //first row int lastHeight= 10; for(int i=0; i(lastHeight), i, 0, strenght); } //other rows for(int j=1; j(cells[0][j-1].height+random.randRange(-1, 1)); applyNewHeight(static_cast(clamp(height, minHeight, maxHeight)), 0, j, strenght); for(int i=1; i((cells[i][j-1].height+cells[i-1][j].height)/2.f+random.randRange(-1, 1)); float newHeight= static_cast(clamp(height, minHeight, maxHeight)); applyNewHeight(newHeight, i, j, strenght); } } } void Map::applyNewHeight(float newHeight, int x, int y, int strenght){ cells[x][y].height= static_cast(((cells[x][y].height*strenght)+newHeight)/(strenght+1)); } }}// end namespace