solitaire.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. DOLPHIN_DEED(DolphinDeedPluginGameWin);
  227. return;
  228. }
  229. }
  230. if (handleInput(game_state)) {
  231. if (game_state->state == GameStatePlay) {
  232. if (game_state->longPress && game_state->dragging_hand.index == 1) {
  233. for (uint8_t i = 0; i < 4; i++) {
  234. if (place_on_top(&(game_state->top_cards[i]), game_state->dragging_hand.cards[0])) {
  235. remove_drag(game_state);
  236. wasAction = true;
  237. break;
  238. }
  239. }
  240. } else {
  241. if (row == 0 && column == 0 && game_state->dragging_hand.index == 0) {
  242. FURI_LOG_D(APP_NAME, "Drawing card");
  243. game_state->deck.index++;
  244. wasAction = true;
  245. if (game_state->deck.index >= (game_state->deck.card_count))
  246. game_state->deck.index = -1;
  247. }
  248. //pick/place from deck
  249. else if (row == 0 && column == 1) {
  250. //place
  251. if (game_state->dragging_deck) {
  252. wasAction = true;
  253. game_state->dragging_deck = false;
  254. game_state->dragging_hand.index = 0;
  255. }
  256. //pick
  257. else {
  258. if (game_state->dragging_hand.index == 0 && game_state->deck.index >= 0) {
  259. wasAction = true;
  260. game_state->dragging_deck = true;
  261. add_to_hand(&(game_state->dragging_hand), game_state->deck.cards[game_state->deck.index]);
  262. }
  263. }
  264. }
  265. //place on top row
  266. else if (row == 0 && game_state->dragging_hand.index == 1) {
  267. column -= 3;
  268. Card currCard = game_state->dragging_hand.cards[0];
  269. wasAction = place_on_top(&(game_state->top_cards[column]), currCard);
  270. if (wasAction)
  271. remove_drag(game_state);
  272. }
  273. //pick/place from bottom
  274. else if (row == 1) {
  275. Hand *curr_hand = &(game_state->bottom_columns[column]);
  276. //pick up
  277. if (game_state->dragging_hand.index == 0) {
  278. Card curr_card = curr_hand->cards[curr_hand->index - 1];
  279. if (curr_card.flipped) {
  280. curr_hand->cards[curr_hand->index - 1].flipped = false;
  281. wasAction = true;
  282. } else {
  283. if (curr_hand->index > 0) {
  284. extract_hand_region(curr_hand, &(game_state->dragging_hand),
  285. curr_hand->index - game_state->selected_card - 1);
  286. game_state->selected_card = 0;
  287. game_state->dragging_column = column;
  288. wasAction = true;
  289. }
  290. }
  291. }
  292. //place
  293. else {
  294. Card first = game_state->dragging_hand.cards[0];
  295. if (game_state->dragging_column == column ||
  296. (curr_hand->index == 0 && first.character == 11) ||
  297. can_place_card(curr_hand->cards[curr_hand->index - 1], first)
  298. ) {
  299. add_hand_region(curr_hand, &(game_state->dragging_hand));
  300. remove_drag(game_state);
  301. wasAction = true;
  302. }
  303. }
  304. }
  305. }
  306. if (!wasAction) {
  307. notification_message(notification, &sequence_fail);
  308. }
  309. }
  310. }
  311. if (game_state->state == GameStateAnimate) {
  312. if (game_state->animation.started && !game_state->longPress && game_state->input==InputKeyOk) {
  313. init(game_state);
  314. game_state->state = GameStateStart;
  315. }
  316. game_state->animation.started = true;
  317. if (game_state->animation.x < -20 || game_state->animation.x > 128) {
  318. game_state->animation.deck++;
  319. if (game_state->animation.deck > 3)
  320. game_state->animation.deck = 0;
  321. int8_t cardIndex = 11 - game_state->animation.indexes[game_state->animation.deck];
  322. if (game_state->animation.indexes[0] == 13 &&
  323. game_state->animation.indexes[1] == 13 &&
  324. game_state->animation.indexes[2] == 13 &&
  325. game_state->animation.indexes[3] == 13) {
  326. init(game_state);
  327. game_state->state = GameStateStart;
  328. return;
  329. }
  330. if (cardIndex == -1)
  331. cardIndex = 12;
  332. game_state->animation.card = (Card) {
  333. game_state->top_cards[game_state->animation.deck].pip,
  334. cardIndex,
  335. false, false
  336. };
  337. game_state->animation.indexes[game_state->animation.deck]++;
  338. game_state->animation.vx = -(rand() % 3 + 1) * (rand() % 2 == 1 ? 1 : -1);
  339. game_state->animation.vy = (rand() % 3 + 1);
  340. game_state->animation.x = columns[game_state->animation.deck + 3][0];
  341. game_state->animation.y = columns[game_state->animation.deck + 3][1];
  342. }
  343. game_state->animation.x += game_state->animation.vx;
  344. game_state->animation.y -= game_state->animation.vy;
  345. game_state->animation.vy -= 1;
  346. if (game_state->animation.vy < -10)game_state->animation.vy = -10;
  347. if (game_state->animation.y > 41) {
  348. game_state->animation.y = 41;
  349. game_state->animation.vy = -(game_state->animation.vy * 0.7f);
  350. }
  351. }
  352. }
  353. void init(GameState *game_state) {
  354. DOLPHIN_DEED(DolphinDeedPluginGameStart);
  355. game_state->selectColumn = 0;
  356. game_state->selected_card = 0;
  357. game_state->selectRow = 0;
  358. generate_deck(&(game_state->deck), 1);
  359. shuffle_deck(&(game_state->deck));
  360. game_state->dragging_deck = false;
  361. game_state->animation.started = false;
  362. game_state->animation.deck = -1;
  363. game_state->animation.x = -21;
  364. game_state->state = GameStatePlay;
  365. game_state->dragging_column = 8;
  366. for (uint8_t i = 0; i < 7; i++) {
  367. free_hand(&(game_state->bottom_columns[i]));
  368. init_hand(&(game_state->bottom_columns[i]), 21);
  369. game_state->bottom_columns[i].index = 0;
  370. for (uint8_t j = 0; j <= i; j++) {
  371. Card cur = remove_from_deck(0, &(game_state->deck));
  372. cur.flipped = i != j;
  373. add_to_hand(&(game_state->bottom_columns[i]), cur);
  374. }
  375. }
  376. for (uint8_t i = 0; i < 4; i++) {
  377. game_state->animation.indexes[i] = 0;
  378. game_state->top_cards[i] = (Card) {0, 0, true, false};
  379. }
  380. game_state->deck.index = -1;
  381. }
  382. void init_start(GameState *game_state) {
  383. game_state->input = InputKeyMAX;
  384. for (uint8_t i = 0; i < 7; i++)
  385. init_hand(&(game_state->bottom_columns[i]), 21);
  386. init_hand(&(game_state->dragging_hand), 13);
  387. game_state->animation.buffer = make_buffer();
  388. }
  389. static void input_callback(InputEvent *input_event, FuriMessageQueue *event_queue) {
  390. furi_assert(event_queue);
  391. AppEvent event = {.type = EventTypeKey, .input = *input_event};
  392. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  393. }
  394. static void update_timer_callback(FuriMessageQueue *event_queue) {
  395. furi_assert(event_queue);
  396. AppEvent event = {.type = EventTypeTick};
  397. furi_message_queue_put(event_queue, &event, 0);
  398. }
  399. int32_t solitaire_app(void *p) {
  400. UNUSED(p);
  401. int32_t return_code = 0;
  402. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(AppEvent));
  403. GameState *game_state = malloc(sizeof(GameState));
  404. init_start(game_state);
  405. set_card_graphics(&I_card_graphics);
  406. game_state->state = GameStateStart;
  407. game_state->processing = true;
  408. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  409. if (!game_state->mutex) {
  410. FURI_LOG_E(APP_NAME, "cannot create mutex\r\n");
  411. return_code = 255;
  412. goto free_and_exit;
  413. }
  414. NotificationApp *notification = furi_record_open(RECORD_NOTIFICATION);
  415. notification_message_block(notification, &sequence_display_backlight_enforce_on);
  416. ViewPort *view_port = view_port_alloc();
  417. view_port_draw_callback_set(view_port, render_callback, game_state);
  418. view_port_input_callback_set(view_port, input_callback, event_queue);
  419. FuriTimer *timer =
  420. furi_timer_alloc(update_timer_callback, FuriTimerTypePeriodic, event_queue);
  421. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 30);
  422. Gui *gui = furi_record_open("gui");
  423. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  424. AppEvent event;
  425. for (bool processing = true; processing;) {
  426. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 150);
  427. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  428. bool hadChange = false;
  429. if (event_status == FuriStatusOk) {
  430. if (event.type == EventTypeKey) {
  431. if (event.input.type == InputTypeLong) {
  432. game_state->longPress = true;
  433. switch (event.input.key) {
  434. case InputKeyUp:
  435. case InputKeyDown:
  436. case InputKeyRight:
  437. case InputKeyLeft:
  438. case InputKeyOk:
  439. game_state->input = event.input.key;
  440. break;
  441. case InputKeyBack:
  442. processing = false;
  443. return_code = 1;
  444. default:
  445. break;
  446. }
  447. } else if (event.input.type == InputTypePress) {
  448. game_state->longPress = false;
  449. switch (event.input.key) {
  450. case InputKeyUp:
  451. case InputKeyDown:
  452. case InputKeyRight:
  453. case InputKeyLeft:
  454. case InputKeyOk:
  455. if (event.input.key == InputKeyOk && game_state->state == GameStateStart) {
  456. game_state->state = GameStatePlay;
  457. init(game_state);
  458. }
  459. else {
  460. hadChange = true;
  461. game_state->input = event.input.key;
  462. }
  463. break;
  464. case InputKeyBack:
  465. init(game_state);
  466. processing = false;
  467. return_code = 1;
  468. break;
  469. default:
  470. break;
  471. }
  472. }
  473. } else if (event.type == EventTypeTick) {
  474. tick(game_state, notification);
  475. processing = game_state->processing;
  476. game_state->input = InputKeyMAX;
  477. }
  478. } else {
  479. FURI_LOG_W(APP_NAME, "osMessageQueue: event timeout");
  480. // event timeout
  481. }
  482. if (hadChange || game_state->state == GameStateAnimate)
  483. view_port_update(view_port);
  484. furi_mutex_release(game_state->mutex);
  485. }
  486. notification_message_block(notification, &sequence_display_backlight_enforce_auto);
  487. furi_timer_free(timer);
  488. view_port_enabled_set(view_port, false);
  489. gui_remove_view_port(gui, view_port);
  490. furi_record_close(RECORD_GUI);
  491. furi_record_close(RECORD_NOTIFICATION);
  492. view_port_free(view_port);
  493. furi_mutex_free(game_state->mutex);
  494. free_and_exit:
  495. free(game_state->animation.buffer);
  496. ui_cleanup();
  497. for (uint8_t i = 0; i < 7; i++)
  498. free_hand(&(game_state->bottom_columns[i]));
  499. free(game_state->deck.cards);
  500. free(game_state);
  501. furi_message_queue_free(event_queue);
  502. return return_code;
  503. }