Convert server_entity to a class
FossilOrigin-Name: 21584a40b686945f20e078af058c7f2ad2f4be197cc4a67335a18329c6fd085b
This commit is contained in:
parent
d42f82f1ec
commit
7162807acd
4 changed files with 45 additions and 26 deletions
9
src/ServerEntity.h
Normal file
9
src/ServerEntity.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#import <ObjFW/ObjFW.h>
|
||||||
|
|
||||||
|
// server side version of "entity" type
|
||||||
|
@interface ServerEntity: OFObject
|
||||||
|
@property (nonatomic) bool spawned;
|
||||||
|
@property (nonatomic) int spawnsecs;
|
||||||
|
|
||||||
|
+ (instancetype)entity;
|
||||||
|
@end
|
8
src/ServerEntity.m
Normal file
8
src/ServerEntity.m
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#import "ServerEntity.h"
|
||||||
|
|
||||||
|
@implementation ServerEntity
|
||||||
|
+ (instancetype)entity
|
||||||
|
{
|
||||||
|
return [[self alloc] init];
|
||||||
|
}
|
||||||
|
@end
|
|
@ -18,6 +18,7 @@ executable('client',
|
||||||
'Projectile.m',
|
'Projectile.m',
|
||||||
'ResolverResult.mm',
|
'ResolverResult.mm',
|
||||||
'ResolverThread.mm',
|
'ResolverThread.mm',
|
||||||
|
'ServerEntity.m',
|
||||||
'ServerInfo.mm',
|
'ServerInfo.mm',
|
||||||
'Variable.mm',
|
'Variable.mm',
|
||||||
'clients.mm',
|
'clients.mm',
|
||||||
|
@ -69,6 +70,7 @@ executable('client',
|
||||||
executable('server',
|
executable('server',
|
||||||
[
|
[
|
||||||
'Client.mm',
|
'Client.mm',
|
||||||
|
'ServerEntity.m',
|
||||||
'server.mm',
|
'server.mm',
|
||||||
'serverms.mm',
|
'serverms.mm',
|
||||||
'serverutil.mm',
|
'serverutil.mm',
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#import "Client.h"
|
#import "Client.h"
|
||||||
#import "Entity.h"
|
#import "Entity.h"
|
||||||
|
#import "ServerEntity.h"
|
||||||
|
|
||||||
enum { ST_EMPTY, ST_LOCAL, ST_TCPIP };
|
enum { ST_EMPTY, ST_LOCAL, ST_TCPIP };
|
||||||
|
|
||||||
|
@ -13,13 +14,7 @@ static OFMutableArray<Client *> *clients;
|
||||||
int maxclients = 8;
|
int maxclients = 8;
|
||||||
static OFString *smapname;
|
static OFString *smapname;
|
||||||
|
|
||||||
// server side version of "entity" type
|
static OFMutableArray<ServerEntity *> *sents;
|
||||||
struct server_entity {
|
|
||||||
bool spawned;
|
|
||||||
int spawnsecs;
|
|
||||||
};
|
|
||||||
|
|
||||||
vector<server_entity> sents;
|
|
||||||
|
|
||||||
// true when map has changed and waiting for clients to send item
|
// true when map has changed and waiting for clients to send item
|
||||||
bool notgotitems = true;
|
bool notgotitems = true;
|
||||||
|
@ -29,11 +24,11 @@ int mode = 0;
|
||||||
void
|
void
|
||||||
restoreserverstate(OFArray<Entity *> *ents)
|
restoreserverstate(OFArray<Entity *> *ents)
|
||||||
{
|
{
|
||||||
loopv(sents)
|
[sents enumerateObjectsUsingBlock:^(
|
||||||
{
|
ServerEntity *e, size_t i, bool *stop) {
|
||||||
sents[i].spawned = ents[i].spawned;
|
e.spawned = ents[i].spawned;
|
||||||
sents[i].spawnsecs = 0;
|
e.spawnsecs = 0;
|
||||||
}
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
int interm = 0, minremain = 0, mapend = 0;
|
int interm = 0, minremain = 0, mapend = 0;
|
||||||
|
@ -116,7 +111,7 @@ disconnect_client(int n, OFString *reason)
|
||||||
void
|
void
|
||||||
resetitems()
|
resetitems()
|
||||||
{
|
{
|
||||||
sents.setsize(0);
|
[sents removeAllObjects];
|
||||||
notgotitems = true;
|
notgotitems = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +119,7 @@ void
|
||||||
pickup(uint i, int sec, int sender) // server side item pickup, acknowledge
|
pickup(uint i, int sec, int sender) // server side item pickup, acknowledge
|
||||||
// first client that gets it
|
// first client that gets it
|
||||||
{
|
{
|
||||||
if (i >= (uint)sents.length())
|
if (i >= (uint)sents.count)
|
||||||
return;
|
return;
|
||||||
if (sents[i].spawned) {
|
if (sents[i].spawned) {
|
||||||
sents[i].spawned = false;
|
sents[i].spawned = false;
|
||||||
|
@ -230,9 +225,9 @@ process(ENetPacket *packet, int sender) // sender may be -1
|
||||||
int n;
|
int n;
|
||||||
while ((n = getint(p)) != -1)
|
while ((n = getint(p)) != -1)
|
||||||
if (notgotitems) {
|
if (notgotitems) {
|
||||||
server_entity se = { false, 0 };
|
while (sents.count <= n)
|
||||||
while (sents.length() <= n)
|
[sents addObject:[ServerEntity
|
||||||
sents.add(se);
|
entity]];
|
||||||
sents[n].spawned = true;
|
sents[n].spawned = true;
|
||||||
}
|
}
|
||||||
notgotitems = false;
|
notgotitems = false;
|
||||||
|
@ -304,7 +299,7 @@ send_welcome(int n)
|
||||||
ENetPacket *packet =
|
ENetPacket *packet =
|
||||||
enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE);
|
enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE);
|
||||||
uchar *start = packet->data;
|
uchar *start = packet->data;
|
||||||
uchar *p = start + 2;
|
__block uchar *p = start + 2;
|
||||||
putint(p, SV_INITS2C);
|
putint(p, SV_INITS2C);
|
||||||
putint(p, n);
|
putint(p, n);
|
||||||
putint(p, PROTOCOL_VERSION);
|
putint(p, PROTOCOL_VERSION);
|
||||||
|
@ -316,7 +311,11 @@ send_welcome(int n)
|
||||||
sendstring(smapname, p);
|
sendstring(smapname, p);
|
||||||
putint(p, mode);
|
putint(p, mode);
|
||||||
putint(p, SV_ITEMLIST);
|
putint(p, SV_ITEMLIST);
|
||||||
loopv(sents) if (sents[i].spawned) putint(p, i);
|
[sents enumerateObjectsUsingBlock:^(
|
||||||
|
ServerEntity *e, size_t i, bool *stop) {
|
||||||
|
if (e.spawned)
|
||||||
|
putint(p, i);
|
||||||
|
}];
|
||||||
putint(p, -1);
|
putint(p, -1);
|
||||||
}
|
}
|
||||||
*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
|
*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
|
||||||
|
@ -402,15 +401,15 @@ serverslice(int seconds,
|
||||||
unsigned int timeout) // main server update, called from cube main loop in
|
unsigned int timeout) // main server update, called from cube main loop in
|
||||||
// sp, or dedicated server loop
|
// sp, or dedicated server loop
|
||||||
{
|
{
|
||||||
loopv(sents) // spawn entities when timer reached
|
// spawn entities when timer reached
|
||||||
{
|
[sents enumerateObjectsUsingBlock:^(
|
||||||
if (sents[i].spawnsecs &&
|
ServerEntity *e, size_t i, bool *stop) {
|
||||||
(sents[i].spawnsecs -= seconds - lastsec) <= 0) {
|
if (e.spawnsecs && (e.spawnsecs -= seconds - lastsec) <= 0) {
|
||||||
sents[i].spawnsecs = 0;
|
e.spawnsecs = 0;
|
||||||
sents[i].spawned = true;
|
e.spawned = true;
|
||||||
send2(true, -1, SV_ITEMSPAWN, i);
|
send2(true, -1, SV_ITEMSPAWN, i);
|
||||||
}
|
}
|
||||||
}
|
}];
|
||||||
|
|
||||||
lastsec = seconds;
|
lastsec = seconds;
|
||||||
|
|
||||||
|
@ -534,6 +533,7 @@ initserver(bool dedicated, int uprate, OFString *sdesc, OFString *ip,
|
||||||
{
|
{
|
||||||
serverpassword = passwd;
|
serverpassword = passwd;
|
||||||
maxclients = maxcl;
|
maxclients = maxcl;
|
||||||
|
sents = [[OFMutableArray alloc] init];
|
||||||
servermsinit(master ? master : @"wouter.fov120.com/cube/masterserver/",
|
servermsinit(master ? master : @"wouter.fov120.com/cube/masterserver/",
|
||||||
sdesc, dedicated);
|
sdesc, dedicated);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue