forked from fogleman/Craft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2708 lines (2548 loc) · 79.8 KB
/
main.c
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 <GL/glew.h>
#include <GLFW/glfw3.h>
#include <curl/curl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "auth.h"
#include "client.h"
#include "config.h"
#include "cube.h"
#include "db.h"
#include "item.h"
#include "map.h"
#include "matrix.h"
#include "noise.h"
#include "sign.h"
#include "util.h"
#include "world.h"
#define MAX_CHUNKS 8192
#define MAX_PLAYERS 128
#define MAX_TEXT_LENGTH 256
#define MAX_NAME_LENGTH 32
#define MAX_PATH_LENGTH 256
#define MAX_ADDR_LENGTH 256
#define ALIGN_LEFT 0
#define ALIGN_CENTER 1
#define ALIGN_RIGHT 2
#define MODE_OFFLINE 0
#define MODE_ONLINE 1
typedef struct {
Map map;
Map lights;
SignList signs;
int p;
int q;
int faces;
int sign_faces;
int dirty;
int miny;
int maxy;
GLuint buffer;
GLuint sign_buffer;
} Chunk;
typedef struct {
int x;
int y;
int z;
int w;
} Block;
typedef struct {
float x;
float y;
float z;
float rx;
float ry;
float t;
} State;
typedef struct {
int id;
char name[MAX_NAME_LENGTH];
State state;
State state1;
State state2;
GLuint buffer;
} Player;
typedef struct {
GLuint program;
GLuint position;
GLuint normal;
GLuint uv;
GLuint matrix;
GLuint sampler;
GLuint camera;
GLuint timer;
GLuint extra1;
GLuint extra2;
GLuint extra3;
GLuint extra4;
} Attrib;
typedef struct {
GLFWwindow *window;
Chunk chunks[MAX_CHUNKS];
int chunk_count;
int create_radius;
int render_radius;
int delete_radius;
int sign_radius;
Player players[MAX_PLAYERS];
int player_count;
int typing;
char typing_buffer[MAX_TEXT_LENGTH];
int message_index;
char messages[MAX_MESSAGES][MAX_TEXT_LENGTH];
int width;
int height;
int observe1;
int observe2;
int flying;
int item_index;
int scale;
int ortho;
float fov;
int suppress_char;
int mode;
int mode_changed;
char db_path[MAX_PATH_LENGTH];
char server_addr[MAX_ADDR_LENGTH];
int server_port;
int day_length;
Block block0;
Block block1;
Block copy0;
Block copy1;
} Model;
static Model model;
static Model *g = &model;
int chunked(float x) {
return floorf(roundf(x) / CHUNK_SIZE);
}
float time_of_day() {
if (g->day_length <= 0) {
return 0.5;
}
float t;
t = glfwGetTime();
t = t / g->day_length;
t = t - (int)t;
return t;
}
float get_daylight() {
float timer = time_of_day();
if (timer < 0.5) {
float t = (timer - 0.25) * 100;
return 1 / (1 + powf(2, -t));
}
else {
float t = (timer - 0.90) * 100;
return 1 - 1 / (1 + powf(2, -t));
}
}
int get_scale_factor() {
int window_width, window_height;
int buffer_width, buffer_height;
glfwGetWindowSize(g->window, &window_width, &window_height);
glfwGetFramebufferSize(g->window, &buffer_width, &buffer_height);
int result = buffer_width / window_width;
result = MAX(1, result);
result = MIN(2, result);
return result;
}
void get_sight_vector(float rx, float ry, float *vx, float *vy, float *vz) {
float m = cosf(ry);
*vx = cosf(rx - RADIANS(90)) * m;
*vy = sinf(ry);
*vz = sinf(rx - RADIANS(90)) * m;
}
void get_motion_vector(int flying, int sz, int sx, float rx, float ry,
float *vx, float *vy, float *vz) {
*vx = 0; *vy = 0; *vz = 0;
if (!sz && !sx) {
return;
}
float strafe = atan2f(sz, sx);
if (flying) {
float m = cosf(ry);
float y = sinf(ry);
if (sx) {
if (!sz) {
y = 0;
}
m = 1;
}
if (sz > 0) {
y = -y;
}
*vx = cosf(rx + strafe) * m;
*vy = y;
*vz = sinf(rx + strafe) * m;
}
else {
*vx = cosf(rx + strafe);
*vy = 0;
*vz = sinf(rx + strafe);
}
}
GLuint gen_crosshair_buffer() {
int x = g->width / 2;
int y = g->height / 2;
int p = 10 * g->scale;
float data[] = {
x, y - p, x, y + p,
x - p, y, x + p, y
};
return gen_buffer(sizeof(data), data);
}
GLuint gen_wireframe_buffer(float x, float y, float z, float n) {
float data[72];
make_cube_wireframe(data, x, y, z, n);
return gen_buffer(sizeof(data), data);
}
GLuint gen_sky_buffer() {
float data[12288];
make_sphere(data, 1, 3);
return gen_buffer(sizeof(data), data);
}
GLuint gen_cube_buffer(float x, float y, float z, float n, int w) {
GLfloat *data = malloc_faces(10, 6);
float ao[6][4] = {0};
float light[6][4] = {
{0.5, 0.5, 0.5, 0.5},
{0.5, 0.5, 0.5, 0.5},
{0.5, 0.5, 0.5, 0.5},
{0.5, 0.5, 0.5, 0.5},
{0.5, 0.5, 0.5, 0.5},
{0.5, 0.5, 0.5, 0.5}
};
make_cube(data, ao, light, 1, 1, 1, 1, 1, 1, x, y, z, n, w);
return gen_faces(10, 6, data);
}
GLuint gen_plant_buffer(float x, float y, float z, float n, int w) {
GLfloat *data = malloc_faces(10, 4);
float ao = 0;
float light = 1;
make_plant(data, ao, light, x, y, z, n, w, 45);
return gen_faces(10, 4, data);
}
GLuint gen_player_buffer(float x, float y, float z, float rx, float ry) {
GLfloat *data = malloc_faces(10, 6);
make_player(data, x, y, z, rx, ry);
return gen_faces(10, 6, data);
}
GLuint gen_text_buffer(float x, float y, float n, char *text) {
int length = strlen(text);
GLfloat *data = malloc_faces(4, length);
for (int i = 0; i < length; i++) {
make_character(data + i * 24, x, y, n / 2, n, text[i]);
x += n;
}
return gen_faces(4, length, data);
}
void draw_triangles_3d_ao(Attrib *attrib, GLuint buffer, int count) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(attrib->position);
glEnableVertexAttribArray(attrib->normal);
glEnableVertexAttribArray(attrib->uv);
glVertexAttribPointer(attrib->position, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 10, 0);
glVertexAttribPointer(attrib->normal, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 10, (GLvoid *)(sizeof(GLfloat) * 3));
glVertexAttribPointer(attrib->uv, 4, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 10, (GLvoid *)(sizeof(GLfloat) * 6));
glDrawArrays(GL_TRIANGLES, 0, count);
glDisableVertexAttribArray(attrib->position);
glDisableVertexAttribArray(attrib->normal);
glDisableVertexAttribArray(attrib->uv);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_triangles_3d_text(Attrib *attrib, GLuint buffer, int count) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(attrib->position);
glEnableVertexAttribArray(attrib->uv);
glVertexAttribPointer(attrib->position, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 5, 0);
glVertexAttribPointer(attrib->uv, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 5, (GLvoid *)(sizeof(GLfloat) * 3));
glDrawArrays(GL_TRIANGLES, 0, count);
glDisableVertexAttribArray(attrib->position);
glDisableVertexAttribArray(attrib->uv);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_triangles_3d(Attrib *attrib, GLuint buffer, int count) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(attrib->position);
glEnableVertexAttribArray(attrib->normal);
glEnableVertexAttribArray(attrib->uv);
glVertexAttribPointer(attrib->position, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, 0);
glVertexAttribPointer(attrib->normal, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, (GLvoid *)(sizeof(GLfloat) * 3));
glVertexAttribPointer(attrib->uv, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, (GLvoid *)(sizeof(GLfloat) * 6));
glDrawArrays(GL_TRIANGLES, 0, count);
glDisableVertexAttribArray(attrib->position);
glDisableVertexAttribArray(attrib->normal);
glDisableVertexAttribArray(attrib->uv);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_triangles_2d(Attrib *attrib, GLuint buffer, int count) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(attrib->position);
glEnableVertexAttribArray(attrib->uv);
glVertexAttribPointer(attrib->position, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 4, 0);
glVertexAttribPointer(attrib->uv, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 4, (GLvoid *)(sizeof(GLfloat) * 2));
glDrawArrays(GL_TRIANGLES, 0, count);
glDisableVertexAttribArray(attrib->position);
glDisableVertexAttribArray(attrib->uv);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_lines(Attrib *attrib, GLuint buffer, int components, int count) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(attrib->position);
glVertexAttribPointer(
attrib->position, components, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINES, 0, count);
glDisableVertexAttribArray(attrib->position);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_chunk(Attrib *attrib, Chunk *chunk) {
draw_triangles_3d_ao(attrib, chunk->buffer, chunk->faces * 6);
}
void draw_item(Attrib *attrib, GLuint buffer, int count) {
draw_triangles_3d_ao(attrib, buffer, count);
}
void draw_text(Attrib *attrib, GLuint buffer, int length) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
draw_triangles_2d(attrib, buffer, length * 6);
glDisable(GL_BLEND);
}
void draw_signs(Attrib *attrib, Chunk *chunk) {
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(-8, -1024);
draw_triangles_3d_text(attrib, chunk->sign_buffer, chunk->sign_faces * 6);
glDisable(GL_POLYGON_OFFSET_FILL);
}
void draw_sign(Attrib *attrib, GLuint buffer, int length) {
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(-8, -1024);
draw_triangles_3d_text(attrib, buffer, length * 6);
glDisable(GL_POLYGON_OFFSET_FILL);
}
void draw_cube(Attrib *attrib, GLuint buffer) {
draw_item(attrib, buffer, 36);
}
void draw_plant(Attrib *attrib, GLuint buffer) {
draw_item(attrib, buffer, 24);
}
void draw_player(Attrib *attrib, Player *player) {
draw_cube(attrib, player->buffer);
}
Player *find_player(int id) {
for (int i = 0; i < g->player_count; i++) {
Player *player = g->players + i;
if (player->id == id) {
return player;
}
}
return 0;
}
void update_player(Player *player,
float x, float y, float z, float rx, float ry, int interpolate)
{
if (interpolate) {
State *s1 = &player->state1;
State *s2 = &player->state2;
memcpy(s1, s2, sizeof(State));
s2->x = x; s2->y = y; s2->z = z; s2->rx = rx; s2->ry = ry;
s2->t = glfwGetTime();
if (s2->rx - s1->rx > PI) {
s1->rx += 2 * PI;
}
if (s1->rx - s2->rx > PI) {
s1->rx -= 2 * PI;
}
}
else {
State *s = &player->state;
s->x = x; s->y = y; s->z = z; s->rx = rx; s->ry = ry;
del_buffer(player->buffer);
player->buffer = gen_player_buffer(s->x, s->y, s->z, s->rx, s->ry);
}
}
void interpolate_player(Player *player) {
State *s1 = &player->state1;
State *s2 = &player->state2;
float t1 = s2->t - s1->t;
float t2 = glfwGetTime() - s2->t;
t1 = MIN(t1, 1);
t1 = MAX(t1, 0.1);
float p = MIN(t2 / t1, 1);
update_player(
player,
s1->x + (s2->x - s1->x) * p,
s1->y + (s2->y - s1->y) * p,
s1->z + (s2->z - s1->z) * p,
s1->rx + (s2->rx - s1->rx) * p,
s1->ry + (s2->ry - s1->ry) * p,
0);
}
void delete_player(int id) {
Player *player = find_player(id);
if (!player) {
return;
}
int count = g->player_count;
del_buffer(player->buffer);
Player *other = g->players + (--count);
memcpy(player, other, sizeof(Player));
g->player_count = count;
}
void delete_all_players() {
for (int i = 0; i < g->player_count; i++) {
Player *player = g->players + i;
del_buffer(player->buffer);
}
g->player_count = 0;
}
float player_player_distance(Player *p1, Player *p2) {
State *s1 = &p1->state;
State *s2 = &p2->state;
float x = s2->x - s1->x;
float y = s2->y - s1->y;
float z = s2->z - s1->z;
return sqrtf(x * x + y * y + z * z);
}
float player_crosshair_distance(Player *p1, Player *p2) {
State *s1 = &p1->state;
State *s2 = &p2->state;
float d = player_player_distance(p1, p2);
float vx, vy, vz;
get_sight_vector(s1->rx, s1->ry, &vx, &vy, &vz);
vx *= d; vy *= d; vz *= d;
float px, py, pz;
px = s1->x + vx; py = s1->y + vy; pz = s1->z + vz;
float x = s2->x - px;
float y = s2->y - py;
float z = s2->z - pz;
return sqrtf(x * x + y * y + z * z);
}
Player *player_crosshair(Player *player) {
Player *result = 0;
float threshold = RADIANS(5);
float best = 0;
for (int i = 0; i < g->player_count; i++) {
Player *other = g->players + i;
if (other == player) {
continue;
}
float p = player_crosshair_distance(player, other);
float d = player_player_distance(player, other);
if (d < 96 && p / d < threshold) {
if (best == 0 || d < best) {
best = d;
result = other;
}
}
}
return result;
}
Chunk *find_chunk(int p, int q) {
for (int i = 0; i < g->chunk_count; i++) {
Chunk *chunk = g->chunks + i;
if (chunk->p == p && chunk->q == q) {
return chunk;
}
}
return 0;
}
int chunk_distance(Chunk *chunk, int p, int q) {
int dp = ABS(chunk->p - p);
int dq = ABS(chunk->q - q);
return MAX(dp, dq);
}
int chunk_visible(float planes[6][4], int p, int q, int miny, int maxy) {
int x = p * CHUNK_SIZE - 1;
int z = q * CHUNK_SIZE - 1;
int d = CHUNK_SIZE + 1;
float points[8][3] = {
{x + 0, miny, z + 0},
{x + d, miny, z + 0},
{x + 0, miny, z + d},
{x + d, miny, z + d},
{x + 0, maxy, z + 0},
{x + d, maxy, z + 0},
{x + 0, maxy, z + d},
{x + d, maxy, z + d}
};
int n = g->ortho ? 4 : 6;
for (int i = 0; i < n; i++) {
int in = 0;
int out = 0;
for (int j = 0; j < 8; j++) {
float d =
planes[i][0] * points[j][0] +
planes[i][1] * points[j][1] +
planes[i][2] * points[j][2] +
planes[i][3];
if (d < 0) {
out++;
}
else {
in++;
}
if (in && out) {
break;
}
}
if (in == 0) {
return 0;
}
}
return 1;
}
int highest_block(float x, float z) {
int result = -1;
int nx = roundf(x);
int nz = roundf(z);
int p = chunked(x);
int q = chunked(z);
Chunk *chunk = find_chunk(p, q);
if (chunk) {
Map *map = &chunk->map;
MAP_FOR_EACH(map, e) {
if (is_obstacle(e->w) && e->x == nx && e->z == nz) {
result = MAX(result, e->y);
}
} END_MAP_FOR_EACH;
}
return result;
}
int _hit_test(
Map *map, float max_distance, int previous,
float x, float y, float z,
float vx, float vy, float vz,
int *hx, int *hy, int *hz)
{
int m = 32;
int px = 0;
int py = 0;
int pz = 0;
for (int i = 0; i < max_distance * m; i++) {
int nx = roundf(x);
int ny = roundf(y);
int nz = roundf(z);
if (nx != px || ny != py || nz != pz) {
int hw = map_get(map, nx, ny, nz);
if (hw > 0) {
if (previous) {
*hx = px; *hy = py; *hz = pz;
}
else {
*hx = nx; *hy = ny; *hz = nz;
}
return hw;
}
px = nx; py = ny; pz = nz;
}
x += vx / m; y += vy / m; z += vz / m;
}
return 0;
}
int hit_test(
int previous, float x, float y, float z, float rx, float ry,
int *bx, int *by, int *bz)
{
int result = 0;
float best = 0;
int p = chunked(x);
int q = chunked(z);
float vx, vy, vz;
get_sight_vector(rx, ry, &vx, &vy, &vz);
for (int i = 0; i < g->chunk_count; i++) {
Chunk *chunk = g->chunks + i;
if (chunk_distance(chunk, p, q) > 1) {
continue;
}
int hx, hy, hz;
int hw = _hit_test(&chunk->map, 8, previous,
x, y, z, vx, vy, vz, &hx, &hy, &hz);
if (hw > 0) {
float d = sqrtf(
powf(hx - x, 2) + powf(hy - y, 2) + powf(hz - z, 2));
if (best == 0 || d < best) {
best = d;
*bx = hx; *by = hy; *bz = hz;
result = hw;
}
}
}
return result;
}
int hit_test_face(Player *player, int *x, int *y, int *z, int *face) {
State *s = &player->state;
int w = hit_test(0, s->x, s->y, s->z, s->rx, s->ry, x, y, z);
if (is_obstacle(w)) {
int hx, hy, hz;
hit_test(1, s->x, s->y, s->z, s->rx, s->ry, &hx, &hy, &hz);
int dx = hx - *x;
int dy = hy - *y;
int dz = hz - *z;
if (dx == -1 && dy == 0 && dz == 0) {
*face = 0; return 1;
}
if (dx == 1 && dy == 0 && dz == 0) {
*face = 1; return 1;
}
if (dx == 0 && dy == 0 && dz == -1) {
*face = 2; return 1;
}
if (dx == 0 && dy == 0 && dz == 1) {
*face = 3; return 1;
}
if (dx == 0 && dy == 1 && dz == 0) {
int degrees = roundf(DEGREES(atan2f(s->x - hx, s->z - hz)));
if (degrees < 0) {
degrees += 360;
}
int top = ((degrees + 45) / 90) % 4;
*face = 4 + top; return 1;
}
}
return 0;
}
int collide(int height, float *x, float *y, float *z) {
int result = 0;
int p = chunked(*x);
int q = chunked(*z);
Chunk *chunk = find_chunk(p, q);
if (!chunk) {
return result;
}
Map *map = &chunk->map;
int nx = roundf(*x);
int ny = roundf(*y);
int nz = roundf(*z);
float px = *x - nx;
float py = *y - ny;
float pz = *z - nz;
float pad = 0.25;
for (int dy = 0; dy < height; dy++) {
if (px < -pad && is_obstacle(map_get(map, nx - 1, ny - dy, nz))) {
*x = nx - pad;
}
if (px > pad && is_obstacle(map_get(map, nx + 1, ny - dy, nz))) {
*x = nx + pad;
}
if (py < -pad && is_obstacle(map_get(map, nx, ny - dy - 1, nz))) {
*y = ny - pad;
result = 1;
}
if (py > pad && is_obstacle(map_get(map, nx, ny - dy + 1, nz))) {
*y = ny + pad;
result = 1;
}
if (pz < -pad && is_obstacle(map_get(map, nx, ny - dy, nz - 1))) {
*z = nz - pad;
}
if (pz > pad && is_obstacle(map_get(map, nx, ny - dy, nz + 1))) {
*z = nz + pad;
}
}
return result;
}
int player_intersects_block(
int height,
float x, float y, float z,
int hx, int hy, int hz)
{
int nx = roundf(x);
int ny = roundf(y);
int nz = roundf(z);
for (int i = 0; i < height; i++) {
if (nx == hx && ny - i == hy && nz == hz) {
return 1;
}
}
return 0;
}
int _gen_sign_buffer(
GLfloat *data, float x, float y, float z, int face, const char *text)
{
static const int glyph_dx[8] = {0, 0, -1, 1, 1, 0, -1, 0};
static const int glyph_dz[8] = {1, -1, 0, 0, 0, -1, 0, 1};
static const int line_dx[8] = {0, 0, 0, 0, 0, 1, 0, -1};
static const int line_dy[8] = {-1, -1, -1, -1, 0, 0, 0, 0};
static const int line_dz[8] = {0, 0, 0, 0, 1, 0, -1, 0};
if (face < 0 || face >= 8) {
return 0;
}
int count = 0;
float max_width = 64;
float line_height = 1.25;
char lines[1024];
int rows = wrap(text, max_width, lines, 1024);
rows = MIN(rows, 5);
int dx = glyph_dx[face];
int dz = glyph_dz[face];
int ldx = line_dx[face];
int ldy = line_dy[face];
int ldz = line_dz[face];
float n = 1.0 / (max_width / 10);
float sx = x - n * (rows - 1) * (line_height / 2) * ldx;
float sy = y - n * (rows - 1) * (line_height / 2) * ldy;
float sz = z - n * (rows - 1) * (line_height / 2) * ldz;
char *key;
char *line = tokenize(lines, "\n", &key);
while (line) {
int length = strlen(line);
int line_width = string_width(line);
line_width = MIN(line_width, max_width);
float rx = sx - dx * line_width / max_width / 2;
float ry = sy;
float rz = sz - dz * line_width / max_width / 2;
for (int i = 0; i < length; i++) {
int width = char_width(line[i]);
line_width -= width;
if (line_width < 0) {
break;
}
rx += dx * width / max_width / 2;
rz += dz * width / max_width / 2;
if (line[i] != ' ') {
make_character_3d(
data + count * 30, rx, ry, rz, n / 2, face, line[i]);
count++;
}
rx += dx * width / max_width / 2;
rz += dz * width / max_width / 2;
}
sx += n * line_height * ldx;
sy += n * line_height * ldy;
sz += n * line_height * ldz;
line = tokenize(NULL, "\n", &key);
rows--;
if (rows <= 0) {
break;
}
}
return count;
}
void gen_sign_buffer(Chunk *chunk) {
SignList *signs = &chunk->signs;
// first pass - count characters
int max_faces = 0;
for (int i = 0; i < signs->size; i++) {
Sign *e = signs->data + i;
max_faces += strlen(e->text);
}
// second pass - generate geometry
GLfloat *data = malloc_faces(5, max_faces);
int faces = 0;
for (int i = 0; i < signs->size; i++) {
Sign *e = signs->data + i;
faces += _gen_sign_buffer(
data + faces * 30, e->x, e->y, e->z, e->face, e->text);
}
del_buffer(chunk->sign_buffer);
chunk->sign_buffer = gen_faces(5, faces, data);
chunk->sign_faces = faces;
}
int has_lights(Chunk *chunk) {
if (!SHOW_LIGHTS) {
return 0;
}
for (int dp = -1; dp <= 1; dp++) {
for (int dq = -1; dq <= 1; dq++) {
Chunk *other = chunk;
if (dp || dq) {
other = find_chunk(chunk->p + dp, chunk->q + dq);
}
if (!other) {
continue;
}
Map *map = &other->lights;
if (map->size) {
return 1;
}
}
}
return 0;
}
void dirty_chunk(Chunk *chunk) {
chunk->dirty = 1;
if (has_lights(chunk)) {
for (int dp = -1; dp <= 1; dp++) {
for (int dq = -1; dq <= 1; dq++) {
Chunk *other = find_chunk(chunk->p + dp, chunk->q + dq);
if (other) {
other->dirty = 1;
}
}
}
}
}
void occlusion(
char neighbors[27], char lights[27], float shades[27],
float ao[6][4], float light[6][4])
{
static const int lookup3[6][4][3] = {
{{0, 1, 3}, {2, 1, 5}, {6, 3, 7}, {8, 5, 7}},
{{18, 19, 21}, {20, 19, 23}, {24, 21, 25}, {26, 23, 25}},
{{6, 7, 15}, {8, 7, 17}, {24, 15, 25}, {26, 17, 25}},
{{0, 1, 9}, {2, 1, 11}, {18, 9, 19}, {20, 11, 19}},
{{0, 3, 9}, {6, 3, 15}, {18, 9, 21}, {24, 15, 21}},
{{2, 5, 11}, {8, 5, 17}, {20, 11, 23}, {26, 17, 23}}
};
static const int lookup4[6][4][4] = {
{{0, 1, 3, 4}, {1, 2, 4, 5}, {3, 4, 6, 7}, {4, 5, 7, 8}},
{{18, 19, 21, 22}, {19, 20, 22, 23}, {21, 22, 24, 25}, {22, 23, 25, 26}},
{{6, 7, 15, 16}, {7, 8, 16, 17}, {15, 16, 24, 25}, {16, 17, 25, 26}},
{{0, 1, 9, 10}, {1, 2, 10, 11}, {9, 10, 18, 19}, {10, 11, 19, 20}},
{{0, 3, 9, 12}, {3, 6, 12, 15}, {9, 12, 18, 21}, {12, 15, 21, 24}},
{{2, 5, 11, 14}, {5, 8, 14, 17}, {11, 14, 20, 23}, {14, 17, 23, 26}}
};
static const float curve[4] = {0.0, 0.25, 0.5, 0.75};
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) {
int corner = neighbors[lookup3[i][j][0]];
int side1 = neighbors[lookup3[i][j][1]];
int side2 = neighbors[lookup3[i][j][2]];
int value = side1 && side2 ? 3 : corner + side1 + side2;
float shade_sum = 0;
float light_sum = 0;
int is_light = lights[13] == 15;
for (int k = 0; k < 4; k++) {
shade_sum += shades[lookup4[i][j][k]];
light_sum += lights[lookup4[i][j][k]];
}
if (is_light) {
light_sum = 15 * 4 * 10;
}
float total = curve[value] + shade_sum / 4.0;
ao[i][j] = MIN(total, 1.0);
light[i][j] = light_sum / 15.0 / 4.0;
}
}
}
#define XZ_SIZE (CHUNK_SIZE * 3 + 2)
#define Y_SIZE 258
void light_fill(
char opaque[XZ_SIZE][Y_SIZE][XZ_SIZE],
char light[XZ_SIZE][Y_SIZE][XZ_SIZE],
int x, int y, int z, int value, int force)
{
if (x < 0 || y < 0 || z < 0) {
return;
}
if (x >= XZ_SIZE || y >= Y_SIZE || z >= XZ_SIZE) {
return;
}
if (light[x][y][z] >= value) {
return;
}
if (!force && opaque[x][y][z]) {
return;
}
light[x][y][z] = value;
value--;
light_fill(opaque, light, x - 1, y, z, value, 0);
light_fill(opaque, light, x + 1, y, z, value, 0);
light_fill(opaque, light, x, y - 1, z, value, 0);
light_fill(opaque, light, x, y + 1, z, value, 0);
light_fill(opaque, light, x, y, z - 1, value, 0);
light_fill(opaque, light, x, y, z + 1, value, 0);
}
void gen_chunk_buffer(Chunk *chunk) {
static char opaque[XZ_SIZE][Y_SIZE][XZ_SIZE];
static char light[XZ_SIZE][Y_SIZE][XZ_SIZE];
static char highest[XZ_SIZE][XZ_SIZE];
static char neighbors[27];
static char lights[27];
static float shades[27];
memset(opaque, 0, sizeof(opaque));
memset(light, 0, sizeof(light));
memset(highest, 0, sizeof(highest));
memset(neighbors, 0, sizeof(neighbors));
memset(lights, 0, sizeof(lights));
memset(shades, 0, sizeof(shades));
int ox = chunk->p * CHUNK_SIZE - CHUNK_SIZE - 1;
int oy = -1;
int oz = chunk->q * CHUNK_SIZE - CHUNK_SIZE - 1;
// populate opaque array
int has_light = has_lights(chunk);
for (int dp = -1; dp <= 1; dp++) {
for (int dq = -1; dq <= 1; dq++) {
Chunk *other = chunk;
if (dp || dq) {
if (!has_light) {
continue;
}
other = find_chunk(chunk->p + dp, chunk->q + dq);
}
if (!other) {
continue;
}
Map *map = &other->map;
MAP_FOR_EACH(map, e) {
int x = e->x - ox;
int y = e->y - oy;
int z = e->z - oz;
int w = e->w;
// TODO: this should be unnecessary
if (x < 0 || y < 0 || z < 0) {
continue;
}
if (x >= XZ_SIZE || y >= Y_SIZE || z >= XZ_SIZE) {
continue;
}
// END TODO
opaque[x][y][z] = !is_transparent(w);
if (opaque[x][y][z]) {
highest[x][z] = MAX(highest[x][z], y);
}
} END_MAP_FOR_EACH;
}
}
// flood fill light intensities
if (SHOW_LIGHTS) {
for (int dp = -1; dp <= 1; dp++) {
for (int dq = -1; dq <= 1; dq++) {
Chunk *other = find_chunk(chunk->p + dp, chunk->q + dq);
if (!other) {
continue;
}
Map *map = &other->lights;
MAP_FOR_EACH(map, e) {
int x = e->x - ox;
int y = e->y - oy;
int z = e->z - oz;
light_fill(opaque, light, x, y, z, e->w, 1);
} END_MAP_FOR_EACH;
}
}
}
Map *map = &chunk->map;
// count exposed faces
chunk->miny = 256;
chunk->maxy = 0;