-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoke.js
1295 lines (1130 loc) · 45.8 KB
/
poke.js
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
var result1 = new Array();
var result2 = new Array();
var typeComp = 0;
var mon1, mon2;
var num1, num2;
var mon1stats, mon2stats;
var mon1types, mon2types;
var mon1abilities, mon2abilities;
var jsonBody;
//Adding options to datalist
var box1 = document.getElementById('fname1');
var box2 = document.getElementById('fname2');
var dl = document.createElement('datalist');
dl.id = 'dlPkmn';
for (var i=0 ; i < ids.length; i += 1) {
var option = document.createElement('option');
//Special name case
if (nameFix.includes(ids[i][0].toLowerCase())) {
var val = nameException[nameFix.indexOf(ids[i][0].toLowerCase())]
option.value = val[0].toUpperCase() + val.substring(1)
}
else {
option.value = ids[i][0];
}
dl.appendChild(option);
}
box1.appendChild(dl);
box2.appendChild(dl);
//Press ENTER on text area 1
var pkmn1 = document.getElementById('fname1');
pkmn1.addEventListener("keydown", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("button").click();
}
});
//Press ENTER on text area 2
var pkmn2 = document.getElementById('fname2');
pkmn2.addEventListener("keydown", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("button").click();
}
});
//Empty pokemon text area
function resetPoke() {
document.getElementById("fname1").value = null;
document.getElementById("fname2").value = null;
document.getElementById("p1").src = "types/unknown.png";
document.getElementById("p2").style = "display:none";
document.getElementById("p3").src = "types/unknown.png";
document.getElementById("p4").style = "display:none";
document.getElementById("pic1").src = "question.png";
document.getElementById("pic2").src = "question.png";
document.getElementById("dexnumber1").innerHTML = " ";
document.getElementById("dexnumber2").innerHTML = " ";
document.getElementById("fusionid1").innerHTML = " ";
document.getElementById("fusionid2").innerHTML = " ";
document.getElementById("FP1").innerHTML = "mon1/mon2";
document.getElementById("FP2").innerHTML = "mon2/mon1";
document.getElementById("hp1").innerHTML = "HP: ";
document.getElementById("atk1").innerHTML = "ATK: ";
document.getElementById("def1").innerHTML = "DEF: ";
document.getElementById("spatk1").innerHTML = "SPE.ATK: ";
document.getElementById("spdef1").innerHTML = "SPE.DEF: ";
document.getElementById("spe1").innerHTML = "SPEED: ";
document.getElementById("bs1").innerHTML = "TOTAL: ";
document.getElementById("ab1").innerHTML = "ABILITY:";
document.getElementById("hab1").innerHTML = "";
document.getElementById("hp2").innerHTML = "HP: ";
document.getElementById("atk2").innerHTML = "ATK: ";
document.getElementById("def2").innerHTML = "DEF: ";
document.getElementById("spatk2").innerHTML = "SPE.ATK: ";
document.getElementById("spdef2").innerHTML = "SPE.DEF: ";
document.getElementById("spe2").innerHTML = "SPEED: ";
document.getElementById("bs2").innerHTML = "TOTAL: ";
document.getElementById("ab2").innerHTML = "ABILITY:";
document.getElementById("hab2").innerHTML = "";
document.getElementById("weak14").innerHTML = "x4: ";
document.getElementById("weak12").innerHTML = "x2: ";
document.getElementById("weak11").innerHTML = "x1: ";
document.getElementById("weak105").innerHTML = "x0.5: ";
document.getElementById("weak1025").innerHTML = "x0.25: ";
document.getElementById("weak100").innerHTML = "x0: ";
document.getElementById("weak24").innerHTML = "x4: ";
document.getElementById("weak22").innerHTML = "x2: ";
document.getElementById("weak21").innerHTML = "x1: ";
document.getElementById("weak205").innerHTML = "x0.5: ";
document.getElementById("weak2025").innerHTML = "x0.25: ";
document.getElementById("weak200").innerHTML = "x0: ";
var L0 = ["hp1","atk1","def1","spatk1","spdef1","spe1","bs1"];
var L1 = ["hp2","atk2","def2","spatk2","spdef2","spe2","bs2"];
for (var i = 0; i < L0.length; i++) {
document.getElementById(L0[i]).style.color = "";
document.getElementById(L1[i]).style.color = "";
}
}
function getRandomPokeID(){
maxPoke = 420
return Math.floor(Math.random() * Math.floor(maxPoke));
}
function randomBothPoke() {randomPoke(true, true)}
function randomFirstPoke() {randomPoke(true, false)}
function randomSecondPoke() {randomPoke(false, true)}
function randomPoke(should_rand1, should_rand2) {
buttons = document.getElementsByClassName("button");
for (let i = 0; i < buttons.length; i++) {
buttons[i].disabled = true;
};
if(should_rand1 || num1 == undefined) {rand1 = getRandomPokeID();}
else{rand1 = num1 - 1;}
if(should_rand2 || num2 == undefined) {rand2 = getRandomPokeID();}
else{rand2 = num2 - 1;}
var name = ids[rand1][0].toLowerCase();
if (nameFix.includes(name)) {
name = nameException[nameFix.indexOf(name)];
}
document.getElementById("fname1").value = name
var name2 = ids[rand2][0].toLowerCase();
if (nameFix.includes(name2)) {
name2 = nameException[nameFix.indexOf(name2)];
}
document.getElementById("fname2").value = name2
fusePoke()
}
function getPokemonName(htmlId){
var pokemonName = (document.getElementById(htmlId)).value.toLowerCase();
pokemonName = pokemonName.replace(/\W/g, '');
return pokemonName;
}
//Fusion calculation function
function fusePoke() {
//Pokemon from both text area
mon1 = getPokemonName("fname1");
mon2 = getPokemonName("fname2");
if (isMissingNames(mon1, mon2)) {
buttons = document.getElementsByClassName("button");
for (let i = 0; i < buttons.length; i++) {
buttons[i].disabled = false;
};
alert("Please fill the two text inputs!");
}
// TODO : test if poke is in fangame
// idCheck
else {
// Nidoran clause
if(mon1=="nidoran" || mon2 =="nidoran"){
alert("Please specify a gender to Nidoran (f or m)");
}
else{
//Special mon selector: Giratina, Deoxys
if (nameException.includes(mon1)) {
mon1 = nameFix[nameException.indexOf(mon1)];
}
if (nameException.includes(mon2)) {
mon2 = nameFix[nameException.indexOf(mon2)];
}
// First request - version A
var xhr1a = new XMLHttpRequest();
var url1a = "https://pokeapi.co/api/v2/pokemon/" + mon1;
xhr1a.open('GET', url1a, true);
xhr1a.send();
xhr1a.onload = function() {
jsonBody = xhr1a.responseText;
if(jsonBody) {
if(jsonBody != "Not Found"){
jp = JSON.parse(jsonBody);
fuseFirstPoke(jp);
}
else{
//First request - version B
var xhr1b = new XMLHttpRequest();
var url1b = "https://pokeapi.co/api/v2/pokemon/" + mon1 + "/";
xhr1b.open('GET', url1b, true);
xhr1b.send();
xhr1b.onload = function() {
jsonBody = xhr1b.responseText;
if(jsonBody) {
if(jsonBody != "Not Found"){
jp = JSON.parse(jsonBody);
fuseFirstPoke(jp);
}
else{
alert("First pokemon was misspelled ?");
}
}
else{
alert("PokeAPI is unreachable (1b)");
}
}
}
}
else{
alert("PokeAPI is unreachable (1a)");
}
}
}
}
}
function fuseFirstPoke(jsonString){
//ID selector for sprite showcase of the 1st mon/ Validator for 1st mon
num1 = jsonString.id;
var id1 = num1;
var idCheck1 = false;
for (var i = 0; i < ids.length; i++){
if (ids[i][0] == mon1.charAt(0).toUpperCase() + mon1.slice(1)) {
idCheck1 = true;
num1 = ids[i][1];
}
}
if (idCheck1 == false && id1 >= 252) {
alert("The first pokemon isn't in the fangame!")
}
else {
//#region Type of 1st mon
//Type selector for fusion type knowledge of the 1st mon
var type1 = jsonString.types;
mon1types = [];
var compt = 0;
//Exception mon selected for swapped types
for (var i = 0; i < typeSwap.length; i++) {
if (typeSwap[i][2] == mon1.charAt(0).toUpperCase() + mon1.slice(1)) {
mon1types.push(typeSwap[i][0]);
mon1types.push(typeSwap[i][1]);
var compt = 1;
}
}
//Exception mon selected for one type
for (var i = 0; i < typeUni.length; i++) {
if (typeUni[i][1] == mon1.charAt(0).toUpperCase() + mon1.slice(1)) {
mon1types.push(typeUni[i][0]);
var compt = 2;
}
}
if (compt == 0) {
mon1types.push(type1[0].type.name);
if (type1.length == 2 && compt != 2) {
if (type1[0].type.name == "normal" && type1[1].type.name == "flying") {
mon1types[0] = "flying";
} else {
mon1types.push(type1[1].type.name);
}
}
}
//#endregion
//#region Stats of 1st mon
var stats1;
if (statsException.includes(mon1)) {
stats1 = statsFix[statsException.indexOf(mon1)];
} else {
stats1 = jsonString.stats;
}
mon1stats = [];
for (var i = 0; i < stats1.length; i++) {
mon1stats.push(stats1[i].base_stat);
}
//#endregion
//#region Abilities of 1st mon
var ab1;
if (abilitiesException.includes(mon1)) {
ab1 = abilitiesFix[abilitiesException.indexOf(mon1)];
} else {
ab1 = jsonString.abilities;
}
mon1abilities = [];
for (var i = 0; i < ab1.length; i++) {
mon1abilities.push([ab1[i].ability, ab1[i].is_hidden]);
}
//#endregion
//#region Request
// Second request - version A
var xhr2a = new XMLHttpRequest();
var url2a = "https://pokeapi.co/api/v2/pokemon/" + mon2;
xhr2a.open('GET', url2a, true);
xhr2a.send();
xhr2a.onload = function() {
jsonBody = xhr2a.responseText;
if(jsonBody){
if(jsonBody != "Not Found"){
jp = JSON.parse(jsonBody);
fuseSecondPoke(jp);
}
else{
// Second request - version B
var xhr2b = new XMLHttpRequest();
var url2b = "https://pokeapi.co/api/v2/pokemon/" + mon2 + "/";
xhr2b.open('GET', url2b, true);
xhr2b.send();
xhr2b.onload = function() {
jsonBody = xhr2b.responseText;
if(jsonBody) {
if(jsonBody != "Not Found"){
jp = JSON.parse(jsonBody);
fuseSecondPoke(jp);
}
else{
alert("Second pokemon was misspelled ?");
}
}
else{
alert("PokeAPI is unreachable (2b)");
}
}
}
}
else{
alert("PokeAPI is unreachable (2a)");
}
}
//#endregion
}
}
function fuseSecondPoke(jsonString){
//ID selector for sprite showcase of the 2st mon
num2 = jsonString.id;
var id2 = num2;
var idCheck2 = false;
for (var i = 0; i < ids.length; i++){
if (ids[i][0] == mon2.charAt(0).toUpperCase() + mon2.slice(1)) {
idCheck2 = true;
num2 = ids[i][1];
}
}
if (idCheck2 == false && id2 >= 252) {
alert("The second pokemon isn't in the fangame!")
}
else {
//#region Type of 2nd mon
//Type selector for fusion type knowledge of the 2nd mon
var type2 = jsonString.types;
mon2types = [];
var compt = 0;
//Exception mon selected for swapped types
for (var i = 0; i < typeSwap.length; i++) {
if (typeSwap[i][2] == mon2.charAt(0).toUpperCase() + mon2.slice(1)) {
mon2types.push(typeSwap[i][0]);
mon2types.push(typeSwap[i][1]);
var compt = 1;
}
}
//Exception mon selected for one type
for (var i = 0; i < typeUni.length; i++) {
if (typeUni[i][1] == mon2.charAt(0).toUpperCase() + mon2.slice(1)) {
mon2types.push(typeUni[i][0]);
var compt = 2;
}
}
//Type of 2nd mon
if (compt == 0) {
mon2types.push(type2[0].type.name);
if (type2.length == 2 && compt != 2) {
if (type2[0].type.name == "normal" && type2[1].type.name == "flying") {
mon2types[0] = "flying";
} else {
mon2types.push(type2[1].type.name);
}
}
}
//#endregion
//#region Stats of 2nd mon
var stats2;
if (statsException.includes(mon2)) {
stats2 = statsFix[statsException.indexOf(mon2)];
} else {
stats2 = jsonString.stats;
}
mon2stats = [];
for (var i = 0; i < stats2.length; i++) {
mon2stats.push(stats2[i].base_stat);
}
//#endregion
//#region Abilities of 2nd mon
var ab2;
if (abilitiesException.includes(mon2)) {
ab2 = abilitiesFix[abilitiesException.indexOf(mon2)];
} else {
ab2 = jsonString.abilities;
}
mon2abilities = [];
for (var i = 0; i < ab2.length; i++) {
mon2abilities.push([ab2[i].ability, ab2[i].is_hidden]);
}
//#endregion
fuseBothPoke()
}
}
function fuseBothPoke(){
//TODO : factor this
//Name of fusion
if (!nameFix.includes(mon1) && !nameFix.includes(mon2)) {
var fmon1 = mon1.charAt(0).toUpperCase() + mon1.slice(1);
var fmon2 = mon2.charAt(0).toUpperCase() + mon2.slice(1);
} else if (nameFix.includes(mon1) && !nameFix.includes(mon2)) {
var fmon1 = nameException[nameFix.indexOf(mon1)].charAt(0).toUpperCase() + nameException[nameFix.indexOf(mon1)].slice(1);
var fmon2 = mon2.charAt(0).toUpperCase() + mon2.slice(1);
} else if (!nameFix.includes(mon1) && nameFix.includes(mon2)) {
var fmon1 = mon1.charAt(0).toUpperCase() + mon1.slice(1);
var fmon2 = nameException[nameFix.indexOf(mon2)].charAt(0).toUpperCase() + nameException[nameFix.indexOf(mon2)].slice(1);
} else if (nameFix.includes(mon1) && nameFix.includes(mon2)) {
var fmon1 = nameException[nameFix.indexOf(mon1)].charAt(0).toUpperCase() + nameException[nameFix.indexOf(mon1)].slice(1);
var fmon2 = nameException[nameFix.indexOf(mon2)].charAt(0).toUpperCase() + nameException[nameFix.indexOf(mon2)].slice(1);
}
//Dex Numbers
var dexnum1 = Math.floor(num1 + (420 * num2));
var dexnum2 = Math.floor(num2 + (420 * num1));
document.getElementById("dexnumber1").innerHTML = dexnum1;
document.getElementById("dexnumber2").innerHTML = dexnum2;
document.getElementById("fusionid1").innerHTML = " (" + num1 + "." + num2 + ")";
document.getElementById("fusionid2").innerHTML = " (" + num2 + "." + num1 + ")";
//Name of fusions
document.getElementById("FP1").innerHTML = fmon1+ "/" + fmon2;
document.getElementById("FP2").innerHTML = fmon2 + "/" + fmon1;
//Name of pictures
var pic1 = num1 + "." + num2 + ".png";
var pic2 = num2 + "." + num1 + ".png";
//Stats calculation
var hp1 = (mon2stats[0]/3) + 2*(mon1stats[0]/3);
var atk1 = 2*(mon2stats[1]/3) + (mon1stats[1]/3);
var def1 = 2*(mon2stats[2]/3) + (mon1stats[2]/3);
var spatk1 = (mon2stats[3]/3) + 2*(mon1stats[3]/3);
var spdef1 = (mon2stats[4]/3) + 2*(mon1stats[4]/3);
var spe1 = 2*(mon2stats[5]/3) + (mon1stats[5]/3);
var bs1 = Math.floor(hp1) + Math.floor(atk1) + Math.floor(def1) + Math.floor(spatk1) + Math.floor(spdef1) + Math.floor(spe1);
var hp2 = (mon1stats[0]/3) + 2*(mon2stats[0]/3);
var atk2 = 2*(mon1stats[1]/3) + (mon2stats[1]/3);
var def2= 2*(mon1stats[2]/3) + (mon2stats[2]/3);
var spatk2 = (mon1stats[3]/3) + 2*(mon2stats[3]/3);
var spdef2 = (mon1stats[4]/3) + 2*(mon2stats[4]/3);
var spe2 = 2*(mon1stats[5]/3) + (mon2stats[5]/3);
var bs2 = Math.floor(hp2) + Math.floor(atk2) + Math.floor(def2) + Math.floor(spatk2) + Math.floor(spdef2) + Math.floor(spe2);
var L0 = ["hp1","atk1","def1","spatk1","spdef1","spe1","bs1"];
var L1 = ["hp2","atk2","def2","spatk2","spdef2","spe2","bs2"];
var L2 = [Math.floor(hp1), Math.floor(atk1), Math.floor(def1), Math.floor(spatk1), Math.floor(spdef1), Math.floor(spe1), Math.floor(bs1)];
var L3 = [Math.floor(hp2), Math.floor(atk2), Math.floor(def2), Math.floor(spatk2), Math.floor(spdef2), Math.floor(spe2), Math.floor(bs2)];
var L4 = [];
var L5 = [];
for (var i = 0; i < L0.length; i++) {
L4.push(Math.max(L2[i], L3[i])-Math.min(L2[i], L3[i]));
}
//Color of stats
for (var i = 0; i < L1.length; i++) {
if (L2[i] < L3[i]) {
document.getElementById(L0[i]).style.color = "red";
document.getElementById(L1[i]).style.color = "green";
L5.push(" (+" + L4[i] + ")");
L4[i] = " (-" + L4[i] + ")";
} else if (L2[i] > L3[i]) {
document.getElementById(L1[i]).style.color = "red";
document.getElementById(L0[i]).style.color = "green";
L5.push(" (-" + L4[i] + ")");
L4[i] = " (+" + L4[i] + ")";
} else {
document.getElementById(L1[i]).style.color = "orange";
document.getElementById(L0[i]).style.color = "orange";
L4[i] = " (0)";
L5.push(" (0)");
}
document.getElementById(L0[i]).innerHTML = L0[i].slice(-1) + ": " + L2[i];
document.getElementById(L1[i]).innerHTML = L1[i].slice(-1) + ": " + L3[i];
}
//Writting stat in HTML
/*
if (mon1 == "shedinja" || mon2 == "shedinja") {
document.getElementById("hp1").innerHTML = "HP: 1 (0)";
document.getElementById("hp1").style.color = "orange";
} else {*/
document.getElementById("hp1").innerHTML = "HP: " + Math.floor(hp1) + L4[0];
/*}*/
document.getElementById("atk1").innerHTML = "ATK: " + Math.floor(atk1) + L4[1];
document.getElementById("def1").innerHTML = "DEF: " + Math.floor(def1) + L4[2];
document.getElementById("spatk1").innerHTML = "SPE.ATK: " + Math.floor(spatk1) + L4[3];
document.getElementById("spdef1").innerHTML = "SPE.DEF: " + Math.floor(spdef1) + L4[4];
document.getElementById("spe1").innerHTML = "SPEED: " + Math.floor(spe1) + L4[5];
document.getElementById("bs1").innerHTML = "TOTAL: " + Math.floor(bs1) + L4[6];
/*
if (mon1 == "shedinja" || mon2 == "shedinja") {
document.getElementById("hp2").innerHTML = "HP: 1 (0)";
document.getElementById("hp2").style.color = "orange";
} else {*/
document.getElementById("hp2").innerHTML = "HP: " + Math.floor(hp2) + L5[0];
/*}*/
document.getElementById("atk2").innerHTML = "ATK: " + Math.floor(atk2) + L5[1];
document.getElementById("def2").innerHTML = "DEF: " + Math.floor(def2) + L5[2];
document.getElementById("spatk2").innerHTML = "SPE.ATK: " + Math.floor(spatk2) + L5[3];
document.getElementById("spdef2").innerHTML = "SPE.DEF: " + Math.floor(spdef2) + L5[4];
document.getElementById("spe2").innerHTML = "SPEED: " + Math.floor(spe2) + L5[5];
document.getElementById("bs2").innerHTML = "TOTAL: " + Math.floor(bs2) + L5[6];
//Abilities of fused mons
if (abilitySwap.includes(mon1)) {
[mon1abilities[0], mon1abilities[1]] = [mon1abilities[1], mon1abilities[0]];
}
if (abilitySwap.includes(mon2)) {
[mon2abilities[0], mon2abilities[1]] = [mon2abilities[1], mon2abilities[0]];
}
//Type of fused mons
var fmonres1 = null;
var fmonres2 = null;
if(mon1 == mon2 && selfFusionTypeException.includes(mon1)){
fmonres1 = selfFusionTypeFix[selfFusionTypeException.indexOf(mon1)];
fmonres2 = fmonres1
}
else{
fmonres1 = fusType(mon1types, mon2types);
fmonres2 = fusType(mon2types, mon1types);
}
//Types effectiveness
if (typeComp>0) {
c = document.getElementsByClassName('monweak');
for( b=0; b < c.length; b++ )
{
defaultValue = c[b].getAttribute('data-default');
if (defaultValue) {
c[b].innerText = defaultValue;
}
}
}
tyeffid1 = typeId(fmonres1);
tyeffid2 = typeId(fmonres1);
for (var i = 0; i < typeName.length; i++) {
result1[i] = (types[i][tyeffid1[0]] * types[i][tyeffid1[1]]);
}
for (var i = 0; i < typeName.length; i++) {
var image = new Image()
image.src = "types/" + typeName[i] + ".png";
if (result1[i] == 4) {
document.getElementById("weak14").appendChild(image);
}
if (result1[i] == 2) {
document.getElementById("weak12").appendChild(image);
}
if (result1[i] == 1) {
document.getElementById("weak11").appendChild(image);
}
if (result1[i] == 0.5) {
document.getElementById("weak105").appendChild(image);
}
if (result1[i] == 0.25) {
document.getElementById("weak1025").appendChild(image);
}
if (result1[i] == 0) {
document.getElementById("weak100").appendChild(image);
}
}
tyeffid1 = typeId(fmonres2);
tyeffid2 = typeId(fmonres2);
for (var i = 0; i < typeName.length; i++) {
result2[i] = (types[i][tyeffid1[0]] * types[i][tyeffid1[1]]);
}
for (var i = 0; i < typeName.length; i++) {
var image = new Image();
image.src = "types/" + typeName[i] + ".png";
if (result2[i] == 4) {
document.getElementById("weak24").appendChild(image);
}
if (result2[i] == 2) {
document.getElementById("weak22").appendChild(image);
}
if (result2[i] == 1) {
document.getElementById("weak21").appendChild(image);
}
if (result2[i] == 0.5) {
document.getElementById("weak205").appendChild(image);
}
if (result2[i] == 0.25) {
document.getElementById("weak2025").appendChild(image);
}
if (result2[i] == 0) {
document.getElementById("weak200").appendChild(image);
}
}
typeComp += 1;
document.getElementById("p1").src = "types/" + fmonres1[0] + ".png";
if (fmonres1.length!=1 && (fmonres1.length == 2 && fmonres1[0] != fmonres1[1])) {
document.getElementById("p2").style.display = "inline-block";
document.getElementById("p2").src = "types/" + fmonres1[1] + ".png";
} else {
document.getElementById("p2").style.display = "none";
}
document.getElementById("p3").src = "types/" + fmonres2[0] + ".png";
if (fmonres2.length!=1 && (fmonres2.length == 2 && fmonres2[0] != fmonres2[1])) {
document.getElementById("p4").style.display = "inline-block";
document.getElementById("p4").src = "types/" + fmonres2[1] + ".png";
} else {
document.getElementById("p4").style.display = "none";
}
//Picture of fusions (if inside CustomBattlers)
showFusion("pic1", pic1, "fusionid1");
showFusion("pic2", pic2, "fusionid2");
//Abilities 1
var abilities1 = fusionAbilities(mon1abilities, mon2abilities);
var hiddenAbilities1 = fusionHiddenAbilities(mon1abilities, mon2abilities, abilities1);
var abilitiesText1 = "ABILITY: " + sanitizeAbilityList(abilities1);
var hiddenAbilitiesText1 = sanitizeAbilityList(hiddenAbilities1);
document.getElementById("ab1").innerHTML = abilitiesText1;
document.getElementById("hab1").innerHTML = hiddenAbilitiesText1;
//Abilities 2
var abilties2 = fusionAbilities(mon2abilities, mon1abilities);
var hiddenAbilities2 = fusionHiddenAbilities(mon2abilities, mon1abilities, abilties2);
var abilitiesText2 = "ABILITY: " + sanitizeAbilityList(abilties2);
var hiddenAbilitiesText2 = sanitizeAbilityList(hiddenAbilities2);
document.getElementById("ab2").innerHTML = abilitiesText2;
document.getElementById("hab2").innerHTML = hiddenAbilitiesText2;
//Fusion done
buttons = document.getElementsByClassName("button");
for (let i = 0; i < buttons.length; i++) {
buttons[i].disabled = false;
};
}
function typeId(ftype) {
var ty1 = typeName.indexOf(ftype[0]);
if (ftype.length == 2) {
var ty2 = typeName.indexOf(ftype[1]);
} else {
var ty2 = 18;
}
return [ty1, ty2];
}
//Custom sprite fusion function
function showFusion(elementId, fusionId, elementFusionId){
fusionUrl = "https://raw.githubusercontent.com/Aegide/custom-fusion-sprites/main/CustomBattlers/" + fusionId;
document.getElementById(elementId).title = fusionId;
if(doesImageExists(fusionUrl)){
document.getElementById(elementId).src = fusionUrl;
document.getElementById(elementFusionId).style.color = "green";
}
else{//Screenshot of autogen pokemon
fallbackFusionRepository = "https://raw.githubusercontent.com/Aegide/autogen-fusion-sprites/master/Battlers/"
headId = fusionId.split(".")[0];
fallbackFusionUrl = fallbackFusionRepository + headId + "/" + fusionId;
document.getElementById(elementId).src = fallbackFusionUrl;
document.getElementById(elementFusionId).style.color = "red";
}
}
//Error detection
function doesImageExists(imageUrl){
var http = new XMLHttpRequest();
http.open('HEAD', imageUrl, false);
// Can't handle error in an easy way
http.send();
return http.status != 404;
}
// Swaps the head with the body and viceversa
function swapPoke(){
let auxId = headId;
let auxFname = document.getElementById("fname1").value;
headId = bodyId;
document.getElementById("fname1").value = document.getElementById("fname2").value;
bodyId = auxId;
document.getElementById("fname2").value = auxFname;
showShinies(false, false);
}
// Let's you pick a image from your machine
$("input[id='pic1']").click(function() {
$("input[id='input_file']").click();
});
function readURLImage(input) {
if (typeof headId !== 'undefined' && typeof bodyId !== 'undefined') {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#pic1').attr('src', e.target.result);
$('#pic2').attr('src', e.target.result);
document.getElementById("pic2").style.filter
= "hue-rotate(" + calcShinyHue(headId, bodyId, true, false) + "deg)";
$('#pic3').attr('src', e.target.result);
document.getElementById("pic3").style.filter
= "hue-rotate(" + calcShinyHue(headId, bodyId, false, true) + "deg)";
$('#pic4').attr('src', e.target.result);
document.getElementById("pic4").style.filter
= "hue-rotate(" + calcShinyHue(headId, bodyId, true, true) + "deg)";
};
reader.readAsDataURL(input.files[0]);
}
} else {
alert("Please fill the two text inputs!");
}
}
// Shows the images for the mon and it's shinies
function showShinies(randomHead, randomBody){
buttons = document.getElementsByClassName("button");
for (let i = 0; i < buttons.length; i++) {
buttons[i].disabled = true;
};
document.getElementById("input_file").value = ""; // To make the input work everytime, even if the input image has not changed
if (randomHead) {
headId = getRandomPokeID();
var name = ids[headId][0].toLowerCase();
if (nameFix.includes(name)) {
name = nameException[nameFix.indexOf(name)];
}
document.getElementById("fname1").value = name;
} else {
var name = document.getElementById("fname1").value.toLowerCase();
if (nameException.includes(name)) {
name = nameFix[nameException.indexOf(name)];
}
for (let i = 0; i < ids.length; i++) {
if (name.toUpperCase() == ids[i][0].toUpperCase()) {
headId = i;
break;
}
}
}
if (randomBody) {
bodyId = getRandomPokeID();
var name2 = ids[bodyId][0].toLowerCase();
if (nameFix.includes(name2)) {
name2 = nameException[nameFix.indexOf(name2)];
}
document.getElementById("fname2").value = name2;
} else {
var name2 = document.getElementById("fname2").value.toLowerCase();
if (nameException.includes(name2)) {
name2 = nameFix[nameException.indexOf(name2)];
}
for (let i = 0; i < ids.length; i++) {
if (name2.toUpperCase() == ids[i][0].toUpperCase()) {
bodyId = i;
break;
}
}
}
document.getElementById("dexnumber1").innerHTML = (bodyId + 1) * 420 + headId + 1;
document.getElementById("fusionid1").innerHTML = " (" + (headId + 1) + "." + (bodyId + 1) + ")"
document.getElementById("fusionid1").style.color = "green";
picShinySrc = "https://raw.githubusercontent.com/Aegide/custom-fusion-sprites/main/CustomBattlers/" + (headId+1) + "." + (bodyId+1) + ".png";
if (!doesImageExists(picShinySrc)) {
picShinySrc = "https://raw.githubusercontent.com/Aegide/autogen-fusion-sprites/master/Battlers/" + (headId+1) + "/" + (headId+1) + "." + (bodyId+1) + ".png";
document.getElementById("fusionid1").style.color = "red";
}
document.getElementById("pic1").src = picShinySrc;
window.hueShift = [];
hueShift[0] = calcShinyHue(headId, bodyId, true, false)
let picShiny = document.getElementById("pic2");
picShiny.title = picShiny.alt = "Hue shift " + Math.trunc(hueShift[0]);
hueShift[1] = calcShinyHue(headId, bodyId, false, true)
picShiny = document.getElementById("pic3");
picShiny.title = picShiny.alt = "Hue shift " + Math.trunc(hueShift[1]);
hueShift[2] = calcShinyHue(headId, bodyId, true, true)
picShiny = document.getElementById("pic4");
picShiny.title = picShiny.alt = "Hue shift " + Math.trunc(hueShift[2]);
buttons = document.getElementsByClassName("button");
for (let i = 0; i < buttons.length; i++) {
buttons[i].disabled = false;
};
}
// Calculates the hue of the shiny and returns it it.
// This tries to replicate the calculation made in the game itself.
function calcShinyHue(num1, num2, hasShinyHead, hasShinyBody) {
let offset = 0;
if (hasShinyHead && hasShinyBody && num1 in shinyColorOffsetsDict && num2 in shinyColorOffsetsDict){
offset = shinyColorOffsetsDict[num1] + shinyColorOffsetsDict[num2];
} else if (hasShinyHead && num1 in shinyColorOffsetsDict) {
offset = shinyColorOffsetsDict[num1]
} else if (hasShinyBody && num2 in shinyColorOffsetsDict) {
offset = shinyColorOffsetsDict[num2]
} else {
offset = calcShinyHueDeafult(num1, num2, hasShinyHead, hasShinyBody);
}
return offset;
}
// Calculates the hue of the shiny and returns it it.
// This tries to replicate the calculation made in the game itself.
function calcShinyHueDeafult(num1, num2, hasShinyHead, hasShinyBody) {
let dexOffset = num1 + num2 * 420;
let dexDiff = Math.abs(num2 - num1);
if (hasShinyHead && !hasShinyBody) {
dexOffset = num1;
} else if (!hasShinyHead && hasShinyBody) {
dexOffset = dexDiff > 20 ? num2 : num2 + 40
}
offset = dexOffset + 75;
if (offset > 420) offset /= 360;
if (offset < 40) offset = 40;
if (Math.abs(360 - offset) < 40) offset = 40;
return offset;
}
function fusionAbilities(headAbilities, bodyAbilities) {
var B0 = bodyAbilities[0][0].name;
var H1;
//If there is only ability, pick that one
if(headAbilities.length == 1){
H1 = headAbilities[0][0].name;
}
//If the second ability is a hidden ability, pick the first ability
else if(headAbilities[1][1] == true){
H1 = headAbilities[0][0].name;
}
//Otherwise, actually take the second ability
else{
H1 = headAbilities[1][0].name;
}
return [B0, H1];
}
function fusionHiddenAbilities(headAbilities, bodyAbilities, fusionAbilities){
var headAbility, bodyAbility;
var allAbilities = [];
var maxAbilities = 3;//Pokémons can't have more than 3 abilities
for(var a = 0; a < maxAbilities; a++){
if( a < headAbilities.length){
headAbility = ability = headAbilities[a][0].name;
allAbilities.push(headAbility);
}
if( a < bodyAbilities.length){
bodyAbility = bodyAbilities[a][0].name;
allAbilities.push(bodyAbility);
}
}
hiddenAbilities = allAbilities.filter(n => !fusionAbilities.includes(n));
return hiddenAbilities;
}
function removeDuplicates(list){
return Array.from(new Set(list));
}
function sanitizeAbilityList(abilityList){
if(abilityList.length == 0){
return abilityList;
}
abilityList = removeDuplicates(abilityList);
var listAb1 = "";
for (var i = 0; i < abilityList.length; i++) {
listAb1 = listAb1 + abilityList[i].charAt(0).toUpperCase() + abilityList[i].slice(1) + " / ";
}
listAb1 = listAb1.slice(0, listAb1.length - 1);
listAb1 = listAb1.split("-").join(" ")
listAb1 = listAb1.split(" ")
for (var i = 0, x = listAb1.length; i < x; i++) {
listAb1[i] = listAb1[i][0].toUpperCase() + listAb1[i].substr(1);
}
listAb1 = listAb1.join(" ").slice(0, -2);
return listAb1;
}
//Ability fusion function
function fusAb(mon1, mon2) {
var fabs = [];
var H0 = mon1[0][0].name;
if (mon1.length == 3 && mon1[2][1] == true) {
var H1 = mon1[1][0].name;
var HH = mon1[2][0].name;
}else if (mon1.length == 2 && mon1[1][1] == true) {
var HH = mon1[1][0].name;
} else if (mon1.length == 2 && mon1[1][1] == false){
var H1 = mon1[1][0].name;
}
var B0 = mon2[0][0].name;
if (mon2.length == 3 && mon2[2][1] == true) {
var B1 = mon2[1][0].name;
var BH = mon2[2][0].name;