-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.cpp
377 lines (344 loc) · 11.4 KB
/
client.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* Copyright 2011 by Benjamin J. Land (a.k.a. BenLand100)
*
* This file is part of the CppCraft.
*
* CppCraft is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CppCraft is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CppCraft. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "client.h"
#include "packets.h"
#include "render.h"
#include "tools.h"
#include <cmath>
Client::Client() {
digging = false;
target = beingdug = NULL;
forwards = sideways = 0;
connected = false;
socket = NULL;
doPhysics = false;
physics = NULL;
doPackets = false;
packets = NULL;
us = NULL;
usLock = false;
usLock = SDL_CreateMutex();
}
Client::~Client() {
disconnect();
SDL_DestroyMutex(usLock);
}
void Client::lockUs() {
SDL_mutexP(usLock);
}
void Client::unlockUs() {
SDL_mutexV(usLock);
}
bool Client::connect(char *host, int port) {
if (!socket) {
IPaddress ip;
if (SDLNet_ResolveHost(&ip, host, port) < 0) {
std::cout << "Error resolving host: " << SDLNet_GetError() << '\n';
return false;
}
if (!(socket = SDLNet_TCP_Open(&ip))) {
std::cout << "Error connecting: " << SDLNet_GetError() << '\n';
socket = NULL;
return false;
}
connected = true;
doPackets = true;
packets = SDL_CreateThread((int (*)(void*))packets_thread, this);
return true;
} else {
return false;
}
}
int physics_thread(Client *client) {
double diginc = 0.0;
while (client->doPhysics) {
SDL_Delay(50);
double dt = 0.05;
client->lockUs();
client->retarget();
if (client->digging) {
if (client->target) {
if (client->beingdug != client->target) {
client->digStatus = 0.0;
diginc = incPerTick(-1,client->target->type,false,client->onGround); //TODO should use the held tool and underwater status
client->beingdug = client->target;
send_player_digging(client->socket,0,client->targetx,client->targety,client->targetz,client->targetface);
} else {
client->digStatus += diginc;
if (client->digStatus >= 1.0) { //Should use the pause required by the block
client->beingdug = NULL;
client->digStatus = 0.0;
send_player_digging(client->socket,2,client->targetx,client->targety,client->targetz,client->targetface);
std::cout << "Dug: " << std::dec << client->targetx << ' ' << client->targety << ' ' << client->targetz << ' ' << client->targetface << '\n';
}
}
} else {
client->beingdug = NULL;
}
}
if (client->forwards || client->sideways) {
double fx = cos((client->us->yaw)/180.0*3.14159);
double fz = sin((client->us->yaw)/180.0*3.14159);
client->us->vx = -fz*client->sideways+fx*client->forwards;
client->us->vz = fx*client->sideways+fz*client->forwards;
} else {
client->us->vx = 0;
client->us->vz = 0;
}
client->us->vy -= 30*dt;
if (client->us->vx > 5.0) client->us->vx = 5.0;
if (client->us->vy > 10.0) client->us->vy = 5.0;
if (client->us->vz > 5.0) client->us->vz = 5.0;
//bounding box in block coords
int sx,sy,sz,ex,ey,ez;
client->us->boundingBox(sx,sy,sz,ex,ey,ez);
if (client->us->vx != 0) {
client->us->x += client->us->vx*dt;
int x = floor(client->us->x + (client->us->vx < 0 ? -1.0 : 1.0)*client->us->apothem);
if (client->world.containsSolid(x,sy,sz,x,ey,ez)) {
client->us->x = x + ((client->us->vx > 0) ? -client->us->apothem : 1+client->us->apothem) * 1.001; //adjust slightly
client->us->vx = 0;
}
}
if (client->us->vz != 0) {
client->us->z += client->us->vz*dt;
int z = floor(client->us->z + (client->us->vz < 0 ? -1.0 : 1.0)*client->us->apothem);
if (client->world.containsSolid(sx,sy,z,ex,ey,z)) {
client->us->z = z + ((client->us->vz > 0) ? -client->us->apothem : 1+client->us->apothem) * 1.001; //adjust slightly
client->us->vz = 0;
}
}
client->onGround = false;
if (client->us->vy != 0) {
client->us->y += client->us->vy*dt;
//position y at our feet or head relative to velocity
int y = floor(client->us->y + ((client->us->vy > 0) ? 1.74 : 0));
if (client->world.containsSolid(sx,y,sz,ex,y,ez)) {
client->us->y = y + ((client->us->vy > 0) ? -1.74 : 1) * 1.001; //adjust slightly above/below
if (client->us->vy < 0) client->onGround = true;
client->us->vy = 0;
}
}
client->sendPos();
client->unlockUs();
}
return 0;
}
void Client::packet(p_generic *p) {
switch (p->id) {
case 0x00:
send_keep_alive(socket);
break;
case 0x01:
std::cout << "Logged In! EID: " << std::dec <<((p_login_request_stc*)p)->EntityID << '\n';
break;
case 0x02:
send_login_request_cts(socket,14,us->name,0,0);
break;
case 0x03:
std::cout << "Chat: " << ((p_chat_message*)p)->Message << '\n';
break;
case 0x08:
if (((p_update_health*)p)->Health <= 0) {
lockUs();
std::cout << "We died, respawning...\n";
send_respawn(socket,0);
unlockUs();
}
break;
case 0x0D:
{
p_player_position_and_look_stc *pos = (p_player_position_and_look_stc*)p;
lockUs();
onGround = pos->OnGround;
us->x = pos->X;
us->y = pos->Y;
us->z = pos->Z;
us->height = pos->Stance - pos->Y;
us->pitch = pos->Pitch;
us->yaw = pos->Yaw;
sendPos();
Chunk *c = world.getChunk(us->x,us->y,us->z); if (c) c->markDirty();
unlockUs();
}
if (!physics) {
doPhysics = true;
physics = SDL_CreateThread((int (*)(void*))physics_thread, this);
}
break;
case 0x18:
break;
case 0x1C:
case 0x1D:
case 0x1E:
case 0x1F:
case 0x20:
case 0x21:
case 0x22:
case 0x26:
break; //ignore entity motion stuff for now
case 0x32:
{
p_prechunk *prechunk = (p_prechunk*)p;
if (!prechunk->Mode) world.deleteChunk(prechunk->X,0,prechunk->Z);
}
break;
case 0x33:
{
p_map_chunk *update = (p_map_chunk*)p;
bool res = world.updateChunk(update->X,update->Y,update->Z,update->SizeX,update->SizeY,update->SizeZ,update->CompressedSize,update->CompressedData);
if (!res) {
std::cout << "Error performing chunk update\n";
disconnect();
}
}
break;
case 0x34:
{
p_multi_block_change *mbc = (p_multi_block_change*)p;
bool res = world.updateChunk(mbc->ChunkX,0,mbc->ChunkZ,mbc->ArraySize,mbc->CoordinateArray,mbc->TypeArray,mbc->MetadataArray);
//Ignore if the chunk is not currently loaded...?
}
break;
case 0x35:
{
p_block_change *change = (p_block_change*)p;
Block *b = world.getBlock(change->X,change->Y,change->Z);
if (b) { //Ignore if the chunk is not currently loaded...?
b->type = change->Type;
b->meta = change->Metadata;
world.updateLighting(change->X,change->Y,change->Z);
}
}
break;
case 0xFF:
std::cout << "KICK: " << ((p_kick*)p)->Message << '\n';
disconnect();
break;
default:
std::cout << "Unhandled Packet: 0x" << std::hex << (int)p->id << '\n';
}
}
void Client::disconnect() {
doPackets = false;
doPhysics = false;
if (physics) SDL_WaitThread(physics, NULL);
if (packets) SDL_WaitThread(packets, NULL);
physics = NULL;
packets = NULL;
connected = false;
if (socket) SDLNet_TCP_Close(socket);
socket = NULL;
if (us) delete us;
us = NULL;
world.clearChunks();
}
bool Client::login(char *username) {
if (!us) {
us = new Player(username);
send_handshake_cts(socket,us->name);
return true;
}
}
void Client::startDigging() {
lockUs();
beingdug = NULL;
digging = true;
unlockUs();
}
void Client::stopDigging() {
lockUs();
digging = false;
beingdug = NULL;
unlockUs();
}
void Client::placeHeld() {
//TODO formulate and send the block place packet
std::cout << "Place here...\n";
}
void Client::jump() {
lockUs();
if (onGround) {
us->vy = 10.0;
}
unlockUs();
}
void Client::relLook(double dpitch, double dyaw) {
lockUs();
us->pitch += dpitch;
us->yaw += dyaw;
if (us->pitch > 90.0) us->pitch = 90.0;
if (us->pitch < -90.0) us->pitch = -90.0;
if (us->yaw > 360.0 || us->yaw < 0) us->yaw = fmod(us->yaw,360.0);
retarget();
unlockUs();
}
void Client::setMotion(double forwards, double sideways) {
lockUs();
this->forwards = forwards;
this->sideways = sideways;
unlockUs();
}
Block* Client::getTarget(int &x, int &y, int &z, int &face) {
x = targetx;
y = targety;
z = targetz;
face = targetface;
return target;
}
bool Client::running() {
return connected;
}
void Client::sendPos() {
lockUs();
send_player_position_and_look_cts(socket,us->x,us->y,us->height+us->y,us->z,us->yaw,us->pitch,onGround);
unlockUs();
}
void Client::retarget() {
target = world.projectToBlock(us->x,us->y+us->height,us->z,us->pitch,us->yaw,targetx,targety,targetz,targetface);
}
void Client::init() {
if (SDLNet_Init() < 0) {
std::cout << "Failed to init SDLNet: " << SDLNet_GetError() <<'\n';
exit(1);
}
}
void Client::quit() {
SDLNet_Quit();
}
int main(int argc, char** argv) {
Client::init();
initRender();
Client *c = new Client();
if (c->connect((char*)"localhost")) {
if (c->login((char*)"YourMom")) {
while (c->running()) {
SDL_Delay(10);
renderWorld(c);
processEvents(c);
}
}
}
delete c;
quitRender();
Client::quit();
std::cout << "Finished!\n";
return 0;
}