Cube  Diff

Differences From Artifact [2bcb653cb7]:

To Artifact [4ce3f10da1]:


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
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
253






254
255

256
257
258
259
260
261
262
263
264
265
266
267
268
269














270
271
272
273
274
275
276
277
278
279
280
281
282
283
















284
285

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324















































325
326
327
328
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
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
253
254
255
256
257
258
259
260
261
262
263
264
265



266
267


268











269
270
271
272
273
274
275
276
277
278







279
280
281
282
283
284
285
286
287
288






289
290
291
292
293
294


295














296
297
298
299
300
301
302
303
304
305
306
307
308
309














310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325


326







































327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376





+
-
+

-
-
+
+


-
+

+
-
+

-
-
-
+
+
+
-
-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+

-
-
+
+


-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+

+
-
+

-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+


+
-
+

-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+

+
-
+

-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
+

+
-
+

-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+


+
-
+

-
-
-
-
+
+
+
+
+
+
+


-
-
-
+
+
+
+

+
-
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+
-
-
+
-
-
+
-
-
+
-
-
-
-
-
-
+
+
+
+
+
-
-
+
-
-
+
-
-
+
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
-
+
+

-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+


-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



-
// worldio.cpp: loading & saving of maps and savegames

#include "cube.h"

void
void backup(char *name, char *backupname)
backup(char *name, char *backupname)
{
    remove(backupname);
    rename(name, backupname);
	remove(backupname);
	rename(name, backupname);
};

string cgzname, bakname, pcfname, mcfname; 
string cgzname, bakname, pcfname, mcfname;

void
void setnames(char *name)
setnames(char *name)
{
    string pakname, mapname;
    char *slash = strpbrk(name, "/\\");
    if(slash)
	string pakname, mapname;
	char *slash = strpbrk(name, "/\\");
	if (slash) {
    {
        strn0cpy(pakname, name, slash-name+1);
        strcpy_s(mapname, slash+1);
		strn0cpy(pakname, name, slash - name + 1);
		strcpy_s(mapname, slash + 1);
    }
    else
	} 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);
		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);
	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).

inline bool nhf(sqr *s) { return s->type!=FHF && s->type!=CHF; };
// 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).

inline bool
nhf(sqr *s)
{
	return s->type != FHF && s->type != CHF;
};

void
void voptimize()        // reset vdeltas on non-hf cubes
voptimize() // reset vdeltas on non-hf cubes
{
    loop(x, ssize) loop(y, ssize)
    {
        sqr *s = S(x, y);
        if(x && y) { if(nhf(s) && nhf(S(x-1, y)) && nhf(S(x-1, y-1)) && nhf(S(x, y-1))) s->vdelta = 0; }
        else s->vdelta = 0;
    };
	loop(x, ssize) loop(y, ssize)
	{
		sqr *s = S(x, y);
		if (x && y) {
			if (nhf(s) && nhf(S(x - 1, y)) &&
			    nhf(S(x - 1, y - 1)) && nhf(S(x, y - 1)))
				s->vdelta = 0;
		} else
			s->vdelta = 0;
	};
};

void
void topt(sqr *s, bool &wf, bool &uf, int &wt, int &ut)
topt(sqr *s, bool &wf, bool &uf, int &wt, int &ut)
{
    sqr *o[4];
    o[0] = SWS(s,0,-1,ssize);
    o[1] = SWS(s,0,1,ssize);
    o[2] = SWS(s,1,0,ssize);
    o[3] = SWS(s,-1,0,ssize);
    wf = true;
    uf = true;
    if(SOLID(s))
	sqr *o[4];
	o[0] = SWS(s, 0, -1, ssize);
	o[1] = SWS(s, 0, 1, ssize);
	o[2] = SWS(s, 1, 0, ssize);
	o[3] = SWS(s, -1, 0, ssize);
	wf = true;
	uf = true;
	if (SOLID(s)) {
    {
        loopi(4) if(!SOLID(o[i]))
        {
            wf = false;
            wt = s->wtex;
            ut = s->utex;
            return;
        };
		loopi(4) if (!SOLID(o[i]))
		{
			wf = false;
			wt = s->wtex;
			ut = s->utex;
			return;
		};
    }
    else
	} else {
    {
        loopi(4) if(!SOLID(o[i]))
        {
            if(o[i]->floor<s->floor) { wt = s->wtex; wf = false; };
            if(o[i]->ceil>s->ceil)   { ut = s->utex; uf = false; };
        };
    };
};
		loopi(4) if (!SOLID(o[i]))
		{
			if (o[i]->floor < s->floor) {
				wt = s->wtex;
				wf = false;
			};
			if (o[i]->ceil > s->ceil) {
				ut = s->utex;
				uf = false;
			};
		};
	};
};

