-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
2834 lines (2548 loc) · 74.3 KB
/
main.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <unistd.h>
#include <uWS/uWS.h>
#include "Box2D/Box2D.h"
#include <iostream>
// #include <set>
#include <thread>
#include <chrono>
#include <unordered_set>
#include <unordered_map>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fstream>
#include <unistd.h>
#define VERSION "2.14"
#define MAX_PLAYERS 60
#define AREA_PER_PLAYER 1800
#define FLOATERS_PER_PLAYER 30
#define PIXEL_TO_METER_RATIO 27
#define MAX_NAME_LENGTH 22
#define FPS 20
#define FRAMES_PER_SEND 1
#define UNIT_LIMIT 20
#define INACTIVITY_KICK_TIME 120000 // 2 minutes
#define FRAME_FROZEN_TIME 5000
#define DRAIN_TIMEOUT 3600000 // 1 hour
#define RESIZE_INTERVAL 5
#define GLITCH_SPEED 6
#define SHIELD_TIME 30000 // 30 seconds
#define UNIT_WEIGHT 0.006
#define WEIGHT_LIMIT 1
#define INITIAL_ACCEL 60//45
#define BR_MIN_PLAYERS 5
#define BR_AREA_PER_PLAYER 4000
#define BR_TIME 240000 // 4 minutes
#define BR_WALL_DAMAGE 0.1
#define BR_BONUS 100
#define BR_REWARD 10
// #define INITIAL_MAX_SPEED 6
// #define ACCEL_SPEED_RATIO 7.5
// #define SAME_IP_LIMIT 10
#define SOCIAL_NUM 6
#define SOCIAL_BONUS 3
#define CAMERA_LAG 4
#define SCREEN_WIDTH 1040
#define SCREEN_HEIGHT 590
#define SCREEN_RATIO SCREEN_WIDTH / SCREEN_HEIGHT
#define VIEW_PADDING 1.5
#define SCALE_THRESHOLD 4
#define SCALE_MULT 0.008
#define LEADERBOARD_NUM 10
#define LEADERBOARD_UPDATE_INTERVAL 2000
#define MODE_FFA 1
#define MODE_BR 2
#define STATE_WAITING 1
#define STATE_PLAYING 2
#define UNIT_DENSITY 1
#define UNIT_FRICTION 1
#define UNIT_LINEAR_DAMPING 2
#define UNIT_ANGULAR_DAMPING 2
#define FLOATER_SIZE_NUM 3
#define FLOATER_DENSITY 0.15 // 0.3
#define FLOATER_FRICTION 1
#define FLOATER_LINEAR_DAMPING 2
#define FLOATER_ANGULAR_DAMPING 2
#define FLOATER_HP 0.8
#define FLOATER_REGEN 0.0006
#define FLOATER_COLLISION_DAMAGE 0.1
#define FLOATER_KILL_MESSAGE "YSB0cmlhbmdsZQ=="
#define BULLET_DENSITY 0.03 // 0.1
#define BULLET_FRICTION 1
#define INTERFACE_DRAIN 0
#define BYTE_SIZE 1
#define SHORT_SIZE 2
#define INT_SIZE 4
#define FLOAT_SIZE 4
#define CLIENT_CODE_READY 1
#define CLIENT_CODE_KEY 2
#define CLIENT_CODE_PLAY 3
#define CLIENT_CODE_MOUSE 4
#define CLIENT_CODE_BUILD 5
#define CLIENT_CODE_UPGRADE 6
#define CLIENT_CODE_DELETE 7
#define CLIENT_CODE_MOVE 8
#define CLIENT_CODE_PING 9
#define CLIENT_LENGTH_READY 1
#define CLIENT_LENGTH_KEY 3
#define CLIENT_LENGTH_PLAY 3
#define CLIENT_LENGTH_MOUSE 9
#define CLIENT_LENGTH_BUILD 9
#define CLIENT_LENGTH_UPGRADE 3
#define CLIENT_LENGTH_DELETE 3
#define CLIENT_LENGTH_MOVE 9
#define CLIENT_LENGTH_PING 1
#define SERVER_CODE_UPDATE 1
#define SERVER_CODE_JOINED 2
#define SERVER_CODE_INFO 3
#define SERVER_CODE_STASH 4
#define SERVER_CODE_SCALE 5
#define SERVER_CODE_LEADERBOARD 6
#define SERVER_CODE_DIE 7
#define SERVER_CODE_KICK 8
#define SERVER_CODE_SHAKE 9
#define SERVER_CODE_KILL 10
#define SERVER_CODE_RESIZE 11
#define SERVER_CODE_BR_STATE 12
#define SERVER_CODE_BR_RANK 13
#define SHAPE_CIRCLE 1
#define SHAPE_TRIANGLE 2
#define TYPE_UNIT 1
#define TYPE_FLOATER 2
#define TYPE_BULLET 3
#define UNIT_TYPE_NUM 11
#define UNIT_CORE 0
#define UNIT_ALCHEMY_LAB 7
#define UNIT_BOOSTER 8
#define MATERIAL_NUM 6
#define FLOATER_SPAWN_SERIES_LENGTH 31
#define MATERIAL_WOOD 0
#define MATERIAL_IRON 1
#define MATERIAL_GOLD 2
#define MATERIAL_DIAMOND 3
#define MATERIAL_AMETHYST 4
#define MATERIAL_ONYX 5
using namespace std;
using namespace std::chrono;
using std::unordered_set;
typedef char byte;
float sqrtv = sqrt(0.5);
class Object;
class Bullet;
class Unit;
class Floater;
class Player;
typedef struct _pos {
float x;
float y;
} pos;
typedef struct _clientKeys {
bool w;
bool a;
bool s;
bool d;
bool q;
bool e;
} clientKeys;
typedef struct _dataPointer {
int type;
void *data;
} dataPointer;
typedef struct _unitDef {
int cost;
float size;
bool manualActivate;
bool autoActivate;
unsigned long activationReloadTime;
bool buildable;
float hp;
float regen;
float bulletDamage;
float bulletSize;
float bulletSpeed;
float bulletRange;
float collisionDamage;
float heal;
float speedBoost;
float speedBoostUpgrade;
int shape;
float spinAnimation;
} unitDef;
typedef int materialCluster[MATERIAL_NUM];
void loop();
void statusServer();
void resize(int n, int direction);
void fire(Unit *u, float x, float y, float angle);
void updateLeaderboard();
void updateStatus();
void updateRestarts(int change);
float amountScale(float amount, byte material);
float costScale(float amount, byte material);
float rewardScale(float amount, byte material);
float expScale(float amount, byte material);
float linearScale(float amount, byte material);
Unit *findUnitById(unsigned short id);
pos randomSpawnPosition();
float afp(float x1, float y1, float x2, float y2);
pos pfa(float x, float y, float a, float d);
float dfp(float x1, float y1, float x2, float y2);
unsigned long getTime();
float norm(float n);
void wipeInputs(clientKeys *keys, bool *mouse);
void halveStash(int *stash, int socialTotal, string name);
// int createServerSocket(int port);
// int waitForConnection(int serverSocket);
void checkDrain();
void sendKill(Player *killer, Player *killed);
void updateBr();
void emitBrState();
// void eraseAddress(const char *address);
int mode = MODE_FFA;
int brState = STATE_WAITING;
int maxPlayers = MAX_PLAYERS;
int socialBonus = SOCIAL_BONUS;
float floaterRewardMult = 1;
float arenaSize;
float width;
float height;
int desiredFloaters;
bool draining = false;
int restarts = 0;
b2Vec2 gravity(0, 0);
b2World world(gravity, true);
long idc = 0;
long udc = 0;
long fdc = 0;
long bdc = 0;
float floaterSizes[FLOATER_SIZE_NUM] = {0.5, 1, 2};
float floaterSizeChances[FLOATER_SIZE_NUM] = {80, 10, 5};
unsigned long lastFrame = 0;
unsigned long frameCount = 0;
unsigned long averageTime = 1000 / FPS;
unsigned long drainTime = 0;
int totalJoined = 0;
float brShrink = sqrt(BR_AREA_PER_PLAYER * BR_MIN_PLAYERS) / (BR_TIME / (1000 / FPS));
vector<Player *> players;
vector<Unit *> units;
vector<Floater *> floaters;
vector<Bullet *> bullets;
// unordered_map<const char *, long> addresses;
// recursive_mutex playersMutex;
// recursive_mutex unitsMutex;
// recursive_mutex floatersMutex;
int fspi = 0; // Floater spawn series index
byte floaterSpawnSeries[FLOATER_SPAWN_SERIES_LENGTH];
unitDef unitDefs[UNIT_TYPE_NUM] = {
{ // Core
24, // cost
1.2, // size
true, // manual activate
false, // auto activate
500, // reload time
true, // buildable
18 * 2, // hp
0.0003 * 4, // regen
0.5, // bullet damage
0.37, // bullet size
11, // bullet speed
12, // bullet range
0.2, // collision damage
-1, // heal
1, // speed boost
0, // speed boost upgrade
SHAPE_CIRCLE, // shape
0 // spin animation
},
{ // Basic unit
6, // cost
1, // size
false, // manual activate
false, // auto activate
0, // reload time
true, // buildable
12 * 2, // hp
0.0003 * 4, // regen
-1, // bullet damage
-1, // bullet size
-1, // bullet speed
-1, // bullet range
0.1, // collision damage
-1, // heal
0, // boost
0, // boost upgrade
SHAPE_CIRCLE, // shape
0 // spin animation
},
{ // Basic turret
8, // cost
1, // size
true, // manual activate
false, // auto activate
500, // reload time
true, // buildable
6 * 2, // hp
0.0003 * 4, // regen
0.25, // bullet damage
0.37, // bullet size
11, // bullet
12, // bullet range
0.1, // collision damage
-1, // heal
0, // boost
0, // boost upgrade
SHAPE_CIRCLE, // shape
0 // spin animation
},
{ // Sniper turret
10, // cost
1, // size
true, // manual activate
false, // auto activate
800, // reload time
true, // buildable
6 * 2, // hp
0.0003 * 4, // regen
0.25, // bullet damage
0.3, // bullet size
12, // bullet
15, // bullet range
0.1, // collision damage
-1, // heal
0, // boost
0, // speed boost upgrade
SHAPE_CIRCLE, // shape
0 // spin animation
},
{ // Twin turret
10, // cost
1, // size
true, // manual activate
false, // auto activate
250, // reload time
true, // buildable
7 * 2, // hp
0.0003 * 4, // regen
0.2, // bullet damage
0.2, // bullet size
11, // bullet speed
12, // bullet range
0.1, // collision damage
-1, // heal
0, // speed boost
0, // speed boost upgrade
SHAPE_CIRCLE, // shape
0 // spin animation
},
{ // Cannon turret
18, // cost
1, // size
true, // manual activate
false, // auto activate
700, // reload time
true, // buildable
8 * 2, // hp
0.0003 * 4, // regen
0.7, // bullet damage
0.5, // bullet size
10, // bullet speed
10, // bullet range
0.1, // collision damage
-1, // heal
0, // speed boost
0, // speed boost upgrade
SHAPE_CIRCLE, // shape
0 // spin animation
},
{ // Healing unit
14, // cost
1, // size
false, // manual activate
true, // auto activate
2000, // reload time
true, // buildable
10 * 2, // hp
0.0003 * 4, // regen
-1, // bullet damage
-1, // bullet size
-1, // bullet speed
-1, // bullet range
0.1, // collision damage
1, // heal
0, // speed boost
0, // speed boost upgrade
SHAPE_CIRCLE, // shape
0 // spin animation
},
{ // Alchemy lab
20, // cost
1, // size
false, // manual activate
true, // auto activate
2000, // reload time
true, // buildable
10 * 2, // hp
0.0003 * 4, // regen
-1, // bullet damage
-1, // bullet size
-1, // bullet speed
-1, // bullet range
0.1, // collision damage
-1, // heal
0, // speed boost
0, // speed boost upgrade
SHAPE_CIRCLE, // shape
0 // spin animation
},
{ // Booster unit
12, // cost
1, // size
false, // manual activate
false, // auto activate
0, // reload time
true, // buildable
10 * 2, // hp
0.0003 * 4, // regen
-1, // bullet damage
-1, // bullet size
-1, // bullet speed
-1, // bullet range
0.1, // collision damage
-1, // heal
0.1, // speed boost
0.05, // speed boost upgrade
SHAPE_CIRCLE, // shape
0.01 // spin animation
},
{ // Octa Turret
16, // cost
1, // size
true, // manual activate
false, // auto activate
1500, // reload time
true, // buildable
8 * 2, // hp
0.0003 * 4, // regen
0.15, // bullet damage
0.2, // bullet size
11, // bullet speed
10, // bullet range
0.1, // collision damage
-1, // heal
0, // speed boost
0, // speed boost upgrade
SHAPE_CIRCLE, // shape
0 // spin animation
},
{ // Spike
8, // cost
0.5, // size
false, // manual activate
false, // auto activate
0, // reload time
false, // buildable
16 * 2, // hp
0.0003 * 4, // regen
-1, // bullet damage
-1, // bullet size
-1, // bullet speed
-1, // bullet range
0.4, // collision damage
-1, // heal
0, // speed boost
0, // speed boost upgrade
SHAPE_TRIANGLE, // shape
0 // spin animation
}
};
class Object {
public:
unsigned short id;
float x;
float y;
int type;
byte material;
int bodyType;
float rotation;
float size;
float density;
float friction;
float linearDamping;
float angularDamping;
bool active;
b2Body *body;
void createBody();
bool updateBody() {
bool touchingWall = false;
if (body == NULL) {
createBody();
}
b2Vec2 position = body->GetPosition();
rotation = body->GetAngle();
active = body->IsAwake();
x = position.x;
y = position.y;
bool collide;
if (mode == MODE_FFA) {
collide = (type != TYPE_BULLET);
} else if (mode == MODE_BR) {
collide = (type != TYPE_BULLET && type != TYPE_UNIT);
}
if (x < -width / 2 + size || x > width / 2 - size || y < -height / 2 + size || y > height / 2 - size) {
touchingWall = true;
if (collide) {
b2Vec2 v = body->GetLinearVelocity();
if (x < -width / 2 + size) {
x = -width / 2 + size;
v.x = 0;
} else if (x > width / 2 - size) {
x = width / 2 - size;
v.x = 0;
}
if (y < -height / 2 + size) {
y = -height / 2 + size;
v.y = 0;
} else if (y > height / 2 - size) {
y = height / 2 - size;
v.y = 0;
}
body->SetTransform(b2Vec2(x, y), body->GetAngle());
body->SetLinearVelocity(v);
}
}
return touchingWall;
}
void bulletHit(Bullet *bullet);
void collisionHit(int type, void *object);
};
class Bullet: public Object {
public:
Player *owner;
b2Vec2 velocity;
float damage;
float ox;
float oy;
float range;
Bullet(Player *aowner, float ax, float ay, float avx, float avy, byte amaterial, float asize, float adamage, float arange) {
bdc++;
id = bdc;
owner = aowner;
x = ax;
y = ay;
ox = x;
oy = y;
range = arange;
b2Vec2 v;
v.x = avx;
v.y = avy;
velocity = v;
material = amaterial;
size = asize;
bodyType = SHAPE_CIRCLE;
density = BULLET_DENSITY;
friction = BULLET_FRICTION;
linearDamping = 0;
angularDamping = 0;
type = TYPE_BULLET;
damage = adamage;
active = false;
body = NULL;
bullets.push_back(this);
}
~Bullet();
bool update() {
bool touchingWall = updateBody();
if (mode == MODE_BR) {
touchingWall = false;
}
if (touchingWall || dfp(ox, oy, x, y) > range) {
delete this;
return false;
} else {
body->SetLinearVelocity(velocity);
return true;
}
}
};
class Unit: public Object {
public:
Player *owner;
Unit *parent;
byte unitType;
float ox;
float oy;
b2Vec2 v;
float angle;
unsigned long activationReloadTime;
unsigned long lastActivated;
bool buildable;
bool manualActivate;
bool autoActivate;
float hp;
float mhp;
float regen;
float bulletDamage;
float bulletSpeed;
float bulletRange;
float collisionDamage;
float heal;
float speedBoost;
float spinAnimation;
float angleFromParent;
b2Joint* parentJoint;
// recursive_mutex childrenUnitsMutex;
vector<Unit *> childrenUnits;
// Special
int lastFireSide;
byte alchemyCycle;
void updateStats();
Unit(Player *aowner, Unit *aparent, float ax, float ay, float aangle, float aangleFromParent, byte atype, byte amaterial) {
udc++;
id = udc;
owner = aowner;
parent = aparent;
x = ax;
y = ay;
v = {0, 0};
angle = aangle;
type = TYPE_UNIT;
unitType = atype;
material = amaterial;
size = unitDefs[unitType].size;
bodyType = unitDefs[unitType].shape;
density = UNIT_DENSITY;
friction = UNIT_FRICTION;
linearDamping = UNIT_LINEAR_DAMPING;
angularDamping = UNIT_ANGULAR_DAMPING;
manualActivate = unitDefs[unitType].manualActivate;
autoActivate = unitDefs[unitType].autoActivate;
activationReloadTime = unitDefs[unitType].activationReloadTime;
lastActivated = 0;
buildable = unitDefs[unitType].buildable;
speedBoost = 0;
updateStats();
regen = unitDefs[unitType].regen;
bulletSpeed = unitDefs[unitType].bulletSpeed;
bulletRange = unitDefs[unitType].bulletRange;
angleFromParent = aangleFromParent;
active = false;
body = NULL;
parentJoint = NULL;
// Special
lastFireSide = 1;
alchemyCycle = 0;
//unitsMutex.lock();
units.push_back(this);
//unitsMutex.unlock();
}
~Unit();
bool update();
void activate();
bool handleHealth(float damage, Player *player);
};
class Floater: public Object {
public:
float mhp;
float hp;
float regen;
float collisionDamage;
int sizeTier;
byte dyingStage;
unordered_set<Player *> viewedBy;
Floater(byte amaterial, float ax, float ay) {
fdc++;
id = fdc;
x = ax;
y = ay;
sizeTier = 0;
float random = (float)(rand()) /(RAND_MAX / 100);
float chanceCounter = 0;
for (int i = 0; i < FLOATER_SIZE_NUM; i++) {
if (random > chanceCounter && random < chanceCounter + floaterSizeChances[i]) {
sizeTier = i;
break;
}
chanceCounter += floaterSizeChances[i];
}
size = floaterSizes[sizeTier];
material = amaterial;
bodyType = SHAPE_TRIANGLE;
density = FLOATER_DENSITY;
friction = FLOATER_FRICTION;
linearDamping = FLOATER_LINEAR_DAMPING;
angularDamping = FLOATER_ANGULAR_DAMPING;
type = TYPE_FLOATER;
hp = amountScale(FLOATER_HP *(sizeTier + 1), material);
regen = FLOATER_REGEN;
mhp = hp;
collisionDamage = FLOATER_COLLISION_DAMAGE;
active = false;
body = NULL;
dyingStage = 0;
//floatersMutex.lock();
floaters.push_back(this);
//floatersMutex.unlock();
}
~Floater();
bool update(bool emitting) {
if (hp < mhp) {
hp += (mhp * regen);
}
bool touchingWall = updateBody();
if (mode == MODE_BR && touchingWall && dyingStage == 0) {
dyingStage = 1;
if (brState == STATE_WAITING) {
pos position = randomSpawnPosition();
new Floater(material, position.x, position.y);
}
}
if (emitting && dyingStage == 1) {
dyingStage++;
} else if (dyingStage == 2) {
delete this;
return false;
}
return true;
}
void handleHealth(float damage, Player *player);
};
int emitMetaSize = BYTE_SIZE + SHORT_SIZE + SHORT_SIZE + SHORT_SIZE + SHORT_SIZE; // code, number of units, number of floaters, number of bullets, number of players
int unitDataSize = SHORT_SIZE + FLOAT_SIZE + FLOAT_SIZE + FLOAT_SIZE + FLOAT_SIZE + BYTE_SIZE + BYTE_SIZE + SHORT_SIZE + FLOAT_SIZE + FLOAT_SIZE; // id, x, y, size, angle, type, material, owner, hp, rotation
int floaterDataSize = SHORT_SIZE + FLOAT_SIZE + FLOAT_SIZE + FLOAT_SIZE + FLOAT_SIZE + BYTE_SIZE + FLOAT_SIZE; // id, x, y, size, rotation, material, hp
int bulletDataSize = SHORT_SIZE + FLOAT_SIZE + FLOAT_SIZE + FLOAT_SIZE + BYTE_SIZE; // id, x, y, size, material
int playerDataSize = SHORT_SIZE + INT_SIZE + BYTE_SIZE; // id, score, name length
class Player {
public:
unsigned short id;
uWS::WebSocket<uWS::SERVER> *io;
// const char *address;
bool ready;
bool joined;
bool hasPlayed;
bool disconnected;
string name;
int socialTotal;
int score;
float x;
float y;
float ox;
float oy;
float tx;
float ty;
float accel;
// float maxSpeed;
float speedMult;
float weight;
int stashBonus;
materialCluster stash;
clientKeys keys;
bool mouse;
float scale;
bool usingJoystick;
float moveAngle;
float moveThrust;
Unit *core;
// recursive_mutex childrenUnitsMutex;
vector<Unit *> childrenUnits;
// recursive_mutex childrenBulletsMutex;
vector<Bullet *> childrenBullets;
unsigned long lastActivity;
unsigned long timeConnected;
unsigned long lastPing;
unsigned long timeJoined;
bool hasBuilt;
Player(uWS::WebSocket<uWS::SERVER> *connection/*, const char *addr*/) {
id = idc;
idc++;
io = connection;
io->setUserData(this);
// address = addr;
ready = false;
joined = false;
hasPlayed = false;
disconnected = false;
name = "";
socialTotal = 0;
score = 0;
x = width / 2;
y = height / 2;
tx = 0;
ty = 0;
accel = INITIAL_ACCEL;
// maxSpeed = INITIAL_MAX_SPEED;
speedMult = 0;
weight = 0;
keys.w = false;
keys.a = false;
keys.s = false;
keys.d = false;
keys.q = false;
keys.e = false;
mouse = false;
scale = 1;
usingJoystick = false;
moveAngle = 0;
moveThrust = 0;
core = NULL;
lastActivity = 0;
timeConnected = getTime();
lastPing = 0;
timeJoined = 0;
hasBuilt = false;
for (int i = 0; i < MATERIAL_NUM; i++) {
stash[i] = 0;
}
//playersMutex.lock();
players.push_back(this);
//playersMutex.unlock();
}
void deleteAllUnits() {
if (core != NULL) {
delete core; // ! deletes unit
core = NULL;
}
// children//unitsMutex.lock();
for (vector<Unit *>::iterator it = childrenUnits.begin(); it != childrenUnits.end(); ) {
Unit *u = *it;
delete u; // ! deletes unit
}
// children//unitsMutex.unlock();
}
~Player() {
checkDrain();
deleteAllUnits();
//playersMutex.lock();
players.erase(remove(players.begin(), players.end(), this), players.end());
//playersMutex.unlock();
// children//bulletsMutex.lock();
for (vector<Bullet *>::iterator it = childrenBullets.begin(); it != childrenBullets.end(); ++it) {
Bullet *b = *it;
b->owner = NULL;
delete b;
}
// children//bulletsMutex.unlock();
//floatersMutex.lock();
for (vector<Floater *>::iterator it = floaters.begin(); it != floaters.end(); ++it) {
Floater *f = *it;
f->viewedBy.erase(this);
// f->viewedBy.erase(remove(f->viewedBy.begin(), f->viewedBy.end(), this), f->viewedBy.end());
}
//floatersMutex.unlock();
}
void update(float timeMult) {
if (joined) {
if (core != NULL && core->body != NULL) {
ox = x;
oy = y;
x = core->x;
y = core->y;
float nvx = 0;
float nvy = 0;
float total = (accel / (1 + weight)) * speedMult * timeMult;
if (!usingJoystick) {
if (keys.w) {
nvy -= total;
}
if (keys.a) {
nvx -= total;
}
if (keys.s) {
nvy += total;
}
if (keys.d) {
nvx += total;
}
if ((nvx > 0 || nvx < 0) && (nvy > 0 || nvy < 0)) {
nvx *= sqrtv;
nvy *= sqrtv;
}
} else {
pos position = pfa(0, 0, moveAngle, moveThrust * total);
nvx = position.x;
nvy = position.y;
}
b2Vec2 v = core->body->GetLinearVelocity();
// float limit = (maxSpeed - weight) * speedMult;
// if (v.x > limit) {
// nvx = 0;
// }
// if (v.y > limit) {
// nvy = 0;
// }
core->body->ApplyForce(b2Vec2(nvx, nvy), core->body->GetWorldCenter());
double rotationForce = 80 * childrenUnits.size();
if (keys.q && !keys.e) {
core->body->ApplyTorque(-rotationForce);
} else if (keys.e && !keys.q) {
core->body->ApplyTorque(rotationForce);
} else {
core->body->ApplyTorque(0);
}
if (mouse) {
core->activate();
}
// children//unitsMutex.lock();
for (vector<Unit *>::iterator it = childrenUnits.begin(); it != childrenUnits.end(); ++it) {
Unit *u = *it;
if (u->body != NULL) {
u->body->ApplyForce(b2Vec2(nvx, nvy), u->body->GetWorldCenter());
if (mouse && u->manualActivate || u->autoActivate) {
u->activate();
}
}
}
// children//unitsMutex.unlock();
}
}
if (mode == MODE_FFA && lastActivity != 0 && getTime() - lastActivity > INACTIVITY_KICK_TIME) {
kick();
}
if ((lastPing == 0 && getTime() - timeConnected > 10000) || (lastPing != 0 && getTime() - lastPing > 10000)) {
kick();
}
}
void send(byte *data, int size) {
if (!disconnected) {
io->send(data, size, uWS::OpCode::BINARY);
}
}
void giveInfo() {