-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArtilleryTime.py
More file actions
3729 lines (3471 loc) · 142 KB
/
Copy pathArtilleryTime.py
File metadata and controls
3729 lines (3471 loc) · 142 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
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
# =====================================================================================
# ARTILLERY TIME — turn-based artillery duel on a wide scrolling battlefield
#
# World is wider than the 64×32 panel. Two guns face off across hills of dirt and
# earth. A digital HUD sits mid-field. Each turn the active gun reads the wind,
# range, and charge, then lob a shell. One direct hit or two shrapnel hits ends a
# round. Best 2 of 3 wins the war.
#
# Launch: LEDpanel / LEDcommander "launch_artillerytime" / standalone
# =====================================================================================
from __future__ import annotations
import copy
import math
import random
import time
from datetime import datetime
import LEDarcade as LED
LED.Initialize()
try:
import pygame
HAS_PYGAME = True
except Exception:
HAS_PYGAME = False
# ---- Panel / world ----
TARGET_FPS = 30
WORLD_W = 128 # wider than playfield (camera scrolls)
VIEW_W = int(getattr(LED, "HatWidth", 64) or 64)
VIEW_H = int(getattr(LED, "HatHeight", 32) or 32)
# ---- Title intro ("ARTY TIME" stylized letters + shell barrage) ----
TITLE_LINE1 = "ARTY"
TITLE_LINE2 = "TIME"
TITLE_LETTER_ZOOM = 2
TITLE_LETTER_GAP = 1
TITLE_LINE_GAP = 2
TITLE_LETTER_RGB = (255, 90, 40)
TITLE_LETTER_SHADOW_RGB = (70, 18, 8)
TITLE_LETTER_STAGGER = 0.18
TITLE_LETTER_GRAVITY = 0.58
TITLE_LETTER_BOUNCE_DAMP = 0.42
TITLE_LETTER_SETTLE_V = 0.35
TITLE_LETTER_MAX_BOUNCES = 3
TITLE_HOLD_SECONDS = 1.1
TITLE_BARRAGE_SECONDS = 4.5
TITLE_INTRO_MAX_SECONDS = 16.0
TITLE_SHELL_RGB = (255, 230, 90)
TITLE_INTRO_FPS = 30
# ---- Terrain ----
# Fallback sky (overridden by real-time sky_colors_for_hour)
SKY_TOP = (8, 12, 40)
SKY_BOT = (40, 70, 120)
GRASS = (40, 160, 50)
GRASS_DARK = (25, 100, 35)
DIRT = (110, 70, 30)
DIRT_DARK = (70, 42, 18)
ROCK = (90, 90, 95)
# Time-of-day sky keyframes: (hour 0..24, zenith RGB, horizon RGB)
# Night = pure black; day = deep blue (clouds drawn separately).
_SKY_KEYS = (
(0.00, (0, 0, 0), (0, 0, 0)), # midnight — black
(4.50, (0, 0, 0), (0, 0, 0)), # deep night
(5.50, (4, 6, 14), (20, 18, 30)), # predawn
(6.25, (25, 40, 90), (160, 95, 50)), # sunrise
(7.50, (12, 42, 120), (35, 80, 155)), # deep blue morning
(12.00, (8, 35, 110), (28, 75, 155)), # noon deep blue
(16.00, (10, 40, 115), (32, 78, 155)), # afternoon
(17.75, (30, 55, 110), (170, 110, 60)), # golden hour
(19.00, (25, 25, 55), (180, 70, 30)), # sunset
(20.25, (5, 5, 12), (20, 12, 18)), # dusk
(21.25, (0, 0, 0), (0, 0, 0)), # night — black
(24.00, (0, 0, 0), (0, 0, 0)),
)
# Daytime puffy clouds: (y_frac, speed_px/s, scale, x_phase)
_SKY_CLOUDS = (
(0.14, 0.55, 1.00, 0.0),
(0.26, 0.38, 1.25, 28.0),
(0.10, 0.70, 0.85, 52.0),
(0.22, 0.45, 0.95, 80.0),
)
# Overlapping soft puffs that make one cloud (ox, oy, radius)
_CLOUD_PUFFS = (
(0.0, 0.0, 2.3),
(-2.8, 0.4, 1.7),
(2.6, 0.3, 1.8),
(-1.0, -1.3, 1.5),
(1.4, -1.1, 1.4),
(0.2, 1.0, 1.3),
)
# ---- Top HUD 7-seg clock (same style as SevenSegClock / pinball apron) ----
SEG_CLOCK_RGB = (255, 36, 28) # lit red (night / default)
SEG_CLOCK_DIM = (27, 6, 5) # ghost segments
SEG_CLOCK_RGB_DAY = (255, 70, 45) # brighter red-orange for blue day sky
SEG_CLOCK_DIM_DAY = (55, 14, 10) # stronger ghost so digits still read
SEG_DIGIT_W = 5
SEG_DIGIT_H = 9
SEG_THICK = 1
SEG_GAP = 1
SEG_COLON_W = 2
_SEG_A, _SEG_B, _SEG_C, _SEG_D = 0x01, 0x02, 0x04, 0x08
_SEG_E, _SEG_F, _SEG_G = 0x10, 0x20, 0x40
_SEG_DIGIT_MASKS = (
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,
)
# ---- Guns ----
GUN_L_RGB = (60, 180, 255)
GUN_R_RGB = (255, 90, 60)
GUN_BARREL = (220, 220, 230)
HP_MAX_SHRAPNEL = 2 # survive this many indirect hits
# one direct hit always destroys
# ---- Ballistics ----
GRAVITY = 28.0 # px/s^2 downward
WIND_MIN, WIND_MAX = -4.5, 4.5
SHELL_TRAIL = 14
SHELL_TRAIL_LIFE = 0.55
SHELL_TRAIL_FADE_RATE = 1.85
SHELL_RGB = (255, 240, 120)
EXPLODE_SPARKS = 22
# ---- Weapons (assigned random per gun at each war start) ----
# standard — classic HE
# airburst — only detonates once over the enemy, rain of shrapnel
# heavy — huge blast crater; only 2 shots per war
# laser — bank-shot beam bouncing off the top of the screen
# phosphorous — bouncing burner that keeps sparking
# bouncer — bouncing bomb; 3 ground bounces then detonates
# sam — surface-to-air missile; huge airburst over the target
# acid — dissolves earth; ground collapses/shifts (re-aim needed)
# flame — lands and flames burst out across the ground
# mg30 — high-arc 30mm; bullets fall in a storm
# drone — single-pixel drones hover over enemy, intercept shots, then storm
# nuke — one mushroom-cloud super shell per war
WEAPONS = (
"standard",
"airburst",
"heavy",
"laser",
"phosphorous",
"bouncer",
"sam",
"acid",
"flame",
"mg30",
"drone",
"nuke",
)
WEAPON_AMMO = {
"heavy": 2,
"nuke": 1,
"sam": 3,
"acid": 3,
"mg30": 4,
"drone": 2,
}
WEAPON_RGB = {
"standard": (255, 240, 120),
"airburst": (200, 220, 255),
"heavy": (255, 120, 40),
"laser": (255, 40, 60),
"phosphorous": (180, 255, 80),
"bouncer": (255, 170, 60),
"sam": (120, 255, 200),
"acid": (80, 255, 40),
"flame": (255, 90, 20),
"mg30": (200, 200, 180),
"drone": (100, 230, 255),
"nuke": (255, 255, 200),
}
BOUNCE_BOMB_BOUNCES = 3 # bouncer detonates after this many ground hits
AIRBURST_OVER_X = 9.0 # horizontal window over enemy for airburst fuse
SAM_OVER_X = 11.0 # SAM fuse window over enemy
SAM_AIR_CLEARANCE = 4.5 # min px above surface / target for SAM fuse
ACID_RADIUS = 12
ACID_DEPTH = 6
FLAME_MAX_REACH = 16
MG30_BULLETS = 32
DRONE_COUNT = 5
DRONE_SPEED = 32.0
DRONE_INTERCEPT_R = 2.6
DRONE_HOVER_MAX = 7.5 # storm if no intercept by then
DRONE_HOVER_MIN = 1.2 # min hover before timeout storm
# ---- Timing (seconds) ----
THINK_SEC = 1.4
CHARGE_SEC = 1.1
IMPACT_HOLD = 1.0
CLOCK_HOLD_SEC = 5.0 # full clock visible this long at banners
# Banner: fade-in (~1s) + 5s hold + fade-out (~0.9s)
ROUND_BANNER = 7.0
WAR_BANNER = 7.0
PHOS_BURN_SEC = 2.8
MUSHROOM_SEC = 2.2
ACID_FX_SEC = 1.9
FLAME_BURST_SEC = 2.5
MG_RAIN_SEC = 1.6
DRONE_STORM_SEC = 1.8
DESTROY_FX_SEC = 1.5 # big death explosion hold
DRIVE_IN_SEC = 2.2 # replacement rolls onto the field
VICTORY_DRIVE_SEC = 2.8 # winner to center
VICTORY_FIREWORKS_SEC = 4.0 # fireworks + YOU WIN
YOU_WIN_RGB = (255, 255, 0) # solid pure yellow
# ---- Match ----
ROUNDS_TO_WIN = 2
def _stop(StopEvent):
try:
return StopEvent is not None and StopEvent.is_set()
except Exception:
return False
def _clamp(v, lo, hi):
return lo if v < lo else hi if v > hi else v
def _lerp(a, b, t):
return a + (b - a) * t
def _lerp_rgb(a, b, t):
return (
int(_lerp(a[0], b[0], t)),
int(_lerp(a[1], b[1], t)),
int(_lerp(a[2], b[2], t)),
)
def _smooth(t):
t = _clamp(t, 0.0, 1.0)
return t * t * (3.0 - 2.0 * t)
def _smoother(t):
"""Quintic smoothstep — very soft ease-in/out for zoom."""
t = _clamp(t, 0.0, 1.0)
return t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
def sky_hour_now(now=None):
"""Fractional local hour in [0, 24)."""
if now is None:
now = datetime.now()
return (
now.hour
+ now.minute / 60.0
+ now.second / 3600.0
+ now.microsecond / 3_600_000_000.0
) % 24.0
def sky_colors_for_hour(hour=None):
"""
Zenith + horizon RGB for the given fractional hour (0..24).
Matches real clock: night, dawn, day, sunset, dusk.
"""
if hour is None:
hour = sky_hour_now()
h = float(hour) % 24.0
keys = _SKY_KEYS
# Find surrounding keyframes (last key is 24.0 wrapping to midnight)
for i in range(len(keys) - 1):
h0, top0, bot0 = keys[i]
h1, top1, bot1 = keys[i + 1]
if h0 <= h <= h1 or (i == len(keys) - 2 and h >= h0):
span = max(1e-6, h1 - h0)
u = _smooth((h - h0) / span)
return _lerp_rgb(top0, top1, u), _lerp_rgb(bot0, bot1, u)
return keys[0][1], keys[0][2]
def sky_is_night(top_rgb, bot_rgb):
"""True for black / near-black night sky (stars + moon)."""
lum = 0.30 * bot_rgb[0] + 0.59 * bot_rgb[1] + 0.11 * bot_rgb[2]
top_lum = 0.30 * top_rgb[0] + 0.59 * top_rgb[1] + 0.11 * top_rgb[2]
return lum < 18.0 and top_lum < 18.0
def sky_is_day(hour=None):
"""True when the sun is well up — deep blue + drifting clouds."""
if hour is None:
hour = sky_hour_now()
h = float(hour) % 24.0
# Roughly after sunrise through before sunset
return 7.2 <= h <= 17.4
# A few fixed star positions — sparse night sky
_NIGHT_STARS = (
(0.10, 0.12),
(0.22, 0.06),
(0.38, 0.18),
(0.55, 0.09),
(0.70, 0.15),
(0.84, 0.05),
(0.92, 0.20),
)
def _moon_params(hour, width, height):
"""Soft moon at night — high and left. Returns (cx, cy, rad) or None."""
h = float(hour) % 24.0
elev = math.cos((h - 12.0) / 12.0 * math.pi)
if elev >= -0.12:
return None
# Stay on the left third of the panel; tiny drift with hour so it isn't frozen
day_u = _clamp((h - 5.0) / 15.0, 0.0, 1.0)
cx = 4.0 + day_u * (width * 0.22) # roughly x 4..18 on 64-wide
cy = height * (0.06 + 0.04 * min(1.0, abs(elev))) # high near top
return cx, cy, 1.35
def _draw_puffy_cloud(set_px, cx, cy, width, height, scale=1.0):
"""Stamp one soft white cloud (overlapping puffs) onto the sky."""
# Soft white with a hint of blue so it sits in deep blue sky
core = (230, 235, 245)
edge = (160, 185, 220)
for ox, oy, rr in _CLOUD_PUFFS:
r = rr * scale
x0 = max(0, int(math.floor(cx + ox * scale - r - 0.5)))
x1 = min(width - 1, int(math.ceil(cx + ox * scale + r + 0.5)))
y0 = max(0, int(math.floor(cy + oy * scale - r - 0.5)))
y1 = min(height - 1, int(math.ceil(cy + oy * scale + r + 0.5)))
px0 = cx + ox * scale
py0 = cy + oy * scale
for py in range(y0, y1 + 1):
for px in range(x0, x1 + 1):
d = math.hypot(px + 0.5 - px0, py + 0.5 - py0)
if d > r:
continue
# Soft falloff — puffy, not hard circles
k = 1.0 - (d / r)
k = k * k * (3.0 - 2.0 * k)
if k < 0.12:
continue
# Bright core, softer blue-white rim
if k > 0.55:
set_px(
px, py,
min(255, int(_lerp(edge[0], core[0], k))),
min(255, int(_lerp(edge[1], core[1], k))),
min(255, int(_lerp(edge[2], core[2], k))),
)
else:
# Lighter blend — still readable on deep blue
set_px(
px, py,
min(255, int(28 + edge[0] * k * 0.9)),
min(255, int(70 + edge[1] * k * 0.75)),
min(255, int(140 + edge[2] * k * 0.45)),
)
def _draw_day_clouds(canvas, width, height):
"""Slow-drifting puffy clouds (wall-clock based so motion is continuous)."""
set_px = canvas.SetPixel
t = time.time()
span = float(width + 24) # wrap margin so clouds re-enter smoothly
for y_frac, speed, scale, phase in _SKY_CLOUDS:
cy = y_frac * (height - 1)
# Drift left→right slowly, wrap
cx = ((t * speed + phase) % span) - 12.0
_draw_puffy_cloud(set_px, cx, cy, width, height, scale=scale)
def fill_sky(canvas, width, height, hour=None):
"""
Time-of-day sky:
night — pure black + very faint blue stars + moon
day — deep blue + slow puffy clouds
"""
if hour is None:
hour = sky_hour_now()
top, bot = sky_colors_for_hour(hour)
night = sky_is_night(top, bot)
day = sky_is_day(hour)
set_px = canvas.SetPixel
denom = max(1, height - 1)
# 1) Base gradient
for y in range(height):
t = y / denom
u = t * t * (3.0 - 2.0 * t)
r = int(_lerp(top[0], bot[0], u))
g = int(_lerp(top[1], bot[1], u))
b = int(_lerp(top[2], bot[2], u))
for x in range(width):
set_px(x, y, r, g, b)
# 2) Day: slow floating puffy clouds
if day:
_draw_day_clouds(canvas, width, height)
return top, bot
if not night:
return top, bot
# 3) Night: very faint blue stars on black
for i, (fx, fy) in enumerate(_NIGHT_STARS):
sx = int(round(fx * (width - 1)))
sy = int(round(fy * (height - 1)))
if not (0 <= sx < width and 0 <= sy < height):
continue
# Barely-there cool blue; tiny brightness wobble
phase = (hour * 0.12 + i * 0.9) % 1.0
br = 0.70 + 0.30 * (0.5 + 0.5 * math.sin(phase * math.tau))
set_px(
sx, sy,
min(255, int(18 * br)),
min(255, int(28 * br)),
min(255, int(55 * br)),
)
# 4) Soft moon
moon = _moon_params(hour, width, height)
if moon is not None:
mx, my, rad = moon
x0 = max(0, int(math.floor(mx - rad - 1)))
x1 = min(width - 1, int(math.ceil(mx + rad + 1)))
y0 = max(0, int(math.floor(my - rad - 1)))
y1 = min(height - 1, int(math.ceil(my + rad + 1)))
for py in range(y0, y1 + 1):
for px in range(x0, x1 + 1):
d = math.hypot(px + 0.5 - mx, py + 0.5 - my)
if d <= rad * 0.55:
set_px(px, py, 180, 185, 200)
elif d <= rad:
k = 1.0 - (d / rad)
set_px(
px, py,
min(255, int(20 + 140 * k)),
min(255, int(22 + 145 * k)),
min(255, int(30 + 155 * k)),
)
return top, bot
# ---------------- Tiny 3×5 digits for HUD / score ----------------
_DIGIT = {
"0": ("111", "101", "101", "101", "111"),
"1": ("010", "110", "010", "010", "111"),
"2": ("111", "001", "111", "100", "111"),
"3": ("111", "001", "111", "001", "111"),
"4": ("101", "101", "111", "001", "001"),
"5": ("111", "100", "111", "001", "111"),
"6": ("111", "100", "111", "101", "111"),
"7": ("111", "001", "001", "001", "001"),
"8": ("111", "101", "111", "101", "111"),
"9": ("111", "101", "111", "001", "111"),
"-": ("000", "000", "111", "000", "000"),
"+": ("000", "010", "111", "010", "000"),
"W": ("101", "101", "101", "101", "010"),
"I": ("111", "010", "010", "010", "111"),
"N": ("101", "111", "111", "101", "101"),
"D": ("110", "101", "101", "101", "110"),
" ": ("000", "000", "000", "000", "000"),
":": ("0", "1", "0", "1", "0"),
"L": ("100", "100", "100", "100", "111"),
"R": ("110", "101", "110", "101", "101"),
"S": ("111", "100", "111", "001", "111"),
"C": ("111", "100", "100", "100", "111"),
"H": ("101", "101", "111", "101", "101"),
"T": ("111", "010", "010", "010", "010"),
"E": ("111", "100", "111", "100", "111"),
"A": ("010", "101", "111", "101", "101"),
"G": ("111", "100", "101", "101", "111"),
"O": ("111", "101", "101", "101", "111"),
"U": ("101", "101", "101", "101", "111"),
"V": ("101", "101", "101", "101", "010"),
"Y": ("101", "101", "010", "010", "010"),
"B": ("110", "101", "110", "101", "110"),
"F": ("111", "100", "110", "100", "100"),
"P": ("111", "101", "111", "100", "100"),
"M": ("101", "111", "111", "101", "101"),
"!": ("010", "010", "010", "000", "010"),
}
def _draw_text(canvas, sx, sy, text, rgb, cam_x=0, scale=1):
"""Draw string in world or screen space. cam_x shifts world→screen."""
set_px = canvas.SetPixel
x = float(sx)
sc = max(1, int(scale))
for ch in text.upper():
rows = _DIGIT.get(ch, _DIGIT.get(" ", ("000",) * 5))
gw = len(rows[0])
for ry, row in enumerate(rows):
for rx, bit in enumerate(row):
if bit != "1":
continue
for dy in range(sc):
for dx in range(sc):
px = int(round(x + rx * sc + dx - cam_x))
py = int(sy + ry * sc + dy)
if 0 <= px < VIEW_W and 0 <= py < VIEW_H:
set_px(px, py, *rgb)
x += (gw + 1) * sc
# ---------------- Terrain ----------------
def generate_terrain(world_w, view_h, seed=None):
"""
Heightmap: surface y for each world x (0=top). Returns list of ints.
Ground fills from surface down to bottom with dirt layers.
"""
rng = random.Random(seed if seed is not None else random.randrange(1 << 30))
# Mid-height baseline with rolling hills
base = view_h * 0.55
heights = []
h = base + rng.uniform(-2, 2)
for x in range(world_w):
h += rng.uniform(-0.55, 0.55)
# Low-frequency hills
h += math.sin(x * 0.07 + rng.random()) * 0.15
h += math.sin(x * 0.03) * 0.35
# Flatten near gun pads
if x < 18 or x > world_w - 19:
h = _lerp(h, view_h * 0.62, 0.25)
# Slight valley mid for the digital display mound
mid = world_w * 0.5
if abs(x - mid) < 14:
h = _lerp(h, view_h * 0.58, 0.12)
h = _clamp(h, view_h * 0.38, view_h * 0.78)
heights.append(h)
# Smooth a few passes
for _ in range(3):
nxt = heights[:]
for x in range(1, world_w - 1):
nxt[x] = 0.25 * heights[x - 1] + 0.5 * heights[x] + 0.25 * heights[x + 1]
heights = nxt
return [int(round(v)) for v in heights]
def surface_y(heights, x):
xi = int(_clamp(round(x), 0, len(heights) - 1))
return float(heights[xi])
def crater(heights, x, radius=4, depth=3):
"""Blast a bowl into the heightmap."""
xi = int(round(x))
r = int(max(1, radius))
d0 = max(1, depth)
for dx in range(-r, r + 1):
px = xi + dx
if 0 <= px < len(heights):
fall = 1.0 - (abs(dx) / float(max(1, r)))
dig = d0 * fall * fall
heights[px] = int(_clamp(heights[px] + dig, VIEW_H * 0.35, VIEW_H - 2))
def blast_push_gun(g, heights, ix, iy, radius, strength=1.0):
"""
If a gun is close to a blast, shove it away from the impact and reseat
it on the (possibly cratered) ground. Returns True if moved.
"""
if g is None or not g.alive or g.driving:
return False
dx = g.x - float(ix)
dy = g.y - float(iy)
dist = math.hypot(dx, dy)
horiz = abs(dx)
r = max(2.5, float(radius))
# Use the nearer of full distance / horizontal so high airbursts still shove
use = min(dist, horiz) if dist > 0.01 else horiz
if use > r:
return False
falloff = 1.0 - (use / r)
push = (1.8 + 5.5 * falloff * falloff) * max(0.35, strength)
if push < 0.6:
return False
direction = 1.0 if dx >= 0.0 else -1.0
if abs(dx) < 0.4:
direction = -1.0 if g.side == "L" else 1.0
g.x = _clamp(g.x + direction * push, 5.0, WORLD_W - 6.0)
g.home_x = g.x
g.sit_on_ground(heights)
return True
def acid_dissolve(heights, x, radius=ACID_RADIUS, depth=ACID_DEPTH, strength=1.0):
"""
Dissolve earth under x and shift/settle the surface so hills collapse
into the pit — opponent must re-calculate ballistics.
"""
xi = int(round(x))
r = int(max(2, radius))
n = len(heights)
lo, hi = VIEW_H * 0.35, VIEW_H - 2
# Eat an irregular acidic pit (larger height = lower surface)
for dx in range(-r, r + 1):
px = xi + dx
if not (0 <= px < n):
continue
fall = 1.0 - (abs(dx) / float(r))
dig = depth * strength * (fall ** 1.15) * random.uniform(0.75, 1.2)
heights[px] = int(_clamp(heights[px] + dig, lo, hi))
# Collapse: taller ground (smaller y) shifts into deeper pockets
for _ in range(3):
nxt = heights[:]
for px in range(1, n - 1):
# Smooth settle
nxt[px] = (
0.18 * heights[px - 1]
+ 0.64 * heights[px]
+ 0.18 * heights[px + 1]
)
heights[:] = [int(round(_clamp(v, lo, hi))) for v in nxt]
# Lateral shift: peaks slump toward valleys
for px in range(2, n - 2):
for d in (-1, 1):
# If neighbor is higher ground (lower surface y), pull material in
if heights[px] > heights[px + d] + 1.5:
shift = 0.55 * strength
heights[px] = int(_clamp(heights[px] - shift * 0.35, lo, hi))
heights[px + d] = int(_clamp(heights[px + d] + shift, lo, hi))
def smooth_terrain(heights, passes=1):
"""Light blur so acid-shifted ground looks settled."""
n = len(heights)
lo, hi = VIEW_H * 0.35, VIEW_H - 2
for _ in range(passes):
nxt = heights[:]
for px in range(1, n - 1):
nxt[px] = 0.25 * heights[px - 1] + 0.5 * heights[px] + 0.25 * heights[px + 1]
heights[:] = [int(round(_clamp(v, lo, hi))) for v in nxt]
# ---------------- Guns ----------------
class Gun(object):
def __init__(self, side, x, heights, weapon=None):
self.side = side # "L" or "R"
self.home_x = float(x) # pad position
self.x = float(x)
self.y = surface_y(heights, x) - 1.0
self.angle = 45.0 if side == "L" else 135.0 # degrees from +x
self.power = 0.55 # 0..1 charge
self.hp = HP_MAX_SHRAPNEL
self.alive = True
self.rgb = GUN_L_RGB if side == "L" else GUN_R_RGB
self.flash = 0.0
self.weapon = weapon or "standard"
self.ammo = WEAPON_AMMO.get(self.weapon) # None = unlimited
self.driving = False
self.drive_from = self.x
self.drive_to = self.x
self.drive_t = 0.0
self.drive_dur = DRIVE_IN_SEC
self.wheel_phase = 0.0
self.explode_t = 0.0 # death explosion timer
def sit_on_ground(self, heights):
self.y = surface_y(heights, self.x) - 1.0
def begin_drive(self, from_x, to_x, duration=None):
self.driving = True
self.drive_from = float(from_x)
self.drive_to = float(to_x)
self.drive_t = 0.0
self.drive_dur = float(duration if duration is not None else DRIVE_IN_SEC)
self.x = self.drive_from
self.alive = True
self.hp = HP_MAX_SHRAPNEL
self.explode_t = 0.0
def update_drive(self, dt, heights):
if not self.driving:
return False
self.drive_t += dt
u = _smooth(self.drive_t / max(0.05, self.drive_dur))
self.x = _lerp(self.drive_from, self.drive_to, u)
self.sit_on_ground(heights)
self.wheel_phase += dt * 14.0
# Bounce slightly while rolling
self.y += math.sin(self.wheel_phase * 2.0) * 0.15 * (1.0 - u)
if self.drive_t >= self.drive_dur:
self.x = self.drive_to
self.home_x = self.drive_to
self.driving = False
self.sit_on_ground(heights)
return True # arrived
return False
def muzzle(self):
rad = math.radians(self.angle)
return (
self.x + math.cos(rad) * 3.2,
self.y - math.sin(rad) * 3.2,
)
def start_death_explosion(self, sparks):
"""Big multi-wave blast when destroyed."""
self.explode_t = DESTROY_FX_SEC
self.alive = False
self.hp = 0
for _ in range(40):
sparks.append(Spark(self.x, self.y, self.rgb))
for _ in range(25):
sparks.append(Spark(
self.x, self.y,
random.choice(((255, 200, 40), (255, 100, 20), (255, 255, 200), (120, 120, 120))),
))
# Loft some debris high
for _ in range(12):
s = Spark(self.x, self.y, self.rgb)
s.vy = -random.uniform(25, 55)
s.vx = random.uniform(-30, 30)
s.life = random.uniform(0.5, 1.1)
sparks.append(s)
def can_fire(self):
if not self.alive:
return False
if self.ammo is None:
return True
return self.ammo > 0
def consume_ammo(self):
if self.ammo is not None and self.ammo > 0:
self.ammo -= 1
# Out of special ammo → fall back to standard
if self.ammo <= 0 and self.weapon in (
"heavy", "nuke", "sam", "acid", "mg30", "drone",
):
print(f"[ArtilleryTime] {self.side} {self.weapon} ammo empty → standard")
self.weapon = "standard"
self.ammo = None
def assign_random_weapons(gun_l, gun_r):
"""Each artillery gets a random weapon for this war."""
for g in (gun_l, gun_r):
g.weapon = random.choice(WEAPONS)
g.ammo = WEAPON_AMMO.get(g.weapon)
print(f"[ArtilleryTime] {g.side} armed with {g.weapon}"
+ (f" x{g.ammo}" if g.ammo is not None else ""))
# ---------------- Ballistics AI ----------------
def simulate_shot(
x0, y0, angle_deg, power, wind, heights, enemy_x, enemy_y,
dt=1.0 / 40.0, record_path=False, weapon="standard",
):
"""
Integrate projectile until ground / airburst / OOB.
Returns (impact_x, impact_y, min_dist_to_enemy, frames[, path]).
"""
# Drone swarm: straight-ish climb toward a hover point over the enemy
if weapon == "drone":
hx = enemy_x
hy = max(3.0, enemy_y - 9.0)
x, y = float(x0), float(y0)
path = [(x, y)] if record_path else None
best = math.hypot(x - enemy_x, y - enemy_y)
for frame in range(120):
dx = hx - x
dy = hy - y
dist = math.hypot(dx, dy) or 1.0
step = DRONE_SPEED * dt
if dist <= step:
x, y = hx, hy
if record_path:
path.append((x, y))
best = min(best, math.hypot(x - enemy_x, y - enemy_y))
if record_path:
return x, y, best, frame, path
return x, y, best, frame
x += (dx / dist) * step
y += (dy / dist) * step
if record_path and frame % 2 == 0:
path.append((x, y))
best = min(best, math.hypot(x - enemy_x, y - enemy_y))
if record_path:
return x, y, best, 120, path
return x, y, best, 120
rad = math.radians(angle_deg)
speed = 8.0 + power * 42.0
if weapon == "heavy":
speed *= 0.92
elif weapon == "nuke":
speed *= 0.85
elif weapon == "laser":
speed = 90.0 + power * 40.0
elif weapon == "sam":
speed = 16.0 + power * 36.0
elif weapon == "bouncer":
speed *= 0.95
elif weapon == "acid":
speed *= 0.90
elif weapon == "flame":
speed *= 0.96
elif weapon == "mg30":
# High lofting 30mm — slightly slower horizontal, more hang time
speed = 10.0 + power * 34.0
vx = math.cos(rad) * speed
vy = -math.sin(rad) * speed
if weapon == "mg30":
# Bias upward for high arc
vy -= 6.0 + power * 8.0
x, y = float(x0), float(y0)
best = 1e9
path = [(x, y)] if record_path else None
bounces = 0
aim_y = enemy_y - 8.0
passed_apex = False
for frame in range(500):
if weapon == "laser":
# No gravity; bounce off top of screen (bank shot)
x += vx * dt
y += vy * dt
if y < 0.5:
y = 0.5
vy = abs(vy)
bounces += 1
elif weapon == "sam":
# Guided surface-to-air: reduced gravity + steer over enemy
dx = enemy_x - x
dy = aim_y - y
dist = math.hypot(dx, dy) or 1.0
steer = 55.0
vx += (dx / dist) * steer * dt
vy += (dy / dist) * steer * dt
# Cap speed
spd = math.hypot(vx, vy)
max_spd = 48.0
if spd > max_spd:
vx *= max_spd / spd
vy *= max_spd / spd
vx += wind * 1.2 * dt
vy += GRAVITY * 0.35 * dt
x += vx * dt
y += vy * dt
if y < 0.4:
y = 0.4
vy = abs(vy) * 0.4
else:
vx += wind * 3.2 * dt
vy += GRAVITY * dt
x += vx * dt
y += vy * dt
if record_path and frame % 2 == 0:
path.append((x, y))
d = math.hypot(x - enemy_x, y - enemy_y)
if d < best:
best = d
if x < -4 or x > WORLD_W + 4 or y > VIEW_H + 4:
if record_path:
return x, y, best, frame, path
return x, y, best, frame
surf = surface_y(heights, x)
left_home = abs(x - x0) > 10.0
# Airburst / SAM: only fuse once horizontally over the enemy
if weapon in ("airburst", "sam") and frame > 6 and left_home:
over_x = SAM_OVER_X if weapon == "sam" else AIRBURST_OVER_X
over_enemy = abs(x - enemy_x) <= over_x
clear = SAM_AIR_CLEARANCE if weapon == "sam" else 3.5
airborne = y < surf - clear and y < enemy_y - 1.5
if over_enemy and airborne:
if record_path:
path.append((x, y))
return x, y, best, frame, path
return x, y, best, frame
# MG30: open the bullet storm high over the enemy half
if weapon == "mg30" and frame > 10:
if vy > 0:
passed_apex = True
if (
passed_apex
and left_home
and y < surf - 6
and abs(x - enemy_x) < 22
):
if record_path:
path.append((x, y))
return x, y, best, frame, path
return x, y, best, frame
if y >= surf - 0.3 and frame > 3:
if weapon == "phosphorous" and bounces < 5:
y = surf - 0.5
vy = -abs(vy) * 0.72
vx *= 0.88
bounces += 1
continue
if weapon == "bouncer" and bounces < BOUNCE_BOMB_BOUNCES:
y = surf - 0.55
vy = -abs(vy) * 0.70 - 2.0
vx *= 0.90
bounces += 1
continue
if record_path:
path.append((x, y))
return x, y, best, frame, path
return x, y, best, frame
if record_path:
return x, y, best, 500, path
return x, y, best, 500
def ai_choose_shot(gun, enemy, wind, heights, power_bias=0.0):
"""
Search angle/power for best predicted hit (weapon-aware).
power_bias > 0 after short shots → prefer higher charge.
"""
best = None
best_score = 1e18
weapon = gun.weapon
bias = _clamp(float(power_bias), -0.25, 0.4)
if gun.side == "L":
if weapon == "laser":
angles = range(15, 70, 2) # lower bank angles
elif weapon == "sam":
angles = range(40, 82, 2) # loft toward sky then enemy
elif weapon == "mg30":
angles = range(52, 84, 2) # high arc for bullet storm
else:
angles = range(28, 78, 2)
else:
if weapon == "laser":
angles = range(110, 165, 2)
elif weapon == "sam":
angles = range(98, 140, 2)
elif weapon == "mg30":
angles = range(96, 128, 2)
else:
angles = range(102, 152, 2)
# After falling short, don't even consider weak charges
p_lo = 25
p_hi = 100
if bias > 0.04:
p_lo = min(82, 25 + int(bias * 120))
elif bias < -0.04:
# Long last time — prefer not maxing power again
p_hi = max(p_lo + 15, 100 + int(bias * 90))
for ang in angles:
for p10 in range(p_lo, p_hi, 3):
power = p10 / 100.0
mx, my = gun.muzzle()
ix, iy, mind, _fr = simulate_shot(
mx, my, ang, power, wind, heights, enemy.x, enemy.y,
weapon=weapon,
)
score = mind + abs(ix - enemy.x) * 0.15
if weapon in ("airburst", "sam"):
# Prefer detonation over enemy, still high
score += abs(ix - enemy.x) * 0.45
score += max(0, iy - (enemy.y - 5)) * 0.12
if weapon == "laser":
score += abs(iy - enemy.y) * 0.1
if weapon == "bouncer":
score += abs(ix - enemy.x) * 0.2
if weapon == "acid":
# Prefer dissolve under / near enemy pad
score += abs(ix - enemy.x) * 0.25
if weapon == "flame":
score += abs(ix - enemy.x) * 0.2
if weapon == "mg30":
# Prefer opening the storm near enemy column, still high
score += abs(ix - enemy.x) * 0.35
score += max(0, iy - 8) * 0.05
if weapon == "drone":
# Always deploy toward enemy — angle barely matters
score = abs(ix - enemy.x) * 0.1 + mind * 0.5
# Bias: reward reaching / passing the enemy when we were short
if bias > 0.02:
if gun.side == "L":
short_err = max(0.0, enemy.x - ix)
else:
short_err = max(0.0, ix - enemy.x)
score += short_err * (0.25 + bias * 1.2)
# Prefer stronger charges after short falls
score += max(0.0, (0.5 + bias) - power) * 1.8
elif bias < -0.02:
if gun.side == "L":
long_err = max(0.0, ix - enemy.x)
else:
long_err = max(0.0, enemy.x - ix)
score += long_err * 0.2
score += random.uniform(0, 0.35)