-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
1152 lines (958 loc) · 31.7 KB
/
main.cpp
File metadata and controls
1152 lines (958 loc) · 31.7 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
#include <cmath>
#define PI 3.14159265358979323846
#ifndef floorf
namespace std {
float floorf(float x) { return ::floor(x); }
float ceilf(float x) { return ::ceil(x); }
}
#endif
#include "iostream"
#include "data.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/select.h>
#include <unistd.h>
#include <signal.h>
#include <chrono>
#include "unordered_dense.h"
#include <mutex>
#include <thread>
namespace races{
static dcon::race_id rats;
static dcon::race_id humans;
static dcon::race_id graci;
static dcon::race_id elodin;
static dcon::race_id meat_ball;
}
namespace items{
static dcon::item_type_id rat_body;
static dcon::item_type_id rat_fur;
static dcon::item_type_id rat_body_skinned;
static dcon::item_type_id rat_skeleton;
static dcon::item_type_id human_body;
static dcon::item_type_id human_body_skinned;
static dcon::item_type_id human_skeleton;
}
void assert_id(std::string text, int a, int b) {
if (a != b) {
printf("WRONG_ID: %s. %d, %d\n", text.c_str(), a, b);
}
}
void check_ids () {
assert_id("rats", races::rats.index(), 0);
assert_id("humans", races::humans.index(), 1);
assert_id("rat_body", items::rat_body.index(), 0);
assert_id("rat_fur", items::rat_fur.index(), 1);
assert_id("rat_body_skinned", items::rat_body_skinned.index(), 2);
assert_id("rat_skeleton", items::rat_skeleton.index(), 3);
assert_id("human_body", items::human_body.index(), 4);
assert_id("human_body_skinned", items::human_body_skinned.index(), 5);
assert_id("human_skeleton", items::human_skeleton.index(), 6);
}
static fd_set udp_singleton;
static fd_set udp_select_singleton;
static fd_set active_connections;
static fd_set read_connections;
static sockaddr_in client_address;
static int connection_address_size;
static struct timeval timeout { 0, 0 };
static int udp_socket;
static int tcp_socket;
static char udp_buffer[1024] = {0};
static std::mutex game_state_mtx {};
static std::default_random_engine rng;
static std::uniform_real_distribution<float> uniform{0.0, 1.0};
static std::normal_distribution<float> normal_d{0.f, 1.f};
static std::normal_distribution<float> size_d{1.f, 0.3f};
void
handle_udp_subscription(
ankerl::unordered_dense::map<in_addr_t, sockaddr_in>& container
) {
udp_select_singleton = udp_singleton;
auto udp_has_message = select(
FD_SETSIZE,
&udp_select_singleton,
NULL,
NULL,
&timeout
);
if (udp_has_message < 0) {
perror("Select error (udp)");
exit(EXIT_FAILURE);
}
if (udp_has_message > 0) {
// handle UDP "subscriptions"
connection_address_size = sizeof(client_address);
auto status = recvfrom(udp_socket, udp_buffer, 1024, 0, (struct sockaddr*)&client_address, (socklen_t *) &connection_address_size);
if (status >= 0) {
printf("got UDP message\n");
container[client_address.sin_addr.s_addr] = client_address;
}
}
}
constexpr uint8_t MODEL_RAT = 0;
constexpr uint8_t MODEL_HUMAN = 1;
constexpr uint8_t MODEL_UNKNOWN = 255;
constexpr uint8_t WEAPON_TYPE_RAT = 0;
constexpr uint8_t WEAPON_TYPE_HUMAN = 1;
constexpr uint8_t WEAPON_TYPE_KNIFE = 2;
constexpr inline uint8_t TCP_LOGIN = 0;
constexpr inline uint8_t TCP_FIGHTER = 1;
struct player_creation;
struct tcp_login_update{
int player_id;
uint8_t padding[4];
uint8_t padding2[4];
};
static_assert(sizeof(tcp_login_update) == 12);
struct tcp_fighter_update{
int fighter_id;
int location_id;
uint8_t race_id;
uint8_t padding2[3];
};
static_assert(sizeof(tcp_fighter_update) == 12);
struct tcp_update{
// 4 bytes
uint8_t update_type;
uint8_t padding[3];
// 12 bytes
union {
tcp_login_update login;
tcp_fighter_update fighter;
} payload;
};
static_assert(sizeof(tcp_update) == 16);
void handle_tcp_connection(dcon::data_container& container ) {
// connection requests
connection_address_size = sizeof(client_address);
int new_connection = accept(
tcp_socket,
(sockaddr *) & client_address,
(socklen_t *) &connection_address_size
);
if (new_connection < 0) {
perror("Accept connection error");
exit(EXIT_FAILURE);
}
fprintf(
stderr,
"SERVER: NEW CONNECTION\n"
);
if (container.player_size() > 100) {
// Deny connections when there are 100 players
return;
}
auto pid = container.create_player();
tcp_update to_send{};
to_send.update_type = TCP_LOGIN;
to_send.payload.login.player_id = pid.index();
send(new_connection, (char*)&to_send, sizeof(tcp_update), 0);
container.player_set_connection(pid, new_connection);
container.player_set_address(pid, client_address.sin_addr.s_addr);
FD_SET(new_connection, &active_connections);
}
// sent via udp
inline constexpr uint8_t UPDATE_SPATIAL = 0;
inline constexpr uint8_t UPDATE_FIGHTER = 1;
inline constexpr uint8_t UPDATE_RELINK = 2;
inline constexpr uint8_t UPDATE_HIGH_PRECISION = 3;
inline constexpr uint8_t UPDATE_ITEM_RELINK = 4;
inline constexpr uint8_t UPDATE_ITEM = 5;
struct item_update {
// 4 bytes
int item_id;
// 4 bytes
uint8_t item_type;
bool is_container;
uint8_t contained_commodity;
uint8_t padding[1];
// 4 bytes
int contained_amount;
};
static_assert(sizeof(item_update) == 12);
struct high_precision_update {
// 4 bytes
int spatial_entity_id;
// 4 bytes
float x;
// 4 bytes
float y;
};
static_assert(sizeof(high_precision_update) == 12);
struct spatial_update {
// 4 bytes
int spatial_entity_id;
// 4 bytes
int16_t x;
int16_t y;
// 4 bytes
uint8_t direction;
uint8_t padding[3];
};
static_assert(sizeof(spatial_update) == 12);
struct fighter_update {
// 4 bytes
int fighter_id;
// 4 bytes
int16_t hp;
int16_t max_hp;
// 4 bytes
uint8_t energy;
uint8_t attack_energy;
uint8_t race;
uint8_t weapon_type;
};
static_assert(sizeof(fighter_update) == 12);
struct relink_update {
// 4 bytes
int fighter_id;
// 4 bytes
int item_id;
// 4 bytes
uint8_t padding[4];
};
static_assert(sizeof(relink_update) == 12);
struct relink_item_update {
// 4 bytes
int item_id;
// 4 bytes
int spatial_id;
// 4 bytes
uint8_t padding[4];
};
static_assert(sizeof(relink_item_update) == 12);
struct udp_update {
// 4 bytes
uint8_t update_type;
uint8_t padding[3];
// 4 bytes
int timestamp;
// 4 bytes
int sent_to_player;
// 12 bytes
union {
spatial_update spatial;
fighter_update fighter;
relink_update relink;
relink_item_update relink_item;
high_precision_update high_precision;
item_update item;
} payload;
};
static_assert(sizeof(udp_update) == 24);
void
send_network_update_player(
dcon::data_container& container,
ankerl::unordered_dense::map<in_addr_t, sockaddr_in>& address_mapping,
int timestamp,
dcon::player_id player
) {
auto connection = container.player_get_connection(player);
if (!FD_ISSET(connection, &active_connections)) {
return;
}
auto internet_address = container.player_get_address(player);
auto& udp_address_iterator = address_mapping[internet_address];
auto player_fighter = container.player_get_controlled_from_player_control(player);
auto player_body = container.fighter_get_item_from_embodiment(player_fighter);
auto player_location = container.item_get_spatial_entity_from_item_location(player_body);
if (player_location) {
// 30 times per second
udp_update next_update;
next_update.update_type = UPDATE_HIGH_PRECISION;
next_update.timestamp = timestamp;
next_update.sent_to_player = player.index();
next_update.payload.high_precision.spatial_entity_id = player_location.index();
next_update.payload.high_precision.x = container.spatial_entity_get_x(player_location);
next_update.payload.high_precision.y = container.spatial_entity_get_y(player_location);
sendto(
udp_socket,
(char*)&next_update,
sizeof(next_update),
0,
(sockaddr *) &(udp_address_iterator),
sizeof(next_update)
);
}
container.for_each_spatial_entity([&](auto location){
auto shift_x = container.spatial_entity_get_x(location) - container.spatial_entity_get_x(player_location);
auto shift_y = container.spatial_entity_get_y(location) - container.spatial_entity_get_y(player_location);
if (abs(shift_x) > 80.f) {
return;
}
if (abs(shift_y) > 80.f) {
return;
}
if (abs(shift_x) > 10.f && (timestamp % 3) != 0) {
return;
}
if (abs(shift_y) > 10.f && (timestamp % 3) != 0) {
return;
}
{
udp_update next_update;
next_update.update_type = UPDATE_SPATIAL;
next_update.timestamp = timestamp;
next_update.sent_to_player = player.index();
next_update.payload.spatial.spatial_entity_id = location.index();
next_update.payload.spatial.x = shift_x * 100.f;
next_update.payload.spatial.y = shift_y * 100.f;
next_update.payload.spatial.direction = (uint8_t)(container.spatial_entity_get_direction(location) / 2.f / PI * 255);
sendto(
udp_socket,
(char*)&next_update,
sizeof(next_update),
0,
(sockaddr *) &(udp_address_iterator),
sizeof(next_update)
);
}
auto item = container.spatial_entity_get_item_from_item_location(location);
auto fighter = container.item_get_fighter_from_embodiment(item);
if (fighter && (timestamp % 6) == 0) {
// 5 times per second
udp_update next_update;
next_update.update_type = UPDATE_FIGHTER;
next_update.timestamp = timestamp;
next_update.sent_to_player = player.index();
next_update.payload.fighter.fighter_id = fighter.index();
next_update.payload.fighter.energy = (uint8_t)(container.fighter_get_energy(fighter) * 255);
next_update.payload.fighter.attack_energy = (uint8_t)(container.fighter_get_attack_energy_buffer(fighter) * 255);
next_update.payload.fighter.hp = container.fighter_get_hp(fighter);
next_update.payload.fighter.max_hp = container.fighter_get_max_hp(fighter);
sendto(
udp_socket,
(char*)&next_update,
sizeof(next_update),
0,
(sockaddr *) &(udp_address_iterator),
sizeof(next_update)
);
}
if (fighter && (timestamp % 15) == 0) {
// 2 times per second
udp_update next_update;
next_update.update_type = UPDATE_RELINK;
next_update.timestamp = timestamp;
next_update.sent_to_player = player.index();
next_update.payload.relink.fighter_id = fighter.index();
next_update.payload.relink.item_id = item.index();
sendto(
udp_socket,
(char*)&next_update,
sizeof(next_update),
0,
(sockaddr *) &(udp_address_iterator),
sizeof(next_update)
);
}
if (item && (timestamp % 30) == 0) {
// 1 time per second
udp_update next_update;
next_update.update_type = UPDATE_ITEM;
next_update.timestamp = timestamp;
next_update.sent_to_player = player.index();
next_update.payload.item.item_id = item.index();
next_update.payload.item.item_type = container.item_get_item_type(item).id.index();
sendto(
udp_socket,
(char*)&next_update,
sizeof(next_update),
0,
(sockaddr *) &(udp_address_iterator),
sizeof(next_update)
);
}
if (item && (timestamp % 60) == 0) {
// 1 time per 2 seconds
udp_update next_update;
next_update.update_type = UPDATE_ITEM_RELINK;
next_update.timestamp = timestamp;
next_update.sent_to_player = player.index();
next_update.payload.relink_item.item_id = item.index();
next_update.payload.relink_item.spatial_id = location.index();
sendto(
udp_socket,
(char*)&next_update,
sizeof(next_update),
0,
(sockaddr *) &(udp_address_iterator),
sizeof(next_update)
);
}
});
}
void
send_network_updates(
dcon::data_container& container,
ankerl::unordered_dense::map<in_addr_t, sockaddr_in>& address_mapping,
int timestamp
) {
container.for_each_player([&](auto dest) {
send_network_update_player(container, address_mapping, timestamp, dest);
});
}
void sigpipe_handler(int unused)
{
}
namespace command {
inline constexpr uint8_t MOVE = 0;
inline constexpr uint8_t RUN_START = 1;
inline constexpr uint8_t RUN_STOP = 2;
inline constexpr uint8_t ATTACK_START = 3;
inline constexpr uint8_t ATTACK_STOP = 4;
inline constexpr uint8_t RESPAWN = 5;
inline constexpr uint8_t CLASS_MAGE = 0;
inline constexpr uint8_t CLASS_WARRIOR = 1;
inline constexpr uint8_t CLASS_ROGUE = 2;
inline constexpr uint8_t CLASS_TOTAL = 3;
struct data {
int32_t actor;
int32_t target_actor;
float target_x;
float target_y;
uint8_t command_type;
uint8_t command_data;
uint8_t race;
uint8_t padding[1];
};
static_assert(sizeof(data) == 20);
}
int consume_command(dcon::data_container& container, int connection, command::data command) {
std::lock_guard<std::mutex> lk(game_state_mtx);
printf("new command %d\n", command.command_type);
dcon::player_id id { (dcon::player_id::value_base_t) command.actor };
if (!container.player_is_valid(id)) {
return 0;
}
/*
We have to check that the player is actually controlled by whoever is connected at the other end
*/
if (container.player_get_connection(id) != connection) {
return 0;
}
/*
Every player has a fighter?
No longer
*/
auto control = container.player_get_player_control(id);
auto fighter = container.player_control_get_controlled(control);
auto body = container.fighter_get_item_from_embodiment(fighter);
auto location = container.item_get_spatial_entity_from_item_location(body);
/*
Some commands are available only for fighters
*/
if (fighter) {
if (
command.command_type == command::MOVE
) {
container.fighter_set_tx(fighter, command.target_x);
container.fighter_set_ty(fighter, command.target_y);
} else if (
command.command_type == command::RUN_START
&& container.fighter_get_energy(fighter) > 0.2f
) {
container.fighter_set_running(fighter, true);
} else if (
command.command_type == command::RUN_STOP
) {
container.fighter_set_running(fighter, false);
} else if (
command.command_type == command::ATTACK_START
&& container.fighter_get_energy(fighter) > 0.1f
) {
container.fighter_set_attacking(fighter, true);
} else if (
command.command_type == command::ATTACK_STOP
) {
container.fighter_set_attacking(fighter, false);
}
}
if (
command.command_type == command::RESPAWN
) {
auto race = dcon::race_id { command.race };
if (container.race_is_valid(race)) {
if (!fighter) {
fighter = container.create_fighter();
container.force_create_player_control(id, fighter);
body = container.create_item();
container.force_create_embodiment(fighter, body);
location = container.create_spatial_entity();
container.force_create_item_location(body, location);
}
auto max_hp = container.race_get_max_hp(race);
auto desired_body_type = container.race_get_body_item(race);
container.item_set_item_type(body, desired_body_type);
container.fighter_set_max_hp(fighter, max_hp);
container.fighter_set_hp(fighter, max_hp);
container.fighter_set_energy(fighter, 1.f);
container.fighter_set_tx(fighter, 0.f);
container.fighter_set_ty(fighter, 0.f);
container.fighter_set_attacking(fighter, false);
container.spatial_entity_set_x(location, 0.f);
container.spatial_entity_set_y(location, 0.f);
/*
Notify the player of new fighter/location
*/
tcp_update to_send{};
to_send.update_type = TCP_FIGHTER;
to_send.payload.fighter.fighter_id = fighter.index();
to_send.payload.fighter.location_id = location.index();
to_send.payload.fighter.race_id = race.index();
send(connection, (char*)&to_send, sizeof(tcp_update), 0);
}
}
return 0;
}
void clean_player(dcon::data_container& container, int connection) {
perror("Read failed");
// connection ended
// delete players with this connection
std::vector<dcon::player_id> players_to_delete;
container.for_each_player([&](auto pid) {
if (container.player_get_connection(pid) == connection)
players_to_delete.push_back(pid);
});
for (int i = 0; i < (int)players_to_delete.size(); ++i) {
auto pid = players_to_delete[i];
auto control = container.player_get_player_control(pid);
auto fighter = container.player_control_get_controlled(control);
// event_notification(container, fighter, update::EVENT_LEFT_GAME);
printf("delete player %d\n", pid.index());
container.delete_player(pid);
if (fighter) {
container.delete_fighter(fighter);
}
}
}
int read_from_connection (dcon::data_container& container, int connection) {
char buffer[256];
int nbytes;
nbytes = read(connection, buffer, 256);
if (nbytes <= 0) {
clean_player(container, connection);
return -1;
} else {
command::data command {};
memcpy(&command, buffer, sizeof(command::data));
return consume_command(container, connection, command);
}
return 0;
}
float move_speed_from_wrong_direction(
dcon::data_container& container,
dcon::spatial_entity_id fid,
float dx,
float dy
) {
if (dx == 0.f && dy== 0.f) {
return 1.f;
}
auto desired_direction = atan2f(dy, dx);
auto direction = container.spatial_entity_get_direction(fid);
return 0.1 + std::max(0.f, cosf(direction - desired_direction));
}
void rotate_toward(dcon::data_container & container, float dt, dcon::spatial_entity_id id, float dx, float dy, float rotation_speed) {
if (dx != 0 || dy != 0) {
auto desired_direction = atan2f(dy, dx);
auto direction = container.spatial_entity_get_direction(id);
auto diff = fmodf(desired_direction - direction + 4 * PI, 2 * PI);
if (diff <= rotation_speed * dt) {
direction = desired_direction;
} else if (diff <= PI) {
direction = direction + rotation_speed * dt;
} else if (diff < 2 * PI - rotation_speed * dt) {
direction = direction - rotation_speed * dt;
} else {
direction = desired_direction;
}
direction = fmodf(direction + 2 * PI, 2 * PI);
container.spatial_entity_set_direction(id, direction);
}
}
void update_ai_state(dcon::data_container & container) {
container.for_each_fighter([&](auto fid){
auto player = container.fighter_get_controller_from_player_control(fid);
if(player) return;
// for now: just wander aimlessly
auto tx = container.fighter_get_tx(fid);
auto ty = container.fighter_get_ty(fid);
tx += 0.5f *(uniform(rng) - 0.5f);
ty += 0.5f *(uniform(rng) - 0.5f);
auto norm = tx * tx + ty * ty;
if (norm > 0.f) {
tx /= norm;
ty /= norm;
}
container.fighter_set_tx(fid, tx);
container.fighter_set_ty(fid, ty);
});
}
void update_game_state(dcon::data_container & container, std::chrono::microseconds last_tick) {
float dt = float(last_tick.count()) / 1'000'000.f;
float energy_regen_rate = 0.15f;
// float energy_walking_spend = energy_regen_rate * 0.75f;
// float energy_running_spend = energy_regen_rate * 1.2f;
float attack_energy_siphon = energy_regen_rate * 1.25f;
float attack_energy_siphon_efficiency = 2.5f;
float attack_max_energy = 0.125f * 5.5f;
float attack_half_angle = PI / 3.f;
float attack_energy_drain = energy_regen_rate * 0.05f;
auto attack_energy_decay = expf(-dt / 2.f);
container.for_each_fighter([&](auto fid){
auto hp = container.fighter_get_hp(fid);
if (hp <= 0) {
return;
}
auto body = container.fighter_get_item_from_embodiment(fid);
auto body_type = container.item_get_item_type(body);
auto race = container.item_type_get_race(body_type);
auto movement_energy_cost = container.race_get_movement_energy_cost(race);
auto movement_energy_throughput = container.race_get_movement_energy_throughput(race);
auto base_weight = container.item_type_get_base_weight(body_type);
auto weight = base_weight;
/*
We assume:
To move 1 unit of mass along the path of length 1 we have to spend *movement_energy_cost* units of energy.
When walking race outputs *movement_energy_throughput* of energy.
Running multiplies the throughput and increases movement_energy_cost.
When we have to rotate, our throughput falls.
*/
auto spatial = container.item_get_spatial_entity_from_item_location(body);
float attack_range = 2.f;
auto x = container.spatial_entity_get_x(spatial);
auto y = container.spatial_entity_get_y(spatial);
auto tx = container.fighter_get_tx(fid);
auto ty = container.fighter_get_ty(fid);
auto dx = tx;
auto dy = ty;
auto energy_gain = dt * energy_regen_rate;
auto energy_loss = 0.f;
auto rotation_speed = 4.5f;
movement_energy_throughput *= move_speed_from_wrong_direction(container, spatial, dx, dy);
auto running = container.fighter_get_running(fid) && container.fighter_get_energy(fid) > 0.1f;
if (running) {
movement_energy_throughput *= 2.5f;
movement_energy_cost *= 1.5f;
}
auto attacking = container.fighter_get_attacking(fid);
if (attacking) {
movement_energy_throughput *= 0.3f;
energy_loss += dt * (attack_energy_siphon + attack_energy_drain);
}
rotate_toward(container, dt, spatial, dx, dy, rotation_speed);
auto norm = sqrtf(dx * dx + dy * dy);
if (norm > 1.f) {
dx /= norm;
dy /= norm;
}
if (norm > 0.f) {
energy_loss += movement_energy_cost * movement_energy_throughput * dt;
}
auto total_energy_budget = container.fighter_get_energy(fid) + energy_gain;
auto actual_energy_spending_rate = 1.f;
if (energy_loss > 0.f && energy_loss > total_energy_budget) {
actual_energy_spending_rate = total_energy_budget / energy_loss;
}
// now we can actually charge attack and move according to available energy
// moving
if (norm > 0.f) {
auto actual_movement_throughput = movement_energy_throughput * actual_energy_spending_rate;
auto energy_spent_on_movement = actual_movement_throughput * dt;
auto movement = energy_spent_on_movement / weight / movement_energy_cost;
x = container.spatial_entity_get_x(spatial);
y = container.spatial_entity_get_y(spatial);
x += dx * movement;
y += dy * movement;
container.spatial_entity_set_x(spatial, x);
container.spatial_entity_set_y(spatial, y);
}
/*
When player is charging attack, the energy is accumulated in a special buffer.
When player releases attack, he spends accumulated energy rounded down to the integer to deal damage.
X units of energy deal X * X damage.
If a target had accumulated not more than 2 X units of attack energy, then their attack is interrupted.
Design goals are:
We want heavy attacks to be more cost efficient, so players want to use them.
We want players to be able intercept heavier attacks, so they could protect themselves.
But we also don't want light attack spam to be a valid tactic, so we don't allow light attacks to intercept really heavy attacks
*/
auto cashback = 0.f;
if (attacking) {
auto siphoned_energy = dt * attack_energy_siphon * attack_energy_siphon_efficiency * actual_energy_spending_rate;
auto current_energy = container.fighter_get_attack_energy_buffer(fid);
cashback += std::max(0.f, current_energy * attack_energy_decay + siphoned_energy - attack_max_energy) / attack_energy_siphon_efficiency / actual_energy_spending_rate;
siphoned_energy = std::min(attack_max_energy - current_energy * attack_energy_decay, siphoned_energy);
container.fighter_set_attack_energy_buffer(fid, current_energy * attack_energy_decay + siphoned_energy);
} else {
auto attack_strength = floorf(container.fighter_get_attack_energy_buffer(fid) / 0.125f);
auto damage = attack_strength * attack_strength;
cashback += container.fighter_get_attack_energy_buffer(fid) - attack_strength * 0.125f;
container.fighter_set_attack_energy_buffer(fid, 0.f);
if (damage > 0.f) {
container.for_each_fighter([&](dcon::fighter_id target){
if (target == fid) return;
auto body = container.fighter_get_item_from_embodiment(target);
auto spatial_target = container.item_get_spatial_entity_from_item_location(body);
auto candidate_x = container.spatial_entity_get_x(spatial_target);
auto candidate_y = container.spatial_entity_get_y(spatial_target);
x = container.spatial_entity_get_x(spatial);
y = container.spatial_entity_get_y(spatial);
auto target_direction_x = candidate_x - x;
auto target_direction_y = candidate_y - y;
auto target_angle = atan2f(target_direction_y, target_direction_x);
auto actual_angle = container.spatial_entity_get_direction(spatial);
if (
abs(target_angle - actual_angle) > attack_half_angle
&& abs(target_angle - actual_angle + PI *2.f) > attack_half_angle
&& abs(target_angle - actual_angle - PI *2.f) > attack_half_angle
) {
return;
}
auto target_distance_squared = target_direction_x * target_direction_x + target_direction_y * target_direction_y;
if (
target_distance_squared > attack_range * attack_range
) {
return;
}
auto hp = container.fighter_get_hp(target);
container.fighter_set_hp(target, hp - damage);
auto target_attack_energy = container.fighter_get_attack_energy_buffer(target);
auto target_attack_strength = floorf(target_attack_energy / 0.125f);
if (target_attack_strength <= attack_strength * 2.f) {
container.fighter_set_attack_energy_buffer(target, 0.f);
}
});
}
}
container.fighter_set_energy(fid, std::clamp(total_energy_budget + cashback - energy_loss, 0.f, 1.f));
});
}
void generate_world(dcon::data_container& container) {
/*
Energy is spent proportinally to movement and mass.
Energy efficiency modifies energy spent on movement.
1.f is energy efficiency of humans
*/
/*
Rats:
Weight: 50
Fur: 5
Meat: 20
Bones: 20
Waste: 5
*/
auto rat_meat = container.create_commodity();
container.commodity_set_weight(rat_meat, 1.f);
auto rat_bones = container.create_commodity();
container.commodity_set_weight(rat_bones, 1.f);
items::rat_body = container.create_item_type();
container.item_type_set_base_weight(items::rat_body, 60.f);
items::rat_fur = container.create_item_type();
container.item_type_set_base_weight(items::rat_fur, 5.f);
items::rat_body_skinned = container.create_item_type();
container.item_type_set_base_weight(items::rat_body_skinned, 40.f);
items::rat_skeleton = container.create_item_type();
container.item_type_set_base_weight(items::rat_skeleton, 20.f);
auto skinning_rat = container.create_action_extract();
item_set skinning_rat_result {};
skinning_rat_result.item[0] = items::rat_fur;
skinning_rat_result.item[1] = items::rat_body_skinned;
container.action_extract_set_item_output(skinning_rat, skinning_rat_result);
auto butcher_rat = container.create_action_extract();
commodity_set rat_meat_extracted {};
rat_meat_extracted.commodity[0] = rat_meat;
rat_meat_extracted.amount[0] = 5;
item_set butcher_rat_skeleton {};
skinning_rat_result.item[0] = items::rat_skeleton;
container.action_extract_set_input(butcher_rat, items::rat_body_skinned);
container.action_extract_set_commodity_output(butcher_rat, rat_meat_extracted);
container.action_extract_set_item_output(butcher_rat, butcher_rat_skeleton);
races::rats = container.create_race();
container.race_set_body_item(races::rats , items::rat_body);
container.race_set_movement_energy_cost(races::rats, 0.015f);
container.race_set_movement_energy_throughput(races::rats, 4.f);
container.race_set_max_hp(races::rats, 60.f);
container.item_type_set_race(items::rat_body, races::rats);
/*
Humans:
Weight: 30
Skin: 1
Meat: 10
Bones: 10
Waste: 9
*/
items::human_body = container.create_item_type();
container.item_type_set_base_weight(items::human_body, 30.f);
items::human_body_skinned = container.create_item_type();
container.item_type_set_base_weight(items::human_body_skinned, 20.f);
items::human_skeleton = container.create_item_type();
container.item_type_set_base_weight(items::human_skeleton, 10.f);
races::humans = container.create_race();
container.race_set_body_item(races::humans, items::human_body);
container.race_set_movement_energy_cost(races::humans, 0.01f);
container.race_set_movement_energy_throughput(races::humans, 1.f);
container.race_set_max_hp(races::humans, 40.f);
container.item_type_set_race(items::human_body, races::humans);
// auto skin_human = container.create_action_extract();
}
dcon::data_container container;
int main(int argc, char const* argv[]) {
generate_world(container);
check_ids();
// Spawn a few rats
for (int count = 0; count < 200; count++) {
auto x = normal_d(rng) * 10.f;
auto y = normal_d(rng) * 10.f;
auto fighter = container.create_fighter();
container.fighter_set_max_hp(fighter, 100);
container.fighter_set_hp(fighter, 100);
container.fighter_set_energy(fighter, 1.f);
container.fighter_set_tx(fighter, 0.f);
container.fighter_set_ty(fighter, 0.f);
container.fighter_set_model(fighter, MODEL_RAT);
auto body = container.create_item();
container.item_set_item_type(body, items::rat_body);
container.force_create_embodiment(fighter, body);