-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDragon_Game_mockup.pde
1450 lines (1348 loc) · 62.7 KB
/
Dragon_Game_mockup.pde
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
//import pallav.Matrix.*; //<>// //<>// //<>// //<>// //<>// //<>//
/****************************************
* Dragon Game Mockup by Daniel Demski.
*
* Generates nonperiodic (quasicrystal) lattices in 3D
* and allows the user to place and delete blocks within
* the lattice. Change "r" inside "generate()" to add more
* (or less) blocks.
*
* Thoughts on directions for this document: I want to settle on a
* chunking method which will work for terrain generation on the
* golden rhombus grid. Once that's working, I can reimplement
* in a more capable language to see how good I can get the
* performance. However, I think at that point it might be good
* for the project to split rather than just move on. I want both
* a game built around a single tesselation, and a more general
* platform which presents voxel-building and terrain-generation
* capabilities on a wide variety of 3D tesselations. Ideally,
* it would be something others could contribute new tesselations
* to, in order to explore a wide variety of them.
*
* Next step is to try "6D chunks", ie, a hierarchical cubic
* structure in 6D where a voxel belongs to a chunk if a
* 6-cube adjacent to the voxel (voxels are 3-facets of 6-cubes)
* belongs to the chunk. The 6D chunks would simply have some
* integer size, e.g. powers of 2 or 3. Voxel contents of a chunk
* would approximate the shape of its intersection with the world-
* plane, rather than a scaled-up shape from the tesselation (ie,
* the dual of the set of such intersections; or, some choice of
* 3-facets of these chunks). However the 3-facets of these chunks
* (or, again, the dual of their intersections w/ world-plane)
* could still be useful for displaying low-detail stuff at a
* distance, and maybe for iterative terrain generation.
*
* Because the chunks are 6D, they are inherently fairly general
* and should allow some other tesselations to work. Also, they
* aren't reflecting a true self-similarity and so the valid
* chunk structures need to more or less be generated indefinitely;
* which again is fairly general.
*
* TODO Write hierarchical chunk class capable of generating terrain
* via inflation and deflation.
* TODO Test hierarchical chunk class using simple hand-coded inflation/deflation templates (e.g. cubes).
* TODO Make a subclass which can store the extra data (including world-plane position) for 6D chunks.
* TODO Intelligently avoid generating new chunk-types when plane position falls within a (3D) range of validity for existing types.
*
* SCRATCH ALL THAT I finally found a paper with a substitution rule for this tiling.
* There's a bit of a catch (some ambiguity in how d30s are filled in, as expected) but it's
* a great starting point for working chunks.
*
* TODO Support for all Boyle/Steinhardt 2016 tilings
* TODO Support for rhombic dodecahedron & other periodic tilings
* TODO SCD tiling (Schmitt, Conway, Danzer)
* TODO Danzer tiling (tetrahedral, octahedral)
* TODO Dual of Danzer tiling (Aranda, Lasch, Bosia, 2007)
*
*****************************************/
//import queasycam.*;
boolean test_assertions = true;
boolean picky_assertions = false;
boolean render_raw_quasicrystal = true;
float driftspeed = 1;
// The phi cubed chunk_ratio looks promising: out of 4118 chunks, ended up with
// 33 unique. BUT they had no child blocks, which is very confusing. I think my
// distance cutoff was too low when searching for overlapping points. So the
// low number of uniques is just because of that.
float chunk_ratio = ((1+sqrt(5))/2);//((1+sqrt(5))/2)*((1+sqrt(5))/2)*((1+sqrt(5))/2);//2.0;
boolean skip_classif = true;
boolean run = true;
boolean firstrun = true;
int initialdelay = 1;
int rounddelay = 10;
boolean playSetupDone = false;
boolean spacePressed = false;
boolean clicked = false;
//Point6D w;
float CameraX = 0;
float CameraY = 0;
float CameraZ = 0;
float CameraRX = 0;
float CameraRY = 0;
float CameraRZ = 0;
QueasyCam cam;
PMatrix3D originalMatrix;
Quasicrystal main_lattice;
Quasicrystal main_chunk_lattice;
void setup() {
size(displayWidth, displayHeight, P3D);
smooth(3);
background(100);
//camera(width/2.0,height/2.0,(height/2.0) / tan(PI*30.0 / 180.0), width/2.0, height/2.0, 0, 0, 1, 0);
//frustum(10, -10, -10*float(displayHeight)/displayWidth, 10*float(displayHeight)/displayWidth, 10, 1000000);
originalMatrix = ((PGraphicsOpenGL)this.g).camera;
cam = new QueasyCam(this);
cam.speed = 0.06;
cam.sensitivity = 0.18;
processing.core.PApplet.class.getClassLoader().setClassAssertionStatus(processing.core.PApplet.class.getName(), test_assertions);
}
void draw() {
lights();
if (spacePressed) {
run = !run;
spacePressed = false;
}
if (run) {
generate();
if (firstrun) {
delay(initialdelay);
firstrun = false;
} else {
delay(rounddelay);
}
while (!run) {
delay(500);
}
} else {
render();
}
if (run) run = !run;
}
void drawCrosshair() {
hint(DISABLE_DEPTH_TEST);
int ccolor = rotateColor(cam.applet.get(width/2, height/2));
stroke(ccolor);
PVector hudorigin = cam.position.copy().add(cam.getForward());
PVector rightside = hudorigin.copy().add(cam.getRight().copy().mult(0.01));
PVector leftside = hudorigin.copy().add(cam.getRight().copy().mult(-0.01));
line(rightside.x, rightside.y, rightside.z, leftside.x, leftside.y, leftside.z);
PVector up = cam.getForward().copy().cross(cam.getRight());
PVector topside = hudorigin.copy().add(up.copy().mult(0.01));
PVector bottomside = hudorigin.copy().add(up.copy().mult(-0.01));
line(topside.x, topside.y, topside.z, bottomside.x, bottomside.y, bottomside.z);
hint(ENABLE_DEPTH_TEST);
}
int rotateColor(int c) {
float r = (red(c) + 128)%256;
float g = (green(c) + 128)%256;
float b = (blue(c) + 128)%256;
return color(r, g, b);
}
void setupRender() {
if (render_raw_quasicrystal) {
// Just generating one chunk of lattice to render in.
// Mainly done in generate(), we just finish it off with conversions
// to 3D and some filled-in blocks.
Quasicrystal main_lattice = (Quasicrystal)this.main_lattice;
for (Object o : main_lattice.cells) {
Vertex v = (Vertex)(o);
v.location_3D = new PVector(v.minus(main_lattice.fivedeew).dot(main_lattice.fivedee0), v.minus(main_lattice.fivedeew).dot(main_lattice.fivedee1), v.minus(main_lattice.fivedeew).dot(main_lattice.fivedee2));
}
for (Object o : main_lattice.rhombs) {
Rhomb r = (Rhomb)(o);
r.center_3D = new PVector(r.center.minus(main_lattice.fivedeew).dot(main_lattice.fivedee0), r.center.minus(main_lattice.fivedeew).dot(main_lattice.fivedee1), r.center.minus(main_lattice.fivedeew).dot(main_lattice.fivedee2));
}
if (test_assertions) {
for (Block block : (Iterable<Block>)(main_lattice.blocks)) {
for (Rhomb face : block.sides) {
assert main_lattice.rhombs.list.contains(face):
"unregistered rhomb as face";
assert main_lattice.cells.list.contains(face.corner1):
"unregistered vertex as corner";
assert face.corner1.location_3D != null :
"Supposedly-registered vertex didn't get a 3D location";
}
}
}
int selection;
for (int loopvar = 0; loopvar < 100; loopvar++) {
selection = floor(random(main_lattice.blocks.size()));
main_lattice.blocks.list.get(selection).value = ceil(random(0, 20));
}
for (Object o : main_chunk_lattice.cells) {
Vertex v = (Vertex)(o);
v.location_3D = new PVector(v.minus(main_chunk_lattice.fivedeew).dot(main_chunk_lattice.fivedee0), v.minus(main_chunk_lattice.fivedeew).dot(main_chunk_lattice.fivedee1), v.minus(main_chunk_lattice.fivedeew).dot(main_chunk_lattice.fivedee2));
}
for (Object o : main_chunk_lattice.rhombs) {
Rhomb r = (Rhomb)(o);
r.center_3D = new PVector(r.center.minus(main_chunk_lattice.fivedeew).dot(main_chunk_lattice.fivedee0), r.center.minus(main_chunk_lattice.fivedeew).dot(main_chunk_lattice.fivedee1), r.center.minus(main_chunk_lattice.fivedeew).dot(main_chunk_lattice.fivedee2));
}
for (int loopvar = 0; loopvar < 80; loopvar++) {
selection = floor(random(main_chunk_lattice.blocks.size()));
main_chunk_lattice.blocks.list.get(selection).value = ceil(random(0, 20));
}
} else {
// Chunks will be generated as needed.
}
playSetupDone = true;
}
void render() {
if (!playSetupDone) {
setupRender();
}
background(0, 100, 0);
// TODO Render in vectors fivedeex, fivedeey and fivedeez
// from each lattice
//stroke(0);
//noFill();
//beginShape();
//vertex(0,0,0);
ArrayList<Rhomb> pointedAt = new ArrayList<Rhomb>();
for (Block block : (Iterable<Block>)(main_lattice.blocks)) {
if (block.value > 0) {
for (Rhomb face : block.sides) {
boolean hasair = true;
if (face.parents.size() == 2) {
if (face.parents.get(0).value > 0 && face.parents.get(1).value > 0) {
hasair = false;
}
}
if (hasair) {
// We know this block isn't air and any neighbor is,
// so we aren't rendering twice here
if (cameraPoint(face)) {
pointedAt.add(face);
}
stroke(100);
//noStroke();
fill(block.value*10, block.value*5, 255-block.value*10);
beginShape();
vertex(face.corner1.location_3D.x, face.corner1.location_3D.y, face.corner1.location_3D.z);
vertex(face.corner2.location_3D.x, face.corner2.location_3D.y, face.corner2.location_3D.z);
vertex(face.corner4.location_3D.x, face.corner4.location_3D.y, face.corner4.location_3D.z);
vertex(face.corner3.location_3D.x, face.corner3.location_3D.y, face.corner3.location_3D.z);
endShape(CLOSE);
}
}
}
}
if (pointedAt.size() > 0) {
Rhomb closest = pointedAt.get(0);
float closestDist = closest.center_3D.copy().sub(cam.position).dot(cam.getForward());
for (int i = 1; i < pointedAt.size(); i++) {
float dist = pointedAt.get(i).center_3D.copy().sub(cam.position).dot(cam.getForward());
if (closestDist > dist) {
closestDist = dist;
closest = pointedAt.get(i);
}
}
Block block = closest.parents.get(0);
if (closest.parents.size() == 2) {
// Both sides are generated
Block empty = closest.parents.get(1);
if (block.value == 0) {
block = closest.parents.get(1);
empty = closest.parents.get(0);
}
if (clicked) {
if (mouseButton == LEFT) empty.value += ceil(random(0, 10));
if (mouseButton == RIGHT) block.value = 0;
clicked = false;
}
stroke(255);
noFill();
for (Rhomb face : empty.sides) {
beginShape();
vertex(face.corner1.location_3D.x, face.corner1.location_3D.y, face.corner1.location_3D.z);
vertex(face.corner2.location_3D.x, face.corner2.location_3D.y, face.corner2.location_3D.z);
vertex(face.corner4.location_3D.x, face.corner4.location_3D.y, face.corner4.location_3D.z);
vertex(face.corner3.location_3D.x, face.corner3.location_3D.y, face.corner3.location_3D.z);
endShape(CLOSE);
}
stroke(0, 255, 0);
fill(255-(255-block.value*25)*0.8, 255-(255-block.value*10)*0.8, 255-block.value*25*0.8);
beginShape();
vertex(closest.corner1.location_3D.x, closest.corner1.location_3D.y, closest.corner1.location_3D.z);
vertex(closest.corner2.location_3D.x, closest.corner2.location_3D.y, closest.corner2.location_3D.z);
vertex(closest.corner4.location_3D.x, closest.corner4.location_3D.y, closest.corner4.location_3D.z);
vertex(closest.corner3.location_3D.x, closest.corner3.location_3D.y, closest.corner3.location_3D.z);
endShape(CLOSE);
} else {
stroke(255, 255, 0);
fill(255-(255-block.value*25)*0.8, 255-(255-block.value*10)*0.8, 255-block.value*25*0.8);
if (clicked) {
if (mouseButton == RIGHT) block.value = 0;
clicked = false;
//println(block.center.point);
}
beginShape();
vertex(closest.corner1.location_3D.x, closest.corner1.location_3D.y, closest.corner1.location_3D.z);
vertex(closest.corner2.location_3D.x, closest.corner2.location_3D.y, closest.corner2.location_3D.z);
vertex(closest.corner4.location_3D.x, closest.corner4.location_3D.y, closest.corner4.location_3D.z);
vertex(closest.corner3.location_3D.x, closest.corner3.location_3D.y, closest.corner3.location_3D.z);
endShape(CLOSE);
}
}
for (Block b : (Iterable<Block>)(main_chunk_lattice.blocks)) {
if (b.value > 0) {
for (Rhomb r : b.sides) {
noFill();
stroke(255, 0, 255);
beginShape();
vertex(r.corner1.location_3D.x, r.corner1.location_3D.y, r.corner1.location_3D.z);
vertex(r.corner2.location_3D.x, r.corner2.location_3D.y, r.corner2.location_3D.z);
vertex(r.corner4.location_3D.x, r.corner4.location_3D.y, r.corner4.location_3D.z);
vertex(r.corner3.location_3D.x, r.corner3.location_3D.y, r.corner3.location_3D.z);
endShape(CLOSE);
}
}
}
drawCrosshair();
}
boolean cameraPoint(Rhomb face) {
// Use camera as origin
PVector c1 = face.corner1.location_3D.copy().sub(cam.position);
PVector c2 = face.corner2.location_3D.copy().sub(cam.position);
PVector c3 = face.corner3.location_3D.copy().sub(cam.position);
PVector c4 = face.corner4.location_3D.copy().sub(cam.position);
float c1f = cam.forward.dot(c1);
float c2f = cam.forward.dot(c2);
float c3f = cam.forward.dot(c3);
float c4f = cam.forward.dot(c4);
// Don't want to do any more multiplication unless we have to
if (c1f > 0 || c2f > 0 || c3f > 0 || c4f > 0) {
// flattening onto a plane
c1.sub(cam.forward.copy().mult(c1f));
c2.sub(cam.forward.copy().mult(c2f));
c3.sub(cam.forward.copy().mult(c3f));
//c4.sub(cam.forward.copy().mult(c4f));
// Now for an actual plane
Point2D flat1 = new Point2D(0, 0);
Point2D flat2 = new Point2D(0, 0);
Point2D flat3 = new Point2D(0, 0);
//Point2D flat4 = new Point2D(0, 0);
Point2D flatcenter = new Point2D(0, 0);// The camera's position is genuinely (0,0).
// We're discarding whichever dimension varies least.
for (int d = 0; d < 3; d++) {
if (max(cam.forward.array()) == cam.forward.array()[d]) {
int j = 0;
for (int i=0;; j++) {
if (i == d) i++;
if (j>=2) break;
flat1.point[j] = c1.array()[i];
flat2.point[j] = c2.array()[i];
flat3.point[j] = c3.array()[i];
//flat4.point[j] = c4.array()[i];
i++;
}
break;
}
}
Point2D edge1 = flat2.minus(flat1);
Point2D axis1 = edge1.orthoflip();
float dp1 = flat1.dot(axis1);
float dp2 = 0;// always works out to zero
float dp3 = flat3.dot(axis1);
if ((dp1 <= dp2 && dp2 <= dp3) || (dp1 >= dp2 && dp2 >= dp3)) {
// Finally we know something: screen center is between two of the edges. Check other two.
Point2D edge2 = flat3.minus(flat1);
Point2D axis2 = edge2.orthoflip();
dp1 = flat1.dot(axis2);
dp2 = 0;// always works out to zero
dp3 = flat2.dot(axis2);
if ((dp1 <= dp2 && dp2 <= dp3) || (dp1 >= dp2 && dp2 >= dp3)) {
return(true);
}
}
}
return(false);
}
Rhomb getNext(Rhomb r, int arrowdim, int arrowdir) {
if (r.axis1 == arrowdim && arrowdir < 0) return r.a1prev;
if (r.axis2 == arrowdim && arrowdir < 0) return r.a2prev;
if (r.axis1 == arrowdim && arrowdir > 0) return r.a1next;
if (r.axis2 == arrowdim && arrowdir > 0) return r.a2next;
return null;
}
void keyPressed() {
if (key==' ') {
spacePressed = true;
}
}
void mouseClicked() {
clicked = true;
}
void generate() {
//float[] fivedeex = {random(-10,10),random(-10,10),random(-10,10),random(-10,10),random(-10,10)};
//float[] fivedeey = {random(-10,10),random(-10,10),random(-10,10),random(-10,10),random(-10,10)};
//float[] fivedeew = {random(-10,10),random(-10,10),random(-10,10),random(-10,10),random(-10,10)};// The position of the screen's origin
// Penrose
float phi = (1+sqrt(5))/2;
Point6D x = new Point6D(new float[]{phi, 0, 1, phi, 0, -1});
Point6D y = new Point6D(new float[]{1, phi, 0, -1, phi, 0});
Point6D z = new Point6D(new float[]{0, 1, phi, 0, -1, phi});
Point6D w = new Point6D(new float[]{0.3, 0.5, 0.7, 0.11, 0.13, 0.17});
//Point6D w = new Point6D(new float[]{0.5, 0.5, 0.5, 0.5, 0.5, 0.5});
// Simple cube grid!!
//Point6D x = new Point6D(new float[]{1, 0, 0, 0, 0, 0});
//Point6D y = new Point6D(new float[]{0, 1, 0, 0, 0, 0});
//Point6D z = new Point6D(new float[]{0, 0, 1, 0, 0, 0});
//Point6D w = new Point6D(new float[]{0, 0, 0, 0, 0, 0});
//float rad = 12;//12;
//float chunk_ratio = 2;
if (render_raw_quasicrystal) {
chunkNetwork cn = new chunkNetwork(w, x, y, z);
} else {
// TODO move some of this setup into the CubeChunk class, like a getFirstChunk() or init() method or something
CubeChunk first_chunk = new CubeChunk(new PVector(-0.5,-0.5,-0.5), 1);
//main_lattice = new GridPatch();
//BlockStore firstblocks = first_chunk.get_blocks();
//for (Block b: firstblocks)
// main_lattice.addBlock(b);
}
/*lattice = new Quasicrystal(w, x, y, z, rad);
// What we want to do with the chunks is tilt the 3D basis within higher-D by the correct amount
// to create a new tiling similar to the old but stretched out.
chunk_lattice = new Quasicrystal(w.times(1.0/chunk_ratio), x.times(1.0/chunk_ratio), y.times(1.0/chunk_ratio), z.times(1.0/chunk_ratio), rad*(1.0/chunk_ratio));
// Scale everything in chunk_lattice back up
chunk_lattice.fivedeex = chunk_lattice.fivedeex.times(chunk_ratio);
chunk_lattice.fivedeey = chunk_lattice.fivedeey.times(chunk_ratio);
chunk_lattice.fivedeez = chunk_lattice.fivedeez.times(chunk_ratio);
chunk_lattice.fivedeew = chunk_lattice.fivedeew.times(chunk_ratio);
for (Rhomb rhomb : chunk_lattice.rhombs) {
rhomb.center = rhomb.center.times(chunk_ratio);
rhomb.corner1 = rhomb.corner1.times(chunk_ratio);
rhomb.corner2 = rhomb.corner2.times(chunk_ratio);
rhomb.corner3 = rhomb.corner3.times(chunk_ratio);
rhomb.corner4 = rhomb.corner4.times(chunk_ratio);
}
for (Block b : chunk_lattice.blocks) {
b.center = b.center.times(chunk_ratio);
}
classifyChunks();*/
}
Matrix unitt(int i, int j) {
/* Creates a matrix which will add the ith
* coordinate to the jth
*/
Matrix m = Matrix.identity(6);
m.array[i][j] += 1;
return m;
}
class chunkNetwork {
ArrayList<Chunk> chunkTypes;
public chunkNetwork(Point6D initial_w, Point6D x, Point6D y, Point6D z) {
float searchradius = 13;// needs to be big enough to guarantee one fully-populated chunk
// Normalize in order to make searchradius "accurate"
x = x.normalized();
y = y.normalized();
z = z.normalized();
println("Generating block lattice...");
Quasicrystal lattice = new Quasicrystal(initial_w, x, y, z, searchradius);
Matrix m = Matrix.Multiply(Matrix.Multiply(unitt(0, 3), unitt(1, 4)), unitt(2, 5));
//Matrix m = Matrix.Multiply(Matrix.Multiply(unitt(0,1),unitt(1,2)),unitt(2,0));
Matrix im = Matrix.inverse(m);
//Quasicrystal lattice = new Quasicrystal(initial_w, x.plus(y), y.plus(z), z, searchradius);
Point6D x_chunk = x.copy();
Point6D y_chunk = y.copy();
Point6D z_chunk = z.copy();
Point6D w_chunk = initial_w.copy();
x_chunk.point = Matrix.Multiply(Matrix.array(new float[][] {x_chunk.point}), m).array[0];
y_chunk.point = Matrix.Multiply(Matrix.array(new float[][] {y_chunk.point}), m).array[0];
z_chunk.point = Matrix.Multiply(Matrix.array(new float[][] {z_chunk.point}), m).array[0];
w_chunk.point = Matrix.Multiply(Matrix.array(new float[][] {w_chunk.point}), m).array[0];
println("Generating chunk lattice...");
//Quasicrystal chunk_lattice = new Quasicrystal(initial_w.times(1.0/chunk_ratio), x.times(1.0/chunk_ratio), y.times(1.0/chunk_ratio), z.times(1.0/chunk_ratio), searchradius*(1.0/chunk_ratio));
Quasicrystal chunk_lattice = new Quasicrystal(w_chunk, x_chunk, y_chunk, z_chunk, searchradius);
/*x.point[0] = lattice.fivedeex.point[0] + lattice.fivedeex.point[1];
y.point[0] = lattice.fivedeey.point[0] + lattice.fivedeey.point[1];
z.point[0] = lattice.fivedeez.point[0] + lattice.fivedeez.point[1];
initial_w.point[0] = lattice.fivedeew.point[0] + lattice.fivedeew.point[1];
x.point[1] = lattice.fivedeex.point[1] + lattice.fivedeex.point[2];
y.point[1] = lattice.fivedeey.point[1] + lattice.fivedeey.point[2];
z.point[1] = lattice.fivedeez.point[1] + lattice.fivedeez.point[2];
initial_w.point[0] = lattice.fivedeew.point[1] + lattice.fivedeew.point[2];
x.point[2] = lattice.fivedeex.point[2] + lattice.fivedeex.point[0];
y.point[2] = lattice.fivedeey.point[2] + lattice.fivedeey.point[0];
z.point[2] = lattice.fivedeez.point[2] + lattice.fivedeez.point[0];
initial_w.point[0] = lattice.fivedeew.point[2] + lattice.fivedeew.point[0];
println("Generating lattice to classify blocks as if they were chunks...");
//Quasicrystal subblock_lattice = new Quasicrystal(initial_w.times(chunk_ratio), x.times(chunk_ratio), y.times(chunk_ratio), z.times(chunk_ratio), searchradius*(chunk_ratio));
Quasicrystal subblock_lattice = new Quasicrystal(initial_w, x, y, z, searchradius);*/
// Scale everything in chunk_lattice back up
//println(chunk_lattice.fivedee0.minus(lattice.fivedee0).point);
//println(chunk_lattice.fivedee1.minus(lattice.fivedee1).point);
//println(chunk_lattice.fivedee2.minus(lattice.fivedee2).point);
Point6D test = chunk_lattice.fivedee0.copy();
test.point = Matrix.Multiply(Matrix.array(new float[][] {test.point}), im).array[0];
chunk_lattice.fivedee0.set(test);
test = chunk_lattice.fivedee1.copy();
test.point = Matrix.Multiply(Matrix.array(new float[][] {test.point}), im).array[0];
chunk_lattice.fivedee1.set(test);
test = chunk_lattice.fivedee2.copy();
test.point = Matrix.Multiply(Matrix.array(new float[][] {test.point}), im).array[0];
chunk_lattice.fivedee2.set(test);
println();
println(chunk_lattice.fivedee0.minus(lattice.fivedee0).point);
println(chunk_lattice.fivedee1.minus(lattice.fivedee1).point);
println(chunk_lattice.fivedee2.minus(lattice.fivedee2).point);
chunk_lattice.fivedee0.set(lattice.fivedee0);
chunk_lattice.fivedee1.set(lattice.fivedee1);
chunk_lattice.fivedee2.set(lattice.fivedee2);
chunk_lattice.fivedeex.set(lattice.fivedeex);
chunk_lattice.fivedeey.set(lattice.fivedeey);
chunk_lattice.fivedeez.set(lattice.fivedeez);
chunk_lattice.fivedeew.set(lattice.fivedeew);
for (Object o : chunk_lattice.rhombs) {
Rhomb rhomb = (Rhomb) o;
Point6D rc = rhomb.center.copy();
rc.point = Matrix.Multiply(Matrix.array(new float[][] {rc.point}), im).array[0];
rhomb.center.set(rc);
}
for (Block b : (Iterable<Block>)(chunk_lattice.blocks)) {
Point6D bc = b.center.copy();
bc.point = Matrix.Multiply(Matrix.array(new float[][] {bc.point}), im).array[0];
b.center.set(bc);
}
for (Vertex v : (Iterable<Vertex>)(chunk_lattice.cells)) {
Point6D vv = v.copy();
vv.point = Matrix.Multiply(Matrix.array(new float[][] {vv.point}), im).array[0];
v.set(vv);
}
/*chunk_lattice.fivedeex.set(chunk_lattice.fivedeex.times(chunk_ratio));
chunk_lattice.fivedeey.set(chunk_lattice.fivedeey.times(chunk_ratio));
chunk_lattice.fivedeez.set(chunk_lattice.fivedeez.times(chunk_ratio));
chunk_lattice.fivedeew.set(chunk_lattice.fivedeew.times(chunk_ratio));
// TODO Why do I have to cast here? Fix it so that I don't
for (Object o : chunk_lattice.rhombs) {
Rhomb rhomb = (Rhomb)o;
rhomb.center.set(rhomb.center.times(chunk_ratio));
//rhomb.corner1.set(rhomb.corner1.times(chunk_ratio));
//rhomb.corner2.set(rhomb.corner2.times(chunk_ratio));
//rhomb.corner3.set(rhomb.corner3.times(chunk_ratio));
//rhomb.corner4.set(rhomb.corner4.times(chunk_ratio));
}
for (Block b : (Iterable<Block>)(chunk_lattice.blocks)) {
b.center.set(b.center.times(chunk_ratio));
}
for (Vertex v : (Iterable<Vertex>)(chunk_lattice.cells)) {
v.set(v.times(chunk_ratio));
}*/
// And everything in subblock_lattice down
/*subblock_lattice.fivedee0.set(lattice.fivedeex);
subblock_lattice.fivedee1.set(lattice.fivedeey);
subblock_lattice.fivedee2.set(lattice.fivedeez);
subblock_lattice.fivedeex.set(lattice.fivedeex);
subblock_lattice.fivedeey.set(lattice.fivedeey);
subblock_lattice.fivedeez.set(lattice.fivedeez);
subblock_lattice.fivedeew.set(lattice.fivedeew);
for (Object o : subblock_lattice.rhombs) {
Rhomb rhomb = (Rhomb) o;
Point6D rc = rhomb.center.copy();
rc.point[0] = 0.5*rc.point[0]+-0.5*rc.point[1]+0.5*rc.point[2];
rc.point[1] = 0.5*rc.point[0]+0.5*rc.point[1]+-0.5*rc.point[2];
rc.point[2] = -0.5*rc.point[0]+0.5*rc.point[1]+0.5*rc.point[2];
rhomb.center.set(rc);
}*/
/*subblock_lattice.fivedeex.set(subblock_lattice.fivedeex.times(1.0/chunk_ratio));
subblock_lattice.fivedeey.set(subblock_lattice.fivedeey.times(1.0/chunk_ratio));
subblock_lattice.fivedeez.set(subblock_lattice.fivedeez.times(1.0/chunk_ratio));
subblock_lattice.fivedeew.set(subblock_lattice.fivedeew.times(1.0/chunk_ratio));
// TODO Here, I'm changing the coordinates of something inside RhombStore.
// This makes RhombStore make mistakes later. Ought to create a legit way
// to do this.
for (Object o : subblock_lattice.rhombs) {
Rhomb rhomb = (Rhomb)o;
rhomb.center.set(rhomb.center.times(1.0/chunk_ratio));
//rhomb.corner1.set(rhomb.corner1.times(1.0/chunk_ratio));
//rhomb.corner2.set(rhomb.corner2.times(1.0/chunk_ratio));
//rhomb.corner3.set(rhomb.corner3.times(1.0/chunk_ratio));
//rhomb.corner4.set(rhomb.corner4.times(1.0/chunk_ratio));
}
for (Block b : (Iterable<Block>)(subblock_lattice.blocks)) {
b.center.set(b.center.times(1.0/chunk_ratio));
}
for (Vertex v : (Iterable<Vertex>)(subblock_lattice.cells)) {
v.set(v.times(1.0/chunk_ratio));
}//*/
main_lattice = lattice;//subblock_lattice;
main_chunk_lattice = chunk_lattice;//lattice;
if (skip_classif) {
return;
}
// Not the right place for this, but gotta get 3D coordinates for the chunk lattice.
for (Object o : chunk_lattice.cells) {
Vertex v = (Vertex)(o);
v.location_3D = new PVector(v.minus(chunk_lattice.fivedeew).dot(chunk_lattice.fivedee0), v.minus(chunk_lattice.fivedeew).dot(chunk_lattice.fivedee1), v.minus(chunk_lattice.fivedeew).dot(chunk_lattice.fivedee2));
}
for (Object o : chunk_lattice.rhombs) {
Rhomb r = (Rhomb)(o);
r.center_3D = new PVector(r.center.minus(chunk_lattice.fivedeew).dot(chunk_lattice.fivedee0), r.center.minus(chunk_lattice.fivedeew).dot(chunk_lattice.fivedee1), r.center.minus(chunk_lattice.fivedeew).dot(chunk_lattice.fivedee2));
}
//println(str(chunk_lattice.blocks.list.size())+" chunks, "+str(lattice.blocks.list.size())+" blocks, "+str(subblock_lattice.blocks.list.size())+" sub-blocks found.");
println("Classifying chunks...");
ArrayList<Chunk> classif1 = classifyChunks3D(chunk_lattice, lattice);
println(str(classif1.size())+" unique chunks found.");
println("Classifying blocks as if they were chunks...");
//ArrayList<Chunk> classif2 = classifyChunks3D(lattice, subblock_lattice);
//println(str(classif2.size())+" unique blocks found.");
for (Chunk c : classif1) {
if (c.instances.size() > 1) {
for (Chunk ci : c.instances) {
// For each instance, we want to print what chunk types its blocks are.
String types = str(classif1.indexOf(c));
int block_count = 0;
for (Block b : ci.blocks) {
/*for (Chunk s : classif2) {
for (Chunk si : s.instances) {
if (max(si.center.minus(b.center).abs().point) < 0.01) {
types = types+" "+classif2.indexOf(s);
block_count += 1;
}
}
}*/
}
if (block_count > 0)
println(types);
}
}
}
}
}
ArrayList<Chunk> classifyChunks(Quasicrystal chunk_lattice, Quasicrystal lattice) {
ArrayList<Chunk> decorated_chunks = new ArrayList<Chunk>();
int progress_count = 0;
float last_progress_report = 0;
for (Block chunk : (Iterable<Block>)(chunk_lattice.blocks)) {
progress_count++;
if (float(progress_count)/chunk_lattice.blocks.list.size()-last_progress_report > 0.105) {
println("Decorating: "+100*float(progress_count)/chunk_lattice.blocks.list.size()+"% complete");
last_progress_report = float(progress_count)/chunk_lattice.blocks.list.size();
}
// TODO This loop is apparently the slow part
boolean skipchunk = false;
PointStore decorations = new PointStore(lattice.tolerance);
//ArrayList<Point6D> corners = new ArrayList<Point6D>();
VertexStore corner_store = new VertexStore(lattice.tolerance);
Chunk decorated_chunk = new Chunk(chunk.center.copy());
for (Rhomb face : chunk.sides) {
corner_store.add(face.corner1);
corner_store.add(face.corner2);
corner_store.add(face.corner3);
corner_store.add(face.corner4);
}
// Collected 6D corners of chunk. Now we have bounds w/in which to collect corners of blocks.
Point6D minp = corner_store.list.get(0).copy();
Point6D maxp = corner_store.list.get(0).copy();
for (Point6D c : corner_store) {
for (int i = 0; i < 6; i++) {
if (minp.point[i] > c.point[i]) minp.point[i] = c.point[i];
if (maxp.point[i] < c.point[i]) maxp.point[i] = c.point[i];
}
}
for (Block block : (Iterable<Block>)(lattice.blocks)) {
// Is the block anywhere near the chunk?
// TODO Measure whether this is helping or hurting. Do it better?
// Maybe manhatten distance is actually more relevant?
float dist = 0;//block.center.minus(chunk.center).length();
if (dist < chunk_ratio*1.5*sqrt(6)) {// TODO is this cutoff ok?
PointStore iterate = new PointStore(lattice.tolerance);
//iterate.add(block.center);
boolean maybe_skipchunk = false;
// Let's decorate with corners too. It's okay that we'll repeat them.
for (Rhomb r : block.sides) {
if (r.parents.size() < 2) {
// This block is too close to the edge of space.
// We need to skip the chunk if it turns out to be
// inside it.
maybe_skipchunk = true;
} else if (r.parents.size() > 2) {
assert 0 == 1 :
"Rhombus with too many parents";
}
iterate.add(r.corner1);
iterate.add(r.corner2);
iterate.add(r.corner3);
iterate.add(r.corner4);
iterate.add(r.center);
}
if (skipchunk) break;
boolean addblock = false;
for (Point6D p : (Iterable<Point6D>)(iterate)) {
boolean inside_chunk = true;
for (int i = 0; i < 6; i++) {
if (p.point[i] - minp.point[i] < -lattice.tolerance || p.point[i] - maxp.point[i] > lattice.tolerance) {
inside_chunk = false;
break;
}
}
if (inside_chunk) {
decorations.add(p.copy());
// Also add block's center, even though it may be outside chunk
// TODO Adding this many decorations seems to greatly slow things down. maybe.
// Fix?
decorations.add(block.center.copy());
addblock = true;
if (maybe_skipchunk) {
skipchunk = true;
break;
}
}
}
if (addblock) decorated_chunk.blocks.add(block);
/*for (int i = 0; i < decorations.size() - 1; i++) {
for (int j = i+1; j < decorations.size(); j++) {
if (max(decorations.get(i).minus(decorations.get(j)).abs().point) < lattice.tolerance) {
decorations.remove(j);
j--;
}
}
}*/
}
}
if (skipchunk) continue;
// now subtract minp from everything
ArrayList<Point6D> corners = new ArrayList<Point6D>();
for (Point6D p : corner_store) corners.add(p);
for (int i = 0; i < corners.size(); i++) corners.set(i, corners.get(i).minus(minp));
for (int i = 0; i < decorations.list.size(); i++) decorations.list.set(i, decorations.list.get(i).minus(minp));
for (Point6D p : decorations.list) {
decorated_chunk.block_centers.add(p);
}
decorated_chunk.corners = corners;
decorated_chunks.add(decorated_chunk);
}
println("Decoration stage completed. Classifying "+decorated_chunks.size()+" chunks.");
ArrayList<Chunk> unique_chunks = new ArrayList<Chunk>();
FakeChunkStore chunk_hash = new FakeChunkStore();
for (Chunk c : decorated_chunks) {
boolean found = false;
ArrayList<Chunk> hash_matches = chunk_hash.getAllSimilar(c);
for (Chunk uc : hash_matches) {
if (c.block_centers.size() == uc.block_centers.size()) {
boolean corners_same = true;
for (Point6D corner : c.corners) {
boolean found_corner = false;
for (Point6D uqcorner : uc.corners) {
if (max(corner.minus(uqcorner).point)<lattice.tolerance) {
found_corner = true;
break;
}
}
if (!found_corner) {
corners_same = false;
break;
}
}
if (corners_same) {
boolean decs_same = true;
for (Point6D dec : (Iterable<Point6D>)(c.block_centers)) {
boolean dec_found = uc.block_centers.contains(dec);
/*for (Point6D uqdec : uc.block_centers) {
if (max(dec.minus(uqdec).point)<lattice.tolerance) {
dec_found = true;
break;
}
}*/
if (!dec_found) {
decs_same = false;
break;
}
}
if (decs_same) {
found = true;
uc.instances.add(c);
break;
}
}
}
}
if (!found) {
c.instances.add(c);
unique_chunks.add(c);
chunk_hash.add(c);
}
}
return unique_chunks;
}
ArrayList<Chunk> classifyChunks3D(Quasicrystal chunk_lattice, Quasicrystal lattice) {
if (!playSetupDone) setupRender();
ArrayList<Chunk> decorated_chunks = new ArrayList<Chunk>();
int progress_count = 0;
float last_progress_report = 0;
for (Block chunk : (Iterable<Block>)(chunk_lattice.blocks)) {
progress_count++;
if (float(progress_count)/chunk_lattice.blocks.list.size()-last_progress_report > 0.105) {
println("Decorating: "+100*float(progress_count)/chunk_lattice.blocks.list.size()+"% complete");
last_progress_report = float(progress_count)/chunk_lattice.blocks.list.size();
}
// TODO This loop is apparently the slow part
boolean skipchunk = false;
PointStore decorations = new PointStore(lattice.tolerance);
//ArrayList<Point6D> corners = new ArrayList<Point6D>();
VertexStore corner_store = new VertexStore(lattice.tolerance);
Chunk decorated_chunk = new Chunk(chunk.center.copy());
for (Rhomb face : chunk.sides) {
corner_store.add(threedee(face.corner1.location_3D));
corner_store.add(threedee(face.corner2.location_3D));
corner_store.add(threedee(face.corner3.location_3D));
corner_store.add(threedee(face.corner4.location_3D));
}
// Collected 6D corners of chunk. Now we have bounds w/in which to collect corners of blocks.
Point6D minp = corner_store.list.get(0).copy();
Point6D maxp = corner_store.list.get(0).copy();
for (Point6D c : corner_store) {
for (int i = 0; i < 6; i++) {
if (minp.point[i] > c.point[i]) minp.point[i] = c.point[i];
if (maxp.point[i] < c.point[i]) maxp.point[i] = c.point[i];
}
}
for (Block block : (Iterable<Block>)(lattice.blocks)) {
// Is the block anywhere near the chunk?
// TODO Measure whether this is helping or hurting. Do it better?
// Maybe manhatten distance is actually more relevant?
float dist = 0;//block.center.minus(chunk.center).length();
if (dist < chunk_ratio*1.5*sqrt(6)) {// TODO is this cutoff ok?
PointStore iterate = new PointStore(lattice.tolerance);
//iterate.add(block.center);
boolean maybe_skipchunk = false;
// Let's decorate with corners too. It's okay that we'll repeat them.
for (Rhomb r : block.sides) {
if (r.parents.size() < 2) {
// This block is too close to the edge of space.
// We need to skip the chunk if it turns out to be
// inside it.
maybe_skipchunk = true;
} else if (r.parents.size() > 2) {
assert 0 == 1 :
"Rhombus with too many parents";
}
iterate.add(threedee(r.corner1.location_3D));
iterate.add(threedee(r.corner2.location_3D));
iterate.add(threedee(r.corner3.location_3D));
iterate.add(threedee(r.corner4.location_3D));
iterate.add(threedee(r.center_3D));
}
if (skipchunk) break;
boolean addblock = false;
for (Point6D p : (Iterable<Point6D>)(iterate)) {
boolean inside_chunk = true;
for (int i = 0; i < 3; i++) {
if (p.point[i] - minp.point[i] < -lattice.tolerance || p.point[i] - maxp.point[i] > lattice.tolerance) {
inside_chunk = false;
break;
}
}
if (inside_chunk) {
decorations.add(p.copy());
// Also add block's center, even though it may be outside chunk
// TODO Adding this many decorations seems to greatly slow things down. maybe.
// Fix?
//decorations.add(block.center.copy());
addblock = true;
if (maybe_skipchunk) {
skipchunk = true;
break;
}
}
}
if (addblock) decorated_chunk.blocks.add(block);
/*for (int i = 0; i < decorations.size() - 1; i++) {
for (int j = i+1; j < decorations.size(); j++) {
if (max(decorations.get(i).minus(decorations.get(j)).abs().point) < lattice.tolerance) {
decorations.remove(j);
j--;
}
}
}*/
}
}
if (skipchunk) continue;
// now subtract minp from everything
ArrayList<Point6D> corners = new ArrayList<Point6D>();
for (Point6D p : corner_store) corners.add(p);
for (int i = 0; i < corners.size(); i++) corners.set(i, corners.get(i).minus(minp));
for (int i = 0; i < decorations.list.size(); i++) decorations.list.set(i, decorations.list.get(i).minus(minp));
for (Point6D p : decorations.list) {
decorated_chunk.block_centers.add(p);
}
decorated_chunk.corners = corners;
decorated_chunks.add(decorated_chunk);
}
println("Decoration stage completed. Classifying "+decorated_chunks.size()+" chunks.");
ArrayList<Chunk> unique_chunks = new ArrayList<Chunk>();
FakeChunkStore chunk_hash = new FakeChunkStore();
for (Chunk c : decorated_chunks) {
boolean found = false;
ArrayList<Chunk> hash_matches = chunk_hash.getAllSimilar(c);
for (Chunk uc : hash_matches) {
if (c.block_centers.size() == uc.block_centers.size()) {
boolean corners_same = true;
for (Point6D corner : c.corners) {
boolean found_corner = false;
for (Point6D uqcorner : uc.corners) {
if (max(corner.minus(uqcorner).point)<lattice.tolerance) {
found_corner = true;
break;
}
}
if (!found_corner) {
corners_same = false;
break;
}
}
if (corners_same) {
boolean decs_same = true;
for (Point6D dec : (Iterable<Point6D>)(c.block_centers)) {
boolean dec_found = uc.block_centers.contains(dec);
/*for (Point6D uqdec : uc.block_centers) {
if (max(dec.minus(uqdec).point)<lattice.tolerance) {
dec_found = true;
break;
}
}*/
if (!dec_found) {
decs_same = false;
break;
}
}
if (decs_same) {
found = true;
uc.instances.add(c);
break;
}
}
}
}
if (!found) {
c.instances.add(c);
unique_chunks.add(c);
chunk_hash.add(c);
}
}
return unique_chunks;
}
// Cheesy conversion function
Vertex threedee(PVector p) {
Vertex v = new Vertex(p.x, p.y, p.z, 0, 0, 0);
v.location_3D = p;
return v;
}
class Chunk extends Block {
int level;
int type;
PointStore block_centers;
ArrayList<Block> blocks;
ArrayList<Chunk> instances;
ArrayList<Point6D> corners;
public Chunk(Point6D p) {
super(p);
level = 0;
block_centers = new PointStore(0.01);
corners = new ArrayList<Point6D>();
blocks = new ArrayList<Block>();
instances = new ArrayList<Chunk>();