game15.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <notification/notification.h>
  4. #include <notification/notification_messages.h>
  5. #include <storage/storage.h>
  6. #include <dolphin/dolphin.h>
  7. #include "sandbox.h"
  8. #define FPS 20
  9. #define CELL_WIDTH 10
  10. #define CELL_HEIGHT 8
  11. #define MOVE_TICKS 5
  12. #define KEY_STACK_SIZE 16
  13. #define SAVING_DIRECTORY STORAGE_APP_DATA_PATH_PREFIX
  14. #define SAVING_FILENAME SAVING_DIRECTORY "/game15.save"
  15. #define POPUP_MENU_ITEMS 2
  16. typedef enum {
  17. DirectionNone,
  18. DirectionUp,
  19. DirectionDown,
  20. DirectionLeft,
  21. DirectionRight
  22. } direction_e;
  23. typedef enum {
  24. ScenePlay,
  25. SceneWin,
  26. ScenePopup
  27. } scene_e;
  28. typedef struct {
  29. uint8_t cell_index;
  30. uint8_t zero_index;
  31. uint8_t move_direction;
  32. uint8_t move_ticks;
  33. } moving_cell_t;
  34. typedef struct {
  35. uint16_t top_record;
  36. scene_e scene;
  37. uint16_t move_count;
  38. uint32_t tick_count;
  39. uint8_t board[16];
  40. } game_state_t;
  41. static game_state_t game_state;
  42. static NotificationApp* notification;
  43. static moving_cell_t moving_cell;
  44. static uint8_t loaded_saving_ticks;
  45. static uint8_t popup_menu_selected_item;
  46. static const char* popup_menu_strings[] = {"Continue", "Reset"};
  47. static uint8_t keys[KEY_STACK_SIZE];
  48. static uint8_t key_stack_head = 0;
  49. static const uint8_t pic_cells[] = {
  50. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51. 0x30, 0xfc, 0x38, 0xfc, 0x30, 0xfc, 0x30, 0xfc, 0x30, 0xfc, 0x30, 0xfc, 0x30, 0xfc, 0x30, 0xfc,
  52. 0x78, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0x60, 0xfc, 0x30, 0xfc, 0x18, 0xfc, 0x0c, 0xfc, 0xfc, 0xfc,
  53. 0x78, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0x60, 0xfc, 0xc0, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0x78, 0xfc,
  54. 0x70, 0xfc, 0x78, 0xfc, 0x68, 0xfc, 0x6c, 0xfc, 0x6c, 0xfc, 0xec, 0xfc, 0xfc, 0xfc, 0x60, 0xfc,
  55. 0xfc, 0xfc, 0x0c, 0xfc, 0x0c, 0xfc, 0x7c, 0xfc, 0xc0, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0x78, 0xfc,
  56. 0x78, 0xfc, 0x0c, 0xfc, 0x0c, 0xfc, 0x7c, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0x78, 0xfc,
  57. 0xfc, 0xfc, 0xc0, 0xfc, 0xc0, 0xfc, 0xc0, 0xfc, 0xc0, 0xfc, 0xc0, 0xfc, 0xc0, 0xfc, 0xc0, 0xfc,
  58. 0x78, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0x78, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0x78, 0xfc,
  59. 0x78, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0xcc, 0xfc, 0xf8, 0xfc, 0xc0, 0xfc, 0xc0, 0xfc, 0x78, 0xfc,
  60. 0xe6, 0xfd, 0x37, 0xff, 0x36, 0xff, 0x36, 0xff, 0x36, 0xff, 0x36, 0xff, 0x36, 0xff, 0xe6, 0xfd,
  61. 0x8c, 0xfd, 0xce, 0xfd, 0x8c, 0xfd, 0x8c, 0xfd, 0x8c, 0xfd, 0x8c, 0xfd, 0x8c, 0xfd, 0x8c, 0xfd,
  62. 0xe6, 0xfd, 0x37, 0xff, 0x36, 0xff, 0x86, 0xfd, 0xc6, 0xfc, 0x66, 0xfc, 0x36, 0xfc, 0xf6, 0xff,
  63. 0xe6, 0xfd, 0x37, 0xff, 0x36, 0xff, 0x86, 0xfd, 0x06, 0xff, 0x36, 0xff, 0x36, 0xff, 0xe6, 0xfd,
  64. 0xc6, 0xfd, 0xe7, 0xfd, 0xa6, 0xfd, 0xb6, 0xfd, 0xb6, 0xfd, 0xb6, 0xff, 0xf6, 0xff, 0x86, 0xfd,
  65. 0xf6, 0xff, 0x37, 0xfc, 0x36, 0xfc, 0xf6, 0xfd, 0x06, 0xff, 0x36, 0xff, 0x36, 0xff, 0xe6, 0xfd,
  66. };
  67. static const uint8_t pic_digits[] = {
  68. 0xf0, 0xf2, 0xf2, 0xf2, 0xf2, 0xf0, 0xf9, 0xf8, 0xf9, 0xf9, 0xf9, 0xf0, 0xf0, 0xf2, 0xf3,
  69. 0xf1, 0xfc, 0xf0, 0xf0, 0xf3, 0xf1, 0xf3, 0xf2, 0xf0, 0xf3, 0xf1, 0xf2, 0xf2, 0xf0, 0xf3,
  70. 0xf0, 0xfc, 0xf0, 0xf3, 0xf2, 0xf0, 0x00, 0x0c, 0x00, 0x02, 0x02, 0x00, 0x00, 0x03, 0x03,
  71. 0x03, 0x03, 0x03, 0x00, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x02, 0x00, 0x03, 0x03, 0x00,
  72. };
  73. static const uint8_t pic_top[] = {11, 4, 0x88, 0xf8, 0xad, 0xfa, 0xad, 0xf8, 0x8d, 0xfe};
  74. static const uint8_t pic_move[] =
  75. {17, 4, 0x2e, 0x2a, 0xfe, 0xa4, 0xaa, 0xff, 0xaa, 0x2a, 0xff, 0x2e, 0x36, 0xfe};
  76. static const uint8_t pic_time[] = {15, 4, 0xa8, 0x8b, 0x2d, 0xe9, 0xad, 0xca, 0xad, 0x8b};
  77. static const uint8_t pic_puzzled[] = {
  78. 0xff, 0xcf, 0x00, 0xf3, 0xff, 0xfc, 0x3f, 0x03, 0xc0, 0xff, 0xf3, 0x0f, 0xdc, 0xff, 0xcf,
  79. 0x00, 0xf3, 0xff, 0xfc, 0x3f, 0x03, 0xc0, 0xff, 0xf3, 0x0f, 0xdc, 0x03, 0xcc, 0x00, 0x03,
  80. 0x38, 0x00, 0x0e, 0x03, 0xc0, 0x00, 0x30, 0x30, 0xdc, 0x03, 0xcc, 0x00, 0x03, 0x1c, 0x00,
  81. 0x07, 0x03, 0xc0, 0x00, 0x30, 0x30, 0xdc, 0xff, 0xcf, 0x00, 0x03, 0x0e, 0x80, 0x03, 0x03,
  82. 0xc0, 0xff, 0x33, 0xc0, 0xdc, 0xff, 0xcf, 0x00, 0x03, 0x07, 0xc0, 0x01, 0x03, 0xc0, 0xff,
  83. 0x33, 0xc0, 0xdc, 0x03, 0xc0, 0x00, 0x83, 0x03, 0xe0, 0x00, 0x03, 0xc0, 0x00, 0x30, 0xc0,
  84. 0xd0, 0x03, 0xc0, 0x00, 0xc3, 0x01, 0x70, 0x00, 0x03, 0xc0, 0x00, 0x30, 0xc0, 0xd0, 0x03,
  85. 0xc0, 0xff, 0xf3, 0xff, 0xfc, 0x3f, 0xff, 0xcf, 0xff, 0xf3, 0xff, 0xdc, 0x03, 0xc0, 0xff,
  86. 0xf3, 0xff, 0xfc, 0x3f, 0xff, 0xcf, 0xff, 0xf3, 0xff, 0xdc};
  87. static void key_stack_init() {
  88. key_stack_head = 0;
  89. }
  90. static uint8_t key_stack_pop() {
  91. return keys[--key_stack_head];
  92. }
  93. static bool key_stack_is_empty() {
  94. return key_stack_head == 0;
  95. }
  96. static int key_stack_push(uint8_t value) {
  97. if(key_stack_head != KEY_STACK_SIZE) {
  98. keys[key_stack_head] = value;
  99. key_stack_head++;
  100. return key_stack_head;
  101. } else
  102. return -1;
  103. }
  104. static bool storage_game_state_load() {
  105. Storage* storage = furi_record_open(RECORD_STORAGE);
  106. storage_common_migrate(storage, EXT_PATH("apps/Games/game15.save"), SAVING_FILENAME);
  107. File* file = storage_file_alloc(storage);
  108. uint16_t bytes_readed = 0;
  109. if(storage_file_open(file, SAVING_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING))
  110. bytes_readed = storage_file_read(file, &game_state, sizeof(game_state_t));
  111. storage_file_close(file);
  112. storage_file_free(file);
  113. furi_record_close(RECORD_STORAGE);
  114. return bytes_readed == sizeof(game_state_t);
  115. }
  116. static void storage_game_state_save() {
  117. Storage* storage = furi_record_open(RECORD_STORAGE);
  118. File* file = storage_file_alloc(storage);
  119. if(storage_file_open(file, SAVING_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  120. storage_file_write(file, &game_state, sizeof(game_state_t));
  121. }
  122. storage_file_close(file);
  123. storage_file_free(file);
  124. furi_record_close(RECORD_STORAGE);
  125. }
  126. static void set_moving_cell_by_direction(direction_e direction) {
  127. moving_cell.move_direction = DirectionNone;
  128. moving_cell.zero_index = 0xff;
  129. for(int i = 0; i < 16; i++) {
  130. if(!game_state.board[i]) {
  131. moving_cell.zero_index = i;
  132. break;
  133. }
  134. }
  135. if(moving_cell.zero_index == 0xff) return;
  136. uint8_t x = moving_cell.zero_index % 4;
  137. uint8_t y = moving_cell.zero_index / 4;
  138. moving_cell.cell_index = moving_cell.zero_index;
  139. if(direction == DirectionUp && y < 3)
  140. moving_cell.cell_index += 4;
  141. else if(direction == DirectionDown && y > 0)
  142. moving_cell.cell_index -= 4;
  143. else if(direction == DirectionLeft && x < 3)
  144. moving_cell.cell_index++;
  145. else if(direction == DirectionRight && x > 0)
  146. moving_cell.cell_index--;
  147. else
  148. return;
  149. moving_cell.move_ticks = 0;
  150. moving_cell.move_direction = direction;
  151. }
  152. static bool is_board_has_solution() {
  153. uint8_t i, j, inv = 0;
  154. for(i = 0; i < 16; ++i)
  155. if(game_state.board[i])
  156. for(j = 0; j < i; ++j)
  157. if(game_state.board[j] > game_state.board[i]) ++inv;
  158. for(i = 0; i < 16; ++i)
  159. if(game_state.board[i] == 0) inv += 1 + i / 4;
  160. return inv % 2 == 0;
  161. }
  162. static void board_init() {
  163. for(int i = 0; i < 16; i++) {
  164. game_state.board[i] = (i + 1) % 16;
  165. }
  166. do {
  167. for(int i = 15; i >= 1; i--) {
  168. int j = rand() % (i + 1);
  169. uint8_t tmp = game_state.board[j];
  170. game_state.board[j] = game_state.board[i];
  171. game_state.board[i] = tmp;
  172. }
  173. } while(!is_board_has_solution());
  174. }
  175. static void game_init() {
  176. game_state.scene = ScenePlay;
  177. game_state.move_count = 0;
  178. game_state.tick_count = 0;
  179. moving_cell.move_direction = DirectionNone;
  180. board_init();
  181. key_stack_init();
  182. popup_menu_selected_item = 0;
  183. }
  184. static bool is_board_solved() {
  185. for(int i = 0; i < 16; i++)
  186. if(((i + 1) % 16) != game_state.board[i]) return false;
  187. return true;
  188. }
  189. static void game_tick() {
  190. switch(game_state.scene) {
  191. case ScenePlay:
  192. if(game_state.move_count >= 1) game_state.tick_count++;
  193. if(loaded_saving_ticks) loaded_saving_ticks--;
  194. if(moving_cell.move_direction == DirectionNone && !key_stack_is_empty()) {
  195. set_moving_cell_by_direction(key_stack_pop());
  196. if(moving_cell.move_direction == DirectionNone) {
  197. notification_message(notification, &sequence_single_vibro);
  198. key_stack_init();
  199. }
  200. }
  201. if(moving_cell.move_direction != DirectionNone) {
  202. moving_cell.move_ticks++;
  203. if(moving_cell.move_ticks == MOVE_TICKS) {
  204. game_state.board[moving_cell.zero_index] =
  205. game_state.board[moving_cell.cell_index];
  206. game_state.board[moving_cell.cell_index] = 0;
  207. moving_cell.move_direction = DirectionNone;
  208. game_state.move_count++;
  209. }
  210. if(is_board_solved()) {
  211. notification_message(notification, &sequence_double_vibro);
  212. if(game_state.move_count < game_state.top_record || game_state.top_record == 0) {
  213. game_state.top_record = game_state.move_count;
  214. storage_game_state_save();
  215. }
  216. game_state.scene = SceneWin;
  217. }
  218. }
  219. break;
  220. case SceneWin:
  221. if(!key_stack_is_empty()) game_init();
  222. break;
  223. case ScenePopup:
  224. if(!key_stack_is_empty()) {
  225. switch(key_stack_pop()) {
  226. case DirectionDown:
  227. popup_menu_selected_item++;
  228. popup_menu_selected_item = popup_menu_selected_item % POPUP_MENU_ITEMS;
  229. break;
  230. case DirectionUp:
  231. popup_menu_selected_item--;
  232. popup_menu_selected_item = popup_menu_selected_item % POPUP_MENU_ITEMS;
  233. break;
  234. case DirectionNone:
  235. if(popup_menu_selected_item == 0) {
  236. game_state.scene = ScenePlay;
  237. notification_message(notification, &sequence_single_vibro);
  238. } else if(popup_menu_selected_item == 1) {
  239. notification_message(notification, &sequence_single_vibro);
  240. game_init();
  241. }
  242. break;
  243. }
  244. }
  245. break;
  246. }
  247. }
  248. static void draw_cell(Canvas* canvas, uint8_t x, uint8_t y, uint8_t cell_number) {
  249. canvas_set_color(canvas, ColorBlack);
  250. canvas_draw_rframe(canvas, x, y, 18, 14, 1);
  251. canvas_set_color(canvas, ColorBlack);
  252. canvas_draw_xbm(canvas, x + 4, y + 3, CELL_WIDTH, CELL_HEIGHT, pic_cells + cell_number * 16);
  253. }
  254. static void board_draw(Canvas* canvas) {
  255. for(int i = 0; i < 16; i++) {
  256. if(game_state.board[i]) {
  257. if(moving_cell.move_direction == DirectionNone || moving_cell.cell_index != i)
  258. draw_cell(canvas, (i % 4) * 20 + 7, (i / 4) * 16 + 1, game_state.board[i]);
  259. if(moving_cell.move_direction != DirectionNone && moving_cell.cell_index == i) {
  260. uint8_t from_x = (moving_cell.cell_index % 4) * 20 + 7;
  261. uint8_t from_y = (moving_cell.cell_index / 4) * 16 + 1;
  262. uint8_t to_x = (moving_cell.zero_index % 4) * 20 + 7;
  263. uint8_t to_y = (moving_cell.zero_index / 4) * 16 + 1;
  264. int now_x = from_x + (to_x - from_x) * moving_cell.move_ticks / MOVE_TICKS;
  265. int now_y = from_y + (to_y - from_y) * moving_cell.move_ticks / MOVE_TICKS;
  266. draw_cell(canvas, now_x, now_y, game_state.board[i]);
  267. }
  268. }
  269. }
  270. }
  271. static void number_draw(Canvas* canvas, uint8_t y, uint32_t value) {
  272. uint8_t x = 121;
  273. while(true) {
  274. uint8_t digit = value % 10;
  275. canvas_draw_xbm(canvas, x, y, 4, 6, pic_digits + digit * 6);
  276. x -= 5;
  277. value = value / 10;
  278. if(!value) break;
  279. }
  280. }
  281. static void plate_draw(
  282. Canvas* canvas,
  283. uint8_t y,
  284. const uint8_t* header,
  285. uint32_t value,
  286. bool dont_draw_zero_value) {
  287. canvas_set_color(canvas, ColorBlack);
  288. canvas_draw_rbox(canvas, 92, y, 35, 19, 2);
  289. canvas_set_color(canvas, ColorBlack);
  290. canvas_draw_xbm(canvas, 95, y + 3, header[0], header[1], &header[2]);
  291. if((!value && !dont_draw_zero_value) || value) number_draw(canvas, y + 10, value);
  292. }
  293. static void info_draw(Canvas* canvas) {
  294. plate_draw(canvas, 1, pic_top, game_state.top_record, true);
  295. plate_draw(canvas, 22, pic_move, game_state.move_count, false);
  296. plate_draw(canvas, 43, pic_time, game_state.tick_count / FPS, false);
  297. }
  298. static void gray_screen(Canvas* const canvas) {
  299. canvas_set_color(canvas, ColorWhite);
  300. for(int x = 0; x < 128; x += 2) {
  301. for(int y = 0; y < 64; y++) {
  302. canvas_draw_dot(canvas, x + (y % 2 == 1 ? 0 : 1), y);
  303. }
  304. }
  305. }
  306. static void render_callback(Canvas* const canvas) {
  307. canvas_set_color(canvas, ColorWhite);
  308. canvas_draw_box(canvas, 0, 0, 128, 64);
  309. if(game_state.scene == ScenePlay || game_state.scene == SceneWin ||
  310. game_state.scene == ScenePopup) {
  311. canvas_set_color(canvas, ColorBlack);
  312. board_draw(canvas);
  313. info_draw(canvas);
  314. if(loaded_saving_ticks && game_state.scene != ScenePopup) {
  315. canvas_set_color(canvas, ColorWhite);
  316. canvas_draw_rbox(canvas, 20, 24, 88, 16, 4);
  317. canvas_set_color(canvas, ColorBlack);
  318. canvas_draw_rframe(canvas, 20, 24, 88, 16, 4);
  319. canvas_draw_str_aligned(
  320. canvas, 64, 32, AlignCenter, AlignCenter, "Restoring game ...");
  321. }
  322. }
  323. if(game_state.scene == SceneWin) {
  324. gray_screen(canvas);
  325. canvas_draw_box(canvas, 7, 20, 114, 24);
  326. canvas_set_color(canvas, ColorBlack);
  327. canvas_draw_box(canvas, 8, 21, 112, 22);
  328. canvas_set_color(canvas, ColorWhite);
  329. canvas_draw_box(canvas, 10, 23, 108, 18);
  330. canvas_set_color(canvas, ColorBlack);
  331. canvas_draw_xbm(canvas, 14, 27, 100, 10, pic_puzzled);
  332. } else if(game_state.scene == ScenePopup) {
  333. gray_screen(canvas);
  334. canvas_set_color(canvas, ColorWhite);
  335. canvas_draw_rbox(canvas, 28, 16, 72, 32, 4);
  336. canvas_set_color(canvas, ColorBlack);
  337. canvas_draw_rframe(canvas, 28, 16, 72, 32, 4);
  338. for(int i = 0; i < POPUP_MENU_ITEMS; i++) {
  339. if(i == popup_menu_selected_item) {
  340. canvas_set_color(canvas, ColorBlack);
  341. canvas_draw_box(canvas, 34, 20 + 12 * i, 60, 12);
  342. }
  343. canvas_set_color(canvas, i == popup_menu_selected_item ? ColorWhite : ColorBlack);
  344. canvas_draw_str_aligned(
  345. canvas, 64, 26 + 12 * i, AlignCenter, AlignCenter, popup_menu_strings[i]);
  346. }
  347. }
  348. }
  349. static void game_event_handler(GameEvent const event) {
  350. if(event.type == EventTypeKey) {
  351. if(event.input.type == InputTypePress) {
  352. switch(event.input.key) {
  353. case InputKeyUp:
  354. key_stack_push(DirectionUp);
  355. break;
  356. case InputKeyDown:
  357. key_stack_push(DirectionDown);
  358. break;
  359. case InputKeyRight:
  360. key_stack_push(DirectionRight);
  361. break;
  362. case InputKeyLeft:
  363. key_stack_push(DirectionLeft);
  364. break;
  365. case InputKeyOk:
  366. if(game_state.scene == ScenePlay) {
  367. game_state.scene = ScenePopup;
  368. key_stack_init();
  369. } else
  370. key_stack_push(DirectionNone);
  371. break;
  372. case InputKeyBack:
  373. if(game_state.scene == ScenePopup) {
  374. game_state.scene = ScenePlay;
  375. } else {
  376. storage_game_state_save();
  377. sandbox_loop_exit();
  378. }
  379. break;
  380. default:
  381. break;
  382. }
  383. }
  384. } else if(event.type == EventTypeTick) {
  385. game_tick();
  386. }
  387. }
  388. static void game_alloc() {
  389. key_stack_init();
  390. notification = furi_record_open(RECORD_NOTIFICATION);
  391. notification_message_block(notification, &sequence_display_backlight_enforce_on);
  392. }
  393. static void game_free() {
  394. notification_message_block(notification, &sequence_display_backlight_enforce_auto);
  395. furi_record_close(RECORD_NOTIFICATION);
  396. }
  397. int32_t game15_app() {
  398. game_alloc();
  399. game_init();
  400. loaded_saving_ticks = 0;
  401. if(storage_game_state_load()) {
  402. if(game_state.scene != ScenePlay)
  403. game_init();
  404. else
  405. loaded_saving_ticks = FPS;
  406. } else
  407. game_init();
  408. sandbox_init(
  409. FPS, (SandboxRenderCallback)render_callback, (SandboxEventHandler)game_event_handler);
  410. // Call dolphin deed on game start
  411. dolphin_deed(DolphinDeedPluginGameStart);
  412. sandbox_loop();
  413. sandbox_free();
  414. game_free();
  415. return 0;
  416. }