-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunks.cpp
86 lines (79 loc) · 2.08 KB
/
chunks.cpp
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
#include "chunks.hpp"
Chunk::Chunk()
: stf::sdb::IChunk({Width, Height})
{
mArray.resize(Width * Height, nullptr);
for(auto &cell : mArray) {
cell = new Cell;
}
}
stf::sdb::IChunk *Chunk::getNew()
{
return new Chunk;
}
bool Chunk::isInitialised() const
{
return mIsInitialised;
}
void Chunk::initialise()
{
mIsInitialised = true;
}
stf::sdb::IChunk &Chunk::load(FILE *file)
{
stf::sdb::IChunk::load(file);
fread(&mIsInitialised, sizeof(mIsInitialised), 1, file);
if(!mIsInitialised) {
int bombs = BombsPerChunk;
do {
stf::Vec2d pos { rand() % Width, rand() % Height };
if(static_cast<Cell *>(at(pos))->uniqueIntView() == Cell().uniqueIntView()) {
delete at(pos);
put(pos, new BombCell);
mBombsPositions.push_back(pos);
--bombs;
}
}
while(bombs);
mIsInitialised = true;
}
for(size_t i = 0; i < mArray.size(); ++i) {
switch (static_cast<Cell*>(mArray[i])->uniqueIntView()) {
case 1: {
auto tmp = new EmptyCell(*static_cast<EmptyCell *>(mArray[i]));
delete mArray[i];
mArray[i] = tmp;
break;
}
case 2: {
auto tmp = new BombCell(*static_cast<BombCell *>(mArray[i]));
delete mArray[i];
mArray[i] = tmp;
break;
}
case 3: {
auto tmp = new BombsNeighborCell(*static_cast<BombsNeighborCell *>(mArray[i]));
delete mArray[i];
mArray[i] = tmp;
break;
}
case 4: {
auto tmp = new LifeCell(*static_cast<LifeCell *>(mArray[i]));
delete mArray[i];
mArray[i] = tmp;
break;
}
}
}
return *this;
}
size_t Chunk::sizeOfSelf() const
{
return stf::sdb::IChunk::sizeOfSelf() + sizeof(mIsInitialised);
}
stf::sdb::IChunk &Chunk::save(FILE *file)
{
stf::sdb::IChunk::save(file);
fwrite(&mIsInitialised, sizeof(mIsInitialised), 1, file);
return *this;
}