-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dgolf.c
2413 lines (2105 loc) · 61.8 KB
/
dgolf.c
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
//
// dgolf.c
// NESert Golfing, by Brad Smith 2019-2022
// http://rainwarrior.ca
//
#define VERSION_STRING "1.5"
const char rom_version[] = " ################ "
"NESert Golfing version 1.5 by Brad Smith, 2022"
" ################ ";
// for debugging performance
// (turns screen greyscale/green at end of frame when SELECT is held)
//#define PROFILE() { if (gamepad & PAD_SELECT) ppu_profile(0x41); }
//#define PROFILE() { ppu_profile(0x41); }
#define PROFILE() {}
// a few checks for safety, triggers infinite loop
#define ASSERT(__c__) {}
//#define ASSERT(__c__) { debug_assert(__c__); }
// for debugging graphics in left column
//#define SHOW_LEFT_COLUMN 1
#define SHOW_LEFT_COLUMN 0
// for testing the last hole
//#define LAST_HOLE_TEST 3
#define LAST_HOLE_TEST 0
// for debug hole skip with START button
//#define HOLE_SKIP 1
#define HOLE_SKIP 0
//
// common library stuff
//
typedef unsigned char uint8;
typedef signed char sint8;
typedef unsigned short int uint16;
typedef signed short int sint16;
typedef unsigned long int uint32;
typedef signed long int sint32;
extern uint8* ptr;
extern uint32 seed; // 24-bit random seed, don't set to 0
extern uint32 seedw; // TE
extern uint32 seedf; // TE
extern uint8 i, j, k, l;
extern uint16 mx,nx,ox,px;
extern sint16 ux,vx;
extern uint8 te; // TE non-zero if tournament edition active
#pragma zpsym("ptr")
#pragma zpsym("seed")
#pragma zpsym("seedw")
#pragma zpsym("seedf")
#pragma zpsym("i")
#pragma zpsym("j")
#pragma zpsym("k")
#pragma zpsym("l")
#pragma zpsym("mx")
#pragma zpsym("nx")
#pragma zpsym("ox")
#pragma zpsym("px")
#pragma zpsym("ux")
#pragma zpsym("vx")
#pragma zpsym("te")
extern uint8 input[16];
extern uint8 input_flags;
extern uint8 gamepad;
extern uint8 mouse1;
extern sint8 mouse2;
extern sint8 mouse3;
#pragma zpsym("input")
#pragma zpsym("input_flags")
#pragma zpsym("gamepad")
#pragma zpsym("mouse1")
#pragma zpsym("mouse2")
#pragma zpsym("mouse3")
extern uint8 prng(); // 8-bit random value
extern uint8 prng1(); // "fast" random value, only bit 0 is truly random (bits 1-7 have increasing entropy)
extern uint8 prngw(); // TE dependent random generator for ongoing wind adjustment only
extern uint8 prngw1(); // TE
extern uint8 prngf(); // TE independent random generator for things that should still be random
extern uint8 prngf1(); // TE
extern void mouse_sense(); // cycles sensitivity setting (doesn't work on Hyperkin clone)
extern void input_setup();
extern void input_poll();
extern void sound_play(const uint8* addr); // play a sound effect
extern void te_switch(); // rendering off, reset and switch mode
extern void soft_reset();
extern void ppu_latch(uint16 addr); // write address to PPU latch
extern void ppu_direction(uint8 vertical); // set write increment direction
extern void ppu_write(uint8 value); // write value to $2007
extern void ppu_load(uint16 count); // uploads bytes from ptr to $2007 (clobbers ptr)
extern void ppu_fill(uint8 value, uint16 count); // uploads single value to $2007
extern void ppu_ctrl(uint8 v); // $2000, only bits 4-6 count (tile pages, sprite height), applies at next post
extern void ppu_mask(uint8 v); // $2001, applies at next post
extern void ppu_scroll_x(uint16 x);
extern void ppu_scroll_y(uint16 y);
extern void ppu_post(uint8 mode); // waits for next frame and posts PPU update
extern void ppu_profile(uint8 emphasis); // immediate $2001 write, OR with current mask (use bit 0 for greyscale)
extern void ppu_apply_direction(uint8 vertical); // immediately set write increment direction
extern void ppu_apply(); // immediately uploads ppu_send to $2007, resets ppu_send_count to 0
// POST_OFF turn off rendering
// POST_NONE turn on, no other updates
// POST_UPDATE turn on, palette, send
// POST_DOUBLE turn on, palette, send 64 bytes across 2 nametables
#define POST_OFF 1
#define POST_NONE 2
#define POST_UPDATE 3
#define POST_DOUBLE 4
#define PAD_A 0x80
#define PAD_B 0x40
#define PAD_SELECT 0x20
#define PAD_START 0x10
#define PAD_UP 0x08
#define PAD_DOWN 0x04
#define PAD_LEFT 0x02
#define PAD_RIGHT 0x01
#define MOUSE_L 0x40
#define MOUSE_R 0x80
extern uint16 ppu_send_addr;
extern uint8 ppu_send_count;
extern uint8 ppu_send[64];
extern uint8 palette[32];
extern uint8 oam[256];
//
// simple color blending library
//
extern uint8 blend50(uint8 a, uint8 b); // best palette match of 50% a, 50% b
extern uint8 blend25(uint8 a, uint8 b); // best palette match of 75% a, 25% b
//
// desert golfing stuff in assembly
//
extern uint8 layers_chr[];
extern uint8 sprite_chr[];
extern uint8 layerste_chr[];
extern const uint16 LAYERS_CHR_SIZE;
extern const uint16 SPRITE_CHR_SIZE;
extern const uint16 LAYERSTE_CHR_SIZE;
extern uint8 floor_column;
extern uint8 weather_tile;
extern uint8 weather_attribute;
extern sint8 weather_wind_dir;
extern uint8 weather_wind_p;
extern uint8 weather_rate_min;
extern uint8 weather_rate_mask;
extern uint8 hole;
extern uint8 ocean_attribute;
extern uint16 tx;
extern uint16 ty;
extern sint16 tsx;
extern sint16 tsy;
extern uint32 ball_x;
extern uint32 ball_y;
extern sint32 ball_vx;
extern sint32 ball_vy;
extern volatile uint32 balls_x[4]; // 16:8 fixed point + 8 bits of padding for convenience
extern volatile uint8 balls_fx[16]; // byte access to balls_x+0 (0,4,8,12), fixed 8
extern volatile uint8 balls_lx[15]; // byte access to balls_x+1 (0,4,8,12), low 8
extern volatile uint8 balls_hx[14]; // byte access to balls_x+2 (0,4,8,12), high 8
extern volatile uint16 balls_wx[7]; // word access to balls_x+1 (0,2,4,6), low,high 16
extern uint8 balls_y[4];
extern sint16 norm_x;
extern sint16 norm_y;
// note: while "volatile" is semantically important because of the type-punned aliases for balls_x
// it was actually a bad idea, because in cc65 it ends up disabling all optimizations in any function
// that uses volatile variables. In hindsight it would have been better to do this without the
// alias access.
#pragma zpsym("floor_column")
#pragma zpsym("weather_tile")
#pragma zpsym("weather_attribute")
#pragma zpsym("weather_wind_dir")
#pragma zpsym("weather_wind_p")
#pragma zpsym("weather_rate_min")
#pragma zpsym("weather_rate_mask")
#pragma zpsym("hole")
#pragma zpsym("ocean_attribute")
#pragma zpsym("tx")
#pragma zpsym("ty")
#pragma zpsym("tsx")
#pragma zpsym("tsy")
#pragma zpsym("ball_x")
#pragma zpsym("ball_y")
#pragma zpsym("ball_vx")
#pragma zpsym("ball_vy")
#pragma zpsym("balls_x")
#pragma zpsym("balls_fx")
#pragma zpsym("balls_lx")
#pragma zpsym("balls_hx")
#pragma zpsym("balls_wx")
#pragma zpsym("balls_y")
#pragma zpsym("norm_x")
#pragma zpsym("norm_y")
extern uint8 floor_render[64];
extern uint8 floor_y[512];
extern uint8 floor_a[512];
extern uint16 read_slope(uint8 index);
extern sint16 read_norm_x(uint8 index);
extern sint16 read_norm_y(uint8 index);
extern void floor_render_prepare(uint8 phase); // 0 = build, 1-5 = send, 6+ nothing (~6500, 300 x 4, 600, 0... cy)
extern void weather_animate(); // <~4300 cy
extern void weather_shift(uint8 shift); // ~800 cy
// fixed point multiply: (a * b) / 256
extern sint16 fmult(sint16 a, sint16 b); // ~545-594 cy
// used this to replace basis transforms like: ((a*b)+(c*d))/256
// (they are about 4x as fast, the original C code is left in comments for comparison)
//
// allocations and other common stuff
//
// fixed point for swing_x/y
#define SWING_FIX 16
// minimum radius for swing (to allow cancel)
#define SWING_MIN (SWING_FIX*3)
// maximum radius of swing
#define SWING_MAX (SWING_FIX*256)
// GLOBAL_FLOOR must be at least 2 pixels above OCEAN_FLOOR
// OCEAN_FLOOR must match definition in dgolf.s
#define GLOBAL_FLOOR 222
#define OCEAN_FLOOR 224
#define WATER_COLOUR 0x11
const uint8 BALL_COLOUR[5] = { 0x06, 0x02, 0x08, 0x0A, 0x00 };
// conventions:
// sx/sy = screen x,y (s = sprite)
// cx = circular x (mod 512)
#pragma bss-name (push, "ZEROPAGE") // was out of BSS space, but had plenty of ZEROPAGE
uint16 tee_cx;
uint16 hole_cx;
uint16 scroll_cx;
// render positions of things to draw
uint16 tee_sx;
uint16 hole_sx;
uint16 status_sx;
uint8 tee_sy;
uint8 hole_sy;
uint8 tee_s;
uint8 ball_s;
uint8 splash_s;
uint8 balls_draw[4]; // which player is in which draw-slot
uint8 hole_digits[3];
uint8 flag_remove;
uint8 players;
uint8 player;
uint8 swinging;
sint16 swing_x;
sint16 swing_y;
uint8 strokes[5*4];
uint8 cleared; // bit 0,1,2,3 = player in hole
uint8 status_w;
uint8 ring_glow;
uint8 roll_sx, roll_sy; // last frame's position
uint8 soll_sx, soll_sy; // two frames ago position
uint16 timeout; // frame timeout
uint8 rollout; // visual timeout (if ball is in same place for too long)
uint8 valley;
uint8 old_lip[2];
uint8 first_stroke;
uint8 next_player;
#pragma bss-name (pop)
// sprite building
uint8 oam_pos;
// floor generation
// control parameters
uint16 fg_min; // floor should not go below min
uint16 fg_max; // floor should not go below max
uint8 fg_hold_mask; // power of 2 - 1, controls maximum length of slopes
uint8 fg_angle_mask; // $80 | power of 2 - 1, lower values favour shallower slopes
uint16 fg_cx; // next floor pixel index to build
uint16 fg_last; // last floor value
uint16 fg_min_soft; // used to guide more toward centre
uint16 fg_max_soft;
uint16 fg_next; // next floor value
uint8 fg_angle; // last floor angle
uint8 fg_hold; // pixels to continue current angle
// animation of scenery etc.
uint8 frame_count;
uint8 transition;
uint8 transition_time;
uint8 day;
sint8 weather_wind_fade_dir;
uint8 weather_wind_fade_p;
uint16 weather_wind_timeout;
// hole generation
uint8 pal_floor;
uint8 pal_sky;
uint8 pal_text;
uint8 course;
uint8 field_set;
// title menu
uint8 title_menu;
uint8 gamepad_last;
uint8 gamepad_new;
uint8 mouse_last;
uint8 mouse_new;
uint8 mouse_steady;
sint8 mouse_sx;
sint8 mouse_sy;
// TE tournament edition
uint16 holes;
uint8 seed_pos;
uint8 holes_pos;
uint32 seed_start;
extern uint16 holes;
extern uint32 seed_start;
//
// sound effects
//
// 0xFF = end
// 0xFE = all notes off
// 0xFD = next frame
// 0xXX 0xYY = write YY to register $4000+XX
// - start all sounds with $FE
// - end all sounds with $FE $FF
#define SFX_BEGIN 0xFE
#define SFX_END 0xFE, 0xFF
#define SFX_FRAME 0xFD
#define SFX(a_,b_) a_, b_
const uint8 SFX_STROKE[] = { SFX_BEGIN,
SFX(0xC,0x3F), SFX(0xE,0x0C), SFX_FRAME,
SFX(0xC,0x3D), SFX(0xE,0x00), SFX_FRAME,
SFX(0xC,0x32), SFX_FRAME,
SFX(0xC,0x31), SFX_FRAME, SFX_END };
const uint8 SFX_FLAG[] = { SFX_BEGIN,
SFX(0x0,0xB2), SFX(0x2,0x93), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x0,0xB3), SFX(0x2,0x3F), SFX_FRAME, SFX_FRAME,
SFX(0x0,0xB4), SFX(0x2,0xEF), SFX(0x3,0x00), SFX_FRAME, SFX_FRAME,
SFX(0x0,0xB5), SFX(0x2,0xBD), SFX_FRAME, SFX_FRAME,
SFX(0x0,0xB4), SFX(0x2,0x9F), SFX_FRAME, SFX_FRAME,
SFX(0x0,0xB3), SFX(0x2,0x7E), SFX_FRAME, SFX_FRAME,
SFX(0x0,0xB2), SFX(0x2,0x64), SFX_FRAME, SFX_FRAME,
SFX(0x0,0xB1), SFX(0x2,0x54), SFX_FRAME, SFX_FRAME,
SFX(0x0,0xB1), SFX(0x2,0x3F), SFX_FRAME, SFX_FRAME, SFX_END };
const uint8 SFX_RESPAWN[] = { SFX_BEGIN,
SFX(0x0,0x71), SFX(0x2,0x59), SFX_FRAME, SFX_FRAME,
SFX(0x0,0x72), SFX(0x2,0x54), SFX_FRAME, SFX_FRAME,
SFX(0x0,0x73), SFX(0x2,0x4F), SFX_FRAME, SFX_FRAME,
SFX(0x0,0x74), SFX(0x2,0x4B), SFX_FRAME, SFX_FRAME,
SFX(0x0,0x73), SFX(0x2,0x59), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x54), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x4F), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x4B), SFX_FRAME, SFX_FRAME,
SFX(0x0,0x71), SFX(0x2,0x59), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x54), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x4F), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x4B), SFX_FRAME, SFX_FRAME, SFX_END };
const uint8 SFX_BOUNCE0[] = { SFX_BEGIN,
SFX(0xC,0x31), SFX(0xE,0x0D), SFX_FRAME,
SFX(0xC,0x32), SFX_FRAME,
SFX(0xC,0x31), SFX_FRAME,
SFX_FRAME, SFX_FRAME, SFX_END };
const uint8 SFX_BOUNCE1[] = { SFX_BEGIN,
SFX(0xC,0x31), SFX(0xE,0x0E), SFX_FRAME,
SFX(0xC,0x35), SFX_FRAME,
SFX(0xC,0x33), SFX_FRAME,
SFX(0xC,0x32), SFX_FRAME,
SFX(0xC,0x31), SFX_FRAME, SFX_END };
const uint8 SFX_BOUNCE2[] = { SFX_BEGIN,
SFX(0xC,0x32), SFX(0xE,0x0C), SFX_FRAME,
SFX(0xC,0x35), SFX_FRAME,
SFX(0xC,0x34), SFX_FRAME,
SFX(0xC,0x32), SFX_FRAME,
SFX(0xC,0x31), SFX_FRAME, SFX_END };
const uint8 SFX_SPLASH[] = { SFX_BEGIN,
SFX(0xC,0x36), SFX(0xE,0x0F), SFX_FRAME,
SFX(0xC,0x36), SFX(0xE,0x09), SFX_FRAME, SFX_FRAME, SFX_FRAME,
SFX(0xC,0x38), SFX(0xE,0x06), SFX_FRAME,
SFX(0xC,0x3C), SFX_FRAME,
SFX(0xC,0x39), SFX_FRAME,
SFX(0xC,0x38), SFX_FRAME,
SFX(0xC,0x37), SFX_FRAME,
SFX(0xC,0x36), SFX_FRAME,
SFX(0xC,0x35), SFX_FRAME,
SFX(0xC,0x34), SFX_FRAME,
SFX(0xC,0x33), SFX_FRAME,
SFX(0xC,0x32), SFX_FRAME, SFX_FRAME, SFX_FRAME, SFX_FRAME,
SFX(0xC,0x31), SFX_FRAME, SFX_FRAME, SFX_FRAME, SFX_FRAME,
SFX_FRAME, SFX_FRAME, SFX_END };
const uint8 SFX_TEE[] = { SFX_BEGIN,
SFX(0x0,0xB2), SFX(0x2,0x9D), SFX(0x3,0x05), SFX_FRAME,
SFX(0x2,0xF9), SFX(0x3,0x02), SFX_FRAME,
SFX(0x2,0x9D), SFX(0x3,0x05), SFX_FRAME,
SFX(0x2,0x4C), SFX(0x3,0x05), SFX_FRAME,
SFX(0x0,0xB3), SFX(0x2,0x80), SFX(0x3,0x02), SFX_FRAME,
SFX(0x2,0xB8), SFX(0x3,0x04), SFX_FRAME,
SFX(0x2,0x00), SFX(0x3,0x05), SFX_FRAME,
SFX(0x2,0x5C), SFX(0x3,0x02), SFX_FRAME,
SFX(0x2,0x74), SFX(0x3,0x04), SFX_FRAME,
SFX(0x2,0x34), SFX(0x3,0x04), SFX_FRAME,
SFX(0x2,0xFB), SFX(0x3,0x01), SFX_FRAME,
SFX(0x2,0xBF), SFX(0x3,0x03), SFX_FRAME,
SFX(0x2,0x89), SFX(0x3,0x03), SFX_FRAME,
SFX(0x2,0xAB), SFX(0x3,0x01), SFX_FRAME,
SFX(0x2,0x89), SFX(0x3,0x03), SFX_FRAME,
SFX(0x2,0xCE), SFX(0x3,0x02), SFX_FRAME,
SFX(0x0,0xB1), SFX(0x2,0x89), SFX(0x3,0x03), SFX_FRAME,
SFX(0x2,0xAB), SFX(0x3,0x01), SFX_FRAME,
SFX(0x2,0x89), SFX(0x3,0x03), SFX_FRAME,
SFX(0x2,0xCE), SFX(0x3,0x02), SFX_FRAME, SFX_END };
const uint8 SFX_PROMPT0[] = { SFX_BEGIN,
SFX(0x0, 0xB3), SFX(0x2,0xFB), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x2,0xC4), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x52), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x0, 0xB1), SFX_FRAME, SFX_FRAME,
SFX_FRAME, SFX_FRAME, SFX_END };
const uint8 SFX_PROMPT1[] = { SFX_BEGIN,
SFX(0x0, 0x73), SFX(0x2,0x7C), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x2,0xAB), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x3A), SFX(0x3,0x02), SFX_FRAME, SFX_FRAME,
SFX(0x0, 0x71), SFX_FRAME, SFX_FRAME,
SFX_FRAME, SFX_FRAME, SFX_END };
const uint8 SFX_PROMPT2[] = { SFX_BEGIN,
SFX(0x0, 0xB3), SFX(0x2,0xAB), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x52), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x1C), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x0, 0xB1), SFX_FRAME, SFX_FRAME,
SFX_FRAME, SFX_FRAME, SFX_END };
const uint8 SFX_PROMPT3[] = { SFX_BEGIN,
SFX(0x0, 0x33), SFX(0x2,0xFD), SFX(0x3,0x00), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x2D), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x2,0x7C), SFX(0x3,0x01), SFX_FRAME, SFX_FRAME,
SFX(0x0, 0x31), SFX_FRAME, SFX_FRAME,
SFX_FRAME, SFX_FRAME, SFX_END };
const uint8* const SFX_PROMPTS[4] = {
SFX_PROMPT0,
SFX_PROMPT1,
SFX_PROMPT2,
SFX_PROMPT3,
};
//
// misc
//
void debug_assert(int v)
{
if (!v)
{
ppu_profile(0x81); // red tinted grey
while(1); // infinite loop
}
}
uint16 mag_squared_s8(sint8 x, sint8 y)
{
if (x < 0) x = -x;
if (y < 0) y = -y;
return ((uint16)x * x) + ((uint16)y * y);
}
//
// graphical stuff
//
void palette_generate(uint8 sky, uint8 ground, uint8 text)
{
palette[ 0] = palette[16] =
palette[ 2] = palette[10] =
palette[ 5] = palette[13] = sky;
palette[26] =
palette[ 9] = palette[11] =
palette[14] = palette[15] = ground;
palette[ 1] = palette[ 3] =
palette[ 6] = palette[ 7] = text;
//palette[26] = 0x21; // for debugging tee
if (ocean_attribute != 255)
{
palette[3] = WATER_COLOUR;
}
}
void ball_draw_setup()
{
// colour for current player
i = BALL_COLOUR[player];
palette[17] = i | 0x10;
palette[18] = i | 0x20;
palette[19] = i | 0x30;
// 3 remaining players packed into one palette
balls_draw[0] = player;
for (i=1; i<4; ++i)
{
if (i >= players) balls_draw[i] = 4; // hidden
else balls_draw[i] = (player + i) % players;
}
for (i=1; i<4; ++i)
{
palette[20+i] = BALL_COLOUR[balls_draw[i]] | 0x10;
}
}
uint8 attribute(tl,tr,bl,br)
{
return tl|(tr<<2)|(bl<<4)|(br<<6);
}
void ppu_text(const char* text, uint16 addr)
{
ptr = (uint8*)text;
nx = addr;
ppu_latch(nx);
i = *ptr;
while (i)
{
if (i == '\n')
{
nx += 32;
ppu_latch(nx);
}
else ppu_write(i);
++ptr;
i = *ptr;
}
}
void cls() // erase nametables
{
ppu_latch(0x2000);
ppu_fill(0x00,0x1000);
}
void sprite_begin()
{
oam_pos = 4;
// sprite 0 always untouched
}
void sprite_end()
{
//while (oam_pos != 0)
while (oam_pos < (32*4)) // leaves last 32 for weather
{
oam[oam_pos] = 0xFF;
oam_pos += 4;
}
}
void sprite_add(uint8 tile, uint8 x, uint8 y, uint8 attrib)
{
(oam+2)[oam_pos] = attrib;
(oam+0)[oam_pos] = (uint8)(y-1);
(oam+3)[oam_pos] = x;
(oam+1)[oam_pos] = tile;
oam_pos += 4;
}
//
// floor generation
//
void floor_build_pixel_calculate_range()
{
fg_min_soft = (fg_min/4) + (fg_max/4) + (fg_min/2);
fg_max_soft = (fg_min/4) + (fg_max/4) + (fg_max/2);
}
void floor_build_pixel_angle()
{
fg_hold = (prng() & fg_hold_mask) + 8;
fg_angle = prng() & fg_angle_mask;
// drive toward centre
if (fg_last < fg_min_soft) fg_angle &= 0x7F;
else if (fg_last > fg_max_soft) fg_angle |= 0x80;
}
void floor_build_pixel_next()
{
fg_next = fg_last + read_slope(fg_angle);
}
const uint8 HOLE_ANGLE[8] = { 4, 4, 0, 0, 0x80+0, 0x80+0, 0x80+4, 0x80+4 };
// builds one pixel worth of floor
// note that this should always be 8 pixels ahead of floor_column,
// so that floor_render_prepare has 8 pixels worth of stuff to build,
// (and floor_column should be another 8 pixels ahead of the scroll)
void floor_build_pixel()
{
uint16 h;
if (ocean_attribute != 255) goto build;
h = fg_cx - hole_cx;
if (h < 8)
{
if (hole == 0)
{
ocean_attribute = ((floor_column + 1) / 4) & 15;
fg_last = fg_next = OCEAN_FLOOR * 256U;
fg_angle = 0;
palette[3] = WATER_COLOUR;
goto build;
}
fg_hold = 0;
fg_angle = HOLE_ANGLE[(uint8)h];
floor_build_pixel_next();
goto build;
}
if (fg_hold) --fg_hold;
else floor_build_pixel_angle();
floor_build_pixel_next();
if (fg_next < fg_min)
{
floor_build_pixel_angle();
fg_angle &= 0x7F; // force positive
floor_build_pixel_next();
}
else if (fg_next > fg_max)
{
floor_build_pixel_angle();
fg_angle |= 0x80; // force negative
floor_build_pixel_next();
}
build:
floor_y[fg_cx] = ((fg_last/2) + (fg_next/2)) / 256;
floor_a[fg_cx] = fg_angle;
fg_last = fg_next;
fg_cx = (fg_cx + 1) & 511;
}
//
// hole generation
//
// snow rain brwn pink dbrn gren purp yell, night...
const uint8 set_f[16] = { 0x30, 0x1A, 0x17, 0x25, 0x07, 0x19, 0x13, 0x27, 0x30, 0x1A, 0x17, 0x15, 0x07, 0x19, 0x13, 0x37 };
const uint8 set_s[16] = { 0x21, 0x2B, 0x27, 0x35, 0x17, 0x21, 0x23, 0x37, 0x0F, 0x0B, 0x0F, 0x05, 0x0F, 0x09, 0x03, 0x01 };
const uint8 set_t[16] = { 0x0F, 0x30, 0x0F, 0x0F, 0x30, 0x0F, 0x0F, 0x0F, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };
const uint8 set_w[16] = { 0x39, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// 18 hole repeating course terrain structure
// angle mask:
// 0 artificial angular, mostly flat
// 1 favour moderate slopes
// 2 all slopes
#define AM0 0x9F
#define AM1 0xBF
#define AM2 0xFF
// hold mask:
// 0 short segments
// 1 medium segments
// 2 long segments
#define HM0 15
#define HM1 31
#define HM2 63
const uint8 course_hm[18] = { HM1,HM0,HM1,HM0,HM1,HM1,HM2,HM1,HM1, HM1,HM1,HM2,HM1,HM0,HM2,HM1,HM2,HM1 };
const uint8 course_am[18] = { AM0,AM0,AM1,AM1,AM1,AM2,AM1,AM1,AM2, AM1,AM2,AM1,AM2,AM2,AM2,AM1,AM2,AM1 };
void hole_next()
{
++hole;
for (i=0;i<3;++i)
{
++hole_digits[i];
if (hole_digits[i] < 10) break;
hole_digits[i] = 0;
}
#if LAST_HOLE_TEST
if (hole == LAST_HOLE_TEST) hole = 0;
#endif
if (te) // TE configurable number of holes
{
if (hole == (holes & 0xFF)) hole = 0;
}
fg_hold_mask = course_hm[course];
fg_angle_mask = course_hm[course];
++course; if (course >= 18) course = 0;
mx = hole_cx - scroll_cx;
if (hole_cx < scroll_cx) mx += 512;
i = (mx / 8) + 7; // min screen column of next hole
j = i + 13; // max screen column of next hole
if (!hole) j = i+1; // "last hole" should be close
if (i < 34) i = 34; // must be at least 2 tiles offscreen
k = j - i; // allowed range
if (k == 0 || k >= 16) k = 1; // prevent invalid ranges (shouldn't occur?)
l = i + (prng() % k); // new hole column
ASSERT((scroll_cx & 7) == 0);
if (!hole)
{
// final hole must fall on 32-pixel attribute boundary
while (((scroll_cx + (l*8)) & 24) != 0) ++l;
// force terrain to go down toward ocean
fg_min = (GLOBAL_FLOOR - 8) * 256U;
floor_build_pixel_calculate_range();
}
tee_cx = hole_cx; // last hole becomes new tee
hole_cx = (scroll_cx + (l * 8)) & 511; // place new hole
hole_sx = l * 8; // screen position of new hole
if (!hole) hole_sx = 2048; // place it offscreen for the ocean
}
//
// common animation
//
#define TRANSITION_TIME 16
#define DAY_TIME 8
void transition_animate()
{
uint8 s, f;
--transition;
if (transition == ((TRANSITION_TIME*3)/4))
{
s = blend25(pal_sky, set_s[field_set]);
f = blend25(pal_floor, set_f[field_set]);
palette_generate(s,f,s);
}
else if (transition == ((TRANSITION_TIME*2)/4))
{
s = blend50(pal_sky, set_s[field_set]);
f = blend50(pal_floor, set_f[field_set]);
palette_generate(s,f,s);
}
else if (transition == ((TRANSITION_TIME*1)/4))
{
s = blend25(set_s[field_set], pal_sky);
f = blend25(set_f[field_set], pal_floor);
palette_generate(s,f,s);
}
else if (transition == 0)
{
pal_sky = set_s[field_set];
pal_floor = set_f[field_set];
palette_generate(pal_sky, pal_floor, pal_sky);
}
}
void weather_fade_automask()
{
// scale back the randomness at each power of 2
if ((weather_rate_min & (weather_rate_min-1)) == 0)
{
weather_rate_mask = weather_rate_min-1;
}
}
void weather_wind_random()
{
if (!te)
{
weather_wind_fade_dir = 1 - ((prng1() & 1) * 2); // 1 or -1, wind direction
weather_wind_fade_p = prng(); // wind strength
weather_wind_timeout = ((prng() & 15) + 32) * 64; // 30-50 seconds before wind changes
}
else
{
weather_wind_fade_dir = 1 - ((prngw1() & 1) * 2); // 1 or -1, wind direction
weather_wind_fade_p = prngw(); // wind strength
weather_wind_timeout = ((prngw() & 15) + 32) * 64; // 30-50 seconds before wind changes
}
}
void weather_fade()
{
if (weather_wind_timeout == 0)
weather_wind_random();
else
--weather_wind_timeout;
// smoothly change wind
if (weather_wind_dir != weather_wind_fade_dir)
{
if (weather_wind_p == 0) weather_wind_dir = weather_wind_fade_dir;
else --weather_wind_p;
}
else
{
if (weather_wind_p < weather_wind_fade_p) ++weather_wind_p;
else if (weather_wind_p > weather_wind_fade_p) --weather_wind_p;
}
// smoothly change particles drop rate
if (frame_count & 15) return;
if (set_w[field_set])
{
if (weather_rate_min == 0)
{
weather_rate_min = 64;
weather_fade_automask();
}
else if (weather_rate_min > 4)
{
--weather_rate_min;
weather_fade_automask();
}
}
else
{
if (weather_rate_min != 0)
{
++weather_rate_min;
weather_fade_automask();
if (weather_rate_min >= 65) weather_rate_min = 0;
}
}
}
void weather_attribute_set()
{
// sets speed and palette via whether tile is rain or snow
// the unused bits of the OAM attribute byte are used to control particle fall speed
weather_attribute = (weather_tile == 0x38) ? ((4<<2)|3) : ((1<<2)|2);
}
void flag_animate()
{
uint8 t;
if (flag_remove == 0) return;
t = hole_sy - 32;
if (t > hole_sy)
{
hole_sy = 255;
flag_remove = 0;
return;
}
hole_sy = t;
}
void frame()
{
if (transition) transition_animate();
weather_fade();
weather_animate();
flag_animate();
PROFILE();
ppu_post(POST_UPDATE);
++frame_count;
}
void frame_double()
{
weather_fade();
weather_animate();
flag_animate();
PROFILE();
ppu_post(POST_DOUBLE);
++frame_count;
}
void frames(uint8 count)
{
while (count--) frame();
}
void delay(uint8 frames)
{
while (frames--) frame();
}
//
// main menu
//
const char help_text[] =
" NESert Golfing\n"
" Brad Smith, 2019\n"
" http://rainwarrior.ca\n"
"\n"
"GAMEPAD CONTROL:\n"
" Hold A to begin swing,\n"
" use directions to aim,\n"
" release to stroke.\n"
" Hold B for fine control.\n"
"\n"
"MOUSE CONTROL:\n"
" Hold left button to begin\n"
" swing, drag to aim,\n"
" release to stroke.\n"
" Hold right button for\n"
" fine control.\n"
"\n"
" NESert Golfing was derived\n"
" from the original game\n"
" Desert Golfing\n"
" by Justin Smith, 2014.";
const char title_text[] =
" Play\n"
"\n"
"How many? 1 2 3 4\n"
"\n"
" Help";
const char help_text_te[] =
" NESert Golfing " VERSION_STRING "\n"
" Brad Smith, 2019-2022\n"
" http://rainwarrior.ca\n"
"\n"
"GAMEPAD CONTROL:\n"
" Hold A to begin swing,\n"
" use directions to aim,\n"
" release to stroke.\n"
" Hold B for fine control.\n"
"\n"
"MOUSE CONTROL:\n"
" Hold left button to begin\n"
" swing, drag to aim,\n"
" release to stroke.\n"
" Hold right button for\n"
" fine control.\n"
"\n"
" NESert Golfing was derived\n"
" from the original game\n"
" Desert Golfing\n"
" by Justin Smith, 2014.";
const char title_text_te[] =
" Play\n"
"\n"
"How many? 1 2 3 4\n"