-
Notifications
You must be signed in to change notification settings - Fork 0
/
tacticsCore.c
2096 lines (1840 loc) · 49.6 KB
/
tacticsCore.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
/* lib includes */
#include <avr/io.h>
#include <stdlib.h>
#include <math.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
//#include <uzebox.h>
#include "kernel/uzebox.h"
/* data includes */
#include "res/tiles.inc"
#include "res/fontmap.inc"
#include "res/sprites.inc"
/* structs */
struct GridBufferSquare {
unsigned char unit; // index to Unit array; 0xff for no unit
unsigned char info; // terrain and player bitfield, also hasproduced
// pp.xx.a.ttt pp=player, a=has produced, ttt=terrain
};
struct Unit {
char isUnit;
char info; // unit type and player bitfield
char hp;
char other; // xxxxxx.ba, a=moved on turn, b=attacked on turn
unsigned char xPos;
unsigned char yPos;
};
struct Movement {
char direction;
char movePoints;
};
/* defines */
#ifndef OFF_SCREEN
#define OFF_SCREEN 28*8
#endif
#define LEVEL_HEIGHT 11
#define MAX_UNITS 40
#define MAX_PROPERTIES 20
#define MAX_LEVEL_WIDTH 30
#define MAX_VIS_WIDTH 14
#define TRUE 1
#define FALSE 0
#define EEPROM_INDEX 833
#define BLINK_UNITS 0
#define BLINK_TERRAIN 1
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? -(a) : (a))
#define MANH(x1, y1, x2, y2) (ABS((x1)-(x2)) + ABS((y1)-(y2)))
#define ERROR(msg) \
{Print(4,4,PSTR(msg));\
while(1)\
WaitVsync(1);}
// convert our player value to controller value
#define JPPLAY(pl) ((pl) == PL2 ? 1 : 0)
// level data masks
#define TERRAIN_MASK 0b00000111
#define UNIT_MASK 0b00111000
#define OWNER_MASK 0b11000000
// produced
#define HASPROD_MASK 0b00001000
#define HASPROD(x) ((x)&HASPROD_MASK)
#define SETHASPROD(x, y, v) levelBuffer[x][y].info = (levelBuffer[x][y].info&0xF7)|((v)<<3)
//unit stats masks
#define HASMOVED_MASK 0b00000001
#define HASATTACKED_MASK 0b00000010
#define HASMOVED(x) ((x)&HASMOVED_MASK)
#define HASATTACKED(x) ((x)&HASATTACKED_MASK)
// x is an index in the unit list
#define SETHASMOVED(x, y) unitList[x].other = (unitList[x].other&0xFE)|((y))
#define SETHASATTACKED(x, y) unitList[x].other = (unitList[x].other&0xFD)|((y)<<1)
#define MAX_UNIT_MP 10
// terrain types
#define PL 0x01 // plain
#define MO 0x02 // mountain
#define FO 0x03 // forest
#define CT 0x04 // city
#define BS 0x05 // base
#define NO_TERRAIN 0xFF //no terrain (when would there ever be no terrain?)
#define GETTERR(x) ((x)&TERRAIN_MASK)
#define INDEXTERR(x) (x)
// unit types
#define UN1 0x08
#define UN2 0x10
#define UN3 0x18
#define UN4 0x20
#define UN5 0x28
#define NO_UNIT 0xFF
#define GETUNIT(x) ((x)&UNIT_MASK)
#define INDEXUNIT(x) ((x) >> 3)
// players
#define PL1 0x80
#define PL2 0x40
#define NEU 0x00
#define GETPLAY(x) ((x)&OWNER_MASK)
#define INDEXPLAY(x) (((x) >> 6))
// map load directions
#define LOAD_ALL 0x01
#define LOAD_LEFT 0x04
#define LOAD_RIGHT 0x06
// cursor/movement directions
#define DIR_LEFT 0x04
#define DIR_RIGHT 0x06
#define DIR_UP 0x08
#define DIR_DOWN 0x0A
//overlay lines
#define OVR1 (VRAM_TILES_V-4)
#define OVR2 (VRAM_TILES_V-3)
#define OVR3 (VRAM_TILES_V-2)
#define OVR4 (VRAM_TILES_V-1)
//interface tiles (this might change if the tile map changes drastically, fuck gconvert
#define INTERFACE_TL 23
#define INTERFACE_TOP 24
#define INTERFACE_TR 25
#define INTERFACE_LEFT 26
#define INTERFACE_MID 27
#define INTERFACE_RIGHT 28
#define INTERFACE_BL 35
#define INTERFACE_BOT 36
#define INTERFACE_BR 37
#define INTERFACE_ARROW 42
#define INTERFACE_ARROW_TOP 53
#define INTERFACE_ARROW_BOT 52
#define INTERFACE_GLIGHT 55
#define INTERFACE_RLIGHT 54
#define INTERFACE_DOLLAR 56
//sprite indices
#define SPRITE_CURSOR 0
#define SPRITE_ARROW 4
#define SPRITE_EXPLOSION 5
#define SPRITE_POS_EXPL1 4
#define SPRITE_POS_EXPL2 5
//#define SPRITE_MOVINGUNIT 5
/* globals */
unsigned char levelWidth, levelHeight;
unsigned char cursorX, cursorY; // absolute coords
unsigned char cameraX;
unsigned char vramX; // where cameraX points to in vram coords, wrapped on 0x1F
unsigned char selectionVar = 0; // generic selection variable
struct EepromBlockStruct eepromData;
char blinkCounter = 0;
char blinkState = BLINK_UNITS;
char blinkMode = FALSE;
char cursorCounter = 0; //for cursor alternation
char cursorAlt = FALSE;
int curInput;
int prevInput;
unsigned char activePlayer;
unsigned char credits[] = {0, 0};
const char* currentLevel;
enum
{
scrolling, unit_menu, unit_movement, unit_moving, unit_attack, end_turn, pause, menu
} controlState;
// what is visible on the screen; 14 wide, 11 high, 2 loading columns on each side
struct GridBufferSquare levelBuffer[MAX_LEVEL_WIDTH][LEVEL_HEIGHT];
unsigned char unitFirstEmpty = 0;
unsigned char unitListEnd = 0;
signed char lastJumpedUnit = -1;
struct Unit unitList[MAX_UNITS]; //is this enough?
struct Movement movementBuffer[10]; // ought to be enough
uint8_t movementCount = 0;
uint8_t movementPoints = 0;
unsigned char movingUnit = 0;
unsigned char arrowX = 0, arrowY = 0;
unsigned char attackingUnit = 0;
unsigned char attackedUnit = 0;
/* declarations */
// param1, param2, param3; return
void initialize();
void loadLevel(const char*); // level
void drawLevel(char); // direction
void drawHPBar(unsigned char, unsigned char, char); // x, y, value
void drawDefenseBar(unsigned char, unsigned char, char); //same as hp bar
void drawOverlay();
void drawArrow();
unsigned char addUnit(unsigned char, unsigned char, char, char); // x, y, player, type; unitIndex
void removeUnitByIndex(unsigned char); // index
void removeUnit(unsigned char, unsigned char); // x, y
void moveUnit();
char moveCamera(char); // direction
char moveCameraInstant(char); // x
char moveCursor(char); // direction
char moveCursorInstant(unsigned char, unsigned char); // x, y
char validArrowTile(unsigned char, unsigned char); // x, y, hasArrow
const char* getTileMap(unsigned char, unsigned char); // x, y; tileMap
void waitGameInput();
void mapCursorSprite(char); // alternate
void mapMovingUnitSprite();
void tweenUnitSprite(char, char, char, char);
void redrawUnits();
void setBlinkMode(char); // on-off
const char* getUnitName(unsigned char); // unit; unitName
char getNeededMovePoints(const char unit, const char terrain);
char getAttackRange(const char unit);
char getDamage(struct Unit* srcUnit, struct Unit* dstUnit);
char getRandomNumber(); // ; rand
char getRandomNumberLimit(char); // max; rand
unsigned char getNextAttackableUnitIndex(signed char last, char dir);
void saveEeprom();
void WaitVsync_(char);
/*const char testlevel[] PROGMEM =
{
16,
PL, MO, FO, PL, MO, FO, PL, MO, FO, PL, MO, FO, PL, MO, FO, PL,
CT|NEU, BS|NEU, CT|PL1, BS|PL1, CT|PL2, BS|PL2, CT|NEU, BS|NEU, CT|PL1, BS|PL1, CT|PL2, BS|PL2, CT|NEU, BS|NEU, PL, FO,
PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, FO, MO,
PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, FO, MO,
PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, FO, MO,
PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, FO, MO,
PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, FO, MO,
PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, FO, MO,
PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, FO, MO,
PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, FO, MO,
PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL, FO, MO
};*/
//const char testlevel[] PROGMEM = {};
const char testlevel[] PROGMEM =
{
16, 11,
PL, FO, PL, PL, PL, PL, PL, PL, PL, MO, MO, PL, MO, PL, PL, MO,
PL, PL, MO, MO, MO, MO, BS, PL, PL, CT, MO, FO, PL, PL|UN1|PL2, BS|PL2, PL,
PL, BS|PL1, PL, MO, PL, PL, FO, FO, PL, FO, FO, PL, PL, MO, MO, PL,
PL|UN3|PL2, FO|UN1|PL1, PL|UN5|PL2, MO, PL, PL, PL, FO, PL, PL, FO, PL, FO, PL, PL, PL,
PL, PL|UN2|PL2, MO, MO, PL, MO, MO, FO, PL, PL, PL, FO, PL, FO, PL, PL,
PL, FO, PL|UN4|PL1, MO, PL, MO, PL, PL, PL, PL, PL, PL, PL, PL, PL, PL,
PL, PL, PL, PL, MO, PL, PL, PL, CT, MO, MO, PL, PL, FO, PL, PL,
PL, FO, PL, PL, PL|UN2|PL2, PL|UN4|PL1, PL, FO, FO, PL, MO, FO, FO, FO, PL|UN4|PL2, PL|UN2|PL2,
PL, PL, CT, PL, PL, FO, FO, PL, PL, PL, MO, FO, PL, PL, PL, PL,
FO, FO|UN2|PL1, FO, PL, PL, PL, FO, FO, PL, MO, PL, PL, FO, CT|UN1|PL2, PL, PL|UN3|PL1,
PL, MO, MO, FO|UN1|PL2, MO, PL, PL, PL, FO, BS, PL, MO, MO, MO, MO, PL
};
const char shortlevel[] PROGMEM =
{
5, 4,
PL, PL, PL, PL, CT,
BS|PL1, MO, MO, FO, MO|UN1|PL1,
FO, MO, MO, BS|PL2, CT|UN3|PL2,
PL, PL, PL, PL, CT
};
/* main function */
int main() {
initialize();
loadLevel(testlevel);
FadeOut(0, true);
drawLevel(LOAD_ALL);
drawOverlay();
mapCursorSprite(FALSE);
MoveSprite(0,0,0,2,2);
FadeIn(5, true);
activePlayer = PL1;
credits[0] = 10;
credits[1] = 10;
waitGameInput();
return 0;
}
void initialize() {
Screen.scrollHeight = 28;
Screen.overlayHeight = 4;
Screen.overlayTileTable = terrainTiles; // seems like it has to share the tiles, otherwise we can't use the fonts
ClearVram();
SetFontTilesIndex(TERRAINTILES_SIZE);
SetTileTable(terrainTiles);
SetSpritesTileTable(spriteTiles);
eepromData.id = EEPROM_INDEX;
if(!isEepromFormatted() || EepromReadBlock(EEPROM_INDEX, &eepromData)) {
// no idea what to do here...
}
}
void jumpToNextUnit() {
for(unsigned char i = lastJumpedUnit+1; i != lastJumpedUnit; i = (i+1)%MAX_UNITS) {
//TODO: make this only jump to not moved or attacked units
//should this be !(hasmoved || hasattacked)?
if(unitList[i].isUnit && GETPLAY(unitList[i].info) == activePlayer && !(HASMOVED(unitList[i].other) && HASATTACKED(unitList[i].other))) {
if((unitList[i].xPos < cameraX) || (unitList[i].xPos > (cameraX + MAX_VIS_WIDTH))) {
signed char tempX = unitList[i].xPos - MAX_VIS_WIDTH/2;
if(tempX < 0) {
tempX = 0;
}
else if(tempX > (levelWidth - MAX_VIS_WIDTH)) {
tempX = levelWidth - MAX_VIS_WIDTH;
}
moveCameraInstant(unitList[i].xPos - MAX_VIS_WIDTH/2);
}
moveCursorInstant(unitList[i].xPos, unitList[i].yPos);
lastJumpedUnit = i;
break;
}
}
}
void drawTwoSelMenu(const char* prompt, const char* sel1, const char* sel2) {
Fill((vramX+5)%0x1F, 5, 12, 5, INTERFACE_MID);
SetTile((vramX+5)%0x1F, 5, INTERFACE_TL);
Fill((vramX+5)%0x1F, 6, 1, 3, INTERFACE_LEFT);
SetTile((vramX+5)%0x1F, 9, INTERFACE_BL);
Fill((vramX+5+1)%0x1F, 9, 10, 1, INTERFACE_BOT);
SetTile((vramX+5+11)%0x1F, 9, INTERFACE_BR);
Fill((vramX+5+11)%0x1F, 6, 1, 3, INTERFACE_RIGHT);
SetTile((vramX+5+11)%0x1F, 5, INTERFACE_TR);
Fill((vramX+5+1)%0x1F, 5, 10, 1, INTERFACE_TOP);
Print((vramX+5+1)%0x1F, 6, prompt);
Print((vramX+5+4)%0x1F, 7, sel1);
Print((vramX+5+4)%0x1F, 8, sel2);
SetTile((vramX+5+3)%0x1F, 7+selectionVar, INTERFACE_ARROW);
}
void attackUnit() {
MoveSprite(0, OFF_SCREEN, 0, 2, 2);
// PrintHexByte(11, OVR2, sprites[SPRITE_POS_EXPL1].y);
// while(1);
sprites[SPRITE_POS_EXPL1].tileIndex = SPRITE_EXPLOSION; // this doesn't need to be set every call
sprites[SPRITE_POS_EXPL1].x = OFF_SCREEN;
sprites[SPRITE_POS_EXPL1].y = 0;
sprites[SPRITE_POS_EXPL2].tileIndex = SPRITE_EXPLOSION; // this doesn't need to be set every call
sprites[SPRITE_POS_EXPL2].flags = SPRITE_FLIP_X;
sprites[SPRITE_POS_EXPL2].x = OFF_SCREEN;
sprites[SPRITE_POS_EXPL2].y = 0;
char cycles = 0;
char ex1_start = (getRandomNumberLimit(4) + 1) * 3; // random half-cycle between 1 and 5
char ex2_start = ex1_start + (getRandomNumberLimit(7) + 3) * 3;
char max_cycles = ex2_start + 20 * 3;
int8_t damage = getDamage(&unitList[attackingUnit], &unitList[attackedUnit]);
while(cycles < max_cycles) {
if(cycles == ex1_start) {
sprites[SPRITE_POS_EXPL1].x = (cursorX-cameraX)*16 + getRandomNumberLimit(10);
sprites[SPRITE_POS_EXPL1].y = cursorY*16 + getRandomNumberLimit(2) + 1;
}
if(cycles == ex2_start) {
sprites[SPRITE_POS_EXPL2].x = (cursorX-cameraX)*16 + getRandomNumberLimit(10) + 1;
sprites[SPRITE_POS_EXPL2].y = cursorY*16 + getRandomNumberLimit(3) + 8;
}
if(cycles > ex1_start && cycles < ex1_start+60) {
sprites[SPRITE_POS_EXPL1].tileIndex = SPRITE_EXPLOSION+(cycles-ex1_start)/6;
}
if(cycles > ex2_start && cycles < ex2_start+60) {
sprites[SPRITE_POS_EXPL2].tileIndex = SPRITE_EXPLOSION+(cycles-ex2_start)/6;
}
if(damage != 0 && unitList[attackedUnit].hp != 0){
unitList[attackedUnit].hp -= 1;
damage--;
drawOverlay();
}
WaitVsync_(3);
cycles += 3;
}
// hide sprites
sprites[SPRITE_POS_EXPL1].x = OFF_SCREEN;
sprites[SPRITE_POS_EXPL1].y = 0;
sprites[SPRITE_POS_EXPL2].x = OFF_SCREEN;
sprites[SPRITE_POS_EXPL2].y = 0;
// animate hp bar
cycles = 0;
while(damage != 0 && unitList[attackedUnit].hp != 0) {
unitList[attackedUnit].hp -= 1;
damage--;
drawOverlay();
WaitVsync_(3);
//cycles += 3;
}
WaitVsync_(10);
if(unitList[attackedUnit].hp <= 0) {
// less than just to be sure
removeUnitByIndex(attackedUnit);
drawLevel(LOAD_ALL);
drawOverlay();
}
}
void endTurn() {
unsigned char i, x, y, terr;
setBlinkMode(FALSE);
drawLevel(LOAD_ALL);
activePlayer = (activePlayer == PL1) ? PL2 : PL1;
for(i = 0; i < MAX_UNITS; i++) {
if(unitList[i].isUnit && GETPLAY(unitList[i].info) == activePlayer) {
// reset markers on units
SETHASMOVED(i, FALSE);
SETHASATTACKED(i, FALSE);
x = unitList[i].xPos;
y = unitList[i].yPos;
terr = GETTERR(levelBuffer[x][y].info);
// heal units on bases&cities
if(GETPLAY(levelBuffer[x][y].info) == activePlayer && (terr == CT || terr == BS)) {
unitList[i].hp += 20;
if(unitList[i].hp > 100)
unitList[i].hp = 100;
}
// convert bases/cities
else if(terr == CT || terr == BS) {
levelBuffer[x][y].info = terr|activePlayer;
}
}
}
// money 'n shit
// 4 per owned, 4 by default
credits[(activePlayer == PL1) ? 0 : 1] += 4;
for(x=0; x < levelWidth; x++) {
for(y=0; y < levelHeight; y++) {
terr = GETTERR(levelBuffer[x][y].info);
if(GETPLAY(levelBuffer[x][y].info) == activePlayer && (terr == CT || terr == BS)) {
SETHASPROD(x, y, FALSE);
credits[activePlayer == PL1 ? 0 : 1] += 4;
}
}
}
if(credits[activePlayer == PL1 ? 0 : 1] > 200)
credits[(activePlayer == PL1) ? 0 : 1] = 200;
}
void waitGameInput() {
curInput = prevInput = ReadJoypad(JPPLAY(activePlayer));
//char asd = 0; //unused
//char tmpUnit = 0; //unused
while(1) {
curInput = ReadJoypad(JPPLAY(activePlayer));
drawOverlay();
switch(controlState) //scrolling, unit_menu, unit_movement, pause, menu
{
case scrolling:
if(curInput&BTN_A && !(prevInput&BTN_A)) {
if(levelBuffer[cursorX][cursorY].unit != 0xff && GETPLAY(unitList[levelBuffer[cursorX][cursorY].unit].info) == activePlayer) {
// enter select unit mode if there's a unit here and it belongs to us
//displayUnitMenu();
selectionVar = 0;
//setBlinkMode(FALSE);
controlState = unit_menu;
}
}
if(curInput&BTN_X && !(prevInput&BTN_X)) {
// toggle blink mode
setBlinkMode(!blinkMode);
}
if(curInput&BTN_LEFT) {
// move cur left
moveCursor(DIR_LEFT);
}
if(curInput&BTN_RIGHT) {
// move cur right
moveCursor(DIR_RIGHT);
}
if(curInput&BTN_UP) {
// move cur up
moveCursor(DIR_UP);
}
if(curInput&BTN_DOWN) {
// move cur down
moveCursor(DIR_DOWN);
}
if(curInput&BTN_Y && !(prevInput&BTN_Y)) {
jumpToNextUnit();
}
if(curInput&BTN_SELECT && !(prevInput&BTN_SELECT)) {
// open end turn menu
controlState = end_turn;
selectionVar = 0;
MoveSprite(0, 224, 0, 2, 2);
drawTwoSelMenu(PSTR("End turn?"), PSTR("Yes"), PSTR("No"));
//
//
// DrawMap2((vramX+5)%0x1F, 5, INTERFACE_TR)
}
break;
case unit_menu:
if(curInput&BTN_X && !(prevInput&BTN_X)) {
// toggle blink mode
setBlinkMode(!blinkMode);
}
if(curInput&BTN_A && !(prevInput&BTN_A)) {
// do selection
if(selectionVar == 1) { // move
if(!HASMOVED(unitList[levelBuffer[cursorX][cursorY].unit].other)) {
controlState = unit_movement;
movementPoints = 10;
moveCursorInstant(cursorX, cursorY); // just to normalize
movingUnit = levelBuffer[cursorX][cursorY].unit;
arrowX = unitList[movingUnit].xPos;
arrowY = unitList[movingUnit].yPos;
}
}
else if(selectionVar == 0){ // attack
if(!HASATTACKED(unitList[levelBuffer[cursorX][cursorY].unit].other)) {
attackingUnit = levelBuffer[cursorX][cursorY].unit;
attackedUnit = getNextAttackableUnitIndex(-1, 1);
if(attackedUnit != 0xFF) {
controlState = unit_attack;
cursorX = unitList[attackedUnit].xPos;
cursorY = unitList[attackedUnit].yPos;
moveCursorInstant(cursorX, cursorY);
}
}
}
else {
ERROR("inv. sel um.");
}
}
if(curInput&BTN_B && !(prevInput&BTN_B)) {
// leave unit action menu
controlState = scrolling;
}
if(curInput&BTN_UP && !(prevInput&BTN_UP)) {
// move selection up
selectionVar = 0; //only works because 2 choices, uncomment below if more
/*
if(selectionVar != 0) {
selectionVar--;
}
*/
}
if(curInput&BTN_DOWN && !(prevInput&BTN_DOWN)) {
// move selection down
selectionVar = 1; //only works because 2 choices, uncomment below if more
/*
if(selectionVar != 1) {
selectionVar++;
}
*/
}
break;
case unit_movement:
if(curInput&BTN_LEFT && !(prevInput&BTN_LEFT)) {
if(movementCount > 0 && movementBuffer[movementCount-1].direction == DIR_RIGHT) {
movementCount--;
arrowX--;
movementPoints += movementBuffer[movementCount].movePoints;
}
else if(movementCount < 10 && validArrowTile(arrowX-1, arrowY)) {
movementBuffer[movementCount].direction = DIR_LEFT;
movementBuffer[movementCount].movePoints = getNeededMovePoints(GETUNIT(unitList[movingUnit].info), GETTERR(levelBuffer[arrowX-1][arrowY].info));
movementPoints -= movementBuffer[movementCount].movePoints;
movementCount++;
arrowX--;
}
}
if(curInput&BTN_RIGHT && !(prevInput&BTN_RIGHT)) {
if(movementCount > 0 && movementBuffer[movementCount-1].direction == DIR_LEFT) {
movementCount--;
arrowX++;
movementPoints += movementBuffer[movementCount].movePoints;
}
else if(movementCount < 10 && validArrowTile(arrowX+1, arrowY)) {
movementBuffer[movementCount].direction = DIR_RIGHT;
movementBuffer[movementCount].movePoints = getNeededMovePoints(GETUNIT(unitList[movingUnit].info), GETTERR(levelBuffer[arrowX+1][arrowY].info));
movementPoints -= movementBuffer[movementCount].movePoints;
movementCount++;
arrowX++;
}
}
if(curInput&BTN_UP && !(prevInput&BTN_UP)) {
if(movementCount > 0 && movementBuffer[movementCount-1].direction == DIR_DOWN) {
movementCount--;
arrowY--;
movementPoints += movementBuffer[movementCount].movePoints;
}
else if(movementCount < 10 && validArrowTile(arrowX, arrowY-1)) {
movementBuffer[movementCount].direction = DIR_UP;
movementBuffer[movementCount].movePoints = getNeededMovePoints(GETUNIT(unitList[movingUnit].info), GETTERR(levelBuffer[arrowX][arrowY-1].info));
movementPoints -= movementBuffer[movementCount].movePoints;
movementCount++;
arrowY--;
}
}
if(curInput&BTN_DOWN && !(prevInput&BTN_DOWN)) {
if(movementCount > 0 && movementBuffer[movementCount-1].direction == DIR_UP) {
movementCount--;
arrowY++;
movementPoints += movementBuffer[movementCount].movePoints;
}
else if(movementCount < 10 && validArrowTile(arrowX, arrowY+1)) {
movementBuffer[movementCount].direction = DIR_DOWN;
movementBuffer[movementCount].movePoints = getNeededMovePoints(GETUNIT(unitList[movingUnit].info), GETTERR(levelBuffer[arrowX][arrowY+1].info));
movementPoints -= movementBuffer[movementCount].movePoints;
movementCount++;
arrowY++;
}
}
if(curInput&BTN_B && !(prevInput&BTN_B)) {
// leave movement mode
controlState = unit_menu;
movementCount = 0;
drawLevel(LOAD_ALL);
}
if(curInput&BTN_X && !(prevInput&BTN_X)) {
// toggle blink mode
setBlinkMode(!blinkMode);
}
if(curInput&BTN_A && !(prevInput&BTN_A)) {
// move unit!
if(movementCount > 0) {
controlState = unit_moving;
drawLevel(LOAD_ALL);
moveUnit();
moveCursorInstant(unitList[movingUnit].xPos, unitList[movingUnit].yPos);
controlState = scrolling;
movementCount = 0;
drawLevel(LOAD_ALL);
SETHASMOVED(movingUnit, TRUE);
}
else {
// some error bleep
}
}
break;
case unit_attack:
if(curInput&BTN_UP && !(prevInput&BTN_UP)) {
attackedUnit = getNextAttackableUnitIndex(attackedUnit, -1);
cursorX = unitList[attackedUnit].xPos;
cursorY = unitList[attackedUnit].yPos;
moveCursorInstant(cursorX, cursorY);
}
if(curInput&BTN_DOWN && !(prevInput&BTN_DOWN)) {
attackedUnit = getNextAttackableUnitIndex(attackedUnit, 1);
cursorX = unitList[attackedUnit].xPos;
cursorY = unitList[attackedUnit].yPos;
moveCursorInstant(cursorX, cursorY);
}
if(curInput&BTN_X && !(prevInput&BTN_X)) {
// toggle blink mode
setBlinkMode(!blinkMode);
}
if(curInput&BTN_B && !(prevInput&BTN_B)) {
// leave attack mode
controlState = unit_menu;
movementCount = 0;
cursorX = unitList[attackingUnit].xPos;
cursorY = unitList[attackingUnit].yPos;
moveCursorInstant(cursorX, cursorY);
drawLevel(LOAD_ALL);
}
if(curInput&BTN_A && !(prevInput&BTN_A)) {
attackUnit();
SETHASATTACKED(attackingUnit, TRUE);
moveCursorInstant(unitList[attackingUnit].xPos, unitList[attackingUnit].yPos);
controlState = scrolling;
}
break;
case end_turn:
if((curInput&BTN_SELECT && !(prevInput&BTN_SELECT)) || (curInput&BTN_B && !(prevInput&BTN_B))) {
// open end turn menu
controlState = scrolling;
moveCursorInstant(cursorX, cursorY);
drawLevel(LOAD_ALL);
}
if((curInput&BTN_UP && !(prevInput&BTN_UP)) || (curInput&BTN_DOWN && !(prevInput&BTN_DOWN))) {
selectionVar = !selectionVar;
drawTwoSelMenu(PSTR("End turn?"), PSTR("Yes"), PSTR("No"));
}
if(curInput&BTN_A && !(prevInput&BTN_A)) {
if(selectionVar == 0) { // end turn
endTurn();
jumpToNextUnit();
controlState = scrolling;
//?
}
else{
controlState = scrolling;
moveCursorInstant(cursorX, cursorY);
drawLevel(LOAD_ALL);
}
}
break;
case pause:
break;
case menu:
break;
case unit_moving:
break;
}
prevInput = curInput;
WaitVsync_(1);
}
}
void displayUnitMenu()
{
Print(8,4,PSTR("one"));
Print(8,5,PSTR("two"));
Print(9,4,PSTR("three"));
Print(9,5,PSTR("four"));
}
void drawScreenData() {
PrintByte(13, OVR3, cursorX, 0);
PrintByte(16, OVR3, cursorY, 0);
PrintByte(13, OVR4, cameraX, 0);
PrintByte(16, OVR4, vramX, 0);
}
void loadLevel(const char* level) {
char val, terr, owner, unit;
unsigned int x, y; // i know i said this wasn't needed but there will be overflow on the array access otherwise
levelWidth = pgm_read_byte(&level[0]);
levelHeight = pgm_read_byte(&level[1]);
if(levelHeight > 11) {
ERROR("inv. level height");
}
currentLevel = level;
cameraX = 0;
Screen.scrollX = 0;
vramX = 0;
// reset the unit list
for(x = 0;x < MAX_UNITS;x++)
unitList[x].isUnit = FALSE;
// loop y first because then we work in order. locality probably isn't an issue but eh.
for(y = 0; y < levelHeight; y++) {
for(x = 0; x < levelWidth; x++) {
val = pgm_read_byte(&level[y*levelWidth+x+2]);
terr = val & TERRAIN_MASK;
owner = val & OWNER_MASK;
unit = val & UNIT_MASK;
levelBuffer[x][y].info = terr | owner;
levelBuffer[x][y].unit = 0xFF;
if(unit != 0 && owner != NEU) {
//this can be a unit
addUnit(x, y, owner, unit);
}
}
}
}
void drawLevel(char dir) {
char x, y, bound;
switch(dir){
case LOAD_ALL:
if(cameraX == 0) {
vramX = 0;
Screen.scrollX = 0;
}
else if(cameraX == levelWidth-MAX_VIS_WIDTH) {
vramX = 4;
Screen.scrollX = 32;
}
else {
vramX = 2;
Screen.scrollX = 16;
}
if(cameraX+MAX_VIS_WIDTH == levelWidth)
bound = cameraX+MAX_VIS_WIDTH;
else
bound = cameraX+MAX_VIS_WIDTH+1;
for(y = 0; y < LEVEL_HEIGHT; y++) {
for(x = 0; x < MAX_VIS_WIDTH+1; x++) {
if(y < levelHeight && x < levelWidth && x+cameraX < bound)
DrawMap2(vramX+x*2, y*2, getTileMap(x+cameraX, y));
else
DrawMap2(vramX+x*2, y*2, map_placeholder);
}
}
break;
case LOAD_LEFT:
// assume that we want to load the column that's cameraX-1
// load it at vramX-2
if(cameraX - 1 < 0) {
ERROR("inv. left map load");
}
for(y = 0; y < levelHeight; y++) {
DrawMap2(vramX-2, y*2, getTileMap(cameraX-1, y));
}
break;
case LOAD_RIGHT:
// assume that we want to load the column that's cameraX+MAX+1
// load it at vramX+MAX*2+2
if(cameraX+MAX_VIS_WIDTH+1 > MAX_LEVEL_WIDTH) {
ERROR("inv. right map load");
}
for(y = 0; y < levelHeight; y++) {
DrawMap2(vramX+(MAX_VIS_WIDTH+1)*2, y*2, getTileMap(cameraX+MAX_VIS_WIDTH+1, y));
}
break;
default:
ERROR("inv. load var");
}
}
void redrawUnits() {
// redraws all unit tiles on the visible map
unsigned char i, v;
v = 0;
// not sure how the unit list thing works.
for(i = 0; i < MAX_UNITS; i++) {
if(unitList[i].isUnit) {
if(unitList[i].xPos >= cameraX-1 && unitList[i].xPos <= cameraX+MAX_VIS_WIDTH) {
// the unit is in our current camera buffer, redraw it
DrawMap2(((unitList[i].xPos-cameraX)*2 + vramX)&0x1F, unitList[i].yPos*2, getTileMap(unitList[i].xPos, unitList[i].yPos));
/*PrintByte(10, OVR1, cameraX, 0);
PrintByte(10, OVR2, vramX, 0);*/
}
v++;
}
}
//PrintByte(3, OVR4, v, 0);
}
void drawOverlay() {
// draw the basic panel
Fill(1, OVR1, 26, 3, INTERFACE_MID);
Fill(0, OVR1, 1, 3, INTERFACE_LEFT);
SetTile(0, OVR4, INTERFACE_BL);
Fill(1, OVR4, 26, 1, INTERFACE_BOT);
SetTile(27, OVR4, INTERFACE_BR);
Fill(27, OVR1, 1, 3, INTERFACE_RIGHT);
// is there a unit here? draw info
// TODO: might need conditions for other overlay types, this is preliminary
if(levelBuffer[cursorX][cursorY].unit != 0xFF) {
struct Unit* unit = &unitList[levelBuffer[cursorX][cursorY].unit];
Print(12, OVR1, getUnitName(unit->info));
drawHPBar(12, OVR2, unit->hp);
if(GETPLAY(unit->info) == activePlayer) {
Print(12, OVR3, PSTR("MOV"));
Print(18, OVR3, PSTR("ATK"));
if(HASMOVED(unit->other))
SetTile(15, OVR3, INTERFACE_RLIGHT);
else
SetTile(15, OVR3, INTERFACE_GLIGHT);
if(HASATTACKED(unit->other))
SetTile(21, OVR3, INTERFACE_RLIGHT);
else
SetTile(21, OVR3, INTERFACE_GLIGHT);
}
}
if(activePlayer == PL1) {
Print(26, OVR1, PSTR("P1"));
PrintByte(27, OVR2, credits[0], TRUE);
}
else {
Print(26, OVR1, PSTR("P2"));
PrintByte(27, OVR2, credits[1], TRUE);
}
SetTile(23, OVR2, INTERFACE_DOLLAR);
const char* map;
switch(levelBuffer[cursorX][cursorY].info & TERRAIN_MASK) {
case PL:
map = map_plain;
Print(3, OVR1, PSTR("Plains"));
break;
case MO:
map = map_mountain;
Print(3, OVR1, PSTR("Mountains"));
break;
case FO:
map = map_forest;
Print(3, OVR1, PSTR("Forest"));
break;
case CT:
Print(3, OVR1, PSTR("City"));
switch(levelBuffer[cursorX][cursorY].info & OWNER_MASK) {
case PL1:
map = map_city_red;
break;
case PL2:
map = map_city_blu;
break;
case NEU:
map = map_city_neu;
break;
default:
map = map_placeholder;
}
break;
case BS:
Print(3, OVR1, PSTR("Base"));
switch(levelBuffer[cursorX][cursorY].info & OWNER_MASK) {
case PL1:
map = map_base_red;
break;
case PL2:
map = map_base_blu;
break;
case NEU:
map = map_base_neu;
break;
default:
map = map_placeholder;
}
break;
default:
map = map_placeholder;
}
DrawMap2(1, OVR2, map);
drawDefenseBar(3, OVR2, 100);