roots_of_life_game.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. }
  125. static void free_root(GameState* state) {
  126. if(state->root) free(state->root);
  127. if(state->filledRoot) free(state->filledRoot);
  128. }
  129. static void game_state_free(GameState* state) {
  130. free(state->filledCells);
  131. free(state->cells);
  132. free(state->pickups);
  133. free(state->rootBase);
  134. free(state->filledRootBase);
  135. free_root(state);
  136. }
  137. /*static bool has_root(GameState* state, int x, int y) {
  138. return x >= 0 && x < ROOT_SIZE_X && y >= 0 && y < ROOT_SIZE_Y &&
  139. state->filledRootBase[ROOT(y, x)];
  140. }*/
  141. static void generate_new_root(GameState* state) {
  142. for(int i = 0; i < ROOT_SIZE_X * ROOT_SIZE_Y; i++) {
  143. state->filledRootBase[i] = false;
  144. state->rootBase[i] = R_NONE;
  145. }
  146. int cX = ROOT_SIZE_X / 2;
  147. int cY = ROOT_SIZE_Y / 2;
  148. int c = ROOT(cY, cX);
  149. state->filledRootBase[c] = true;
  150. for(int i = 0; i < SPAWN_DIRECTIONS; i++) {
  151. int pX = cX, pY = cY;
  152. Direction oldDir = rand_dir();
  153. for(int g = 0; g < GROW_STEPS; g++) {
  154. Direction dir = rand_chance(GROW_SAME_DIRECTION_CHANCE) ? oldDir : rand_dir();
  155. oldDir = dir;
  156. int nX = pX - (dir & R_LEFT ? 1 : 0) + (dir & R_RIGHT ? 1 : 0);
  157. int nY = pY - (dir & R_UP ? 1 : 0) + (dir & R_DOWN ? 1 : 0);
  158. if(nX < 0 || nY < 0 || nX >= ROOT_SIZE_X || nY >= ROOT_SIZE_Y) continue;
  159. int n = ROOT(nY, nX);
  160. state->filledRootBase[n] = true;
  161. // Connect points
  162. int p = ROOT(pY, pX);
  163. state->rootBase[p] |= dir;
  164. state->rootBase[n] |= reverse_dir(dir);
  165. // Grow from new point
  166. pX = nX;
  167. pY = nY;
  168. }
  169. }
  170. for(int y = 0; y < ROOT_SIZE_Y; y++) {
  171. for(int x = 0; x < ROOT_SIZE_X; x++) {
  172. int c = ROOT(y, x);
  173. if(!state->filledRootBase[c]) continue;
  174. /*
  175. if(has_root(state, x - 1, y)) state->rootBase[c] |= R_LEFT;
  176. if(has_root(state, x + 1, y)) state->rootBase[c] |= R_RIGHT;
  177. if(has_root(state, x, y - 1)) state->rootBase[c] |= R_UP;
  178. if(has_root(state, x, y + 1)) state->rootBase[c] |= R_DOWN;
  179. */
  180. for(int r = 0; r < RANDOM_GROW_ATTEMPTS; r++) {
  181. if(!rand_chance(RANDOM_GROW_CHANCE)) continue;
  182. state->rootBase[c] |= rand_dir();
  183. }
  184. }
  185. }
  186. // Copy root to real root
  187. int minX = cX, maxX = cX, minY = cY, maxY = cY;
  188. for(int y = 0; y < ROOT_SIZE_Y; y++) {
  189. for(int x = 0; x < ROOT_SIZE_X; x++) {
  190. int r = ROOT(y, x);
  191. if(!state->filledRootBase[r]) continue;
  192. minX = MIN(minX, x);
  193. maxX = MAX(maxX, x);
  194. minY = MIN(minY, y);
  195. maxY = MAX(maxY, y);
  196. }
  197. }
  198. // Clone to real root
  199. state->rootSizeX = maxX - minX + 1;
  200. state->rootSizeY = maxY - minY + 1;
  201. free_root(state);
  202. state->root = (char*)malloc(state->rootSizeX * state->rootSizeY * sizeof(char));
  203. state->filledRoot = (bool*)malloc(state->rootSizeX * state->rootSizeY * sizeof(bool));
  204. for(int y = 0; y < state->rootSizeY; y++) {
  205. for(int x = 0; x < state->rootSizeX; x++) {
  206. int c = root_index(state, y, x);
  207. int r = ROOT(y + minY, x + minX);
  208. state->filledRoot[c] = state->filledRootBase[r];
  209. state->root[c] = state->rootBase[r];
  210. }
  211. }
  212. }
  213. static bool in_borders(int x, int y) {
  214. return x >= 0 && y >= 0 && x < CELLS_X && y < CELLS_Y;
  215. }
  216. static char get_cell(GameState* state, int x, int y) {
  217. if(!in_borders(x, y)) return R_NONE;
  218. return state->cells[CELL(y, x)];
  219. }
  220. static bool get_filled_cell(GameState* state, int x, int y) {
  221. if(!in_borders(x, y)) return false;
  222. return state->filledCells[CELL(y, x)];
  223. }
  224. static bool can_place_root(GameState* state) {
  225. bool hasConnection = false;
  226. for(int y = 0; y < state->rootSizeY; y++) {
  227. for(int x = 0; x < state->rootSizeX; x++) {
  228. int r = root_index(state, y, x);
  229. if(!state->filledRoot[r]) {
  230. continue;
  231. }
  232. char root = state->root[r];
  233. int rY = y + state->pY;
  234. int rX = x + state->pX;
  235. // Check if colliding
  236. if(get_filled_cell(state, rX, rY)) {
  237. char cell = get_cell(state, rX, rY);
  238. if(has_intersection(cell, root)) {
  239. return false;
  240. }
  241. hasConnection = true;
  242. }
  243. // Check neighbours
  244. hasConnection |= (root & R_RIGHT) && (get_cell(state, rX + 1, rY) & R_LEFT);
  245. hasConnection |= (root & R_LEFT) && (get_cell(state, rX - 1, rY) & R_RIGHT);
  246. hasConnection |= (root & R_UP) && (get_cell(state, rX, rY - 1) & R_DOWN);
  247. hasConnection |= (root & R_DOWN) && (get_cell(state, rX, rY + 1) & R_UP);
  248. }
  249. }
  250. return hasConnection;
  251. }
  252. static bool try_place_root(GameState* state) {
  253. if(!can_place_root(state)) return false;
  254. for(int y = 0; y < state->rootSizeY; y++) {
  255. for(int x = 0; x < state->rootSizeX; x++) {
  256. int r = root_index(state, y, x);
  257. if(!state->filledRoot[r]) continue;
  258. int rY = y + state->pY;
  259. int rX = x + state->pX;
  260. // Root may be out of borders in rare cases (after new cpawn changed its size), just ignore that part
  261. if(in_borders(rX, rY)) {
  262. int c = CELL(rY, rX);
  263. state->filledCells[c] = true;
  264. state->cells[c] |= state->root[r];
  265. }
  266. }
  267. }
  268. return true;
  269. }
  270. static void reset_level(GameState* state) {
  271. state->stage = StageStart;
  272. state->tick = 0;
  273. for(int i = 0; i < CELLS_TOTAL; i++) {
  274. state->filledCells[i] = false;
  275. state->cells[i] = R_NONE;
  276. }
  277. generate_new_root(state);
  278. // Starting cells
  279. int midX = CELLS_X / 2;
  280. set_cell(state, 0, midX, R_UP | R_DOWN);
  281. set_cell(state, 1, midX, R_UP | R_DOWN | R_LEFT | R_RIGHT);
  282. set_cell(state, 1, midX - 1, R_RIGHT | R_DOWN);
  283. set_cell(state, 1, midX + 1, R_LEFT | R_DOWN);
  284. set_cell(state, 2, midX, R_UP);
  285. state->pX = midX;
  286. state->pY = 4;
  287. state->rerolls = REROLLS_MAX;
  288. state->score = 0;
  289. state->collectedPickups = 0;
  290. for(int i = 0, n = rand_range(PICKUPS_MIN, PICKUPS_MAX); i < n; i++) {
  291. int x = rand_range(0, CELLS_X);
  292. int y = rand_range(0, CELLS_Y);
  293. state->pickups[CELL(y, x)] = true;
  294. }
  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. int h = 13, w = 54;
  449. canvas_set_color(canvas, ColorWhite);
  450. canvas_draw_box(canvas, 0, FLIPPER_LCD_HEIGHT - h, w + 1, h + 1);
  451. canvas_set_color(canvas, ColorBlack);
  452. canvas_draw_frame(canvas, 0, FLIPPER_LCD_HEIGHT - h, w, h);
  453. canvas_draw_str(canvas, 2, FLIPPER_LCD_HEIGHT - 3, "by @Xorboo");
  454. UNUSED(state);
  455. }
  456. static void roots_draw_callback(Canvas* const canvas, void* ctx) {
  457. GameState* state = acquire_mutex((ValueMutex*)ctx, 25);
  458. if(state == NULL) {
  459. return;
  460. }
  461. if(!state->initialDraw) {
  462. state->initialDraw = true;
  463. canvas_set_font(canvas, FontSecondary);
  464. reset_level(state);
  465. }
  466. state->tick++;
  467. draw_gui(canvas, state);
  468. draw_placed_roots(canvas, state);
  469. draw_pickups(canvas, state);
  470. switch(state->stage) {
  471. case StageStart:
  472. draw_start_ui(canvas, state);
  473. break;
  474. case StageRun:
  475. draw_active_root(canvas, state);
  476. #if DRAW_DEBUG
  477. draw_generated_root(canvas, state);
  478. #endif
  479. break;
  480. case StageOver:
  481. draw_end_ui(canvas, state);
  482. break;
  483. }
  484. release_mutex((ValueMutex*)ctx, state);
  485. }
  486. static void roots_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  487. furi_assert(event_queue);
  488. GameEvent event = {.type = EventTypeKey, .input = *input_event};
  489. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  490. }
  491. static void roots_update_timer_callback(FuriMessageQueue* event_queue) {
  492. furi_assert(event_queue);
  493. GameEvent event = {.type = EventTypeTick};
  494. furi_message_queue_put(event_queue, &event, 0);
  495. }
  496. static void ProcessStartInput(GameState* state, InputKey key) {
  497. if(key == InputKeyOk) {
  498. state->stage = StageRun;
  499. }
  500. }
  501. static void ProcessRunInput(GameState* state, InputKey key) {
  502. switch(key) {
  503. case InputKeyRight:
  504. state->pX = MIN(state->pX + 1, CELLS_X - state->rootSizeX);
  505. break;
  506. case InputKeyLeft:
  507. state->pX = MAX(state->pX - 1, 0);
  508. break;
  509. case InputKeyUp:
  510. state->pY = MAX(state->pY - 1, 0);
  511. break;
  512. case InputKeyDown:
  513. state->pY = MIN(state->pY + 1, CELLS_Y - state->rootSizeY);
  514. break;
  515. case InputKeyOk: {
  516. bool rootPlaced = try_place_root(state);
  517. if(rootPlaced) {
  518. recalculate_score(state);
  519. generate_new_root(state);
  520. } else {
  521. state->rerolls--;
  522. if(state->rerolls >= 0) {
  523. generate_new_root(state);
  524. } else {
  525. state->stage = StageOver;
  526. }
  527. }
  528. break;
  529. }
  530. default:
  531. break;
  532. }
  533. }
  534. static void ProcessOverInput(GameState* state, InputKey key) {
  535. if(key == InputKeyOk) {
  536. state->stage = StageStart;
  537. reset_level(state);
  538. }
  539. }
  540. int32_t roots_of_life_game_app(void* p) {
  541. FURI_LOG_D(TAG, "Starting game...");
  542. UNUSED(p);
  543. int32_t return_code = 0;
  544. // Set random seed from interrR_UPts
  545. srand(DWT->CYCCNT);
  546. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
  547. GameState* state = malloc(sizeof(GameState));
  548. game_state_init(state);
  549. ValueMutex state_mutex;
  550. if(!init_mutex(&state_mutex, state, sizeof(GameState))) {
  551. FURI_LOG_E(TAG, "Cannot create mutex\r\n");
  552. return_code = 255;
  553. goto free_and_exit;
  554. }
  555. // Set system callbacks
  556. ViewPort* view_port = view_port_alloc();
  557. view_port_draw_callback_set(view_port, roots_draw_callback, &state_mutex);
  558. view_port_input_callback_set(view_port, roots_input_callback, event_queue);
  559. FuriTimer* timer =
  560. furi_timer_alloc(roots_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  561. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 22);
  562. // Open GUI and register view_port
  563. Gui* gui = furi_record_open(RECORD_GUI);
  564. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  565. FURI_LOG_D(TAG, "Entering game loop...");
  566. GameEvent event;
  567. for(bool processing = true; processing;) {
  568. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  569. GameState* state = (GameState*)acquire_mutex_block(&state_mutex);
  570. if(event_status == FuriStatusOk) {
  571. // Key events
  572. if(event.type == EventTypeKey) {
  573. FURI_LOG_D(TAG, "Got key: %d", event.input.key);
  574. if(event.input.type == InputTypePress || event.input.type == InputTypeLong ||
  575. event.input.type == InputTypeRepeat) {
  576. if(event.input.key == InputKeyBack) {
  577. processing = false;
  578. }
  579. switch(state->stage) {
  580. case StageStart:
  581. ProcessStartInput(state, event.input.key);
  582. break;
  583. case StageRun:
  584. ProcessRunInput(state, event.input.key);
  585. break;
  586. case StageOver:
  587. ProcessOverInput(state, event.input.key);
  588. break;
  589. }
  590. }
  591. }
  592. } else {
  593. // Event timeout
  594. FURI_LOG_D(TAG, "furi_message_queue: Event timeout");
  595. }
  596. view_port_update(view_port);
  597. release_mutex(&state_mutex, state);
  598. }
  599. furi_timer_free(timer);
  600. view_port_enabled_set(view_port, false);
  601. gui_remove_view_port(gui, view_port);
  602. furi_record_close(RECORD_GUI);
  603. furi_record_close(RECORD_NOTIFICATION);
  604. view_port_free(view_port);
  605. delete_mutex(&state_mutex);
  606. free_and_exit:
  607. FURI_LOG_D(TAG, "Quitting game...");
  608. game_state_free(state);
  609. free(state);
  610. furi_message_queue_free(event_queue);
  611. return return_code;
  612. }