-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
1675 lines (1493 loc) · 50.4 KB
/
server.cpp
File metadata and controls
1675 lines (1493 loc) · 50.4 KB
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
/* By Tyler Clarke
The server for MMOSG, a multiplayer strategy game (basically Rampart + Galaga).
Please give credit if you copy or otherwise use anything in here!
The structure:
FPS times per second, the game is simulated another tick.
Every time something changes, all the connected Clients are sent some information on what happened, such as
*/
#define PI 3.141592
#define CROW_ENFORCE_WS_SPEC
#include <crow.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#include <vector>
#include <mutex>
#include <functional>
#include "logo.h"
#include "terminalstuff.hpp"
#include <signal.h>
#include "vector.hpp"
#define FPS 30
const float ALLOWED_MICROS_PER_FRAME = 1000000.f/FPS;
char* code;
unsigned int counter = 1;
unsigned int livePlayerCount = 0;
bool stratChangeMode = false;
bool playing = false;
unsigned long gameSize = 5000;
long millisPerTick = 0;
std::string serverName = "StrategyGameMMO";
CleverTerminal terminal;
bool isAutonomous = false;
int autonomousMaxPlayers = 0;
int autonomousMinPlayers = 0;
int autonomousStartTimer = 0;
int autonomousTimer = 0;
bool teamMode = false;
unsigned int terrainSeed;
std::vector<std::string> banners;
long clamp (long min, long val, long max){
if (val < min){
val = min;
}
if (val > max){
val = max;
}
return val;
}
struct team {
long TLbanner = -1;
size_t banner;
char* password;
bool refPassed = false;
~team(){
if (!refPassed){
free(password);
}
}
team(team& t){
t.refPassed = true;
password = t.password;
banner = t.banner;
}
team(size_t bann, char* passwd) {
banner = bann;
password = passwd;
}
};
std::vector<team*> teams;
void broadcast(std::string broadcast);
size_t getTeamOf(size_t banner);
void addBanner(std::string thing, size_t team = 0){
banners.push_back(thing);
broadcast((std::string)"b" + std::to_string(banners.size() - 1) + " " + thing + (teamMode ? " " + std::to_string(teams[team] -> banner) : "")); // b = add banner
}
template <int length = 32>
char* randCode(){
const char* characters = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#&";
const int len = strlen(characters);
char* ret = (char*)malloc(length);
for (int x = 0; x < length; x ++){
ret[x] = characters[rand() % len];
}
return ret;
}
double coterminal(double angle, double about = PI * 2){
while (angle < 0){
angle += about;
}
while (angle >= about){
angle -= about;
}
return angle;
}
double errorify(double one, double two, double about = PI * 2) {
one = coterminal(one, about);
two = coterminal(two, about);
double error = coterminal(two - one, about);
if (error < PI){
error = error - PI * 2;
}
return error;
}
std::vector<std::string> splitString(std::string str, char delim){
std::vector<std::string> ret;
std::string buf;
bool escape;
for (char c : str){
if ((c == delim) && (!escape)){
ret.push_back(buf);
buf = "";
}
else if ((c == '\\') && (!escape)){
escape = true;
}
else{
buf += c;
escape = false;
}
}
if (buf.size() != 0){
ret.push_back(buf);
}
return ret;
}
struct Box {
double x1;
double y1;
double x2;
double y2;
bool check(Box other){
return (other.x1 < x2) && (other.x2 > x1) && (other.y1 < y2) && (other.y2 > y1);
}
};
bool listContains(std::vector<auto> list, auto item) {
for (auto& thing : list){
if (thing == item){
return true;
}
}
return false;
}
struct Client;
struct GameObject {
double health = 1;
double damage = 1;
long banner = -1;
static unsigned long topId;
unsigned long id = -1;
double x = 0;
double y = 0;
float angle = 0;
long goalX = 0;
long goalY = 0;
float goalAngle = 0;
bool rm = false;
Client* owner = NULL;
GameObject* killer = NULL;
std::function <void(GameObject*)> lostFunction;
bool hasLostCallback = false;
void setLostCallback(std::function<void(GameObject*)> function){
lostFunction = function;
hasLostCallback = true;
}
virtual ~GameObject () {
broadcast((std::string)"d" + std::to_string(id));
}
virtual void update() {
}
virtual Box box () {
return { 0, 0, 0, 0 };
}
virtual char identify() {
return 'g';
}
virtual bool editable() { // All things have the same properties: position and rotation. Not one or the other, both or neither.
return false;
}
virtual void destroy() {
rm = true;
if (hasLostCallback){
lostFunction(this);
}
}
virtual void greet() {
// Extend to "greet" new clients
}
void shoot(float angle, float speed = 20, float dist = 50, int duration = -1);
};
std::vector <GameObject*> objects;
class FortObject : public GameObject {
public:
void destroy() {
rm = true;
}
void update() {
}
Box box(){
return { x - 10, y - 10, x + 10, y + 10 };
}
char identify() {
return 'F';
}
};
class CastleObject : public GameObject {
public:
vector velocity;
bool isRTF = false;
float angularVelocity = 0;
long shootCycle = 0;
CastleObject(){
health = 3;
}
void enableRTF(){
health = 3;
isRTF = true;
}
std::vector<GameObject*> forts;
void update() {
shootCycle --;
for (size_t f = 0; f < forts.size(); f ++){
if (forts[f] -> rm){
forts.erase(forts.begin() + f);
f --;
}
}
if (isRTF){
if (velocity.magnitude() > 20){
velocity.setMagnitude(20);
}
x += velocity.x;
y += velocity.y;
x = coterminal(x, gameSize);
y = coterminal(y, gameSize);
angle += angularVelocity;
angularVelocity *= 0.9;
broadcast((std::string)"M" + std::to_string(id) + " " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(angle + PI/2));
}
}
void rtf(bool forwards, bool left, bool right, bool airbrake, bool shewt){
if (airbrake){
velocity.setMagnitude(velocity.magnitude() * 0.8);
}
vector thrust;
thrust.setMandA(forwards * 2, angle);
velocity += thrust;
angularVelocity += (left * -1 + right) * 0.02;
if (shewt){
if (shootCycle < 0){
shootCycle = 15;
shoot(angle, 35, 50);
}
}
}
void destroy () {
if (forts.size() > 0) {
x = forts[0] -> x;
y = forts[0] -> y;
forts[0] -> rm = true;
forts.erase(forts.begin());
broadcast((std::string)"M" + std::to_string(id) + " " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(angle));
}
else {
rm = true;
if (hasLostCallback){
lostFunction(this);
}
}
}
Box box(){
return { x - 25, y - 25, x + 25, y + 25 };
}
char identify(){
return isRTF ? 'R' : 'c';
}
};
class BasicFighterObject : public GameObject {
float xv = 0;
float yv = 0;
int shootTimer = 20;
void update() {
shootTimer --;
if (shootTimer < 0){
shootTimer = 30;
shoot(angle);
}
double dx = goalX - x;
double dy = goalY - y;
if (((dx * dx) + (dy * dy)) > 10 * 10){
angle = atan(dy/dx);
if (dx <= 0){
angle += PI;
}
xv += cos(angle) * 0.25;
yv += sin(angle) * 0.25;
}
else {
angle = goalAngle;
}
x += xv;
y += yv;
xv *= 0.95;
yv *= 0.95;
broadcast((std::string)"M" + std::to_string(id) + " " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(angle));
}
char identify() {
return 'f';
}
bool editable() {
return true;
}
Box box (){
return { x - 10, y - 10, x + 10, y + 10 };
}
};
class HypersonicMissileObject : public GameObject {
float xv = 0;
float yv = 0;
void update() {
double dx = goalX - x;
double dy = goalY - y;
float goToAngle = goalAngle;
if (((dx * dx) + (dy * dy)) > 10 * 10){
goToAngle = atan(dy/dx);
if (dx <= 0){
goToAngle += PI;
}
}
angle = angle * 0.9 + goToAngle * 0.1;
xv += cos(angle) * 0.3;
yv += sin(angle) * 0.3;
xv *= 0.99;
yv *= 0.99;
x += xv;
y += yv;
broadcast((std::string)"M" + std::to_string(id) + " " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(angle));
}
char identify() {
return 'h';
}
bool editable() {
return true;
}
Box box (){
return { x - 3, y - 3, x + 3, y + 3 };
}
};
class TieFighterObject : public GameObject {
float xv = 0;
float yv = 0;
int shootTimer = 30;
void update() {
shootTimer --;
if (shootTimer < 0){
shootTimer = 40;
shoot(angle);
shoot(angle + PI);
}
double dx = goalX - x;
double dy = goalY - y;
if (((dx * dx) + (dy * dy)) > 10 * 10){
angle = atan(dy/dx);
if (dx <= 0){
angle += PI;
}
xv += cos(angle) * 0.35;
yv += sin(angle) * 0.35;
}
else {
angle = goalAngle;
}
x += xv;
y += yv;
xv *= 0.95;
yv *= 0.95;
broadcast((std::string)"M" + std::to_string(id) + " " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(angle));
}
char identify() {
return 't';
}
bool editable() {
return true;
}
Box box (){
return { x - 10, y - 10, x + 10, y + 10 };
}
};
class SniperObject : public GameObject {
float xv = 0;
float yv = 0;
int shootTimer = 60;
void update() {
shootTimer --;
if (shootTimer < 0){
shootTimer = 80; // They shoot far less often than the other guys
shoot(angle, 25, 20, 90); // 5 faster than regular bullets, and they last 3 times longer (they're snipers)
}
double dx = goalX - x;
double dy = goalY - y;
if (((dx * dx) + (dy * dy)) > 10 * 10){
angle = atan(dy/dx);
if (dx <= 0){
angle += PI;
}
xv += cos(angle) * 1.2;
yv += sin(angle) * 1.2;
}
else {
angle = goalAngle;
}
x += xv;
y += yv;
xv *= 0.9;
yv *= 0.9;
broadcast((std::string)"M" + std::to_string(id) + " " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(angle));
}
char identify() {
return 's';
}
bool editable() {
return true;
}
Box box (){
return { x - 10, y - 10, x + 10, y + 10 };
}
};
class BulletObject : public GameObject {
public:
float xv = 0;
float yv = 0;
int TTL = 30; // they last the same number of ticks, even if you speed up the framerate
char identify(){
return 'b';
}
void update() {
TTL --;
if (TTL < 0){
rm = true;
}
y += yv;
x += xv;
broadcast((std::string)"M" + std::to_string(id) + " " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(angle));
}
Box box () {
return { x - 5, y - 5, x + 5, y + 5 };
}
};
class RadiationObject : public GameObject {
public:
int halflife = 150;
double startingStrength = 0.5;
unsigned long cycle = 0;
Box mBox { -200, -200, 200, 200 };
RadiationObject() {
health = 10000000;
}
char identify(){
return 'r';
}
void update() {
double strength = std::pow(0.5, ((double)cycle)/halflife) * startingStrength;
broadcast("r" + std::to_string(id) + " " + std::to_string(strength));
cycle ++;
if (strength < 0.01){
rm = true;
}
damage = strength/15;
}
void destroy(){
// do nothing! Radiation can't be killed
}
Box box () {
return { x + mBox.x1, y + mBox.y1, x + mBox.x2, y + mBox.y2 };
}
void upd8Box () {
broadcast("█" + std::to_string(id) + " " + std::to_string(mBox.x1) + " " + std::to_string(mBox.y1) + " " + std::to_string(mBox.x2) + " " + std::to_string(mBox.y2));
}
void greet() {
upd8Box();
}
};
class NukeObject : public GameObject {
public:
~NukeObject(){
broadcast((std::string)"d" + std::to_string(id));
blowUP();
}
float xv = 0;
float yv = 0;
int explodeTimer = 500;
void blowUP();
void update() {
explodeTimer --;
if (explodeTimer < 0){
destroy();
}
double dx = goalX - x;
double dy = goalY - y;
if (((dx * dx) + (dy * dy)) > 10 * 10){
angle = atan(dy/dx);
if (dx <= 0){
angle += PI;
}
xv += cos(angle) * 0.1;
yv += sin(angle) * 0.1;
}
else {
angle = goalAngle;
}
x += xv;
y += yv;
xv *= 0.999;
yv *= 0.999;
broadcast((std::string)"M" + std::to_string(id) + " " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(angle));
}
void destroy() {
terminal.printLn("destroying");
broadcast("j100");
if (hasLostCallback){
lostFunction(this);
}
rm = true;
}
char identify() {
return 'n';
}
bool editable() {
return true;
}
Box box (){
return { x - 10, y - 10, x + 10, y + 10 };
}
};
class WallObject : public GameObject {
public:
WallObject() {
health = 2;
}
char identify() {
return 'w';
}
int TTL = 600;
void update() {
TTL --;
if (TTL < 0){
rm = true;
}
}
Box box () {
return { x - 15, y - 15, x + 15, y + 15 };
}
};
class TurretObject : public GameObject {
double angleV = 0;
int shootTimer = 20;
char identify() {
return 'T';
}
void update() {
GameObject* closestShootah = NULL;
double bestDistance = 500 * 500; // It won't seek ships more than 500 away
double bestDx = 0;
double bestDy = 0;
for (GameObject* obj : objects){
if (obj -> owner != owner){
char id = obj -> identify();
if ((id != 'b') && (id != 'T') && (id != 'w') && (id != 'c') && (id != 'C')){
double dX = obj -> x - x;
double dY = obj -> y - y;
double d = dX * dX + dY * dY;
if (d < bestDistance){
bestDistance = d;
closestShootah = obj;
bestDx = dX;
bestDy = dY;
}
}
}
}
if (closestShootah != NULL){
goalAngle = atan2(bestDy, bestDx);
shootTimer --;
if (shootTimer < 0){
shoot(angle);
shootTimer = 30;
}
}
angleV += errorify(angle, goalAngle) * -0.005;
angleV *= 0.85;
angle += angleV;
broadcast((std::string)"M" + std::to_string(id) + " " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(angle));
}
Box box (){
return { x - 10, y - 10, x + 10, y + 10 };
}
};
void addObject(GameObject* object);
struct Client {
crow::websocket::connection& conn;
std::mutex sendMutex;
std::mutex processingMutex; // For generic INTERNAL tasks
bool is_authorized = false;
bool hasPlacedCastle = false;
CastleObject* deMoi = new CastleObject;
std::vector <GameObject*> myFighters;
long score = 0;
size_t myBanner = -1;
size_t myTeam = -1;
char arrangement = 'k';
void collect(int amount){
score += amount;
sendText("S" + std::to_string(score));
}
~Client (){
for (GameObject* obj : myFighters){
obj -> owner = NULL;
obj -> hasLostCallback = false;
}
if (is_authorized){
livePlayerCount --;
}
}
void sendText(std::string text){
sendMutex.lock();
conn.send_text(text);
sendMutex.unlock();
}
void process(std::string message){ // INSIDE A CROW THREAD
if (message == "_"){ // ping
sendText("_"); // pong
return;
}
char command = message[0];
std::vector<std::string> args = splitString(message.substr(1, message.size() - 1), ' ');
processingMutex.lock();
if (command == 'c'){
if ((!isAutonomous || (livePlayerCount < autonomousMaxPlayers )) && (args[0] != "_spectator") && (!playing)){ // If it's not autonomous OR the current player count is under max AND the first argument (code) is not spectator mode, and we aren't playing.
while (listContains(banners, args[1])){
args[1] += ".copy";
}
if (teamMode){
team* theTeam = NULL;
for (team* t : teams){
if (t -> password == args[0]) {
theTeam = t;
break;
}
}
if (theTeam) {
terminal.printLn("New player logged in with valid team " + banners[theTeam -> banner] + " access code.");
sendText("s");
is_authorized = true;
livePlayerCount ++;
addBanner(args[1], theTeam -> banner);
myTeam = theTeam -> banner;
myBanner = banners.size() - 1;
if (theTeam -> TLbanner == -1){
theTeam -> TLbanner = myBanner;
sendText("s1");
}
metadata();
terminal.printLn("New banner " + args[1]);
}
else {
terminal.printLn("Player failed to log in - has the wrong code?");
sendText("e0"); // ERROR 0 invalid code
}
}
else {
if (args[0] == code){
terminal.printLn("New player logged in with access code!");
sendText("s"); // SUCCESS
is_authorized = true;
livePlayerCount ++;
addBanner(args[1]);
myBanner = banners.size() - 1;
metadata();
terminal.printLn("New banner " + args[1]);
}
else{
terminal.printLn("Player failed to log in - has the wrong code?");
sendText("e0"); // ERROR 0 invalid code
}
}
arrangement = args[2][0];
if (args[2] == "p"){
deMoi -> enableRTF();
}
}
else{
terminal.printLn("A spectator entered the arena!");
sendText("w0"); // WARNING 0 you are a spectator
metadata();
}
}
else if (is_authorized){
if (command == 'p'){
if (args[0] == "c"){
if (!hasPlacedCastle){
hasPlacedCastle = true;
deMoi -> x = std::stoi(args[1]);
deMoi -> y = std::stoi(args[2]);
add(deMoi);
collect(50);
fighters();
}
else{
terminal.printLn("Some idiot just tried to hack this system");
}
}
else if (args[0] == "w"){
WallObject* w = new WallObject;
w -> x = std::stoi(args[1]);
w -> y = std::stoi(args[2]);
add(w);
}
else if (args[0] == "f"){
if (score >= 10){
BasicFighterObject* f = new BasicFighterObject;
f -> x = std::stoi(args[1]);
f -> y = std::stoi(args[2]);
f -> goalX = f -> x;
f -> goalY = f -> y;
add(f);
collect(-10);
}
}
else if (args[0] == "t"){
if (score >= 20){
TieFighterObject* f = new TieFighterObject;
f -> x = std::stoi(args[1]);
f -> y = std::stoi(args[2]);
f -> goalX = f -> x;
f -> goalY = f -> y;
add(f);
collect(-20);
}
}
else if (args[0] == "s"){
if (score >= 30){
SniperObject* f = new SniperObject;
f -> x = std::stoi(args[1]);
f -> y = std::stoi(args[2]);
f -> goalX = f -> x;
f -> goalY = f -> y;
add(f);
collect(-30);
}
}
else if (args[0] == "h"){
if (score >= 5){
HypersonicMissileObject* f = new HypersonicMissileObject;
f -> x = std::stoi(args[1]);
f -> y = std::stoi(args[2]);
f -> goalX = f -> x;
f -> goalY = f -> y;
add(f);
collect(-5);
}
}
else if (args[0] == "n"){
if (score >= 150){
NukeObject* f = new NukeObject;
f -> x = std::stoi(args[1]);
f -> y = std::stoi(args[2]);
f -> goalX = f -> x;
f -> goalY = f -> y;
add(f);
collect(-150);
}
}
else if (args[0] == "T"){
if (score >= 100){
TurretObject* f = new TurretObject;
f -> x = std::stoi(args[1]);
f -> y = std::stoi(args[2]);
f -> goalX = f -> x;
f -> goalY = f -> y;
add(f);
collect(-100);
}
}
else if (args[0] == "F"){
if (score >= 120){
FortObject* f = new FortObject;
f -> x = std::stoi(args[1]);
f -> y = std::stoi(args[2]);
f -> goalX = f -> x;
f -> goalY = f -> y;
add(f);
collect(-120);
}
}
}
else if (command == 'm'){
for (GameObject* obj : myFighters){
if (obj -> id == std::stoi(args[0])) {
obj -> goalX = std::stoi(args[1]);
obj -> goalY = std::stoi(args[2]);
obj -> goalAngle = std::stod(args[3]);
}
}
}
else if (command == 'C'){
long monies = std::abs(std::stoi(args[0]));
collect(-monies);
}
else if (command == 'R') {
if (deMoi -> isRTF){
deMoi -> rtf(args[0][0] == '1', args[0][1] == '1', args[0][2] == '1', args[0][3] == '1', args[0][4] == '1');
}
}
}
else{
sendText("e1"); // ERROR 1 premature command
}
processingMutex.unlock();
}
void metadata(){
sendText("m" + std::to_string(gameSize) + " " + std::to_string(terrainSeed));
for (GameObject* obj : objects){
sendObject(obj);
obj -> greet();
}
for (size_t banner = 0; banner < banners.size(); banner ++){
size_t t = getTeamOf(banner);
if (t != -1){
sendText((std::string)"b" + std::to_string(banner) + " " + banners[banner] + " " + std::to_string(teams[t] -> banner));
}
else {
sendText((std::string)"b" + std::to_string(banner) + " " + banners[banner]);
}
}
}
void sendObject(GameObject* obj){
sendText((std::string)"n" + obj -> identify() + " " + std::to_string(obj -> id) + " " + std::to_string(obj -> x) + " " + std::to_string(obj -> y) + " " + std::to_string(obj -> angle) + " " + (obj -> editable() ? "1" : "0") + " " + std::to_string(obj -> banner));
}
void tick(unsigned int counter, bool mode){
sendText((std::string)"t" + std::to_string(counter) + " " + (mode ? "1" : "0"));
}
void lostCallback(GameObject* thing){
long res = -1;
for (size_t i = 0; i < myFighters.size(); i ++){
if (myFighters[i] -> id == thing -> id){
res = i;
}
}
if (res >= 0){
myFighters.erase(myFighters.begin() + res);
switch (thing -> identify()){
case 'R':
case 'c':
if (thing -> killer != NULL && thing -> killer -> owner != NULL){
if (thing -> killer -> owner != this){
thing -> killer -> owner -> collect(50); // Enemy castles are worth 50 points
}
}
sendText((std::string)"l");