heap_defence.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. //
  2. // Created by moh on 30.11.2021.
  3. //
  4. // Ported to latest firmware by @xMasterX - 18 Oct 2022
  5. //
  6. #include <string.h>
  7. #include "hede_assets.h"
  8. #include "heap_defence_icons.h"
  9. #include <furi.h>
  10. #include <gui/gui.h>
  11. #include <input/input.h>
  12. #include <notification/notification.h>
  13. #include <notification/notification_messages.h>
  14. #include <gui/canvas_i.h>
  15. #include <dolphin/dolphin.h>
  16. #define Y_FIELD_SIZE 6
  17. #define Y_LAST (Y_FIELD_SIZE - 1)
  18. #define X_FIELD_SIZE 12
  19. #define X_LAST (X_FIELD_SIZE - 1)
  20. #define DRAW_X_OFFSET 4
  21. #define TAG "HeDe"
  22. #define BOX_HEIGHT 10
  23. #define BOX_WIDTH 10
  24. #define TIMER_UPDATE_FREQ 8
  25. #define BOX_GENERATION_RATE 15
  26. static IconAnimation* BOX_DESTROYED;
  27. static const Icon* boxes[] = {
  28. (Icon*)&A_HD_BoxDestroyed_10x10,
  29. &I_Box1_10x10,
  30. &I_Box2_10x10,
  31. &I_Box3_10x10,
  32. &I_Box4_10x10,
  33. &I_Box5_10x10};
  34. static uint8_t BOX_TEXTURE_COUNT = sizeof(boxes) / sizeof(Icon*);
  35. typedef enum {
  36. AnimationGameOver = 0,
  37. AnimationPause,
  38. AnimationLeft,
  39. AnimationRight,
  40. } Animations;
  41. static IconAnimation* animations[4];
  42. typedef u_int8_t byte;
  43. typedef enum {
  44. GameStatusVibro = 1 << 0,
  45. GameStatusInProgress = 1 << 1,
  46. } GameStatuses;
  47. typedef struct {
  48. uint8_t x;
  49. uint8_t y;
  50. } Position;
  51. typedef enum { PlayerRising = 1, PlayerFalling = -1, PlayerNothing = 0 } PlayerStates;
  52. typedef struct {
  53. Position p;
  54. int8_t x_direction;
  55. int8_t j_tick;
  56. int8_t h_tick;
  57. int8_t states;
  58. bool right_frame;
  59. } Person;
  60. typedef struct {
  61. uint8_t offset : 4;
  62. uint8_t box_id : 3;
  63. uint8_t exists : 1;
  64. } Box;
  65. static const uint8_t ROW_BYTE_SIZE = sizeof(Box) * X_FIELD_SIZE;
  66. typedef struct {
  67. Box** field;
  68. Person* person;
  69. Animations animation;
  70. GameStatuses game_status;
  71. FuriMutex* mutex;
  72. } GameState;
  73. typedef Box** Field;
  74. typedef enum { EventGameTick, EventKeyPress } EventType;
  75. typedef struct {
  76. EventType type;
  77. InputEvent input;
  78. } GameEvent;
  79. /**
  80. * #Construct / Destroy
  81. */
  82. static void game_reset_field_and_player(GameState* game) {
  83. ///Reset field
  84. bzero(game->field[0], X_FIELD_SIZE * Y_FIELD_SIZE * sizeof(Box));
  85. ///Reset person
  86. bzero(game->person, sizeof(Person));
  87. game->person->p.x = X_FIELD_SIZE / 2;
  88. game->person->p.y = Y_LAST;
  89. }
  90. static GameState* allocGameState() {
  91. GameState* game = malloc(sizeof(GameState));
  92. game->person = malloc(sizeof(Person));
  93. game->field = malloc(Y_FIELD_SIZE * sizeof(Box*));
  94. game->field[0] = malloc(X_FIELD_SIZE * Y_FIELD_SIZE * sizeof(Box));
  95. for(int y = 1; y < Y_FIELD_SIZE; ++y) {
  96. game->field[y] = game->field[0] + (y * X_FIELD_SIZE);
  97. }
  98. game_reset_field_and_player(game);
  99. game->game_status = GameStatusInProgress;
  100. return game;
  101. }
  102. static void game_destroy(GameState* game) {
  103. furi_assert(game);
  104. free(game->field[0]);
  105. free(game->field);
  106. free(game);
  107. }
  108. static void assets_load() {
  109. /// Init animations
  110. animations[AnimationPause] = icon_animation_alloc(&A_HD_start_128x64);
  111. animations[AnimationGameOver] = icon_animation_alloc(&A_HD_game_over_128x64);
  112. animations[AnimationLeft] = icon_animation_alloc(&A_HD_person_left_10x20);
  113. animations[AnimationRight] = icon_animation_alloc(&A_HD_person_right_10x20);
  114. BOX_DESTROYED = icon_animation_alloc(&A_HD_BoxDestroyed_10x10);
  115. icon_animation_start(animations[AnimationLeft]);
  116. icon_animation_start(animations[AnimationRight]);
  117. }
  118. static void assets_clear() {
  119. for(int i = 0; i < 4; ++i) {
  120. icon_animation_stop(animations[i]);
  121. icon_animation_free(animations[i]);
  122. }
  123. icon_animation_free(BOX_DESTROYED);
  124. }
  125. /**
  126. * Box utils
  127. */
  128. static inline bool is_empty(Box* box) {
  129. return !box->exists;
  130. }
  131. static inline bool has_dropped(Box* box) {
  132. return box->offset == 0;
  133. }
  134. static Box* get_upper_box(Field field, Position current) {
  135. return (&field[current.y - 1][current.x]);
  136. }
  137. static Box* get_lower_box(Field field, Position current) {
  138. return (&field[current.y + 1][current.x]);
  139. }
  140. static Box* get_next_box(Field field, Position current, int x_direction) {
  141. return (&field[current.y][current.x + x_direction]);
  142. }
  143. static inline void decrement_y_offset_to_zero(Box* n) {
  144. if(n->offset) --n->offset;
  145. }
  146. static inline void heap_swap(Box* first, Box* second) {
  147. Box temp = *first;
  148. *first = *second;
  149. *second = temp;
  150. }
  151. /**
  152. * #Box logic
  153. */
  154. static void generate_box(GameState const* game) {
  155. furi_assert(game);
  156. static byte tick_count = BOX_GENERATION_RATE;
  157. if(tick_count++ != BOX_GENERATION_RATE) {
  158. return;
  159. }
  160. tick_count = 0;
  161. int x_offset = rand() % X_FIELD_SIZE;
  162. while(game->field[1][x_offset].exists) {
  163. x_offset = rand() % X_FIELD_SIZE;
  164. }
  165. game->field[1][x_offset].exists = true;
  166. game->field[1][x_offset].offset = BOX_HEIGHT;
  167. game->field[1][x_offset].box_id = (rand() % (BOX_TEXTURE_COUNT - 1)) + 1;
  168. }
  169. static void drop_box(GameState* game) {
  170. furi_assert(game);
  171. for(int y = Y_LAST; y > 0; y--) {
  172. for(int x = 0; x < X_FIELD_SIZE; x++) {
  173. Box* current_box = game->field[y] + x;
  174. Box* upper_box = game->field[y - 1] + x;
  175. if(y == Y_LAST) {
  176. decrement_y_offset_to_zero(current_box);
  177. }
  178. decrement_y_offset_to_zero(upper_box);
  179. if(is_empty(current_box) && !is_empty(upper_box) && has_dropped(upper_box)) {
  180. upper_box->offset = BOX_HEIGHT;
  181. heap_swap(current_box, upper_box);
  182. }
  183. }
  184. }
  185. }
  186. static bool clear_rows(Box** field) {
  187. for(int x = 0; x < X_FIELD_SIZE; ++x) {
  188. if(is_empty(field[Y_LAST] + x) || !has_dropped(field[Y_LAST] + x)) {
  189. return false;
  190. }
  191. }
  192. memset(field[Y_LAST], 128, ROW_BYTE_SIZE);
  193. return true;
  194. }
  195. /**
  196. * Input Handling
  197. */
  198. static inline bool on_ground(Person* person, Field field) {
  199. return person->p.y == Y_LAST || field[person->p.y + 1][person->p.x].exists;
  200. }
  201. static void handle_key_presses(Person* person, InputEvent* input, GameState* game) {
  202. switch(input->key) {
  203. case InputKeyUp:
  204. if(person->states == PlayerNothing && on_ground(person, game->field)) {
  205. person->states = PlayerRising;
  206. person->j_tick = 0;
  207. }
  208. break;
  209. case InputKeyLeft:
  210. person->right_frame = false;
  211. if(person->h_tick == 0) {
  212. person->h_tick = 1;
  213. person->x_direction = -1;
  214. }
  215. break;
  216. case InputKeyRight:
  217. person->right_frame = true;
  218. if(person->h_tick == 0) {
  219. person->h_tick = 1;
  220. person->x_direction = 1;
  221. }
  222. break;
  223. case InputKeyOk:
  224. game->game_status &= ~GameStatusInProgress;
  225. game->animation = AnimationPause;
  226. icon_animation_start(animations[AnimationPause]);
  227. default:
  228. break;
  229. }
  230. }
  231. /**
  232. * #Person logic
  233. */
  234. static inline bool ground_box_check(Field field, Position new_position) {
  235. Box* lower_box = get_lower_box(field, new_position);
  236. bool ground_box_dropped =
  237. (new_position.y == Y_LAST || //Eсли мы и так в самом низу
  238. is_empty(lower_box) || // Ecли снизу пустота
  239. has_dropped(lower_box)); //Eсли бокс снизу допадал
  240. return ground_box_dropped;
  241. }
  242. static inline bool is_movable(Field field, Position box_pos, int x_direction) {
  243. //TODO::Moжет и не двух, предположение
  244. bool out_of_bounds = box_pos.x == 0 || box_pos.x == X_LAST;
  245. if(out_of_bounds) return false;
  246. bool box_on_top = box_pos.y < 1 || get_upper_box(field, box_pos)->exists;
  247. if(box_on_top) return false;
  248. bool has_next_box = get_next_box(field, box_pos, x_direction)->exists;
  249. if(has_next_box) return false;
  250. return true;
  251. }
  252. static bool horizontal_move(Person* person, Field field) {
  253. Position new_position = person->p;
  254. if(!person->x_direction) return false;
  255. new_position.x += person->x_direction;
  256. bool on_edge_column = new_position.x > X_LAST;
  257. if(on_edge_column) return false;
  258. if(is_empty(&field[new_position.y][new_position.x])) {
  259. bool ground_box_dropped = ground_box_check(field, new_position);
  260. if(ground_box_dropped) {
  261. person->p = new_position;
  262. return true;
  263. }
  264. } else if(is_movable(field, new_position, person->x_direction)) {
  265. *get_next_box(field, new_position, person->x_direction) =
  266. field[new_position.y][new_position.x];
  267. field[new_position.y][new_position.x] = (Box){0};
  268. person->p = new_position;
  269. return true;
  270. }
  271. return false;
  272. }
  273. void hd_person_set_state(Person* person, PlayerStates state) {
  274. person->states = state;
  275. person->j_tick = 0;
  276. }
  277. static void person_move(Person* person, Field field) {
  278. /// Left-right logic
  279. FURI_LOG_W(TAG, "[JUMP]func:[%s] line: %d", __FUNCTION__, __LINE__);
  280. if(person->states == PlayerNothing) {
  281. if(!on_ground(person, field)) {
  282. hd_person_set_state(person, PlayerFalling);
  283. }
  284. } else if(person->states == PlayerRising) {
  285. if(person->j_tick++ == 0) {
  286. person->p.y--;
  287. } else if(person->j_tick == 6) {
  288. hd_person_set_state(person, PlayerNothing);
  289. }
  290. /// Destroy upper box
  291. get_upper_box(field, person->p)->box_id = 0;
  292. field[person->p.y][person->p.x].box_id = 0;
  293. } else if(person->states == PlayerFalling) {
  294. if(person->j_tick++ == 0) {
  295. if(on_ground(person, field)) { // TODO: Test the bugfix
  296. hd_person_set_state(person, PlayerNothing);
  297. } else {
  298. person->p.y++;
  299. }
  300. } else if(person->j_tick == 5) {
  301. if(on_ground(person, field)) {
  302. hd_person_set_state(person, PlayerNothing);
  303. } else {
  304. hd_person_set_state(person, PlayerFalling);
  305. }
  306. }
  307. }
  308. switch(person->h_tick) {
  309. case 0:
  310. break;
  311. case 1:
  312. person->h_tick++;
  313. FURI_LOG_W(TAG, "[JUMP]func:[%s] line: %d", __FUNCTION__, __LINE__);
  314. bool moved = horizontal_move(person, field);
  315. if(!moved) {
  316. person->h_tick = 0;
  317. person->x_direction = 0;
  318. }
  319. break;
  320. case 5:
  321. FURI_LOG_W(TAG, "[JUMP]func:[%s] line: %d", __FUNCTION__, __LINE__);
  322. person->h_tick = 0;
  323. person->x_direction = 0;
  324. break;
  325. default:
  326. FURI_LOG_W(TAG, "[JUMP]func:[%s] line: %d", __FUNCTION__, __LINE__);
  327. person->h_tick++;
  328. }
  329. }
  330. static inline bool is_person_dead(Person* person, Box** field) {
  331. return get_upper_box(field, person->p)->box_id != 0;
  332. }
  333. /**
  334. * #Callback
  335. */
  336. static void draw_box(Canvas* canvas, Box* box, int x, int y) {
  337. if(is_empty(box)) {
  338. return;
  339. }
  340. byte y_screen = y * BOX_HEIGHT - box->offset;
  341. byte x_screen = x * BOX_WIDTH + DRAW_X_OFFSET;
  342. if(box->box_id == 0) {
  343. canvas_set_bitmap_mode(canvas, true);
  344. icon_animation_start(BOX_DESTROYED);
  345. canvas_draw_icon_animation(canvas, x_screen, y_screen, BOX_DESTROYED);
  346. if(icon_animation_is_last_frame(BOX_DESTROYED)) {
  347. *box = (Box){0};
  348. icon_animation_stop(BOX_DESTROYED);
  349. }
  350. canvas_set_bitmap_mode(canvas, false);
  351. } else {
  352. canvas_draw_icon(canvas, x_screen, y_screen, boxes[box->box_id]);
  353. }
  354. }
  355. static void heap_defense_render_callback(Canvas* const canvas, void* mutex) {
  356. furi_assert(mutex);
  357. const GameState* game = mutex;
  358. furi_mutex_acquire(game->mutex, FuriWaitForever);
  359. ///Draw GameOver or Pause
  360. if(!(game->game_status & GameStatusInProgress)) {
  361. FURI_LOG_W(TAG, "[DAED_DRAW]func: [%s] line: %d ", __FUNCTION__, __LINE__);
  362. canvas_draw_icon_animation(canvas, 0, 0, animations[game->animation]);
  363. furi_mutex_release(game->mutex);
  364. return;
  365. }
  366. ///Draw field
  367. canvas_draw_icon(canvas, 0, 0, &I_Background_128x64);
  368. ///Draw Person
  369. const Person* person = game->person;
  370. IconAnimation* player_animation = person->right_frame ? animations[AnimationRight] :
  371. animations[AnimationLeft];
  372. uint8_t x_screen = person->p.x * BOX_WIDTH + DRAW_X_OFFSET;
  373. if(person->h_tick && person->h_tick != 1) {
  374. if(person->right_frame) {
  375. x_screen += (person->h_tick) * 2 - BOX_WIDTH;
  376. } else {
  377. x_screen -= (person->h_tick) * 2 - BOX_WIDTH;
  378. }
  379. }
  380. uint8_t y_screen = (person->p.y - 1) * BOX_HEIGHT;
  381. if(person->j_tick) {
  382. if(person->states == PlayerRising) {
  383. y_screen += BOX_HEIGHT - (person->j_tick) * 2;
  384. } else if(person->states == PlayerFalling) {
  385. y_screen -= BOX_HEIGHT - (person->j_tick) * 2;
  386. }
  387. }
  388. canvas_draw_icon_animation(canvas, x_screen, y_screen, player_animation);
  389. ///Draw Boxes
  390. canvas_set_color(canvas, ColorBlack);
  391. for(int y = 1; y < Y_FIELD_SIZE; ++y) {
  392. for(int x = 0; x < X_FIELD_SIZE; ++x) {
  393. draw_box(canvas, &(game->field[y][x]), x, y);
  394. }
  395. }
  396. furi_mutex_release(game->mutex);
  397. }
  398. static void heap_defense_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  399. if(input_event->type != InputTypePress && input_event->type != InputTypeLong) return;
  400. furi_assert(event_queue);
  401. GameEvent event = {.type = EventKeyPress, .input = *input_event};
  402. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  403. }
  404. static void heap_defense_timer_callback(FuriMessageQueue* event_queue) {
  405. furi_assert(event_queue);
  406. GameEvent event;
  407. event.type = EventGameTick;
  408. event.input = (InputEvent){0};
  409. furi_message_queue_put(event_queue, &event, 0);
  410. }
  411. int32_t heap_defence_app(void* p) {
  412. UNUSED(p);
  413. //FURI_LOG_W(TAG, "Heap defence start %d", __LINE__);
  414. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
  415. GameState* game = allocGameState();
  416. game->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  417. if(!game->mutex) {
  418. game_destroy(game);
  419. return 1;
  420. }
  421. assets_load();
  422. ViewPort* view_port = view_port_alloc();
  423. view_port_draw_callback_set(view_port, heap_defense_render_callback, game);
  424. view_port_input_callback_set(view_port, heap_defense_input_callback, event_queue);
  425. FuriTimer* timer =
  426. furi_timer_alloc(heap_defense_timer_callback, FuriTimerTypePeriodic, event_queue);
  427. furi_timer_start(timer, furi_kernel_get_tick_frequency() / TIMER_UPDATE_FREQ);
  428. Gui* gui = furi_record_open(RECORD_GUI);
  429. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  430. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  431. memset(game->field[Y_LAST], 128, ROW_BYTE_SIZE);
  432. game->person->p.y -= 2;
  433. game->game_status = 0;
  434. game->animation = AnimationPause;
  435. // Call dolphin deed on game start
  436. dolphin_deed(DolphinDeedPluginGameStart);
  437. GameEvent event = {0};
  438. while(event.input.key != InputKeyBack) {
  439. if(furi_message_queue_get(event_queue, &event, 100) != FuriStatusOk) {
  440. continue;
  441. }
  442. furi_mutex_acquire(game->mutex, FuriWaitForever);
  443. //unset vibration
  444. if(game->game_status & GameStatusVibro) {
  445. notification_message(notification, &sequence_reset_vibro);
  446. game->game_status &= ~GameStatusVibro;
  447. icon_animation_stop(BOX_DESTROYED);
  448. memset(game->field[Y_LAST], 0, ROW_BYTE_SIZE);
  449. }
  450. if(!(game->game_status & GameStatusInProgress)) {
  451. if(event.type == EventKeyPress && event.input.key == InputKeyOk) {
  452. game->game_status |= GameStatusInProgress;
  453. icon_animation_stop(animations[game->animation]);
  454. }
  455. } else if(event.type == EventKeyPress) {
  456. handle_key_presses(game->person, &(event.input), game);
  457. } else { // EventGameTick
  458. drop_box(game);
  459. generate_box(game);
  460. if(clear_rows(game->field)) {
  461. notification_message(notification, &sequence_set_vibro_on);
  462. icon_animation_start(BOX_DESTROYED);
  463. game->game_status |= GameStatusVibro;
  464. }
  465. person_move(game->person, game->field);
  466. if(is_person_dead(game->person, game->field)) {
  467. game->game_status &= ~GameStatusInProgress;
  468. game->animation = AnimationGameOver;
  469. icon_animation_start(animations[AnimationGameOver]);
  470. game_reset_field_and_player(game);
  471. notification_message(notification, &sequence_error);
  472. }
  473. }
  474. furi_mutex_release(game->mutex);
  475. view_port_update(view_port);
  476. }
  477. furi_timer_free(timer);
  478. view_port_enabled_set(view_port, false);
  479. gui_remove_view_port(gui, view_port);
  480. view_port_free(view_port);
  481. furi_record_close(RECORD_GUI);
  482. furi_record_close(RECORD_NOTIFICATION);
  483. furi_message_queue_free(event_queue);
  484. assets_clear();
  485. furi_mutex_free(game->mutex);
  486. game_destroy(game);
  487. return 0;
  488. }