-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
solver.html
2010 lines (1867 loc) · 75.9 KB
/
solver.html
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
<!DOCTYPE html>
<html>
<head>
<title>TC Research Solver</title>
<!--Thaumcraft research minigame solver using heuristics and bruteforce-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="icon" href="./ressources/images/favicon.png">
<link rel="stylesheet" type="text/css" href="./ressources/css/mainStylesheet.css"/>
<link rel="stylesheet" type="text/css" href="./ressources/css/solver.css"/>
</head>
<body onload="load_aspect_selector()">
<div id="wrapper">
<div class="article">
<h1>Thaumcraft 4 Research Solver</h1>
<br>
<div id="canvas_selector_wrapper">
<canvas id='canvas' onclick="handle_click(event)"></canvas>
<div id="aspect_selector_modpack_wrapper">
<div id="aspect_selector_gui"></div>
<br>
<div id="search_modpack_wrapper">
<div id="search_wrapper">
<label for="search_box">Search: </label>
<input id="search_box" type="text" name="search box" oninput="search(this)">
</div>
<br>
<div id="modpack_selector">
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="vanilla_checkbox" type="checkbox" name="Vanilla TC" value="vanilla_tc">
<label for="vanilla_checkbox">Vanilla TC</label>
</div>
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="forbidden_magic_checkbox" type="checkbox" name="Forbidden Magic" value="forbidden_magic">
<label for="forbidden_magic_checkbox">Forbidden Magic</label>
</div>
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="gregtech_checkbox" type="checkbox" name="Gregtech" value="gregtech">
<label for="gregtech_checkbox">Gregtech</label>
</div>
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="magic_bees_checkbox" type="checkbox" name="Magic Bees" value="magic_bees">
<label for="magic_bees_checkbox">Magic Bees</label>
</div>
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="avaritia_checkbox" type="checkbox" name="Avaritia" value="avaritia">
<label for="avaritia_checkbox">Avaritia</label>
</div>
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="abyssal_checkbox" type="checkbox" name="AbyssalCraft" value="abyssal">
<label for="abyssal_checkbox">AbyssalCraft</label>
</div>
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="botanical_checkbox" type="checkbox" name="Botanical Addons" value="botanical">
<label for="botanical_checkbox">Botanical Addons</label>
</div>
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="elysium_checkbox" type="checkbox" name="The Elysium" value="elysium">
<label for="elysium_checkbox">The Elysium</label>
</div>
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="revelations_checkbox" type="checkbox" name="Thaumic Revelations" value="revelations">
<label for="revelations_checkbox">Thaumic Revelations</label>
</div>
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="additions_checkbox" type="checkbox" name="Thaumic Additions" value="additions">
<label for="additions_checkbox">Thaumic Additions</label>
</div>
<div class="input_wrapper" onclick="toggle_aspect_set(this)">
<input id="essential_checkbox" type="checkbox" name="Essential Thaumaturgy" value="essential">
<label for="essential_checkbox">Essential Thaumaturgy</label>
</div>
</div>
</div>
</div>
</div>
<h2 id="wait_solving"></h2>
<button id="barring_button" class="myButton" onclick='mouse_barring_mode()'>Barring mode (bar the cells that aren't available)</button>
<br>
<button class="myButton" onclick="solve_wrapper()">Solve</button>
<br>
<button class="myButton" onclick="reset()">Reset</button>
<br>
<button class="myButton" onclick="preset()">Preset (demo)</button>
<br><br>
<form onsubmit="return false;">
<label for="grid_size_input">Grid size: </label>
<input class="css-input" type="number" name="Grid size" onchange="resize_grid()" id="grid_size_input" value="5">
</form>
<h2 id="loading">Please wait, loading aspect images...</h2>
<div id="gui_aspect_selector"></div>
<br>
<h2>How do I use it?</h2>
<ul>
<li>Drag aspects on the grid to recreate a research note. Drag the images, not the box or text.</li>
<li>You can also select a cell and click an aspect image to set it as the cell aspect.</li>
<li>You can remove aspects from the grid by simply clicking on them.</li>
<li>Use the "Barring mode" button to remove any tiles that can not be used.</li>
<li>Toggle the availabilty of an aspect by clicking its text.</li>
<li>Even if an aspect can't be used in research, it still can be dragged onto the grid.</li>
<li>Click "Solve" and wait. A backtracing algorithm will make your poor CPU work.</li>
<li>If you feel like it takes too long, or some kind of error message pops up <strong><i>on the site, not a browser popup (!)</i></strong>, congratulations! You probably discovered a bug! Report your initial aspect setup here: <a href="https://github.com/jackowski626/universal_tc_research_solver/issues" target="_blank">github.com/jackowski626/universal_tc_research_solver/issues</a></li>
</ul>
<h2>Where can I send my precious suggestions?</h2>
<p>Do it here: <a href="https://github.com/jackowski626/universal_tc_research_solver/issues" target="_blank">github.com/jackowski626/universal_tc_research_solver/issues</a></p>
<h2>What is your favorite part?</h2>
<p>Stardust Crusaders.</p>
<br>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick" />
<input type="hidden" name="hosted_button_id" value="SJY37JP2UFB6S" />
<input type="image" src="https://www.paypalobjects.com/en_US/CH/i/btn/btn_donateCC_LG.gif" border="0" name="submit" title="PayPal - The safer, easier way to pay online!" alt="Donate with PayPal button" />
<img alt="" border="0" src="https://www.paypal.com/en_CH/i/scr/pixel.gif" width="1" height="1" />
</form>
<br>
</div>
</div>
</body>
<script>
var debugging = false;
var preset_num = 6;
var holding_aspect = false;
var held_aspect = 'none';
var last_highlighted_id = null;
var body_color = '#1C1D1F';
/* detect click outside of canvas */
document.addEventListener('click', function(event) {
var isClickInside = document.getElementById('canvas').contains(event.target);// || document.getElementById('aspect_selector').contains(event.target);
if (!isClickInside && selected_cell != undefined) {
selected_cell.selected = false;
selected_cell = undefined;
draw_grid(grid);
}
});
var image_cache = {};
/* util functions, going into separate file later */
function get_mouse_pos_canvas(canvas, evt) {
let rect = canvas.getBoundingClientRect();
return {
x: (evt.clientX - rect.left) / (rect.right - rect.left) * canvas.width,
y: (evt.clientY - rect.top) / (rect.bottom - rect.top) * canvas.height
}
}
/* degrees to radians */
function to_rad(deg) {
return deg * Math.PI / 180;
}
/* distance between two points */
function get_distance(x1, y1, x2, y2) {
return Math.sqrt(Math.abs(x1 - x2) * Math.abs(x1 - x2) + Math.abs(y1 - y2) * Math.abs(y1 - y2));
}
/* object handling */
function keylen(obj) {
return Object.keys(obj).length;
}
function keyAtIndex(obj, index) {
return Object.keys(obj)[index];
}
function elemAtIndex(obj, index) {
return obj[Object.keys(obj)[index]];
}
function indexOfElem(obj, elem) {
for (let i = 0; i < keylen(obj); i++) {
if (keyAtIndex(obj, i) == elem) {
return i;
}
}
}
function obj_expr_xnor(object, expression) {
if (object === 'undefined') {
return true;
} else {
return expression;
}
}
function capitalize(string) {
return string[0].toUpperCase() + string.substring(1);
}
function communize(string) {
return string[0].toLowerCase() + string.substring(1);
}
function text_width(text, font) {
let text_canvas = document.createElement('canvas');
let text_ctx = text_canvas.getContext('2d');
text_ctx.font = font;
let text_width = text_ctx.measureText(text).width;
text_canvas.remove();
return text_width;
}
var aspect_flavor_dict = {
'aer':'air',
'alienis':'eldritch',
'aqua':'water',
'arbor':'tree',
'auram':'aura',
'bestia':'beast',
'cognitio':'mind',
'corpus':'flesh',
'exanimis':'undead',
'fabrico':'craft',
'fames':'hunger',
'gelum':'cold',
'herba':'plant',
'humanus':'man',
'ignis':'fire',
'instrumentum':'tool',
'iter':'travel',
'limus':'slime',
'lucrum':'desire',
'lux':'light',
'machina':'mechanism',
'messis':'crop',
'metallum':'metal',
'meto':'harvest',
'mortuus':'death',
'motus':'motion',
'ordo':'order',
'pannus':'cloth',
'perditio':'entropy',
'perfodio':'mine',
'permutatio':'exchange',
'potentia':'energy',
'praecantatio':'magic',
'sano':'health',
'sensus':'senses',
'spiritus':'soul',
'telum':'weapon',
'tempestas':'weather',
'tenebrae':'darkness',
'terra':'earth',
'tutamen':'armor',
'vacuos':'void',
'venenum':'poison',
'victus':'life',
'vinculum':'trap',
'vitium':'taint',
'vitreus':'crystal',
'volatus':'flight',
'desidia':'sloth',
'gula':'gluttony',
'infernus':'nether',
'invidia':'envy',
'ira':'wrath',
'luxuria':'lust',
'superbia':'pride',
'tempus':'time',
'electrum':'electricity',
'magneto':'magnetism',
'nebrisum':'cheatiness',
'radio':'radioactivity',
'strontio':'stupidity',
'terminus':'apocalypse',
'coralos':'coralium',
'dreadia':'dread',
'tincturem':'color',
'sanctus':'holiness',
'exubitor':'warden',
'saxum':'stone',
'granum':'seed',
'mru':'magical radiation unit',
'radiation':'radiation',
'matrix':'protection'
}
var all_aspect_flavor_dict = {};
for (let i = 0; i < keylen(aspect_flavor_dict); i++) {
all_aspect_flavor_dict[keyAtIndex(aspect_flavor_dict, i)] = elemAtIndex(aspect_flavor_dict, i);
}
var enabled_flavor_dict = {};
for (let i = 0; i < keylen(aspect_flavor_dict); i++) {
enabled_flavor_dict[keyAtIndex(aspect_flavor_dict, i)] = true;
}
function load_aspect_selector(set_def_aspect_set = true) {
let aspect_selector_gui = document.getElementById('aspect_selector_gui');
aspect_selector_gui.innerHTML = '';
for (let i = 0; i < keylen(aspect_flavor_dict); i++) {
let onload = '';
if (i == keylen(aspect_flavor_dict) - 1) {
onload = set_def_aspect_set ? ' onload = "on_resize(); set_default_aspect_set();"' : ' onload = "on_resize();"';
}
let style = '';
if (!enabled_flavor_dict[keyAtIndex(aspect_flavor_dict, i)]) {
style = ' style="filter: brightness(25%);"';
}
let child = '<div id="aspect_content_wrapper_' + i + '" class="aspect_content_wrapper"><img' + style + onload + ' id="aspect_image_' + i + '" src="./ressources/images/hq_images_white_all/' + keyAtIndex(aspect_flavor_dict, i) + '.png"><div class="aspect_text_wrapper" id="aspect_text_' + i + '" onclick = "toggle_availability(' + i + ')"><span id="text_' + i + '">' + capitalize(keyAtIndex(aspect_flavor_dict, i)) + '<br></span><span id="flavor_' + i + '">' + elemAtIndex(aspect_flavor_dict, i) + '</span></div></div>';
aspect_selector_gui.innerHTML += child;
}
//on_resize();
}
document.addEventListener('mousedown', function(event) {
if (event.target.id.startsWith('aspect_image_')) {
event.preventDefault();
//console.log('clicked ' + keyAtIndex(aspect_flavor_dict, event.target.id.substring(13)));
document.body.style.cursor /*document.getElementById('aspect_image_0').style.cursor*/ = "url('./ressources/images/cursors/" + keyAtIndex(aspect_flavor_dict, event.target.id.substring(13)) + '.png' + "') 15 15, pointer";
//console.log(document.body.style.cursor);
holding_aspect = true;
held_aspect = keyAtIndex(aspect_flavor_dict, event.target.id.substring(13));
//console.log('<img id="aspect_cursor" src="./hq_images_black_all/' + keyAtIndex(aspect_flavor_dict, event.target.id.substring(13)) + '">');
/*let aspect_cursor = '<img id="aspect_cursor" style="width: ' + Math.trunc(current_state.grid[0][0].side_len) * 2 + 'px;" src="./hq_images_black_all/' + keyAtIndex(aspect_flavor_dict, event.target.id.substring(13)) + '.png">';
document.getElementById('cursor_placeholder').innerHTML = aspect_cursor;*/
}
});
document.addEventListener('mouseup', function(event) {
if (held_aspect != 'none' && last_highlighted_id != null) {
//console.log('last_highlighted_id: ' + last_highlighted_id);
let last_highlighted_cell = get_cell_by_id(last_highlighted_id, current_state.grid);
last_highlighted_cell.highlighted = false;
last_highlighted_cell.aspect = held_aspect;
update_cell(current_state.grid, last_highlighted_cell.array_x, last_highlighted_cell.array_y);
}
document.body.style.cursor = '';
holding_aspect = false;
held_aspect = 'none';
});
document.addEventListener('mousemove', function(event) {
let mouse_in_cell = false;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
//console.debug(grid[i][j].y);
//console.debug(`acceptable distance: ${grid[i][j].side_len}`);
let mouse_x = get_mouse_pos_canvas(canvas, event).x;//event.clientX;
let mouse_y = get_mouse_pos_canvas(canvas, event).y;//event.clientY;
//console.log(get_mouse_pos_canvas(canvas, event).x);
let distance = Math.sqrt(Math.pow(Math.abs(grid[i][j].x - mouse_x), 2) + Math.pow(Math.abs(grid[i][j].y - mouse_y), 2));
if (distance <= grid[i][j].side_len) {
if (i == 0 && j == 0) {
//console.log('hmmmm');
}
mouse_in_cell = true;
current_state.grid[i][j].highlighted = true;
update_cell(current_state.grid, i, j);
if (last_highlighted_id == null) {
last_highlighted_id = current_state.grid[i][j].id;
//console.log('0 last highlighted cell: ' + last_highlighted_id);
}
if (current_state.grid[i][j].id != last_highlighted_id) {
let last_highlighted_cell = get_cell_by_id(last_highlighted_id, current_state.grid);
last_highlighted_cell.highlighted = false;
update_cell(current_state.grid, last_highlighted_cell.array_x, last_highlighted_cell.array_y);
last_highlighted_id = current_state.grid[i][j].id;
//console.log('1 last highlighted cell: ' + last_highlighted_id);
}
}
}
}
//console.log('mouse in cell: ' + mouse_in_cell);
if (!mouse_in_cell && last_highlighted_id != null) {
//console.log('out of canvas');
let last_highlighted_cell = get_cell_by_id(last_highlighted_id, current_state.grid);
last_highlighted_cell.highlighted = false;
update_cell(current_state.grid, last_highlighted_cell.array_x, last_highlighted_cell.array_y);
last_highlighted_id = null;
}
});
document.addEventListener('mousedown', function(event) {
if (event.target.id.startsWith('aspect_image_') && selected_cell != null) {
//console.log(event.target);
let aspect = communize(document.getElementById('text_'+event.target.id.slice(13)).innerText).replace(/(\r\n|\n|\r)/gm, "");
//console.log(aspect)
selected_cell.aspect = aspect;
}
});
function on_resize() {
for (let i = 0; i < keylen(aspect_flavor_dict); i++) {
let text = document.getElementById('text_' + i);
let flavor = document.getElementById('flavor_' + i);
let image = document.getElementById('aspect_image_' + i);
flavor.style.fontSize = "0.7em";//document.getElementById("aspect_text_" + i).offsetWidth / flavor.offsetWidth * 20 + 'px';
text.style.fontSize = "1em";//document.getElementById("aspect_text_" + i).offsetWidth / text.offsetWidth * 30 + 'px';
//while (/*text_width(text.innerText, text.style.fontSize + ' Helvetica Neue, Helvetica')*/text.offsetWidth + image.offsetWidth > document.getElementById('aspect_content_wrapper_' + i).offsetWidth - 5 && text.style.fontSize != '0.1em') {
while (text.offsetWidth + image.offsetWidth > document.getElementById('aspect_content_wrapper_' + i).offsetWidth / 1.1 && text.style.fontSize != '0.1em') {
text.style.fontSize = text.style.fontSize.substring(0, 2)[1] == 'e' ? (parseFloat(text.style.fontSize.substring(0, 1)) - 0.1).toString() + 'em' : (parseFloat(text.style.fontSize.substring(0, 3)) - 0.1).toString() + 'em';
}
while (flavor.offsetWidth + image.offsetWidth > document.getElementById('aspect_content_wrapper_' + i).offsetWidth / 1.1 && flavor.style.fontSize != '0.1em') {
flavor.style.fontSize = flavor.style.fontSize.substring(0, 2)[1] == 'e' ? (parseFloat(flavor.style.fontSize.substring(0, 1)) - 0.1).toString() + 'em' : (parseFloat(flavor.style.fontSize.substring(0, 3)) - 0.1).toString() + 'em';
}
}
}
window.onresize = on_resize;
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
function white_canvas() {
ctx.fillStyle = body_color;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
class Cell {
constructor(x, y, aspect, barred, side_len, id, array_x, array_y) {
this.x = x;
this.y = y;
this.aspect = aspect;
this.barred = barred;
this.side_len = side_len;
this.selected = false;
this.highlighted = false;
this.img;
this.base = false;
this.id = id;
this.parent_id;
this.array_x = array_x;
this.array_y = array_y;
this.path_child;
//this.path_parent;
}
}
var distance_dict = {};
class State {
constructor(grid) {
this.grid = grid;
this.impossible_closest_cells = {};
this.impossible_aspects_for_current_path = {};
this.impossible_last_cell_id;
this.last_move = null;
this.last_cell = null;
this.last_cell2 = null;
this.current_path = null;
this.blacklisted_paths = []; /* I swear, I'll update my terminology ASAP, straight from the master branch of the political correctness repo */
this.blacklisted_cells = [];
this.current_path_child_dict = {};
this.current_path_child_blacklist = {};
this.current_path_forbidden_child;
this.bad_state_reason = null;
}
}
var mouse_mode = 'selecting';
var selected_cell;
function mouse_selection_mode() {
mouse_mode = 'selecting';
document.getElementById('selection_button').style.borderColor = '#1da4b8';
document.getElementById('selection_button').style.borderWidth = '5px';
document.getElementById('barring_button').style.borderColor = '#000000';
document.getElementById('barring_button').style.borderWidth = '1px';
}
function mouse_barring_mode() {
if (mouse_mode == 'selecting') {
mouse_mode = 'barring';
if (selected_cell != undefined) {
selected_cell.selected = false;
selected_cell = undefined;
}
document.getElementById('barring_button').style.borderColor = '#ff4242';
document.getElementById('barring_button').style.borderWidth = '5px';
} else {
mouse_mode = 'selecting';
document.getElementById('barring_button').style.borderColor = '#000000';
document.getElementById('barring_button').style.borderWidth = '1px';
}
}
function handle_click(event) {
let mouse_x = get_mouse_pos_canvas(canvas, event).x;//event.clientX;
let mouse_y = get_mouse_pos_canvas(canvas, event).y;//event.clientY;
var coords = "X coords: " + mouse_x + ", Y coords: " + mouse_y;
//console.debug(coords);
var found = false;
/* loop through all cells in grid and find which one is nearest to the click */
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
//console.debug(grid[i][j].y);
//console.debug(`acceptable distance: ${grid[i][j].side_len}`);
let distance = Math.sqrt(Math.pow(Math.abs(grid[i][j].x - mouse_x), 2) + Math.pow(Math.abs(grid[i][j].y - mouse_y), 2));
if (distance <= grid[i][j].side_len) {
found = true;
//console.debug(`found at ${parseFloat(grid[i][j].x).toFixed(2)}, ${parseFloat(grid[i][j].y).toFixed(2)} at distance ${distance}`);
if (mouse_mode == 'barring') {
if (grid[i][j].barred) {
grid[i][j].barred = false;
} else {
grid[i][j].selected = false;
grid[i][j].aspect = 'none';
grid[i][j].barred = true;
}
}
if (mouse_mode == 'selecting') {
if (grid[i][j].selected) {
grid[i][j].selected = false;
} else if (!grid[i][j].barred) {
grid[i][j].selected = true;
if (selected_cell != undefined && selected_cell != grid[i][j]) {
selected_cell.selected = false;
selected_cell = undefined;
}
selected_cell = grid[i][j];
if (selected_cell.aspect != 'none') {
selected_cell.aspect = 'none';
update_cell(grid, selected_cell.array_x, selected_cell.array_y);
selected_cell.selected = false;
selected_cell = undefined;
}
}
}
break;
}
}
}
if (!found && selected_cell != undefined) {
selected_cell.selected = false;
selected_cell = undefined;
}
/*if (selected_cell != undefined && selected_cell.selected) {
document.getElementById('aspect_selector').value = selected_cell.aspect;
}*/
draw_grid(grid);
}
/* Resizing the canvas 31.25 percent of screen width (400 px if the screen is 1280 px wide) */
//alert(screen.width);
//1536
//var screen_percentage = 31.25;
if (screen.width >= 1000) {
canvas.height = screen.width / 100 * 32;
canvas.width = screen.width / 100 * 32;
document.getElementById('aspect_selector_modpack_wrapper').style.height = Math.floor(screen.width / 100 * 32) + 'px';
} else if (screen.width >= 500) {
canvas.height = screen.width / 100 * 50;
canvas.width = screen.width / 100 * 50;
} else if (screen.width < 500) {
canvas.height = screen.width;
canvas.width = screen.width
canvas.style.marginLeft = '-10px';
}
/* drawing default size hex grid. Regular hexagon, "size 4" */
/* the whole grid should always take the maximum space */
/* a place in the grid should be selected on the canvas and then the aspect that is there must be a html panel below where one can search for the aspect and click to place it */
var grid_size = 5;
/* Drawing hexagon at coords */
function draw_hexagon(x, y, side_len, lineWidth = 1, lineColor = '#000000', fillColor = '#FFFFFF', pointing_up = false) {
//console.debug("side_len: " + side_len)
side_len -= Math.ceil(lineWidth / 2) + 1;
var triangle_height = Math.sqrt(Math.pow(side_len, 2) - Math.pow(side_len / 2, 2));
var points;
if (pointing_up) {
points = [[x + triangle_height, y - side_len / 2], [x + triangle_height, y + side_len / 2], [x, y + side_len], [x - triangle_height, y + side_len / 2], [x - triangle_height, y - side_len / 2], [x, y - side_len]];
} else {
points = [[x + side_len / 2, y - triangle_height], [x + side_len, y], [x + side_len / 2, y + triangle_height], [x - side_len / 2, y + triangle_height], [x - side_len, y], [x - side_len / 2, y - triangle_height]];
}
ctx.beginPath();
ctx.lineWidth = lineWidth;
ctx.strokeStyle = lineColor;
ctx.fillStyle = fillColor;
ctx.moveTo(points[5][0], points[5][1]);
for (let i = 0; i < points.length; i++) {
ctx.lineTo(points[i][0], points[i][1]);
}
ctx.fill();
ctx.stroke();
ctx.closePath();
}
function draw_initial_grid(grid_size) {
let hex_side_len = canvas.height / 2 - 1;
//draw_hexagon(canvas.width / 2, canvas.height / 2, hex_side_len, undefined, undefined, undefined, true);
var hex_num_vertical = grid_size * 2 - 1;
var blank_num_vertical = hex_num_vertical - 1;
//console.debug("hex_num_vertical: " + hex_num_vertical + ", blank_num_vertical: " + blank_num_vertical)
let blank_ratio = 1/10;
let blank_len = Math.floor(canvas.height * blank_ratio / (hex_num_vertical * blank_ratio + blank_num_vertical));
let small_hex_len_vertical = canvas.height / (hex_num_vertical + blank_num_vertical * blank_ratio);//canvas.height / hex_num_vertical - blank_len / hex_num_vertical;
let small_hex_len = small_hex_len_vertical / 2 / Math.cos(to_rad(30));
//console.debug(`blank_len: ${blank_len}, small_hex_len_vertical: ${small_hex_len_vertical}, small_hex_len: ${small_hex_len}`);
/*for (let i = 0; i < hex_num_vertical; i++) {
draw_hexagon(canvas.width / 2, i * (small_hex_len_vertical + blank_len) + small_hex_len_vertical / 2, small_hex_len, 2);
}*/
/* the number of "columns" is also the number of tiles in the middle column */
/* the column in the middle has grid_size * 2 - 1 tiles */
column_num = grid_size * 2 - 1;
let list = [];
let id = 0;
for (let i = 0; i < column_num; i++) {
list.push([]);
/* number of tiles in column is grid_size + i, valid up to the middle tho.*/
let num_of_cells_in_column;
let shift_x;
if (i <= Math.floor(column_num / 2)) {
num_of_cells_in_column = grid_size + i;
shift_x = (column_num - num_of_cells_in_column) * (1.5 * small_hex_len + blank_len);//(column_num - num_of_cells_in_column) * (small_hex_len + blank_len);
} else {
num_of_cells_in_column = column_num - i + grid_size - 1;
shift_x = -(column_num - num_of_cells_in_column) * (1.5 * small_hex_len + blank_len);
}
//console.debug("num of cells: " + num_of_cells_in_column);
/* each row going from the center, is shifted by half a hexagone per row */
let shift_y = (column_num - num_of_cells_in_column) * (small_hex_len_vertical / 2 + blank_len / 2);//(column_num - num_of_cells_in_column) * small_hex_len_vertical / 2 + blank_len / 2;
for (let j = 0; j < num_of_cells_in_column; j++) {
draw_hexagon(canvas.width / 2 - shift_x, j * (small_hex_len_vertical + blank_len) + small_hex_len_vertical / 2 + shift_y, small_hex_len, 2);
list[i].push(new Cell(canvas.width / 2 - shift_x, j * (small_hex_len_vertical + blank_len) + small_hex_len_vertical / 2 + shift_y, 'none', false, small_hex_len, id, i, j));
id++;
}
}
return list;
}
function draw_grid(grid) {
//console.debug('drawing grid');
white_canvas();
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j].barred) {
draw_hexagon(grid[i][j].x, grid[i][j].y, grid[i][j].side_len, 2, undefined, '#000000');
//console.debug('filling');
} else if (grid[i][j].selected) {
draw_hexagon(grid[i][j].x, grid[i][j].y, grid[i][j].side_len, 3, '#47443c', '#baf6ff');
} else if (grid[i][j].highlighted) {
draw_hexagon(grid[i][j].x, grid[i][j].y, grid[i][j].side_len, 3, '#47443c', '#dcf8fc');
} else {
draw_hexagon(grid[i][j].x, grid[i][j].y, grid[i][j].side_len, 2);
}
if (!grid[i][j].barred && grid[i][j].aspect != 'none') {
//console.debug('drawing image');
img_size = grid[i][j].side_len * 1.2;
//ctx.drawImage(grid[i][j].img, grid[i][j].x - img_size / 2, grid[i][j].y - img_size / 2, img_size, img_size);
ctx.drawImage(image_cache[grid[i][j].aspect], grid[i][j].x - img_size / 2, grid[i][j].y - img_size / 2, img_size, img_size);
}
}
}
if (debugging) {
draw_hex_ids(grid);
}
}
function update_cell(grid, i, j) {
draw_hexagon(grid[i][j].x, grid[i][j].y, grid[i][j].side_len * 0.9, 2, '#ffffff', '#ffffff');
if (grid[i][j].barred) {
draw_hexagon(grid[i][j].x, grid[i][j].y, grid[i][j].side_len, 2, undefined, '#000000');
//console.debug('filling');
} else if (grid[i][j].selected) {
draw_hexagon(grid[i][j].x, grid[i][j].y, grid[i][j].side_len, 3, '#47443c', '#baf6ff');
} else if (grid[i][j].highlighted) {
if (grid[i][j].aspect == 'none') {
draw_hexagon(grid[i][j].x, grid[i][j].y, grid[i][j].side_len, 3, '#47443c', '#dcf8fc');
} else {
draw_hexagon(grid[i][j].x, grid[i][j].y, grid[i][j].side_len, 3, '#47443c', '#ff809d');
}
} else {
draw_hexagon(grid[i][j].x, grid[i][j].y, grid[i][j].side_len, 2);
}
if (!grid[i][j].barred && grid[i][j].aspect != 'none') {
//console.debug('drawing image');
img_size = grid[i][j].side_len * 1.2;
//ctx.drawImage(grid[i][j].img, grid[i][j].x - img_size / 2, grid[i][j].y - img_size / 2, img_size, img_size);
ctx.drawImage(image_cache[grid[i][j].aspect], grid[i][j].x - img_size / 2, grid[i][j].y - img_size / 2, img_size, img_size);
}
}
function draw_debug_grid(grid) {
console.log("===================");
for (let i = 0; i < grid.length; i++) {
var temp_str = "";
for (let j = 0; j < grid[i].length; j++) {
if (j < grid[i].length - 1) {
temp_str += grid[i][j].id + ": " + grid[i][j].aspect + ", ";
} else {
temp_str += grid[i][j].id + ": " + grid[i][j].aspect;
}
}
console.log(temp_str);
}
console.log("===================");
}
/* draws a circle in the middle of each cell/tile, for debugging purposes */
function draw_hex_centers(grid) {
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
ctx.beginPath();
ctx.arc(grid[i][j].x, grid[i][j].y, 5, 0, 2 * Math.PI);
ctx.stroke();
ctx.closePath();
}
}
}
function draw_hex_ids(grid) {
ctx.font = "30px Arial";
ctx.fillStyle = 'red';
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
ctx.fillText(grid[i][j].id, grid[i][j].x, grid[i][j].y);
}
}
}
function assign_aspect() {
if (selected_cell != undefined) {
selected_cell.aspect = document.getElementById('aspect_selector').value;
draw_grid(grid);
}
}
function clear_grid(grid) {
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
grid[i][j].aspect = 'none';
grid[i][j].barred = false;
}
}
}
function preset() {
let num = preset_num;
/* base */
clear_grid(grid);
reset();
if (num == 0) {
grid = draw_initial_grid(3);
current_state.grid = grid;
grid[0][2].aspect = 'instrumentum';grid[2][0].aspect = 'permutatio';grid[4][2].aspect = 'metallum';
draw_grid(grid);
} else if (num == 1) {
grid = draw_initial_grid(3);
current_state.grid = grid;
var barred_blacklist = [2, 5, 9, 13];
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
var can_barr = true;
for (let k = 0; k < barred_blacklist.length; k++) {
if (barred_blacklist[k] == grid[i][j].id) {
can_barr = false;
}
}
if (can_barr) {
grid[i][j].barred = true;
}
}
}
grid[0][2].aspect = 'aer';grid[3][1].aspect = 'lux';
draw_grid(grid);
} else if (num == 2) {
grid = draw_initial_grid(4);
current_state.grid = grid;grid[0][3].aspect = 'instrumentum';grid[3][0].aspect = 'arbor';grid[6][3].aspect = 'praecantatio';grid[0][0].barred = true;grid[4][2].barred = true;grid[5][3].barred = true;grid[6][1].barred = true;
draw_grid(grid);
} else if (num == 3) {
grid = draw_initial_grid(4);
current_state.grid = grid;
grid[0][3].aspect = 'permutatio';grid[1][0].aspect = 'motus';grid[5][4].aspect = 'auram';grid[6][0].aspect = 'praecantatio';grid[3][4].barred = true;grid[3][6].barred = true;grid[4][4].barred = true;grid[6][3].barred = true;
draw_grid(grid);
} else if (num == 4) {
grid = draw_initial_grid(5);
current_state.grid = grid;
grid[0][0].aspect = 'terra';grid[0][4].aspect = 'instrumentum';grid[4][0].aspect = 'aer';grid[4][8].aspect = 'praecantatio';grid[8][0].aspect = 'ordo';grid[8][4].aspect = 'arbor';grid[1][0].barred = true;grid[2][2].barred = true;grid[2][3].barred = true;grid[4][4].barred = true;grid[5][2].barred = true;grid[5][6].barred = true;grid[7][2].barred = true;grid[7][4].barred = true;
draw_grid(grid);
} else if (num == 5) {
grid = draw_initial_grid(4);
current_state.grid = grid;grid[0][3].aspect = 'instrumentum';grid[1][0].aspect = 'arbor';grid[4][0].aspect = 'gelum';grid[4][5].aspect = 'praecantatio';grid[6][2].aspect = 'aqua';grid[1][3].barred = true;grid[2][0].barred = true;grid[4][3].barred = true;
draw_grid(grid);
} else if (num == 6) {
grid = draw_initial_grid(4);
current_state.grid = grid;
grid[0][3].aspect = 'instrumentum';grid[1][0].aspect = 'arbor';grid[4][0].aspect = 'ordo';grid[4][5].aspect = 'praecantatio';grid[6][2].aspect = 'terra';grid[0][1].barred = true;grid[3][4].barred = true;grid[4][4].barred = true;grid[5][4].barred = true;
draw_grid(grid);
} else if (num == 7) {
grid = draw_initial_grid(5);
current_state.grid = grid;
grid[0][1].aspect = 'auram';
grid[0][4].aspect = 'terra';
grid[1][0].aspect = 'ignis';
grid[3][7].aspect = 'permutatio';
grid[4][0].aspect = 'gelum';
grid[5][7].aspect = 'potentia';
grid[7][0].aspect = 'perfodio';
grid[8][1].aspect = 'perditio';
grid[8][4].aspect = 'ordo';
grid[1][3].barred = true;
grid[3][0].barred = true;
grid[4][3].barred = true;
grid[5][0].barred = true;
grid[5][6].barred = true;
grid[7][4].barred = true;
draw_grid(grid);
} else {
console.warn("Invalid preset!");
}
precalculate_cell_distances();
}
function remove_from_aspect_table(aspect) {
let res = {};
for (let i = 0; i < keylen(aspect_table); i++) {
if (keyAtIndex(aspect_table, i) != aspect) {
res[keyAtIndex(aspect_table, i)] = elemAtIndex(aspect_table, i);
}
}
aspect_table = res;
}
function add_to_aspect_table(aspect) {
let one = null;
let two = null;
if (all_aspect_table[aspect].length > 0) {
one = all_aspect_table[aspect][0];
two = all_aspect_table[aspect][1];
aspect_table[aspect] = [one, two];
} else {
aspect_table[aspect] = [];
}
}
var aspect_table = {
'aer':[],
'alienis':['vacuos','tenebrae'],
'aqua':[],
'arbor':['aer','herba'],
'auram':['aer','praecantatio'],
'bestia':['motus','victus'],
'cognitio':['ignis','spiritus'],
'corpus':['bestia','mortuus'],
'exanimis':['motus','mortuus'],
'fabrico':['humanus','instrumentum'],
'fames':['vacuos','victus'],
'gelum':['ignis','perditio'],
'herba':['terra','victus'],
'humanus':['bestia','cognitio'],
'ignis':[],
'instrumentum':['ordo','humanus'],
'iter':['terra','motus'],
'limus':['aqua','victus'],
'lucrum':['fames','humanus'],
'lux':['aer','ignis'],
'machina':['motus','instrumentum'],
'messis':['herba','humanus'],
'metallum':['terra','vitreus'],
'meto':['instrumentum','messis'],
'mortuus':['perditio','victus'],
'motus':['aer','ordo'],
'ordo':[],
'pannus':['bestia','instrumentum'],
'perditio':[],
'perfodio':['terra','humanus'],
'permutatio':['ordo','perditio'],
'potentia':['ignis','ordo'],
'praecantatio':['potentia','vacuos'],
'sano':['ordo','victus'],
'sensus':['aer','spiritus'],
'spiritus':['victus','mortuus'],
'telum':['ignis','instrumentum'],
'tempestas':['aer','aqua'],
'tenebrae':['lux','vacuos'],
'terra':[],
'tutamen':['terra','instrumentum'],
'vacuos':['aer','perditio'],
'venenum':['aqua','perditio'],
'victus':['aqua','terra'],
'vinculum':['perditio','motus'],
'vitium':['perditio','praecantatio'],
'vitreus':['ordo','terra'],
'volatus':['aer','motus'],
'desidia':['vinculum','spiritus'],
'gula':['fames','vacuos'],
'infernus':['ignis','praecantatio'],
'invidia':['sensus','fames'],
'ira':['telum','ignis'],
'luxuria':['corpus','fames'],
'superbia':['volatus','vacuos'],
'tempus':['vacuos','ordo'],
'electrum':['potentia','machina'],
'magneto':['metallum','iter'],
'nebrisum':['perfodio', 'lucrum'],
'radio':['lux','potentia'],
'strontio':['perditio','cognitio'],
'terminus':['alienis','lucrum'],
'coralos':['venenum', 'aqua'],
'dreadia':['venenum', 'ignis'],
'tincturem':['lux', 'ordo'],
'sanctus':['spiritus', 'auram'],
'exubitor':['alienis', 'mortuus'],
'saxum':['terra', 'terra'],
'granum':['terra', 'victus'],
'mru':['praecantatio', 'potentia'],
'radiation':['mru', 'motus'],
'matrix':['mru', 'humanus']
};
var all_aspect_table = {};
for (let i = 0; i < keylen(aspect_table); i++) {
all_aspect_table[keyAtIndex(aspect_table, i)] = elemAtIndex(aspect_table, i);
}
function toggle_availability(id) {
let aspect = communize(document.getElementById('text_' + id).innerText).replace(/(\r\n|\n|\r)/gm, "");
let found = false;
for (let i = 0; i < keylen(aspect_table); i++) {
//if (keyAtIndex(aspect_table, i) == keyAtIndex(all_aspect_table, id)) {
if (keyAtIndex(aspect_table, i) == aspect) {
found = true;
}
}
if (found) {
remove_from_aspect_table(aspect);
document.getElementById('aspect_image_' + id).style.filter = 'brightness(25%)';
enabled_flavor_dict[aspect] = false;
} else {
add_to_aspect_table(aspect);
document.getElementById('aspect_image_' + id).style.filter = 'brightness(100%)';
enabled_flavor_dict[aspect] = true;
}
}
function set_default_aspect_set() {
document.getElementById('vanilla_checkbox').checked = true;
let default_aspects = ['aer','alienis','aqua','arbor','auram','bestia','cognitio','corpus','exanimis','fabrico','fames','gelum','herba','humanus','ignis','instrumentum','iter','limus','lucrum','lux','machina','messis','metallum','meto','mortuus','motus','ordo','pannus','perditio','perfodio','permutatio','potentia','praecantatio','sano','sensus','spiritus','telum','tempestas','tenebrae','terra','tutamen','vacuos','venenum','victus','vinculum','vitium','vitreus','volatus'];
for (let i = 0; i < keylen(all_aspect_table); i++) {
if (!default_aspects.includes(keyAtIndex(all_aspect_table, i))) {
remove_from_aspect_table(keyAtIndex(all_aspect_table, i));
document.getElementById('aspect_image_' + i).style.filter = 'brightness(25%)';
enabled_flavor_dict[keyAtIndex(all_aspect_table, i)] = false;
}
}
}
function toggle_aspect_set(checkbox_wrapper) {
let checkbox = checkbox_wrapper.childNodes[1];
let aspect_set = {
vanilla_tc : ['aer','alienis','aqua','arbor','auram','bestia','cognitio','corpus','exanimis','fabrico','fames','gelum','herba','humanus','ignis','instrumentum','iter','limus','lucrum','lux','machina','messis','metallum','meto','mortuus','motus','ordo','pannus','perditio','perfodio','permutatio','potentia','praecantatio','sano','sensus','spiritus','telum','tempestas','tenebrae','terra','tutamen','vacuos','venenum','victus','vinculum','vitium','vitreus','volatus'],
forbidden_magic : ['desidia','gula','infernus','invidia','ira','luxuria','superbia'],
gregtech : ['electrum','magneto','nebrisum','radio','strontio'],
magic_bees : ['tempus'],
avaritia : ['terminus'],
abyssal : ['coralos', 'dreadia'],
botanical : ['tincturem'],
elysium : ['sanctus'],
revelations : ['exubitor'],
additions : ['saxum', 'granum'],
essential : ['mru', 'radiation', 'matrix']
};
if (!checkbox.checked) {
for (let i = 0; i < aspect_set[checkbox.value].length; i++) {
for (let j = 0; j < keylen(all_aspect_table); j++) {
if (keyAtIndex(all_aspect_table, j) == aspect_set[checkbox.value][i]) {
remove_from_aspect_table(aspect_set[checkbox.value][i]);
//document.getElementById('aspect_image_' + indexOfElem(all_aspect_table, aspect_set[checkbox.value][i])).style.filter = 'brightness(25%)';
enabled_flavor_dict[keyAtIndex(all_aspect_table, j)] = false;
}
}
}
} else {
for (var i = 0; i < aspect_set[checkbox.value].length; i++) {
add_to_aspect_table(aspect_set[checkbox.value][i]);
//document.getElementById('aspect_image_' + indexOfElem(all_aspect_table, aspect_set[checkbox.value][i])).style.filter = 'brightness(100%)';
enabled_flavor_dict[keyAtIndex(all_aspect_table, indexOfElem(all_aspect_table, aspect_set[checkbox.value][i]))] = true;
}
}
load_aspect_selector(false);
}
function search(box) {
let results_beginning = [];
let results_anywhere = [];
for (let i = 0; i < keylen(all_aspect_table); i++) {
if (keyAtIndex(all_aspect_table, i).startsWith(box.value.toLowerCase())) {