Browse Source

add proper score keeping

Thomas B 1 month ago
parent
commit
be9cf7e2cc
5 changed files with 64 additions and 40 deletions
  1. 1
    1
      Makefile
  2. 3
    1
      docs/index.html
  3. 1
    1
      src/main.c
  4. 57
    37
      src/score.ba0.c
  5. 2
    0
      src/score.h

+ 1
- 1
Makefile View File

82
 
82
 
83
 $(GIT): $(DATA_DIR)/git.c
83
 $(GIT): $(DATA_DIR)/git.c
84
 	@echo Generating $@ from $<
84
 	@echo Generating $@ from $<
85
-	sed 's/GIT_VERSION/$(shell git describe --abbrev=7 --dirty --always --tags)/g' $< > $@
85
+	@sed 's|GIT_VERSION|$(shell git describe --abbrev=7 --dirty --always --tags)|g' $< > $@
86
 
86
 
87
 usage: $(BUILD_DIR)/$(BIN)
87
 usage: $(BUILD_DIR)/$(BIN)
88
 	@echo Analyzing $<
88
 	@echo Analyzing $<

+ 3
- 1
docs/index.html View File

60
                 <tr><td>Arrow Right</td><td>Rotate Right</td></tr>
60
                 <tr><td>Arrow Right</td><td>Rotate Right</td></tr>
61
                 <tr><td>A (Z)</td><td>Accelerate</td></tr>
61
                 <tr><td>A (Z)</td><td>Accelerate</td></tr>
62
                 <tr><td>B (X)</td><td>Shoot</td></tr>
62
                 <tr><td>B (X)</td><td>Shoot</td></tr>
63
+                <tr><td>Start (Enter)</td><td>Pause</td></tr>
64
+                <tr><td>Select (V)</td><td>About</td></tr>
63
             </table>
65
             </table>
64
-            <p>Press Left or Right on the title screen to show either the black or white highscores.</p>
66
+            <p>Press Left or Right on the title screen to show either the black or white highscores. Press Select to show the about screen and build info.</p>
65
             <p>Collect small white spheres to get +5 white score. Collect small black spheres to get +5 black score. The opposite color will reduce your score when collected. Large black holes will attract you and damage your ship when touched. Large white spheres will repel you and replenish your health when touched. Accelerating will reduce your fuel, which will recharge when not accelerating. You can shoot large spheres for +10 points.<p>
67
             <p>Collect small white spheres to get +5 white score. Collect small black spheres to get +5 black score. The opposite color will reduce your score when collected. Large black holes will attract you and damage your ship when touched. Large white spheres will repel you and replenish your health when touched. Accelerating will reduce your fuel, which will recharge when not accelerating. You can shoot large spheres for +10 points.<p>
66
             <p>For a more detailed description of the original game check out the <a href="https://gta.fandom.com/wiki/Duality">Duality article on GTA Wiki</a>.</p>
68
             <p>For a more detailed description of the original game check out the <a href="https://gta.fandom.com/wiki/Duality">Duality article on GTA Wiki</a>.</p>
67
             <hr>
69
             <hr>

+ 1
- 1
src/main.c View File

