-
Notifications
You must be signed in to change notification settings - Fork 0
/
a1.c
1837 lines (1670 loc) · 71.7 KB
/
a1.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
/* Derived from scene.c in the The OpenGL Programming Guide */
/* Keyboard and mouse rotation taken from Swiftless Tutorials #23 Part 2 */
/* http://www.swiftless.com/tutorials/opengl/camera2.html */
/* Frames per second code taken from : */
/* http://www.lighthouse3d.com/opengl/glut/index.php?fps */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include "graphics.h"
//so many externs...
extern GLubyte world[WORLDX][WORLDY][WORLDZ];
extern float frustum[6][4];
extern void ExtractFrustum();
extern int PointInFrustum(float x, float y, float z);
extern int SphereInFrustum(float x, float y, float z, float radius);
/* mouse function called by GLUT when a button is pressed or released */
void mouse(int, int, int, int);
/* initialize graphics library */
extern void graphicsInit(int *, char **);
/* lighting control */
extern void setLightPosition(GLfloat, GLfloat, GLfloat);
extern GLfloat* getLightPosition();
/* viewpoint control */
extern void setViewPosition(float, float, float);
extern void getViewPosition(float *, float *, float *);
extern void getOldViewPosition(float *, float *, float *);
extern void setOldViewPosition(float, float, float);
extern void setViewOrientation(float, float, float);
extern void getViewOrientation(float *, float *, float *);
/* add cube to display list so it will be drawn */
extern void addDisplayList(int, int, int);
extern void createMob(int, float, float, float, float);
extern void setMobPosition(int, float, float, float, float);
extern void hideMob(int);
extern void showMob(int);
extern void createPlayer(int, float, float, float, float);
extern void setPlayerPosition(int, float, float, float, float);
extern void hidePlayer(int);
extern void showPlayer(int);
extern void createTube(int, float, float, float, float, float, float, int);
extern void hideTube(int);
extern void showTube(int);
/* x1 y1 x2 y2 */
extern void draw2Dline(int, int, int, int, int);
/* x1 y1 x2 y2 */
extern void draw2Dbox(int, int, int, int);
/* x1 y1 x2 y2 */
extern void draw2Dtriangle(int, int, int, int, int, int);
extern void set2Dcolour(float []);
/* texture functions */
extern int setAssignedTexture(int, int);
extern void unsetAssignedTexture(int);
extern int getAssignedTexture(int);
extern void setTextureOffset(int, float, float);
extern float getx(int);
extern float gety(int);
extern float getz(int);
extern float getoldx(int);
extern float getoldy(int);
extern float getoldz(int);
extern int getVisible(int);
extern bool getChase(int);
extern void setChase(int, bool);
extern int getDestination(int);
extern void setDestination(int, int);
extern bool getActive(int);
extern void setActive(int, bool);
extern bool getPresent(int);
extern void setPresent(int, bool);
/* flag which is set to 1 when flying behaviour is desired */
extern int flycontrol;
/* flag used to indicate that the test world should be used */
extern int testWorld;
/* flag to print out frames per second */
extern int fps;
/* flag to indicate the space bar has been pressed */
extern int space;
/* flag indicates the program is a client when set = 1 */
extern int netClient;
/* flag indicates the program is a server when set = 1 */
extern int netServer;
/* size of the window in pixels */
extern int screenWidth, screenHeight;
/* flag indicates if map is to be printed */
extern int displayMap;
/* flag indicates use of a fixed viewpoint */
extern int fixedVP;
/* flag toggles gravity */
extern bool usegravity;
/* flag toggles collision */
extern bool collisions;
/* frustum corner coordinates, used for visibility determination */
extern float corners[4][3];
/* determine which cubes are visible e.g. in view frustum */
extern void ExtractFrustum();
extern void tree(float, float, float, float, float, float, int); //so nostalgic
/* allows users to define colours */
extern int setUserColour(int, GLfloat, GLfloat, GLfloat, GLfloat, GLfloat,
GLfloat, GLfloat, GLfloat);
void unsetUserColour(int);
extern void getUserColour(int, GLfloat *, GLfloat *, GLfloat *, GLfloat *,
GLfloat *, GLfloat *, GLfloat *, GLfloat *);
/* mesh creation, translatio, rotation functions */
extern void setMeshID(int, int, float, float, float);
extern void unsetMeshID(int);
extern void setTranslateMesh(int, float, float, float);
extern void resetTranslateMesh(int);
extern void setRotateMesh(int, float, float, float);
extern void setScaleMesh(int, float);
extern void drawMesh(int);
extern void hideMesh(int);
extern int getMeshNumber(int); //"mesh number" is a bit misleading, it actually refers to the type
extern int getMeshUsed(int);
/********* end of extern variable declarations **************/
/********* utility functions *********/
//and temp globals
#define FLOORHEIGHT 24
#define NUMROOMS 9
#define MAX_FLOORS 100
//i dont care. this makes it more readable and i want to do it
enum direction {n, ne, e, se, s, sw, w, nw, none}; //directions from north clockwise, with a special fail case
enum direction combineDirections(enum direction v, enum direction h) { //i could do this instead by arranging the enum in a clever way but i dont have time
if (v == n) {
if (h == e) {
return ne;
}
else if (h == w) {
return nw;
}
}
else if (v == s) {
if (h == e) {
return se;
}
else if (h == w) {
return sw;
}
}
return n; //if everything else fails just return north. this will happen if two composite vectors are added.
}
struct coord {
int x;
int y;
int z;
}; //just to make working with this shit easier although i might not end up using it
struct record { //this might need to store mobs too... and other things...
GLbyte world[WORLDX][WORLDY][WORLDZ];
struct coord spawn;
struct coord stairs[2]; //this is stored because the stairs hold special data + also need to be cross referenced from several scopes
};
const int map_offset = 5; //ATTN: 5 pixels?
bool world_inited = false;
time_t timings[100] = {0L}; //timings[0] will hold the clouds, [1] the mobs, then whatever else.
struct record* floors[MAX_FLOORS] = {NULL}; //if only things were different... @dcalvert...
struct coord points[MAX_FLOORS * NUMROOMS];
struct coord dimensions[MAX_FLOORS * NUMROOMS];
struct coord random_things[MAX_FLOORS * NUMROOMS]; //TODO: INFROOMS scale up for infinite room gen
struct coord curr_viewpos = {0, 0, 0};
int currfloor = 0; //global memory management for records.the first one will be the outside level,
int prevfloor = 0;
int numMesh = 0; //dcalvert, your mob system hurts my soul
int meshIndex = 0; //this is actually the global offset for indices.
float scaling = 5.0;
int hjoined[2 * MAX_FLOORS][2 * MAX_FLOORS];
//#define STAIRS_UP (currfloor * 2) + 0
//#define STAIRS_DOWN (currfloor * 2) + 1
#define STAIRS_UP 0
#define STAIRS_DOWN 1
#define SIDE_A currfloor + 0
#define SIDE_B currfloor + 2 //NOTE THIS LEAVES SOME SLOTS OPEN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FIXME:FIXME:
/*actually, i won't be fixing this. i'm so fucking overworked i havent had time to shower in 4.5 days
and this prof has the fucking gall to give the students who went to class a salty little deal n boost their grades
jesus fuck my guy some people have other classes with massive workloads and not to mention two jobs
and then he has the GUMPTION not to do course evals (even though i know they dont matter) to avoid having to read
people complaining that him giving 20% grade boosters to students that show up to class might be unfair to people
who need to pay for their own schooling or who have poor mental health. and you know what i'm fine with getting a
60 in this course, i just feel frustrated at the lack of compassion and of consideration this professor has. the
only reason he granted me extensions for these previous assignments is because all the marking and stuff is done
by his TAs so he couldnt give two shits when things are marked, not because he genuinely had any interest in the
well-being of his students, or that he wished for them to succeed at all. i've wasted like 5 minutes writing this
rant and that's far more than i can afford, but at least it felt good despite that no-one will read it. if you do
read this rekkab thank you so much for correcting my a2 grade, and for the nice e-mail. you're a legend and
would probably make a better prof than this guy*/
float squared(float n) {return n*n;}
//utility function. rand should be seeded before this is called, please! if you do this then you're the nicest. also min and max are inclusive!
int getRandomNumber(int min, int max) {
return min + (rand() % (max - min + 1));
}
double getDistance2d(struct coord a, struct coord b) {
double n_squared = (double)((b.x - a.x) * (b.x - a.x)); //not my usual style but i don't want to mess anything up!!
double m_squared = (double)((b.z - a.z) * (b.z - a.z));
return sqrt(n_squared + m_squared);
}
struct coord getMobCoord(int id) {
return (struct coord){(int)getx(id), (int)gety(id), (int)getz(id)};
}
int determineRoom(struct coord pos) { //-1 means to find the player's room
float x, y, z;
int roomindex = -1;
for (int i = meshIndex; i < meshIndex + NUMROOMS; i++) {
if (pos.x > points[i].x && pos.x < points[i].x + dimensions[i].x) {
if (pos.z > points[i].z && pos.z < points[i].z + dimensions[i].z) {
//if (noncalverty > points[i].y && noncalverty < points[i].y + dimensions[i].y) {
roomindex = i;
break;
//}
}
}
}
return roomindex;
}
long getTime() { //in ms
struct timeval time;
gettimeofday(&time, NULL);
long s1 = (long)(time.tv_sec) * 1000;
long s2 = (time.tv_usec / 1000);
return s1 + s2;
}
struct coord generatePoint(int minx, int maxx, int minz, int maxz) { //i dont care that its shit you made me do this calvert. three rows
struct coord point = {.x = getRandomNumber(minx, maxx), .y = FLOORHEIGHT, .z = getRandomNumber(minz, maxz)};
//////printf("Generated point: %d %d %d\n", point.x, point.y, point.z);
return point;
}
//offsets a point by some value. negative numbers work, obv
struct coord offsetPoint(struct coord a, int offsetx, int offsety, int offsetz) {
struct coord b = {.x = a.x + offsetx, .y = a.y + offsety, .z = a.z + offsetz};
return b;
}
void fillVolume(struct coord origin, int l, int w, int h, GLbyte colour) {
//////printf("origin %d %d %d; l=%d, w=%d, h=%d\n", origin.x, origin.y, origin.z, l, w, h);
for(int x = origin.x; x < origin.x+w; x++) {
//////printf("x: %d\t", x);
for(int y = origin.y; y < origin.y+h; y++) {
//////printf("y: %d\t", y);
for(int z = origin.z; z < origin.z+l; z++) {
//////printf("z: %d\n", z);
world[x][y][z] = colour;
}
}
}
}
bool attack_check() {
return getRandomNumber(0, 100) > 50 ? true : false;
}
bool adjacencyCheck(struct coord a, struct coord b) { //NOTE: doesn't check y!
return abs(a.x - b.x) <= 1 && abs(a.z - b.z) <= 1;
}
/*** collisionResponse() ***/
/* -performs collision detection and response */
/* sets new xyz to position of the viewpoint after collision */
/* -can also be used to implement gravity by updating y position of vp*/
/* note that the world coordinates returned from getViewPosition()
will be the negative value of the array indices */
void collisionResponse() {
if (collisions) {
float x, y, z, ox, oy, oz = 0.0;
getViewPosition(&x, &y, &z);
const float margin = 0.3; //calvert's favourite random number
bool collisionX = false;
bool ascendY = false;
bool collisionZ = false;
//getOldViewPosition(&ox, &oy, &oz);
//setViewPosition(ox, oy, oz);
if(world[-(int)(x-margin)][-(int)y][-(int)(z)] || world[-(int)(x+margin)][-(int)y][-(int)(z)] || -(int)x > WORLDX-1 || -(int)x <= 0) {
//printf("x axis collision?\n");
collisionX = true;
}
if (world[-(int)(x)][-(int)y][-(int)(z-margin)] || world[-(int)(x)][-(int)y][-(int)(z+margin)] || -(int)z > WORLDZ-1 || -(int)z <= 0) {
//printf("z axis collision?\n");
collisionZ = true;
}
if (collisionX || collisionZ) { //cis1500 tier code structure dont @ me fuckers because i _know_
if(!(-(int)x > WORLDX-1 || -(int)z > WORLDZ-1 || -(int)x <= 0 || -(int)z <= 0) && world[-(int)(x-margin)][-(int)y + 1][-(int)(z-margin)] == 0 && world[-(int)(x+margin)][-(int)y + 1][-(int)(z+margin)] == 0) {
ascendY = true;
}
getOldViewPosition(&ox, &oy, &oz);
setViewPosition((collisionX ? ox : x), (ascendY ? y-1.0 : y), (collisionZ ? oz : z));
}
if (currfloor > 0) { //this loop would crash on the outside level, also it's not useful there anyways
//this function checks if the player is colliding with any mobs on the floor
for (int id = meshIndex; id < (currfloor * NUMROOMS); id++) {
if (getActive(id) && getVisible(id)) {
float mx = getx(id); // these arent calvertized for some reason. very good thank you mr calvreyt
float mz = getz(id); // no need to check for y overlap because fuck you
//printf("mx = %d mz = %d x = %d z = %d\n", -(int)mx, -(int)mz, -(int)x, -(int)z);
if (-(int)x == (int)mx && -(int)z == (int)mz) {
getOldViewPosition(&x, &y, &z);
setViewPosition(x, y, z);
}
}
}
}
}
}
enum direction determineVerticalDirection(struct coord mob, struct coord dest) {
if (dest.z > mob.z) { //player is north somewhere -- also i say player but i mean destination
return n;
}
else if (dest.z < mob.z) { //player is south somewhere
return s;
}
return none;
}
enum direction determineHorizontalDirection(struct coord mob, struct coord dest) {
if (dest.x > mob.x) { //player is east somewhere
return e;
}
else if (dest.x < mob.x) { //player is west somewhere
return w;
}
return none;
}
enum direction determineDirectionToDestination(int id, struct coord dest) {
struct coord mob = getMobCoord(id); //in the wise words of dr. kremer: i could do this in a really clever way. OR, i could write 17 if statements
enum direction horizontal = determineHorizontalDirection(mob, dest);
enum direction vertical = determineVerticalDirection(mob, dest);
if (horizontal != none && vertical != none) { //then we have a composite direction!
return combineDirections(vertical, horizontal);
}
else if (horizontal != none) return horizontal;
return vertical;
} //reworked to be a lil messier than the classy thing i had before... but it had to be done.
void moveInDirection(int id, enum direction dir) { //could have done this all so much more cleverly i know but i havent time to think
const float speed = 0.75;
float x = getx(id);
float y = gety(id);
float z = getz(id);
switch(dir) {
case n:
setTranslateMesh(id, x, y, z + speed);
break;
case ne:
setTranslateMesh(id, x + speed, y, z + speed);
break;
case e:
setTranslateMesh(id, x + speed, y, z);
break;
case se:
setTranslateMesh(id, x + speed, y, z - speed);
break;
case s:
setTranslateMesh(id, x, y, z - speed);
break;
case sw:
setTranslateMesh(id, x - speed, y, z - speed);
break;
case w:
setTranslateMesh(id, x - speed, y, z);
break;
case nw:
setTranslateMesh(id, x - speed, y, z + speed);
break;
default:
;
//printf("Cannot move in %d (probably none) direction\n", dir);
}
}
/*int chooseBestDoor(struct coord pos, int dest_index) {
//if the room you're in is an h-joiner and you need it, take it, otherwise path down or up as needed
int cur_index = determineRoom(pos);
if (points[dest_index].x < points[cur_index].x) {
for (int i = 0; i < 2; i++) {
for (int j = 0; )
}
}
return lowest_index;
}*/
bool mobCollisionResponse(int id) {
bool collisionX, collisionZ = false;
bool collisionMob = false;
if (getActive(id) && getPresent(id)) {
//int dest = dest = getDestination(id);
float x = getx(id);
float y = gety(id);
float z = getz(id);
float ox = getoldx(id);
float oy = getoldy(id);
float oz = getoldz(id);
int mob_hit = -1;
const float margin = 0.3;
//the following could definitely be improved:
if(world[(int)(x-margin)][(int)y][(int)(z)] || world[(int)(x+margin)][(int)y][(int)(z)] || (int)x > WORLDX-1 || (int)x < 0) {
//printf("x axis collision?\n");
collisionX = true;
}
if (world[(int)(x)][(int)y][(int)(z-margin)] || world[(int)(x)][(int)y][(int)(z+margin)] || (int)z > WORLDZ-1 || (int)z < 0) {
//printf("z axis collision?\n");
collisionZ = true;
}
if (currfloor > 0) { //this shouldnt trigger on floor0 but we'll check just in case, since it WILL crash if it somehow doees
struct coord mob = getMobCoord(id);
for (int i = meshIndex; i < (currfloor * NUMROOMS); i++) { //FIXME: INFROOMS doesn't scale right!
if(id != i && getActive(id)) {
struct coord other_mob = getMobCoord(i);
if (mob.x == other_mob.x && mob.y == other_mob.y && mob.z == other_mob.z) {
resetTranslateMesh(id); //turns are kinda wasted when they run into walls tho
collisionMob = true; //just collision SOMETHing, for the return value
mob_hit = meshIndex;
}
}
}
}
//if we're not an arrow, we'll only move in the direction that doesn't put us inside a wall. otherwise, we die.
if (collisionX || collisionZ || collisionMob) { //cis1500 tier code structure dont @ me fuckers because i _know_
if (getMeshNumber(id) != 18) {
setTranslateMesh(id, (collisionX ? ox : x), y, (collisionZ ? oz : z));
}
else {
if (collisionMob) {
//we have struck the mob with the id "mob_hit"! (actually, this is the LAST mob we hit but since they can't overlap...)
}
setActive(id, false); //note that setActive has the "side effect" of also setting the mob to invisible.
setPresent(id, false);
unsetMeshID(id);
}
}
/*if (world[mob.x-margin][mob.y][mob.z] || world[mob.x+margin][mob.y][mob.z]) { //collision on the x axis
resetTranslateMesh(id);
correction = determineVerticalDirection(mob, (dest == -1 ? curr_viewpos : offsetPoint(points[dest], 2, 0, 2)));
//printf("correctionA = %d\n", correction);
moveInDirection(id, correction);
}
if (world[mob.x][mob.y][mob.z-margin] || world[mob.x][mob.y][mob.z+margin]) { //collision on the z axis
resetTranslateMesh(id);
correction = determineHorizontalDirection(mob, (dest == -1 ? curr_viewpos : offsetPoint(points[dest], 2, 0, 2)));
//printf("correctionB = %d\n", correction);
moveInDirection(id, correction);
}
if (mob.x > WORLDX-1 || mob.z > WORLDZ-1 || mob.x < 0 || mob.z < 0) { //out of bounds, just reset!
resetTranslateMesh(id);
}*/
}
return (collisionX || collisionZ || collisionMob);
}
void drawPlayerMap() {
float px, py, pz;
getViewPosition(&px, &py, &pz);
int ix, iy, iz;
ix = -(int)px;
iy = -(int)py;
iz = -(int)pz;
GLfloat red[] = {1.0, 0.0, 0.0, 0.5};
set2Dcolour(red);
draw2Dtriangle((ix + 3) * scaling, iz * scaling, ix * scaling, iz * scaling, ix * scaling, (iz + 3) * scaling);
}
void drawRoomMap(int id) {
GLfloat black[] = {0.0, 0.0, 0.0, 0.5};
set2Dcolour(black);
//printf("drawing box at %d %d %d %d\n", points[id].x, points[id].z, points[id].x + dimensions[id].x, points[id].z + dimensions[id].z);
draw2Dbox(points[id].x * scaling, points[id].z * scaling, (points[id].x + dimensions[id].x) * scaling, (points[id].z + dimensions[id].z) * scaling);
}
void drawStairsMap() {
GLfloat green[] = {0.0, 1.0, 0.0, 0.5}; //for down
GLfloat blue[] = {0.0, 0.2, 1.0, 0.5}; //for up
set2Dcolour(green);
draw2Dbox(floors[currfloor]->stairs[STAIRS_DOWN].x * scaling, floors[currfloor]->stairs[STAIRS_DOWN].z * scaling,
(floors[currfloor]->stairs[STAIRS_DOWN].x + 1) * scaling, (floors[currfloor]->stairs[STAIRS_DOWN].z + 1) * scaling);
if (currfloor > 0) {
set2Dcolour(blue);
draw2Dbox(floors[currfloor]->stairs[STAIRS_UP].x * scaling, floors[currfloor]->stairs[STAIRS_UP].z * scaling,
(floors[currfloor]->stairs[STAIRS_UP].x + 1) * scaling, (floors[currfloor]->stairs[STAIRS_UP].z + 1) * scaling);
}
}
//draw2Dline: int x1, int y1, int x2, int y2, int lineWidth
void drawJoiningRoomMap(int r1_index, int r2_index) {
//printf("currfloor is %d\n", currfloor);
const int w = 3;
GLfloat black[] = {0.0, 0.0, 0.0, 0.5};
set2Dcolour(black);
struct coord corner_for_z_join = offsetPoint(points[r1_index], 0, 0, dimensions[r1_index].z);
draw2Dline(points[r1_index].x * scaling, corner_for_z_join.z * scaling, points[r2_index].x * scaling, points[r2_index].z * scaling, w);
for (int i = SIDE_A; i <= SIDE_B; i++) {
//printf("i=%d\n", i);
struct coord corner_for_x_join = offsetPoint(points[hjoined[i][SIDE_A]], dimensions[hjoined[i][SIDE_A]].x, 0, 0);
draw2Dline(corner_for_x_join.x * scaling, points[hjoined[i][SIDE_A]].z * scaling, points[hjoined[i][SIDE_B]].x * scaling, points[hjoined[i][SIDE_B]].z * scaling, w);
} //TODO: revamp this to scale, to fix the (secret little bug) that nobody will notice. 1mo later: fuck i dont't even notice it
}
void drawMobs() {
GLfloat red[] = {1.0, 0.0, 0.0, 0.5};
GLfloat black[] = {0.0, 0.0, 0.0, 0.87};
GLfloat cactusgreen[] = {0.2, 1.0, 0.2, 0.5};
for(int i = meshIndex; i <= meshIndex + NUMROOMS; i++) {
int visible = getVisible(i);
int mob_type = getMeshNumber(i);
switch(mob_type) {
case 1:
set2Dcolour(red);
break;
case 2:
set2Dcolour(black);
break;
case 3:
set2Dcolour(cactusgreen);
break;
default:
set2Dcolour(red);
}
//printf("mesh %d's visibility is %d\n", i, visible);
//don't draw arrows on the map
if (getActive(i) && getMeshNumber(i) != 18) {
int x = (int)getx(i);
int z = (int)getz(i);
draw2Dbox(x * scaling, z * scaling, (x + 1) * scaling, (z + 1) * scaling);
}
}
}
void drawRandomBlocks(int i) {
GLfloat yellow[] = {1.0, 1.0, 0.0, 0.5};
set2Dcolour(yellow);
if (currfloor > 0) {
draw2Dbox(random_things[i].x * scaling, random_things[i].z * scaling,
(random_things[i].x + 1) * scaling, (random_things[i].z + 1) * scaling);
}
}
/******* draw2D() *******/
/* draws 2D shapes on screen */
/* use the following functions: */
/* draw2Dline(int, int, int, int, int); */
/* draw2Dbox(int, int, int, int); */
/* draw2Dtriangle(int, int, int, int, int, int); */
/* set2Dcolour(float []); */
/* colour must be set before other functions are called */
void draw2D() {
if (testWorld) {
/* draw some sample 2d shapes */
if (displayMap == 1) {
GLfloat green[] = {0.0, 0.5, 0.0, 0.5};
set2Dcolour(green);
draw2Dline(0, 0, 500, 500, 15);
draw2Dtriangle(0, 0, 200, 200, 0, 200);
GLfloat black[] = {0.0, 0.0, 0.0, 0.5};
set2Dcolour(black);
draw2Dbox(500, 380, 524, 388);
}
} else {
/* your code goes here */
//also we should draw the map (jank but i have 8 hours left):
GLfloat black[] = {0.0, 0.0, 0.0, 0.5};
if (displayMap == 1) {
if (currfloor > 0) {
drawMobs();
for (int i = meshIndex; i < meshIndex + NUMROOMS; i++) {
drawRandomBlocks(i);
drawStairsMap();
drawPlayerMap();
drawRoomMap(i);
}
//printf("meshindex is %d and currfloor is %d\n", meshIndex, currfloor);
for(int i = meshIndex; i < meshIndex + ((NUMROOMS/3) * 2); i++) { //i think my math on numrooms/3 * 2 is wrong but tbh in this case we want 6 so it works (9/3 * 2 = 6)
drawJoiningRoomMap(i, i+(NUMROOMS/3)); //3 would be NUMROWS if calvert's generation was well-advised but i fear he may change it any time
}
}
else {
drawPlayerMap();
drawStairsMap();
set2Dcolour(black);
draw2Dbox(0, 0, WORLDX * scaling, WORLDZ * scaling);
}
}
}
}
/* called by GLUT when a mouse button is pressed or released */
/* -button indicates which button was pressed or released */
/* -state indicates a button down or button up event */
/* -x,y are the screen coordinates when the mouse is pressed or */
/* released */
void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON)
printf("left button - ");
else if (button == GLUT_MIDDLE_BUTTON)
printf("middle button - ");
else
printf("right button - ");
if (state == GLUT_UP)
printf("up - ");
else
printf("down - ");
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
float xp, yp, zp;
getViewPosition(&xp, &yp, &zp);
printf("viewpos %f %f %f\n", xp, yp, zp);
}
}
/*(all ints pls) origin, l w h, colour.
the origin coords will point to the bottom left of the room*/
void createRoom(struct coord origin, int l, int w, int h, GLbyte colour) { //FIXME: could be done faster.
//dx, dy, dz = drwaing x,y,z. the n stands for nega
GLbyte floorcol = colour - 1;
for (int dy = origin.y; dy <= origin.y + h; dy++) {
for (int dx = origin.x; dx <= origin.x + w; dx++) {
world[dx][dy][origin.z] = colour;
world[dx][dy][origin.z + l] = colour;
for (int floorz = origin.z; floorz <= origin.z + l; floorz++) {
world[dx][origin.y][floorz] = floorcol;
world[dx][origin.y + h][floorz] = colour; //roof should match the walls
}
}
for (int dz = origin.z; dz <= origin.z + l; dz++) {
world[origin.x][dy][dz] = colour;
world[origin.x + w][dy][dz] = colour;
}
}
fillVolume(offsetPoint(origin, 1, 1, 1), l-1, w-1, h-1, 0); //why didnt i do this before.
} //i like pot_hole but i guess i'll SnakeStance this shit
struct coord getMidpoint(struct coord a, struct coord b) {
struct coord midpoint;
midpoint.x = (a.x + b.x) / 2; //int math
midpoint.y = FLOORHEIGHT;
midpoint.z = (a.z + b.z) / 2;
return midpoint;
} //FIXME: not deprecated! :)
struct coord getRandomDimensions(int min, int max) {
//this is the brain damaged version of the above function. rooms cant exceed max size in any dimension.
struct coord dimension;
dimension.x = getRandomNumber(min, max);
dimension.y = getRandomNumber(min, max);
dimension.z = getRandomNumber(min, max);
//////printf("generated dimensions: %d %d %d\n", dimensions.x, dimensions.y, dimensions.z);
return dimension; //why has god forsaken me i just wanted to use a quadtree.
} //x = w; y = h; z = l
int meshVisibilityCheck(int id){
//printf("mesh #%d's active status is (%d) and presence is (%d)\n", getActive(id), getPresent(id));
if (getActive(id) && getPresent(id)) {
struct coord mob = getMobCoord(id);
const double viewDistance = 40.0;
int visible = SphereInFrustum((float)mob.x, (float)(mob.y + 1), (float)mob.z, 1.5); //we'll just pretend y doesnt exist
if (getDistance2d(curr_viewpos, mob) > viewDistance) {
visible = 0;
} //if the object is too far we'll say its not visible
if (visible) {
//printf("Cow mesh #%d is visible\n",id);
drawMesh(id);
} else if (!visible){ //TODO: performance will tank if tehres tons of mobs even if they're invisible won't it?
//printf("Cow mesh #%d is not visible\n",id);
hideMesh(id);
}
}
}
bool proximityCheck(int pindex, int margin) { //idc idgaf that its bad this is not my fault i just wanted qtree
bool result = true; //yes i know this doesnt account for the dimensions of point 2 but dont fuCking worry ab it
//int NUMROOMS = sizeof(points)/sizeof(*points); //ONLY PASS AN ARRAY HERE!!!
//////printf("NUMROOMS resolves to %d\n", NUMROOMS);
struct coord point1_max;
int longer_side = 0;
point1_max.x = points[pindex].x + dimensions[pindex].x;
point1_max.y = FLOORHEIGHT;
point1_max.z = points[pindex].z + dimensions[pindex].z;
struct coord center = getMidpoint(points[pindex], point1_max); //this is the center of our circle;
if (dimensions[pindex].x > dimensions[pindex].z) {
longer_side = dimensions[pindex].x;
} else {longer_side = dimensions[pindex].z;} //the longer side len / 2 will be used as the radius
int r1 = (longer_side / 2) + margin;
//TODO: optimally, this would only check adjacents, but i dont have time to do that right now
for(int i = meshIndex; i < meshIndex + NUMROOMS; i++) {
if (!(i == pindex)) {
struct coord point2_max;
int longer_side_r2 = 0;
point2_max.x = points[i].x + dimensions[i].x;
point2_max.y = FLOORHEIGHT;
point2_max.z = points[i].z + dimensions[i].z;
struct coord center2 = getMidpoint(points[i], point2_max); //this is the center of our circle;
if (dimensions[i].x > dimensions[i].z) {
longer_side_r2 = dimensions[i].x;
} else {longer_side_r2 = dimensions[i].z;} //the longer side len / 2 will be used as the radius
int r2 = longer_side_r2 + margin;
int d = (int)getDistance2d(center, center2); //INTEGER TRUNCATION BLAH BLAH BLUUUURGH
if (d < (r1 + r2)) {
result = false;
break;
}
}
}
return result;
} //FIXME: no longer deprecated :)
void createDoor(struct coord a, int w, int h) {
for (int dw = a.x; dw < a.x + w; dw++) {
for (int dh = a.y + 1; dh < a.y + h; dh++) {
world[dw][dh][a.z] = 0;
}
}
} //FIXME: deprecated: bad
void joinRooms(int r1_index, int r2_index, const int w, const int h, GLbyte colour) {
int mid;
if (r2_index == r1_index + 3) { //mode 0 - x*
mid = (points[r2_index].z - (points[r1_index].z + dimensions[r1_index].z)) / 2;
//draw the corridors from and to two rooms
createRoom(offsetPoint(points[r1_index], 0, 0, dimensions[r1_index].z), mid, w, h, colour);
createRoom(offsetPoint(points[r2_index], 0, 0, -mid), mid, w, h, colour); //IMPORTANT: keep ratio o:o-1
//draw the horizontal (connecting) corridor
/*since we can't draw right to left becuase i dont want to change anything,
we draw the corridor from the leftmost side first. love to just use abs but 🤷*/
if (points[r1_index].x < points[r2_index].x) { //x+
//////printf("todoroki %d\n", points[r2_index].x - points[r1_index].x); //w is passed as the l because its horizontal
createRoom(offsetPoint(points[r1_index], 0, 0, mid + dimensions[r1_index].z), w, points[r2_index].x - points[r1_index].x + w, h, colour); //TODO: + w in w dubious?
fillVolume(offsetPoint(points[r1_index], 1, 1, mid + dimensions[r1_index].z + 1), w - 1, points[r2_index].x - points[r1_index].x + w - 1, h - 1, 0);
} else{ //x-
//////printf("else %d\n", points[r1_index].x - points[r2_index].x);
createRoom(offsetPoint(points[r2_index], 0, 0, -mid), w, points[r1_index].x - points[r2_index].x + w, h, colour);
fillVolume(offsetPoint(points[r2_index], 1, 1, -mid + 1), w - 1, points[r1_index].x - points[r2_index].x + w - 1, h - 1, 0);
}
//clear the vertical tunnels
fillVolume(offsetPoint(points[r1_index], 1, 1, dimensions[r1_index].z), mid + w - 1, w - 1, h - 1, 0);
fillVolume(offsetPoint(points[r2_index], 1, 1, -mid + 1), mid + w - 1, w - 1, h - 1, 0);
//////printf("%d/%d's corridor pair generated.\n", r1_index, r2_index);
}
else { //mode 1 - z*
mid = ((points[r2_index].x - (points[r1_index].x + dimensions[r1_index].x)) / 2) - 1;
bool odd = !(mid % 2);
//draw the corridors from and to two rooms
createRoom(offsetPoint(points[r1_index], dimensions[r1_index].x, 0, 0), w, mid, h, colour); //offset wrong?
createRoom(offsetPoint(points[r2_index], -mid, 0, 0), w, mid, h, colour);
//draw the horizontal (connecting) corridor
if (points[r1_index].z < points[r2_index].z) { //z+?
//////printf("todoroki %d\n", points[r2_index].z - points[r1_index].z); //w is passed as the l because its horizontal
createRoom(offsetPoint(points[r1_index], mid + dimensions[r1_index].x, 0, 0), points[r2_index].z - points[r1_index].z + w, w, h, colour); //TODO: + w in w dubious?
fillVolume(offsetPoint(points[r1_index], mid + dimensions[r1_index].x + 1, 1, 1), points[r2_index].z - points[r1_index].z + w - 1, w - 1, h - 1, 0);
}
else{ //z-
//////printf("else %d\n", points[r1_index].z - points[r2_index].z);
createRoom(offsetPoint(points[r2_index], -(mid + w), 0, 0), points[r1_index].z - points[r2_index].z + w, w, h, colour);
fillVolume(offsetPoint(points[r2_index], -(mid + w) + 1, 1, 1), points[r1_index].z - points[r2_index].z + w - 1, w - 1, h - 1, 0);
}
//clear the "horizontal tunnels"
fillVolume(offsetPoint(points[r1_index], dimensions[r1_index].x, 1, 1), w - 1, mid + 1, h - 1, 0); //aioubdiuq3TODO: THIS FIXME: THIS
fillVolume(offsetPoint(points[r2_index], (-mid), 1, 1), w - 1, mid + 1, h - 1, 0);
//////printf("%d/%d's corridor pair generated.\n", r1_index, r2_index);
} //FIXME: for some reason, this generates holes in walls and doesnt make doorways. maybe the connection isnt center? maybe fix later
}
/* CODE FROM WIKIPEDIA
* Function to linearly interpolate between a0 and a1
* Weight w should be in the range [0.0, 1.0]
*/
float interpolate(float a0, float a1, float w) {
/* // You may want clamping by inserting:
* if (0.0 > w) return a0;
* if (1.0 < w) return a1;
*/
return (a1 - a0) * w + a0;
/* // Use this cubic interpolation [[Smoothstep]] instead, for a smooth appearance:
* return (a1 - a0) * (3.0 - w * 2.0) * w * w + a0;
*
* // Use [[Smootherstep]] for an even smoother result with a second derivative equal to zero on boundaries:
* return (a1 - a0) * ((w * (w * 6.0 - 15.0) + 10.0) * w * w * w) + a0;
*/
}
typedef struct {
float x, y;
} vector2;
/* Create random direction vector
*/
vector2 randomGradient(int ix, int iy) {
// Random float. No precomputed gradients mean this works for any number of grid coordinates
float random = 2920.f * sin(ix * 21942.f + iy * 171324.f + 8912.f) * cos(ix * 23157.f * iy * 217832.f + 9758.f);
return (vector2) { .x = cos(random), .y = sin(random) };
}
// Computes the dot product of the distance and gradient vectors.
float dotGridGradient(int ix, int iy, float x, float y) {
// Get gradient from integer coordinates
vector2 gradient = randomGradient(ix, iy);
// Compute the distance vector
float dx = x - (float)ix;
float dy = y - (float)iy;
// Compute the dot-product
return (dx*gradient.x + dy*gradient.y);
}
// Compute Perlin noise at coordinates x, y
float perlin(float x, float y) {
// Determine grid cell coordinates
int x0 = (int)x;
int x1 = x0 + 1;
int y0 = (int)y;
int y1 = y0 + 1;
// Determine interpolation weights
// Could also use higher order polynomial/s-curve here
float sx = x - (float)x0;
float sy = y - (float)y0;
// Interpolate between grid point gradients
float n0, n1, ix0, ix1, value;
n0 = dotGridGradient(x0, y0, x, y);
n1 = dotGridGradient(x1, y0, x, y);
ix0 = interpolate(n0, n1, sx);
n0 = dotGridGradient(x0, y1, x, y);
n1 = dotGridGradient(x1, y1, x, y);
ix1 = interpolate(n0, n1, sx);
value = interpolate(ix0, ix1, sy);
return value;
}
void generateCave() {
//zeroth, let's clear everything:
for (int x = 0; x < WORLDX; x++) {
for (int y = 0; y < WORLDY; y++) {
for (int z = 0; z < WORLDZ; z++) {
world[x][y][z] = 0;
}
}
}
//first, let's generate the flat floor: (this could be combined with the last loop but idc)
for (int x = 0; x < WORLDX; x++) {
for (int z = 0; z < WORLDZ; z++) {
world[x][FLOORHEIGHT][z] = 26;
}
}
const float max_height = 25.0;
const float scalar = 15.0;
const float offset = (float)getRandomNumber(0,GL_MAX);
float y_offset = 0.0;
float x, y, z = 0.0;
//now, let's make the cave:
for (int x_world = 0; x_world < WORLDX; x_world++) { //note that x_world & z_world are integers!~
x = ((float)(x_world - WORLDX/2))/(float)(WORLDX/2);
for (int z_world = 0; z_world < WORLDZ; z_world++) {
z = ((float)(z_world - WORLDZ/2))/(float)(WORLDZ/2);
y_offset = perlin((float)x_world / scalar + offset, (float)z_world / scalar + offset);
y = (1 - (squared(x) + squared(z)) / 2.0) + y_offset;
int y_world = (int)(y * max_height) + FLOORHEIGHT;
for (int thickness = 0; thickness < 5; thickness++) {
world[x_world][y_world-thickness][z_world] = 26;
}
}
}
//now, let's generate the mobs:
for (int cow_room = meshIndex; cow_room < (currfloor * NUMROOMS); cow_room++) {
int mesh_identity = 1; //they're all fish
//let's just spawn em at random spots
//we need to move the fish up more cuz if we dont it spawns in the ground lol
struct coord cow_pos = {getRandomNumber(3,WORLDX-3),
FLOORHEIGHT + 2,
getRandomNumber(3,WORLDZ-3)};
//cow_room actually also represents the index of the mesh, conveniently. the math just checks out, maybe i should rename it ?
setMeshID(cow_room, mesh_identity, (float)cow_pos.x,
(float)cow_pos.y, //he flies
(float)cow_pos.z); //don't forget to de-calvertize
}
floors[currfloor] = calloc(1, sizeof(struct record));
//the stairs will just be placed randomly somewhere
floors[currfloor]->stairs[STAIRS_UP] = (struct coord){getRandomNumber(10, WORLDX-10), FLOORHEIGHT, getRandomNumber(10, WORLDZ-10)};
floors[currfloor]->stairs[STAIRS_DOWN] = (struct coord){getRandomNumber(10, WORLDX-10), FLOORHEIGHT, getRandomNumber(10, WORLDZ-10)};
printf("stairs1 %d %d %d stairs2 %d %d %d\n", floors[currfloor]->stairs[STAIRS_UP].x, floors[currfloor]->stairs[STAIRS_UP].y, floors[currfloor]->stairs[STAIRS_UP].z, floors[currfloor]->stairs[STAIRS_DOWN].x, floors[currfloor]->stairs[STAIRS_DOWN].y, floors[currfloor]->stairs[STAIRS_DOWN].z);
//HERE'S HOPING THEY DON'T OVERLAP LOL
world[floors[currfloor]->stairs[STAIRS_UP].x][floors[currfloor]->stairs[STAIRS_UP].y][floors[currfloor]->stairs[STAIRS_UP].z] = 24; //creates up
world[floors[currfloor]->stairs[STAIRS_DOWN].x][floors[currfloor]->stairs[STAIRS_DOWN].y][floors[currfloor]->stairs[STAIRS_DOWN].z] = 25; //creates down
//spawn should be near the stairs
struct coord spawn = offsetPoint(floors[currfloor]->stairs[STAIRS_UP], getRandomNumber(4,7), 2, getRandomNumber(4,7));
//now, we'll handle saving.
for(int sx=0; sx<WORLDX; sx++) {
for(int sy=0; sy<WORLDY; sy++) {
for(int sz=0; sz<WORLDZ; sz++) {
floors[currfloor]->world[sx][sy][sz] = world[sx][sy][sz];
}
}
}
floors[currfloor]->spawn = spawn;
curr_viewpos.x = spawn.x; curr_viewpos.y = spawn.y; curr_viewpos.z = spawn.z;
setViewPosition((float)spawn.x, (float)spawn.y, (float)spawn.z);
}
void generateMaze() {
//just read i need to generate the maze in three rows
//three rows >:(
//imagine making a perfectly fine quadtree then being forced to mutilate it into "three rows" waht a stupid clause
setUserColour(24, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0); //for up stairs
setAssignedTexture(24, 31);
setUserColour(25, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0); //for down stairs
setAssignedTexture(25, 32); //please
/*in order to generate "three rows", i will do the following instead of just normally using the quadtree as described in lecture
(i really do not understand why you'd detail a really nice method like quadtree and then make everyone generate a completely
different structure. seems pretty mean-spirited to me and i really wish i could just generate 9 rooms in any way i want): i will
generate 9 points in predefined zones, then then generate 9 rooms of random sizes in three three "rows" that you have described.
these rooms will also be guaranteed to be at least 1 block away from each other and not intersect. why cant i just quadtree*/
////printf("beginnign maze generation!\n");
const int min_d = 8;
const int max_d = 15;
const int padding = min_d + (min_d/2) + 2;
const int roomsepMargin = 0; //play w this like u play with my heart
int borderz1 = WORLDZ/3; //alice in borderland(s 2)
int borderz2 = 2*(WORLDZ/3);
int borderz3 = WORLDZ - max_d;
int borderx1 = WORLDX/3;
int borderx2 = 2*(WORLDX/3);
int borderx3 = WORLDX - max_d;
struct coord stairs[2]; //shouldn't this actually always be 2 since its stored in the floors[] array...
struct coord spawn;
const int w = 5;
const int h = 5; //could make this random, min = 2, max = dimensions[i].h; but i dont really care
GLbyte colour = 18;
int mid;
bool generationSuccess = true;
do {
generationSuccess = true;
for(int i=0; i<WORLDX; i++) {
for(int j=0; j<WORLDY; j++) {
for(int k=0; k<WORLDZ; k++) {
world[i][j][k] = 0;
}
}
}
printf("generation attempt\n");
//the following code generates 9 rooms in 9 zones with padding so they don't intersect.
points[meshIndex + 0] = generatePoint(0, borderx1 - padding, 0, borderz1 - padding);
dimensions[meshIndex + 0] = getRandomDimensions(min_d, max_d);
points[meshIndex + 1] = generatePoint(borderx1 - padding, borderx2 - padding, 0, borderz1 - padding);
dimensions[meshIndex + 1] = getRandomDimensions(min_d, max_d);
points[meshIndex + 2] = generatePoint(borderx2 + padding, WORLDX - max_d, 0, borderz1 - padding);