-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.nbis
More file actions
922 lines (764 loc) · 33.6 KB
/
Copy pathsim.nbis
File metadata and controls
922 lines (764 loc) · 33.6 KB
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
from si import *
import random
import math
import graphics
import time
from math import roundn
from constants import pi
from consts import *
from structs import *
from autopilot import autopilot
# Status
started = HEADLESS
crashed = false
landed = false
score = 0
landing_bonus = 0
last_time = time.now() -> s
actual_fps = 60.0 s^(-1)
idle_time = 0.0 s
fade_time = 0.0 s
crash_time = 0.0 s
# More params
wind_time = 0.0 s
wind_speed = 0.0 m/s
auto_mode = CLI_ARGS.contains("--autopilot")
# Colors
sky_colors = ["#4a7faa", "#4d89b8", "#5293c6", "#5a9fd4", "#65aee0", "#74baeb", "#87c8f4", "#9bd8ff"]
rocket_colors = ["#9a9a9a", "#bcbcbc", "#dadada", "#ededed", "#f5f5f5", "#ffffff"]
fuel_colors = ["#c0392b", "#e74c3c", "#e67e22", "#f39c12", "#f1c40f", "#f7dc6f", "#abebc6", "#82e0aa", "#58d68d", "#2ecc71"]
exhaust_colors = ["#2a2a2a11","#3a3a3a22","#4a4a4a33","#6a6a6a55","#8a8a8a77","#b0a8a099","#d8c0b0bb","#f2d0c0cc","#ffd8cadd","#ffe5dfee"]
fire_colors = ["#333333", "#ff2200", "#ff6600", "#ffcc00", "#333333", "#ff2200", "#ff6600", "#ffcc00", "#333333", "#ff2200", "#ff6600", "#ffcc00", "#333333", "#ff2200", "#ff6600", "#ffcc00"]
# Globals
rckt: {Rocket} = Rocket()
platform: {Platform} = Platform()
particles: List[{Particle}] = []
# Initialize graphics
if not HEADLESS then {
graphics.init(SCREEN_WIDTH, SCREEN_HEIGHT)
graphics.set_title("Falcon 9 Landing Simulator")
graphics.set_font("Fira Code")
}
reset!() = {
global rckt, platform, particles, started, crashed, landed, idle_time, wind_time, wind_speed, crash_time, score, landing_bonus
rckt = Rocket()
platform = Platform()
particles = []
started = false
crashed = false
landed = false
score = 0
landing_bonus = 0
idle_time = 0.0 s
wind_time = 0.0 s
wind_speed = 0.0 m/s
crash_time = 0.0 s
}
# ==============================
update_physics!() = {
global rckt, crashed, landed, wind_speed, DT
if crashed or landed then {return;}
rckt.mass = DRY_MASS + rckt.fuel
moi = (1.0 / 12.0) * rckt.mass * (3 * ROCKET_RADIUS^2 + ROCKET_HEIGHT^2)
force_x = 0.0 N
force_y = 0.0 N
torque = 0.0 N*m
# -- Gravity --
force_y += rckt.mass * GRAVITY
# -- Aerodynamic Drag --
v_air_x = rckt.vx - wind_speed
v_air_y = rckt.vy
v_mag = (v_air_x^2 + v_air_y^2)^0.5
if v_mag > 0.1 m/s then {
dir_x = math.sin(rckt.angle)
dir_y = -math.cos(rckt.angle)
cos_alpha = math.abs(
dir_x * (v_air_x / v_mag) + dir_y * (v_air_y / v_mag)
)
sin_alpha = math.sqrt(1 - cos_alpha^2)
area_base = pi * ROCKET_RADIUS^2
area_side = 2 * ROCKET_RADIUS * ROCKET_HEIGHT
effective_area = (area_base * cos_alpha * DRAG_COEFF_BASE) + (
area_side * sin_alpha * DRAG_COEFF_SIDE
)
drag_force = 0.5 * AIR_DENSITY * (v_mag^2) * effective_area
force_x -= drag_force * (v_air_x / v_mag)
force_y -= drag_force * (v_air_y / v_mag)
}
# -- Engine Thrust --
if rckt.engine_on and rckt.fuel > 0 then {
force_x += ENGINE_THRUST * math.sin(rckt.angle)
force_y += ENGINE_THRUST * -math.cos(rckt.angle)
mass_flow_rate = ENGINE_THRUST / (ISP * GRAVITY)
rckt.fuel -= mass_flow_rate * DT
if rckt.fuel < 0 kg then {rckt.fuel = 0 kg}
}
# -- RCS (Reaction Control System / Grid Fins) --
if rckt.rcs_left then {
torque -= RCS_TORQUE
}
if rckt.rcs_right then {
torque += RCS_TORQUE
}
damping_coeff = 10000.0 kg*m
torque -= rckt.omega * (v_mag * damping_coeff)
# Semi-Implicit Euler Integration
accel_x = force_x / rckt.mass
accel_y = force_y / rckt.mass
rckt.vx += accel_x * DT
rckt.vy += accel_y * DT
rckt.x += rckt.vx * DT
rckt.y += rckt.vy * DT
alpha = torque / moi
rckt.omega += alpha * DT
rckt.angle += rckt.omega * DT
}
score_landing!() = {
global rckt, platform, score, landing_bonus
pad_centre = platform.x + platform.w / 2
dist_from_center = math.abs(rckt.x - pad_centre)
time_score = math.max(0.0, 1.0 - (wind_time / 15s)^3)
v_speed_score = math.max(0.0, 1.0 - math.abs(rckt.vy) / MAX_LANDING_SPEED_Y)
h_speed_score = math.max(0.0, 1.0 - math.abs(rckt.vx) / MAX_LANDING_SPEED_X)
angle_score = math.max(0.0, 1.0 - math.abs(rckt.angle) / MAX_LANDING_ANGLE)
omega_score = math.max(0.0, 1.0 - math.abs(rckt.omega) / MAX_ROTATION_SPEED)
pos_score = math.max(0.0, 1.0 - dist_from_center / (platform.w / 2))
fuel_score = rckt.fuel / INITIAL_FUEL
landing = (v_speed_score * 7) + (h_speed_score * 7) + (angle_score * 6) + (pos_score * 5) + (omega_score * 5)
rest = (time_score * 50) + (fuel_score * 30)
score = math.round(rest * 10)
landing_bonus = math.round(landing * 10)
}
# ==============================
update_particles!() = {
global rckt, platform, particles, DT
new_particles: List[{Particle}] = []
for p in particles do {
# Apply drag to simulate air resistance slowing the particles
p.vx *= (1.0 - 1.5 * (DT->1))
p.vy *= (1.0 - 1.5 * (DT->1))
# Apply a slight gravity to smoke/exhaust
p.vy += (GRAVITY * 0.1) * DT
new_x = p.x + p.vx * DT
new_y = p.y + p.vy * DT
# Reflection / Collisions with the deck and water
if new_y >= platform.y then {
on_pad = (platform.x <= new_x <= platform.x + platform.w)
if on_pad and new_y < platform.y + 2m then {
# Platform
new_y = platform.y
} else if new_y >= platform.y + 2m then {
# Water
new_y = platform.y + 2m
}
p.vy = -p.vy * 0.8
}
p.x = new_x
p.y = new_y
p.life -= DT
if p.life >= 0s then {
new_particles.append(p)
}
}
particles = new_particles
}
update_time!(action_received: Bool) = {
global last_time, actual_fps, idle_time, fade_time, crashed, crash_time, landed
# Calculate real delta time and FPS
current_time = time.now() -> s
real_dt = current_time - last_time
last_time = current_time
if real_dt > 0 then {actual_fps = (1/real_dt)}
if action_received then {
idle_time = 0.0 s
} else {
idle_time += real_dt
}
if fade_time > 0.0 s then {
fade_time = math.max(0.0 s, fade_time - real_dt)
}
if crashed or landed then {
crash_time += DT
}
}
# ==============================
rotate_polygon!(cx: Length, cy: Length, points: List[Length], rad: Angle): List[Length] = {
rotated: List[Length] = []
for i in 0..(points.len())..2 do {
px = points[i]
py = points[i + 1]
rotated.append(cx + px * math.cos(rad) - py * math.sin(rad))
rotated.append(cy + px * math.sin(rad) + py * math.cos(rad))
}
return rotated
}
fade_alpha_hex!(t: Time, fade_dur: Time): Str = {
ratio = math.max(0.0, math.min(1.0, (t / fade_dur)))
alpha = math.floor(ratio * 255.0) -> Int
hex_digits = "0123456789abcdef"
hi = math.floor(alpha / 16) -> Int
lo = alpha - hi * 16
return hex_digits[hi] + hex_digits[lo]
}
# ==============================
# Shared rocket renderer.
# ox, oy: world-space origin (metres) for the rocket centre.
# angle: rotation in radians.
# engine_on, rcs_left, rcs_right: effect flags.
# leg_ext: 0.0 = retracted, 1.0 = fully deployed.
# scale_factor: multiplies all metric lengths (1.0 = normal game size).
# For the HUD preview set this to a small value like 0.15.
draw_rocket!(ox: Length, oy: Length, angle: Angle,
engine_on: Bool, rcs_left: Bool, rcs_right: Bool,
leg_ext: Num, scale_factor: Num) = {
sf = scale_factor
w_r = ROCKET_RADIUS * sf
h_r = ROCKET_HEIGHT / 2 * sf
body_bot_y = (h_r - 6m * sf)
skirt_bot_y = (h_r - 1m * sf)
nozzle_bot_y = (h_r + 2m * sf)
is_h = 5m * sf
# RCS Plumes
rcs_offset_y = -h_r + 2.5m * sf
if rcs_left then {
rcs_len = random.uniform(3.0 m, 6.0 m) * sf
rcs_pts = rotate_polygon(ox, oy, [w_r, rcs_offset_y, w_r+rcs_len, rcs_offset_y-2m * sf, w_r+(rcs_len*0.8), rcs_offset_y+1m * sf], angle)
graphics.polygon(rcs_pts, "#ffffffcc")
}
if rcs_right then {
rcs_len = random.uniform(3.0 m, 6.0 m) * sf
rcs_pts = rotate_polygon(ox, oy, [-w_r, rcs_offset_y, -w_r-rcs_len, rcs_offset_y-2m * sf, -w_r-(rcs_len*0.8), rcs_offset_y+1m * sf], angle)
graphics.polygon(rcs_pts, "#ffffffcc")
}
# Interstage (Top)
is_pts = rotate_polygon(ox, oy, [-w_r, -h_r, w_r, -h_r, w_r, -h_r + is_h, -w_r, -h_r + is_h], angle)
graphics.polygon(is_pts, "#151515")
# Grid Fins
for s in [-1, 1] do {
gf = rotate_polygon(ox, oy, [s*w_r, -h_r+1.2m * sf, s*(w_r+1.2m * sf), -h_r+1.8m * sf, s*(w_r+1.2m * sf), -h_r+3.8m * sf, s*w_r, -h_r+4.4m * sf], angle)
graphics.polygon(gf, "#252525")
}
# Main Body
steps = rocket_colors.len()
strip_w = (2.0 * w_r) / steps
for i in 0..steps do {
lx = -w_r + (i * strip_w)
rx = lx + strip_w
b_pts = rotate_polygon(ox, oy, [lx, -h_r + is_h, rx, -h_r + is_h, rx, body_bot_y, lx, body_bot_y], angle)
graphics.polygon(b_pts, rocket_colors[i])
}
# Nozzle & Engine Section
skirt_pts_dark = rotate_polygon(ox, oy, [-w_r, body_bot_y, 0m, body_bot_y, 0m, skirt_bot_y, -w_r * 1.12, skirt_bot_y], angle)
skirt_pts_light = rotate_polygon(ox, oy, [0.0m, body_bot_y, w_r, body_bot_y, w_r * 1.12, skirt_bot_y, 0m, skirt_bot_y], angle)
graphics.polygon(skirt_pts_dark, "#1a1a1a")
graphics.polygon(skirt_pts_light, "#2e2e2e")
nozzle_pts_dark = rotate_polygon(ox, oy, [-w_r * 0.55, skirt_bot_y, 0m, skirt_bot_y, 0m, nozzle_bot_y, -w_r * 0.78, nozzle_bot_y], angle)
nozzle_pts_light = rotate_polygon(ox, oy, [0.0m, skirt_bot_y, w_r * 0.55, skirt_bot_y, w_r * 0.78, nozzle_bot_y, 0m, nozzle_bot_y], angle)
graphics.polygon(nozzle_pts_dark, "#2a2a2a")
graphics.polygon(nozzle_pts_light, "#3d3d3d")
nozzle_inner = rotate_polygon(ox, oy, [-w_r * 0.42, nozzle_bot_y - 0.5m * sf, w_r * 0.42, nozzle_bot_y - 0.5m * sf, w_r * 0.52, nozzle_bot_y, -w_r * 0.52, nozzle_bot_y], angle)
graphics.polygon(nozzle_inner, "#111111")
# Landing Legs
if leg_ext > 0.0 then {
for side in [-1, 1] do {
root_x = side * w_r
root_y = body_bot_y + 1m * sf
tip_x = side * (w_r + (8.5m * sf * leg_ext))
tip_y = skirt_bot_y + (3.5m * sf * leg_ext)
leg_p = rotate_polygon(ox, oy, [root_x, root_y, tip_x, tip_y], angle)
graphics.line(leg_p[0], leg_p[1], leg_p[2], leg_p[3], thickness=3.5, color="#111111")
leg_a = rotate_polygon(ox, oy, [side * w_r, skirt_bot_y - 1m * sf, tip_x, tip_y], angle)
graphics.line(leg_a[0], leg_a[1], leg_a[2], leg_a[3], thickness=1.5, color="#333333")
pad = rotate_polygon(ox, oy, [tip_x - 1.2m * sf, tip_y, tip_x + 1.2m * sf, tip_y, tip_x + 1.0m * sf, tip_y + 0.4m * sf, tip_x - 1.0m * sf, tip_y + 0.4m * sf], angle)
graphics.polygon(pad, "#050505")
}
}
# Engine Flame
if engine_on then {
flame_h = random.uniform(17.0 m, 32.0 m) * sf
flicker_w = random.uniform(0.8, 1.1)
f_out = rotate_polygon(ox, oy, [-w_r*flicker_w, h_r, w_r*flicker_w, h_r, 0m, h_r+flame_h], angle)
graphics.polygon(f_out, "#ff66005b")
f_mid = rotate_polygon(ox, oy, [-w_r*0.6, h_r, w_r*0.6, h_r, 0m, h_r+(flame_h*0.7)], angle)
graphics.polygon(f_mid, "#ffaa0080")
f_in = rotate_polygon(ox, oy, [-w_r*0.3, h_r, w_r*0.3, h_r, 0m, h_r+(flame_h*0.4)], angle)
graphics.polygon(f_in, "#ffffffa8")
}
}
# ==============================
# Helper to render LCD-style bold numbers manually (not as text)
draw_digit!(d: Num, cx: Num, cy: Num, w: Num, h: Num, col: Str) = {
t = math.min(w, h) * 0.25
if d == 1 then {
graphics.rect(cx - t/2, cy - h/2, t, h, col)
graphics.polygon([cx - t/2, cy - h/2, cx - t/2 - t, cy - h/2 + t, cx - t/2, cy - h/2 + t], col)
} else {
if d == 2 or d == 3 then graphics.rect(cx - w/2, cy - h/2, w, t, col) # top
if d == 2 or d == 3 or d == 4 then graphics.rect(cx + w/2 - t, cy - h/2, t, h/2 + t/2, col) # top-right
if d == 3 or d == 4 then graphics.rect(cx + w/2 - t, cy - t/2, t, h/2 + t/2, col) # bot-right
if d == 2 or d == 3 then graphics.rect(cx - w/2, cy + h/2 - t, w, t, col) # bot
if d == 2 then graphics.rect(cx - w/2, cy - t/2, t, h/2 + t/2, col) # bot-left
if d == 4 then graphics.rect(cx - w/2, cy - h/2, t, h/2 + t/2, col) # top-left
if d == 2 or d == 3 or d == 4 then graphics.rect(cx - w/2, cy - t/2, w, t, col) # mid
}
}
# ==============================
# Key button with an arrow drawn as a filled triangle or manual digits.
# direction: "up" | "down" | "left" | "right" | "" (no arrow, use label text)
draw_key!(x: Num, y: Num, w: Num, h: Num, direction: Str, label: Str) = {
# shadow
graphics.rect(x+2, y+3, w, h, "#00000055")
# key body
graphics.rect(x, y, w, h, "#1e2a38")
# top highlight edge
graphics.rect(x, y, w, 2, "#3a5068")
mx = x + w / 2.0
my = y + h / 2.0
aw = w * 0.38 # half-width of arrow base
ah = h * 0.38 # height of arrow
if direction == "up" then graphics.polygon([mx, my - ah, mx - aw, my + ah*0.6, mx + aw, my + ah*0.6], "#c8d8e8")
if direction == "down" then graphics.polygon([mx, my + ah, mx - aw, my - ah*0.6, mx + aw, my - ah*0.6], "#c8d8e8")
if direction == "left" then graphics.polygon([mx - ah, my, mx + ah*0.6, my - aw, mx + ah*0.6, my + aw], "#c8d8e8")
if direction == "right" then graphics.polygon([mx + ah, my, mx - ah*0.6, my - aw, mx - ah*0.6, my + aw], "#c8d8e8")
if direction == "" then {
if label == "3" or label == "4" then {
draw_digit(label -> Int, mx, my, w * 0.35, h * 0.55, "#c8d8e8")
} else {
lx = x + w/2 - (label.len() * 5)
ly = y + h/2 - 8
graphics.text(lx, ly, label, size=15, color="#c8d8e8")
}
}
}
# ==============================
draw_telemetry!(alt: Length) = {
global rckt, landed, started, crashed, wind_speed, actual_fps, score, landing_bonus, auto_mode
if not started then return;
sw = SCREEN_WIDTH -> 1
sh = SCREEN_HEIGHT -> 1
# HUD panel: 22% of screen width, rows proportional to screen height
hud_w = sw * 0.29
hud_row_h = sh * 0.026
hud_rows = 9
hud_h = hud_row_h * hud_rows + 12.0
hud_font = math.max(10, (hud_row_h * 0.72) -> Int)
fuel_ratio = rckt.fuel / INITIAL_FUEL
color_idx = math.floor(fuel_ratio * 9.99)
graphics.rect(0, 0, hud_w, hud_h, "#050b14cc")
graphics.rect(0, 0, hud_w * fuel_ratio, 4, fuel_colors[color_idx])
angle_ = math.round(math.rad_to_deg(rckt.angle) -> °)
fuel_liters = rckt.fuel / FUEL_DENSITY -> 1
metrics = [
"ALTITUDE: " + (roundn(alt) -> Str),
"V-SPEED: " + (roundn(-rckt.vy, 1) -> Str),
"H-SPEED: " + (roundn(rckt.vx, 1) -> Str),
"ANGLE: " + (angle_ -> Str),
"FUEL: " + (roundn(fuel_liters) -> Str) + " L",
"WIND: " + (roundn(wind_speed, 1) -> Str),
"FPS: " + (math.round(actual_fps) -> 1 -> Str),
"SCORE: " + (score -> Str),
"LANDING BONUS: " + "+" + (landing_bonus -> Str)
]
text_color = "#e0e0e0"
text_danger = "#ff5555"
text_good = "#55ff55"
hud_x = sw * 0.008
for i in 0..metrics.len() do {
color = text_color
if i == 1 and rckt.vy > MAX_LANDING_SPEED_Y then {color = text_danger}
if i == 2 and math.abs(rckt.vx) > MAX_LANDING_SPEED_X then {color = text_danger}
if i == 3 and math.abs(rckt.angle) > MAX_LANDING_ANGLE then {color = text_danger}
if i == 8 then {color = text_good}
hud_y = sh * 0.012 + (i * hud_row_h)
graphics.text(hud_x, hud_y, metrics[i], size=hud_font, color=color)
}
# -- Autopilot indicator badge --
ap_badge_w = hud_w
ap_badge_h = sh * 0.038
ap_badge_x = 0.0
ap_badge_y = hud_h
ap_font = math.max(10, (ap_badge_h * 0.52) -> Int)
if auto_mode then {
graphics.rect(ap_badge_x, ap_badge_y, ap_badge_w, ap_badge_h, "#00aa44")
graphics.rect(ap_badge_x, ap_badge_y, ap_badge_w, 2, "#00ff88")
graphics.text(ap_badge_x + hud_x, ap_badge_y + ap_badge_h * 0.2, "AUTOPILOT ON", size=ap_font, color="#ccffdd")
} else {
graphics.rect(ap_badge_x, ap_badge_y, ap_badge_w, ap_badge_h, "#1a2230cc")
graphics.rect(ap_badge_x, ap_badge_y, ap_badge_w, 2, "#2a3a50")
graphics.text(ap_badge_x + hud_x, ap_badge_y + ap_badge_h * 0.2, "AUTOPILOT OFF", size=ap_font, color="#445566")
}
# Crash indicator
if math.abs(rckt.y) < SCREEN_HEIGHT_M/4 and
(rckt.vy > MAX_LANDING_SPEED_Y
or math.abs(rckt.vx) > MAX_LANDING_SPEED_X
or math.abs(rckt.angle) >MAX_LANDING_ANGLE
or math.abs(rckt.omega) > MAX_ROTATION_SPEED)
then {
warning_x = sw / 2
warning_y = sh / 2
tri_w = sw * 0.04
tri_h = sh * 0.08
border = sw * 0.009
c_warn = "#d61818"
if not auto_mode then {
# Triangle
graphics.polygon([warning_x, warning_y, warning_x - tri_w, warning_y + tri_h, warning_x + tri_w, warning_y + tri_h], color=c_warn)
graphics.polygon([warning_x, warning_y + (border * 1.5), warning_x - (tri_w - border*1.5), warning_y + tri_h - border, warning_x + (tri_w - border*1.5), warning_y + tri_h - border], color="#ffffff")
# !
graphics.rect(warning_x - border*0.28, warning_y + tri_h*0.33, border*0.56, tri_h*0.33, color=c_warn)
graphics.rect(warning_x - border*0.28, warning_y + tri_h*0.73, border*0.56, border*0.56, color=c_warn)
}
}
# Final Status Text Banner
if landed or crashed then {
banner_w = sw * (if crashed then 0.5 else 1)
banner_h = sh * 0.16
banner_y = sh / 2 - (banner_h / 2)
banner_x = sw / 2 - (banner_w / 2)
offset = sw * 0.025
banner_font_big = math.max(16, (banner_h * 0.32) -> Int)
banner_font_sm = math.max(10, (banner_h * 0.16) -> Int)
graphics.rect(banner_x, banner_y, banner_w, banner_h, "#050b14cc")
if landed then {
graphics.text(banner_x + offset, banner_y + offset, "LANDING SUCCESSFUL", size=banner_font_big, color="#00ff66")
} else {
graphics.text(banner_x + offset, banner_y + offset, "MISSION FAILED", size=banner_font_big, color="#ff3333")
}
score_text = if landed then " - SCORE: " + ((score + landing_bonus) -> Str) else ""
graphics.text(banner_x + offset, banner_y + banner_h * 0.55, "PRESS SPACE OR (4) TO RESTART" + score_text, size=banner_font_sm, color="#aaaaaa")
}
}
draw_title!() = {
global started, fade_time
sw = SCREEN_WIDTH -> 1
sh = SCREEN_HEIGHT -> 1
if not started then {
cx = sw / 2.0
# -- Title Banner --
wm_w = sw * 0.74
wm_h = sh * 0.14
wm_y = sh * 0.2
graphics.rect(cx - wm_w / 2.0, wm_y, wm_w, wm_h, "#050b1455")
title_font = math.max(14, (wm_h * 0.34) -> Int)
graphics.text(cx - wm_w * 0.45, wm_y + wm_h * 0.32, "ROCKET LANDING SIMULATOR", size=title_font, color="#e8edf2")
# -- Tutorial Cards --
card_gap = sw * 0.025
# side cards + 1.53x wider center card
card_w = (sw * 0.88 - 2.0 * card_gap) / 3.53
center_w = card_w * 1.53
card_y = wm_y + wm_h + sh * 0.06
card_h = sh * 0.5
total_w = card_w + card_gap + center_w + card_gap + card_w
left_x = cx - total_w / 2.0
cx1 = left_x + card_w / 2.0
cx2 = left_x + card_w + card_gap + center_w / 2.0
cx3 = left_x + card_w + card_gap + center_w + card_gap + card_w / 2.0
graphics.rect(cx1 - card_w / 2.0, card_y, card_w, card_h, "#050b1455")
graphics.rect(cx2 - center_w / 2.0, card_y, center_w, card_h, "#050b1455")
graphics.rect(cx3 - card_w / 2.0, card_y, card_w, card_h, "#050b1455")
header_font = math.max(12, (card_h * 0.07) -> Int)
# -- Text Headers --
graphics.text(cx1 - header_font * 1.7, card_y + card_h * 0.07, "THRUST", size=header_font, color="#e8edf2")
graphics.text(cx2 - header_font * 1.7, card_y + card_h * 0.07, "ROTATE", size=header_font, color="#e8edf2")
graphics.text(cx3 - header_font * 1.7, card_y + card_h * 0.07, "LAUNCH", size=header_font, color="#e8edf2")
# -- Rocket Previews --
preview_px = card_h / sh * 0.37
rocket_render_y = card_y + card_h * 0.44
# THRUST
graphics.set_scale(preview_px)
graphics.set_origin(cx1, rocket_render_y)
draw_rocket(0.0m, 0.0m, 0.0 rad, true, false, false, 1.0, 1.0)
# ROTATE
graphics.set_scale(preview_px * 1.2)
tilt = 0.28 rad
pair_off = center_w * 0.28
graphics.set_origin(cx2 - pair_off, rocket_render_y)
draw_rocket(0.0m, 0.0m, -tilt, false, true, false, 1.0, 1.0)
graphics.set_origin(cx2 + pair_off, rocket_render_y)
draw_rocket(0.0m, 0.0m, tilt, false, false, true, 1.0, 1.0)
# LAUNCH
graphics.set_scale(preview_px)
graphics.set_origin(cx3, rocket_render_y)
draw_rocket(0.0m, 0.0m, 0.0 rad, false, false, false, 1.0, 1.0)
graphics.set_scale(1)
graphics.set_origin(0, 0)
# -- Controls --
kw = card_w * 0.44
kh = card_h * 0.12
kgap = card_w * 0.08
keys_y = card_y + card_h * 0.8
x_left = -kw - kgap / 2.0
x_right = kgap / 2.0
# Card 1 Keys
draw_key(cx1 + x_left, keys_y, kw, kh, "up", "")
draw_key(cx1 + x_right, keys_y, kw, kh, "", "3")
# Card 2 Keys
draw_key(cx2 + x_left, keys_y, kw, kh, "left", "")
draw_key(cx2 + x_right, keys_y, kw, kh, "right", "")
# Card 3 Keys
draw_key(cx3 + x_left, keys_y, kw, kh, "", "SPACE")
draw_key(cx3 + x_right, keys_y, kw, kh, "", "4")
}
# Fade Overlay
if fade_time > 0.0 s then {
fade_alpha = fade_alpha_hex(fade_time, 0.8 s)
if fade_alpha != "00" then {
graphics.rect(0, 0, sw, sh, "#000000" + fade_alpha)
}
}
}
draw_explosion!() = {
global rckt, crash_time
t = crash_time -> 1
# Initial Flash
if t < 0.15 then {
flash_alpha = math.max(0.0, 1.0 - t/0.15)
graphics.circle(rckt.x, rckt.y, 100.0m, "#ffffff" + fade_alpha_hex(flash_alpha * 1s, 1s))
}
# Expanding Shockwave
wave_r = t * 250.0m
wave_alpha = math.max(0.0, 1.0 - t/0.6)
if wave_alpha > 0.01 then {
graphics.circle(rckt.x, rckt.y, wave_r, "#ffffff" + fade_alpha_hex(wave_alpha * 0.8s, 1s))
graphics.circle(rckt.x, rckt.y, wave_r * 0.95, "#ffcc55" + fade_alpha_hex(wave_alpha * 0.4s, 1s))
}
# Core Fireball Layers
fire_alpha = math.max(0.0, 1.0 - t/1.5)
if fire_alpha > 0.01 then {
for i in 0..16 do {
ang = (i * 1.88 + 1.0) -> rad
dist = (t * 25.0 * (1.0 + math.sin((i * 2.7) -> rad))) -> m
fx = rckt.x + math.cos(ang) * dist
fy = rckt.y + math.sin(ang) * dist - (t * 20.0m)
fr = math.max(0.0, (1.5 - t) * (18.0 + math.sin((i * 3.2) -> rad)*8.0)) -> m
col = fire_colors[i]
graphics.circle(fx, fy, fr, col + fade_alpha_hex(fire_alpha * 1s, 1s))
}
}
# Lingering Smoke Cloud
smoke_alpha = math.max(0.0, 1.0 - t/1.5)
if smoke_alpha > 0.01 then {
for i in 0..9 do {
sx = rckt.x + math.sin(i * 1.1) * 25.0m * t
sy = rckt.y - t * (15.0m + i * 2.5m)
sr = t * 20.0m + 12.0m + i * 3.0m
graphics.circle(sx, sy, sr, "#1a1a1a" + fade_alpha_hex(smoke_alpha * 0.6s, 1s))
}
}
# Flying Tumbling Debris
deb_alpha = math.max(0.0, 1.0 - t/3.0)
if deb_alpha > 0.01 then {
for i in 0..26 do {
# Bias upward scatter angles
ang = (i * 0.25 rad - 1.57 rad + math.sin(i * 2.0 rad) * 0.8)
speed = 30.0 + math.sin(i * 4.1) * 25.0
dx = math.cos(ang) * speed * t -> m
dy = math.sin(ang) * speed * t + 0.5 * 9.81 * t * t -> m
deb_x = rckt.x + dx
deb_y = rckt.y + dy
# Floor stop
if deb_y > platform.y then {
deb_y = platform.y
}
size_deb = 1.5m + math.sin(t * 15.0 + i) * 0.5m
graphics.rect(deb_x, deb_y, size_deb, size_deb, "#111111" + fade_alpha_hex(deb_alpha * 1s, 1s))
}
}
}
# ==============================
main!() = {
global rckt, platform, particles, started, crashed, landed, idle_time, wind_time, wind_speed, last_time, actual_fps, fade_time, crash_time, score, FPS, DT, auto_mode, landing_bonus
gamepad_throttle = graphics.gamepad_axis(2)
while not graphics.quit_requested() and not graphics.key_pressed("q") do {
if graphics.key_pressed("A") or graphics.gamepad_button(4) then {
auto_mode = not auto_mode
while graphics.key_pressed("A") or graphics.gamepad_button(4) do {}
}
if auto_mode and not landed and not crashed then {
autopilot(rckt, platform)
} else {
rckt.engine_on = graphics.key_pressed("Up") or graphics.gamepad_button(2) or graphics.gamepad_axis(1) < -1000
rckt.rcs_left = if INVERT then graphics.key_pressed("Right") else graphics.key_pressed("Left") or graphics.gamepad_axis(0) < -1000 or graphics.gamepad_button(6)
rckt.rcs_right = if INVERT then graphics.key_pressed("Left") else graphics.key_pressed("Right") or graphics.gamepad_axis(0) > 1000 or graphics.gamepad_button(7)
}
for i in 0..10 do {
fps = i -> Str
if graphics.key_pressed(fps) then {
FPS = if i != 0 then i*10 else 5
DT = 1.0 / FPS -> s
break
}
}
if gamepad_throttle != graphics.gamepad_axis(2) then {
gamepad_throttle = graphics.gamepad_axis(2)
throttle = math.round(gamepad_throttle / 16_000) + 3 # 1-4
FPS = throttle * 15
DT = 1.0 / FPS -> s
}
_space_pressed = graphics.key_pressed("Space") or graphics.gamepad_button(3)
if _space_pressed then {
if not started then {
started = true
} else {
reset()
started = true # skip title screen
fade_time = 0.8 s
}
while graphics.key_pressed("Space") or graphics.gamepad_button(3) do {}
}
manual_input = rckt.engine_on or rckt.rcs_left or rckt.rcs_right
action_received = manual_input or _space_pressed or (auto_mode and started and not crashed and not landed)
if not started and (manual_input or _space_pressed) then {
started = true
}
update_time(action_received)
# Reset to title screen after inactivity cooldown
if started and idle_time > INACTIVITY_COOLDOWN then {
reset()
auto_mode = false
}
# Update physics and game state
wind_time += DT
wind_speed = (
math.sin(wind_time * 0.5) * 6.0 + math.sin(wind_time * 1.3) * 3.0
) -> 1 -> m/s
if not (crashed or landed) and started then
update_physics()
update_particles()
bx = rckt.x - math.sin(rckt.angle) * (ROCKET_HEIGHT / 2)
by = rckt.y + math.cos(rckt.angle) * (ROCKET_HEIGHT / 2)
# Camera and zoom control
cam_focus_x = 0.0m
scale = 1.0m
if AUTOZOOM then {
dist = ((rckt.x - (platform.x + platform.w / 2))^2 + (platform.y - rckt.y)^2)^0.5 -> m
view_height = math.max(240.0m, math.min(SCREEN_HEIGHT_M, dist + 150m))
graphics.set_scale(view_height, SCREEN_HEIGHT)
scale = view_height/SCREEN_HEIGHT -> m
cam_focus_x = (rckt.x + platform.x + platform.w/2) / 2
} else {
graphics.set_scale(SCALE)
scale = SCALE
}
half_view_w = (SCREEN_WIDTH * scale) / 2
view_left = cam_focus_x - half_view_w
view_right = cam_focus_x + half_view_w
if rckt.x < view_left - ROCKET_RADIUS or rckt.x > view_right + ROCKET_RADIUS then {
crashed = true;
}
if not landed and not crashed then {
if started then {
score_landing()
}
if by >= platform.y then {
base_span = ROCKET_RADIUS * math.cos(rckt.angle)
left_edge = bx - base_span
right_edge = bx + base_span
on_pad = (platform.x <= left_edge) and (right_edge <= platform.x + platform.w)
if (
on_pad
and rckt.vy <= MAX_LANDING_SPEED_Y
and math.abs(rckt.vx) <= MAX_LANDING_SPEED_X
and math.abs(rckt.angle) <= MAX_LANDING_ANGLE
and math.abs(rckt.omega) <= MAX_ROTATION_SPEED
) then {
landed = true
rckt.y = platform.y - math.cos(rckt.angle) * (ROCKET_HEIGHT / 2)
} else {
crashed = true
rckt.y = platform.y
}
}
}
if crashed or landed then {
rckt.vx = 0.0 m/s
rckt.vy = 0.0 m/s
rckt.angle = 0.0 rad
rckt.omega = 0.0 rad/s
}
# ==============================
# -- RENDERING --
# ==============================
if not HEADLESS then {
origin_x = (SCREEN_WIDTH / 2) - (cam_focus_x -> 1)
shake = if rckt.engine_on then random.uniform(-1, 1) else
if crashed and crash_time < 1s then random.uniform(-7, 7) else 0
graphics.set_origin(origin_x + shake, SCREEN_HEIGHT + shake)
# Sky
band_h = SCREEN_HEIGHT_M / sky_colors.len()
for i in 0..sky_colors.len() do {
graphics.rect(-SCREEN_WIDTH_M / 2 - MARGIN*4, -SCREEN_HEIGHT_M + (i * band_h), SCREEN_WIDTH_M + MARGIN*8, band_h + 1m, sky_colors[i])
}
# Ocean
ocean_base_y = platform.y + 2m
graphics.rect(-SCREEN_WIDTH_M / 2 - MARGIN*4, ocean_base_y, SCREEN_WIDTH_M + MARGIN*8, SCREEN_HEIGHT_M, "#0f5e9c")
# Landing Pad
deck_y = platform.y
# Thrusters
graphics.rect(platform.x - 4m, deck_y + 2m, 4m, 4m, "#555555") # Left thruster
graphics.rect(platform.x + platform.w, deck_y + 2m, 4m, 4m, "#555555") # Right thruster
# Main Deck and Hull
graphics.rect(platform.x, deck_y, platform.w, platform.h, "#2a2a2a") # Deck surface
graphics.rect(platform.x, deck_y + platform.h - 1m, platform.w, 2m, "#111111") # Shadow/Hull depth
# Droneship Deck Markings
graphics.rect(platform.x, deck_y, platform.w, 0.8m, "#ffcc00") # Forward edge caution line
graphics.rect(platform.x + 2m, deck_y, 1.5m, platform.h, "#ffcc00") # Left caution
graphics.rect(platform.x + platform.w - 3.5m, deck_y, 1.5m, platform.h, "#ffcc00") # Right caution
# Center 'X'
cx = platform.x + platform.w / 2
cy = deck_y + platform.h / 2
graphics.line(cx - 6m, cy - 1.5m, cx + 6m, cy + 1.5m, thickness=3.0, color="#ffffff")
graphics.line(cx - 6m, cy + 1.5m, cx + 6m, cy - 1.5m, thickness=3.0, color="#ffffff")
# Rocket
alt = math.max(0.0m, platform.y - by)
if not crashed then {
leg_ext = if landed then 1.0 else if alt < 50.0 m then math.max(0.0, math.min(1.0, (50.0m - alt) / 40.0m)) else 0.0
draw_rocket(rckt.x, rckt.y, rckt.angle, rckt.engine_on and rckt.fuel > 0 and not landed, rckt.rcs_left, rckt.rcs_right, leg_ext, 1.0)
# Spawn exhaust particles
if rckt.engine_on and rckt.fuel > 0 and not landed then {
for j in 0..18 do {
offset = random.uniform(-0.8 m, 0.8 m)
p_x = bx + math.cos(rckt.angle) * offset
p_y = by + math.sin(rckt.angle) * offset
core_factor = 1.0 - math.abs(offset) / 0.8m
spread = random.uniform(-0.35, 0.35) * (1.2 - core_factor)
exhaust_vel = random.uniform(30.0 m/s, 50.0 m/s) + core_factor * 30.0 m/s
p_life = random.uniform(0.6 s, 1.5 s)
particles.append(
Particle(p_x, p_y,
vx = rckt.vx - math.sin(rckt.angle + spread) * exhaust_vel,
vy = rckt.vy + math.cos(rckt.angle + spread) * exhaust_vel,
life = p_life,
max_life = p_life,
color = "#ffaa00"
)
)
}
}
} else {
draw_explosion()
}
if crash_time == 0.0s then {
if crashed then graphics.play_sound("assets/boom.wav")
if landed then graphics.play_sound("assets/win.wav")
}
# Render Exhaust Particles
for p in particles do {
life_ratio = p.life / p.max_life
color_idx = math.floor(life_ratio * 9.99) -> Int
p_size = (1.2 - life_ratio) * 150cm
graphics.circle(p.x, p.y, p_size, exhaust_colors[color_idx])
}
# Reset scale and origin for UI
graphics.set_scale(1)
graphics.set_origin(0, 0)
draw_telemetry(alt)
draw_title()
graphics.blit()
} else if landed or crashed then {
echo("SCORE=" + (if crashed then "0" else (score + landing_bonus) -> Str))
break
}
}
}
main()