-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsaveload.h
214 lines (175 loc) · 4.17 KB
/
saveload.h
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#pragma once
#include <fstream>
#include "enginefuncs.h"
#include "inititems.h"
template <typename T>
void Write (std::ofstream &f, const T& t)
{
f.write(reinterpret_cast<const char*>(&t), sizeof(t));
}
template <typename T>
void Read (std::ifstream &f, T& t)
{
f.read(reinterpret_cast<char*>(&t), sizeof(t));
}
const int cur_ver = 0;
void WriteBloodInfo (std::ofstream& f)
{
for (int j=0; j<MMY; ++j)
{
Write(f, j);
bool prevb=false;
for (int i=0; i<=MMX; ++i)
{
bool curb = i<MMX && (level.map[j][i]==COLOR_BLOOD_1 || level.map[j][i]==COLOR_BLOOD_2);
if (curb^prevb)
Write(f, i);
prevb=curb;
}
Write(f, -1);
}
}
void ReadBloodInfo (std::ifstream& f)
{
FxRandom localrnd (tbal::MSec());
for (int j=0; j<MMY; ++j)
{
int y;
Read(f, y);
assert(y==j);
int x1=-1, x2=-1;
for (;;)
{
Read(f, x1);
if (x1<=x2 || x1>=MMX) break;
Read(f, x2);
if (x2>MMX) break;
for (int i=x1; i<x2; ++i)
level.map[j][i] = localrnd.random(2)==0 ? COLOR_BLOOD_1 : COLOR_BLOOD_2;
}
}
}
void NewGame ()
{
tblib::recreate(activeItems);
tblib::recreate(level);
tblib::recreate(items);
tblib::recreate(itemCells);
level.Init(nextLevelSeed);
/*
tbal::Picture top(1024,600);
level.t.Draw(top);
top.Save("topology.bmp");
tbal::Picture megamap(MMX, MMY);
megamap.Copy(TableCopy(), level.map, 0, 0);
megamap.Save("megamap.bmp");/**/
InitItems ();
Phys ();
}
bool SaveLevel (const char* filename)
{
std::ofstream f( (std::string(tbal::GetExternalFilesDir())+filename).c_str(), std::ios::binary);
if (f.is_open())
{
// ìíå ïîõóé, ÷òî íà 64 áèòàõ ýòîò êîä áóäåò âåñòè ñåáÿ èíà÷å
// èëè ÷òî ñîõðàí¸íêè äåáàãà è ðåëèçà íåñîâìåñòèìû
// íå áóäó ÿ çà òðè äíÿ ðàäè òðýø-ïðîåêòà ïèëèòü íîðìàëüíóþ ñåðèàëèçàöèþ
// ïåðå÷èñëÿòü êàæäûé êîìïîíåíò, àãà, ëîë
Write(f, cur_ver);
Write(f, level.seed);
Write(f, rnd.randseed);
Write(f, isSounds);
Write(f, volume);
//
Write(f, viewX);
Write(f, viewY);
Write(f, sizeof(Item));
Write(f, &itemClasses);
Write(f, &items);
Write(f, items); // òóïî, íî áåç ïîëíîöåííûõ îïåðàòîðîâ ñåðèàëèçàöèè òîëüêî òàê
Write(f, wasIn);
WriteBloodInfo (f);
f.flush();
return ((f.rdstate() & std::ios::badbit) ==0);
}
return false;
}
class AdjustItem
{
ItemClass* rdFirstClass;
Item* rdFirstItem;
public :
AdjustItem(ItemClass* rdFirstClass, Item* rdFirstItem)
: rdFirstClass(rdFirstClass), rdFirstItem(rdFirstItem) {}
void AdjustPtr (ItemClass* &ptr)
{
if (ptr)
ptr = &itemClasses [int(ptr-rdFirstClass)];
}
void AdjustPtr (Item* &ptr)
{
if (ptr)
ptr = &items [int(ptr-rdFirstItem)];
}
void operator () (Item& item)
{
new (&item) IL::xclass(); // òóïî èãíîðèðóåì ìóñîðíûå äàííûå ïðî õýø ïîñëå ÷òåíèÿ èç ôàéëà
AdjustPtr (item.itemClass);
int& tc = Item::TypeCount(item.itemClass);
--tc;
assert(tc>=0);
item.PutToCells();
for (int i=0; i<item.weapons.size(); ++i)
AdjustPtr (item.weapons[i].weaponClass);
AdjustPtr (item.bornHost);
for (int i=0; i<item.childs.size(); ++i) if (item.childs.valid(i))
AdjustPtr(item.childs[i]);
if (item.active)
{
item.active = false;
activeItems.emplace_back(&item);
// òóïî âòûêàåì â ìàññèâ, íå äåëàÿ ëèøíèõ äâèæåíèé
}
}
};
bool LoadLevel (const char* filename)
{
std::ifstream f((std::string(tbal::GetExternalFilesDir())+filename).c_str(), std::ios::binary);
if (f.is_open())
{
// ÷èñòêà ïóëîâ
tblib::recreate(level);
tblib::recreate(items);
tblib::recreate(itemCells);
cntUnits = MAX_UNITS;
cntLoots = MAX_LOOTS;
cntGibs = MAX_GIBS;
cntBullets = MAX_BULLETS;
uint32_t ver;
Read(f, ver);
Read(f, level.seed);
nextLevelSeed = level.seed;
level.Init(nextLevelSeed);
Read(f, rnd.randseed);
Read(f, isSounds);
Read(f, volume);
Read(f, viewX);
Read(f, viewY);
int itemSize;
Item* rdFirstItem;
ItemClass* rdFirstClass;
Read(f, itemSize);
Read(f, rdFirstClass);
Read(f, rdFirstItem);
Read(f, items); // êîíñòðóêòîðû íå âûïîëíÿþòñÿ
Read(f, wasIn);
ReadBloodInfo(f);
AdjustItem adjustItem(rdFirstClass, rdFirstItem);
activeItems.shrink(0);
for (int i=0; i<items.size(); ++i) if (items.valid(i))
adjustItem(items[i]);
player = &items[0];
return true;
}
return false;
}