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
|
//world.cpp
World::~World()
{
if(tiles) delete [] tiles;
if(entities) delete [] entities;
if(newChunks) delete [] newChunks;
}
int World::create(std::string name, uint8_t world_mode, uint8_t difficulty, long startX, long startY)
{
//some code
std::cout<<"Generating chunks \n";
generateChunks(playerX, playerY, 4, 2);
std::cout<<"saving chunks \n";
if(saveChunksToFile()==-1) return -1;
return 0;
}
int World::saveChunksToFile(int mode)
{
std::cout<<"save chunks: opening ofstream \n";
std::ofstream outp(filename2.c_str(), std::fstream::app);
if(!outp.is_open()) return -1;
std::cout<<"start loop \n";
for(int i=0; i<newChunkCount; i++)
{
std::cout<<"outp header \n";
outp<<" #chnk "<<newChunks[i].X<<" "<<newChunks[i].Y<<" "<<newChunks[i].how_many_entities<<" ";
std::cout<<"posting chars \n";
for(int j=0; j<64; j++)
{
outp<<char(newChunks[i].tiles[j].type)<<" ";
}
outp<<" #ents ";
std::cout<<"entities \n";
if(newChunks[i].how_many_entities>MAX_ENTITIES) newChunks[i].how_many_entities=MAX_ENTITIES;
for(int j=0; j<newChunks[i].how_many_entities; j++)
{
outp<<newChunks[i].entities[j].active<<" ";
outp<<char(newChunks[i].entities[j].Chunk_X)<<" ";
outp<<char(newChunks[i].entities[j].Chunk_Y)<<" ";
outp<<char(newChunks[i].entities[j].type)<<" ";
}
outp<<"\n ";
std::cout<<"1 chunk posted \n";
}
std::cout<<"closing \n";
outp.close();
std::cout<<"size: "<<newChunkCount<<"\n";
std::cout<<"deleting newChunks \n";
newChunkCount=0;
if(newChunks)
{
delete [] newChunks;
newChunks=nullptr;
}
return 0;
}
int World::generateChunks(long X, long Y, int howMany_X, int howMany_Y)
{
int ix, iy, i, am=0;
Coordinate coord[4*howMany_X*howMany_Y];
for(iy=Y-howMany_Y; iy<Y+howMany_Y; iy++)
{
for(ix=X-howMany_X; ix<X+howMany_X; ix++)
{
if(!checkChunk(ix,iy))
{
coord[am].x=ix;
coord[am].y=iy;
am++;
}
}
}
if(am==0) return 1;
newChunkCount=am;
if(newChunks) delete [] newChunks;
newChunks=new Chunk[newChunkCount]; //creating that pointer
for(i=0; i<am; i++)
{
newChunks[i].X=coord[i].x;
newChunks[i].Y=coord[i].y;
newChunks[i].how_many_entities=0;
newChunks[i].number=i;
generateTiles(newChunks[i]);
}
populateNewChunks();
//if(saveChunksToFile()==-1) return -1;
return 0;
}
|