-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
2071 lines (1814 loc) · 80 KB
/
Copy pathgame.js
File metadata and controls
2071 lines (1814 loc) · 80 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
// ============================================================
// CROPBOT — game.js
// C++ code editor + 20x20 grid + drone simulation
// ============================================================
'use strict';
// ────────────────────────────────────────────────────────────
// CONSTANTS
// ────────────────────────────────────────────────────────────
let GRID_W = 10, GRID_H = 10;
let CELL = 60; // px per cell (recalculated on grid resize)
const State = { EMPTY: 0, TILLED: 1, PLANTED: 2, GROWING: 3, READY: 4, BASE: 5 };
const Dir = { NORTH: 0, EAST: 1, SOUTH: 2, WEST: 3 };
const CropType = { WHEAT: 0, POTATO: 1, PUMPKIN: 2, CORN: 3, MUSHROOM: 4, COFFEE: 5 };
// Per-crop config: growth ticks, harvest gold, seed cost, visual colors
// supplierCost: gold to unlock supplier (0 = unlocked from start)
// minWater: cell water must be > minWater for growth to proceed (mushroom inverts this)
const CROPS = [
// WHEAT — fast, cheap
{ name: 'Wheat', time1: 8, time2: 12, value: 2, seedCost: 1, supplierCost: 0,
colors: { planted: '#70cc30', growing: '#c8d830', ready: '#ffd000' } },
// POTATO — medium
{ name: 'Potato', time1: 14, time2: 20, value: 6, seedCost: 2, supplierCost: 0,
colors: { planted: '#50bc40', growing: '#38a828', ready: '#d8a840' } },
// PUMPKIN — slow, high absolute value
{ name: 'Pumpkin', time1: 25, time2: 40, value: 10, seedCost: 5, supplierCost: 80,
colors: { planted: '#40b030', growing: '#e86820', ready: '#ff5500' } },
// CORN — best g/tick of non-special crops (0.133)
{ name: 'Corn', time1: 18, time2: 27, value: 9, seedCost: 3, supplierCost: 280,
colors: { planted: '#78c830', growing: '#c8d020', ready: '#f8c000' } },
// MUSHROOM — inverted water: grows only if water > 40
{ name: 'Mushroom', time1: 15, time2: 25, value: 10, seedCost: 4, supplierCost: 650,
minWater: 40,
colors: { planted: '#806040', growing: '#c07840', ready: '#e8a860' } },
// COFFEE — endgame, highest absolute profit per harvest
{ name: 'Coffee', time1: 40, time2: 80, value: 35, seedCost: 20, supplierCost: 1500,
colors: { planted: '#508030', growing: '#704820', ready: '#c05010' } },
];
// Non-crop cell base colors
const BASE_COLORS = { EMPTY: '#2d1808', TILLED: '#3d2210' };
// Water config
const WATER_MAX = 100; // max cell water level
const WATER_START = 50; // water level when planted
const WATER_DRAIN = 2; // per tick while planted/growing
const TANK_MAX_BASE = 50; // base drone tank capacity
let TANK_MAX = 50; // effective capacity (raised by upgrades)
const WATER_COST = 15; // tank units per water() call
const WATER_GIVE = 50; // cell water units added by water()
const WATER_BUY_PACK = 50; // tank units per buy_water(1)
const WATER_BUY_COST = 10; // gold per buy_water(1) — slight profit if used on pumpkins (~1.28x ROI)
// Energy config
const ENERGY_MAX_BASE = 120; // base drone battery capacity
let ENERGY_MAX = 120; // effective capacity (raised by upgrades)
const ENERGY_START = 120; // energy on reset
const ENERGY_ACTION_COST = 1; // energy spent per tick-action (move/till/plant/water/harvest)
const ENERGY_CHARGE_RATE = 10; // energy recharged per tick when at base
const WATER_REFILL_RATE = 8; // tank units refilled per tick when at base
// Upgrade config
const UPG_MAX = 10; // max upgrade level for each stat
const UPG_TANK_STEP = 10; // +10/level (+20% of base 50) → max 150 (3× base)
const UPG_ENERGY_STEP = 24; // +24/level (+20% of base 120) → max 360 (3× base)
const UPG_TANK_BASE_COST = 60; // cheaper start — tank only matters for watering
const UPG_ENERGY_BASE_COST= 100; // pricier — battery affects every action (×1.65 per level)
const UPG_COST_MULT = 1.65; // cost multiplier per upgrade level
let upgTank = 0; // current tank upgrade level (0–10)
let upgEnergy = 0; // current energy upgrade level (0–10)
function upgCost(baseCost, level) {
return Math.floor(baseCost * Math.pow(UPG_COST_MULT, level));
}
// ────────────────────────────────────────────────────────────
// GAME STATE
// ────────────────────────────────────────────────────────────
const game = {
grid: [], // [y][x] = { state, growTimer, cropType, waterLevel }
drone: { x: 0, y: 0 },
score: 0,
ticks: 0,
running: false,
speed: 5,
};
// Economy — lives outside game{} so Reset doesn't wipe it (optional design choice)
const eco = {
gold: 50,
seeds: [10, 5, 0, 0, 0, 0], // [WHEAT, POTATO, PUMPKIN, CORN, MUSHROOM, COFFEE]
tank: TANK_MAX,
energy: ENERGY_START,
unlockedCrops: new Set([0, 1]), // WHEAT, POTATO unlocked by default
};
function initGrid() {
game.grid = [];
for (let y = 0; y < GRID_H; y++) {
game.grid[y] = [];
for (let x = 0; x < GRID_W; x++) {
game.grid[y][x] = { state: State.EMPTY, growTimer: 0, cropType: -1, waterLevel: 0, dirty: true };
}
}
// Mark the base tile — always at (0,0)
game.grid[0][0].state = State.BASE;
game.drone = { x: 0, y: 0 };
game.score = 0;
game.ticks = 0;
upgTank = 0;
upgEnergy = 0;
TANK_MAX = TANK_MAX_BASE;
ENERGY_MAX = ENERGY_MAX_BASE;
eco.gold = 50;
eco.seeds = [10, 5, 0, 0, 0, 0];
eco.tank = TANK_MAX;
eco.energy = ENERGY_START;
eco.unlockedCrops = new Set([0, 1]);
}
function isDroneOnBase() {
return game.grid[game.drone.y][game.drone.x].state === State.BASE;
}
function growTick() {
for (let y = 0; y < GRID_H; y++) {
for (let x = 0; x < GRID_W; x++) {
const cell = game.grid[y][x];
if (cell.state === State.BASE) continue; // base tile is not farmland
const crop = cell.cropType >= 0 ? CROPS[cell.cropType] : null;
// Growth and drain: check water BEFORE draining so a plant can grow on its last tick of water
// minWater: normal crops need water > 0; mushroom needs water > 40 (inverted mechanic)
if ((cell.state === State.PLANTED || cell.state === State.GROWING) && crop) {
const growThreshold = crop.minWater ?? 0;
if (cell.waterLevel > growThreshold) {
if (cell.state === State.PLANTED) {
cell.growTimer++;
if (cell.growTimer >= crop.time1) {
cell.state = State.GROWING;
cell.growTimer = 0;
}
} else {
cell.growTimer++;
if (cell.growTimer >= crop.time2) {
cell.state = State.READY;
cell.growTimer = 0;
}
}
}
cell.waterLevel = Math.max(0, cell.waterLevel - WATER_DRAIN);
cell.dirty = true; // water bar + progress bar change every tick
}
}
}
}
// ────────────────────────────────────────────────────────────
// RENDERER
// ────────────────────────────────────────────────────────────
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
// Pre-rendered furrow texture — avoids ~600 stroke() calls per frame at 60fps
const furrowCanvas = new OffscreenCanvas(CELL, CELL);
(function() {
const fc = furrowCanvas.getContext('2d');
fc.strokeStyle = 'rgba(20,10,2,0.5)';
fc.lineWidth = 1;
for (let fy = 7; fy < CELL; fy += 9) {
fc.beginPath(); fc.moveTo(3, fy); fc.lineTo(CELL - 3, fy); fc.stroke();
}
})();
function markDirty(x, y) {
if (x >= 0 && x < GRID_W && y >= 0 && y < GRID_H) game.grid[y][x].dirty = true;
}
function markAllDirty() {
for (let y = 0; y < GRID_H; y++)
for (let x = 0; x < GRID_W; x++)
game.grid[y][x].dirty = true;
}
// Drone visual animation state
let droneAnim = { x: 0, y: 0, tx: 0, ty: 0, progress: 1.0,
propAngle: 0, bobPhase: 0, tiltX: 0, tiltY: 0 };
function lerp(a, b, t) { return a + (b - a) * Math.min(t, 1); }
function render(animProgress) {
// Only redraw dirty cells — skip unchanged ones to save ~1000 canvas ops/frame
for (let y = 0; y < GRID_H; y++) {
for (let x = 0; x < GRID_W; x++) {
const cell = game.grid[y][x];
if (!cell.dirty) continue;
cell.dirty = false;
const px = x * CELL, py = y * CELL;
const crop = cell.cropType >= 0 ? CROPS[cell.cropType] : null;
// ── Clear cell with grid background ────────────────────
ctx.fillStyle = '#2a1a0e';
ctx.fillRect(px, py, CELL, CELL);
// ── BASE tile ───────────────────────────────────────────
if (cell.state === State.BASE) {
drawBaseTile(px, py);
continue;
}
// ── Base soil fill (always dark brown) ─────────────────
ctx.fillStyle = cell.state === State.EMPTY ? '#2d1808' : '#3d2210';
ctx.fillRect(px + 1, py + 1, CELL - 2, CELL - 2);
// ── Furrow texture for tilled+ states (pre-rendered blit) ──
if (cell.state !== State.EMPTY) {
ctx.drawImage(furrowCanvas, px, py);
}
// ── Water tint (globalAlpha avoids string creation) ────
if (cell.waterLevel > 0 && cell.state >= State.PLANTED) {
ctx.globalAlpha = (cell.waterLevel / WATER_MAX) * 0.18;
ctx.fillStyle = '#2878ff';
ctx.fillRect(px + 1, py + 1, CELL - 2, CELL - 2);
ctx.globalAlpha = 1;
}
// ── Dry warning ────────────────────────────────────────
if (cell.waterLevel <= 0 && (cell.state === State.PLANTED || cell.state === State.GROWING)) {
ctx.fillStyle = 'rgba(255,60,0,0.22)';
ctx.fillRect(px + 1, py + 1, CELL - 2, CELL - 2);
}
// ── Ready state: golden glow border ────────────────────
if (cell.state === State.READY) {
ctx.fillStyle = 'rgba(255,220,0,0.07)';
ctx.fillRect(px + 1, py + 1, CELL - 2, CELL - 2);
ctx.strokeStyle = 'rgba(255,200,20,0.75)';
ctx.lineWidth = 2;
ctx.strokeRect(px + 2, py + 2, CELL - 4, CELL - 4);
}
// ── Grid line ──────────────────────────────────────────
ctx.strokeStyle = 'rgba(0,0,0,0.5)';
ctx.lineWidth = 1;
ctx.strokeRect(px + 0.5, py + 0.5, CELL - 1, CELL - 1);
// ── Growth progress bar ────────────────────────────────
if (crop && (cell.state === State.PLANTED || cell.state === State.GROWING)) {
const total = cell.state === State.PLANTED ? crop.time1 : crop.time2;
const pct = cell.growTimer / total;
ctx.fillStyle = 'rgba(0,0,0,0.3)';
ctx.fillRect(px + 2, py + CELL - 5, CELL - 4, 3);
ctx.fillStyle = 'rgba(255,255,255,0.55)';
ctx.fillRect(px + 2, py + CELL - 5, (CELL - 4) * pct, 3);
}
// ── Water level bar ────────────────────────────────────
if (cell.state === State.PLANTED || cell.state === State.GROWING) {
const wpct = cell.waterLevel / WATER_MAX;
ctx.fillStyle = 'rgba(0,0,0,0.25)';
ctx.fillRect(px + 2, py + CELL - 9, CELL - 4, 3);
ctx.fillStyle = wpct > 0.3 ? 'rgba(60,160,255,0.85)' : 'rgba(255,80,0,0.85)';
ctx.fillRect(px + 2, py + CELL - 9, (CELL - 4) * wpct, 3);
}
// ── Crop sprite ────────────────────────────────────────
if (cell.state >= State.PLANTED && crop) {
const scale = cell.state === State.PLANTED ? 0.38 : cell.state === State.GROWING ? 0.68 : 1.0;
drawCrop(px + CELL / 2, py + CELL / 2, scale, cell.state, cell.cropType);
}
}
}
// Drone (animated — always drawn on top)
const t = animProgress !== undefined ? animProgress : 1.0;
const dx = lerp(droneAnim.x, droneAnim.tx, t) * CELL + CELL / 2;
const dy = lerp(droneAnim.y, droneAnim.ty, t) * CELL + CELL / 2;
// Charging glow ring when drone is on base
if (isDroneOnBase()) {
const pulse = 0.22 + 0.12 * Math.sin(droneAnim.bobPhase * 2);
ctx.globalAlpha = pulse;
ctx.strokeStyle = '#38d58c';
ctx.lineWidth = 2.5;
ctx.beginPath();
ctx.arc(dx, dy, CELL * 0.40, 0, Math.PI * 2);
ctx.stroke();
ctx.globalAlpha = 1;
}
drawDrone(dx, dy, t);
}
function drawBaseTile(px, py) {
const cx = px + CELL / 2, cy = py + CELL / 2;
// Steel-plate background
ctx.fillStyle = '#141e2c';
ctx.fillRect(px + 1, py + 1, CELL - 2, CELL - 2);
// Subtle metal sheen gradient
const grad = ctx.createLinearGradient(px, py, px + CELL, py + CELL);
grad.addColorStop(0, 'rgba(70, 100, 150, 0.22)');
grad.addColorStop(1, 'rgba(20, 40, 80, 0.10)');
ctx.fillStyle = grad;
ctx.fillRect(px + 1, py + 1, CELL - 2, CELL - 2);
// Landing pad circle
const circR = CELL * 0.36;
ctx.beginPath();
ctx.arc(cx, cy, circR, 0, Math.PI * 2);
ctx.strokeStyle = 'rgba(56, 213, 140, 0.28)';
ctx.lineWidth = 1.5;
ctx.stroke();
// Cross marker
const arm = CELL * 0.18;
ctx.strokeStyle = 'rgba(56, 213, 140, 0.40)';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(cx - arm, cy); ctx.lineTo(cx + arm, cy);
ctx.moveTo(cx, cy - arm); ctx.lineTo(cx, cy + arm);
ctx.stroke();
// Corner brackets
const m = CELL * 0.12, p = px + 2, q = py + 2, r = px + CELL - 2, s = py + CELL - 2;
ctx.strokeStyle = 'rgba(96, 184, 255, 0.38)';
ctx.lineWidth = 1.5;
ctx.beginPath();
// TL
ctx.moveTo(p + m, q); ctx.lineTo(p, q); ctx.lineTo(p, q + m);
// TR
ctx.moveTo(r - m, q); ctx.lineTo(r, q); ctx.lineTo(r, q + m);
// BL
ctx.moveTo(p + m, s); ctx.lineTo(p, s); ctx.lineTo(p, s - m);
// BR
ctx.moveTo(r - m, s); ctx.lineTo(r, s); ctx.lineTo(r, s - m);
ctx.stroke();
// Border
ctx.strokeStyle = 'rgba(56, 213, 140, 0.22)';
ctx.lineWidth = 1;
ctx.strokeRect(px + 0.5, py + 0.5, CELL - 1, CELL - 1);
}
function drawCrop(cx, cy, scale, state, cropType) {
const s = scale * CELL * 0.45;
ctx.save();
ctx.translate(cx, cy);
// Helper: fill then stroke the current path
function fo(fillC, strokeC = 'rgba(0,0,0,0.65)', lw = 1.1) {
ctx.fillStyle = fillC;
ctx.fill();
ctx.strokeStyle = strokeC;
ctx.lineWidth = lw;
ctx.stroke();
}
if (cropType === CropType.WHEAT) {
if (state === State.PLANTED) {
// Tiny sprout: stem + two small leaves
ctx.strokeStyle = '#70cc30'; ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.moveTo(0, s*0.55); ctx.lineTo(0, -s*0.15); ctx.stroke();
ctx.beginPath(); ctx.ellipse(-s*0.27, s*0.12, s*0.22, s*0.1, -0.6, 0, Math.PI*2);
fo('#88d840', 'rgba(25,60,0,0.6)', 0.7);
ctx.beginPath(); ctx.ellipse(s*0.27, -s*0.04, s*0.22, s*0.1, 0.6, 0, Math.PI*2);
fo('#88d840', 'rgba(25,60,0,0.6)', 0.7);
} else if (state === State.GROWING) {
// 3 stalks with developing heads
const xs = [-s*0.36, 0, s*0.36];
for (let i = 0; i < 3; i++) {
const sx = xs[i], side = i % 2 === 0 ? -1 : 1;
ctx.strokeStyle = '#90c838'; ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.moveTo(sx, s*0.65); ctx.lineTo(sx, -s*0.38); ctx.stroke();
ctx.beginPath(); ctx.ellipse(sx + side*s*0.28, s*0.12, s*0.25, s*0.1, side*0.5, 0, Math.PI*2);
fo('#70c028', 'rgba(20,50,0,0.5)', 0.7);
ctx.beginPath(); ctx.ellipse(sx, -s*0.52, s*0.15, s*0.28, 0, 0, Math.PI*2);
fo('#d8d030', 'rgba(80,70,0,0.6)', 0.8);
}
} else { // READY
// 4 stalks, curved, golden drooping heads with bristles
const xs = [-s*0.42, -s*0.14, s*0.14, s*0.42];
for (let i = 0; i < 4; i++) {
const sx = xs[i], lean = i < 2 ? -0.28 : 0.28;
ctx.strokeStyle = '#c8b830'; ctx.lineWidth = 1.8;
ctx.beginPath();
ctx.moveTo(sx, s*0.7);
ctx.quadraticCurveTo(sx + lean*s*0.3, s*0.1, sx + lean*s*0.55, -s*0.3);
ctx.stroke();
ctx.beginPath(); ctx.ellipse(sx + lean*s*0.15, s*0.28, s*0.21, s*0.09, lean*0.8, 0, Math.PI*2);
fo('#a0c020', 'rgba(40,50,0,0.5)', 0.6);
ctx.beginPath(); ctx.ellipse(sx + lean*s*0.55, -s*0.52, s*0.17, s*0.36, lean*0.3, 0, Math.PI*2);
fo('#ffd000', 'rgba(100,70,0,0.8)', 1.0);
// Bristles
ctx.strokeStyle = '#e8a000'; ctx.lineWidth = 0.8;
for (let b = -2; b <= 2; b++) {
ctx.beginPath();
ctx.moveTo(sx + lean*s*0.55 + b*s*0.06, -s*0.52 - s*0.36);
ctx.lineTo(sx + lean*s*0.55 + b*s*0.1, -s*0.52 - s*0.36 - s*0.12);
ctx.stroke();
}
}
}
} else if (cropType === CropType.POTATO) {
if (state === State.PLANTED) {
for (const side of [-1, 1]) {
ctx.strokeStyle = '#50bc40'; ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.moveTo(side*s*0.1, s*0.5);
ctx.quadraticCurveTo(side*s*0.32, s*0.1, side*s*0.2, -s*0.2);
ctx.stroke();
ctx.beginPath(); ctx.ellipse(side*s*0.27, -s*0.3, s*0.19, s*0.12, side*0.6, 0, Math.PI*2);
fo('#68d050', 'rgba(10,50,10,0.6)', 0.7);
}
} else if (state === State.GROWING) {
const circles = [{x:-s*0.28,y:s*0.06,r:s*0.34},{x:s*0.28,y:s*0.06,r:s*0.32},{x:0,y:-s*0.28,r:s*0.37},{x:-s*0.12,y:-s*0.04,r:s*0.26}];
for (let i = 0; i < circles.length; i++) {
const {x, y, r} = circles[i];
ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI*2);
fo(i < 2 ? '#35a028' : '#48b835', 'rgba(10,40,5,0.5)', 0.7);
}
} else { // READY
// Tan potato body
ctx.beginPath(); ctx.ellipse(s*0.06, s*0.24, s*0.54, s*0.38, 0.15, 0, Math.PI*2);
fo('#d8a840', 'rgba(90,55,10,0.85)', 1.5);
// Spots
ctx.fillStyle = 'rgba(160,100,30,0.45)';
ctx.beginPath(); ctx.ellipse(-s*0.2, s*0.18, s*0.1, s*0.08, 0, 0, Math.PI*2); ctx.fill();
ctx.beginPath(); ctx.ellipse(s*0.22, s*0.32, s*0.09, s*0.07, 0.5, 0, Math.PI*2); ctx.fill();
// Sprout eyes
ctx.fillStyle = '#88c850';
ctx.beginPath(); ctx.arc(-s*0.35, s*0.04, s*0.07, 0, Math.PI*2); ctx.fill();
ctx.beginPath(); ctx.arc(s*0.3, s*0.08, s*0.06, 0, Math.PI*2); ctx.fill();
// Green bush on top
for (const [bx, by, br] of [[-s*0.2,-s*0.14,s*0.28],[s*0.18,-s*0.1,s*0.25],[0,-s*0.3,s*0.3]]) {
ctx.beginPath(); ctx.arc(bx, by, br, 0, Math.PI*2);
fo('#50bc40', 'rgba(10,50,5,0.5)', 0.8);
}
}
} else if (cropType === CropType.PUMPKIN) {
if (state === State.PLANTED) {
ctx.strokeStyle = '#40b030'; ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.moveTo(0, s*0.55); ctx.quadraticCurveTo(s*0.2, s*0.1, 0, -s*0.18); ctx.stroke();
for (const [lx, ly, ang] of [[-s*0.34, s*0.02, -0.5],[s*0.3, -s*0.04, 0.5]]) {
ctx.beginPath(); ctx.ellipse(lx, ly, s*0.3, s*0.2, ang, 0, Math.PI*2);
fo('#52c840', 'rgba(10,50,5,0.6)', 0.8);
ctx.strokeStyle = 'rgba(20,80,10,0.35)'; ctx.lineWidth = 0.7;
ctx.beginPath(); ctx.moveTo(lx, ly+s*0.15); ctx.lineTo(lx, ly-s*0.15); ctx.stroke();
}
} else if (state === State.GROWING) {
ctx.strokeStyle = '#40b030'; ctx.lineWidth = 2;
ctx.beginPath(); ctx.moveTo(-s*0.5, s*0.35); ctx.quadraticCurveTo(-s*0.1, s*0.1, s*0.22, -s*0.08); ctx.stroke();
ctx.beginPath(); ctx.ellipse(-s*0.28, s*0.09, s*0.29, s*0.19, -0.4, 0, Math.PI*2);
fo('#44bc34', 'rgba(10,50,5,0.6)', 0.8);
// Small pumpkin
const pr = s*0.37;
ctx.beginPath(); ctx.arc(s*0.3, -s*0.2, pr, 0, Math.PI*2);
fo('#e86820', 'rgba(100,35,0,0.8)', 1.2);
ctx.strokeStyle = 'rgba(100,35,0,0.4)'; ctx.lineWidth = 1;
for (const rx of [-pr*0.38, pr*0.38]) {
ctx.beginPath();
ctx.moveTo(s*0.3+rx, -s*0.2-pr);
ctx.bezierCurveTo(s*0.3+rx*1.5,-s*0.2, s*0.3+rx*1.5,-s*0.2, s*0.3+rx,-s*0.2+pr);
ctx.stroke();
}
ctx.strokeStyle = '#226611'; ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.moveTo(s*0.3, -s*0.2-pr); ctx.lineTo(s*0.36, -s*0.2-pr-s*0.16); ctx.stroke();
} else { // READY
const pr = s * 0.6, pcx = 0, pcy = s*0.06;
// Shadow
ctx.fillStyle = 'rgba(0,0,0,0.22)';
ctx.beginPath(); ctx.ellipse(pcx, pcy+pr*0.88, pr*0.82, pr*0.2, 0, 0, Math.PI*2); ctx.fill();
// Body
ctx.beginPath(); ctx.arc(pcx, pcy, pr, 0, Math.PI*2);
fo('#ff5500', 'rgba(110,35,0,0.9)', 1.8);
// Ribs
ctx.strokeStyle = 'rgba(120,40,0,0.45)'; ctx.lineWidth = 1.5;
for (const rx of [-pr*0.4, 0, pr*0.4]) {
ctx.beginPath();
ctx.moveTo(pcx+rx, pcy-pr);
ctx.bezierCurveTo(pcx+rx+pr*0.18, pcy-pr*0.3, pcx+rx+pr*0.18, pcy+pr*0.3, pcx+rx, pcy+pr);
ctx.stroke();
}
// Highlight
ctx.fillStyle = 'rgba(255,200,80,0.32)';
ctx.beginPath(); ctx.ellipse(pcx-pr*0.26, pcy-pr*0.28, pr*0.28, pr*0.18, -0.5, 0, Math.PI*2); ctx.fill();
// Stem
ctx.strokeStyle = '#1e6010'; ctx.lineWidth = 2.5; ctx.lineCap = 'round';
ctx.beginPath(); ctx.moveTo(pcx, pcy-pr); ctx.quadraticCurveTo(pcx+s*0.16, pcy-pr-s*0.18, pcx+s*0.1, pcy-pr-s*0.3); ctx.stroke();
// Leaf
ctx.beginPath(); ctx.ellipse(pcx+s*0.24, pcy-pr-s*0.2, s*0.26, s*0.17, 0.5, 0, Math.PI*2);
fo('#33aa22', 'rgba(10,50,5,0.55)', 0.8);
ctx.lineCap = 'butt';
}
} else if (cropType === CropType.CORN) {
if (state === State.PLANTED) {
ctx.strokeStyle = '#78c830'; ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.moveTo(0, s*0.55); ctx.lineTo(0, -s*0.1); ctx.stroke();
ctx.beginPath(); ctx.ellipse(-s*0.28, s*0.2, s*0.26, s*0.1, -0.5, 0, Math.PI*2);
fo('#90d840', 'rgba(20,55,0,0.6)', 0.7);
ctx.beginPath(); ctx.ellipse(s*0.28, -s*0.02, s*0.26, s*0.1, 0.5, 0, Math.PI*2);
fo('#90d840', 'rgba(20,55,0,0.6)', 0.7);
} else if (state === State.GROWING) {
// Tall stalk with wide leaves
ctx.strokeStyle = '#68b828'; ctx.lineWidth = 2;
ctx.beginPath(); ctx.moveTo(0, s*0.7); ctx.lineTo(0, -s*0.55); ctx.stroke();
for (const [side, hy] of [[-1, s*0.2],[1, -s*0.1]]) {
ctx.beginPath(); ctx.ellipse(side*s*0.42, hy, s*0.42, s*0.11, side*0.4, 0, Math.PI*2);
fo('#80cc30', 'rgba(15,50,0,0.5)', 0.8);
}
// Cob nub
ctx.beginPath(); ctx.ellipse(s*0.12, -s*0.38, s*0.12, s*0.22, 0.2, 0, Math.PI*2);
fo('#d8b820', 'rgba(90,60,0,0.7)', 1);
} else { // READY
ctx.strokeStyle = '#50a020'; ctx.lineWidth = 2;
ctx.beginPath(); ctx.moveTo(0, s*0.75); ctx.lineTo(0, -s*0.65); ctx.stroke();
// Full cob
ctx.beginPath(); ctx.ellipse(s*0.18, -s*0.22, s*0.2, s*0.42, 0.25, 0, Math.PI*2);
fo('#f8c000', 'rgba(110,70,0,0.85)', 1.4);
// Husk leaves
ctx.beginPath(); ctx.ellipse(-s*0.06, -s*0.1, s*0.22, s*0.36, -0.3, 0, Math.PI*2);
fo('rgba(100,185,30,0.7)', 'rgba(20,70,0,0.5)', 0.8);
// Kernels hint
ctx.fillStyle = 'rgba(220,160,0,0.4)';
for (let ky = -3; ky <= 3; ky++) {
ctx.beginPath(); ctx.ellipse(s*0.18, -s*0.22+ky*s*0.1, s*0.08, s*0.05, 0.25, 0, Math.PI*2); ctx.fill();
}
}
} else if (cropType === CropType.MUSHROOM) {
if (state === State.PLANTED) {
// Tiny mushroom nub
ctx.beginPath(); ctx.ellipse(0, s*0.25, s*0.1, s*0.16, 0, 0, Math.PI*2);
fo('#c07840', 'rgba(60,30,5,0.7)', 0.8);
ctx.beginPath(); ctx.ellipse(0, s*0.12, s*0.22, s*0.14, 0, 0, Math.PI*2);
fo('#b05030', 'rgba(60,20,5,0.7)', 0.8);
} else if (state === State.GROWING) {
// Medium mushroom
ctx.beginPath(); ctx.ellipse(0, s*0.35, s*0.14, s*0.28, 0, 0, Math.PI*2);
fo('#c07838', 'rgba(70,35,5,0.7)', 1);
ctx.beginPath(); ctx.ellipse(0, -s*0.06, s*0.48, s*0.32, 0, 0, Math.PI*2);
fo('#c05028', 'rgba(80,20,5,0.75)', 1.2);
// Spots
ctx.fillStyle = 'rgba(255,220,180,0.55)';
for (const [dx,dy] of [[-s*0.2,-s*0.12],[s*0.18,-s*0.18],[0,-s*0.04]]) {
ctx.beginPath(); ctx.arc(dx, dy, s*0.07, 0, Math.PI*2); ctx.fill();
}
} else { // READY
// Big mushroom cap
ctx.beginPath(); ctx.ellipse(0, s*0.38, s*0.14, s*0.3, 0, 0, Math.PI*2);
fo('#d08840', 'rgba(80,40,5,0.8)', 1.2);
ctx.beginPath(); ctx.ellipse(0, -s*0.08, s*0.62, s*0.44, 0, 0, Math.PI*2);
fo('#e06030', 'rgba(100,25,5,0.85)', 1.8);
// White spots
ctx.fillStyle = 'rgba(255,235,200,0.7)';
for (const [dx,dy,dr] of [[-s*0.28,-s*0.14,s*0.1],[s*0.26,-s*0.2,s*0.09],[0,-s*0.06,s*0.11],[s*0.12,-s*0.32,s*0.07]]) {
ctx.beginPath(); ctx.arc(dx, dy, dr, 0, Math.PI*2); ctx.fill();
}
// Gills
ctx.strokeStyle = 'rgba(180,100,40,0.4)'; ctx.lineWidth = 0.7;
for (let gx = -4; gx <= 4; gx++) {
ctx.beginPath(); ctx.moveTo(gx*s*0.14, s*0.36); ctx.lineTo(gx*s*0.1, s*0.04); ctx.stroke();
}
}
} else if (cropType === CropType.COFFEE) {
if (state === State.PLANTED) {
// Seed in soil
ctx.beginPath(); ctx.ellipse(0, s*0.2, s*0.18, s*0.13, 0, 0, Math.PI*2);
fo('#703020', 'rgba(40,15,5,0.8)', 0.8);
ctx.strokeStyle = '#508030'; ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.moveTo(0, s*0.08); ctx.lineTo(0, -s*0.12); ctx.stroke();
} else if (state === State.GROWING) {
// Leafy shrub
ctx.strokeStyle = '#508030'; ctx.lineWidth = 1.8;
ctx.beginPath(); ctx.moveTo(0, s*0.6); ctx.lineTo(0, -s*0.1); ctx.stroke();
for (const [side, hy, ang] of [[-1, s*0.2, -0.5],[1, s*0.05, 0.5],[-1,-s*0.1, -0.3],[1,-s*0.2, 0.3]]) {
ctx.beginPath(); ctx.ellipse(side*s*0.35, hy, s*0.32, s*0.14, ang*side, 0, Math.PI*2);
fo('#508030', 'rgba(15,45,5,0.5)', 0.7);
}
} else { // READY
// Dense bush with red berries
ctx.strokeStyle = '#406828'; ctx.lineWidth = 2;
ctx.beginPath(); ctx.moveTo(0, s*0.65); ctx.lineTo(0, -s*0.1); ctx.stroke();
for (const [side, hy, ang] of [[-1, s*0.18, -0.5],[1, s*0.04, 0.5],[-1,-s*0.12,-0.3],[1,-s*0.24, 0.3]]) {
ctx.beginPath(); ctx.ellipse(side*s*0.36, hy, s*0.34, s*0.15, ang*side, 0, Math.PI*2);
fo('#527830', 'rgba(10,40,5,0.55)', 0.8);
}
// Coffee cherries (red berries)
const berries = [[-s*0.32, s*0.05],[s*0.28, -s*0.08],[-s*0.18, -s*0.28],[s*0.38, s*0.16],[0, s*0.32],[-s*0.38, -s*0.1]];
for (const [bx, by] of berries) {
ctx.beginPath(); ctx.arc(bx, by, s*0.1, 0, Math.PI*2);
fo('#c02010', 'rgba(80,5,0,0.85)', 0.8);
ctx.fillStyle = 'rgba(255,120,100,0.5)';
ctx.beginPath(); ctx.arc(bx - s*0.03, by - s*0.03, s*0.04, 0, Math.PI*2); ctx.fill();
}
}
}
ctx.restore();
}
function drawDrone(cx, cy, t) {
const r = Math.min(CELL * 0.33, 20);
const armLen = r * 1.65;
const propR = r * 0.62;
// Tilt: lean in movement direction as animation starts (lean=1 at t=0, lean=0 at t=1)
const lean = 1.0 - Math.min(t, 1.0);
const tiltOX = droneAnim.tiltX * lean * r * 0.55;
const tiltOY = droneAnim.tiltY * lean * r * 0.35;
// Hover bob
const bob = Math.sin(droneAnim.bobPhase) * 2.0;
const dcx = cx + tiltOX;
const dcy = cy + tiltOY + bob;
// ── Thruster downwash glow ──────────────────────────────────
const gGrad = ctx.createRadialGradient(cx, cy + r*0.4, 0, cx, cy + r*0.4, r * 2.4);
gGrad.addColorStop(0, 'rgba(56,213,140,0.18)');
gGrad.addColorStop(1, 'rgba(56,213,140,0)');
ctx.beginPath();
ctx.ellipse(cx, cy + r * 0.5, r * 2.0, r * 0.75, 0, 0, Math.PI * 2);
ctx.fillStyle = gGrad;
ctx.fill();
// ── Ground shadow ───────────────────────────────────────────
ctx.beginPath();
ctx.ellipse(cx - tiltOX*0.3, cy + r * 0.9, r * (1.05 + lean*0.22), r * 0.2, 0, 0, Math.PI * 2);
ctx.fillStyle = `rgba(0,0,0,${(0.28 + lean*0.1).toFixed(2)})`;
ctx.fill();
// ── Arms at 45°/135°/225°/315° ─────────────────────────────
const armAngles = [Math.PI*0.25, Math.PI*0.75, Math.PI*1.25, Math.PI*1.75];
const ledColors = ['#ff3333', '#3399ff', '#33ff88', '#ffdd00'];
for (let i = 0; i < 4; i++) {
const ang = armAngles[i];
const ex = dcx + Math.cos(ang) * armLen;
const ey = dcy + Math.sin(ang) * armLen;
// Arm beam (dark with highlight)
ctx.beginPath(); ctx.moveTo(dcx, dcy); ctx.lineTo(ex, ey);
ctx.strokeStyle = '#111e2a'; ctx.lineWidth = 3.5; ctx.stroke();
ctx.strokeStyle = '#1e3a52'; ctx.lineWidth = 1.5; ctx.stroke();
// Spinning propeller blur (two orthogonal thin ellipses)
const pa = droneAnim.propAngle + i * (Math.PI * 0.5);
ctx.save();
ctx.globalAlpha = 0.55;
ctx.beginPath(); ctx.ellipse(ex, ey, propR, propR*0.11, pa, 0, Math.PI*2);
ctx.fillStyle = '#4ecca3'; ctx.fill();
ctx.globalAlpha = 0.38;
ctx.beginPath(); ctx.ellipse(ex, ey, propR, propR*0.11, pa + Math.PI/2, 0, Math.PI*2);
ctx.fillStyle = '#9af5dc'; ctx.fill();
ctx.restore();
// Motor hub
ctx.beginPath(); ctx.arc(ex, ey, 4.5, 0, Math.PI*2);
ctx.fillStyle = '#0e1820'; ctx.fill();
ctx.strokeStyle = '#38d58c'; ctx.lineWidth = 1.2; ctx.stroke();
// LED dot
ctx.beginPath(); ctx.arc(ex, ey, 2, 0, Math.PI*2);
ctx.fillStyle = ledColors[i]; ctx.fill();
}
// ── Body ─────────────────────────────────────────────────────
ctx.save();
ctx.translate(dcx, dcy);
// Radial gradient body
const bGrad = ctx.createRadialGradient(-r*0.3, -r*0.35, r*0.03, 0, 0, r);
bGrad.addColorStop(0, '#80fce0');
bGrad.addColorStop(0.5, '#38d58c');
bGrad.addColorStop(1, '#12673e');
ctx.beginPath(); ctx.arc(0, 0, r, 0, Math.PI*2);
ctx.fillStyle = bGrad; ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,0.22)'; ctx.lineWidth = 1.2; ctx.stroke();
// Decorative inner ring
ctx.beginPath(); ctx.arc(0, 0, r * 0.7, 0, Math.PI*2);
ctx.strokeStyle = 'rgba(0,0,0,0.18)'; ctx.lineWidth = 1.5; ctx.stroke();
// Camera lens
const lGrad = ctx.createRadialGradient(-r*0.1, -r*0.11, 0, 0, 0, r*0.32);
lGrad.addColorStop(0, '#5577cc');
lGrad.addColorStop(0.5, '#101c40');
lGrad.addColorStop(1, '#040810');
ctx.beginPath(); ctx.arc(0, 0, r*0.32, 0, Math.PI*2);
ctx.fillStyle = lGrad; ctx.fill();
ctx.strokeStyle = 'rgba(80,130,220,0.55)'; ctx.lineWidth = 0.8; ctx.stroke();
// Lens flare highlight
ctx.beginPath(); ctx.arc(-r*0.1, -r*0.1, r*0.1, 0, Math.PI*2);
ctx.fillStyle = 'rgba(255,255,255,0.65)'; ctx.fill();
// Top body shine stripe
ctx.beginPath(); ctx.ellipse(0, -r*0.38, r*0.38, r*0.11, 0, 0, Math.PI*2);
ctx.fillStyle = 'rgba(255,255,255,0.16)'; ctx.fill();
ctx.restore();
}
// ────────────────────────────────────────────────────────────
// DRONE ACTIONS (called by interpreter)
// ────────────────────────────────────────────────────────────
const droneActions = {
move(dir) {
const d = game.drone;
let nx = d.x, ny = d.y;
if (dir === Dir.NORTH && d.y > 0) ny--;
else if (dir === Dir.SOUTH && d.y < GRID_H - 1) ny++;
else if (dir === Dir.EAST && d.x < GRID_W - 1) nx++;
else if (dir === Dir.WEST && d.x > 0) nx--;
droneAnim.x = d.x; droneAnim.y = d.y;
droneAnim.tx = nx; droneAnim.ty = ny;
droneAnim.tiltX = nx - d.x;
droneAnim.tiltY = ny - d.y;
droneAnim.progress = 0;
d.x = nx; d.y = ny;
},
till() {
const cell = game.grid[game.drone.y][game.drone.x];
if (cell.state === State.BASE) return; // no-op on base
if (cell.state === State.EMPTY) {
cell.state = State.TILLED;
cell.cropType = -1;
cell.waterLevel = 0;
cell.dirty = true;
}
},
plant(cropTypeArg) {
const cell = game.grid[game.drone.y][game.drone.x];
if (cell.state === State.BASE) return; // no-op on base
if (cell.state !== State.TILLED) return;
const ct = (cropTypeArg !== undefined && cropTypeArg >= 0 && cropTypeArg < CROPS.length)
? cropTypeArg : CropType.WHEAT;
if (!eco.unlockedCrops.has(ct)) return; // supplier not purchased
if (eco.seeds[ct] <= 0) return; // no seeds
eco.seeds[ct]--;
cell.state = State.PLANTED;
cell.cropType = ct;
cell.growTimer = 0;
cell.waterLevel = WATER_START;
cell.dirty = true;
},
harvest() {
const cell = game.grid[game.drone.y][game.drone.x];
if (cell.state === State.BASE) return; // no-op on base
if (cell.state !== State.READY) return;
const value = cell.cropType >= 0 ? CROPS[cell.cropType].value : 1;
eco.gold += value;
cell.state = State.EMPTY;
cell.cropType = -1;
cell.growTimer = 0;
cell.waterLevel = 0;
cell.dirty = true;
game.score++;
},
water() {
const cell = game.grid[game.drone.y][game.drone.x];
if (cell.state === State.BASE) return; // no-op on base
if (eco.tank < WATER_COST) return; // not enough water in tank
eco.tank -= WATER_COST;
cell.waterLevel = Math.min(WATER_MAX, cell.waterLevel + WATER_GIVE);
cell.dirty = true;
},
get_state_at(x, y) {
if (x < 0 || x >= GRID_W || y < 0 || y >= GRID_H) return -1;
return game.grid[y][x].state;
},
get_state() {
return game.grid[game.drone.y][game.drone.x].state;
},
get_crop_type_at(x, y) {
if (x < 0 || x >= GRID_W || y < 0 || y >= GRID_H) return -1;
return game.grid[y][x].cropType;
},
get_crop_type() {
return game.grid[game.drone.y][game.drone.x].cropType;
},
get_water_level_at(x, y) {
if (x < 0 || x >= GRID_W || y < 0 || y >= GRID_H) return -1;
return game.grid[y][x].waterLevel;
},
get_water_level() {
return game.grid[game.drone.y][game.drone.x].waterLevel;
},
get_tank() { return eco.tank; },
get_max_tank() { return TANK_MAX; },
get_energy() { return eco.energy; },
get_max_energy() { return ENERGY_MAX; },
is_at_base() { return (game.drone.x === 0 && game.drone.y === 0) ? 1 : 0; },
get_gold() { return eco.gold; },
get_seeds(ct) { return (ct >= 0 && ct < CROPS.length) ? eco.seeds[ct] : 0; },
buy_seeds(ct, cnt) {
if (ct < 0 || ct >= CROPS.length) return;
if (!eco.unlockedCrops.has(ct)) return; // supplier not purchased
const count = Math.trunc(cnt);
if (!Number.isFinite(count) || count <= 0) return;
const cost = CROPS[ct].seedCost * count;
if (eco.gold < cost) return;
eco.gold -= cost;
eco.seeds[ct] += count;
updateStats();
},
buy_water(cnt) {
const packs = Math.trunc(cnt);
if (!Number.isFinite(packs) || packs <= 0) return;
const cost = WATER_BUY_COST * packs;
if (eco.gold < cost) return;
eco.gold -= cost;
eco.tank = Math.min(TANK_MAX, eco.tank + WATER_BUY_PACK * packs);
updateStats();
},
get_x() { return game.drone.x; },
get_y() { return game.drone.y; },
get_ticks() { return game.ticks; },
get_score() { return game.score; },
};
// ────────────────────────────────────────────────────────────
// UI HELPERS
// ────────────────────────────────────────────────────────────
function updateStats() {
document.getElementById('score-val').textContent = game.score;
document.getElementById('tick-val').textContent = game.ticks;
document.getElementById('gold-val').textContent = eco.gold;
document.getElementById('tank-val').textContent = eco.tank;
document.getElementById('tank-max-disp').textContent = '/' + TANK_MAX;
document.getElementById('energy-val').textContent = eco.energy;
document.getElementById('energy-max-disp').textContent = '/' + ENERGY_MAX;
// Seeds mini-display — locked crops show 🔒, unlocked show count
document.getElementById('seeds-wheat').textContent = eco.seeds[0];
document.getElementById('seeds-potato').textContent = eco.seeds[1];
const lockedSeedItems = [
['seeds-pumpkin', 'seed-item-pumpkin', 2],
['seeds-corn', 'seed-item-corn', 3],
['seeds-mushroom', 'seed-item-mushroom', 4],
['seeds-coffee', 'seed-item-coffee', 5],
];
for (const [countId, itemId, ct] of lockedSeedItems) {
const unlocked = eco.unlockedCrops.has(ct);
const countEl = document.getElementById(countId);
const itemEl = document.getElementById(itemId);
if (countEl) countEl.textContent = unlocked ? eco.seeds[ct] : '🔒';
if (itemEl) itemEl.classList.toggle('seed-locked', !unlocked);
}
// Toggle locked class on API panel crop cards
const lockedCropCards = [
['crop-card-2', 2],
['crop-card-3', 3],
['crop-card-4', 4],
['crop-card-5', 5],
];
for (const [id, ct] of lockedCropCards) {
const el = document.getElementById(id);
if (el) el.classList.toggle('crop-locked', !eco.unlockedCrops.has(ct));
}
}
const consoleArea = document.getElementById('console-area');
function consolePrint(msg, cls = 'console-out') {
const div = document.createElement('div');
div.className = `console-line ${cls}`;
div.textContent = msg;
consoleArea.appendChild(div);
consoleArea.scrollTop = consoleArea.scrollHeight;
}
let saveIndicatorTimer = null;
function showSaveIndicator() {
const el = document.getElementById('save-indicator');
if (!el) return;
el.textContent = '● saved';
el.style.color = '#38d58c';
clearTimeout(saveIndicatorTimer);
saveIndicatorTimer = setTimeout(() => { el.style.color = ''; }, 2000);
}
// ────────────────────────────────────────────────────────────
// EXECUTION ENGINE
// ────────────────────────────────────────────────────────────
let rafHandle = null; // requestAnimationFrame handle
let lastTick = 0;
let energyWarned = false;
function ensureEnergy() {
if (eco.energy >= ENERGY_ACTION_COST) return true;
if (!energyWarned) {
consolePrint('// Out of energy: drone is stranded! Return to base (0,0) to recharge.', 'console-warn');
energyWarned = true;
}
return false;
}
function spendEnergyForAction() {
if (!ensureEnergy()) return false;
eco.energy = Math.max(0, eco.energy - ENERGY_ACTION_COST);
if (eco.energy > 0) energyWarned = false;
return true;
}
function stopExecution(msg) {
game.running = false;
if (rafHandle) { cancelAnimationFrame(rafHandle); rafHandle = null; }
stopWasmWorker();
setRunUI(false);
setCompileUI('idle');
if (msg) consolePrint(msg, msg.startsWith('Error') ? 'console-error' : 'console-info');
markAllDirty();
render(1.0);
}
function setRunUI(running) {
document.getElementById('run-btn').disabled = running;
document.getElementById('run-btn2').disabled = running;
document.getElementById('stop-btn').disabled = !running;
document.getElementById('stop-btn2').disabled = !running;
}
let tickAccum = 0;
function gameLoop(now) {
if (!game.running) return;
const elapsed = now - lastTick;
lastTick = now;
const msPerTick = 1000 / game.speed;
tickAccum += elapsed;
let ticksDone = 0;
while (tickAccum >= msPerTick && ticksDone < 5) {
tickAccum -= msPerTick;
ticksDone++;
doTick();
if (!game.running) break;
}
// Animate drone smoothly — cap elapsed to msPerTick so multi-tick frames don't teleport
const animDt = Math.min(elapsed, msPerTick);
droneAnim.progress = Math.min(1.0, droneAnim.progress + animDt / msPerTick * 0.8);
// Propeller spin (faster while moving) + hover bob
const isMoving = droneAnim.progress < 0.97;
droneAnim.propAngle = (droneAnim.propAngle + (isMoving ? 0.48 : 0.22)) % (Math.PI * 4);
droneAnim.bobPhase = (droneAnim.bobPhase + 0.04) % (Math.PI * 2);
// Drone sprite overflows cell boundary slightly — mark 3×3 area around source+target dirty
for (let dy2 = -1; dy2 <= 1; dy2++) {
for (let dx2 = -1; dx2 <= 1; dx2++) {
markDirty(droneAnim.x + dx2, droneAnim.y + dy2);
markDirty(droneAnim.tx + dx2, droneAnim.ty + dy2);
}
}
render(droneAnim.progress);
if (game.running) rafHandle = requestAnimationFrame(gameLoop);
}
function doTick() {
game.ticks++;
growTick();
// Base services — energy + water refill, one increment per tick
if (isDroneOnBase()) {
eco.energy = Math.min(ENERGY_MAX, eco.energy + ENERGY_CHARGE_RATE);
eco.tank = Math.min(TANK_MAX, eco.tank + WATER_REFILL_RATE);