solitaire.c 20 KB

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