-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2035 lines (1774 loc) · 62.8 KB
/
main.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
/*
* StickGBC by Matt Comben is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
* To view a copy of this license, visit
* http://creativecommons.org/licenses/by-nc-nd/4.0/.
*/
/*#include <gb/drawing.h>*/
#include <stdio.h>
#include <stdlib.h>
#include <types.h>
#include <gb/gb.h>
#include <gb/drawing.h>
#include "main_map_tileset.h"
#include "main_map.h"
#include "main_map_palette.h"
#include "main_map_boundaries.h"
#include "main_game.h"
#include "logic_functions.h"
#include "cheat.h"
#include "building_menu_tiles.h"
#include "building_menu_tiles_2.h"
#include "building_menu_map.h"
#include "building_menu_palette.h"
#include "main_map_sprite_tileset.h"
#include "main_map_sprite_palette.h"
#include "opening_screen.h"
#include "endgame.h"
#include "bar_fight.h"
#include "background_time_colors.h"
#include "game_constants.h"
#include "game_state.c"
#include "menu_config.h"
#include "screen_state.c"
#include "window.h"
#include "sprite.h"
#include "joy.h"
#include "window_text.h"
#include "window_text_data.h"
#include "balance.h"
#include "casino.h"
#include "main.h"
UBYTE * debug_address;
// Temporary storege for transfer of tile data and tile data vram1 data
UBYTE tile_data[1];
UWORD word_data[4];
// Storage for scratch palette data
UWORD scratch_palette_data[4][4];
joypad_state_t joypad_state;
// Game state
game_state_t game_state;
screen_state_t screen_state;
// Define global instance of menu config
menu_config_t *menu_config;
menu_state_t menu_state;
// General iterators
unsigned int itx_start;
unsigned int itx_end;
unsigned int itx;
// Bunch of variables from load_menu_tiles.
// This fixed a compiler error, which gave no
// indication of issue and no resources online.
unsigned int itx_x;
unsigned int itx_y;
UINT8 menu_item_index;
unsigned int tile_index;
unsigned int tile_data_index;
UINT8 tile_itx_x_start;
UINT8 tile_itx_y_start;
UINT8 tile_itx_x;
UINT8 tile_itx_y;
UINT8 second_tile_row;
// Main player sprite
ai_sprite player_sprite = {
// Speed
0x00U,
// Sprite index
0x00U,
// Sprite bit index
0x00U,
// Color palette,
0x00U,
// Sprite count
0x01U,
0x01U,
// Sprite base tile
SPRITE_TILESET_WALK,
// Fake values, since the
// main player has different state for these
// Travel X/Y, rest direction, start location, min/max x, min/max Y
0x00, 0x00, 0x00, 0x00, 0x0U, 0x0U, 0x0U, 0x0U, 0x0U, 0x0U,
// Pause period
0x40U, 0x00U,
};
// Setup skater sprite
ai_sprite skater_sprite = {
// Speed
0x05U,
// Sprite index
0x01U,
// Sprite bit index
0x00U,
// Color palette,
0x01U,
// Sprite count
0x01U,
0x01U,
// Sprite base tile
SPRITE_TILESET_WALK,
// Travel X (right)
0x01,
// Travel Y
0x00,
// Rest direction X/Y (face down)
0x00,
0x01,
// Start location x, y
0xD8U,
0x58U,
// Min/max X location
0xD8U,
0xE8U,
// Min/max Y location
0x58U,
0x58U,
// Pause period and current pause.
0x0FU,
0x00U,
};
// Setup dealer sprite
ai_sprite dealer_sprite = {
// Speed
0x05U,
// Sprite index
0x02U,
// Sprite bit index
0x01U,
// Color palette,
0x02U,
// Sprite count
0x01U,
0x01U,
// Sprite base tile
SPRITE_TILESET_WALK,
// Travel X
0x00,
// Travel Y (down)
0x01,
// Rest direction X/Y (face down)
-1,
0x00,
// Start location x, y
0x157U,
0x173U,
// Min/max X location
0x157U,
0x157U,
// Min/max Y location
0x173U,
0x17BU,
// Pause period and current pause.
0x0FU,
0x00U,
};
// Setup house car sprite
ai_sprite house_car_sprite = {
// Speed
0x00U,
// Base sprite index (uses 4 (3 through 6) for 2x2 tiles)
0x03U,
// Sprite bit index
0x02U,
// Color palette,
0x03U,
// Sprite count (2 x 2)
0x02U,
0x02U,
// Sprite base tile
0x08U,
// Travel X (right)
0x01,
// Travel Y
0x00,
// Rest direction X/Y (face right)
0x01,
0x00,
// Start location x, y
0x3BU,
0x58U,
// Min/max X location
0x3BU,
0x44U,
// Min/max Y location
0x58U,
0x58U,
// Pause period and current pause.
0x00U,
0x00U,
};
// Setup AI road car
ai_sprite road_car_sprite = {
// Speed
0x01U,
// Base sprite index (uses 4 (7 through A) for 2x2 tiles)
0x07U,
// Sprite bit index
0x03U,
// Color palette,
0x03U,
// Sprite count (2 x 2)
0x02U,
0x02U,
// Sprite base tile
0x08U,
// Travel X
0x00,
// Travel Y (down - though this is randomised)
0x01,
// Rest direction X/Y (face down)
0x00,
0x01,
// Start location x, y
0x110U,
0x58U,
// Min/max X location
0x110U,
0x110U,
// Min/max Y location
0x0U,
0x1C0U,
// Pause period and current pause.
0x80U,
0x00U,
};
void add_debug(UBYTE val)
{
*debug_address = val;
debug_address ++;
}
// Update background color based on time of day
void update_background_color()
{
UWORD palette_data[4];
// Copy palette 1
ROM_BANK_TIME_COLORS_SWITCH;
palette_data[0U] = background_time_colors[game_state.hour],
ROM_BANK_TILE_DATA_SWITCH;
palette_data[1U] = main_map_palette[1U];
palette_data[2U] = main_map_palette[2U];
palette_data[3U] = main_map_palette[3U];
ROM_BANK_RESET;
set_bkg_palette(0U, 1U, &palette_data);
}
void setup_globals()
{
game_state.current_building = S_B_NO_BUILDING;
game_state.sub_menu = S_M_NO_SUBMENU;
game_state.game_ended = 0;
game_state.last_movement_time = 0x0U;
// @TODO make sure display works after 999
game_state.days_passed = 0U;
game_state.hour = S_HOUR_WAKEUP_NORMAL;
// Start with $100
game_state.balance[0] = 100U;
game_state.balance[1] = 0U;
game_state.bank_balance = 0U;
game_state.loan = 0U;
game_state.loan_days = 0U;
game_state.bank_rate = (sys_time % 50) + 1;
// Randomise initial stats.
// Use different modulus operands to get different results.
game_state.intelligence = (sys_time % 10);
game_state.strength = (sys_time % 11);
game_state.charm = (sys_time % 12);
game_state.karma = 0;
game_state.hobo_given_money = 0U;
game_state.hobo_given_beer = 0U;
game_state.intro_shown = 0U;
// Set HP twice - for some reason, the initial set
// causes a weird issue. Assigning hp to max_hp works.
// Then setting hp again from this value fixes the issue.
game_state.hp = S_INITIAL_BASE_HP + game_state.strength;
game_state.max_hp = game_state.hp;
game_state.hp = game_state.max_hp;
screen_state.displayed_sprites_x[skater_sprite.sprite_display_bit] = 0U;
screen_state.displayed_sprites_y[skater_sprite.sprite_display_bit] = 1U;
screen_state.displayed_sprites_x[dealer_sprite.sprite_display_bit] = 0U;
screen_state.displayed_sprites_y[dealer_sprite.sprite_display_bit] = 0U;
screen_state.displayed_sprites_x[house_car_sprite.sprite_display_bit] = 1U;
screen_state.displayed_sprites_y[house_car_sprite.sprite_display_bit] = 1U;
screen_state.displayed_sprites_x[road_car_sprite.sprite_display_bit] = 0U;
screen_state.displayed_sprites_y[road_car_sprite.sprite_display_bit] = 1U;
// Mark screen as having moved, so that sprites are initially placed on-screen
screen_state.screen_has_moved = 1U;
// Setup buildings that do not transition in some axis
// and those that are displayed on start of game.
screen_state.displayed_buildings_x = SC_HOUSE | SC_RESTAURANT | SC_SHOP | SC_PAWN;
screen_state.displayed_buildings_y = SC_HOUSE | SC_UNIVERSITY | SC_NLI;
screen_state.displayed_buildings_2_x = SC_APPLIANCE_STORE | SC_APPLIANCE_STORE_CASINO_SHARED | SC_CASINO;
screen_state.displayed_buildings_2_y = SC_BANK;
// Setup inventory items
game_state.inventory[S_INVENTORY_SMOKES] = 0x0U;
game_state.inventory[S_INVENTORY_CAFFEINE_PILLS] = 0x0U;
game_state.inventory[S_INVENTORY_HAND_GUN] = 0x0U;
game_state.inventory[S_INVENTORY_KNIFE] = 0x0U;
game_state.inventory[S_INVENTORY_ALARM_CLOCK] = 0x0U;
game_state.inventory[S_INVENTORY_CELL_PHONE] = 0x0U;
game_state.inventory[S_INVENTORY_SKATEBOARD] = 0x0U;
game_state.inventory[S_INVENTORY_COCAINE] = 0x0U;
game_state.inventory[S_INVENTORY_BOTTLE_OF_BEER] = 0x0U;
game_state.inventory[S_INVENTORY_BED] = 0x0U;
game_state.inventory[S_INVENTORY_TV] = 0x0U;
game_state.inventory[S_INVENTORY_PC] = 0x0U;
game_state.inventory[S_INVENTORY_DEEP_FREEZE] = 0x0U;
game_state.inventory[S_INVENTORY_SATELLITE] = 0x0U;
game_state.inventory[S_INVENTORY_TREADMILL] = 0x0U;
game_state.inventory[S_INVENTORY_STICKOPEDIA] = 0x0U;
game_state.inventory[S_INVENTORY_MINIBAR] = 0x0U;
screen_state.screen_location_x = 0x10U;
screen_state.screen_location_x_tiles = screen_state.screen_location_x >> 3;
screen_state.screen_location_y = 0x10U;
screen_state.screen_location_y_tiles = screen_state.screen_location_y >> 3;
game_state.user_pos_x = 0x70U;
game_state.user_pos_y = 0x70U;
game_state.user_pos_tiles_x = PIXEL_LOCATION_TO_TILE_COUNT(game_state.user_pos_x);
game_state.user_pos_tiles_y = PIXEL_LOCATION_TO_TILE_COUNT(game_state.user_pos_y);
game_state.bar_fight_count = 0U;
#ifdef IN_TESTING
// Add hacks for testing
game_state.inventory[S_INVENTORY_SKATEBOARD] = 0x1U;
game_state.inventory[S_INVENTORY_SMOKES] = 0x1U;
game_state.inventory[S_INVENTORY_COCAINE] = 49U;
game_state.inventory[S_INVENTORY_BOTTLE_OF_BEER] = 49U;
game_state.inventory[S_INVENTORY_HAND_GUN] = 0x1U;
game_state.inventory[S_INVENTORY_AMMO] = 0x10U;
game_state.inventory[S_INVENTORY_CELL_PHONE] = 0x1U;
game_state.balance[0U] = 8000U;
game_state.balance[1U] = 0U;
game_state.bank_balance = 3000U;
game_state.max_hp = 100U;
game_state.intelligence = 250U;
game_state.strength = 90U;
game_state.charm = 99U;
game_state.hour = 0;
game_state.bar_fight_count = 5U;
#endif
}
void update_ai_positions()
{
#if IN_TESTING && DEBUG_DISABLE_AI_MOVEMENT
return;
#endif
ROM_BANK_SPRITE_SWITCH;
move_ai_sprite(&screen_state, &skater_sprite);
move_ai_sprite(&screen_state, &dealer_sprite);
move_ai_sprite(&screen_state, &house_car_sprite);
move_ai_sprite(&screen_state, &road_car_sprite);
// Perform special checks for
check_road_car_onscreen(&screen_state, &road_car_sprite);
ROM_BANK_RESET;
}
void main_set_bkg_data(UINT8 start_index, UINT8 cnt, unsigned char *data_ptr, UINT8 data_bank, UINT8 return_bank) NONBANKED
{
// Switch to data bank
SWITCH_ROM_MBC5(data_bank);
set_bkg_data(start_index, cnt, data_ptr);
// Reset ROM bank to original
SWITCH_ROM_MBC5(return_bank);
}
void set_background_tiles(unsigned int map_data_bank, unsigned int tile_data_bank, unsigned int palette_data_bank, unsigned int return_bank) NONBANKED
{
// @TODO Fix the increment
//unsigned long current_tile_itx = FRAME_BUFFER_TILE_POS_X + (FRAME_BUFFER_TILE_POS_Y * mainmapWidth);
unsigned int current_tile_itx = 0;
unsigned int current_tile_data_itx = 0;
unsigned int current_tile_palette_itx = 0;
unsigned int max_x;
unsigned int max_y;
// If loading main map, use screen_location variables
// to offset tiles
max_x = screen_state.draw_offset_x + screen_state.draw_max_x;
max_y = screen_state.draw_offset_y + screen_state.draw_max_y;
// Load color palette
SWITCH_ROM_MBC5(palette_data_bank);
set_bkg_palette(0, 8, screen_state.background_color_palette);
VBK_REG = 0;
SWITCH_ROM_MBC5(tile_data_bank);
set_bkg_data(0, 8, screen_state.background_tiles);
ROM_BANK_BUILDING_MENU_SWITCH;
// Load in digits/symbols from building menu tiles, including clock tiles before it
set_bkg_data(MENU_ROW_2_TILE_DATA_OFFSET, 31U, &(buildingmenutiles[(MENU_ROW_2_TILE_DATA_OFFSET) << 4U]));
ROM_BANK_RESET;
for (itx_x = screen_state.draw_offset_x;
itx_x != max_x;
itx_x ++)
{
#if IN_TESTING && DEBUG_SET_BACKGROUND_SKIP
// TEMP HACK TO NOT DRAW MOST OF BACKGROUND IN VRAM
if (itx_x == 0x10U)
break;
#endif
for (itx_y = screen_state.draw_offset_y;
itx_y != max_y;
itx_y ++)
{
// Temp Test
current_tile_itx = ((itx_y) * screen_state.background_width) + itx_x;
#if IN_TESTING && DEBUG_SET_BACKGROUND_SKIP
// TEMP HACK TO NOT DRAW MOST OF BACKGROUND IN VRAM
if (itx_y == 0x10U)
break;
#endif
// Tile data is split across two bytes. In the layout:
// 0-6 - tile number
// 7 - blank
// 8-A - palette number
// B - vertical flip
// C - horizontal flip
current_tile_data_itx = current_tile_itx * 2;
current_tile_palette_itx = current_tile_data_itx + 1;
SWITCH_ROM_MBC5(map_data_bank);
tile_data[0] = screen_state.background_tile_map[current_tile_data_itx] & 0x7F;
ROM_BANK_RESET;
#if IN_TESTING && DEBUG_BOUNDARIES
ROM_BANK_BOUNDARY_DATA_SWITCH;
// Check if tile is a boundary
if (TILE_INDEX_BIT_MAP_VALUE(MAIN_MAP_BOUNDARIES, current_tile_itx))
// Random tile
tile_data[0] = 0x06;
ROM_BANK_RESET;
#endif
VBK_REG = 0;
// Set map data
set_bkg_tiles(
itx_x & BACKGROUND_BUFFER_MAX_X,
itx_y & BACKGROUND_BUFFER_MAX_Y,
1, 1, // Only setting 1 tile
// Lookup tile from background tile map
&(tile_data[0])
);
VBK_REG = 1;
SWITCH_ROM_MBC5(map_data_bank);
// Lookup tile palette from background tile map
tile_data[0] = screen_state.background_tile_map[current_tile_palette_itx] & 0x07;
// Bit shift last bit of tile index to determine low or high bank (bit 3)
tile_data[0] |= (screen_state.background_tile_map[current_tile_data_itx] & 0x80U) >> 4;
// Check if current tile is flipped
if (screen_state.background_tile_map[current_tile_palette_itx] & 0x08)
tile_data[0] |= S_FLIPY;
if (screen_state.background_tile_map[current_tile_palette_itx] & 0x10)
tile_data[0] |= S_FLIPX;
ROM_BANK_RESET;
#if IN_TESTING && DEBUG_BOUNDARIES
ROM_BANK_BOUNDARY_DATA_SWITCH;
// Check if tile is a boundary
if (TILE_INDEX_BIT_MAP_VALUE(MAIN_MAP_BOUNDARIES, current_tile_itx))
// Forth palette
tile_data[0] = 0x03;
ROM_BANK_RESET;
#endif
// Set palette data in VBK_REG1 for tile
set_bkg_tiles(
itx_x & BACKGROUND_BUFFER_MAX_X,
itx_y & BACKGROUND_BUFFER_MAX_Y,
1, 1, // Only setting 1 tile
&(tile_data[0])
);
// @TODO Fix this
//current_tile_itx ++;
}
// Add remainig undrawn tiles to itx of tiles
//current_tile_itx += (mainmapWidth - BACKGROUND_BUFFER_SIZE_Y);
}
// Reset VKG_REG to original value
VBK_REG = 0;
// Reset ROM bank to original
SWITCH_ROM_MBC5(return_bank);
}
void move_background(signed int move_x, signed int move_y) NONBANKED
{
unsigned int itx_x;
unsigned int itx_y;
unsigned int base_itx_x;
unsigned int base_itx_y;
UINT16 current_tile_itx;
UINT16 current_tile_data_itx;
UINT16 current_tile_palette_itx;
unsigned int itx_x_max;
unsigned int itx_y_max;
if (move_x == 0 && move_y == 0)
{
delay(20);
return;
}
// Mark screen as having moved, which indicates to AI sprite to move,
// even if they are not themselves moving
screen_state.screen_has_moved = 1U;
scroll_bkg(move_x, move_y);
screen_state.screen_location_x += move_x;
screen_state.screen_location_y += move_y;
screen_state.screen_location_x_tiles = screen_state.screen_location_x >> 3;
screen_state.screen_location_y_tiles = screen_state.screen_location_y >> 3;
screen_state.screen_location_x_tilepixel = screen_state.screen_location_x & 0x07U;
screen_state.screen_location_y_tilepixel = screen_state.screen_location_y & 0x07U;
// Set current redraw in X to current user position (bit shift to remove pixels within tile) plus
// current frame buffer size + redraw offset.
// Mask with vram tile size in X, as a form of cheap modulus (which we can do as it is a power of 2)
base_itx_x = screen_state.screen_location_x >> 3;
if (move_x == 1)
// If moving to right, start redraw tile after
// Calulate entire width of screen, plus one tile either side.
base_itx_x += (SCREEN_TILE_MAX_X + 2U);
else if (move_x == -1)
// If moving left, redraw tile before
base_itx_x -= 1U;
// See X alternative
base_itx_y = screen_state.screen_location_y >> 3;
if (move_y == 1)
// If moving to right, start redraw tile after
base_itx_y += (SCREEN_TILE_MAX_Y + 2U);
else if (move_y == -1)
// If moving left, redraw tile before
base_itx_y -= 1U;
// Redraw tiles in unallocated vram
if (move_x != 0)
{
itx_x = base_itx_x;
// If moving in X, redraw column.
// The iterator is the frame buffer position (not the map position)
// Use screen_location_x_subpixel to divide up tile in entire row. Start with 0 on first pixel. Set end of iterator to start of next block of tiles.
itx_y_max = base_itx_y + (3U * (screen_state.screen_location_x_tilepixel + 1U)) + 1U;
for (itx_y = base_itx_y + (3U * screen_state.screen_location_x_tilepixel) - 1U;
itx_y != itx_y_max;
itx_y ++)
{
// Work out current tile - base on tile location in frame buffer plus current map in vram location
current_tile_itx = (itx_y * mainmapWidth) + itx_x;
current_tile_data_itx = current_tile_itx * 2;
current_tile_palette_itx = current_tile_data_itx + 1;
ROM_BANK_TILE_DATA_SWITCH;
tile_data[0] = screen_state.background_tile_map[current_tile_data_itx] & 0x7F;
ROM_BANK_RESET;
#if IN_TESTING && DEBUG_BOUNDARIES
ROM_BANK_BOUNDARY_DATA_SWITCH;
// Check if tile is a boundary
if (TILE_INDEX_BIT_MAP_VALUE(MAIN_MAP_BOUNDARIES, current_tile_itx))
// Random tile
tile_data[0] = 0x06;
ROM_BANK_RESET;
#endif
VBK_REG = 0;
// Set map data
set_bkg_tiles(
itx_x & BACKGROUND_BUFFER_MAX_X,
itx_y & BACKGROUND_BUFFER_MAX_Y,
1, 1, // Only setting 1 tile
// Lookup tile from background tile map
&(tile_data[0])
);
// Lookup tile from background tile map
ROM_BANK_TILE_DATA_SWITCH;
tile_data[0] = screen_state.background_tile_map[current_tile_palette_itx] & 0x07;
// Check if current tile is flipped
if (screen_state.background_tile_map[current_tile_palette_itx] & 0x08)
tile_data[0] |= S_FLIPY;
if (screen_state.background_tile_map[current_tile_palette_itx] & 0x10)
tile_data[0] |= S_FLIPX;
ROM_BANK_RESET;
#if IN_TESTING && DEBUG_BOUNDARIES
ROM_BANK_BOUNDARY_DATA_SWITCH;
// Check if tile is a boundary
if (TILE_INDEX_BIT_MAP_VALUE(MAIN_MAP_BOUNDARIES, current_tile_itx))
// Forth palette
tile_data[0] = 0x03;
ROM_BANK_RESET;
#endif
VBK_REG = 1;
// Set palette data in VBK_REG1 for tile
set_bkg_tiles(
itx_x & BACKGROUND_BUFFER_MAX_X,
itx_y & BACKGROUND_BUFFER_MAX_Y,
1, 1, // Only setting 1 tile
&(tile_data[0])
);
VBK_REG = 0;
}
}
if (move_y != 0)
{
itx_y = base_itx_y;
// If moving in X, redraw column.
// The iterator is the frame buffer position (not the map position)
itx_x_max = base_itx_x + (3U * (screen_state.screen_location_y_tilepixel + 1U)) + 1U;
for (itx_x = base_itx_x + (3U * screen_state.screen_location_y_tilepixel) - 1U;
itx_x != itx_x_max;
itx_x ++)
{
// Work out current tile - base on tile location in frame buffer plus current map in vram location
current_tile_itx = (itx_y * mainmapWidth) + itx_x;
current_tile_data_itx = current_tile_itx * 2;
current_tile_palette_itx = current_tile_data_itx + 1;
ROM_BANK_TILE_DATA_SWITCH;
tile_data[0] = screen_state.background_tile_map[current_tile_data_itx] & 0x7F;
ROM_BANK_RESET;
#if IN_TESTING && DEBUG_BOUNDARIES
ROM_BANK_BOUNDARY_DATA_SWITCH;
// Check if tile is a boundary
if (TILE_INDEX_BIT_MAP_VALUE(MAIN_MAP_BOUNDARIES, current_tile_itx))
// Random tile
tile_data[0] = 0x06;
ROM_BANK_RESET;
#endif
VBK_REG = 0;
// Set map data
set_bkg_tiles(
itx_x & BACKGROUND_BUFFER_MAX_X,
itx_y & BACKGROUND_BUFFER_MAX_Y,
1, 1, // Only setting 1 tile
// Lookup tile from background tile map
&(tile_data[0])
);
ROM_BANK_TILE_DATA_SWITCH;
// Lookup tile from background tile map
tile_data[0] = screen_state.background_tile_map[current_tile_palette_itx] & 0x07;
// Check if current tile is flipped
if (screen_state.background_tile_map[current_tile_palette_itx] & 0x08)
tile_data[0] |= S_FLIPY;
if (screen_state.background_tile_map[current_tile_palette_itx] & 0x10)
tile_data[0] |= S_FLIPX;
ROM_BANK_RESET;
#if IN_TESTING && DEBUG_BOUNDARIES
ROM_BANK_BOUNDARY_DATA_SWITCH;
// Check if tile is a boundary
if (TILE_INDEX_BIT_MAP_VALUE(MAIN_MAP_BOUNDARIES, current_tile_itx))
// Forth palette
tile_data[0] = 0x03;
ROM_BANK_RESET;
#endif
VBK_REG = 1;
// Set palette data in VBK_REG1 for tile
set_bkg_tiles(
itx_x & BACKGROUND_BUFFER_MAX_X,
itx_y & BACKGROUND_BUFFER_MAX_Y,
1, 1, // Only setting 1 tile
&(tile_data[0])
);
VBK_REG = 0;
}
}
}
// Setup globals to draw main map
void setup_main_map()
{
DISPLAY_OFF;
game_state.current_building = S_B_NO_BUILDING;
screen_state.background_color_palette = main_map_palette;
screen_state.background_tile_map = mainmap;
screen_state.background_tiles = mainmaptiles;
screen_state.background_width = mainmapWidth;
screen_state.draw_offset_x = screen_state.screen_location_x >> 3;
screen_state.draw_offset_y = screen_state.screen_location_y >> 3;
// If not starting from the left/top edge of the screen,
// move offset to top/left by 1, which will begin redraw one tile to the left.
// This means that the tile to the left and above will be drawn as the left/top
// (as opposed to being the tiles wrapped around from drawing to the right/down).
// This means that as the player moves to the left or up, the tiles are already drawn
// and don't cause incorrect tiles as the movement of the screne expects a buffer of
// 1 tile to have already been drawn for this direction of travel.
if (screen_state.draw_offset_x != 0)
{
screen_state.draw_offset_x -= 1;
}
if (screen_state.draw_offset_y != 0)
{
screen_state.draw_offset_y -= 1;
}
screen_state.draw_max_x = BACKGROUND_BUFFER_SIZE_X;
screen_state.draw_max_y = BACKGROUND_BUFFER_SIZE_Y;
set_background_tiles(ROM_BANK_TILE_DATA, ROM_BANK_MAIN_MAP_TILESET, ROM_BANK_TILE_DATA, 1U);
ROM_BANK_SPRITE_SWITCH;
setup_sprites(&player_sprite, &skater_sprite, &dealer_sprite, &house_car_sprite, &road_car_sprite);
ROM_BANK_RESET;
// Move background to screen location
scroll_bkg(
screen_state.screen_location_x & BACKGROUND_BUFFER_SIZE_PIXELS_MAX_X,
screen_state.screen_location_y & BACKGROUND_BUFFER_SIZE_PIXELS_MAX_Y
);
// Load additional tiles required for main map
ROM_BANK_MAIN_MAP_TILESET_SWITCH;
set_bkg_data(8U, 5U, &(mainmaptiles[8U << 4]));
// Load currently displayed buildings
ROM_BANK_TILE_DATA_SWITCH;
load_building_tile_data(&screen_state, &house_car_sprite, &road_car_sprite);
ROM_BANK_RESET;
update_background_color();
DISPLAY_ON;
// Set last_movement_time to current systime, allowing user to
// immediately move, in case it has been set to the future by
// another method
game_state.last_movement_time = sys_time;
}
/*
* load_menu_tiles
*
* Load each of the menu items on to the screen
* from the active menu_config, using the
* two-letter menu tiles.
*/
void load_menu_tiles(unsigned int return_bank) NONBANKED
{
unsigned int menu_item_itx;
// Reset VBK_REG
VBK_REG = 0;
// Iterate over all menu items and load palette data.
// Start from 1 , as first item column is 'exit'
for (itx_x = 0; itx_x != MENU_MAX_ITEMS_X; itx_x ++)
{
for (itx_y = 0; itx_y != MENU_MAX_ITEMS_Y; itx_y ++)
{
// Work out menu item index, based on co-ords
menu_item_index = (itx_y * MENU_MAX_ITEMS_X) + itx_x;
menu_item_itx = menu_config->items[menu_item_index];
// Ignore top right exit and item is exit
if (itx_x == 1U && itx_y == 0U && menu_item_itx == MENU_ITEM_INDEX_EXIT)
continue;
second_tile_row = 0U;
// Pad from left with offset on screen. The menu items are 7 + margin of 1, so times with itx_x.
tile_itx_x_start = MENU_ITEM_SCREEN_OFFSET_LEFT + (8U * itx_x);
tile_itx_y_start = MENU_ITEM_SCREEN_OFFSET_TOP + (3U * itx_y);
for (tile_index = 0U; tile_index != MENU_ITEM_TILE_COUNT; tile_index ++)
{
// Once second row of menu item data tiles is reached,
// mark as such
if (tile_index == MENU_ITEM_WIDTH)
{
tile_itx_x_start -= MENU_ITEM_WIDTH;
second_tile_row = 1U;
}
ROM_BANK_MENU_CONFIG_SWITCH;
tile_data_index = menu_config_items[menu_item_itx].tiles[tile_index];
// Only load data if tile contains data
if (tile_data_index != 0U)
{
ROM_BANK_BUILDING_MENU_SWITCH;
// Load tile data for menu item based on tile data offset
// in menu config and tile config in menu tile array
set_bkg_data(
tile_data_index,
1,
&(screen_state.background_tiles[tile_data_index << 4])
);
ROM_BANK_RESET;
}
tile_itx_x = tile_itx_x_start + tile_index;
tile_itx_y = tile_itx_y_start + second_tile_row;
tile_data[0] = tile_data_index;
// Set map data
set_bkg_tiles(
tile_itx_x,
tile_itx_y,
1U, 1U, // Only setting 1 tile
&(tile_data[0])
);
if (tile_data_index == MENU_ITEM_NO_TILE)
// If tile is empty, use blank palette
tile_data[0] = MENU_ITEM_DEFAULT_PALETTE;
else
{
ROM_BANK_MENU_CONFIG_SWITCH;
// Load default palette
tile_data[0] = menu_config_items[menu_item_itx].palette[tile_index];
ROM_BANK_RESET;
// If not palette data, specified, use default
if (tile_data[0] == MENU_ITEM_NO_PALETTE)
tile_data[0] = MENU_ITEM_DEFAULT_PALETTE;
}
VBK_REG = 1;
// Set palette data in VBK_REG1 for tile
set_bkg_tiles(
tile_itx_x,
tile_itx_y,
1, 1, // Only setting 1 tile
&(tile_data[0])
);
VBK_REG = 0;
}
}
}
SWITCH_ROM_MBC5(return_bank);
}
void setup_building_menu(UINT8 menu_number, unsigned int return_bank) NONBANKED
{
DISPLAY_OFF;
// Update globals for references to map/tile information
screen_state.background_tile_map = buildingmenumap;
if (menu_number == 1U)
screen_state.background_tiles = buildingmenutiles;
else if (menu_number == 2U)
screen_state.background_tiles = buildingmenutiles2;
screen_state.background_width = buildingmenumapWidth;
screen_state.background_color_palette = building_menu_palette;
// Draw top left of screen
screen_state.draw_offset_x = 0U;
screen_state.draw_offset_y = 0U;
screen_state.draw_max_x = SCREEN_WIDTH_TILES;
screen_state.draw_max_y = SCREEN_HEIGHT_TILES;
if (game_state.current_building == S_B_HOUSE)
{
// Menu has 3 items, default to sleep
menu_state.current_item_x = 1;
menu_state.current_item_y = 3;
menu_config = &menu_config_house;
}
else if (game_state.current_building == S_B_RESTAURANT)
{
// Menu has 3 items, default to fries
menu_state.current_item_x = 0;
menu_state.current_item_y = 1;
menu_config = &menu_config_restaurant;
}
else if (game_state.current_building == S_B_SHOP)
{
// Default to slushee
menu_state.current_item_x = 0U;
menu_state.current_item_y = 1U;
menu_config = &menu_config_shop;
}
else if (game_state.current_building == S_B_PAWN)
{
menu_config = &menu_config_pawn;
// Default to hand gun, if available
if (menu_config->items[MENU_PAWN_HAND_GUN_ITEM] != MENU_ITEM_INDEX_EMPTY) {
menu_state.current_item_x = 0U;
menu_state.current_item_y = 1U;
} else {
// Otherwise, default to ammo
menu_state.current_item_x = 0U;
menu_state.current_item_y = 0U;
}
}
else if (game_state.current_building == S_B_UNIVERSITY)
{
// Default to study
menu_state.current_item_x = 0U;
menu_state.current_item_y = 1U;
menu_config = &menu_config_university;
}
else if (game_state.current_building == S_B_SKATER)
{
// Default to study
menu_state.current_item_x = 0U;
menu_state.current_item_y = 0U;
menu_config = &menu_config_skater;