solitaire.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #include <stdlib.h>
  2. #include <dolphin/dolphin.h>
  3. #include <furi.h>
  4. #include <gui/canvas_i.h>
  5. #include "defines.h"
  6. #include "common/ui.h"
  7. #include "solitaire_icons.h"
  8. #include <notification/notification.h>
  9. #include <notification/notification_messages.h>
  10. void init(GameState *game_state);
  11. const NotificationSequence sequence_fail = {
  12. &message_vibro_on,
  13. &message_note_c4,
  14. &message_delay_10,
  15. &message_vibro_off,
  16. &message_sound_off,
  17. &message_delay_10,
  18. &message_vibro_on,
  19. &message_note_a3,
  20. &message_delay_10,
  21. &message_vibro_off,
  22. &message_sound_off,
  23. NULL,
  24. };
  25. int8_t columns[7][3] = {
  26. {1, 1, 25},
  27. {19, 1, 25},
  28. {37, 1, 25},
  29. {55, 1, 25},
  30. {73, 1, 25},
  31. {91, 1, 25},
  32. {109, 1, 25},
  33. };
  34. bool can_place_card(Card where, Card what) {
  35. bool a_black = where.pip == 0 || where.pip == 3;
  36. bool b_black = what.pip == 0 || what.pip == 3;
  37. if (a_black == b_black) return false;
  38. int8_t a_letter = (int8_t) where.character;
  39. int8_t b_letter = (int8_t) what.character;
  40. if (a_letter == 12) a_letter = -1;
  41. if (b_letter == 12) b_letter = -1;
  42. return (a_letter - 1) == b_letter;
  43. }
  44. static void draw_scene(Canvas *const canvas, const GameState *game_state) {
  45. int deckIndex = game_state->deck.index;
  46. if (game_state->dragging_deck)
  47. deckIndex--;
  48. if ((game_state->deck.index < (game_state->deck.card_count - 1) || game_state->deck.index == -1) && game_state->deck.card_count>0) {
  49. draw_card_back_at(columns[0][0], columns[0][1], canvas);
  50. if (game_state->selectRow == 0 && game_state->selectColumn == 0) {
  51. draw_rounded_box(canvas, columns[0][0] + 1, columns[0][1] + 1, CARD_WIDTH - 2, CARD_HEIGHT - 2,
  52. Inverse);
  53. }
  54. } else
  55. draw_card_space(columns[0][0], columns[0][1],
  56. game_state->selectRow == 0 && game_state->selectColumn == 0,
  57. canvas);
  58. //deck side
  59. if (deckIndex >= 0) {
  60. Card c = game_state->deck.cards[deckIndex];
  61. draw_card_at_colored(columns[1][0], columns[1][1], c.pip, c.character,
  62. game_state->selectRow == 0 && game_state->selectColumn == 1, canvas);
  63. } else
  64. draw_card_space(columns[1][0], columns[1][1],
  65. game_state->selectRow == 0 && game_state->selectColumn == 1,
  66. canvas);
  67. for (uint8_t i = 0; i < 4; i++) {
  68. Card current = game_state->top_cards[i];
  69. bool selected = game_state->selectRow == 0 && game_state->selectColumn == (i + 3);
  70. if (current.disabled) {
  71. draw_card_space(columns[i + 3][0], columns[i + 3][1], selected, canvas);
  72. } else {
  73. draw_card_at(columns[i + 3][0], columns[i + 3][1], current.pip, current.character, canvas);
  74. if (selected) {
  75. draw_rounded_box(canvas, columns[i + 3][0], columns[i + 3][1], CARD_WIDTH, CARD_HEIGHT,
  76. Inverse);
  77. }
  78. }
  79. }
  80. for (uint8_t i = 0; i < 7; i++) {
  81. bool selected = game_state->selectRow == 1 && game_state->selectColumn == i;
  82. int8_t index= (game_state->bottom_columns[i].index - 1 - game_state->selected_card);
  83. if(index<0)index=0;
  84. draw_hand_column(game_state->bottom_columns[i], columns[i][0], columns[i][2],
  85. selected ? index : -1, canvas);
  86. }
  87. int8_t pos[2] = {columns[game_state->selectColumn][0],
  88. columns[game_state->selectColumn][game_state->selectRow + 1]};
  89. /* draw_icon_clip(canvas, &I_card_graphics, pos[0] + CARD_HALF_WIDTH, pos[1] + CARD_HALF_HEIGHT, 30, 5, 5, 5,
  90. Filled);*/
  91. if (game_state->dragging_hand.index > 0) {
  92. draw_hand_column(game_state->dragging_hand,
  93. pos[0] + CARD_HALF_WIDTH + 3, pos[1] + CARD_HALF_HEIGHT + 3,
  94. -1, canvas);
  95. }
  96. }
  97. static void draw_animation(Canvas *const canvas, const GameState *game_state) {
  98. if (!game_state->animation.started) {
  99. draw_scene(canvas, game_state);
  100. } else {
  101. clone_buffer(game_state->animation.buffer, get_buffer(canvas));
  102. draw_card_at((int8_t) game_state->animation.x, (int8_t) game_state->animation.y, game_state->animation.card.pip,
  103. game_state->animation.card.character, canvas);
  104. }
  105. clone_buffer(get_buffer(canvas), game_state->animation.buffer);
  106. }
  107. static void render_callback(Canvas *const canvas, void *ctx) {
  108. const GameState *game_state = acquire_mutex((ValueMutex *) ctx, 25);
  109. if (game_state == NULL) {
  110. return;
  111. }
  112. switch (game_state->state) {
  113. case GameStateAnimate:
  114. draw_animation(canvas, game_state);
  115. break;
  116. case GameStateStart:
  117. canvas_draw_icon(canvas, 0, 0, &I_solitaire_main);
  118. break;
  119. case GameStatePlay:
  120. draw_scene(canvas, game_state);
  121. break;
  122. default:
  123. break;
  124. }
  125. release_mutex((ValueMutex *) ctx, game_state);
  126. }
  127. void remove_drag(GameState *game_state) {
  128. if (game_state->dragging_deck) {
  129. remove_from_deck(game_state->deck.index, &(game_state->deck));
  130. game_state->dragging_deck = false;
  131. } else if (game_state->dragging_column < 7) {
  132. game_state->dragging_column = 8;
  133. }
  134. game_state->dragging_hand.index = 0;
  135. }
  136. bool handleInput(GameState *game_state) {
  137. Hand currentHand = game_state->bottom_columns[game_state->selectColumn];
  138. switch (game_state->input) {
  139. case InputKeyUp:
  140. if (game_state->selectRow > 0) {
  141. int first = first_non_flipped_card(currentHand);
  142. first = currentHand.index - first;
  143. if ((first - 1) > game_state->selected_card && game_state->dragging_hand.index == 0 &&
  144. !game_state->longPress) {
  145. game_state->selected_card++;
  146. } else {
  147. game_state->selectRow--;
  148. game_state->selected_card = 0;
  149. }
  150. }
  151. break;
  152. case InputKeyDown:
  153. if (game_state->selectRow < 1) {
  154. game_state->selectRow++;
  155. game_state->selected_card = 0;
  156. } else {
  157. if (game_state->selected_card > 0) {
  158. if (game_state->longPress)
  159. game_state->selected_card = 0;
  160. else
  161. game_state->selected_card--;
  162. }
  163. }
  164. break;
  165. case InputKeyRight:
  166. if (game_state->selectColumn < 6) {
  167. game_state->selectColumn++;
  168. game_state->selected_card = 0;
  169. }
  170. break;
  171. case InputKeyLeft:
  172. if (game_state->selectColumn > 0) {
  173. game_state->selectColumn--;
  174. game_state->selected_card = 0;
  175. }
  176. break;
  177. case InputKeyOk:
  178. return true;
  179. break;
  180. default:
  181. break;
  182. }
  183. if (game_state->selectRow == 0 && game_state->selectColumn == 2) {
  184. if (game_state->input == InputKeyRight)
  185. game_state->selectColumn++;
  186. else
  187. game_state->selectColumn--;
  188. }
  189. if (game_state->dragging_hand.index > 0)
  190. game_state->selected_card = 0;
  191. return false;
  192. }
  193. bool place_on_top(Card *where, Card what) {
  194. if (where->disabled && what.character == 12) {
  195. where->disabled = what.disabled;
  196. where->pip = what.pip;
  197. where->character = what.character;
  198. return true;
  199. } else if (where->pip == what.pip) {
  200. int8_t a_letter = (int8_t) where->character;
  201. int8_t b_letter = (int8_t) what.character;
  202. if (a_letter == 12) a_letter = -1;
  203. if (b_letter == 12) b_letter = -1;
  204. if(where->disabled && b_letter!=-1)
  205. return false;
  206. if ((a_letter + 1) == b_letter) {
  207. where->disabled = what.disabled;
  208. where->pip = what.pip;
  209. where->character = what.character;
  210. return true;
  211. }
  212. }
  213. return false;
  214. }
  215. void tick(GameState *game_state, NotificationApp *notification) {
  216. game_state->last_tick = furi_get_tick();
  217. uint8_t row = game_state->selectRow;
  218. uint8_t column = game_state->selectColumn;
  219. if (game_state->state != GameStatePlay && game_state->state != GameStateAnimate) return;
  220. bool wasAction = false;
  221. if (game_state->state == GameStatePlay) {
  222. if (game_state->top_cards[0].character == 11 && game_state->top_cards[1].character == 11 &&
  223. game_state->top_cards[2].character == 11 && game_state->top_cards[3].character == 11) {
  224. game_state->state = GameStateAnimate;
  225. return;
  226. }
  227. }
  228. if (handleInput(game_state)) {
  229. if (game_state->state == GameStatePlay) {
  230. if (game_state->longPress && game_state->dragging_hand.index == 1) {
  231. for (uint8_t i = 0; i < 4; i++) {
  232. if (place_on_top(&(game_state->top_cards[i]), game_state->dragging_hand.cards[0])) {
  233. remove_drag(game_state);
  234. wasAction = true;
  235. break;
  236. }
  237. }
  238. } else {
  239. if (row == 0 && column == 0 && game_state->dragging_hand.index == 0) {
  240. FURI_LOG_D(APP_NAME, "Drawing card");
  241. game_state->deck.index++;
  242. wasAction = true;
  243. if (game_state->deck.index >= (game_state->deck.card_count))
  244. game_state->deck.index = -1;
  245. }
  246. //pick/place from deck
  247. else if (row == 0 && column == 1) {
  248. //place
  249. if (game_state->dragging_deck) {
  250. wasAction = true;
  251. game_state->dragging_deck = false;
  252. game_state->dragging_hand.index = 0;
  253. }
  254. //pick
  255. else {
  256. if (game_state->dragging_hand.index == 0 && game_state->deck.index >= 0) {
  257. wasAction = true;
  258. game_state->dragging_deck = true;
  259. add_to_hand(&(game_state->dragging_hand), game_state->deck.cards[game_state->deck.index]);
  260. }
  261. }
  262. }
  263. //place on top row
  264. else if (row == 0 && game_state->dragging_hand.index == 1) {
  265. column -= 3;
  266. Card currCard = game_state->dragging_hand.cards[0];
  267. wasAction = place_on_top(&(game_state->top_cards[column]), currCard);
  268. if (wasAction)
  269. remove_drag(game_state);
  270. }
  271. //pick/place from bottom
  272. else if (row == 1) {
  273. Hand *curr_hand = &(game_state->bottom_columns[column]);
  274. //pick up
  275. if (game_state->dragging_hand.index == 0) {
  276. Card curr_card = curr_hand->cards[curr_hand->index - 1];
  277. if (curr_card.flipped) {
  278. curr_hand->cards[curr_hand->index - 1].flipped = false;
  279. wasAction = true;
  280. } else {
  281. if (curr_hand->index > 0) {
  282. extract_hand_region(curr_hand, &(game_state->dragging_hand),
  283. curr_hand->index - game_state->selected_card - 1);
  284. game_state->selected_card = 0;
  285. game_state->dragging_column = column;
  286. wasAction = true;
  287. }
  288. }
  289. }
  290. //place
  291. else {
  292. Card first = game_state->dragging_hand.cards[0];
  293. if (game_state->dragging_column == column ||
  294. (curr_hand->index == 0 && first.character == 11) ||
  295. can_place_card(curr_hand->cards[curr_hand->index - 1], first)
  296. ) {
  297. add_hand_region(curr_hand, &(game_state->dragging_hand));
  298. remove_drag(game_state);
  299. wasAction = true;
  300. }
  301. }
  302. }
  303. }
  304. if (!wasAction) {
  305. notification_message(notification, &sequence_fail);
  306. }
  307. }
  308. }
  309. if (game_state->state == GameStateAnimate) {
  310. if (game_state->animation.started && !game_state->longPress && game_state->input==InputKeyOk) {
  311. init(game_state);
  312. game_state->state = GameStateStart;
  313. }
  314. game_state->animation.started = true;
  315. if (game_state->animation.x < -20 || game_state->animation.x > 128) {
  316. game_state->animation.deck++;
  317. if (game_state->animation.deck > 3)
  318. game_state->animation.deck = 0;
  319. int8_t cardIndex = 11 - game_state->animation.indexes[game_state->animation.deck];
  320. if (game_state->animation.indexes[0] == 13 &&
  321. game_state->animation.indexes[1] == 13 &&
  322. game_state->animation.indexes[2] == 13 &&
  323. game_state->animation.indexes[3] == 13) {
  324. init(game_state);
  325. game_state->state = GameStateStart;
  326. return;
  327. }
  328. if (cardIndex == -1)
  329. cardIndex = 12;
  330. game_state->animation.card = (Card) {
  331. game_state->top_cards[game_state->animation.deck].pip,
  332. cardIndex,
  333. false, false
  334. };
  335. game_state->animation.indexes[game_state->animation.deck]++;
  336. game_state->animation.vx = -(rand() % 3 + 1) * (rand() % 2 == 1 ? 1 : -1);
  337. game_state->animation.vy = (rand() % 3 + 1);
  338. game_state->animation.x = columns[game_state->animation.deck + 3][0];
  339. game_state->animation.y = columns[game_state->animation.deck + 3][1];
  340. }
  341. game_state->animation.x += game_state->animation.vx;
  342. game_state->animation.y -= game_state->animation.vy;
  343. game_state->animation.vy -= 1;
  344. if (game_state->animation.vy < -10)game_state->animation.vy = -10;
  345. if (game_state->animation.y > 41) {
  346. game_state->animation.y = 41;
  347. game_state->animation.vy = -(game_state->animation.vy * 0.7f);
  348. }
  349. }
  350. }
  351. void init(GameState *game_state) {
  352. game_state->selectColumn = 0;
  353. game_state->selected_card = 0;
  354. game_state->selectRow = 0;
  355. generate_deck(&(game_state->deck), 1);
  356. shuffle_deck(&(game_state->deck));
  357. game_state->dragging_deck = false;
  358. game_state->animation.started = false;
  359. game_state->animation.deck = -1;
  360. game_state->animation.x = -21;
  361. game_state->state = GameStatePlay;
  362. game_state->dragging_column = 8;
  363. for (uint8_t i = 0; i < 7; i++) {
  364. free_hand(&(game_state->bottom_columns[i]));
  365. init_hand(&(game_state->bottom_columns[i]), 21);
  366. game_state->bottom_columns[i].index = 0;
  367. for (uint8_t j = 0; j <= i; j++) {
  368. Card cur = remove_from_deck(0, &(game_state->deck));
  369. cur.flipped = i != j;
  370. add_to_hand(&(game_state->bottom_columns[i]), cur);
  371. }
  372. }
  373. for (uint8_t i = 0; i < 4; i++) {
  374. game_state->animation.indexes[i] = 0;
  375. game_state->top_cards[i] = (Card) {0, 0, true, false};
  376. }
  377. game_state->deck.index = -1;
  378. }
  379. void init_start(GameState *game_state) {
  380. game_state->input = InputKeyMAX;
  381. for (uint8_t i = 0; i < 7; i++)
  382. init_hand(&(game_state->bottom_columns[i]), 21);
  383. init_hand(&(game_state->dragging_hand), 13);
  384. game_state->animation.buffer = make_buffer();
  385. }
  386. static void input_callback(InputEvent *input_event, FuriMessageQueue *event_queue) {
  387. furi_assert(event_queue);
  388. AppEvent event = {.type = EventTypeKey, .input = *input_event};
  389. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  390. }
  391. static void update_timer_callback(FuriMessageQueue *event_queue) {
  392. furi_assert(event_queue);
  393. AppEvent event = {.type = EventTypeTick};
  394. furi_message_queue_put(event_queue, &event, 0);
  395. }
  396. int32_t solitaire_app(void *p) {
  397. UNUSED(p);
  398. int32_t return_code = 0;
  399. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(AppEvent));
  400. GameState *game_state = malloc(sizeof(GameState));
  401. init_start(game_state);
  402. set_card_graphics(&I_card_graphics);
  403. game_state->state = GameStateStart;
  404. game_state->processing = true;
  405. ValueMutex state_mutex;
  406. if (!init_mutex(&state_mutex, game_state, sizeof(GameState))) {
  407. FURI_LOG_E(APP_NAME, "cannot create mutex\r\n");
  408. return_code = 255;
  409. goto free_and_exit;
  410. }
  411. NotificationApp *notification = furi_record_open(RECORD_NOTIFICATION);
  412. notification_message_block(notification, &sequence_display_backlight_enforce_on);
  413. ViewPort *view_port = view_port_alloc();
  414. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  415. view_port_input_callback_set(view_port, input_callback, event_queue);
  416. FuriTimer *timer =
  417. furi_timer_alloc(update_timer_callback, FuriTimerTypePeriodic, event_queue);
  418. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 30);
  419. Gui *gui = furi_record_open("gui");
  420. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  421. AppEvent event;
  422. for (bool processing = true; processing;) {
  423. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 150);
  424. GameState *localstate = (GameState *) acquire_mutex_block(&state_mutex);
  425. bool hadChange = false;
  426. if (event_status == FuriStatusOk) {
  427. if (event.type == EventTypeKey) {
  428. if (event.input.type == InputTypeLong) {
  429. game_state->longPress = true;
  430. switch (event.input.key) {
  431. case InputKeyUp:
  432. case InputKeyDown:
  433. case InputKeyRight:
  434. case InputKeyLeft:
  435. case InputKeyOk:
  436. localstate->input = event.input.key;
  437. break;
  438. case InputKeyBack:
  439. processing = false;
  440. return_code = 1;
  441. default:
  442. break;
  443. }
  444. } else if (event.input.type == InputTypePress) {
  445. game_state->longPress = false;
  446. switch (event.input.key) {
  447. case InputKeyUp:
  448. case InputKeyDown:
  449. case InputKeyRight:
  450. case InputKeyLeft:
  451. case InputKeyOk:
  452. if (event.input.key == InputKeyOk && localstate->state == GameStateStart) {
  453. localstate->state = GameStatePlay;
  454. init(game_state);
  455. }
  456. else {
  457. hadChange = true;
  458. localstate->input = event.input.key;
  459. }
  460. break;
  461. case InputKeyBack:
  462. init(game_state);
  463. processing = false;
  464. return_code = 1;
  465. break;
  466. default:
  467. break;
  468. }
  469. }
  470. } else if (event.type == EventTypeTick) {
  471. tick(localstate, notification);
  472. processing = localstate->processing;
  473. localstate->input = InputKeyMAX;
  474. }
  475. } else {
  476. FURI_LOG_W(APP_NAME, "osMessageQueue: event timeout");
  477. // event timeout
  478. }
  479. if (hadChange || game_state->state == GameStateAnimate)
  480. view_port_update(view_port);
  481. release_mutex(&state_mutex, localstate);
  482. }
  483. notification_message_block(notification, &sequence_display_backlight_enforce_auto);
  484. furi_timer_free(timer);
  485. view_port_enabled_set(view_port, false);
  486. gui_remove_view_port(gui, view_port);
  487. furi_record_close(RECORD_GUI);
  488. furi_record_close(RECORD_NOTIFICATION);
  489. view_port_free(view_port);
  490. delete_mutex(&state_mutex);
  491. free_and_exit:
  492. free(game_state->animation.buffer);
  493. ui_cleanup();
  494. for (uint8_t i = 0; i < 7; i++)
  495. free_hand(&(game_state->bottom_columns[i]));
  496. free(game_state->deck.cards);
  497. free(game_state);
  498. furi_message_queue_free(event_queue);
  499. return return_code;
  500. }