-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
1784 lines (1458 loc) · 54.9 KB
/
main.cpp
File metadata and controls
1784 lines (1458 loc) · 54.9 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 "data_ids.hpp"
#include <optional>
#define GLM_FORCE_SWIZZLE
#define GLEW_STATIC
#include <array>
#include <assert.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdexcept>
#define DEFAULT_BUFLEN 512
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <string>
#include <string_view>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <random>
#include "stb_image/stb_image.h"
#include "content.hpp"
#include "unordered_dense.h"
// Include all GLM core / GLSL features
#include <glm/glm.hpp> // vec2, vec3, mat4, radians
// Include all GLM extensions
#include <glm/ext.hpp> // perspective, translate, rotate
#include "glm/ext/matrix_transform.hpp"
#include "imgui/backends/imgui_impl_glfw.h"
#include "imgui/backends/imgui_impl_opengl3.h"
#include "imgui/misc/cpp/imgui_stdlib.h"
#include "implot/implot.h"
#include "implot/implot_internal.h"
#include "data.hpp"
#include "frustum.hpp"
#include "sprite.hpp"
constexpr inline float TILE_SIZE = 64.f;
constexpr inline float GRASS_TILE_SIZE = 16.f;
void APIENTRY glDebugOutput(
GLenum source,
GLenum type,
unsigned int id,
GLenum severity,
GLsizei length,
const char *message,
const void *userParam
) {
// ignore non-significant error/warning codes
if(id == 131169 || id == 131185 || id == 131218 || id == 131204) return;
std::cout << "---------------" << std::endl;
std::cout << "Debug message (" << id << "): " << message << std::endl;
switch (source)
{
case GL_DEBUG_SOURCE_API: std::cout << "Source: API"; break;
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: std::cout << "Source: Window System"; break;
case GL_DEBUG_SOURCE_SHADER_COMPILER: std::cout << "Source: Shader Compiler"; break;
case GL_DEBUG_SOURCE_THIRD_PARTY: std::cout << "Source: Third Party"; break;
case GL_DEBUG_SOURCE_APPLICATION: std::cout << "Source: Application"; break;
case GL_DEBUG_SOURCE_OTHER: std::cout << "Source: Other"; break;
} std::cout << std::endl;
switch (type)
{
case GL_DEBUG_TYPE_ERROR: std::cout << "Type: Error"; break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: std::cout << "Type: Deprecated Behaviour"; break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: std::cout << "Type: Undefined Behaviour"; break;
case GL_DEBUG_TYPE_PORTABILITY: std::cout << "Type: Portability"; break;
case GL_DEBUG_TYPE_PERFORMANCE: std::cout << "Type: Performance"; break;
case GL_DEBUG_TYPE_MARKER: std::cout << "Type: Marker"; break;
case GL_DEBUG_TYPE_PUSH_GROUP: std::cout << "Type: Push Group"; break;
case GL_DEBUG_TYPE_POP_GROUP: std::cout << "Type: Pop Group"; break;
case GL_DEBUG_TYPE_OTHER: std::cout << "Type: Other"; break;
} std::cout << std::endl;
switch (severity)
{
case GL_DEBUG_SEVERITY_HIGH: std::cout << "Severity: high"; break;
case GL_DEBUG_SEVERITY_MEDIUM: std::cout << "Severity: medium"; break;
case GL_DEBUG_SEVERITY_LOW: std::cout << "Severity: low"; break;
case GL_DEBUG_SEVERITY_NOTIFICATION: std::cout << "Severity: notification"; break;
} std::cout << std::endl;
std::cout << std::endl;
}
struct buffer_pair {
GLuint vao;
GLuint vbo;
};
std::string_view opengl_get_error_name(GLenum t) {
switch(t) {
case GL_INVALID_ENUM:
return "GL_INVALID_ENUM";
case GL_INVALID_VALUE:
return "GL_INVALID_VALUE";
case GL_INVALID_OPERATION:
return "GL_INVALID_OPERATION";
case GL_INVALID_FRAMEBUFFER_OPERATION:
return "GL_INVALID_FRAMEBUFFER_OPERATION";
case GL_OUT_OF_MEMORY:
return "GL_OUT_OF_MEMORY";
case GL_STACK_UNDERFLOW:
return "GL_STACK_UNDERFLOW";
case GL_STACK_OVERFLOW:
return "GL_STACK_OVERFLOW";
case GL_NO_ERROR:
return "GL_NO_ERROR";
default:
return "Unknown";
}
}
std::string to_string(std::string_view str) {
return std::string(str.begin(), str.end());
}
void opengl_error_print(std::string message) {
std::string full_message = message;
full_message += "\n";
full_message += opengl_get_error_name(glGetError());
printf("%s\n", ("OpenGL error:" + full_message).c_str());
}
void assert_no_errors() {
auto error = glGetError();
if (error != GL_NO_ERROR) {
auto message = opengl_get_error_name(error);
printf("%s\n", (to_string(message)).c_str());
assert(false);
}
}
const std::string read_shader(const std::string path) {
std::string shader_source;
std::ifstream shader_file;
shader_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
shader_file.open(path);
std::stringstream shader_source_stream;
shader_source_stream << shader_file.rdbuf();
shader_file.close();
shader_source = shader_source_stream.str();
} catch (std::ifstream::failure& e) {
throw std::runtime_error(e);
}
return shader_source;
}
GLuint create_shader(GLenum type, const char *source) {
GLuint result = glCreateShader(type);
glShaderSource(result, 1, &source, nullptr);
glCompileShader(result);
GLint status;
glGetShaderiv(result, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
GLint info_log_length;
glGetShaderiv(result, GL_INFO_LOG_LENGTH, &info_log_length);
std::string info_log(info_log_length, '\0');
glGetShaderInfoLog(result, info_log.size(), nullptr, info_log.data());
throw std::runtime_error("Shader compilation failed: " + info_log);
}
return result;
}
template <typename ... Shaders>
GLuint create_program(Shaders ... shaders)
{
GLuint result = glCreateProgram();
(glAttachShader(result, shaders), ...);
glLinkProgram(result);
GLint status;
glGetProgramiv(result, GL_LINK_STATUS, &status);
if (status != GL_TRUE)
{
GLint info_log_length;
glGetProgramiv(result, GL_INFO_LOG_LENGTH, &info_log_length);
std::string info_log(info_log_length, '\0');
glGetProgramInfoLog(result, info_log.size(), nullptr, info_log.data());
throw std::runtime_error("Program linkage failed: " + info_log);
}
return result;
}
void glew_fail(std::string_view message, GLenum error) {
throw std::runtime_error(to_string(message) + reinterpret_cast<const char *>(glewGetErrorString(error)));
}
void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
static int current_move_x = 0;
static int current_move_y = 0;
static int prev_move_x = 0;
static int prev_move_y = 0;
static bool running = false;
static bool prev_running = false;
static bool attacking = false;
static bool prev_attacking = false;
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
if (key == GLFW_KEY_UP) {
if (action == GLFW_PRESS) {
current_move_y += -1;
} else if (action == GLFW_RELEASE) {
current_move_y -= -1;
}
}
if (key == GLFW_KEY_DOWN) {
if (action == GLFW_PRESS) {
current_move_y -= -1;
} else if (action == GLFW_RELEASE) {
current_move_y += -1;
}
}
if (key == GLFW_KEY_LEFT) {
if (action == GLFW_PRESS) {
current_move_x -= 1;
} else if (action == GLFW_RELEASE) {
current_move_x += 1;
}
}
if (key == GLFW_KEY_RIGHT) {
if (action == GLFW_PRESS) {
current_move_x += 1;
} else if (action == GLFW_RELEASE) {
current_move_x -= 1;
}
}
if (key == GLFW_KEY_LEFT_SHIFT) {
if (action == GLFW_PRESS) {
running = true;
} else if (action == GLFW_RELEASE) {
running = false;
}
}
if (key == GLFW_KEY_SPACE) {
if (action == GLFW_PRESS) {
attacking = true;
} else if (action == GLFW_RELEASE) {
attacking = false;
}
}
}
struct vertex {
glm::vec3 position;
glm::vec3 normal_direction;
glm::vec2 texture_coordinate;
};
static std::vector<vertex> world_object_vertical_mesh = {
{{-0.5f, 0.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 1.f}},
{{0.5f, 0.f, 0.f}, {0.f, 1.f, 0.f}, {1.f, 1.f}},
{{-0.5f, 1.f, 1.f}, {0.f, 1.f, 0.f}, {0.f, 0.f}},
{{0.5f, 1.f, 1.f}, {0.f, 1.f, 0.f}, {1.f, 0.f}}
};
static std::vector<vertex> world_object_horizontal_mesh = {
{{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}, {0.f, 1.f}},
{{1.f, 0.f, 0.f}, {0.f, 0.f, 1.f}, {1.f, 1.f}},
{{0.f, 1.f, 0.f}, {0.f, 0.f, 1.f}, {0.f, 0.f}},
{{1.f, 1.f, 0.f}, {0.f, 0.f, 1.f}, {1.f, 0.f}}
};
static std::vector<vertex> world_object_centered_mesh = {
{{-1.f, -1.f, 0.f}, {0.f, 0.f, 1.f}, {-1.f, -1.f}},
{{1.f, -1.f, 0.f}, {0.f, 0.f, 1.f}, {1.f, -1.f}},
{{-1.f, 1.f, 0.f}, {0.f, 0.f, 1.f}, {-1.f, 1.f}},
{{1.f, 1.f, 0.f}, {0.f, 0.f, 1.f}, {1.f, 1.f}}
};
buffer_pair create_centered_square_buffer() {
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, world_object_centered_mesh.size() * sizeof(vertex), world_object_centered_mesh.data(), GL_STATIC_DRAW);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(sizeof(float) * 3));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(sizeof(float) * 3 + sizeof(float) * 3));
return {vao, vbo};
}
buffer_pair create_world_object_buffer() {
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, world_object_vertical_mesh.size() * sizeof(vertex), world_object_vertical_mesh.data(), GL_STATIC_DRAW);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(sizeof(float) * 3));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(sizeof(float) * 3 + sizeof(float) * 3));
return {vao, vbo};
}
buffer_pair create_world_tile_buffer() {
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, world_object_horizontal_mesh.size() * sizeof(vertex), world_object_horizontal_mesh.data(), GL_STATIC_DRAW);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(sizeof(float) * 3));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(sizeof(float) * 3 + sizeof(float) * 3));
return {vao, vbo};
}
constexpr inline int grass_quads =512;
buffer_pair create_grass_buffer(
std::default_random_engine& rng,
std::uniform_real_distribution<float>& uniform
) {
static std::vector<vertex> grass_mesh;
for (int count = 0; count < grass_quads; count++) {
grass_mesh.push_back(world_object_vertical_mesh[0]);
grass_mesh.push_back(world_object_vertical_mesh[1]);
grass_mesh.push_back(world_object_vertical_mesh[2]);
grass_mesh.push_back(world_object_vertical_mesh[2]);
grass_mesh.push_back(world_object_vertical_mesh[1]);
grass_mesh.push_back(world_object_vertical_mesh[3]);
auto shift = glm::vec3(uniform(rng), uniform(rng), 0.f);
for (int prev = 0; prev < 6; prev++) {
grass_mesh[grass_mesh.size() - 1 - prev].position += shift * GRASS_TILE_SIZE;
}
}
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, grass_mesh.size() * sizeof(vertex), grass_mesh.data(), GL_STATIC_DRAW);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(sizeof(float) * 3));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), reinterpret_cast<void*>(sizeof(float) * 3 + sizeof(float) * 3));
return {vao, vbo};
}
namespace geometry {
namespace screen_relative {
struct point {
glm::vec2 data;
};
}
namespace world{
struct point {
glm::vec2 data;
};
}
namespace screen_opengl {
struct point {
glm::vec2 data;
};
point convert(const geometry::world::point in, const geometry::world::point camera, const float aspect_ratio, const float zoom) {
auto result = (in.data - camera.data) * glm::vec2 { aspect_ratio, 1.f } * zoom;
return {
result
};
}
}
}
struct shader_2d_data {
GLuint shift;
GLuint zoom;
GLuint aspect_ratio;
};
constexpr inline uint8_t TCP_LOGIN = 0;
constexpr inline uint8_t TCP_FIGHTER = 1;
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);
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);
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 REQUEST_ACTIONS = 5;
struct data {
int32_t player;
int32_t target_entity;
float target_x;
float target_y;
uint8_t command_type;
uint8_t command_data;
uint8_t race;
uint8_t padding[1];
};
}
static dcon::data_container container {};
struct shader_basic_uniforms {
GLint model;
GLint view;
GLint projection;
};
shader_basic_uniforms get_basic_uniforms(GLuint shader) {
return {
glGetUniformLocation(shader, "model"),
glGetUniformLocation(shader, "view"),
glGetUniformLocation(shader, "projection")
};
}
int main(void)
{
ankerl::unordered_dense::map<int, dcon::visible_spatial_entity_id> index_to_spatial_entity;
ankerl::unordered_dense::map<int, dcon::known_fighter_id> index_to_fighter;
ankerl::unordered_dense::map<int, dcon::known_item_id> index_to_item;
std::optional<int> my_player_index;
std::optional<int> my_fighter_index;
std::optional<int> my_spatial_index;
// std::optional<uint8_t> my_race;
float my_x = 0.f;
float my_y = 0.f;
float my_energy;
std::vector<float> energy_history;
energy_history.resize(60 * 10);
std::vector<float> attack_energy_history;
attack_energy_history.resize(60 * 10);
std::default_random_engine rng;
std::uniform_real_distribution<float> uniform{0.0, 1.0};
std::normal_distribution<float> normal_d{0.f, 0.1f};
std::normal_distribution<float> size_d{1.f, 0.3f};
glfwSetErrorCallback(error_callback);
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
GLFWwindow* window;
float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor());
window = glfwCreateWindow(1280 * main_scale, 960 * main_scale, "3", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImPlot::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
// ImGui::StyleColorsDark();
ImGui::StyleColorsLight();
geometry::world::point camera { {0.f, 0.f} };
float zoom = 0.1f;
ImGuiStyle& style = ImGui::GetStyle();
style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330 core");
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
// GLEW validation
if (auto result = glewInit(); result != GLEW_NO_ERROR)
glew_fail("glewInit: ", result);
if (!GLEW_VERSION_3_3)
throw std::runtime_error("OpenGL 3.3 is not supported");
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
// illumination settings
glm::vec3 light_color = glm::vec3(1.f, 0.8f, 0.3f);
glm::vec3 ambient = glm::vec3(0.2f, 0.6f, 0.8f);
glClearColor(ambient.x, ambient.y, ambient.z, 0.f);
assert_no_errors();
auto object_mesh = create_world_object_buffer();
auto centered_square = create_centered_square_buffer();
auto tile_mesh = create_world_tile_buffer();
auto grass_mesh = create_grass_buffer(rng, uniform);
load_textures();
assert_no_errors();
// setting up a flat shader
std::string flat_shader_vertex_path = "./shaders/basic_shader_flat.vert";
std::string flat_shader_fragment_path = "./shaders/basic_shader_flat.frag";
std::string flat_vertex_shader_source = read_shader( flat_shader_vertex_path );
std::string flat_fragment_shader_source = read_shader( flat_shader_fragment_path );
auto flat_shader = create_program(
create_shader(GL_VERTEX_SHADER, flat_vertex_shader_source.c_str()),
create_shader(GL_FRAGMENT_SHADER, flat_fragment_shader_source.c_str())
);
shader_basic_uniforms flat_basic_location = get_basic_uniforms(flat_shader);
GLuint flat_albedo_location = glGetUniformLocation(flat_shader, "albedo");
// setting up a flat shader
std::string circle_shader_vertex_path = "./shaders/basic_shader_meshes.vert";
std::string circle_shader_fragment_path = "./shaders/circle.frag";
std::string circle_vertex_shader_source = read_shader( circle_shader_vertex_path );
std::string circle_fragment_shader_source = read_shader( circle_shader_fragment_path );
auto circle_shader = create_program(
create_shader(GL_VERTEX_SHADER, circle_vertex_shader_source.c_str()),
create_shader(GL_FRAGMENT_SHADER, circle_fragment_shader_source.c_str())
);
shader_basic_uniforms circle_basic_location = get_basic_uniforms(circle_shader);
GLuint circle_albedo_location = glGetUniformLocation(circle_shader, "albedo");
GLuint circle_direction_location = glGetUniformLocation(circle_shader, "direction");
GLuint circle_half_angle_location = glGetUniformLocation(circle_shader, "half_angle");
// setting up a default shader
std::string default_shader_vertex_path = "./shaders/basic_shader_meshes.vert";
std::string default_shader_fragment_path = "./shaders/basic_shader_meshes.frag";
std::string vertex_shader_source = read_shader( default_shader_vertex_path );
std::string fragment_shader_source = read_shader( default_shader_fragment_path );
auto default_shader = create_program(
create_shader(GL_VERTEX_SHADER, vertex_shader_source.c_str()),
create_shader(GL_FRAGMENT_SHADER, fragment_shader_source.c_str())
);
shader_basic_uniforms default_basic_location = get_basic_uniforms(default_shader);
GLuint albedo_texture_location = glGetUniformLocation(default_shader, "albedo_texture");
GLuint flip_location = glGetUniformLocation(default_shader, "flip");
assert_no_errors();
float albedo_world[] = {0.4f, 0.5f, 0.8f};
float albedo_character[] = {0.5f, 0.5f, 0.5f};
float albedo_critter[] = {0.5f, 0.1f, 0.1f};
glm::vec3 camera_position{0.f, 0.f, 15.f};
glm::vec3 camera_target{0.f, 0.f, 15.f};
int width = 1;
int height = 1;
assert_no_errors();
float update_timer = 0.f;
glm::vec3 light_direction {0.5f, 0.5f, 0.5f};
glm::vec2 camera_speed = {};
int tick = 0;
float data[512] {};
// Connection stuff
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
addrinfo *result = NULL;
addrinfo *ptr = NULL;
addrinfo hints_tcp;
ZeroMemory( &hints_tcp, sizeof(hints_tcp) );
hints_tcp.ai_family = AF_UNSPEC;
hints_tcp.ai_socktype = SOCK_STREAM;
hints_tcp.ai_protocol = IPPROTO_TCP;
addrinfo hints_udp;
ZeroMemory( &hints_udp, sizeof(hints_udp) );
hints_udp.ai_family = AF_UNSPEC;
hints_udp.ai_socktype = SOCK_DGRAM;
hints_udp.ai_protocol = IPPROTO_UDP;
SOCKET ConnectSocketTCP = INVALID_SOCKET;
SOCKET ConnectSocketUDP = INVALID_SOCKET;
std::string connection_address = "127.0.0.1";
int connection_port = 8080;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
tcp_update action_received {};
udp_update position_received {};
bool udp_socket_ready = false;
bool game_in_process = true;
std::thread position_updates([&](){
int last_timestamp_spatial = -1;
int last_timestamp_fighter = -1;
int last_timestamp_item = -1;
int last_timestamp_relink = -1;
int last_timestamp_relink_item = -1;
int last_timestamp_high_precision = -1;
while(game_in_process) {
if (!udp_socket_ready) {
Sleep(10);
}
iResult = recv(ConnectSocketUDP, recvbuf, recvbuflen, 0);
if (iResult > 0) {
auto time = glfwGetTime();
// printf("Bytes received: %d\n", iResult);
memcpy(&position_received, recvbuf, sizeof(udp_update));
auto sent_to = position_received.sent_to_player;
if (sent_to != my_player_index) {
continue;
}
if (position_received.update_type == UPDATE_SPATIAL) {
if (position_received.timestamp < last_timestamp_spatial && last_timestamp_spatial < position_received.timestamp + 16000) {
continue;
} else {
last_timestamp_spatial = position_received.timestamp;
}
auto spatial = position_received.payload.spatial;
auto it = index_to_spatial_entity.find(spatial.spatial_entity_id);
if (it != index_to_spatial_entity.end()) {
auto entity = it->second;
container.visible_spatial_entity_set_direction(entity, ((float)spatial.direction * 2.f * glm::pi<float>()) / 255.f);
auto last_x = container.visible_spatial_entity_get_target_x(entity);
auto last_y = container.visible_spatial_entity_get_target_y(entity);
auto next_x = my_x + (float)spatial.x / 100.f;
auto next_y = my_y + (float)spatial.y / 100.f;
auto path = container.visible_spatial_entity_get_path_length(entity);
if (spatial.spatial_entity_id != my_spatial_index) {
container.visible_spatial_entity_set_previous_x(entity, last_x);
container.visible_spatial_entity_set_previous_y(entity, last_y);
container.visible_spatial_entity_set_target_x(entity, next_x);
container.visible_spatial_entity_set_target_y(entity, next_y);
container.visible_spatial_entity_set_last_update_time(entity, container.visible_spatial_entity_get_current_update_time(entity));
container.visible_spatial_entity_set_current_update_time(entity, time);
}
} else {
auto next_x = my_x + (float)spatial.x / 100.f;
auto next_y = my_y + (float)spatial.y / 100.f;
auto entity = container.create_visible_spatial_entity();
container.visible_spatial_entity_set_direction(entity, ((float)spatial.direction * 2.f * glm::pi<float>()) / 255.f);
container.visible_spatial_entity_set_path_length(entity, 0.f);
assert(std::isfinite(next_x));
assert(std::isfinite(next_y));
container.visible_spatial_entity_set_target_x(entity, next_x);
container.visible_spatial_entity_set_target_y(entity, next_y);
container.visible_spatial_entity_set_last_update_time(entity, container.visible_spatial_entity_get_current_update_time(entity));
container.visible_spatial_entity_set_current_update_time(entity, time);
index_to_spatial_entity[spatial.spatial_entity_id] = entity;
}
} else if (position_received.update_type == UPDATE_FIGHTER) {
if (position_received.timestamp < last_timestamp_fighter && last_timestamp_fighter < position_received.timestamp + 16000) {
continue;
} else {
last_timestamp_fighter = position_received.timestamp;
}
auto payload = position_received.payload.fighter;
auto it2 = index_to_fighter.find(payload.fighter_id);
if (it2 != index_to_fighter.end()) {
auto entity = it2->second;
container.known_fighter_set_hp_current(entity, payload.hp);
container.known_fighter_set_hp_max(entity, payload.max_hp);
if (payload.fighter_id == my_fighter_index) {
my_energy = (float)payload.energy / 255.f;
for (int index = 0; index < energy_history.size() - 1; index++) {
energy_history[index] = energy_history[index + 1];
}
energy_history.back() = my_energy;
for (int index = 0; index < attack_energy_history.size() - 1; index++) {
attack_energy_history[index] = attack_energy_history[index + 1];
}
attack_energy_history.back() = (float)payload.attack_energy / 255.f;
}
container.known_fighter_set_attack_energy(entity, (float)payload.attack_energy / 255.f);
} else {
auto entity = container.create_known_fighter();
container.known_fighter_set_hp_current(entity, payload.hp);
container.known_fighter_set_hp_max(entity, payload.max_hp);
container.known_fighter_set_attack_energy(entity, (float)payload.attack_energy / 255.f);
index_to_fighter[payload.fighter_id] = entity;
}
} else if (position_received.update_type == UPDATE_RELINK) {
if (position_received.timestamp < last_timestamp_relink && last_timestamp_relink < position_received.timestamp + 16000) {
continue;
} else {
last_timestamp_relink = position_received.timestamp;
}
auto it = index_to_item.find(position_received.payload.relink.item_id);
auto it2 = index_to_fighter.find(position_received.payload.relink.fighter_id);
if (
it != index_to_item.end()
&& it2 != index_to_fighter.end()
) {
container.force_create_embodiment(it2->second, it->second);
}
} else if (position_received.update_type == UPDATE_HIGH_PRECISION) {
if (position_received.timestamp < last_timestamp_high_precision && last_timestamp_high_precision < position_received.timestamp + 16000) {
continue;
} else {
last_timestamp_high_precision = position_received.timestamp;
}
auto it = index_to_spatial_entity.find(position_received.payload.high_precision.spatial_entity_id);
if (position_received.payload.high_precision.spatial_entity_id == my_spatial_index && it != index_to_spatial_entity.end()) {
auto prev_x = container.visible_spatial_entity_get_target_x(it->second);
auto prev_y = container.visible_spatial_entity_get_target_y(it->second);
container.visible_spatial_entity_set_previous_x(it->second, prev_x);
container.visible_spatial_entity_set_previous_y(it->second, prev_y);
container.visible_spatial_entity_set_target_x(it->second, position_received.payload.high_precision.x);
container.visible_spatial_entity_set_target_y(it->second, position_received.payload.high_precision.y);
container.visible_spatial_entity_set_last_update_time(it->second, container.visible_spatial_entity_get_current_update_time(it->second));
container.visible_spatial_entity_set_current_update_time(it->second, time);
my_x = position_received.payload.high_precision.x;
my_y = position_received.payload.high_precision.y;
}
} else if (position_received.update_type == UPDATE_ITEM_RELINK) {
if (position_received.timestamp < last_timestamp_relink_item && last_timestamp_relink_item < position_received.timestamp + 16000) {
continue;
} else {
last_timestamp_relink_item = position_received.timestamp;
}
auto it = index_to_spatial_entity.find(position_received.payload.relink_item.spatial_id);
auto it2 = index_to_item.find(position_received.payload.relink_item.item_id);
if (
it != index_to_spatial_entity.end()
&& it2 != index_to_item.end()
) {
container.force_create_item_location(it2->second, it->second);
}
} else if (position_received.update_type == UPDATE_ITEM) {
if (position_received.timestamp < last_timestamp_item && last_timestamp_item < position_received.timestamp + 16000) {
continue;
} else {