void
void toptimize() // FIXME: only does 2x2, make atleast for 4x4 also
toptimize() // FIXME: only does 2x2, make atleast for 4x4 also
{
    bool wf[4], uf[4];
    sqr *s[4];
    for(int x = 2; x<ssize-4; x += 2) for(int y = 2; y<ssize-4; y += 2)
	bool wf[4], uf[4];
	sqr *s[4];
	for (int x = 2; x < ssize - 4; x += 2)
    {
        s[0] = S(x,y);
        int wt = s[0]->wtex, ut = s[0]->utex;
        topt(s[0], wf[0], uf[0], wt, ut);
        topt(s[1] = SWS(s[0],0,1,ssize), wf[1], uf[1], wt, ut);
        topt(s[2] = SWS(s[0],1,1,ssize), wf[2], uf[2], wt, ut);
        topt(s[3] = SWS(s[0],1,0,ssize), wf[3], uf[3], wt, ut);
        loopi(4)
        {
            if(wf[i]) s[i]->wtex = wt;
            if(uf[i]) s[i]->utex = ut;
        };
    };
		for (int y = 2; y < ssize - 4; y += 2) {
			s[0] = S(x, y);
			int wt = s[0]->wtex, ut = s[0]->utex;
			topt(s[0], wf[0], uf[0], wt, ut);
			topt(s[1] = SWS(s[0], 0, 1, ssize), wf[1], uf[1], wt,
			    ut);
			topt(s[2] = SWS(s[0], 1, 1, ssize), wf[2], uf[2], wt,
			    ut);
			topt(s[3] = SWS(s[0], 1, 0, ssize), wf[3], uf[3], wt,
			    ut);
			loopi(4)
			{
				if (wf[i])
					s[i]->wtex = wt;
				if (uf[i])
					s[i]->utex = ut;
			};
		};
};

// these two are used by getmap/sendmap.. transfers compressed maps directly 
// these two are used by getmap/sendmap.. transfers compressed maps directly

void
void writemap(char *mname, int msize, uchar *mdata)
writemap(char *mname, int msize, uchar *mdata)
{
    setnames(mname);
    backup(cgzname, bakname);
    FILE *f = fopen(cgzname, "wb");
    if(!f) { conoutf("could not write map to %s", cgzname); return; };
    fwrite(mdata, 1, msize, f);
    fclose(f);
    conoutf("wrote map %s as file %s", mname, cgzname);
	setnames(mname);
	backup(cgzname, bakname);
	FILE *f = fopen(cgzname, "wb");
	if (!f) {
		conoutf("could not write map to %s", cgzname);
		return;
	};
	fwrite(mdata, 1, msize, f);
	fclose(f);
	conoutf("wrote map %s as file %s", mname, cgzname);
}

uchar *
uchar *readmap(char *mname, int *msize)
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;
	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.
