-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
3113 lines (2590 loc) · 90.6 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
/* INCLUDES */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <signal.h>
//#include <unistd.h>
#include <thread>
//TODO remove this when possible
#include "Global.h"
#include "SKO_Utilities/OPI_Clock.h"
#include "SKO_Utilities/OPI_Timestep.h"
#include "SKO_Utilities/SKO_Utilities.h"
#include "SKO_Game/SKO_Player.h"
#include "SKO_Game/SKO_Item.h"
#include "SKO_Game/SKO_ItemObject.h"
#include "SKO_Game/SKO_Enemy.h"
#include "SKO_Game/SKO_Stall.h"
#include "SKO_Game/SKO_Shop.h"
#include "SKO_Game/SKO_Map.h"
#include "SKO_Game/SKO_item_defs.h"
#include "SKO_Game/SKO_Portal.h"
#include "SKO_Game/SKO_Target.h"
#include "SKO_Network/SKO_PacketTypes.h"
#include "SKO_Network/SKO_Network.h"
bool SERVER_QUIT = false;
SKO_Network *network;
SKO_Repository *repository;
/* Ctrl+C in terminal */
void terminal_quit(int signal){
printf(kGreen "\nGracefully shutting down after receiving signal=%i.\n kNormal", signal);
SERVER_QUIT = true;
network->cleanup();
}
/* DEFINES */
//HIT_LOOPS is how many for loop iterations to follow during collisions
#define HIT_LOOPS 50
//players
SKO_Player User[MAX_CLIENTS];
//maps and stuff :)
const char NUM_MAPS = 6;
SKO_Map map[NUM_MAPS];
//item definition
SKO_Item Item[256];
//cosmetic hp bars
unsigned char hpBar = 25;
//gravity
const float GRAVITY = 0.18;
//player walk speed
const float WALK_SPEED = 2.0f;
//holiday events TODO use a config not hard coded magic
unsigned const char HOLIDAY_NPC_DROP = ITEM_CHERRY_PI, HOLIDAY_BOX_DROP = ITEM_NERD_GLASSES;
const bool HOLIDAY = true;
bool blocked(unsigned char mapId, float box1_x1, float box1_y1, float box1_x2, float box1_y2, bool npc);
//attack time
unsigned long long int attack_speed = 40 * 6;
//TODO -Game Logic Refactor
void GiveLoot(int enemy, int player);
void Attack(unsigned char userId, float x, float y);
void Jump(unsigned char userId, float x, float y);
void Left(unsigned char userId, float x, float y);
void Right(unsigned char userId, float x, float y);
void Stop(unsigned char userId, float x, float y);
void GiveXP(unsigned char userId, int xp);
void quitParty(unsigned char userId);
void PlayerDamaged(unsigned char userId, unsigned char damage);
void EnemyHit(unsigned char enemyId, unsigned char mapId, unsigned char userId);
void RespawnEnemy(unsigned char mapId, int enemy);
void KillEnemy(unsigned char enemyId, unsigned char mapId, unsigned char killerUserId);
void SpawnItem(unsigned char mapId, unsigned char itemObjId, unsigned char itemId, float x, float y, float x_speed, float y_speed);
void DespawnItem(unsigned char itemId, unsigned char mapId);
void PocketItem(unsigned char userId, unsigned char itemID, unsigned int amount);
void EnemyAttack(int i, unsigned char mapId);
void Warp(unsigned char userId, SKO_Portal *portal);
void Respawn(unsigned char mapId, unsigned char userId);
void SpawnLoot(unsigned char mapId, SKO_ItemObject lootItem);
void ShopBuy(unsigned char userId, unsigned char shopItem, unsigned int amount);
void SpendStatPoint(unsigned char userId, unsigned char statId);
void UseItem(unsigned char userId, unsigned char itemId);
void DropItem(unsigned char userId, unsigned char itemId, unsigned int amount);
void InvitePlayerToTrade(unsigned char userId, unsigned char playerId);
void AcceptTrade(unsigned char userId);
void CancelTrade(unsigned char userId);
void OfferTradeItem(unsigned char userId, unsigned char itemId, unsigned int amount);
void ConfirmTrade(unsigned char userId);
void InvitePlayerToParty(unsigned char userId, unsigned char playerB);
void AcceptPartyInvite(unsigned char userId);
void ShopSell(unsigned char userId, unsigned char itemId, unsigned int amount);
void DepositBankItem(unsigned char userId, unsigned char itemId, unsigned int amount);
void WithdrawalBankItem(unsigned char userId, unsigned char itemId, unsigned int amount);
void DeclineClanInvite(unsigned char userId);
void AcceptClanInvite(unsigned char userId, std::string clanTag);
void AcceptClanInvite(unsigned char userId);
void ClanInvite(unsigned char userId, unsigned char playerId);
// threads
void PhysicsLoop();
void MapObjectLoop();
void NpcLoop();
int snap_distance = 64;
// Use environment variables for config values.
// Or set values in .env file (see: .env.example)
std::string getvar(const char* key)
{
printf("Reading environment variable {%s} ...\r\n", key);
char* value = getenv(key);
printf("Read %s=%s\r\n from getenv()\r\n", key, value);
// Validate environment variable loaded
if (value) {
printf("Read %s=%s\r\n from getenv()\r\n", key, value);
return std::string(value);
}
// Fall back to .env file
INIReader configFile(".env");
if (configFile.ParseError() < 0) {
printf("error: Can't load value for '%s'\n", key);
return "";
}
auto iniValue = configFile.Get("", key, "");
printf("Read %s=%s\r\n from .env", key, iniValue.c_str());
return iniValue;
}
/* CODE */
int main()
{
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = terminal_quit;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
printf("Starting Server...\n");
//load maps and stuff
for (int mp = 0; mp < NUM_MAPS; mp++)
{
//map filename
std::stringstream fileName;
fileName << "map";
fileName << mp;
map[mp] = SKO_Map(fileName.str());
}
for (int i = 0; i < MAX_CLIENTS; i++)
{
printf("User[%i] = SKO_Player();\n", i);
User[i] = SKO_Player();
}
unsigned int serverPort = atoi(getvar("SKO_PORT").c_str());
unsigned int databasePort = atoi(getvar("SKO_DB_PORT").c_str());
std::string databaseHostname = getvar("SKO_DB_HOST");
std::string databaseUsername = getvar("SKO_DB_USER");
std::string databasePassword = getvar("SKO_DB_PASSWORD");
std::string databaseSchema = getvar("SKO_DB_SCHEMA");
printf("About to connect to database.\n");
repository = new SKO_Repository();
if (repository->Connect(databaseHostname, databaseSchema, databasePort, databaseUsername, databasePassword) == "error")
return 1;
//items width, height, type, def, str, hp, reach, equipID
//values w h t d s h r e s = sell
Item[ITEM_GOLD] = SKO_Item(8, 8, 0, 0, 0, 0, 0, 0, 0);
Item[ITEM_DIAMOND] = SKO_Item(15, 14, 0, 0, 0, 0, 0, 0, 100);
Item[ITEM_MYSTERY_BOX] = SKO_Item(16, 15, 4, 0, 0, 0, 0, 0, 0);
//food
Item[ITEM_CARROT] = SKO_Item(15, 17, 1, 0, 0, 1, 0, 0, 1);
Item[ITEM_CHEESE] = SKO_Item(13, 20, 1, 0, 0, 2, 0, 0, 1);
Item[ITEM_BEER] = SKO_Item(9, 31, 1, 0, 0, 3, 0, 0, 2);
Item[ITEM_WINE] = SKO_Item(9, 31, 1, 0, 0, 5, 0, 0, 4);
Item[ITEM_POTION] = SKO_Item(10, 16, 1, 0, 0, 20, 0, 0, 5);
//weapon
Item[ITEM_SWORD_TRAINING] = SKO_Item(32, 16, 2, 0, 1, 0, 23, 1, 5);
//hat
Item[ITEM_BLUE_PHAT] = SKO_Item(10, 13, 3, 0, 0, 0, 0, 1, 0);
Item[ITEM_SANTA_HAT] = SKO_Item(10, 13, 3, 0, 0, 0, 0, 2, 0);
Item[ITEM_GOLD_PHAT] = SKO_Item(10, 13, 3, 0, 0, 0, 0, 3, 0);
//pi day event
Item[ITEM_NERD_GLASSES] = SKO_Item(21, 13, 3, 0, 0, 0, 0, 4, 0);
Item[ITEM_CHERRY_PI] = SKO_Item(32, 28, 5, 0, 0, 100, 0, 1, 0);
//easter event
Item[ITEM_BUNNY_EARS] = SKO_Item(14, 10, 3, 0, 0, 0, 0, 5, 0);
Item[ITEM_EASTER_EGG] = SKO_Item(8, 10, 5, 0, 0, 100, 0, 2, 0);
//halloween event
Item[ITEM_PUMPKIN] = SKO_Item(21, 23, 5, 0, 0, 0, 0, 3, 0);
Item[ITEM_SKULL_MASK] = SKO_Item(13, 18, 3, 0, 0, 0, 0, 6, 0);
//welcome to summer event
Item[ITEM_ICECREAM] = SKO_Item(9, 16, 5, 0, 0, 0, 0, 4, 0);
Item[ITEM_SUNGLASSES] = SKO_Item(21, 13, 3, 0, 0, 0, 0, 8, 0);
Item[ITEM_SWORD_RUSTED] = SKO_Item(11, 37, 2, 1, 2, 0, 32, 3, 25);
Item[ITEM_SWORD_STEEL] = SKO_Item(11, 37, 2, 2, 4, 0, 32, 4, 90);
Item[ITEM_SWORD_GOLD] = SKO_Item(11, 42, 2, 3, 11, 0, 40, 5, 6000);
Item[ITEM_SWORD_CRYSTAL] = SKO_Item(15, 43, 2, 5, 22, 0, 42, 6, 80000);
Item[ITEM_AXE_RUSTED] = SKO_Item(12, 31, 2, 1, 3, 0, 28, 7, 25);
Item[ITEM_AXE_STEEL] = SKO_Item(12, 31, 2, 1, 5, 0, 28, 8, 90);
Item[ITEM_AXE_GOLD] = SKO_Item(21, 36, 2, 2, 13, 0, 37, 9, 6000);
Item[ITEM_AXE_CRYSTAL] = SKO_Item(21, 38, 2, 3, 24, 0, 40, 10, 80000);
Item[ITEM_HAMMER_RUSTED] = SKO_Item(17, 33, 2, 0, 4, 0, 22, 11, 25);
Item[ITEM_HAMMER_STEEL] = SKO_Item(17, 33, 2, 0, 7, 0, 22, 12, 90);
Item[ITEM_HAMMER_GOLD] = SKO_Item(17, 33, 2, 2, 16, 0, 22, 13, 6000);
Item[ITEM_HAMMER_CRYSTAL] = SKO_Item(27, 33, 2, 3, 25, 0, 32, 14, 80000);
Item[ITEM_SCYTHE] = SKO_Item(23, 25, 2, 3, 3, 0, 22, 15, 900);
Item[ITEM_SCYTHE_REAPER] = SKO_Item(27, 32, 2, 10, 10, 10, 32, 16, 18000);
Item[ITEM_CANDY_CANE] = SKO_Item(10, 13, 3, 3, 3, 3, 0, 17, 0);
///halloween event items
Item[ITEM_HALLOWEEN_MASK] = SKO_Item(18, 21, 3, 0, 0, 0, 0, 10, 0);
Item[ITEM_GUARD_HELM] = SKO_Item(18, 21, 3, 5, 1, 10, 0, 11, 2500);
Item[ITEM_JACK_OLANTERN] = SKO_Item(21, 23, 5, 0, 0, 0, 0, 5, 0);
Item[ITEM_WHITE_PHAT] = SKO_Item(10, 13, 3, 0, 0, 0, 0, 12, 0);
Item[ITEM_SKELETON_HELM] = SKO_Item(18, 21, 3, 1, 2, 1, 0, 13, 500);
Item[ITEM_TRAINING_HELM] = SKO_Item(18, 21, 3, 1, 2, 1, 0, 14, 10);
Item[ITEM_PURPLE_PHAT] = SKO_Item(10, 13, 3, 0, 0, 0, 0, 15, 0);
Item[ITEM_SNOW_BALL] = SKO_Item(12, 12, 5, 0, 0, 0, 0, 6, 0);
for (int mp = 0; mp < NUM_MAPS; mp++)
for (int i = 0; i < 256; i++)
map[mp].ItemObj[i] = SKO_ItemObject();
network = new SKO_Network(repository, serverPort, 60);
printf("Initialized SKO_Network.\n");
printf("Starting up SKO_Network...\n");
std::string networkStatus = network->startup();
if (networkStatus == "success")
{
//TODO: make nice logging function that abstracts the console colors
printf(kGreen "SKO Network initialized successfully.\n" kNormal);
}
else
{
printf(kRed "Could not initialize SKO_Network. Here is the status: [%s]\n", networkStatus.c_str());
network->cleanup();
return 1;
}
/* initialize random seed: */
srand(OPI_Clock::nanoseconds());
//multi threading
std::thread physicsThread(PhysicsLoop);
std::thread npcThread(NpcLoop);
std::thread mapObjectThread(MapObjectLoop);
mapObjectThread.join();
npcThread.join();
physicsThread.join();
return 0;
}
bool blocked(unsigned char mapId, float box1_x1, float box1_y1, float box1_x2, float box1_y2, bool npc)
{
for (int r = 0; r < map[mapId].number_of_rects; r++)
{
float box2_x1 = map[mapId].collision_rect[r].x;
float box2_y1 = map[mapId].collision_rect[r].y;
float box2_x2 = map[mapId].collision_rect[r].x + map[mapId].collision_rect[r].w;
float box2_y2 = map[mapId].collision_rect[r].y + map[mapId].collision_rect[r].h;
if (box1_x2 > box2_x1 && box1_x1 < box2_x2 && box1_y2 > box2_y1 && box1_y1 < box2_y2)
return true;
}
for (int r = 0; r < map[mapId].num_targets; r++)
{
if (!map[mapId].Target[r].active)
{
if (!npc)
continue;
}
float box2_x1 = map[mapId].Target[r].x;
float box2_y1 = map[mapId].Target[r].y;
float box2_x2 = map[mapId].Target[r].x + map[mapId].Target[r].w;
float box2_y2 = map[mapId].Target[r].y + map[mapId].Target[r].h;
if (box1_x2 > box2_x1 && box1_x1 < box2_x2 && box1_y2 > box2_y1 && box1_y1 < box2_y2)
return true;
}
return false;
}
void MapObjectLoop()
{
while (!SERVER_QUIT)
{
for (unsigned char mapId = 0; mapId < NUM_MAPS; mapId++)
{
// Respawn targets
for (int i = 0; i < map[mapId].num_targets; i++)
{
//respawn this box if it's been dead a while
if (!map[mapId].Target[i].active && OPI_Clock::milliseconds() - map[mapId].Target[i].respawn_ticker > 5000)
{
map[mapId].Target[i].active = true;
network->sendSpawnTarget(i, mapId);
}
}
// Check for portal collisions
for (int portal = 0; portal < map[mapId].num_portals; portal++)
{
for (int userId = 0; userId < MAX_CLIENTS; userId++)
{
if (!User[userId].Status || User[userId].mapId != mapId)
continue;
// Do not bother checking if the player is standing still
if (User[userId].x_speed == 0 && User[userId].ground)
continue;
//player box
float box1_x1 = User[userId].x + 23;
float box1_y1 = User[userId].y + 13;
float box1_x2 = User[userId].x + 40;
float box1_y2 = User[userId].y + 64;
//portal box
float box2_x1 = map[mapId].Portal[portal]->x;
float box2_y1 = map[mapId].Portal[portal]->y;
float box2_x2 = map[mapId].Portal[portal]->x + map[mapId].Portal[portal]->w;
float box2_y2 = map[mapId].Portal[portal]->y + map[mapId].Portal[portal]->h;
if (box1_x2 > box2_x1 && box1_x1 < box2_x2 && box1_y2 > box2_y1 && box1_y1 < box2_y2)
{
if (User[userId].level >= map[mapId].Portal[portal]->level_required)
{
Warp(userId, map[mapId].Portal[portal]);
//TODO: how do players stay in a party on different maps?
//Maybe disconnect after a minute.
//quitParty(userId);
}
} //end portal collison
}
} //end for portal
}
// Use less CPU power on this thread by sleeping more.
// Actions in this thread are very low priority that should not check very often
OPI_Sleep::milliseconds(100);
}
}
void NpcLoop()
{
while (!SERVER_QUIT)
{
for (unsigned char mapId = 0; mapId < NUM_MAPS; mapId++)
{
//enemy
for (int i = 0; i < map[mapId].num_enemies; i++)
{
int next_action = 0;
//check for dibs on the kill, reset after 5 seconds
if (OPI_Clock::milliseconds() - map[mapId].Enemy[i]->dibsTicker >= 5000)
{
map[mapId].Enemy[i]->dibsPlayer = -1;
map[mapId].Enemy[i]->dibsTicker = 0;
}
if (map[mapId].Enemy[i]->y > map[mapId].death_pit)
{
RespawnEnemy(mapId, i);
printf("Fix enemy from falling into death pit: map %i Enemy %i spawns at (%i,%i)\n", mapId, i, map[mapId].Enemy[i]->sx, map[mapId].Enemy[i]->sy);
}
//check for spawn
if (map[mapId].Enemy[i]->dead)
{
if (OPI_Clock::milliseconds() - map[mapId].Enemy[i]->respawn_ticker >= 7000)
{
map[mapId].Enemy[i]->Respawn();
network->sendSpawnEnemy(map[mapId].Enemy[i], i, mapId);
map[mapId].Enemy[i]->dead = false;
}
} //end if dead
else
{
//find out if bandit confronts someone
bool confront = false;
//decide what to do
for (int u = 0; u < MAX_CLIENTS; u++)
{
//find a target baby!
// rules: first player to be an acceptable target
if (User[u].Ident && User[u].mapId == mapId)
{
float x_dist, y_dist;
//horizontal
if (map[mapId].Enemy[i]->facing_right)
x_dist = (User[u].x + 23) - (map[mapId].Enemy[i]->x + 41);
else
x_dist = (map[mapId].Enemy[i]->x + 23) - (User[u].x + 41);
//vertical
y_dist = User[u].y - map[mapId].Enemy[i]->y;
// confront
if (x_dist >= -10 && x_dist <= 10 && y_dist >= -60 && y_dist <= 60)
{
confront = true;
//timer override if first time
if (map[mapId].Enemy[i]->x_speed != 0)
map[mapId].Enemy[i]->AI_period = 32;
}
} //end for user Ident only
if (confront)
{
next_action = ENEMY_MOVE_STOP;
break;
}
} //end for user: u
//not dead, do some actions.
if ((OPI_Clock::milliseconds() - map[mapId].Enemy[i]->AI_ticker) > map[mapId].Enemy[i]->AI_period)
{
//only move if you shouldnt defend yourself, dumb enemy.
if (!confront)
{
//decide what to do
for (int u = 0; u < MAX_CLIENTS; u++)
{
//find a target baby!
// rules: first player to be an acceptable target
if (User[u].Ident && User[u].mapId == mapId)
{
float x_dist, y_dist;
if (map[mapId].Enemy[i]->facing_right)
x_dist = User[u].x - map[mapId].Enemy[i]->x;
else
x_dist = map[mapId].Enemy[i]->x - User[u].x;
//vertical
y_dist = User[u].y - map[mapId].Enemy[i]->y;
bool persue = false;
/* chase */
if (y_dist <= 60 && y_dist >= -60 && x_dist > -200 && x_dist < 200)
{
persue = true;
//push walk into right direction _OR_ continue
if (User[u].x > map[mapId].Enemy[i]->x)
{
//chase
if (map[mapId].Enemy[i]->hp > (map[mapId].Enemy[i]->hp_max / 2))
{
next_action = ENEMY_MOVE_RIGHT;
}
//flee
else
{
if (rand() % 3)
next_action = ENEMY_MOVE_LEFT;
else if (rand() % 2)
next_action = ENEMY_MOVE_STOP;
else
next_action = ENEMY_MOVE_RIGHT;
}
}
else // if (User[u].x < map[mapId].Enemy[i]->x)
{
//chase
if (map[mapId].Enemy[i]->hp > (map[mapId].Enemy[i]->hp_max / 2))
{
next_action = ENEMY_MOVE_LEFT;
}
else
{
if (rand() % 3)
next_action = ENEMY_MOVE_RIGHT;
else if (rand() % 2)
next_action = ENEMY_MOVE_STOP;
else
next_action = ENEMY_MOVE_LEFT;
}
}
u = MAX_CLIENTS;
break;
} //end same plane actions
//quit running or chasing me!
if (!persue)
{
// 1:15 chance of an action
int random_action = rand() % 15;
// UP TO quarter second extra delay
int random_delay = 500 + rand() % 250;
switch (random_action)
{
case 0:
next_action = ENEMY_MOVE_LEFT;
map[mapId].Enemy[i]->AI_period = random_delay;
break;
case 1:
next_action = ENEMY_MOVE_RIGHT;
map[mapId].Enemy[i]->AI_period = random_delay;
break;
default:
next_action = ENEMY_MOVE_STOP;
map[mapId].Enemy[i]->AI_period = random_delay;
break;
}
}
} //end u ident
} //end for User u
} //end !confront
else // if confront is true then:
{
//if you've stopped then attack
if (map[mapId].Enemy[i]->x_speed == 0)
{
next_action = ENEMY_ATTACK;
//half a second plus UP TO another half second
int attack_pause = 200 + rand() % 800;
map[mapId].Enemy[i]->AI_period = attack_pause;
}
//else if you're still moving, stop!
else
{
next_action = ENEMY_MOVE_STOP;
}
}
bool redundant = false;
switch (next_action)
{
case ENEMY_MOVE_STOP:
if (map[mapId].Enemy[i]->x_speed == 0)
redundant = true;
map[mapId].Enemy[i]->x_speed = 0;
break;
case ENEMY_MOVE_LEFT:
if (map[mapId].Enemy[i]->x_speed < 0)
redundant = true;
map[mapId].Enemy[i]->x_speed = -WALK_SPEED;
map[mapId].Enemy[i]->facing_right = false;
break;
case ENEMY_MOVE_RIGHT:
if (map[mapId].Enemy[i]->x_speed > 0)
redundant = true;
map[mapId].Enemy[i]->x_speed = WALK_SPEED;
map[mapId].Enemy[i]->facing_right = true;
break;
case ENEMY_ATTACK:
EnemyAttack(i, mapId);
break;
default:
redundant = true;
break;
}
//dont spam packets bitch
if (!redundant)
{
//if it wasn't redundant, reset the ticker
network->sendEnemyAction(map[mapId].Enemy[i], next_action, i, mapId);
map[mapId].Enemy[i]->AI_ticker = OPI_Clock::milliseconds();
} //end no spam
} //end enemy AI
} //end not dead
} // end enemy for loop
//npc
for (unsigned char i = 0; i < map[mapId].num_npcs; i++)
{
unsigned char next_action = 0;
//not dead, do some actions.
if ((OPI_Clock::milliseconds() - map[mapId].NPC[i]->AI_ticker) > map[mapId].NPC[i]->AI_period)
{
// UP TO quarter second extra delay
int random_delay = 2500 + rand() % 169;
int random_action = rand() % 4; //2:1 ratio stopping to moving.
switch (random_action)
{
case 0:
next_action = NPC_MOVE_LEFT;
map[mapId].NPC[i]->AI_period = random_delay;
break;
case 1:
next_action = NPC_MOVE_RIGHT;
map[mapId].NPC[i]->AI_period = random_delay;
break;
default:
next_action = NPC_MOVE_STOP;
map[mapId].NPC[i]->AI_period = random_delay;
break;
}
bool redundant = false;
switch (next_action)
{
case NPC_MOVE_STOP:
if (map[mapId].NPC[i]->x_speed == 0)
redundant = true;
map[mapId].NPC[i]->x_speed = 0;
break;
case NPC_MOVE_LEFT:
if (map[mapId].NPC[i]->x_speed < 0)
redundant = true;
map[mapId].NPC[i]->x_speed = -WALK_SPEED;
map[mapId].NPC[i]->facing_right = false;
break;
case NPC_MOVE_RIGHT:
if (map[mapId].NPC[i]->x_speed > 0)
redundant = true;
map[mapId].NPC[i]->x_speed = WALK_SPEED;
map[mapId].NPC[i]->facing_right = true;
break;
default:
redundant = true;
break;
}
//dont spam packets bitch
if (!redundant)
{
//if it wasn't redundant, reset the ticker
map[mapId].NPC[i]->AI_ticker = OPI_Clock::milliseconds();
network->sendNpcAction(map[mapId].NPC[i], next_action, i, mapId);
} //end no spam
} //end enemy AI
} // end npc for loop
}
// Medium thread priority, should be quick enough to make responsive enemies, but not so quick it takes away from physics and network handling
OPI_Sleep::milliseconds(32);
}
}
void enemyPhysics(unsigned char mapId)
{
bool block_y;
bool block_x;
//enemies
for (int i = 0; i < map[mapId].num_enemies; i++)
{
//enemy physics
if (map[mapId].Enemy[i]->y_speed < 10)
map[mapId].Enemy[i]->y_speed += GRAVITY;
//verical collision detection
block_y = blocked(mapId, map[mapId].Enemy[i]->x + map[mapId].Enemy[i]->x1, map[mapId].Enemy[i]->y + map[mapId].Enemy[i]->y_speed + map[mapId].Enemy[i]->y1 + 0.25, map[mapId].Enemy[i]->x + map[mapId].Enemy[i]->x2, map[mapId].Enemy[i]->y + map[mapId].Enemy[i]->y_speed + map[mapId].Enemy[i]->y2, true);
map[mapId].Enemy[i]->ground = true;
//vertical movement
if (!block_y)
{ //not blocked, fall
//animation
map[mapId].Enemy[i]->ground = false;
map[mapId].Enemy[i]->y += map[mapId].Enemy[i]->y_speed;
}
else
{ //blocked, stop
if (map[mapId].Enemy[i]->y_speed > 0)
{
map[mapId].Enemy[i]->y_speed = 1;
for (int loopVar = 0; loopVar < HIT_LOOPS && (!blocked(mapId, map[mapId].Enemy[i]->x + map[mapId].Enemy[i]->x1, map[mapId].Enemy[i]->y + map[mapId].Enemy[i]->y_speed + map[mapId].Enemy[i]->y1 + 0.25, map[mapId].Enemy[i]->x + map[mapId].Enemy[i]->x2, map[mapId].Enemy[i]->y + map[mapId].Enemy[i]->y_speed + map[mapId].Enemy[i]->y1 + 0.25 + map[mapId].Enemy[i]->y2, true)); loopVar++)
map[mapId].Enemy[i]->y += map[mapId].Enemy[i]->y_speed;
map[mapId].Enemy[i]->y = (int)(map[mapId].Enemy[i]->y + 0.5);
}
if (map[mapId].Enemy[i]->y_speed < 0)
{
map[mapId].Enemy[i]->y_speed = -1;
for (int loopVar = 0; loopVar < HIT_LOOPS && (!blocked(mapId, map[mapId].Enemy[i]->x + map[mapId].Enemy[i]->x1, map[mapId].Enemy[i]->y + map[mapId].Enemy[i]->y_speed + map[mapId].Enemy[i]->y1 + 0.25, map[mapId].Enemy[i]->x + map[mapId].Enemy[i]->x2, map[mapId].Enemy[i]->y + map[mapId].Enemy[i]->y_speed + map[mapId].Enemy[i]->y1 + 0.25 + map[mapId].Enemy[i]->y2, true)); loopVar++)
map[mapId].Enemy[i]->y += map[mapId].Enemy[i]->y_speed;
}
map[mapId].Enemy[i]->y_speed = 0;
}
//horizontal collision detection
block_x = blocked(mapId, map[mapId].Enemy[i]->x + map[mapId].Enemy[i]->x_speed + map[mapId].Enemy[i]->x1, map[mapId].Enemy[i]->y + map[mapId].Enemy[i]->y1, map[mapId].Enemy[i]->x + map[mapId].Enemy[i]->x_speed + map[mapId].Enemy[i]->x2, map[mapId].Enemy[i]->y + map[mapId].Enemy[i]->y2, true);
//horizontal movement
if (!block_x)
{ //not blocked, walk
map[mapId].Enemy[i]->x += map[mapId].Enemy[i]->x_speed;
}
if ((map[mapId].Enemy[i]->x_speed == 0 && map[mapId].Enemy[i]->y_speed == 0) || !map[mapId].Enemy[i]->attacking)
map[mapId].Enemy[i]->current_frame = 0;
} //end enemies/npc
}
void npcPhysics(unsigned char mapId)
{
bool block_y;
bool block_x;
//npcs
for (int i = 0; i < map[mapId].num_npcs; i++)
{
//enemy physics
if (map[mapId].NPC[i]->y_speed < 10)
map[mapId].NPC[i]->y_speed += GRAVITY;
//verical collision detection
block_y = blocked(mapId, map[mapId].NPC[i]->x + map[mapId].NPC[i]->x1, map[mapId].NPC[i]->y + map[mapId].NPC[i]->y_speed + map[mapId].NPC[i]->y1 + 0.25, map[mapId].NPC[i]->x + map[mapId].NPC[i]->x2, map[mapId].NPC[i]->y + map[mapId].NPC[i]->y_speed + map[mapId].NPC[i]->y2, true);
map[mapId].NPC[i]->ground = true;
//vertical movement
if (!block_y)
{ //not blocked, fall
//animation
map[mapId].NPC[i]->ground = false;
map[mapId].NPC[i]->y += map[mapId].NPC[i]->y_speed;
}
else
{
//blocked, stop
if (map[mapId].NPC[i]->y_speed > 0)
{
map[mapId].NPC[i]->y_speed = 1;
//while or if TODO
for (int loopVar = 0; loopVar < HIT_LOOPS && (!blocked(mapId, map[mapId].NPC[i]->x + map[mapId].NPC[i]->x1, map[mapId].NPC[i]->y + map[mapId].NPC[i]->y_speed + map[mapId].NPC[i]->y1 + 0.25, map[mapId].NPC[i]->x + map[mapId].NPC[i]->x2, map[mapId].NPC[i]->y + map[mapId].NPC[i]->y_speed + map[mapId].NPC[i]->y1 + 0.25 + map[mapId].NPC[i]->y2, true)); loopVar++)
map[mapId].NPC[i]->y += map[mapId].NPC[i]->y_speed;
map[mapId].NPC[i]->y = (int)(map[mapId].NPC[i]->y + 0.5);
}
if (map[mapId].NPC[i]->y_speed < 0)
{
map[mapId].NPC[i]->y_speed = -1;
//while or if TODO
for (int loopVar = 0; loopVar < HIT_LOOPS && (!blocked(mapId, map[mapId].NPC[i]->x + map[mapId].NPC[i]->x1, map[mapId].NPC[i]->y + map[mapId].NPC[i]->y_speed + map[mapId].NPC[i]->y1 + 0.25, map[mapId].NPC[i]->x + map[mapId].NPC[i]->x2, map[mapId].NPC[i]->y + map[mapId].NPC[i]->y_speed + map[mapId].NPC[i]->y1 + 0.25 + map[mapId].NPC[i]->y2, true)); loopVar++)
map[mapId].NPC[i]->y += map[mapId].NPC[i]->y_speed;
}
map[mapId].NPC[i]->y_speed = 0;
}
//horizontal collision detection
block_x = blocked(mapId, map[mapId].NPC[i]->x + map[mapId].NPC[i]->x_speed + map[mapId].NPC[i]->x1, map[mapId].NPC[i]->y + map[mapId].NPC[i]->y1, map[mapId].NPC[i]->x + map[mapId].NPC[i]->x_speed + map[mapId].NPC[i]->x2, map[mapId].NPC[i]->y + map[mapId].NPC[i]->y2, true);
//horizontal movement
if (!block_x)
{
//not blocked, walk
map[mapId].NPC[i]->x += map[mapId].NPC[i]->x_speed;
}
} //end npc
}
void checkPlayerRegen(unsigned char userId, unsigned char regen)
{
if (OPI_Clock::milliseconds() - User[userId].regen_ticker < 1200)
{
return;
}
//regen hp
if (User[userId].hp < User[userId].max_hp)
{
User[userId].hp += regen;
//cap the hp to their max
if (User[userId].hp > User[userId].max_hp)
User[userId].hp = User[userId].max_hp;
//Send changed HP to client player's party members
unsigned char displayHp = (int)((User[userId].hp / (float)User[userId].max_hp) * 80);
for (int playerId = 0; playerId < MAX_CLIENTS; playerId++)
{
if (playerId != userId && User[playerId].Ident && User[playerId].partyStatus == SKO_PartyStatus::Active && User[playerId].party == User[userId].party)
network->sendBuddyStatHp(playerId, userId, displayHp);
}
}
//Send changed HP stat to client player
network->sendStatHp(userId, User[userId].hp);
User[userId].regen_ticker = OPI_Clock::milliseconds();
}
void playerPhysics(unsigned char mapId)
{
bool block_y;
bool block_x;
for (unsigned char i = 0; i < MAX_CLIENTS; i++)
{
// Only check players logged in
if (!User[i].Ident)
continue;
// Only check players on this map
if (User[i].mapId != mapId)
continue;
//stop attacking
if (OPI_Clock::milliseconds() - User[i].attack_ticker > attack_speed)
{
User[i].attacking = false;
//if you made an action during an attack, it is saved utnil now. Execute
if (User[i].queue_action != 0)
{
printf("Glitched user is %s :O\n", User[i].Nick.c_str());
float numx = User[i].x;
float numy = User[i].y;
switch (User[i].queue_action)
{
case MOVE_LEFT:
Left(i, numx, numy);
printf("\e[0;32mCorrection! Queue action being sent NOW: MOVE_LEFT\e[m\n");
break;
case MOVE_RIGHT:
Right(i, numx, numy);
printf("\e[0;32mCorrection! Queue action being sent NOW: MOVE_RIGHT\e[m\n");
break;
case MOVE_JUMP:
Jump(i, numx, numy);
printf("\e[0;32mCorrection! Queue action being sent NOW: JUMP\e[m\n");
break;
case ATTACK:
printf("\e[0;32mCorrection! Queue action being sent NOW: ATTACK\e[m\n");
//do attack actions
Attack(i, numx, numy);
break;
case MOVE_STOP:
printf("\e[0;32mCorrection! Queue action being sent NOW: MOVE_STOP\e[m\n");
//do attack actions
Stop(i, numx, numy);
break;
default:
printf("\e[0;32mCorrection! Queue action glitch: %i\e[m\n", User[i].queue_action);
break;
} //end switch
//reset the cue
User[i].queue_action = 0;
} // end Queue actions
} //end attack ticker
//HP regen
int regen = User[i].regen;
int weap = User[i].equip[0];
int hat = User[i].equip[1];
if (weap > 0)
regen += Item[weap].hp;
if (hat > 0)
regen += Item[hat].hp;
// TODO - move this code as it does not belong in Physics
if (regen > 0)
{
checkPlayerRegen(i, regen);
}
if (User[i].y_speed < 10)
{
User[i].y_speed += GRAVITY;
}
//verical collision detection
block_y = blocked(mapId, User[i].x + 25, User[i].y + User[i].y_speed + 13 + 0.25, User[i].x + 38, User[i].y + User[i].y_speed + 64, false);
//vertical movement
if (!block_y)
{ //not blocked, fall
//animation
User[i].ground = false;
User[i].y += User[i].y_speed;
//bottomless pit
if (User[i].y > map[mapId].death_pit)
{
printf("user died in death pit on map %i because %i > %i\n", (int)mapId, (int)User[i].y, (int)map[mapId].death_pit);
Respawn(User[i].mapId, i);
}
}
else
{
User[i].ground = true;
//blocked, stop
if (User[i].y_speed > 0)
{
User[i].y_speed = 1;
//while or if TODO
for (int loopVar = 0; loopVar < HIT_LOOPS && (!blocked(mapId, User[i].x + 25, User[i].y + User[i].y_speed + 13 + 0.25, User[i].x + 38, User[i].y + User[i].y_speed + 64 + 0.25, false)); loopVar++)
User[i].y += User[i].y_speed;
User[i].y = (int)(User[i].y + 0.5);
}
if (User[i].y_speed < 0)
{
User[i].y_speed = -1;
//while or if TODO
for (int loopVar = 0; loopVar < HIT_LOOPS && (!blocked(mapId, User[i].x + 25, User[i].y + User[i].y_speed + 13 + 0.25, User[i].x + 38, User[i].y + User[i].y_speed + 64 + 0.25, false)); loopVar++)
User[i].y += User[i].y_speed;
}
User[i].y_speed = 0;
}
if (User[i].x_speed != 0)
{
//horizontal collision detection
block_x = blocked(mapId, User[i].x + User[i].x_speed + 23, User[i].y + 13, User[i].x + User[i].x_speed + 40, User[i].y + 64, false);
//horizontal movement
if (!block_x)