Cube  Diff

Differences From Artifact [69c5d759fc]:

To Artifact [9e76792f10]:


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
// server.cpp: little more than enhanced multicaster
// runs dedicated or as client coroutine

#include "cube.h"

#import "Client.h"
#import "Entity.h"


enum { ST_EMPTY, ST_LOCAL, ST_TCPIP };

static OFMutableArray<Client *> *clients;

int maxclients = 8;
static OFString *smapname;

// server side version of "entity" type
struct server_entity {
	bool spawned;
	int spawnsecs;
};

vector<server_entity> sents;

// true when map has changed and waiting for clients to send item
bool notgotitems = true;
int mode = 0;

// hack: called from savegame code, only works in SP
void
restoreserverstate(OFArray<Entity *> *ents)
{
	loopv(sents)
	{

		sents[i].spawned = ents[i].spawned;
		sents[i].spawnsecs = 0;
	}
}

int interm = 0, minremain = 0, mapend = 0;
bool mapreload = false;

static OFString *serverpassword = @"";








>








<
<
<
<
<
|
<









|
<
>
|
|
|







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
// server.cpp: little more than enhanced multicaster
// runs dedicated or as client coroutine

#include "cube.h"

#import "Client.h"
#import "Entity.h"
#import "ServerEntity.h"

enum { ST_EMPTY, ST_LOCAL, ST_TCPIP };

static OFMutableArray<Client *> *clients;

int maxclients = 8;
static OFString *smapname;






static OFMutableArray<ServerEntity *> *sents;


// true when map has changed and waiting for clients to send item
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) {
		e.spawned = ents[i].spawned;
		e.spawnsecs = 0;
	}];
}

int interm = 0, minremain = 0, mapend = 0;
bool mapreload = false;

static OFString *serverpassword = @"";

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
	clients[n].type = ST_EMPTY;
	send2(true, -1, SV_CDIS, n);
}

void
resetitems()
{
	sents.setsize(0);
	notgotitems = true;
}

void
pickup(uint i, int sec, int sender) // server side item pickup, acknowledge
                                    // first client that gets it
{
	if (i >= (uint)sents.length())
		return;
	if (sents[i].spawned) {
		sents[i].spawned = false;
		sents[i].spawnsecs = sec;
		send2(true, sender, SV_ITEMACC, i);
	}
}







|







|







107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
	clients[n].type = ST_EMPTY;
	send2(true, -1, SV_CDIS, n);
}

void
resetitems()
{
	[sents removeAllObjects];
	notgotitems = true;
}

void
pickup(uint i, int sec, int sender) // server side item pickup, acknowledge
                                    // first client that gets it
{
	if (i >= (uint)sents.count)
		return;
	if (sents[i].spawned) {
		sents[i].spawned = false;
		sents[i].spawnsecs = sec;
		send2(true, sender, SV_ITEMACC, i);
	}
}
226
227
228
229
230
231
232
233
234

235
236
237
238
239
240
241
242
			break;
		}

		case SV_ITEMLIST: {
			int n;
			while ((n = getint(p)) != -1)
				if (notgotitems) {
					server_entity se = { false, 0 };
					while (sents.length() <= n)

						sents.add(se);
					sents[n].spawned = true;
				}
			notgotitems = false;
			break;
		}

		case SV_ITEMPICKUP: {







<
|
>
|







221
222
223
224
225
226
227

228
229
230
231
232
233
234
235
236
237
			break;
		}

		case SV_ITEMLIST: {
			int n;
			while ((n = getint(p)) != -1)
				if (notgotitems) {

					while (sents.count <= n)
						[sents addObject:[ServerEntity
						                     entity]];
					sents[n].spawned = true;
				}
			notgotitems = false;
			break;
		}

		case SV_ITEMPICKUP: {
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

void
send_welcome(int n)
{
	ENetPacket *packet =
	    enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(p, SV_INITS2C);
	putint(p, n);
	putint(p, PROTOCOL_VERSION);
	putint(p, *smapname.UTF8String);
	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);



		loopv(sents) if (sents[i].spawned) putint(p, i);

		putint(p, -1);
	}
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	send(n, packet);
}








|











>
>
>
|
>







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

void
send_welcome(int n)
{
	ENetPacket *packet =
	    enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	__block uchar *p = start + 2;
	putint(p, SV_INITS2C);
	putint(p, n);
	putint(p, PROTOCOL_VERSION);
	putint(p, *smapname.UTF8String);
	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) {
			if (e.spawned)
				putint(p, i);
		}];
		putint(p, -1);
	}
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	send(n, packet);
}

398
399
400
401
402
403
404
405
406
407

408
409
410
411
412
413
414
415
416
417
418
419
420
int lastconnect = 0;

void
serverslice(int seconds,
    unsigned int timeout) // main server update, called from cube main loop in
                          // sp, or dedicated server loop
{
	loopv(sents) // spawn entities when timer reached
	{
		if (sents[i].spawnsecs &&

		    (sents[i].spawnsecs -= seconds - lastsec) <= 0) {
			sents[i].spawnsecs = 0;
			sents[i].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) {







|
<
|
>
|
|
|


|







397
398
399
400
401
402
403
404

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
int lastconnect = 0;

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) {
		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) {
530
531
532
533
534
535
536

537
538
539
540
541
542
543

void
initserver(bool dedicated, int uprate, OFString *sdesc, OFString *ip,
    OFString *master, OFString *passwd, int maxcl)
{
	serverpassword = passwd;
	maxclients = maxcl;

	servermsinit(master ? master : @"wouter.fov120.com/cube/masterserver/",
	    sdesc, dedicated);

	if ((isdedicated = dedicated)) {
		ENetAddress address = { ENET_HOST_ANY, CUBE_SERVER_PORT };
		if (ip.length > 0 &&
		    enet_address_set_host(&address, ip.UTF8String) < 0)







>







529
530
531
532
533
534
535
536
537
538
539
540
541
542
543

void
initserver(bool dedicated, int uprate, OFString *sdesc, OFString *ip,
    OFString *master, OFString *passwd, int maxcl)
{
	serverpassword = passwd;
	maxclients = maxcl;
	sents = [[OFMutableArray alloc] init];
	servermsinit(master ? master : @"wouter.fov120.com/cube/masterserver/",
	    sdesc, dedicated);

	if ((isdedicated = dedicated)) {
		ENetAddress address = { ENET_HOST_ANY, CUBE_SERVER_PORT };
		if (ip.length > 0 &&
		    enet_address_set_host(&address, ip.UTF8String) < 0)