// 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
void save_world(char *mname)
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) 
	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)
			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 (SOLID(s)) {
        {
            if(t && c(type) && c(wtex) && c(vdelta))
			if (t && c(type) && c(wtex) && c(vdelta)) {
            {
                sc++;
				sc++;
            }
            else
			} else {
            {
                spurge;
                gzputc(f, s->type);
                gzputc(f, s->wtex);
                gzputc(f, s->vdelta);
            };
				spurge;
				gzputc(f, s->type);
				gzputc(f, s->wtex);
				gzputc(f, s->vdelta);
			};
        }
        else
		} else {
        {
            if(t && c(type) && c(floor) && c(ceil) && c(ctex) && c(ftex) && c(utex) && c(wtex) && c(vdelta) && c(tag))
			if (t && c(type) && c(floor) && c(ceil) && c(ctex) &&
            {
                sc++;
			    c(ftex) && c(utex) && c(wtex) && c(vdelta) &&
			    c(tag)) {
				sc++;
            }
            else
			} 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();
				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
void load_world(char *mname)        // still supports all map formats that have existed since the earliest cube betas!
load_world(char *mname) // still supports all map formats that have existed
                        // since the earliest cube betas!
{
    stopifrecording();
    cleardlights();
    pruneundos();
    setnames(mname);
    gzFile f = gzopen(cgzname, "rb9");
    if(!f) { conoutf("could not read map %s", cgzname); return; };
    gzread(f, &hdr, sizeof(header)-sizeof(int)*16);
    endianswap(&hdr.version, sizeof(int), 4);
    if(strncmp(hdr.head, "CUBE", 4)!=0) fatal("while reading map: header malformatted");
    if(hdr.version>MAPVERSION) fatal("this map requires a newer version of cube");
    if(sfactor<SMALLEST_FACTOR || sfactor>LARGEST_FACTOR) fatal("illegal map size");
    if(hdr.version>=4)
	stopifrecording();
	cleardlights();
	pruneundos();
	setnames(mname);
	gzFile f = gzopen(cgzname, "rb9");
	if (!f) {
		conoutf("could not read map %s", cgzname);
		return;
	};
	gzread(f, &hdr, sizeof(header) - sizeof(int) * 16);
	endianswap(&hdr.version, sizeof(int), 4);
	if (strncmp(hdr.head, "CUBE", 4) != 0)
		fatal("while reading map: header malformatted");
	if (hdr.version > MAPVERSION)
		fatal("this map requires a newer version of cube");
	if (sfactor < SMALLEST_FACTOR || sfactor > LARGEST_FACTOR)
		fatal("illegal map size");
	if (hdr.version >= 4) {
    {
        gzread(f, &hdr.waterlevel, sizeof(int)*16);
        endianswap(&hdr.waterlevel, sizeof(int), 16);
		gzread(f, &hdr.waterlevel, sizeof(int) * 16);
		endianswap(&hdr.waterlevel, sizeof(int), 16);
    }
    else
	} else {
    {
        hdr.waterlevel = -100000;
    };
    ents.setsize(0);
    loopi(hdr.numents)
    {
        entity &e = ents.add();
        gzread(f, &e, sizeof(persistent_entity));
        endianswap(&e, sizeof(short), 4);
        e.spawned = false;
        if(e.type==LIGHT)
		hdr.waterlevel = -100000;
	};
	ents.setsize(0);
	loopi(hdr.numents)
	{
		entity &e = ents.add();
		gzread(f, &e, sizeof(persistent_entity));
		endianswap(&e, sizeof(short), 4);
		e.spawned = false;
		if (e.type == LIGHT) {
        {
            if(!e.attr2) e.attr2 = 255;  // needed for MAPVERSION<=2
            if(e.attr1>32) e.attr1 = 32; // 12_03 and below
        };
    };
    free(world);
    setupworld(hdr.sfactor);
			if (!e.attr2)
				e.attr2 = 255; // needed for MAPVERSION<=2
			if (e.attr1 > 32)
				e.attr1 = 32; // 12_03 and below
		};
	};
	free(world);
	setupworld(hdr.sfactor);
	char texuse[256];
	loopi(256) texuse[i] = 0;
    sqr *t = NULL;
    loopk(cubicsize)
    {
        sqr *s = &world[k];
        int type = gzgetc(f);
        switch(type)
	sqr *t = NULL;
	loopk(cubicsize)
	{
		sqr *s = &world[k];
		int type = gzgetc(f);
		switch (type) {
        {
            case 255:  
		case 255: {
            {
                int n = gzgetc(f);
                for(int i = 0; i<n; i++, k++) memcpy(&world[k], t, sizeof(sqr));
                k--;
                break;
            };
            case 254: // only in MAPVERSION<=2
            {
                memcpy(s, t, sizeof(sqr));
                s->r = s->g = s->b = gzgetc(f);
                gzgetc(f);
                break;
            };
            case SOLID:
			int n = gzgetc(f);
			for (int i = 0; i < n; i++, k++)
				memcpy(&world[k], t, sizeof(sqr));
			k--;
			break;
		};
		case 254: // only in MAPVERSION<=2
		{
			memcpy(s, t, sizeof(sqr));
			s->r = s->g = s->b = gzgetc(f);
			gzgetc(f);
			break;
		};
		case SOLID: {
            {
                s->type = SOLID;
                s->wtex = gzgetc(f);
                s->vdelta = gzgetc(f);
                if(hdr.version<=2) { gzgetc(f); gzgetc(f); };
                s->ftex = DEFAULT_FLOOR;
                s->ctex = DEFAULT_CEIL;
                s->utex = s->wtex;
                s->tag = 0;
                s->floor = 0;
                s->ceil = 16;
                break;
            };
            default:
			s->type = SOLID;
			s->wtex = gzgetc(f);
			s->vdelta = gzgetc(f);
			if (hdr.version <= 2) {
				gzgetc(f);
				gzgetc(f);
			};
			s->ftex = DEFAULT_FLOOR;
			s->ctex = DEFAULT_CEIL;
			s->utex = s->wtex;
			s->tag = 0;
			s->floor = 0;
			s->ceil = 16;
			break;
		};
		default: {
            {
                if(type<0 || type>=MAXTYPE)
			if (type < 0 || type >= MAXTYPE) {
                {
                    sprintf_sd(t)("%d @ %d", type, k);
                    fatal("while reading map: type out of range: ", t);
                };
                s->type = type;
                s->floor = gzgetc(f);
                s->ceil = gzgetc(f);
                if(s->floor>=s->ceil) s->floor = s->ceil-1;  // for pre 12_13
                s->wtex = gzgetc(f);
                s->ftex = gzgetc(f);
                s->ctex = gzgetc(f);
                if(hdr.version<=2) { gzgetc(f); gzgetc(f); };
                s->vdelta = gzgetc(f);
                s->utex = (hdr.version>=2) ? gzgetc(f) : s->wtex;
                s->tag = (hdr.version>=5) ? gzgetc(f) : 0;
                s->type = type;
            };
        };
        s->defer = 0;
        t = s;
        texuse[s->wtex] = 1;
        if(!SOLID(s)) texuse[s->utex] = texuse[s->ftex] = texuse[s->ctex] = 1;
    };
    gzclose(f);
    calclight();
    settagareas();
    int xs, ys;
    loopi(256) if(texuse) lookuptexture(i, xs, ys);
    conoutf("read map %s (%d milliseconds)", cgzname, SDL_GetTicks()-lastmillis);
    conoutf("%s", hdr.maptitle);
    startmap(mname);
    loopl(256)
    {
        sprintf_sd(aliasname)("level_trigger_%d", l);     // can this be done smarter?
        if(identexists(aliasname)) alias(aliasname, "");
    };
    execfile("data/default_map_settings.cfg");
    execfile(pcfname);
    execfile(mcfname);
				sprintf_sd(t)("%d @ %d", type, k);
				fatal("while reading map: type out of range: ",
				    t);
			};
			s->type = type;
			s->floor = gzgetc(f);
			s->ceil = gzgetc(f);
			if (s->floor >= s->ceil)
				s->floor = s->ceil - 1; // for pre 12_13
			s->wtex = gzgetc(f);
			s->ftex = gzgetc(f);
			s->ctex = gzgetc(f);
			if (hdr.version <= 2) {
				gzgetc(f);
				gzgetc(f);
			};
			s->vdelta = gzgetc(f);
			s->utex = (hdr.version >= 2) ? gzgetc(f) : s->wtex;
			s->tag = (hdr.version >= 5) ? gzgetc(f) : 0;
			s->type = type;
		};
		};
		s->defer = 0;
		t = s;
		texuse[s->wtex] = 1;
		if (!SOLID(s))
			texuse[s->utex] = texuse[s->ftex] = texuse[s->ctex] = 1;
	};
	gzclose(f);
	calclight();
	settagareas();
	int xs, ys;
	loopi(256) if (texuse) lookuptexture(i, xs, ys);
	conoutf("read map %s (%d milliseconds)", cgzname,
	    SDL_GetTicks() - lastmillis);
	conoutf("%s", hdr.maptitle);
	startmap(mname);
	loopl(256)
	{
		sprintf_sd(aliasname)(
		    "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);