solitaire.c 21 KB

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