254
     while (1) {
254
     while (1) {
255
         int32_t score = game();
255
         int32_t score = game();
256
 
256
 
257
-        if (score_ranking(score)) {
257
+        if ((score != 0) && score_ranking(score)) {
258
             uint16_t name = ask_name(score);
258
             uint16_t name = ask_name(score);
259
             struct scores s = { .name = name, .score = score };
259
             struct scores s = { .name = name, .score = score };
260
             score_add(s);
260
             score_add(s);

+ 57
- 37
src/score.ba0.c View File

18
  */
18
  */
19
 
19
 
20
 #include <gbdk/platform.h>
20
 #include <gbdk/platform.h>
21
+#include <string.h>
21
 
22
 
22
 #include "score.h"
23
 #include "score.h"
24
+#include "gb/gb.h"
23
 
25
 
24
 static struct scores scores[SCORE_NUM * 2];
26
 static struct scores scores[SCORE_NUM * 2];
25
 static uint32_t scores_crc;
27
 static uint32_t scores_crc;
26
 
28
 
29
+BANKREF(score)
30
+
31
+#define NAME(a, b, c) (((uint16_t)(a - 'a') << 10) | ((uint16_t)(b - 'a') << 5) | (uint16_t)(c - 'a'))
32
+
33
+const struct scores initial_scores[SCORE_NUM * 2] = {
34
+    //{ .name = NAME('a', 'd', 'z'), .score = 10000 },
35
+    //{ .name = NAME('c', 'a', 'n'), .score = 7500 },
36
+    //{ .name = NAME('i', 'm', 'y'), .score = 5000 },
37
+    //{ .name = NAME('w', 'i', 'l'), .score = 2500 },
38
+    { .name = NAME('d', 'b', 'p'), .score = 1000 },
39
+    { .name = NAME('d', 'a', 'v'), .score = 750 },
40
+    { .name = NAME('d', 'o', 'd'), .score = 500 },
41
+    //{ .name = NAME('n', 'f', '.'), .score = 250 },
42
+    { .name = NAME('k', 'm', 'b'), .score = 175 },
43
+    { .name = NAME('s', 'j', 'l'), .score = 100 },
44
+
45
+    { .name = NAME('j', 'u', 'd'), .score = -100 },
46
+    //{ .name = NAME('1', '0', '1'), .score = -175 },
47
+    { .name = NAME('g', 'a', 'z'), .score = -250 },
48
+    { .name = NAME('n', 'o', 'n'), .score = -500 },
49
+    { .name = NAME('l', 'r', 'g'), .score = -750 },
50
+    { .name = NAME('d', 'a', 'n'), .score = -1000 },
51
+    //{ .name = NAME('w', 'd', 'y'), .score = -2500 },
52
+    //{ .name = NAME('s', 'i', 's'), .score = -5000 },
53
+    //{ .name = NAME('k', 'r', 'y'), .score = -7500 },
54
+    //{ .name = NAME('d', 'j', '.'), .score = -10000 },
55
+};
56
+
27
 uint16_t convert_name(char a, char b, char c) NONBANKED {
57
 uint16_t convert_name(char a, char b, char c) NONBANKED {
28
     // convert to lowercase
58
     // convert to lowercase
29
     if ((a >= 'A') && (a <= 'Z')) a = a - 'A' + 'a';
59
     if ((a >= 'A') && (a <= 'Z')) a = a - 'A' + 'a';
64
 }
94
 }
65
 
95
 
66
 static void score_init(void) NONBANKED {
96
 static void score_init(void) NONBANKED {
67
-    // TODO
68
-    scores[0].name = convert_name('a', 'b', 'c');
69
-    scores[0].score = 10000;
70
-
71
-    scores[1].name = convert_name('d', 'e', 'f');
72
-    scores[1].score = 8765;
73
-
74
-    scores[2].name = convert_name('g', 'h', 'i');
75
-    scores[2].score = 6999;
76
-
77
-    scores[3].name = convert_name('j', 'k', 'l');
78
-    scores[3].score = 4321;
79
-
80
-    scores[4].name = convert_name('m', 'n', 'o');
81
-    scores[4].score = 2000;
82
-
83
-    scores[5].name = convert_name('p', 'q', 'r');
84
-    scores[5].score = -2000;
85
-
86
-    scores[6].name = convert_name('s', 't', 'u');
87
-    scores[6].score = -4321;
88
-
89
-    scores[7].name = convert_name('v', 'w', 'x');
90
-    scores[7].score = -6999;
91
-
92
-    scores[8].name = convert_name('y', 'z', 'c');
93
-    scores[8].score = -8765;
94
-
95
-    scores[9].name = convert_name('a', 'b', 'c');
96
-    scores[9].score = -10000;
97
-
97
+    SWITCH_ROM(BANK(score));
98
+    memcpy(scores, initial_scores, sizeof(scores));
98
     scores_crc = calc_crc();
99
     scores_crc = calc_crc();
99
 }
100
 }
100
 
101
 
101
 static uint8_t score_pos(int32_t score) NONBANKED {
102
 static uint8_t score_pos(int32_t score) NONBANKED {
102
-    // TODO find place for new score
103
-    return (score < 0) ? 9 : 0;
103
+    if (score > 0) {
104
+        for (uint8_t i = 0; i < SCORE_NUM; i++) {
105
+            if (score > scores[i].score) {
106
+                return i;
107
+            }
108
+        }
109
+    } else if (score < 0) {
110
+        for (uint8_t i = (SCORE_NUM * 2) - 1; i >= 5; i--) {
111
+            if (score < scores[i].score) {
112
+                return i;
113
+            }
114
+        }
115
+    }
116
+
117
+    return 0xFF;
104
 }
118
 }
105
 
119
 
106
 uint8_t score_ranking(int32_t score) NONBANKED {
120
 uint8_t score_ranking(int32_t score) NONBANKED {
128
     }
142
     }
129
 
143
 
130
     uint8_t new = score_pos(score.score);
144
     uint8_t new = score_pos(score.score);
145
+    if (new < (SCORE_NUM * 2)) {
146
+        // move old scores out of the way
147
+        if ((score.score > 0) && (new < (SCORE_NUM - 1))) {
148
+            memmove(scores + new + 1, scores + new, sizeof(struct scores) * (SCORE_NUM - 1 - new));
149
+        } else if ((score.score < 0) && (new > SCORE_NUM)) {
150
+            memmove(scores + new - 1, scores + new, sizeof(struct scores) * (new - SCORE_NUM));
151
+        }
131
 
152
 
132
-    // TODO move old scores out of the way
133
-
134
-    scores[new] = score;
135
-    scores_crc = calc_crc();
153
+        scores[new] = score;
154
+        scores_crc = calc_crc();
155
+    }
136
 
156
 
137
     DISABLE_RAM;
157
     DISABLE_RAM;
138
 }
158
 }

+ 2
- 0
src/score.h View File

35
 struct scores score_highest(uint8_t off);
35
 struct scores score_highest(uint8_t off);
36
 struct scores score_lowest(uint8_t off);
36
 struct scores score_lowest(uint8_t off);
37
 
37
 
38
+BANKREF_EXTERN(score)
39
+
38
 #endif // __SCORE_H__
40
 #endif // __SCORE_H__

Loading…
Cancel
Save