forked from fogleman/Craft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1260 lines (1170 loc) · 36.5 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
#ifndef __APPLE_CC__
#include <GL/glew.h>
#endif
#include <GLFW/glfw3.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "client.h"
#include "config.h"
#include "cube.h"
#include "db.h"
#include "map.h"
#include "matrix.h"
#include "noise.h"
#include "util.h"
#include "world.h"
#define MAX_CHUNKS 1024
#define MAX_PLAYERS 128
#define CREATE_CHUNK_RADIUS 6
#define RENDER_CHUNK_RADIUS 6
#define DELETE_CHUNK_RADIUS 12
#define RECV_BUFFER_SIZE 1024
#define TEXT_BUFFER_SIZE 256
#define LEFT 0
#define CENTER 1
#define RIGHT 2
static GLFWwindow *window;
static int exclusive = 1;
static int left_click = 0;
static int right_click = 0;
static int middle_click = 0;
static int teleport = 0;
static int flying = 0;
static int block_type = 1;
static int ortho = 0;
static float fov = 65.0;
static int typing = 0;
static char typing_buffer[TEXT_BUFFER_SIZE] = {0};
typedef struct {
Map map;
int p;
int q;
int faces;
int dirty;
GLuint buffer;
} Chunk;
typedef struct {
int id;
float x;
float y;
float z;
float rx;
float ry;
GLuint buffer;
} Player;
int is_plant(int w) {
return w > 16;
}
int is_obstacle(int w) {
w = ABS(w);
return w > 0 && w < 16;
}
int is_transparent(int w) {
w = ABS(w);
return w == 0 || w == 10 || w == 15 || is_plant(w);
}
int is_destructable(int w) {
return w > 0 && w != 16;
}
int is_selectable(int w) {
return w > 0 && w <= 11;
}
int chunked(float x) {
return floorf(roundf(x) / CHUNK_SIZE);
}
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) {
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 width, int height) {
int x = width / 2;
int y = height / 2;
int p = 10;
float data[] = {
x, y - p, x, y + p,
x - p, y, x + p, y
};
return gen_buffer(GL_ARRAY_BUFFER, sizeof(data), data);
}
GLuint gen_wireframe_buffer(float x, float y, float z, float n) {
float data[144];
make_cube_wireframe(data, x, y, z, n);
return gen_buffer(GL_ARRAY_BUFFER, sizeof(data), data);
}
GLuint gen_cube_buffer(float x, float y, float z, float n, int w) {
GLfloat *data = malloc_faces(8, 6);
make_cube(data, 1, 1, 1, 1, 1, 1, x, y, z, n, w);
return gen_faces(8, 6, data);
}
GLuint gen_plant_buffer(float x, float y, float z, float n, int w) {
GLfloat *data = malloc_faces(8, 4);
float rotation = simplex3(x, y, z, 4, 0.5, 2) * 360;
make_plant(data, x, y, z, n, w, rotation);
return gen_faces(8, 4, data);
}
GLuint gen_player_buffer(float x, float y, float z, float rx, float ry) {
GLfloat *data = malloc_faces(8, 6);
make_player(data, x, y, z, rx, ry);
return gen_faces(8, 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_chunk(
Chunk *chunk, GLuint position_loc, GLuint normal_loc, GLuint uv_loc)
{
glBindBuffer(GL_ARRAY_BUFFER, chunk->buffer);
glEnableVertexAttribArray(position_loc);
glEnableVertexAttribArray(normal_loc);
glEnableVertexAttribArray(uv_loc);
glVertexAttribPointer(position_loc, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, 0);
glVertexAttribPointer(normal_loc, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, (GLvoid *)(sizeof(GLfloat) * 3));
glVertexAttribPointer(uv_loc, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, (GLvoid *)(sizeof(GLfloat) * 6));
glDrawArrays(GL_TRIANGLES, 0, chunk->faces * 6);
glDisableVertexAttribArray(position_loc);
glDisableVertexAttribArray(normal_loc);
glDisableVertexAttribArray(uv_loc);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_item(
GLuint buffer,
GLuint position_loc, GLuint normal_loc, GLuint uv_loc, int count)
{
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(position_loc);
glEnableVertexAttribArray(normal_loc);
glEnableVertexAttribArray(uv_loc);
glVertexAttribPointer(position_loc, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, 0);
glVertexAttribPointer(normal_loc, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, (GLvoid *)(sizeof(GLfloat) * 3));
glVertexAttribPointer(uv_loc, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, (GLvoid *)(sizeof(GLfloat) * 6));
glDrawArrays(GL_TRIANGLES, 0, count);
glDisableVertexAttribArray(position_loc);
glDisableVertexAttribArray(normal_loc);
glDisableVertexAttribArray(uv_loc);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_text(
GLuint buffer,
GLuint position_loc, GLuint uv_loc, int length)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(position_loc);
glEnableVertexAttribArray(uv_loc);
glVertexAttribPointer(position_loc, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 4, 0);
glVertexAttribPointer(uv_loc, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 4, (GLvoid *)(sizeof(GLfloat) * 2));
glDrawArrays(GL_TRIANGLES, 0, length * 6);
glDisableVertexAttribArray(position_loc);
glDisableVertexAttribArray(uv_loc);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisable(GL_BLEND);
}
void draw_cube(
GLuint buffer, GLuint position_loc, GLuint normal_loc, GLuint uv_loc)
{
draw_item(buffer, position_loc, normal_loc, uv_loc, 36);
}
void draw_plant(
GLuint buffer, GLuint position_loc, GLuint normal_loc, GLuint uv_loc)
{
draw_item(buffer, position_loc, normal_loc, uv_loc, 24);
}
void draw_player(
Player *player, GLuint position_loc, GLuint normal_loc, GLuint uv_loc)
{
draw_cube(player->buffer, position_loc, normal_loc, uv_loc);
}
void draw_lines(GLuint buffer, GLuint position_loc, int size, int count) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(position_loc);
glVertexAttribPointer(position_loc, size, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINES, 0, count);
glDisableVertexAttribArray(position_loc);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void print(
GLuint position_loc, GLuint uv_loc, int justify,
float x, float y, float n, char *text)
{
int length = strlen(text);
x -= n * justify * (length - 1) / 2;
GLuint buffer = gen_text_buffer(x, y, n, text);
draw_text(buffer, position_loc, uv_loc, length);
glDeleteBuffers(1, &buffer);
}
Player *find_player(Player *players, int player_count, int id) {
for (int i = 0; i < player_count; i++) {
Player *player = 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)
{
player->x = x;
player->y = y;
player->z = z;
player->rx = rx;
player->ry = ry;
glDeleteBuffers(1, &player->buffer);
player->buffer = gen_player_buffer(x, y + 0.1, z, rx, ry);
}
void delete_player(Player *players, int *player_count, int id) {
Player *player = find_player(players, *player_count, id);
if (!player) {
return;
}
int count = *player_count;
glDeleteBuffers(1, &player->buffer);
Player *other = players + (--count);
memcpy(player, other, sizeof(Player));
*player_count = count;
}
Chunk *find_chunk(Chunk *chunks, int chunk_count, int p, int q) {
for (int i = 0; i < chunk_count; i++) {
Chunk *chunk = 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(Chunk *chunk, float *matrix) {
for (int dp = 0; dp <= 1; dp++) {
for (int dq = 0; dq <= 1; dq++) {
for (int y = 0; y < 128; y += 16) {
float vec[4] = {
(chunk->p + dp) * CHUNK_SIZE - dp,
y,
(chunk->q + dq) * CHUNK_SIZE - dq,
1};
mat_vec_multiply(vec, matrix, vec);
if (vec[3] >= 0) {
return 1;
}
}
}
}
return 0;
}
int highest_block(Chunk *chunks, int chunk_count, 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(chunks, chunk_count, 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 = 8;
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(
Chunk *chunks, int chunk_count, 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 < chunk_count; i++) {
Chunk *chunk = 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 collide(
Chunk *chunks, int chunk_count,
int height, float *x, float *y, float *z)
{
int result = 0;
int p = chunked(*x);
int q = chunked(*z);
Chunk *chunk = find_chunk(chunks, chunk_count, 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;
}
void exposed_faces(
Map *map, int x, int y, int z,
int *f1, int *f2, int *f3, int *f4, int *f5, int *f6)
{
*f1 = is_transparent(map_get(map, x - 1, y, z));
*f2 = is_transparent(map_get(map, x + 1, y, z));
*f3 = is_transparent(map_get(map, x, y + 1, z));
*f4 = is_transparent(map_get(map, x, y - 1, z)) && (y > 0);
*f5 = is_transparent(map_get(map, x, y, z - 1));
*f6 = is_transparent(map_get(map, x, y, z + 1));
}
void gen_chunk_buffers(Chunk *chunk) {
Map *map = &chunk->map;
int faces = 0;
MAP_FOR_EACH(map, e) {
if (e->w <= 0) {
continue;
}
int f1, f2, f3, f4, f5, f6;
exposed_faces(map, e->x, e->y, e->z, &f1, &f2, &f3, &f4, &f5, &f6);
int total = f1 + f2 + f3 + f4 + f5 + f6;
if (is_plant(e->w)) {
total = total ? 4 : 0;
}
faces += total;
} END_MAP_FOR_EACH;
GLfloat *data = malloc_faces(8, faces);
int offset = 0;
MAP_FOR_EACH(map, e) {
if (e->w <= 0) {
continue;
}
int f1, f2, f3, f4, f5, f6;
exposed_faces(map, e->x, e->y, e->z, &f1, &f2, &f3, &f4, &f5, &f6);
int total = f1 + f2 + f3 + f4 + f5 + f6;
if (is_plant(e->w)) {
total = total ? 4 : 0;
}
if (total == 0) {
continue;
}
if (is_plant(e->w)) {
float rotation = simplex3(e->x, e->y, e->z, 4, 0.5, 2) * 360;
make_plant(
data + offset,
e->x, e->y, e->z, 0.5, e->w, rotation);
}
else {
make_cube(
data + offset,
f1, f2, f3, f4, f5, f6,
e->x, e->y, e->z, 0.5, e->w);
}
offset += total * 48;
} END_MAP_FOR_EACH;
glDeleteBuffers(1, &chunk->buffer);
chunk->buffer = gen_faces(8, faces, data);
chunk->faces = faces;
chunk->dirty = 0;
}
void create_chunk(Chunk *chunk, int p, int q) {
chunk->p = p;
chunk->q = q;
chunk->faces = 0;
chunk->dirty = 1;
chunk->buffer = 0;
Map *map = &chunk->map;
map_alloc(map);
create_world(map, p, q);
db_load_map(map, p, q);
gen_chunk_buffers(chunk);
int key = db_get_key(p, q);
client_chunk(p, q, key);
}
void ensure_chunks(
Chunk *chunks, int *chunk_count,
float x, float y, float z, int force)
{
int p = chunked(x);
int q = chunked(z);
int count = *chunk_count;
for (int i = 0; i < count; i++) {
Chunk *chunk = chunks + i;
if (chunk_distance(chunk, p, q) >= DELETE_CHUNK_RADIUS) {
map_free(&chunk->map);
glDeleteBuffers(1, &chunk->buffer);
Chunk *other = chunks + (--count);
memcpy(chunk, other, sizeof(Chunk));
}
}
int rings = force ? 1 : CREATE_CHUNK_RADIUS;
int generated = 0;
for (int ring = 0; ring <= rings; ring++) {
for (int dp = -ring; dp <= ring; dp++) {
for (int dq = -ring; dq <= ring; dq++) {
if (ring != MAX(ABS(dp), ABS(dq))) {
continue;
}
if (!force && generated && ring > 1) {
continue;
}
int a = p + dp;
int b = q + dq;
Chunk *chunk = find_chunk(chunks, count, a, b);
if (chunk) {
if (chunk->dirty) {
gen_chunk_buffers(chunk);
generated++;
}
}
else {
if (count < MAX_CHUNKS) {
create_chunk(chunks + count, a, b);
generated++;
count++;
}
}
}
}
}
*chunk_count = count;
}
void _set_block(
Chunk *chunks, int chunk_count,
int p, int q, int x, int y, int z, int w)
{
Chunk *chunk = find_chunk(chunks, chunk_count, p, q);
if (chunk) {
Map *map = &chunk->map;
if (map_get(map, x, y, z) != w) {
map_set(map, x, y, z, w);
chunk->dirty = 1;
}
}
db_insert_block(p, q, x, y, z, w);
}
void set_block(
Chunk *chunks, int chunk_count,
int x, int y, int z, int w)
{
int p = chunked(x);
int q = chunked(z);
_set_block(chunks, chunk_count, p, q, x, y, z, w);
if (chunked(x - 1) != p) {
_set_block(chunks, chunk_count, p - 1, q, x, y, z, -w);
}
if (chunked(x + 1) != p) {
_set_block(chunks, chunk_count, p + 1, q, x, y, z, -w);
}
if (chunked(z - 1) != q) {
_set_block(chunks, chunk_count, p, q - 1, x, y, z, -w);
}
if (chunked(z + 1) != q) {
_set_block(chunks, chunk_count, p, q + 1, x, y, z, -w);
}
client_block(x, y, z, w);
}
int get_block(
Chunk *chunks, int chunk_count,
int x, int y, int z)
{
int p = chunked(x);
int q = chunked(z);
Chunk *chunk = find_chunk(chunks, chunk_count, p, q);
if (chunk) {
Map *map = &chunk->map;
return map_get(map, x, y, z);
}
return 0;
}
void on_key(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (action != GLFW_PRESS) {
return;
}
if (key == GLFW_KEY_ESCAPE) {
if (typing) {
typing = 0;
}
else if (exclusive) {
exclusive = 0;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
if (key == GLFW_KEY_ENTER) {
if (typing) {
typing = 0;
client_talk(typing_buffer);
}
else {
if (mods & GLFW_MOD_SUPER) {
right_click = 1;
}
else {
left_click = 1;
}
}
}
if (key == GLFW_KEY_BACKSPACE) {
if (typing) {
int n = strlen(typing_buffer);
if (n > 0) {
typing_buffer[n - 1] = '\0';
}
}
}
if (!typing) {
if (key == CRAFT_KEY_FLY) {
flying = !flying;
}
if (key == CRAFT_KEY_TELEPORT) {
teleport = 1;
}
if (key >= '1' && key <= '9') {
block_type = key - '1' + 1;
}
if (key == CRAFT_KEY_BLOCK_TYPE) {
block_type = block_type % 11 + 1;
}
}
}
void on_char(GLFWwindow *window, unsigned int u) {
if (typing) {
if (u >= 32 && u < 128) {
char c = (char)u;
int n = strlen(typing_buffer);
if (n < TEXT_BUFFER_SIZE - 1) {
typing_buffer[n] = c;
typing_buffer[n + 1] = '\0';
}
}
}
else {
if (u == CRAFT_KEY_CHAT) {
typing = 1;
typing_buffer[0] = '\0';
}
if (u == CRAFT_KEY_COMMAND) {
typing = 1;
typing_buffer[0] = '/';
typing_buffer[1] = '\0';
}
}
}
void on_scroll(GLFWwindow *window, double xdelta, double ydelta) {
static double ypos = 0;
ypos += ydelta;
if (ypos < -SCROLL_THRESHOLD) {
block_type++;
if (block_type > 11) {
block_type = 1;
}
ypos = 0;
}
if (ypos > SCROLL_THRESHOLD) {
block_type--;
if (block_type < 1) {
block_type = 11;
}
ypos = 0;
}
}
void on_mouse_button(GLFWwindow *window, int button, int action, int mods) {
if (action != GLFW_PRESS) {
return;
}
if (button == GLFW_MOUSE_BUTTON_LEFT) {
if (exclusive) {
if (mods & GLFW_MOD_SUPER) {
right_click = 1;
}
else {
left_click = 1;
}
}
else {
exclusive = 1;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
}
if (button == GLFW_MOUSE_BUTTON_RIGHT) {
if (exclusive) {
right_click = 1;
}
}
if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
if (exclusive) {
middle_click = 1;
}
}
}
void create_window() {
int width = WINDOW_WIDTH;
int height = WINDOW_HEIGHT;
GLFWmonitor *monitor = NULL;
if (FULLSCREEN) {
int mode_count;
monitor = glfwGetPrimaryMonitor();
const GLFWvidmode *modes = glfwGetVideoModes(monitor, &mode_count);
width = modes[mode_count - 1].width;
height = modes[mode_count - 1].height;
}
window = glfwCreateWindow(width, height, "Craft", monitor, NULL);
}
int main(int argc, char **argv) {
srand(time(NULL));
rand();
if (argc == 2 || argc == 3) {
char *hostname = argv[1];
int port = DEFAULT_PORT;
if (argc == 3) {
port = atoi(argv[2]);
}
if (USE_CACHE) {
char path[1024];
snprintf(path, 1024, "cache.%s.%d.db", hostname, port);
db_enable();
if (db_init(path)) {
return -1;
}
}
client_enable();
client_connect(hostname, port);
client_start();
}
else {
db_enable();
if (db_init(DB_PATH)) {
return -1;
}
}
if (!glfwInit()) {
return -1;
}
create_window();
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(VSYNC);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetKeyCallback(window, on_key);
glfwSetCharCallback(window, on_char);
glfwSetMouseButtonCallback(window, on_mouse_button);
glfwSetScrollCallback(window, on_scroll);
#ifndef __APPLE__
if (glewInit() != GLEW_OK) {
return -1;
}
#endif
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glLogicOp(GL_INVERT);
glClearColor(0.53, 0.81, 0.92, 1.00);
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
load_png_texture("texture.png");
GLuint font;
glGenTextures(1, &font);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, font);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
load_png_texture("font.png");
GLuint block_program = load_program(
"shaders/block_vertex.glsl", "shaders/block_fragment.glsl");
GLuint matrix_loc = glGetUniformLocation(block_program, "matrix");
GLuint camera_loc = glGetUniformLocation(block_program, "camera");
GLuint sampler_loc = glGetUniformLocation(block_program, "sampler");
GLuint timer_loc = glGetUniformLocation(block_program, "timer");
GLuint position_loc = glGetAttribLocation(block_program, "position");
GLuint normal_loc = glGetAttribLocation(block_program, "normal");
GLuint uv_loc = glGetAttribLocation(block_program, "uv");
GLuint line_program = load_program(
"shaders/line_vertex.glsl", "shaders/line_fragment.glsl");
GLuint line_matrix_loc = glGetUniformLocation(line_program, "matrix");
GLuint line_position_loc = glGetAttribLocation(line_program, "position");
GLuint text_program = load_program(
"shaders/text_vertex.glsl", "shaders/text_fragment.glsl");
GLuint text_matrix_loc = glGetUniformLocation(text_program, "matrix");
GLuint text_sampler_loc = glGetUniformLocation(text_program, "sampler");
GLuint text_position_loc = glGetAttribLocation(text_program, "position");
GLuint text_uv_loc = glGetAttribLocation(text_program, "uv");
GLuint item_buffer = 0;
int previous_block_type = 0;
char messages[MAX_MESSAGES][TEXT_BUFFER_SIZE] = {0};
int message_index = 0;
double last_commit = glfwGetTime();
Chunk chunks[MAX_CHUNKS];
int chunk_count = 0;
Player players[MAX_PLAYERS];
int player_count = 0;
FPS fps = {0, 0};
float matrix[16];
float x = (rand_double() - 0.5) * 10000;
float z = (rand_double() - 0.5) * 10000;
float y = 0;
float dy = 0;
float rx = 0;
float ry = 0;
double px = 0;
double py = 0;
int loaded = db_load_state(&x, &y, &z, &rx, &ry);
ensure_chunks(chunks, &chunk_count, x, y, z, 1);
if (!loaded) {
y = highest_block(chunks, chunk_count, x, z) + 2;
}
glfwGetCursorPos(window, &px, &py);
double previous = glfwGetTime();
while (!glfwWindowShouldClose(window)) {
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
update_fps(&fps, SHOW_FPS);
double now = glfwGetTime();
double dt = MIN(now - previous, 0.2);
previous = now;
if (now - last_commit > COMMIT_INTERVAL) {
last_commit = now;
db_commit();
}
if (exclusive && (px || py)) {
double mx, my;
glfwGetCursorPos(window, &mx, &my);
float m = 0.0025;
rx += (mx - px) * m;
ry -= (my - py) * m;
if (rx < 0) {
rx += RADIANS(360);
}
if (rx >= RADIANS(360)){
rx -= RADIANS(360);
}
ry = MAX(ry, -RADIANS(90));
ry = MIN(ry, RADIANS(90));
px = mx;
py = my;
}
else {
glfwGetCursorPos(window, &px, &py);
}
int sz = 0;
int sx = 0;
if (!typing) {
float m = dt * 1.0;
ortho = glfwGetKey(window, CRAFT_KEY_ORTHO);
fov = glfwGetKey(window, CRAFT_KEY_ZOOM) ? 15.0 : 65.0;
if (glfwGetKey(window, CRAFT_KEY_QUIT)) break;
if (glfwGetKey(window, CRAFT_KEY_FORWARD)) sz--;
if (glfwGetKey(window, CRAFT_KEY_BACKWARD)) sz++;
if (glfwGetKey(window, CRAFT_KEY_LEFT)) sx--;
if (glfwGetKey(window, CRAFT_KEY_RIGHT)) sx++;
if (glfwGetKey(window, GLFW_KEY_LEFT)) rx -= m;
if (glfwGetKey(window, GLFW_KEY_RIGHT)) rx += m;
if (glfwGetKey(window, GLFW_KEY_UP)) ry += m;
if (glfwGetKey(window, GLFW_KEY_DOWN)) ry -= m;
}
float vx, vy, vz;
get_motion_vector(flying, sz, sx, rx, ry, &vx, &vy, &vz);
if (!typing) {
if (glfwGetKey(window, CRAFT_KEY_JUMP)) {
if (flying) {
vy = 1;
}
else if (dy == 0) {
dy = 8;
}
}
if (glfwGetKey(window, CRAFT_KEY_XM)) {
vx = -1; vy = 0; vz = 0;
}
if (glfwGetKey(window, CRAFT_KEY_XP)) {
vx = 1; vy = 0; vz = 0;
}
if (glfwGetKey(window, CRAFT_KEY_YM)) {
vx = 0; vy = -1; vz = 0;
}
if (glfwGetKey(window, CRAFT_KEY_YP)) {
vx = 0; vy = 1; vz = 0;
}
if (glfwGetKey(window, CRAFT_KEY_ZM)) {
vx = 0; vy = 0; vz = -1;
}
if (glfwGetKey(window, CRAFT_KEY_ZP)) {