-
Notifications
You must be signed in to change notification settings - Fork 2
/
game2.js
16207 lines (15960 loc) · 599 KB
/
game2.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
/*
------------------------------------------------------Changelog------------------------------------------------------
27/02/2024
• imported AIcode project
28/02/2024
• created skibidi toilet model (based of skibidi toilet from skibidi toilet)
• reworked collision, bullets, rendering, healthbars
• created github repo
• added laser cannon weapon (based of laser cannon from bloons tower defence 5)
• added standard skibidi toilet enemy
• added fast skibidi toilet enemy
• created cameraman model (based of cameraman from skibidi toilet)
29/02/2024
• added machine gun skibidi toilet enemy
• added laser eye skibidi toilet enemy
• added pistol weapon
• added cannon weapon
• added shotgun weapon
• added rifle weapon
• added sniper weapon
• added plasma cannon weapon (based of plsama blasts from bloons tower defence 6)
• created laser cannon model (based of laser cannon from bloons tower defence 5)
• added upgrades menu
• added economy
01/03/2024
• created heavy sniper model (based of U-Marksman from starblast.io)
2/03/2024
• imported rifle model
• created plasma cannon model
• added railgun
• imported railgun model
• changed sniper bullet to laser
3/03/2024
• some progress
4/03/2024
• more progress
---------------------------------------------------------------------------------------------------------------------
*/
// The support functions that might not be necessary
function isin(a, b) { // check is a in b
for (var i = 0; i < b.length; i += 1) {
if (a == b[i]) {
return true;
}
}
return false;
};
function randchoice(list, remove = false) { // chose 1 from a list and update list
let length = list.length;
let choice = randint(0, length-1);
if (remove) {
let chosen = list.splice(choice, 1);
return [chosen, list];
}
return list[choice];
};
// Randint returns random interger between min and max (both included)
function randint(min, max, notequalto=false) {
if (max - min < 1) {
return min;
}
var gen;
var i = 0;
do {
gen = Math.floor(Math.random() * (max - min + 1)) + min;
i += 1;
if (i >= 100) {
console.log('ERROR: could not generate suitable number');
return gen;
}
} while (notequalto && (gen === min || gen === max));
return gen;
}
/*
function randint(min, max, notequalto=false) {
if (max - min <= 1) {
return min;
}
var gen = Math.floor(Math.random() * (max - min + 1)) + min;
var i = 0; //
while (gen != min && gen != max && notequalto && i < 100) { // loop max 100 times
gen = Math.floor(Math.random() * (max - min + 1)) + min;
i += 1;
console.log('calculating...');
}
if (i >= 100) {
console.log('ERROR: could not generate suitable number');
}
return gen;
};*/
function replacehtml(text) {
document.getElementById("game").innerHTML = text;
};
function addImage(img, x, y, cx, cy, scale, r, absolute, opacity=1) {
var c = document.getElementById('main');
var ctx = c.getContext("2d");
ctx.globalAlpha = opacity;
if (absolute) {
ctx.setTransform(scale, 0, 0, scale, x, y); // sets scale and origin
ctx.rotate(r);
ctx.drawImage(img, -cx, -cy);
} else {
ctx.setTransform(scale, 0, 0, scale, x-player.x+display.x/2, y-player.y+display.y/2); // position relative to player
ctx.rotate(r);
ctx.drawImage(img, -cx, -cy);
}
ctx.globalAlpha = 1.0;
};
function clearCanvas(canvas) {
var c = document.getElementById(canvas);
var ctx = c.getContext("2d");
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, display.x, display.y);
ctx.restore();
};
function drawLine(pos, r, length, style, absolute) {
var c = document.getElementById("main");
var ctx = c.getContext("2d");
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
if (style) {
ctx.strokeStyle = style.colour;
ctx.lineWidth = style.width*data.constants.zoom;
ctx.globalAlpha = style.opacity;
}
ctx.beginPath();
if (absolute) {
ctx.moveTo(pos.x*data.constants.zoom, pos.y*data.constants.zoom);
ctx.lineTo((pos.x + length * Math.cos(r))*data.constants.zoom, (pos.y + length * Math.sin(r))*data.constants.zoom);
} else {
ctx.moveTo((pos.x-player.x)*data.constants.zoom+display.x/2, (pos.y-player.y)*data.constants.zoom+display.y/2);
ctx.lineTo((pos.x-player.x + length * Math.cos(r))*data.constants.zoom+display.x/2, (pos.y-player.y + length * Math.sin(r))*data.constants.zoom+display.y/2);
}
ctx.stroke();
ctx.restore();
};
function renderLine(pos, r, length, style) {
let ns = undefined;
switch (style) {
case 'red':
ns = data.red;
break;
case 'green':
ns = data.green;
break;
case 'blue':
ns = data.blue;
break;
case 'black':
ns = data.black;
break;
case 'white':
default:
ns = data.white;
break;
}
drawLine(pos, r-Math.PI/2, length, ns, false);
};
function getDist(sPos, tPos) {
// Mathematics METHods
var dx = tPos.x - sPos.x;
var dy = tPos.y - sPos.y;
var dist = Math.sqrt(dx*dx+dy*dy);
return dist;
};
function correctAngle(a) {
a = a%(Math.PI*2);
return a;
};
function adjustAngle(a) {
if (a > Math.PI) {
a -= 2*Math.PI;
}
return a;
};
function rotateAngle(r, rTarget, increment) {
if (Math.abs(r) > Math.PI*4 || Math.abs(rTarget) > Math.PI*4) {
throw "Error: You f*cked up the angle thing again...";
console.log(r, rTarget);
r = correctAngle(r);
rTarget = correctAngle(rTarget);
}
if (r == rTarget) {
return correctAngle(r);
}else if (rTarget - r <= Math.PI && rTarget - r > 0) {
if (rTarget - r < increment) {
r = rTarget;
} else {
r += increment;
}
return r;
} else if (r - rTarget < Math.PI && r - rTarget > 0) {
if (r - rTarget < increment) {
r = rTarget;
} else {
r -= increment;
}
return correctAngle(r);
} else {
if (r < rTarget) {
r += Math.PI*2;
} else {
rTarget += Math.PI*2;
}
return correctAngle(rotateAngle(r, rTarget, increment));
}
};
function aim(initial, final) {
if (initial == final) {
return 0;
}
let diff = {x: final.x - initial.x, y: initial.y - final.y};
if (diff.x == 0) {
if (diff.y > 0) {
return 0;
} else {
return Math.PI;
}
} else if (diff.y == 0) {
if (diff.x > 0) {
return Math.PI/2;
} else {
return 3*Math.PI/2;
}
}
let angle = Math.atan(Math.abs(diff.y / diff.x));
if (diff.x > 0 && diff.y > 0) {
return Math.PI/2 - angle;
} else if (diff.x > 0 && diff.y < 0) {
return Math.PI/2 + angle;
} else if (diff.x < 0 && diff.y < 0) {
return 3*Math.PI/2 - angle;
} else {
return 3*Math.PI/2 + angle;
}
};
function offsetPoints(points, offset) {
for (let i = 0; i < points.length; i++){
points[i].x += offset.x;
points[i].y += offset.y;
}
return points;
};
function roman(number) {
if (number <= 0 || number >= 4000) {
var symbols = ['0','1','2','3','4','5','6','7','8','9','¡','£','¢','∞','§','¶','œ','ß','∂','∫','∆','√','µ','†','¥','ø'];
return `${randchoice(symbols)}${randchoice(symbols)}${randchoice(symbols)}`;
}
const romanNumerals = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1
};
let romanNumeral = '';
for (let key in romanNumerals) {
while (number >= romanNumerals[key]) {
romanNumeral += key;
number -= romanNumerals[key];
}
}
return romanNumeral;
};
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
function toColour(colour) {
return `rgba(${colour.r}, ${colour.g}, ${colour.b}, ${colour.a})`;
};
function drawCircle(x, y, radius, fill, stroke, strokeWidth, opacity, absolute) { // draw a circle
var canvas = document.getElementById('main');
var ctx = canvas.getContext("2d");
ctx.resetTransform();
ctx.beginPath();
ctx.globalAlpha = opacity;
if (absolute) {
ctx.arc(x*data.constants.zoom, y*data.constants.zoom, radius*data.constants.zoom, 0, 2 * Math.PI, false);
} else {
ctx.arc((-player.x+x)*data.constants.zoom+display.x/2, (-player.y+y)*data.constants.zoom+display.y/2, radius*data.constants.zoom, 0, 2 * Math.PI, false);
}
if (fill) {
ctx.fillStyle = fill;
ctx.fill();
}
if (stroke) {
ctx.lineWidth = strokeWidth*data.constants.zoom;
ctx.strokeStyle = stroke;
ctx.stroke();
}
ctx.globalAlpha = 1.0;
};
function displaytxt(txt, pos) {
var canvas = document.getElementById("canvasOverlay");
var ctx = canvas.getContext("2d");
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
// Set the font and text color
ctx.font = "20px Verdana";
ctx.fillStyle = "rgba(0, 0, 0, 0.9)";
// Display the points on the canvas
ctx.fillText(txt, pos.x*data.constants.zoom, pos.y*data.constants.zoom);
ctx.stroke();
ctx.restore();
};
function rotatePolygon(point, r) {
let points = JSON.parse(JSON.stringify(point));
for (let i = 0; i < points.length; i++) {
points[i].x = point[i].x * Math.cos(r) - point[i].y * Math.sin(r);
points[i].y = point[i].x * Math.sin(r) + point[i].y * Math.cos(r);
}
return points
};
function drawPolygon(point, offset, r, fill, stroke, absolute, debug=false) {
let points = JSON.parse(JSON.stringify(point));
//console.log(points);
if (points.length < 3) {
throw "Error: Your polygon needs to have at least 3 points dumbass";
}
points = rotatePolygon(points, r)
var canvas = document.getElementById('main');
var ctx = canvas.getContext("2d");
ctx.resetTransform();
ctx.beginPath();
if (absolute) {
ctx.moveTo((points[0].x + offset.x)*data.constants.zoom, (points[0].y + offset.y)*data.constants.zoom);
if (debug) {displaytxt(`(${Math.round((points[0].x + offset.x)*data.constants.zoom)}, ${Math.round((points[0].y + offset.y)*data.constants.zoom)})`, {x: (points[0].x + offset.x)*data.constants.zoom, y: (points[0].y + offset.y)*data.constants.zoom});}
} else {
ctx.moveTo((points[0].x-player.x + offset.x)*data.constants.zoom+display.x/2, (points[0].y-player.y + offset.y)*data.constants.zoom+display.y/2);
if (debug) {displaytxt(`(${Math.round((points[0].x-player.x + offset.x)*data.constants.zoom+display.x/2)}, ${Math.round((points[0].y-player.y + offset.y)*data.constants.zoom+display.y/2)})`, {x: (points[0].x-player.x + offset.x)*data.constants.zoom+display.x/2, y: (points[0].y-player.y + offset.y)*data.constants.zoom+display.y/2});}
//if (debug) {displaytxt(`(${Math.round(points[0].x-player.x+display.x/2 + offset.x)}, ${Math.round(points[0].y-player.y+display.y/2 + offset.y)})`, {x: points[0].x-player.x+display.x/2 + offset.x, y: points[0].y-player.y+display.y/2 + offset.y});}
}
for (let i = 1; i < points.length; i++) {
if (absolute) {
ctx.lineTo((points[i].x + offset.x)*data.constants.zoom, (points[i].y + offset.y)*data.constants.zoom);
if (debug) {displaytxt(`(${Math.round((points[i].x + offset.x)*data.constants.zoom)}, ${Math.round((points[i].y + offset.y)*data.constants.zoom)})`, {x: (points[i].x + offset.x)*data.constants.zoom, y: (points[i].y + offset.y)*data.constants.zoom});}
} else {
ctx.lineTo((points[i].x-player.x + offset.x)*data.constants.zoom+display.x/2, (points[i].y-player.y + offset.y)*data.constants.zoom+display.y/2);
if (debug) {displaytxt(`(${Math.round((points[i].x-player.x + offset.x)*data.constants.zoom+display.x/2)}, ${Math.round((points[i].y-player.y + offset.y)*data.constants.zoom+display.y/2)})`, {x: (points[i].x-player.x + offset.x)*data.constants.zoom+display.x/2, y: (points[i].y-player.y + offset.y)*data.constants.zoom+display.y/2});}
//if (debug) {displaytxt(`(${Math.round(points[i].x-player.x+display.x/2 + offset.x)}, ${Math.round(points[i].y-player.y+display.y/2 + offset.y)})`, {x: points[i].x-player.x+display.x/2 + offset.x, y: points[i].y-player.y+display.y/2 + offset.y});}
}
}
ctx.closePath();
if (fill) {
ctx.fillStyle = fill;
ctx.fill();
}
if (stroke) {
ctx.lineWidth = stroke.width*data.constants.zoom;
ctx.strokeStyle = stroke.colour;
ctx.stroke();
}
};
function rect(coords, size, style, absolute=false, canvas='main') {
var canvas = document.getElementById(canvas);
var ctx = canvas.getContext("2d");
ctx.resetTransform();
ctx.beginPath();
if (absolute) {
ctx.moveTo(coords.x*data.constants.zoom, coords.y*data.constants.zoom);
ctx.lineTo(coords.x*data.constants.zoom, (coords.y+size.y)*data.constants.zoom);
ctx.lineTo((coords.x+size.x)*data.constants.zoom, (coords.y+size.y)*data.constants.zoom);
ctx.lineTo((coords.x+size.x)*data.constants.zoom, coords.y*data.constants.zoom);
} else {
ctx.moveTo((coords.x-player.x)*data.constants.zoom+display.x/2, (coords.y-player.y)*data.constants.zoom+display.y/2);
ctx.lineTo((coords.x-player.x)*data.constants.zoom+display.x/2, (coords.y+size.y-player.y)*data.constants.zoom+display.y/2);
ctx.lineTo((coords.x+size.x-player.x)*data.constants.zoom+display.x/2, (coords.y+size.y-player.y)*data.constants.zoom+display.y/2);
ctx.lineTo((coords.x+size.x-player.x)*data.constants.zoom+display.x/2, (coords.y-player.y)*data.constants.zoom+display.y/2);
}
ctx.closePath();
ctx.fillStyle = style.fill;
ctx.fill();
ctx.lineWidth = style.stroke.width*data.constants.zoom;
ctx.strokeStyle = style.stroke.colour;
ctx.stroke();
}
function renderBar(centre, shift, size, value, increments, padding, spacing, bgStyle, fillStyle) {
let vPadding = {x: padding, y: padding};
let startPos = vMath(centre, vMath(size, 0.5, '*'), '-');
if (shift != 0) {
startPos = vMath(startPos, shift, '+');
}
let blockSize = {x: (size.x - spacing * (increments-1)) / increments, y: size.y};
rect(vMath(startPos, vPadding, '-'), vMath(size, vMath(vPadding, 2, '*'), '+'), bgStyle);
for (let i = 0; i < value; i++) {
rect(startPos, blockSize, fillStyle);
startPos.x += spacing + blockSize.x;
}
};
function drawLight(x, y, radius) {
var canvas = document.getElementById('main');
var ctx = canvas.getContext("2d");
ctx.beginPath();
if (false) {
ctx.arc(x*data.constants.zoom, y*data.constants.zoom, radius*data.constants.zoom, 0, 2 * Math.PI, false);
} else {
ctx.arc((player.x+x)*data.constants.zoom+display.x/2, (player.y+y)*data.constants.zoom+display.y/2, radius*data.constants.zoom, 0, 2 * Math.PI, false);
}
const gradient = ctx.createRadialGradient(x, y, 0, x, y, radius);
gradient.addColorStop(0, 'rgba(255, 255, 255, 1)');
gradient.addColorStop(1, 'rgba(255, 255, 255, 0)');
ctx.fillStyle = gradient;
ctx.fill();
};
function grid(spacing, reference) { // TODO: update colours
for (let i = 0; i >= reference.x - (display.x/2 + spacing*5)/data.constants.zoom; i -= spacing) {
drawLine({x: i, y: reference.y + (display.y/2 + spacing)/data.constants.zoom}, 3*Math.PI/2, (display.y + spacing*2)/data.constants.zoom, {colour:'#000000',width:10,opacity:0.05}, false);
}
for (let i = 0; i <= reference.x + (display.x/2 + spacing*5)/data.constants.zoom; i += spacing) {
drawLine({x: i, y: reference.y + (display.y/2 + spacing)/data.constants.zoom}, 3*Math.PI/2, (display.y + spacing*2)/data.constants.zoom, {colour:'#000000',width:10,opacity:0.05}, false);
}
for (let i = 0; i >= reference.y - (display.y/2 + spacing*5)/data.constants.zoom; i -= spacing) {
drawLine({x: reference.x + (display.x/2 + spacing)/data.constants.zoom, y: i}, Math.PI, (display.x + spacing*2)/data.constants.zoom, {colour:'#000000',width:10,opacity:0.05}, false);
}
for (let i = 0; i <= reference.y + (display.y/2 + spacing*5)/data.constants.zoom; i += spacing) {
drawLine({x: reference.x + (display.x/2 + spacing)/data.constants.zoom, y: i}, Math.PI, (display.x + spacing*2)/data.constants.zoom, {colour:'#000000',width:10,opacity:0.05}, false);
}
};
function renderExplosion(explosion) {
drawCircle(explosion.x-explosion.r, explosion.y-explosion.r, explosion.r, '#fccbb1', '#f7b28d', 0.1, 0.2*explosion.transparancy, false);
drawCircle(explosion.x-explosion.r, explosion.y-explosion.r, explosion.r, false, '#f7b28d', 5, 0.2);
drawCircle(explosion.x-explosion.r, explosion.y-explosion.r, Math.max(explosion.r-20, 0), false, '#fcd8d2', 20, 0.1*explosion.transparancy, false);
drawCircle(explosion.x-explosion.r, explosion.y-explosion.r, Math.max(explosion.r-15, 0), false, '#fcd8d2', 15, 0.1*explosion.transparancy, false);
drawCircle(explosion.x-explosion.r, explosion.y-explosion.r, Math.max(explosion.r-10, 0), false, '#fcd8d2', 10, 0.1*explosion.transparancy, false);
drawCircle(explosion.x-explosion.r, explosion.y-explosion.r, Math.max(explosion.r-5, 0), false, '#fcd8d2', 5, 0.1*explosion.transparancy, false);
drawLight(explosion.x-explosion.r, explosion.y-explosion.r, explosion.r*1.1);
};
function handleExplosion(explosion) {
//console.log(explosion);
if (explosion.r >= explosion.maxR) {
explosion.transparancy *= 0.75;
explosion.r *= 1.2;
explosion.active = false;
}
if (explosion.r < explosion.maxR) {
explosion.active = true;
explosion.r += explosion.expandSpeed;
if (explosion.r > explosion.maxR) {
explosion.r = explosion.maxR;
}
}
if (explosion.transparancy > 0.25) {
return explosion;
} return false;
};
function normalDistribution(mean, sDiv) {
let u = 0;
let v = 0;
while (u === 0) u = Math.random();
while (v === 0) v = Math.random();
let z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2 * Math.PI * v);
return mean + z * sDiv;
};
function raySegmentIntersection(pointIn, segmentIn) {
let point = vMath(pointIn, 1.1, 'multiply');
let segment = {start: vMath(segmentIn.start, 1.1, 'multiply'), end: vMath(segmentIn.end, 1.1, 'multiply')};
let A1 = adjustAngle(correctAngle(aim(point, segment.start)));
let A2 = adjustAngle(correctAngle(aim(point, segment.end)));
if ((A1 >= 0 && A2 <= 0 || A2 >= 0 && A1 <= 0) && Math.abs(A1) + Math.abs(A2) < Math.PI) {
return true;
}
return false;
};
function pointInPolygon(point, polygon) {
let inside = false;
let cnt = 0;
if (raySegmentIntersection(point, {start: polygon[0], end: polygon[polygon.length-1]})) {
inside = !inside;
cnt++;
}
for (let i = 0; i < polygon.length-1; i++) {
if (raySegmentIntersection(point, {start: polygon[i], end: polygon[i+1]})) {
inside = !inside;
cnt++;
}
}
return inside;
};
function vMath(v1, v2, mode) {
switch (mode) {
case '||':
case 'magnitude':
return Math.sqrt(v1.x**2+v1.y**2);
case '+':
case 'addition':
case 'add':
return {x: v1.x+v2.x, y: v1.y+v2.y};
case '-':
case 'subtraction':
case 'subtract':
return {x: v1.x-v2.x, y: v1.y-v2.y};
case '*':
case 'x':
case 'scalar multiplication':
case 'multiplication':
case 'multiply': // v2 is now a scalar
return {x: v1.x*v2, y: v1.y*v2};
case '/':
case 'division':
case 'divide': // v2 is now a scalar
return {x: v1.x/v2, y: v1.y/v2};
case '•':
case '.':
case 'dot product':
return v1.x * v2.x + v1.y * v2.y;
case 'cross product': // chat gpt, I believe in you (I doubt this is correct)
return v1.x * v2.y - v1.y * v2.x;
case 'projection':
case 'vector resolute':
return vMath(v2, vMath(v1, v2, '.')/vMath(v2, null, '||')**2, 'x');
default:
throw 'what are you trying to do to to that poor vector?';
}
};
function toComponent(m, r) {
return {x: m * Math.sin(r), y: -m * Math.cos(r)};
};
function toPol(i, j) {
return {m: Math.sqrt(i**2+j**2), r: aim({x: 0, y: 0}, {x: i, y: j})};
};
function circleToPolygon(pos, r, sides) {
let step = Math.PI*2/sides;
let polygon = [];
for(let i = 0; i < sides; i++) {
polygon.push(vMath(toComponent(r, step*i),pos,'add'));
}
return polygon;
};
function pressKey(key) {
orders.push({id: key, value: true});
}
function releaseKey(key) {
orders.push({id: key, value: false});
}
const noAI = `
let orders = [];
return orders;
`;
const basicTurretAI = `
let orders = [];
let target = entities[0];
orders.push({id: 'aim', value: {x: target.x, y: target.y}});
orders.push({id: 'click', value: true});
return orders;
`;
const basicShootingAI = `
let orders = [];
let target = entities[0];
let nr = adjustAngle(correctAngle(aim(unit, target)-unit.r));
if (Math.abs(nr) > Math.PI/48) {
if (nr > 0) {
orders.push({id: 'd', value: true});
orders.push({id: 'a', value: false});
} else {
orders.push({id: 'a', value: true});
orders.push({id: 'd', value: false});
}
}
let dist = getDist(unit, target);
if (Math.abs(nr) < Math.PI/6 && dist > 750) {
orders.push({id: 'w', value: true});
orders.push({id: 's', value: false});
}
if (dist < 750) {
orders.push({id: 's', value: true});
orders.push({id: 'w', value: false});
}
if ((Math.abs(aim(unit, entities[0])) < Math.PI || Math.PI*2-Math.abs(aim(unit, entities[0])) < Math.PI) && dist < 1250) {
orders.push({id: 'click', value: true});
} else {
orders.push({id: 'click', value: false});
}
return orders;
`;
const ramAI = `
let orders = [];
let target = entities[0];
orders.push({id: 'aim', value: {x: target.x, y: target.y}});
orders.push({id: 'click', value: true});
let nr = adjustAngle(correctAngle(aim(unit, target)-unit.r));
if (Math.abs(nr) > Math.PI/48) {
if (nr > 0) {
orders.push({id: 'd', value: true});
orders.push({id: 'a', value: false});
} else {
orders.push({id: 'a', value: true});
orders.push({id: 'd', value: false});
}
}
let dist = getDist(unit, target);
if (Math.abs(nr) < Math.PI/6 && dist > 200) {
orders.push({id: 'w', value: true});
orders.push({id: 's', value: false});
}if (dist < 200) {
orders.push({id: 's', value: true});
orders.push({id: 'w', value: false});
}
if ((Math.abs(aim(unit, entities[0])) < Math.PI || Math.PI*2-Math.abs(aim(unit, entities[0])) < Math.PI) && dist < 400) {
orders.push({id: 'click', value: true});
} else {
orders.push({id: 'click', value: false});
}
return orders;
`;
const basicMovingTargetAI = `
let orders = [];
let target = entities[0];
if (unit.x > 1500) {
orders.push({id: 'a', value: true});
orders.push({id: 'd', value: false});
} else if (unit.x < -1500) {
orders.push({id: 'a', value: false});
orders.push({id: 'd', value: true});
}
return orders;
`;
const betterTurretAI = `
let orders = [];
let target = undefined;
for (let i = 0; i < entities.length; i++) {
if (entities[i].team != unit.team) {
if (target == undefined || getDist(unit, entities[i]) < getDist(unit, target)) {
target = entities[i];
}
}
}
if (target) {
orders.push({id: 'aim', value: {x: target.x, y: target.y}});
orders.push({id: 'click', value: true});
} else {
orders.push({id: 'aim', value: {x: unit.x, y: unit.y}});
orders.push({id: 'click', value: false});
}
return orders;
`;
const shieldAI = `
let orders = [];
let target = undefined;
for (let i = 0; i < entities.length; i++) {
if (entities[i].team != unit.team) {
if (target == undefined || getDist(unit, entities[i]) < getDist(unit, target)) {
target = entities[i];
}
}
}
if (target) {
let caim = toPol(target.x-unit.x, target.y-unit.y);
caim.r -= Math.PI/2;
let naim = vMath(toComponent(caim.m, caim.r), unit, '+');
orders.push({id: 'aim', value: naim});
} else {
orders.push({id: 'aim', value: {x: unit.x, y: unit.y}});
}
return orders;
`;
// Aim assist program
const advancedTurretAI = `
let orders = [];
let target = undefined;
for (let i = 0; i < entities.length; i++) {
if (entities[i].team != unit.team) {
if (target == undefined || getDist(unit, entities[i]) < getDist(unit, target)) {
target = entities[i];
}
}
}
if (target) {
let dist = getDist(unit, target);
const bulletVelocity=30;
let time = dist/bulletVelocity;
let enemymotionx = time*target.vx;
let enemymotiony = time*target.vy;
let playermotionx = unit.v*Math.cos(unit.r);
let playermotiony = unit.v*Math.sin(unit.r);
drawLine(unit, aim(unit, {x: target.x+enemymotionx+playermotionx, y: target.y+enemymotiony+playermotiony})-Math.PI/2, 5000, data.red.stroke, false);
orders.push({id: 'aim', value: {x: target.x+enemymotionx+playermotionx, y: target.y+enemymotiony+playermotiony}});
if (dist < 3500) {
orders.push({id: 'click', value: true});
} else {
orders.push({id: 'click', value: false});
}
} else {
orders.push({id: 'aim', value: {x: unit.x, y: unit.y}});
orders.push({id: 'click', value: false});
}
return orders;
`;
const sniperTurretAI = `
let orders = [];
let target = undefined;
for (let i = 0; i < entities.length; i++) {
if (entities[i].team != unit.team) {
if (target == undefined || getDist(unit, entities[i]) < getDist(unit, target)) {
target = entities[i];
}
}
}
if (target) {
let dist = getDist(unit, target);
const bulletVelocity=50;
let time = dist/bulletVelocity;
let enemymotionx = time*target.vx;
let enemymotiony = time*target.vy;
let playermotionx = unit.v*Math.cos(unit.r);
let playermotiony = unit.v*Math.sin(unit.r);
drawLine(unit, aim(unit, {x: target.x+enemymotionx+playermotionx, y: target.y+enemymotiony+playermotiony})-Math.PI/2, 5000, data.red.stroke, false);
orders.push({id: 'aim', value: {x: target.x+enemymotionx+playermotionx, y: target.y+enemymotiony+playermotiony}});
if (dist < 3500) {
orders.push({id: 'click', value: true});
} else {
orders.push({id: 'click', value: false});
}
} else {
orders.push({id: 'aim', value: {x: unit.x, y: unit.y}});
orders.push({id: 'click', value: false});
}
console.log(orders);
return orders;
`;
const aimAssistAI = `
let orders = [];
let target = undefined;
for (let i = 0; i < entities.length; i++) {
if (entities[i].team != unit.team) {
if (target == undefined || getDist(unit, entities[i]) < getDist(unit, target)) {
target = entities[i];
}
}
}
if (target) {
let dist = getDist(unit, target);
const bulletVelocity=10;
let time = dist/bulletVelocity;
let enemymotionx = time*target.vx;
let enemymotiony = time*target.vy;
let playermotionx = unit.v*Math.cos(unit.r);
let playermotiony = unit.v*Math.sin(unit.r);
drawLine(unit, aim(unit, {x: target.x+enemymotionx+playermotionx, y: target.y+enemymotiony+playermotiony})-Math.PI/2, 5000, data.green.stroke, false);
orders.push({id: 'aim', value: {x: target.x+enemymotionx+playermotionx, y: target.y+enemymotiony+playermotiony}});
if (dist < 5000) {
orders.push({id: 'click', value: true});
} else {
orders.push({id: 'click', value: false});
}
} else {
orders.push({id: 'aim', value: {x: unit.x, y: unit.y}});
orders.push({id: 'click', value: false});
}
return orders;
`;
const leftArmAssistAI = `
let orders = [];
let target = undefined;
for (let i = 0; i < entities.length; i++) {
if (entities[i].team != unit.team) {
if (target == undefined || getDist(unit, entities[i]) < getDist(unit, target)) {
target = entities[i];
}
}
}
if (target) {
let dist = getDist(unit, target);
const bulletVelocity=30;
let offset = toPol(-100, 0);
offset.r += aim(unit, {x: target.x, y: target.y});
offset = toComponent(offset.m, offset.r);
let newpos = vMath(unit, offset, '+');
let time = dist/bulletVelocity;
let enemymotionx = time*target.vx;
let enemymotiony = time*target.vy;
let playermotionx = unit.v*Math.cos(unit.r);
let playermotiony = unit.v*Math.sin(unit.r);
let aimr = aim(newpos, {x: target.x+enemymotionx+playermotionx, y: target.y+enemymotiony+playermotiony});
renderLine(unit, aimr, 5000, 'green');
orders.push({id: 'aim', value: vMath(unit, toComponent(dist, aimr), '+')});
if (dist < 5000) {
orders.push({id: 'click', value: true});
} else {
orders.push({id: 'click', value: false});
}
} else {
orders.push({id: 'aim', value: {x: unit.x, y: unit.y}});
orders.push({id: 'click', value: false});
}
return orders;
`;
const rightArmAssistAI = `
let orders = [];
let target = undefined;
for (let i = 0; i < entities.length; i++) {
if (entities[i].team != unit.team) {
if (target == undefined || getDist(unit, entities[i]) < getDist(unit, target)) {
target = entities[i];
}
}
}
if (target) {
let dist = getDist(unit, target);
const bulletVelocity=30;
let offset = toPol(100, 0);
offset.r += aim(unit, {x: target.x, y: target.y});
offset = toComponent(offset.m, offset.r);
let newpos = vMath(unit, offset, '+');
let time = dist/bulletVelocity;
let enemymotionx = time*target.vx;
let enemymotiony = time*target.vy;
let playermotionx = unit.v*Math.cos(unit.r);
let playermotiony = unit.v*Math.sin(unit.r);
let aimr = aim(newpos, {x: target.x+enemymotionx+playermotionx, y: target.y+enemymotiony+playermotiony});
renderLine(unit, aimr, 5000, 'green');
orders.push({id: 'aim', value: vMath(unit, toComponent(dist, aimr), '+')});
if (dist < 5000) {
orders.push({id: 'click', value: true});
} else {
orders.push({id: 'click', value: false});
}
} else {
orders.push({id: 'aim', value: {x: unit.x, y: unit.y}});
orders.push({id: 'click', value: false});
}
return orders;
`;
const scriptMovementI = `
let orders = [];
orders.push({id: 'w', value: true});
return orders;
`;
const scriptMovementII = `
let orders = [];
if (unit.y > -2000) {
orders.push({id: 'w', value: true});
} else {
orders.push({id: 'w', value: false});
orders.push({id: 'd', value: true});
}
return orders;
`;
const scriptMovementIV = `
let orders = [];
let aDist = 200;
let target = undefined;
let minDist = 9999999;
let minR = 9999999;
for (let i = 1; i < entities.length; i++) {
let dist = getDist(unit, entities[i]);
let r = aim(unit, entities[i]);
if (dist < minDist || (dist < 475 && Math.abs(r - unit.r) < minR)) {
target = entities[i];
minDist = dist;
minR = r;
}
}
if (!target) {
target = checkpoint;
aDist = 50;
}
let xdist = target.x - unit.x;
let ydist = target.y - unit.y;
let dist = getDist(unit, target);
if (Math.abs(dist) > aDist) {
if (xdist > aDist) {
orders.push({id: 'd', value: true});
orders.push({id: 'a', value: false});
} else if (xdist < -aDist) {
orders.push({id: 'a', value: true});
orders.push({id: 'd', value: false});
} else {
orders.push({id: 'a', value: false});
orders.push({id: 'd', value: false});
}
if (ydist > aDist) {
orders.push({id: 's', value: true});
orders.push({id: 'w', value: false});
} else if (ydist < -aDist) {
orders.push({id: 'w', value: true});
orders.push({id: 's', value: false});
} else {
orders.push({id: 'w', value: false});
orders.push({id: 's', value: false});
}
} else if (Math.abs(dist) < 125) {
console
if (xdist > 0) {
orders.push({id: 'a', value: true});
orders.push({id: 'd', value: false});
} else {
orders.push({id: 'd', value: true});
orders.push({id: 'a', value: false});
}
if (ydist > 0) {
orders.push({id: 'w', value: true});
orders.push({id: 's', value: false});
} else {
orders.push({id: 's', value: true});
orders.push({id: 'w', value: false});
}
}
let offset = toPol(100, 0);
offset.r += aim(unit, {x: target.x, y: target.y});
offset = toComponent(offset.m, offset.r);
let newpos = vMath(unit, offset, '+');
let aimr = aim(newpos, {x: target.x, y: target.y});
orders.push({id: 'aim', value: vMath(vMath(unit, toComponent(dist, aimr), '+'), {x: randint(0,20)-10, y: randint(0,20)-10}, '+')});
if (dist < 600) {
orders.push({id: 'click', value: true});
} else {
orders.push({id: 'click', value: false});
}
return orders;
`;
const scriptAimingI = `
let orders = [];
let target = entities[1];
if (target) {
let dist = getDist(unit, target);
let offset = toPol(100, 0);
offset.r += aim(unit, {x: target.x, y: target.y});
offset = toComponent(offset.m, offset.r);