Cube  Diff

Differences From Artifact [853a6dbe34]:

To Artifact [95faadaf28]:


20
21
22
23
24
25
26
27
28


29
30
31
32
33
34
35
20
21
22
23
24
25
26


27
28
29
30
31
32
33
34
35







-
-
+
+







bool notgotitems = true;
int mode = 0;

// hack: called from savegame code, only works in SP
void
restoreserverstate(OFArray<Entity *> *ents)
{
	[sents enumerateObjectsUsingBlock:^(
	    ServerEntity *e, size_t i, bool *stop) {
	[sents enumerateObjectsUsingBlock:
	    ^ (ServerEntity *e, size_t i, bool *stop) {
		e.spawned = ents[i].spawned;
		e.spawnsecs = 0;
	}];
}

int interm = 0, minremain = 0, mapend = 0;
bool mapreload = false;
97
98
99
100
101
102
103
104
105


106
107
108
109
110
111
112
97
98
99
100
101
102
103


104
105
106
107
108
109
110
111
112







-
-
+
+







	if (packet->referenceCount == 0)
		enet_packet_destroy(packet);
}

void
disconnect_client(int n, OFString *reason)
{
	[OFStdOut writeFormat:@"disconnecting client (%@) [%@]\n",
	    clients[n].hostname, reason];
	[OFStdOut writeFormat: @"disconnecting client (%@) [%@]\n",
			       clients[n].hostname, reason];
	enet_peer_disconnect(clients[n].peer);
	clients[n].type = ST_EMPTY;
	send2(true, -1, SV_CDIS, n);
}

void
resetitems()
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
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







-
+












-
-
+
+







	clients[sender].mapvote = map;
	clients[sender].modevote = reqmode;

	int yes = 0, no = 0;
	for (Client *client in clients) {
		if (client.type != ST_EMPTY) {
			if (client.mapvote.length > 0) {
				if ([client.mapvote isEqual:map] &&
				if ([client.mapvote isEqual: map] &&
				    client.modevote == reqmode)
					yes++;
				else
					no++;
			} else
				no++;
		}
	}

	if (yes == 1 && no == 0)
		return true; // single player

	OFString *msg = [OFString
	    stringWithFormat:@"%@ suggests %@ on map %@ (set map to vote)",
	OFString *msg = [OFString stringWithFormat:
	    @"%@ suggests %@ on map %@ (set map to vote)",
	    clients[sender].name, modestr(reqmode), map];
	sendservmsg(msg);

	if (yes / (float)(yes + no) <= 0.5f)
		return false;

	sendservmsg(@"vote passed");
223
224
225
226
227
228
229
230
231


232
233
234
235
236
237
238
223
224
225
226
227
228
229


230
231
232
233
234
235
236
237
238







-
-
+
+







		}

		case SV_ITEMLIST: {
			int n;
			while ((n = getint(&p)) != -1)
				if (notgotitems) {
					while (sents.count <= n)
						[sents addObject:[ServerEntity
						                     entity]];
						[sents addObject:
						    [ServerEntity entity]];
					sents[n].spawned = true;
				}
			notgotitems = false;
			break;
		}

		case SV_ITEMPICKUP: {
310
311
312
313
314
315
316
317
318


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


317
318
319
320
321
322
323
324
325







-
-
+
+







	sendstring(serverpassword, &p);
	putint(&p, clients.count > maxclients);
	if (smapname.length > 0) {
		putint(&p, SV_MAPCHANGE);
		sendstring(smapname, &p);
		putint(&p, mode);
		putint(&p, SV_ITEMLIST);
		[sents enumerateObjectsUsingBlock:^(
		    ServerEntity *e, size_t i, bool *stop) {
		[sents enumerateObjectsUsingBlock:
		    ^ (ServerEntity *e, size_t i, bool *stop) {
			if (e.spawned)
				putint(&p, i);
		}];
		putint(&p, -1);
	}
	*(unsigned short *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
352
353
354
355
356
357
358
359

360
361
362
363
364
365
366
352
353
354
355
356
357
358

359
360
361
362
363
364
365
366







-
+







			return client;

	Client *client = [Client client];

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

	[clients addObject:client];
	[clients addObject: client];

	return client;
}

void
checkintermission()
{
401
402
403
404
405
406
407
408
409


410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425


426
427
428
429
430
431
432
401
402
403
404
405
406
407


408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423


424
425
426
427
428
429
430
431
432







-
-
+
+














-
-
+
+








void
serverslice(int seconds,
    unsigned int timeout) // main server update, called from cube main loop in
                          // sp, or dedicated server loop
{
	// spawn entities when timer reached
	[sents enumerateObjectsUsingBlock:^(
	    ServerEntity *e, size_t i, bool *stop) {
	[sents enumerateObjectsUsingBlock:
	    ^ (ServerEntity *e, size_t i, bool *stop) {
		if (e.spawnsecs && (e.spawnsecs -= seconds - lastsec) <= 0) {
			e.spawnsecs = 0;
			e.spawned = true;
			send2(true, -1, SV_ITEMSPAWN, i);
		}
	}];

	lastsec = seconds;

	if ((mode > 1 || (mode == 0 && nonlocalclients)) &&
	    seconds > mapend - minremain * 60)
		checkintermission();
	if (interm && seconds > interm) {
		interm = 0;
		[clients enumerateObjectsUsingBlock:^(
		    Client *client, size_t i, bool *stop) {
		[clients enumerateObjectsUsingBlock:
		    ^ (Client *client, size_t i, bool *stop) {
			if (client.type != ST_EMPTY) {
				// ask a client to trigger map reload
				send2(true, i, SV_MAPRELOAD, 0);
				mapreload = true;
				*stop = true;
				return;
			}
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
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







-
-
+
+












-
+
+







			c.peer = event.peer;
			c.peer->data = (void *)(clients.count - 1);
			char hn[1024];
			c.hostname = (enet_address_get_host(
			                  &c.peer->address, hn, sizeof(hn)) == 0
			        ? @(hn)
			        : @"localhost");
			[OFStdOut
			    writeFormat:@"client connected (%@)\n", c.hostname];
			[OFStdOut writeFormat: @"client connected (%@)\n",
					       c.hostname];
			send_welcome(lastconnect = clients.count - 1);
			break;
		}
		case ENET_EVENT_TYPE_RECEIVE:
			brec += event.packet->dataLength;
			process(event.packet, (intptr_t)event.peer->data);
			if (event.packet->referenceCount == 0)
				enet_packet_destroy(event.packet);
			break;
		case ENET_EVENT_TYPE_DISCONNECT:
			if ((intptr_t)event.peer->data < 0)
				break;
			[OFStdOut writeFormat:@"disconnected client (%@)\n",
			[OFStdOut writeFormat:
			    @"disconnected client (%@)\n",
			    clients[(size_t)event.peer->data].hostname];
			clients[(size_t)event.peer->data].type = ST_EMPTY;
			send2(true, -1, SV_CDIS, (intptr_t)event.peer->data);
			event.peer->data = (void *)-1;
			break;
		case ENET_EVENT_TYPE_NONE:
			break;