Cube  Diff

Differences From Artifact [b93241b086]:

To Artifact [822ad5d4d5]:


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






-


+











-
+







// loading and saving of savegames & demos, dumps the spawn state of all
// mapents, the full state of all dynents (monsters + player)

#include "cube.h"

#import "Command.h"
#import "DynamicEntity.h"
#import "Entity.h"
#import "Monster.h"
#import "Player.h"

#ifdef OF_BIG_ENDIAN
static const int islittleendian = 0;
#else
static const int islittleendian = 1;
#endif

static gzFile f = NULL;
bool demorecording = false;
bool demoplayback = false;
bool demoloading = false;
static OFMutableArray<DynamicEntity *> *playerhistory;
static OFMutableArray<Player *> *playerhistory;
int democlientnum = 0;

extern void startdemo();

static void
gzput(int i)
{
101
102
103
104
105
106
107
108

109
110
111
112
113
114
115
101
102
103
104
105
106
107

108
109
110
111
112
113
114
115







-
+







	if (!f) {
		conoutf(@"could not write %@", IRI.string);
		return;
	}
	gzwrite(f, (void *)"CUBESAVE", 8);
	gzputc(f, islittleendian);
	gzputi(SAVEGAMEVERSION);
	OFData *data = [player1 dataBySerializing];
	OFData *data = [Player.player1 dataBySerializing];
	gzputi(data.count);
	char map[_MAXDEFSTR] = { 0 };
	memcpy(map, getclientmap().UTF8String,
	    min(getclientmap().UTF8StringLength, _MAXDEFSTR - 1));
	gzwrite(f, map, _MAXDEFSTR);
	gzputi(gamemode);
	gzputi(ents.count);
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
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







-
-
+
+










-
+










-
+








	restoreserverstate(ents);

	OFMutableData *data =
	    [OFMutableData dataWithCapacity:DynamicEntity.serializedSize];
	[data increaseCountBy:DynamicEntity.serializedSize];
	gzread(f, data.mutableItems, data.count);
	[player1 setFromSerializedData:data];
	player1.lastAction = lastmillis;
	[Player.player1 setFromSerializedData:data];
	Player.player1.lastAction = lastmillis;

	int nmonsters = gzgeti();
	OFArray<Monster *> *monsters = Monster.monsters;
	if (nmonsters != monsters.count)
		return loadgameout();

	for (Monster *monster in monsters) {
		gzread(f, data.mutableItems, data.count);
		[monster setFromSerializedData:data];
		// lazy, could save id of enemy instead
		monster.enemy = player1;
		monster.enemy = Player.player1;
		// also lazy, but no real noticable effect on game
		monster.lastAction = monster.trigger = lastmillis + 500;
		if (monster.state == CS_DEAD)
			monster.lastAction = 0;
	}
	[Monster restoreAll];

	int nplayers = gzgeti();
	for (int i = 0; i < nplayers; i++) {
		if (!gzget()) {
			DynamicEntity *d = getclient(i);
			Player *d = getclient(i);
			assert(d);
			gzread(f, data.mutableItems, data.count);
			[d setFromSerializedData:data];
		}
	}

	conoutf(@"savegame restored");
296
297
298
299
300
301
302


303
304

305
306
307
308
309
310
311
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314







+
+


+







{
	bdamage = damage;
}

void
incomingdemodata(unsigned char *buf, int len, bool extras)
{
	Player *player1 = Player.player1;

	if (!demorecording)
		return;

	gzputi(lastmillis - starttime);
	gzputi(len);
	gzwrite(f, buf, len);
	gzput(extras);
	if (extras) {
		gzput(player1.gunSelect);
		gzput(player1.lastAttackGun);
366
367
368
369
370
371
372
373

374
375
376
377
378
379
380
369
370
371
372
373
374
375

376
377
378
379
380
381
382
383







-
+







void
startdemo()
{
	democlientnum = gzgeti();
	demoplayback = true;
	starttime = lastmillis;
	conoutf(@"now playing demo");
	setclient(democlientnum, [player1 copy]);
	setclient(democlientnum, [Player.player1 copy]);
	readdemotime();
}

VAR(demodelaymsec, 0, 120, 500);

// spline interpolation
#define catmulrom(z, a, b, c, s, dest)                                  \
417
418
419
420
421
422
423
424

425
426
427
428
429
430
431
420
421
422
423
424
425
426

427
428
429
430
431
432
433
434







-
+







			stopreset();
			return;
		}
		unsigned char buf[MAXTRANS];
		gzread(f, buf, len);
		localservertoclient(buf, len); // update game state

		DynamicEntity *target = players[democlientnum];
		Player *target = players[democlientnum];
		assert(target);

		int extras;
		// read additional client side state not present in normal
		// network stream
		if ((extras = gzget())) {
			target.gunSelect = gzget();
448
449
450
451
452
453
454
455

456
457
458
459
460
461
462
451
452
453
454
455
456
457

458
459
460
461
462
463
464
465







-
+







			// FIXME: set more client state here
		}

		// insert latest copy of player into history
		if (extras &&
		    (playerhistory.count == 0 ||
		        playerhistory.lastObject.lastUpdate != playbacktime)) {
			DynamicEntity *d = [target copy];
			Player *d = [target copy];
			d.lastUpdate = playbacktime;

			if (playerhistory == nil)
				playerhistory = [[OFMutableArray alloc] init];

			[playerhistory addObject:d];

471
472
473
474
475
476
477
478
479


480
481
482
483
484

485
486
487
488
489
490
491
492
493
494
495
496
497
498
499



500
501
502
503
504
505

506
507
508
509
510
511
512
513
514

515
516

517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532



533
534
535
536
537
538
539
474
475
476
477
478
479
480


481
482
483
484
485
486

487
488
489
490
491
492
493
494
495
496
497
498
499



500
501
502
503
504
505
506
507

508
509
510
511
512
513
514
515
516

517


518
519
520
521
522
523
524
525
526
527
528
529
530
531



532
533
534
535
536
537
538
539
540
541







-
-
+
+




-
+












-
-
-
+
+
+





-
+








-
+
-
-
+













-
-
-
+
+
+







		return;

	int itime = lastmillis - demodelaymsec;
	// find 2 positions in history that surround interpolation time point
	size_t count = playerhistory.count;
	for (ssize_t i = count - 1; i >= 0; i--) {
		if (playerhistory[i].lastUpdate < itime) {
			DynamicEntity *a = playerhistory[i];
			DynamicEntity *b = a;
			Player *a = playerhistory[i];
			Player *b = a;

			if (i + 1 < playerhistory.count)
				b = playerhistory[i + 1];

			player1 = b;
			Player.player1 = b;
			// interpolate pos & angles
			if (a != b) {
				DynamicEntity *c = b;
				if (i + 2 < playerhistory.count)
					c = playerhistory[i + 2];
				DynamicEntity *z = a;
				if (i - 1 >= 0)
					z = playerhistory[i - 1];
				// if(a==z || b==c)
				//	printf("* %d\n", lastmillis);
				float bf = (itime - a.lastUpdate) /
				    (float)(b.lastUpdate - a.lastUpdate);
				fixwrap(a, player1);
				fixwrap(c, player1);
				fixwrap(z, player1);
				fixwrap(a, b);
				fixwrap(c, b);
				fixwrap(z, b);
				float dist =
				    OFDistanceOfVectors3D(c.origin, z.origin);
				// if teleport or spawn, don't interpolate
				if (dist < 16) {
					catmulrom(z.origin, a.origin, b.origin,
					    c.origin, bf, player1.origin);
					    c.origin, bf, b.origin);
					OFVector3D vz = OFMakeVector3D(
					    z.yaw, z.pitch, z.roll);
					OFVector3D va = OFMakeVector3D(
					    a.yaw, a.pitch, a.roll);
					OFVector3D vb = OFMakeVector3D(
					    b.yaw, b.pitch, b.roll);
					OFVector3D vc = OFMakeVector3D(
					    c.yaw, c.pitch, c.roll);
					OFVector3D vp1 =
					OFVector3D vp1 = OFMakeVector3D(
					    OFMakeVector3D(player1.yaw,
					        player1.pitch, player1.roll);
					    b.yaw, b.pitch, b.roll);
					catmulrom(vz, va, vb, vc, bf, vp1);
					z.yaw = vz.x;
					z.pitch = vz.y;
					z.roll = vz.z;
					a.yaw = va.x;
					a.pitch = va.y;
					a.roll = va.z;
					b.yaw = vb.x;
					b.pitch = vb.y;
					b.roll = vb.z;
					c.yaw = vc.x;
					c.pitch = vc.y;
					c.roll = vc.z;
					player1.yaw = vp1.x;
					player1.pitch = vp1.y;
					player1.roll = vp1.z;
					b.yaw = vp1.x;
					b.pitch = vp1.y;
					b.roll = vp1.z;
				}
				fixplayer1range();
			}
			break;
		}
	}
	// if(player1->state!=CS_DEAD) showscores(false);