1
2
3
4
5
6
7
8
9
10
11
12
13
|
// client.cpp, mostly network related client game code
#include "cube.h"
#import "Command.h"
#import "DynamicEntity.h"
static ENetHost *clienthost = NULL;
static int connecting = 0;
static int connattempts = 0;
static int disconnecting = 0;
// our client id in the game
int clientnum = -1;
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// client.cpp, mostly network related client game code
#include "cube.h"
#import "Command.h"
#import "Player.h"
static ENetHost *clienthost = NULL;
static int connecting = 0;
static int connattempts = 0;
static int disconnecting = 0;
// our client id in the game
int clientnum = -1;
|
︙ | | | ︙ | |
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
newname(OFString *name)
{
c2sinit = false;
if (name.length > 16)
name = [name substringToIndex:16];
player1.name = name;
}
COMMAND(name, ARG_1STR, ^(OFString *name) {
newname(name);
})
static void
newteam(OFString *name)
{
c2sinit = false;
if (name.length > 5)
name = [name substringToIndex:5];
player1.team = name;
}
COMMAND(team, ARG_1STR, ^(OFString *name) {
newteam(name);
})
void
writeclientinfo(OFStream *stream)
{
[stream writeFormat:@"name \"%@\"\nteam \"%@\"\n", player1.name,
player1.team];
}
void
connects(OFString *servername)
{
disconnect(true, false); // reset state
addserver(servername);
|
|
|
|
|
|
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
newname(OFString *name)
{
c2sinit = false;
if (name.length > 16)
name = [name substringToIndex:16];
Player.player1.name = name;
}
COMMAND(name, ARG_1STR, ^(OFString *name) {
newname(name);
})
static void
newteam(OFString *name)
{
c2sinit = false;
if (name.length > 5)
name = [name substringToIndex:5];
Player.player1.team = name;
}
COMMAND(team, ARG_1STR, ^(OFString *name) {
newteam(name);
})
void
writeclientinfo(OFStream *stream)
{
[stream writeFormat:@"name \"%@\"\nteam \"%@\"\n", Player.player1.name,
Player.player1.team];
}
void
connects(OFString *servername)
{
disconnect(true, false); // reset state
addserver(servername);
|
︙ | | | ︙ | |
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
conoutf(@"disconnected");
clienthost = NULL;
connecting = 0;
connattempts = 0;
disconnecting = 0;
clientnum = -1;
c2sinit = false;
player1.lifeSequence = 0;
[players removeAllObjects];
localdisconnect();
if (!onlyclean) {
stop();
localconnect();
|
|
|
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
conoutf(@"disconnected");
clienthost = NULL;
connecting = 0;
connattempts = 0;
disconnecting = 0;
clientnum = -1;
c2sinit = false;
Player.player1.lifeSequence = 0;
[players removeAllObjects];
localdisconnect();
if (!onlyclean) {
stop();
localconnect();
|
︙ | | | ︙ | |
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
disconnect(0, !disconnecting);
}
static OFString *ctext;
void
toserver(OFString *text)
{
conoutf(@"%@:\f %@", player1.name, text);
ctext = text;
}
COMMAND(echo, ARG_VARI, ^(OFString *text) {
conoutf(@"%@", text);
})
COMMAND(say, ARG_VARI, ^(OFString *text) {
|
|
|
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
disconnect(0, !disconnecting);
}
static OFString *ctext;
void
toserver(OFString *text)
{
conoutf(@"%@:\f %@", Player.player1.name, text);
ctext = text;
}
COMMAND(echo, ARG_VARI, ^(OFString *text) {
conoutf(@"%@", text);
})
COMMAND(say, ARG_VARI, ^(OFString *text) {
|
︙ | | | ︙ | |
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
enet_host_flush(clienthost);
} else
localclienttoserver((ENetPacket *)packet);
}
// send update to the server
void
c2sinfo(DynamicEntity *d)
{
if (clientnum < 0)
return; // we haven't had a welcome message from the server yet
if (lastmillis - lastupdate < 40)
return; // don't update faster than 25fps
ENetPacket *packet = enet_packet_create(NULL, MAXTRANS, 0);
unsigned char *start = packet->data;
|
|
|
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
enet_host_flush(clienthost);
} else
localclienttoserver((ENetPacket *)packet);
}
// send update to the server
void
c2sinfo(Player *d)
{
if (clientnum < 0)
return; // we haven't had a welcome message from the server yet
if (lastmillis - lastupdate < 40)
return; // don't update faster than 25fps
ENetPacket *packet = enet_packet_create(NULL, MAXTRANS, 0);
unsigned char *start = packet->data;
|
︙ | | | ︙ | |
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
ctext = @"";
}
// tell other clients who I am
if (!c2sinit) {
packet->flags = ENET_PACKET_FLAG_RELIABLE;
c2sinit = true;
putint(&p, SV_INITC2S);
sendstring(player1.name, &p);
sendstring(player1.team, &p);
putint(&p, player1.lifeSequence);
}
for (OFData *msg in messages) {
// send messages collected during the previous frames
if (*(int *)[msg itemAtIndex:1])
packet->flags = ENET_PACKET_FLAG_RELIABLE;
for (int i = 0; i < *(int *)[msg itemAtIndex:0]; i++)
putint(&p, *(int *)[msg itemAtIndex:i + 2]);
|
|
|
|
|
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
ctext = @"";
}
// tell other clients who I am
if (!c2sinit) {
packet->flags = ENET_PACKET_FLAG_RELIABLE;
c2sinit = true;
putint(&p, SV_INITC2S);
sendstring(Player.player1.name, &p);
sendstring(Player.player1.team, &p);
putint(&p, Player.player1.lifeSequence);
}
for (OFData *msg in messages) {
// send messages collected during the previous frames
if (*(int *)[msg itemAtIndex:1])
packet->flags = ENET_PACKET_FLAG_RELIABLE;
for (int i = 0; i < *(int *)[msg itemAtIndex:0]; i++)
putint(&p, *(int *)[msg itemAtIndex:i + 2]);
|
︙ | | | ︙ | |