GameBoy (Color) port of the GTA San Andreas arcade game Duality
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

game.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * game.c
  3. * Duality
  4. *
  5. * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * See <http://www.gnu.org/licenses/>.
  18. */
  19. #include <gbdk/metasprites.h>
  20. #include <string.h>
  21. #include <rand.h>
  22. #include <stdint.h>
  23. #include "banks.h"
  24. #include "config.h"
  25. #include "maps.h"
  26. #include "obj.h"
  27. #include "sprites.h"
  28. #include "sound.h"
  29. #include "input.h"
  30. #include "main.h"
  31. #include "sample.h"
  32. #include "window.h"
  33. #include "multiplayer.h"
  34. #include "table_speed_shot.h"
  35. #include "table_speed_move.h"
  36. #include "timer.h"
  37. #include "game.h"
  38. #define BAR_OFFSET_X (4 - 80)
  39. #define HEALTH_OFFSET_Y -16
  40. #define POWER_OFFSET_Y 16
  41. #define PAUSE_BLINK_FRAMES 32
  42. #define SPEED_INC 1
  43. #define SPEED_DEC 1
  44. #define SPEED_MAX_IDLE 16
  45. #define SPEED_MAX_DBG 256
  46. #define POWER_MAX 0x1FF
  47. #define POWER_SHIFT 1
  48. #define POWER_INC 2
  49. #define POWER_DEC 4
  50. BANKREF(game)
  51. const int8_t table_shot_offsets[ROT_INVALID * 2] = {
  52. 0, -SHIP_OFF, // 0.0
  53. SHIP_OFF / 2 - 1, -SHIP_OFF / 2 - 4, // 22.5
  54. SHIP_OFF / 2 + 3, -SHIP_OFF / 2 - 2, // 45.0
  55. SHIP_OFF / 2 + 5, -SHIP_OFF / 2 + 2, // 67.5
  56. SHIP_OFF, 0, // 90.0
  57. SHIP_OFF / 2 + 5, SHIP_OFF / 2 + 0, // 112.5
  58. SHIP_OFF / 2 + 3, SHIP_OFF / 2 + 2, // 135.0
  59. SHIP_OFF / 2 + 1, SHIP_OFF / 2 + 4, // 157.5
  60. 0, SHIP_OFF, // 180.0
  61. -SHIP_OFF / 2 + 2, SHIP_OFF / 2 + 3, // 202.5
  62. -SHIP_OFF / 2 - 3, SHIP_OFF / 2 + 2, // 225.0
  63. -SHIP_OFF / 2 - 5, SHIP_OFF / 2 - 1, // 247.5
  64. -SHIP_OFF, 0, // 270.0
  65. -SHIP_OFF / 2 - 2, -SHIP_OFF / 2 + 2, // 292.5
  66. -SHIP_OFF / 2 - 3, -SHIP_OFF / 2 - 2, // 315.0
  67. -SHIP_OFF / 2 + 1, -SHIP_OFF / 2 - 4, // 337.5
  68. };
  69. enum ACCELERATION {
  70. ACC_X = (1U << 1),
  71. ACC_Y = (1U << 2),
  72. ACC_R = (1U << 3),
  73. };
  74. static uint8_t fps_count = 0;
  75. static uint16_t prev_fps_start = 0;
  76. struct game_state game_state;
  77. uint16_t frame_count = 0;
  78. uint8_t game_fps = 0;
  79. static void calc_fps(void) {
  80. frame_count++;
  81. fps_count++;
  82. uint16_t diff = timer_get() - prev_fps_start;
  83. if (diff >= TIMER_HZ) {
  84. prev_fps_start = timer_get();
  85. game_fps = fps_count;
  86. fps_count = 0;
  87. }
  88. }
  89. static uint8_t pause_screen(void) {
  90. snd_music_off();
  91. snd_note_off();
  92. uint8_t n = 0;
  93. while (1) {
  94. key_read();
  95. if (key_pressed(J_START)) {
  96. break;
  97. } else if (key_pressed(J_SELECT)) {
  98. return 1;
  99. }
  100. n = (n + 1) & (PAUSE_BLINK_FRAMES - 1);
  101. uint8_t hiwater = SPR_NUM_START;
  102. spr_draw(SPR_PAUSE, FLIP_NONE, 0, 0, (n < (PAUSE_BLINK_FRAMES / 2)) ? 0 : 1, &hiwater);
  103. hide_sprites_range(hiwater, MAX_HARDWARE_SPRITES);
  104. if ((_cpu == CGB_TYPE) && (conf_get()->debug_flags & DBG_OUT_ON)) {
  105. uint8_t x_off = win_game_draw(game_state.score, 0);
  106. move_win(MINWNDPOSX + DEVICE_SCREEN_PX_WIDTH - x_off,
  107. MINWNDPOSY + DEVICE_SCREEN_PX_HEIGHT - 16);
  108. }
  109. calc_fps();
  110. vsync();
  111. }
  112. return 0;
  113. }
  114. static void status(uint8_t health, uint8_t power, uint8_t *hiwater) {
  115. if (health > 0) {
  116. switch (health >> 6) {
  117. case 3:
  118. spr_draw(SPR_HEALTH, FLIP_X, BAR_OFFSET_X, HEALTH_OFFSET_Y - 24,
  119. ((health >> 6) == 3) ? 7 - ((health >> 3) & 7) : 0, hiwater);
  120. case 2:
  121. spr_draw(SPR_HEALTH, FLIP_X, BAR_OFFSET_X, HEALTH_OFFSET_Y - 16,
  122. ((health >> 6) == 2) ? 7 - ((health >> 3) & 7) : 0, hiwater);
  123. case 1:
  124. spr_draw(SPR_HEALTH, FLIP_X, BAR_OFFSET_X, HEALTH_OFFSET_Y - 8,
  125. ((health >> 6) == 1) ? 7 - ((health >> 3) & 7) : 0, hiwater);
  126. case 0:
  127. spr_draw(SPR_HEALTH, FLIP_X, BAR_OFFSET_X, HEALTH_OFFSET_Y - 0,
  128. ((health >> 6) == 0) ? 7 - ((health >> 3) & 7) : 0, hiwater);
  129. }
  130. }
  131. if (power > 0) {
  132. switch (power >> 6) {
  133. case 3:
  134. spr_draw(SPR_POWER, FLIP_X, BAR_OFFSET_X, POWER_OFFSET_Y + 0,
  135. ((power >> 6) == 3) ? 7 - ((power >> 3) & 7) : 0, hiwater);
  136. case 2:
  137. spr_draw(SPR_POWER, FLIP_X, BAR_OFFSET_X, POWER_OFFSET_Y + 8,
  138. ((power >> 6) == 2) ? 7 - ((power >> 3) & 7) : 0, hiwater);
  139. case 1:
  140. spr_draw(SPR_POWER, FLIP_X, BAR_OFFSET_X, POWER_OFFSET_Y + 16,
  141. ((power >> 6) == 1) ? 7 - ((power >> 3) & 7) : 0, hiwater);
  142. case 0:
  143. spr_draw(SPR_POWER, FLIP_X, BAR_OFFSET_X, POWER_OFFSET_Y + 24,
  144. ((power >> 6) == 0) ? 7 - ((power >> 3) & 7) : 0, hiwater);
  145. }
  146. }
  147. }
  148. static void show_explosion(uint16_t power) {
  149. snd_music_off();
  150. snd_note_off();
  151. sample_play(SFX_EXPL_SHIP);
  152. for (uint8_t n = 0; n < (4 * 4 * 4); n++) {
  153. uint8_t hiwater = SPR_NUM_START;
  154. status(0, power, &hiwater);
  155. if (n < (4 * 4)) {
  156. spr_draw(SPR_EXPL, FLIP_NONE, 0, 0, n >> 2, &hiwater);
  157. }
  158. hide_sprites_range(hiwater, MAX_HARDWARE_SPRITES);
  159. vsync();
  160. }
  161. }
  162. void game_get_mp_state(void) BANKED {
  163. static struct mp_player_state mps;
  164. // TODO pass own pos to mp
  165. // TODO scale?
  166. mps.spd_x = game_state.spd_x;
  167. mps.spd_y = game_state.spd_y;
  168. mps.rot = game_state.rot;
  169. mp_new_state(&mps);
  170. }
  171. void game_set_mp_player2(struct mp_player_state *state) BANKED {
  172. // TODO update p2 pos
  173. }
  174. void game_set_mp_shot(struct mp_shot_state *state) BANKED {
  175. // TODO add shot
  176. }
  177. static void get_max_spd(int16_t *max_spd_x, int16_t *max_spd_y) NONBANKED {
  178. START_ROM_BANK(BANK(table_speed_move)) {
  179. *max_spd_x = table_speed_move[(game_state.rot * table_speed_move_WIDTH) + 0];
  180. *max_spd_y = -table_speed_move[(game_state.rot * table_speed_move_WIDTH) + 1];
  181. } END_ROM_BANK;
  182. }
  183. static void get_shot_spd(int16_t *shot_spd_x, int16_t *shot_spd_y) NONBANKED {
  184. START_ROM_BANK(BANK(table_speed_shot)) {
  185. *shot_spd_x = table_speed_shot[(game_state.rot * table_speed_move_WIDTH) + 0];
  186. *shot_spd_y = -table_speed_shot[(game_state.rot * table_speed_move_WIDTH) + 1];
  187. } END_ROM_BANK;
  188. }
  189. void game_init(void) BANKED {
  190. game_state.spd_x = 0;
  191. game_state.spd_y = 0;
  192. game_state.rot = 0;
  193. game_state.health = HEALTH_MAX;
  194. game_state.power = POWER_MAX;
  195. game_state.score = 0;
  196. memset(&obj_state, 0, sizeof(struct obj_state));
  197. }
  198. uint8_t game(enum GAME_MODE mode) BANKED {
  199. snd_music_off();
  200. snd_note_off();
  201. disable_interrupts();
  202. DISPLAY_OFF;
  203. map_load(0);
  204. map_fill(MAP_GAME_1 + conf_get()->game_bg, 1);
  205. SHOW_BKG;
  206. spr_init_pal();
  207. SHOW_SPRITES;
  208. SPRITES_8x8;
  209. frame_count = 0;
  210. fps_count = 0;
  211. prev_fps_start = 0;
  212. if (mode == GM_SINGLE) {
  213. if (!(conf_get()->debug_flags & DBG_NO_OBJ)) {
  214. obj_spawn();
  215. }
  216. }
  217. uint8_t x_off = win_game_draw(game_state.score, 1);
  218. move_win(MINWNDPOSX + DEVICE_SCREEN_PX_WIDTH - x_off,
  219. MINWNDPOSY + DEVICE_SCREEN_PX_HEIGHT - 16);
  220. SHOW_WIN;
  221. DISPLAY_ON;
  222. enable_interrupts();
  223. snd_music(SND_GAME);
  224. uint8_t return_value = 0xFF;
  225. while(1) {
  226. key_read();
  227. if (mode != GM_SINGLE) {
  228. mp_handle();
  229. }
  230. enum ACCELERATION acc = 0;
  231. int32_t prev_score = game_state.score;
  232. if (key_pressed(J_LEFT)) {
  233. game_state.rot = (game_state.rot - 1) & (ROT_INVALID - 1);
  234. acc |= ACC_R;
  235. } else if (key_pressed(J_RIGHT)) {
  236. game_state.rot = (game_state.rot + 1) & (ROT_INVALID - 1);
  237. acc |= ACC_R;
  238. }
  239. if (key_down(J_A) && (game_state.power > 0)) {
  240. int16_t max_spd_x;
  241. int16_t max_spd_y;
  242. get_max_spd(&max_spd_x, &max_spd_y);
  243. if (conf_get()->debug_flags & DBG_FAST) {
  244. if (max_spd_x > 0) {
  245. max_spd_x = SPEED_MAX_DBG;
  246. } else if (max_spd_x < 0) {
  247. max_spd_x = -SPEED_MAX_DBG;
  248. }
  249. if (max_spd_y > 0) {
  250. max_spd_y = SPEED_MAX_DBG;
  251. } else if (max_spd_y < 0) {
  252. max_spd_y = -SPEED_MAX_DBG;
  253. }
  254. }
  255. if (max_spd_x != 0) {
  256. if (max_spd_x > 0) {
  257. game_state.spd_x += SPEED_INC;
  258. if (game_state.spd_x > max_spd_x) {
  259. game_state.spd_x = max_spd_x;
  260. }
  261. } else {
  262. game_state.spd_x -= SPEED_INC;
  263. if (game_state.spd_x < max_spd_x) {
  264. game_state.spd_x = max_spd_x;
  265. }
  266. }
  267. acc |= ACC_X;
  268. }
  269. if (max_spd_y != 0) {
  270. if (max_spd_y > 0) {
  271. game_state.spd_y += SPEED_INC;
  272. if (game_state.spd_y > max_spd_y) {
  273. game_state.spd_y = max_spd_y;
  274. }
  275. } else {
  276. game_state.spd_y -= SPEED_INC;
  277. if (game_state.spd_y < max_spd_y) {
  278. game_state.spd_y = max_spd_y;
  279. }
  280. }
  281. acc |= ACC_Y;
  282. }
  283. if (!(conf_get()->debug_flags & DBG_NO_FUEL)) {
  284. if (game_state.power >= POWER_DEC) {
  285. game_state.power -= POWER_DEC;
  286. } else {
  287. game_state.power = 0;
  288. }
  289. }
  290. } else if (!key_down(J_A) && (game_state.power < POWER_MAX)) {
  291. if (game_state.power <= (POWER_MAX - POWER_INC)) {
  292. game_state.power += POWER_INC;
  293. } else {
  294. game_state.power = POWER_MAX;
  295. }
  296. }
  297. // adjust speed down when not moving
  298. if (!(acc & ACC_X)) {
  299. if (game_state.spd_x != 0) {
  300. if (!(conf_get()->debug_flags & DBG_FAST)) {
  301. if (game_state.spd_x > SPEED_MAX_IDLE) game_state.spd_x -= SPEED_DEC;
  302. else if (game_state.spd_x < -SPEED_MAX_IDLE) game_state.spd_x += SPEED_DEC;
  303. } else {
  304. game_state.spd_x = 0;
  305. }
  306. }
  307. }
  308. if (!(acc & ACC_Y)) {
  309. if (game_state.spd_y != 0) {
  310. if (!(conf_get()->debug_flags & DBG_FAST)) {
  311. if (game_state.spd_y > SPEED_MAX_IDLE) game_state.spd_y -= SPEED_DEC;
  312. else if (game_state.spd_y < -SPEED_MAX_IDLE) game_state.spd_y += SPEED_DEC;
  313. } else {
  314. game_state.spd_y = 0;
  315. }
  316. }
  317. }
  318. if (key_pressed(J_B)) {
  319. int16_t shot_spd_x;
  320. int16_t shot_spd_y;
  321. get_shot_spd(&shot_spd_x, &shot_spd_y);
  322. shot_spd_x += game_state.spd_x;
  323. shot_spd_y += game_state.spd_y;
  324. int16_t shot_pos_x = table_shot_offsets[(game_state.rot * 2) + 0];
  325. int16_t shot_pos_y = table_shot_offsets[(game_state.rot * 2) + 1];
  326. int8_t ret = obj_add(SPR_SHOT,
  327. shot_pos_x, shot_pos_y,
  328. shot_spd_x, shot_spd_y);
  329. if (ret == OBJ_ADDED) {
  330. sample_play(SFX_SHOT);
  331. if (mode == GM_SINGLE) {
  332. if (game_state.score > 0) {
  333. game_state.score--;
  334. }
  335. } else {
  336. static struct mp_shot_state state;
  337. // TODO send absolute coordinate
  338. state.pos_x = shot_pos_x;
  339. state.pos_y = shot_pos_y;
  340. // TODO scale?
  341. state.spd_x = shot_spd_x;
  342. state.spd_y = shot_spd_y;
  343. mp_add_shot(&state);
  344. }
  345. }
  346. }
  347. if (key_pressed(J_START)) {
  348. if (pause_screen()) {
  349. return_value = 1;
  350. break;
  351. }
  352. // restart bg music
  353. snd_music(SND_GAME);
  354. }
  355. if (key_pressed(J_SELECT) && conf_get()->debug_flags) {
  356. map_dbg_reset();
  357. }
  358. map_move(game_state.spd_x, game_state.spd_y);
  359. uint8_t hiwater = SPR_NUM_START;
  360. status(game_state.health >> HEALTH_SHIFT, game_state.power >> POWER_SHIFT, &hiwater);
  361. if (conf_get()->debug_flags & DBG_MARKER) {
  362. spr_draw(SPR_DEBUG, FLIP_NONE, 0, 0, 0, &hiwater);
  363. spr_draw(SPR_DEBUG_LARGE, FLIP_NONE, 0, 0, 0, &hiwater);
  364. }
  365. spr_ship(game_state.rot, acc & (ACC_X | ACC_Y), &hiwater);
  366. int16_t damage = obj_do(&game_state.spd_x, &game_state.spd_y, &game_state.score, &hiwater,
  367. (conf_get()->debug_flags & DBG_NO_OBJ) ? 1 : 0);
  368. if (damage > 0) {
  369. if (conf_get()->debug_flags & DBG_GOD_MODE) {
  370. damage = 0;
  371. }
  372. if (game_state.health > damage) {
  373. game_state.health -= damage;
  374. if ((!sample_running()) && (sample_last() != SFX_DAMAGE)) {
  375. sample_play(SFX_DAMAGE);
  376. }
  377. } else if (game_state.health <= damage) {
  378. game_state.health = 0;
  379. show_explosion(game_state.power);
  380. return_value = 0;
  381. break;
  382. }
  383. } else if ((damage < 0) && (game_state.health < HEALTH_MAX)) {
  384. if ((!sample_running()) && (sample_last() != SFX_HEAL)) {
  385. sample_play(SFX_HEAL);
  386. }
  387. game_state.health += -damage;
  388. if (game_state.health > HEALTH_MAX) {
  389. game_state.health = HEALTH_MAX;
  390. }
  391. } else if (damage == 0) {
  392. sample_last_reset();
  393. }
  394. hide_sprites_range(hiwater, MAX_HARDWARE_SPRITES);
  395. if ((game_state.score != prev_score)
  396. || ((_cpu == CGB_TYPE) && (conf_get()->debug_flags & DBG_OUT_ON))) {
  397. uint8_t x_off = win_game_draw(game_state.score, 0);
  398. move_win(MINWNDPOSX + DEVICE_SCREEN_PX_WIDTH - x_off,
  399. MINWNDPOSY + DEVICE_SCREEN_PX_HEIGHT - 16);
  400. }
  401. calc_fps();
  402. vsync();
  403. }
  404. return return_value;
  405. }