Cube  Diff

Differences From Artifact [3c6f02e0b1]:

To Artifact [002c7a497a]:


15
16
17
18
19
20
21

22
23
24
25
26
27
28
void
backup(OFString *name, OFString *backupname)
{
	[OFFileManager.defaultManager removeItemAtPath: backupname];
	[OFFileManager.defaultManager moveItemAtPath: name toPath: backupname];
}


static OFString *cgzname, *bakname, *pcfname, *mcfname;

static void
setnames(OFString *name)
{
	OFCharacterSet *cs =
	    [OFCharacterSet characterSetWithCharactersInString: @"/\\"];







>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void
backup(OFString *name, OFString *backupname)
{
	[OFFileManager.defaultManager removeItemAtPath: backupname];
	[OFFileManager.defaultManager moveItemAtPath: name toPath: backupname];
}

// FIXME: Should be OFIRI
static OFString *cgzname, *bakname, *pcfname, *mcfname;

static void
setnames(OFString *name)
{
	OFCharacterSet *cs =
	    [OFCharacterSet characterSetWithCharactersInString: @"/\\"];
141
142
143
144
145
146
147


148



149
150
151
152
153
154
155
156
157
158
159
160
161

void
writemap(OFString *mname, int msize, unsigned char *mdata)
{
	setnames(mname);
	backup(cgzname, bakname);



	FILE *f = fopen([cgzname cStringWithEncoding: OFLocale.encoding], "wb");



	if (!f) {
		conoutf(@"could not write map to %@", cgzname);
		return;
	}
	fwrite(mdata, 1, msize, f);
	fclose(f);
	conoutf(@"wrote map %@ as file %@", mname, cgzname);
}

OFData *
readmap(OFString *mname)
{
	setnames(mname);







>
>
|
>
>
>
|



|
<







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

void
writemap(OFString *mname, int msize, unsigned char *mdata)
{
	setnames(mname);
	backup(cgzname, bakname);

	@try {
		OFIRI *IRI = [Cube.sharedInstance.gameDataIRI
		    IRIByAppendingPathComponent: cgzname];
		OFStream *stream = [OFIRIHandler openItemAtIRI: IRI mode: @"w"];
		[stream writeBuffer: mdata length: msize];
		[stream close];
	} @catch (id e) {
		conoutf(@"could not write map to %@", cgzname);
		return;
	}


	conoutf(@"wrote map %@ as file %@", mname, cgzname);
}

OFData *
readmap(OFString *mname)
{
	setnames(mname);
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
})

// still supports all map formats that have existed since the earliest cube
// betas!
void
load_world(OFString *mname)
{


	stopifrecording();
	cleardlights();
	pruneundos(0);
	setnames(mname);
	gzFile f = gzopen([cgzname cStringWithEncoding: OFLocale.encoding],




	    "rb9");




	if (!f) {

		conoutf(@"could not read map %@", cgzname);
		return;
	}
	gzread(f, &hdr, sizeof(struct 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);
	} else {
		hdr.waterlevel = -100000;
	}
	[ents removeAllObjects];
	for (int i = 0; i < hdr.numents; i++) {
		struct persistent_entity tmp;

		gzread(f, &tmp, sizeof(struct persistent_entity));
		endianswap(&tmp, sizeof(short), 4);

		Entity *e = [Entity entity];
		e.x = tmp.x;
		e.y = tmp.y;
		e.z = tmp.z;
		e.attr1 = tmp.attr1;







>
>




|
>
>
>
>
|
>
>
>
>
|
>



|








>
|







>
|







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
})

// still supports all map formats that have existed since the earliest cube
// betas!
void
load_world(OFString *mname)
{
	OFGZIPStream *stream;

	stopifrecording();
	cleardlights();
	pruneundos(0);
	setnames(mname);

	@try {
		OFIRI *IRI = [Cube.sharedInstance.gameDataIRI
		    IRIByAppendingPathComponent: cgzname];
		OFStream *compressedStream = [OFIRIHandler openItemAtIRI: IRI
								    mode: @"r"];
		stream = [OFGZIPStream streamWithStream: compressedStream
						   mode: @"r"];
		[stream readIntoBuffer: &hdr
			   exactLength: sizeof(struct header) -
					sizeof(int) * 16];
	} @catch (id e) {
		conoutf(@"could not read map %@", cgzname);
		return;
	}

	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) {
		[stream readIntoBuffer: &hdr.waterlevel
			   exactLength: sizeof(int) * 16];
		endianswap(&hdr.waterlevel, sizeof(int), 16);
	} else {
		hdr.waterlevel = -100000;
	}
	[ents removeAllObjects];
	for (int i = 0; i < hdr.numents; i++) {
		struct persistent_entity tmp;
		[stream readIntoBuffer: &tmp
			   exactLength: sizeof(struct persistent_entity)];
		endianswap(&tmp, sizeof(short), 4);

		Entity *e = [Entity entity];
		e.x = tmp.x;
		e.y = tmp.y;
		e.z = tmp.z;
		e.attr1 = tmp.attr1;
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

377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
	setupworld(hdr.sfactor);
	char texuse[256];
	for (int i = 0; i < 256; i++)
		texuse[i] = 0;
	struct sqr *t = NULL;
	for (int k = 0; k < cubicsize; k++) {
		struct sqr *s = &world[k];
		int type = gzgetc(f);
		switch (type) {
		case 255: {
			int n = gzgetc(f);
			for (int i = 0; i < n; i++, k++)
				memcpy(&world[k], t, sizeof(struct sqr));
			k--;
			break;
		}
		case 254: // only in MAPVERSION<=2
		{
			memcpy(s, t, sizeof(struct 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: {
			if (type < 0 || type >= MAXTYPE)
				fatal(@"while reading map: type out of range: "
				      @"%d @ %d",
				    type, k);
			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;
	for (int i = 0; i < 256; i++)
		if (texuse[i])
			lookuptexture(i, &xs, &ys);
	conoutf(@"read map %@ (%d milliseconds)", cgzname,







|


|








|
|




|
|

|
|















|
|


|
|
|

|
|

|
|
>
|









|







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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
	setupworld(hdr.sfactor);
	char texuse[256];
	for (int i = 0; i < 256; i++)
		texuse[i] = 0;
	struct sqr *t = NULL;
	for (int k = 0; k < cubicsize; k++) {
		struct sqr *s = &world[k];
		int type = [stream readInt8];
		switch (type) {
		case 255: {
			int n = [stream readInt8];
			for (int i = 0; i < n; i++, k++)
				memcpy(&world[k], t, sizeof(struct sqr));
			k--;
			break;
		}
		case 254: // only in MAPVERSION<=2
		{
			memcpy(s, t, sizeof(struct sqr));
			s->r = s->g = s->b = [stream readInt8];
			[stream readInt8];
			break;
		}
		case SOLID: {
			s->type = SOLID;
			s->wtex = [stream readInt8];
			s->vdelta = [stream readInt8];
			if (hdr.version <= 2) {
				[stream readInt8];
				[stream readInt8];
			}
			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)
				fatal(@"while reading map: type out of range: "
				      @"%d @ %d",
				    type, k);
			s->type = type;
			s->floor = [stream readInt8];
			s->ceil = [stream readInt8];
			if (s->floor >= s->ceil)
				s->floor = s->ceil - 1; // for pre 12_13
			s->wtex = [stream readInt8];
			s->ftex = [stream readInt8];
			s->ctex = [stream readInt8];
			if (hdr.version <= 2) {
				[stream readInt8];
				[stream readInt8];
			}
			s->vdelta = [stream readInt8];
			s->utex = (hdr.version >= 2
			    ? [stream readInt8] : s->wtex);
			s->tag = (hdr.version >= 5 ? [stream readInt8] : 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;
	}
	[stream close];
	calclight();
	settagareas();
	int xs, ys;
	for (int i = 0; i < 256; i++)
		if (texuse[i])
			lookuptexture(i, &xs, &ys);
	conoutf(@"read map %@ (%d milliseconds)", cgzname,