roots_of_life_game.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <stdlib.h>
  5. #include <gui/view.h>
  6. #include <notification/notification.h>
  7. #include <notification/notification_messages.h>
  8. #include "roots_of_life_game_icons.h"
  9. #define TAG "RootsOfLife"
  10. // Flipper
  11. #define FLIPPER_LCD_WIDTH 128
  12. #define FLIPPER_LCD_HEIGHT 64
  13. // General
  14. #define GROUND_HEIGHT 10
  15. #define CELL_SIZE 3
  16. #define FIELD_START_X 0
  17. #define FIELD_START_Y (GROUND_HEIGHT + 1)
  18. #define CELLS_X (FLIPPER_LCD_WIDTH / CELL_SIZE)
  19. #define CELLS_Y ((FLIPPER_LCD_HEIGHT - GROUND_HEIGHT) / CELL_SIZE)
  20. #define CELLS_TOTAL (CELLS_Y * CELLS_X)
  21. #define CELL(Y, X) (Y * CELLS_X + X)
  22. // Root Spawn
  23. #define ROOT_SIZE_X 7
  24. #define ROOT_SIZE_Y 7
  25. #define ROOT(Y, X) ((Y)*ROOT_SIZE_X + (X))
  26. #define SPAWN_DIRECTIONS 2
  27. #define GROW_STEPS 4
  28. #define GROW_SAME_DIRECTION_CHANCE 70
  29. #define RANDOM_GROW_ATTEMPTS 4
  30. #define RANDOM_GROW_CHANCE 50
  31. // UI
  32. #define BLINK_PERIOD 12
  33. #define BLINK_HIDE_FRAMES 5
  34. #define TREE_HEIGHT 10
  35. #define PICKUP_FREQUENCY 10
  36. // Game
  37. #define REROLLS_MAX 5
  38. #define SCORE_FACTOR 10
  39. #define PICKUPS_MIN 1
  40. #define PICKUPS_MAX 5
  41. #define PICKUPS_POINTS_FACTOR 10
  42. typedef enum { EventTypeTick, EventTypeKey } EventType;
  43. typedef enum {
  44. R_NONE = 0,
  45. R_UP = 0b1000,
  46. R_DOWN = 0b0100,
  47. R_LEFT = 0b0010,
  48. R_RIGHT = 0b0001
  49. } Direction;
  50. typedef enum { StageStart, StageRun, StageOver } GameStage;
  51. typedef struct {
  52. bool initialDraw;
  53. GameStage stage;
  54. int tick;
  55. bool* filledCells;
  56. char* cells;
  57. bool* pickups;
  58. int collectedPickups;
  59. bool* filledRootBase;
  60. char* rootBase;
  61. int rootSizeX;
  62. int rootSizeY;
  63. bool* filledRoot;
  64. char* root;
  65. int pX, pY;
  66. int rerolls;
  67. int score;
  68. } GameState;
  69. typedef struct {
  70. EventType type;
  71. InputEvent input;
  72. } GameEvent;
  73. static Direction rand_dir() {
  74. int r = rand() % 4;
  75. return 1 << r;
  76. }
  77. static Direction reverse_dir(Direction dir) {
  78. switch(dir) {
  79. case R_UP:
  80. return R_DOWN;
  81. case R_DOWN:
  82. return R_UP;
  83. case R_LEFT:
  84. return R_RIGHT;
  85. case R_RIGHT:
  86. return R_LEFT;
  87. default:
  88. return R_NONE;
  89. }
  90. }
  91. static int rand_range(int min, int max) {
  92. return min + rand() % (max - min);
  93. }
  94. static bool rand_chance(int chance) {
  95. return (rand() % 100) < chance;
  96. }
  97. static bool has_intersection(char cellA, char cellB) {
  98. return cellA & cellB;
  99. }
  100. static int root_index(GameState* state, int y, int x) {
  101. return y * state->rootSizeX + x;
  102. }
  103. static void set_cell(GameState* state, int y, int x, char cellRoot) {
  104. int c = CELL(y, x);
  105. state->filledCells[c] = true;
  106. state->cells[c] = cellRoot;
  107. }
  108. static void game_state_init(GameState* state) {
  109. state->initialDraw = false;
  110. state->tick = 0;
  111. // Init field arrays
  112. state->filledCells = (bool*)malloc(CELLS_TOTAL * sizeof(bool));
  113. state->cells = (char*)malloc(CELLS_TOTAL * sizeof(char));
  114. state->pickups = (bool*)malloc(CELLS_TOTAL * sizeof(char));
  115. state->rootBase = (char*)malloc(ROOT_SIZE_X * ROOT_SIZE_Y * sizeof(char));
  116. state->filledRootBase = (bool*)malloc(ROOT_SIZE_X * ROOT_SIZE_Y * sizeof(bool));
  117. state->root = NULL;
  118. state->filledRoot = NULL;
  119. for(int i = 0; i < CELLS_TOTAL; i++) {
  120. state->filledCells[i] = false;
  121. state->cells[i] = R_NONE;
  122. state->pickups[i] = false;
  123. }
  124. state->collectedPickups = 0;
  125. for(int i = 0, n = rand_range(PICKUPS_MIN, PICKUPS_MAX); i < n; i++) {
  126. int x = rand_range(0, CELLS_X);
  127. int y = rand_range(0, CELLS_Y);
  128. state->pickups[CELL(y, x)] = true;
  129. }
  130. }
  131. static void free_root(GameState* state) {
  132. if(state->root) free(state->root);
  133. if(state->filledRoot) free(state->filledRoot);
  134. }
  135. static void game_state_free(GameState* state) {
  136. free(state->filledCells);
  137. free(state->cells);
  138. free(state->pickups);
  139. free(state->rootBase);
  140. free(state->filledRootBase);
  141. free_root(state);
  142. }
  143. /*static bool has_root(GameState* state, int x, int y) {
  144. return x >= 0 && x < ROOT_SIZE_X && y >= 0 && y < ROOT_SIZE_Y &&
  145. state->filledRootBase[ROOT(y, x)];
  146. }*/
  147. static void generate_new_root(GameState* state) {
  148. for(int i = 0; i < ROOT_SIZE_X * ROOT_SIZE_Y; i++) {
  149. state->filledRootBase[i] = false;
  150. state->rootBase[i] = R_NONE;
  151. }
  152. int cX = ROOT_SIZE_X / 2;
  153. int cY = ROOT_SIZE_Y / 2;
  154. int c = ROOT(cY, cX);
  155. state->filledRootBase[c] = true;
  156. for(int i = 0; i < SPAWN_DIRECTIONS; i++) {
  157. int pX = cX, pY = cY;
  158. Direction oldDir = rand_dir();
  159. for(int g = 0; g < GROW_STEPS; g++) {
  160. Direction dir = rand_chance(GROW_SAME_DIRECTION_CHANCE) ? oldDir : rand_dir();
  161. oldDir = dir;
  162. int nX = pX - (dir & R_LEFT ? 1 : 0) + (dir & R_RIGHT ? 1 : 0);
  163. int nY = pY - (dir & R_UP ? 1 : 0) + (dir & R_DOWN ? 1 : 0);
  164. if(nX < 0 || nY < 0 || nX >= ROOT_SIZE_X || nY >= ROOT_SIZE_Y) continue;
  165. int n = ROOT(nY, nX);
  166. state->filledRootBase[n] = true;
  167. // Connect points
  168. int p = ROOT(pY, pX);
  169. state->rootBase[p] |= dir;
  170. state->rootBase[n] |= reverse_dir(dir);
  171. // Grow from new point
  172. pX = nX;
  173. pY = nY;
  174. }
  175. }
  176. for(int y = 0; y < ROOT_SIZE_Y; y++) {
  177. for(int x = 0; x < ROOT_SIZE_X; x++) {
  178. int c = ROOT(y, x);
  179. if(!state->filledRootBase[c]) continue;
  180. /*
  181. if(has_root(state, x - 1, y)) state->rootBase[c] |= R_LEFT;
  182. if(has_root(state, x + 1, y)) state->rootBase[c] |= R_RIGHT;
  183. if(has_root(state, x, y - 1)) state->rootBase[c] |= R_UP;
  184. if(has_root(state, x, y + 1)) state->rootBase[c] |= R_DOWN;
  185. */
  186. for(int r = 0; r < RANDOM_GROW_ATTEMPTS; r++) {
  187. if(!rand_chance(RANDOM_GROW_CHANCE)) continue;
  188. state->rootBase[c] |= rand_dir();
  189. }
  190. }
  191. }
  192. // Copy root to real root
  193. int minX = cX, maxX = cX, minY = cY, maxY = cY;
  194. for(int y = 0; y < ROOT_SIZE_Y; y++) {
  195. for(int x = 0; x < ROOT_SIZE_X; x++) {
  196. int r = ROOT(y, x);
  197. if(!state->filledRootBase[r]) continue;
  198. minX = MIN(minX, x);
  199. maxX = MAX(maxX, x);
  200. minY = MIN(minY, y);
  201. maxY = MAX(maxY, y);
  202. }
  203. }
  204. // Clone to real root
  205. state->rootSizeX = maxX - minX + 1;
  206. state->rootSizeY = maxY - minY + 1;
  207. free_root(state);
  208. state->root = (char*)malloc(state->rootSizeX * state->rootSizeY * sizeof(char));
  209. state->filledRoot = (bool*)malloc(state->rootSizeX * state->rootSizeY * sizeof(bool));
  210. for(int y = 0; y < state->rootSizeY; y++) {
  211. for(int x = 0; x < state->rootSizeX; x++) {
  212. int c = root_index(state, y, x);
  213. int r = ROOT(y + minY, x + minX);
  214. state->filledRoot[c] = state->filledRootBase[r];
  215. state->root[c] = state->rootBase[r];
  216. }
  217. }
  218. }
  219. static bool in_borders(int x, int y) {
  220. return x >= 0 && y >= 0 && x < CELLS_X && y < CELLS_Y;
  221. }
  222. static char get_cell(GameState* state, int x, int y) {
  223. if(!in_borders(x, y)) return R_NONE;
  224. return state->cells[CELL(y, x)];
  225. }
  226. static bool get_filled_cell(GameState* state, int x, int y) {
  227. if(!in_borders(x, y)) return false;
  228. return state->filledCells[CELL(y, x)];
  229. }
  230. static bool can_place_root(GameState* state) {
  231. bool hasConnection = false;
  232. for(int y = 0; y < state->rootSizeY; y++) {
  233. for(int x = 0; x < state->rootSizeX; x++) {
  234. int r = root_index(state, y, x);
  235. if(!state->filledRoot[r]) {
  236. continue;
  237. }
  238. char root = state->root[r];
  239. int rY = y + state->pY;
  240. int rX = x + state->pX;
  241. // Check if colliding
  242. if(get_filled_cell(state, rX, rY)) {
  243. char cell = get_cell(state, rX, rY);
  244. if(has_intersection(cell, root)) {
  245. return false;
  246. }
  247. hasConnection = true;
  248. }
  249. // Check neighbours
  250. hasConnection |= (root & R_RIGHT) && (get_cell(state, rX + 1, rY) & R_LEFT);
  251. hasConnection |= (root & R_LEFT) && (get_cell(state, rX - 1, rY) & R_RIGHT);
  252. hasConnection |= (root & R_UP) && (get_cell(state, rX, rY - 1) & R_DOWN);
  253. hasConnection |= (root & R_DOWN) && (get_cell(state, rX, rY + 1) & R_UP);
  254. }
  255. }
  256. return hasConnection;
  257. }
  258. static bool try_place_root(GameState* state) {
  259. if(!can_place_root(state)) return false;
  260. for(int y = 0; y < state->rootSizeY; y++) {
  261. for(int x = 0; x < state->rootSizeX; x++) {
  262. int r = root_index(state, y, x);
  263. if(!state->filledRoot[r]) continue;
  264. int rY = y + state->pY;
  265. int rX = x + state->pX;
  266. // Root may be out of borders in rare cases (after new cpawn changed its size), just ignore that part
  267. if(in_borders(rX, rY)) {
  268. int c = CELL(rY, rX);
  269. state->filledCells[c] = true;
  270. state->cells[c] |= state->root[r];
  271. }
  272. }
  273. }
  274. return true;
  275. }
  276. static void reset_level(GameState* state) {
  277. state->stage = StageStart;
  278. state->tick = 0;
  279. for(int i = 0; i < CELLS_TOTAL; i++) {
  280. state->filledCells[i] = false;
  281. state->cells[i] = R_NONE;
  282. }
  283. generate_new_root(state);
  284. // Starting cells
  285. int midX = CELLS_X / 2;
  286. set_cell(state, 0, midX, R_UP | R_DOWN);
  287. set_cell(state, 1, midX, R_UP | R_DOWN | R_LEFT | R_RIGHT);
  288. set_cell(state, 1, midX - 1, R_RIGHT | R_DOWN);
  289. set_cell(state, 1, midX + 1, R_LEFT | R_DOWN);
  290. set_cell(state, 2, midX, R_UP);
  291. state->pX = midX;
  292. state->pY = 4;
  293. state->rerolls = REROLLS_MAX;
  294. state->score = 0;
  295. }
  296. static void recalculate_score(GameState* state) {
  297. int score = 0;
  298. for(int i = 0; i < CELLS_TOTAL; i++) {
  299. if(state->filledCells[i]) score++;
  300. }
  301. for(int i = 0; i < CELLS_TOTAL; i++) {
  302. if(!state->pickups[i] || !state->filledCells[i]) continue;
  303. state->pickups[i] = false;
  304. state->collectedPickups++;
  305. state->rerolls++;
  306. }
  307. state->score = (score + state->collectedPickups * PICKUPS_POINTS_FACTOR) * SCORE_FACTOR;
  308. }
  309. static void draw_root_cell(Canvas* canvas, char root, int y, int x, bool isHidden) {
  310. int posX = FIELD_START_X + x * CELL_SIZE + 1, posY = FIELD_START_Y + y * CELL_SIZE + 1;
  311. canvas_draw_dot(canvas, posX, posY);
  312. if(isHidden) {
  313. canvas_set_color(canvas, ColorXOR);
  314. }
  315. if(root & R_UP) canvas_draw_dot(canvas, posX, posY - 1);
  316. if(root & R_DOWN) canvas_draw_dot(canvas, posX, posY + 1);
  317. if(root & R_LEFT) canvas_draw_dot(canvas, posX - 1, posY);
  318. if(root & R_RIGHT) canvas_draw_dot(canvas, posX + 1, posY);
  319. if(isHidden) {
  320. canvas_set_color(canvas, ColorBlack);
  321. }
  322. }
  323. static void draw_placed_roots(Canvas* canvas, GameState* state) {
  324. for(int y = 0; y < CELLS_Y; y++) {
  325. for(int x = 0; x < CELLS_X; x++) {
  326. int c = CELL(y, x);
  327. if(!state->filledCells[c]) continue;
  328. draw_root_cell(canvas, state->cells[c], y, x, false);
  329. }
  330. }
  331. }
  332. static void draw_pickup(Canvas* canvas, GameState* state, int y, int x) {
  333. int posX = FIELD_START_X + x * CELL_SIZE + 1, posY = FIELD_START_Y + y * CELL_SIZE + 1;
  334. int stage = state->tick / PICKUP_FREQUENCY;
  335. if(stage++ % 4 < 3) canvas_draw_dot(canvas, posX + 1, posY);
  336. if(stage++ % 4 < 3) canvas_draw_dot(canvas, posX, posY + 1);
  337. if(stage++ % 4 < 3) canvas_draw_dot(canvas, posX - 1, posY);
  338. if(stage++ % 4 < 3) canvas_draw_dot(canvas, posX, posY - 1);
  339. }
  340. static void draw_pickups(Canvas* canvas, GameState* state) {
  341. for(int y = 0; y < CELLS_Y; y++) {
  342. for(int x = 0; x < CELLS_X; x++) {
  343. int c = CELL(y, x);
  344. if(!state->pickups[c]) continue;
  345. draw_pickup(canvas, state, y, x);
  346. }
  347. }
  348. }
  349. static void draw_active_root(Canvas* canvas, GameState* state) {
  350. bool isHidden = (state->tick % BLINK_PERIOD) < BLINK_HIDE_FRAMES;
  351. for(int y = 0; y < state->rootSizeY; y++) {
  352. for(int x = 0; x < state->rootSizeX; x++) {
  353. int c = root_index(state, y, x);
  354. if(!state->filledRoot[c]) continue;
  355. int realX = x + state->pX;
  356. int realY = y + state->pY;
  357. draw_root_cell(canvas, state->root[c], realY, realX, isHidden);
  358. }
  359. }
  360. }
  361. #if DRAW_DEBUG
  362. static void draw_generated_root(Canvas* canvas, GameState* state) {
  363. bool isHidden = (state->tick % BLINK_PERIOD) < BLINK_HIDE_FRAMES;
  364. for(int y = 0; y < ROOT_SIZE_Y; y++) {
  365. for(int x = 0; x < ROOT_SIZE_X; x++) {
  366. int c = ROOT(y, x);
  367. if(!state->filledRootBase[c]) continue;
  368. int realX = x + 1;
  369. int realY = y + 1;
  370. draw_root_cell(canvas, state->rootBase[c], realY, realX, isHidden);
  371. }
  372. }
  373. }
  374. #endif
  375. static void draw_ground(Canvas* canvas, GameState* state) {
  376. canvas_draw_line(canvas, 0, GROUND_HEIGHT, FLIPPER_LCD_WIDTH, GROUND_HEIGHT);
  377. UNUSED(state);
  378. }
  379. static void draw_tree(Canvas* canvas, GameState* state) {
  380. canvas_draw_icon(canvas, FLIPPER_LCD_WIDTH / 2 - 5, GROUND_HEIGHT - TREE_HEIGHT, &I_tree);
  381. UNUSED(state);
  382. }
  383. static void draw_placement(Canvas* canvas, GameState* state) {
  384. bool canPlace = can_place_root(state);
  385. canvas_draw_icon(canvas, FLIPPER_LCD_WIDTH - 10, 0, canPlace ? &I_place_ok : &I_place_error);
  386. }
  387. static void draw_rerolls(Canvas* canvas, GameState* state) {
  388. UNUSED(canvas);
  389. UNUSED(state);
  390. canvas_draw_icon(canvas, 0, 0, &I_root_reroll);
  391. // Ugh
  392. FuriString* tmp_string = furi_string_alloc();
  393. furi_string_printf(tmp_string, "%d", MAX(0, state->rerolls));
  394. canvas_draw_str(canvas, 11, 9, furi_string_get_cstr(tmp_string));
  395. furi_string_free(tmp_string);
  396. }
  397. static void draw_score(Canvas* canvas, GameState* state) {
  398. UNUSED(canvas);
  399. UNUSED(state);
  400. int x = FLIPPER_LCD_WIDTH / 2 + 15;
  401. canvas_draw_icon(canvas, x, 0, &I_score);
  402. // Ugh
  403. FuriString* tmp_string = furi_string_alloc();
  404. furi_string_printf(tmp_string, "%d", MAX(0, state->score));
  405. canvas_draw_str(canvas, x + 11, 9, furi_string_get_cstr(tmp_string));
  406. furi_string_free(tmp_string);
  407. }
  408. static void draw_gui(Canvas* canvas, GameState* state) {
  409. draw_ground(canvas, state);
  410. draw_tree(canvas, state);
  411. draw_placement(canvas, state);
  412. draw_rerolls(canvas, state);
  413. draw_score(canvas, state);
  414. }
  415. static void draw_center_box(Canvas* canvas, int w2, int h2, int margin) {
  416. int x = FLIPPER_LCD_WIDTH / 2 - w2;
  417. int y = FLIPPER_LCD_HEIGHT / 2 - h2;
  418. canvas_set_color(canvas, ColorWhite);
  419. canvas_draw_box(
  420. canvas, x - margin - 1, y - margin - 1, (w2 + margin + 1) * 2, (h2 + margin + 1) * 2);
  421. canvas_set_color(canvas, ColorBlack);
  422. canvas_draw_frame(canvas, x - margin, y - margin, (w2 + margin) * 2, (h2 + margin) * 2);
  423. }
  424. static void draw_start_ui(Canvas* canvas, GameState* state) {
  425. int w2 = 40;
  426. int margin = 3;
  427. int h2 = 10;
  428. draw_center_box(canvas, w2, h2, margin);
  429. int x = FLIPPER_LCD_WIDTH / 2 - w2;
  430. int y = FLIPPER_LCD_HEIGHT / 2 - h2;
  431. canvas_draw_str(canvas, x + 1, y + 9, " Grow your roots ");
  432. canvas_draw_str(canvas, x + 1, y + 18, "Press [OK] to start");
  433. UNUSED(state);
  434. }
  435. static void draw_end_ui(Canvas* canvas, GameState* state) {
  436. int w2 = 46;
  437. int margin = 3;
  438. int h2 = 15;
  439. draw_center_box(canvas, w2, h2, margin);
  440. int x = FLIPPER_LCD_WIDTH / 2 - w2;
  441. int y = FLIPPER_LCD_HEIGHT / 2 - h2;
  442. canvas_draw_str(canvas, x + 1, y + 9, " Game Over ");
  443. FuriString* tmp_string = furi_string_alloc();
  444. furi_string_printf(tmp_string, "You've got %d points", MAX(0, state->score));
  445. canvas_draw_str(canvas, x + 1, y + 19, furi_string_get_cstr(tmp_string));
  446. furi_string_free(tmp_string);
  447. canvas_draw_str(canvas, x + 2, y + 29, "Press [OK] to restart");
  448. UNUSED(state);
  449. }
  450. static void roots_draw_callback(Canvas* const canvas, void* ctx) {
  451. GameState* state = acquire_mutex((ValueMutex*)ctx, 25);
  452. if(state == NULL) {
  453. return;
  454. }
  455. if(!state->initialDraw) {
  456. state->initialDraw = true;
  457. canvas_set_font(canvas, FontSecondary);
  458. reset_level(state);
  459. }
  460. state->tick++;
  461. draw_gui(canvas, state);
  462. draw_placed_roots(canvas, state);
  463. draw_pickups(canvas, state);
  464. switch(state->stage) {
  465. case StageStart:
  466. draw_start_ui(canvas, state);
  467. break;
  468. case StageRun:
  469. draw_active_root(canvas, state);
  470. #if DRAW_DEBUG
  471. draw_generated_root(canvas, state);
  472. #endif
  473. break;
  474. case StageOver:
  475. draw_end_ui(canvas, state);
  476. break;
  477. }
  478. release_mutex((ValueMutex*)ctx, state);
  479. }
  480. static void roots_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  481. furi_assert(event_queue);
  482. GameEvent event = {.type = EventTypeKey, .input = *input_event};
  483. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  484. }
  485. static void roots_update_timer_callback(FuriMessageQueue* event_queue) {
  486. furi_assert(event_queue);
  487. GameEvent event = {.type = EventTypeTick};
  488. furi_message_queue_put(event_queue, &event, 0);
  489. }
  490. static void ProcessStartInput(GameState* state, InputKey key) {
  491. if(key == InputKeyOk) {
  492. state->stage = StageRun;
  493. }
  494. }
  495. static void ProcessRunInput(GameState* state, InputKey key) {
  496. switch(key) {
  497. case InputKeyRight:
  498. state->pX = MIN(state->pX + 1, CELLS_X - state->rootSizeX);
  499. break;
  500. case InputKeyLeft:
  501. state->pX = MAX(state->pX - 1, 0);
  502. break;
  503. case InputKeyUp:
  504. state->pY = MAX(state->pY - 1, 0);
  505. break;
  506. case InputKeyDown:
  507. state->pY = MIN(state->pY + 1, CELLS_Y - state->rootSizeY);
  508. break;
  509. case InputKeyOk: {
  510. bool rootPlaced = try_place_root(state);
  511. if(rootPlaced) {
  512. recalculate_score(state);
  513. generate_new_root(state);
  514. } else {
  515. state->rerolls--;
  516. if(state->rerolls >= 0) {
  517. generate_new_root(state);
  518. } else {
  519. state->stage = StageOver;
  520. }
  521. }
  522. break;
  523. }
  524. default:
  525. break;
  526. }
  527. }
  528. static void ProcessOverInput(GameState* state, InputKey key) {
  529. if(key == InputKeyOk) {
  530. state->stage = StageStart;
  531. reset_level(state);
  532. }
  533. }
  534. int32_t roots_of_life_game_app(void* p) {
  535. FURI_LOG_D(TAG, "Starting game...");
  536. UNUSED(p);
  537. int32_t return_code = 0;
  538. // Set random seed from interrR_UPts
  539. srand(DWT->CYCCNT);
  540. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
  541. GameState* state = malloc(sizeof(GameState));
  542. game_state_init(state);
  543. ValueMutex state_mutex;
  544. if(!init_mutex(&state_mutex, state, sizeof(GameState))) {
  545. FURI_LOG_E(TAG, "Cannot create mutex\r\n");
  546. return_code = 255;
  547. goto free_and_exit;
  548. }
  549. // Set system callbacks
  550. ViewPort* view_port = view_port_alloc();
  551. view_port_draw_callback_set(view_port, roots_draw_callback, &state_mutex);
  552. view_port_input_callback_set(view_port, roots_input_callback, event_queue);
  553. FuriTimer* timer =
  554. furi_timer_alloc(roots_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  555. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 22);
  556. // Open GUI and register view_port
  557. Gui* gui = furi_record_open(RECORD_GUI);
  558. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  559. FURI_LOG_D(TAG, "Entering game loop...");
  560. GameEvent event;
  561. for(bool processing = true; processing;) {
  562. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  563. GameState* state = (GameState*)acquire_mutex_block(&state_mutex);
  564. if(event_status == FuriStatusOk) {
  565. // Key events
  566. if(event.type == EventTypeKey) {
  567. FURI_LOG_D(TAG, "Got key: %d", event.input.key);
  568. if(event.input.type == InputTypePress || event.input.type == InputTypeLong ||
  569. event.input.type == InputTypeRepeat) {
  570. if(event.input.key == InputKeyBack) {
  571. processing = false;
  572. }
  573. switch(state->stage) {
  574. case StageStart:
  575. ProcessStartInput(state, event.input.key);
  576. break;
  577. case StageRun:
  578. ProcessRunInput(state, event.input.key);
  579. break;
  580. case StageOver:
  581. ProcessOverInput(state, event.input.key);
  582. break;
  583. }
  584. }
  585. }
  586. } else {
  587. // Event timeout
  588. FURI_LOG_D(TAG, "furi_message_queue: Event timeout");
  589. }
  590. view_port_update(view_port);
  591. release_mutex(&state_mutex, state);
  592. }
  593. furi_timer_free(timer);
  594. view_port_enabled_set(view_port, false);
  595. gui_remove_view_port(gui, view_port);
  596. furi_record_close(RECORD_GUI);
  597. furi_record_close(RECORD_NOTIFICATION);
  598. view_port_free(view_port);
  599. delete_mutex(&state_mutex);
  600. free_and_exit:
  601. FURI_LOG_D(TAG, "Quitting game...");
  602. game_state_free(state);
  603. free(state);
  604. furi_message_queue_free(event_queue);
  605. return return_code;
  606. }