game15.c 17 KB

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