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
|
// worldio.cpp: loading & saving of maps and savegames
#include "cube.h"
void
backup(char *name, char *backupname)
{
remove(backupname);
rename(name, backupname);
};
string cgzname, bakname, pcfname, mcfname;
void
setnames(char *name)
{
string pakname, mapname;
char *slash = strpbrk(name, "/\\");
if (slash) {
strn0cpy(pakname, name, slash - name + 1);
strcpy_s(mapname, slash + 1);
} else {
strcpy_s(pakname, "base");
strcpy_s(mapname, name);
};
sprintf_s(cgzname)("packages/%s/%s.cgz", pakname, mapname);
sprintf_s(bakname)(
"packages/%s/%s_%d.BAK", pakname, mapname, lastmillis);
sprintf_s(pcfname)("packages/%s/package.cfg", pakname);
sprintf_s(mcfname)("packages/%s/%s.cfg", pakname, mapname);
path(cgzname);
path(bakname);
};
// the optimize routines below are here to reduce the detrimental effects of
// messy mapping by setting certain properties (vdeltas and textures) to
// neighbouring values wherever there is no visible difference. This allows the
// mipmapper to generate more efficient mips. the reason it is done on save is
// to reduce the amount spend in the mipmapper (as that is done in realtime).
|
|
|
|
<
>
<
>
|
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
|
// worldio.cpp: loading & saving of maps and savegames
#include "cube.h"
void
backup(char *name, char *backupname)
{
remove(backupname);
rename(name, backupname);
};
string cgzname, bakname, pcfname, mcfname;
static void
setnames(const char *name)
{
string pakname, mapname;
const char *slash = strpbrk(name, "/\\");
if (slash) {
strn0cpy(pakname, name, slash - name + 1);
strcpy_s(mapname, slash + 1);
} else {
strcpy_s(pakname, "base");
strcpy_s(mapname, name);
}
sprintf_s(cgzname)("packages/%s/%s.cgz", pakname, mapname);
sprintf_s(bakname)(
"packages/%s/%s_%d.BAK", pakname, mapname, lastmillis);
sprintf_s(pcfname)("packages/%s/package.cfg", pakname);
sprintf_s(mcfname)("packages/%s/%s.cfg", pakname, mapname);
path(cgzname);
path(bakname);
}
// the optimize routines below are here to reduce the detrimental effects of
// messy mapping by setting certain properties (vdeltas and textures) to
// neighbouring values wherever there is no visible difference. This allows the
// mipmapper to generate more efficient mips. the reason it is done on save is
// to reduce the amount spend in the mipmapper (as that is done in realtime).
|
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
};
fwrite(mdata, 1, msize, f);
fclose(f);
conoutf(@"wrote map %s as file %s", mname, cgzname);
}
uchar *
readmap(char *mname, int *msize)
{
setnames(mname);
uchar *mdata = (uchar *)loadfile(cgzname, msize);
if (!mdata) {
conoutf(@"could not read map %s", cgzname);
return NULL;
};
return mdata;
}
// save map as .cgz file. uses 2 layers of compression: first does simple
// run-length encoding and leaves out data for certain kinds of cubes, then zlib
// removes the last bits of redundancy. Both passes contribute greatly to the
// miniscule map sizes.
void
save_world(char *mname)
{
resettagareas(); // wouldn't be able to reproduce tagged areas otherwise
voptimize();
toptimize();
if (!*mname)
mname = getclientmap();
setnames(mname);
backup(cgzname, bakname);
gzFile f = gzopen(cgzname, "wb9");
if (!f) {
conoutf(@"could not write map to %s", cgzname);
return;
};
hdr.version = MAPVERSION;
hdr.numents = 0;
loopv(ents) if (ents[i].type != NOTUSED) hdr.numents++;
header tmp = hdr;
endianswap(&tmp.version, sizeof(int), 4);
endianswap(&tmp.waterlevel, sizeof(int), 16);
gzwrite(f, &tmp, sizeof(header));
loopv(ents)
{
if (ents[i].type != NOTUSED) {
entity tmp = ents[i];
endianswap(&tmp, sizeof(short), 4);
gzwrite(f, &tmp, sizeof(persistent_entity));
};
};
sqr *t = NULL;
int sc = 0;
#define spurge \
while (sc) { \
gzputc(f, 255); \
if (sc > 255) { \
gzputc(f, 255); \
sc -= 255; \
} else { \
gzputc(f, sc); \
sc = 0; \
} \
};
loopk(cubicsize)
{
sqr *s = &world[k];
#define c(f) (s->f == t->f)
// 4 types of blocks, to compress a bit:
// 255 (2): same as previous block + count
// 254 (3): same as previous, except light // deprecated
// SOLID (5)
// anything else (9)
if (SOLID(s)) {
if (t && c(type) && c(wtex) && c(vdelta)) {
sc++;
} else {
spurge;
gzputc(f, s->type);
gzputc(f, s->wtex);
gzputc(f, s->vdelta);
};
} else {
if (t && c(type) && c(floor) && c(ceil) && c(ctex) &&
c(ftex) && c(utex) && c(wtex) && c(vdelta) &&
c(tag)) {
sc++;
} else {
spurge;
gzputc(f, s->type);
gzputc(f, s->floor);
gzputc(f, s->ceil);
gzputc(f, s->wtex);
gzputc(f, s->ftex);
gzputc(f, s->ctex);
gzputc(f, s->vdelta);
gzputc(f, s->utex);
gzputc(f, s->tag);
};
};
t = s;
};
spurge;
gzclose(f);
conoutf(@"wrote map file %s", cgzname);
settagareas();
};
void
load_world(char *mname) // still supports all map formats that have existed
// since the earliest cube betas!
{
stopifrecording();
cleardlights();
|
|
|
>
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
>
>
|
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
};
fwrite(mdata, 1, msize, f);
fclose(f);
conoutf(@"wrote map %s as file %s", mname, cgzname);
}
uchar *
readmap(const char *mname, int *msize)
{
setnames(mname);
uchar *mdata = (uchar *)loadfile(cgzname, msize);
if (!mdata) {
conoutf(@"could not read map %s", cgzname);
return NULL;
};
return mdata;
}
// save map as .cgz file. uses 2 layers of compression: first does simple
// run-length encoding and leaves out data for certain kinds of cubes, then zlib
// removes the last bits of redundancy. Both passes contribute greatly to the
// miniscule map sizes.
void
save_world(const char *mname)
{
@autoreleasepool {
resettagareas(); // wouldn't be able to reproduce tagged areas
// otherwise
voptimize();
toptimize();
if (!*mname)
mname = getclientmap().UTF8String;
setnames(mname);
backup(cgzname, bakname);
gzFile f = gzopen(cgzname, "wb9");
if (!f) {
conoutf(@"could not write map to %s", cgzname);
return;
};
hdr.version = MAPVERSION;
hdr.numents = 0;
loopv(ents) if (ents[i].type != NOTUSED) hdr.numents++;
header tmp = hdr;
endianswap(&tmp.version, sizeof(int), 4);
endianswap(&tmp.waterlevel, sizeof(int), 16);
gzwrite(f, &tmp, sizeof(header));
loopv(ents)
{
if (ents[i].type != NOTUSED) {
entity tmp = ents[i];
endianswap(&tmp, sizeof(short), 4);
gzwrite(f, &tmp, sizeof(persistent_entity));
};
};
sqr *t = NULL;
int sc = 0;
#define spurge \
while (sc) { \
gzputc(f, 255); \
if (sc > 255) { \
gzputc(f, 255); \
sc -= 255; \
} else { \
gzputc(f, sc); \
sc = 0; \
} \
};
loopk(cubicsize)
{
sqr *s = &world[k];
#define c(f) (s->f == t->f)
// 4 types of blocks, to compress a bit:
// 255 (2): same as previous block + count
// 254 (3): same as previous, except light // deprecated
// SOLID (5)
// anything else (9)
if (SOLID(s)) {
if (t && c(type) && c(wtex) && c(vdelta)) {
sc++;
} else {
spurge;
gzputc(f, s->type);
gzputc(f, s->wtex);
gzputc(f, s->vdelta);
};
} else {
if (t && c(type) && c(floor) && c(ceil) &&
c(ctex) && c(ftex) && c(utex) && c(wtex) &&
c(vdelta) && c(tag)) {
sc++;
} else {
spurge;
gzputc(f, s->type);
gzputc(f, s->floor);
gzputc(f, s->ceil);
gzputc(f, s->wtex);
gzputc(f, s->ftex);
gzputc(f, s->ctex);
gzputc(f, s->vdelta);
gzputc(f, s->utex);
gzputc(f, s->tag);
};
};
t = s;
};
spurge;
gzclose(f);
conoutf(@"wrote map file %s", cgzname);
settagareas();
}
}
void
load_world(char *mname) // still supports all map formats that have existed
// since the earliest cube betas!
{
stopifrecording();
cleardlights();
|
367
368
369
370
371
372
373
374
375
376
|
"level_trigger_%d", l); // can this be done smarter?
if (identexists(aliasname))
alias(aliasname, "");
};
execfile("data/default_map_settings.cfg");
execfile(pcfname);
execfile(mcfname);
};
COMMANDN(savemap, save_world, ARG_1STR);
|
<
|
>
|
|
370
371
372
373
374
375
376
377
378
379
|
"level_trigger_%d", l); // can this be done smarter?
if (identexists(aliasname))
alias(aliasname, "");
};
execfile("data/default_map_settings.cfg");
execfile(pcfname);
execfile(mcfname);
}
COMMANDN(savemap, save_world, ARG_1CSTR)
|