blackjack.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. #include <gui/gui.h>
  2. #include <stdlib.h>
  3. #include <dolphin/dolphin.h>
  4. #include <dialogs/dialogs.h>
  5. #include <gui/canvas_i.h>
  6. #include <math.h>
  7. #include "util.h"
  8. #include "defines.h"
  9. #include "common/card.h"
  10. #include "common/dml.h"
  11. #include "common/queue.h"
  12. #include "util.h"
  13. #include "ui.h"
  14. #include "blackjack_icons.h"
  15. #define DEALER_MAX 17
  16. void start_round(GameState *game_state);
  17. void init(GameState *game_state);
  18. static void draw_ui(Canvas *const canvas, const GameState *game_state) {
  19. draw_money(canvas, game_state->player_score);
  20. draw_score(canvas, true, hand_count(game_state->player_cards, game_state->player_card_count));
  21. if (!game_state->queue_state.running && game_state->state == GameStatePlay) {
  22. render_menu(game_state->menu,canvas, 2, 47);
  23. }
  24. }
  25. static void render_callback(Canvas *const canvas, void *ctx) {
  26. const GameState *game_state = ctx;
  27. furi_mutex_acquire(game_state->mutex, 25);
  28. if (game_state == NULL) {
  29. return;
  30. }
  31. canvas_set_color(canvas, ColorBlack);
  32. canvas_draw_frame(canvas, 0, 0, 128, 64);
  33. if (game_state->state == GameStateStart) {
  34. canvas_draw_icon(canvas, 0, 0, &I_blackjack);
  35. }
  36. if (game_state->state == GameStateGameOver) {
  37. canvas_draw_icon(canvas, 0, 0, &I_endscreen);
  38. }
  39. if (game_state->state == GameStatePlay || game_state->state == GameStateDealer) {
  40. if (game_state->state == GameStatePlay)
  41. draw_player_scene(canvas, game_state);
  42. else
  43. draw_dealer_scene(canvas, game_state);
  44. render_queue(&(game_state->queue_state), game_state, canvas);
  45. draw_ui(canvas, game_state);
  46. } else if (game_state->state == GameStateSettings) {
  47. settings_page(canvas, game_state);
  48. }
  49. furi_mutex_release(game_state->mutex);
  50. }
  51. //region card draw
  52. Card draw_card(GameState *game_state) {
  53. Card c = game_state->deck.cards[game_state->deck.index];
  54. game_state->deck.index++;
  55. return c;
  56. }
  57. void drawPlayerCard(void *ctx) {
  58. GameState *game_state = ctx;
  59. Card c = draw_card(game_state);
  60. game_state->player_cards[game_state->player_card_count] = c;
  61. game_state->player_card_count++;
  62. if(game_state->player_score < game_state->settings.round_price || game_state->doubled){
  63. set_menu_state(game_state->menu, 0, false);
  64. }
  65. }
  66. void drawDealerCard(void *ctx) {
  67. GameState *game_state = ctx;
  68. Card c = draw_card(game_state);
  69. game_state->dealer_cards[game_state->dealer_card_count] = c;
  70. game_state->dealer_card_count++;
  71. }
  72. //endregion
  73. //region queue callbacks
  74. void to_lose_state(const void *ctx, Canvas *const canvas) {
  75. const GameState *game_state = ctx;
  76. if (game_state->settings.message_duration == 0)
  77. return;
  78. popup_frame(canvas);
  79. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "You lost");
  80. }
  81. void to_bust_state(const void *ctx, Canvas *const canvas) {
  82. const GameState *game_state = ctx;
  83. if (game_state->settings.message_duration == 0)
  84. return;
  85. popup_frame(canvas);
  86. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Busted!");
  87. }
  88. void to_draw_state(const void *ctx, Canvas *const canvas) {
  89. const GameState *game_state = ctx;
  90. if (game_state->settings.message_duration == 0)
  91. return;
  92. popup_frame(canvas);
  93. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Draw");
  94. }
  95. void to_dealer_turn(const void *ctx, Canvas *const canvas) {
  96. const GameState *game_state = ctx;
  97. if (game_state->settings.message_duration == 0)
  98. return;
  99. popup_frame(canvas);
  100. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Dealers turn");
  101. }
  102. void to_win_state(const void *ctx, Canvas *const canvas) {
  103. const GameState *game_state = ctx;
  104. if (game_state->settings.message_duration == 0)
  105. return;
  106. popup_frame(canvas);
  107. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "You win");
  108. }
  109. void to_start(const void *ctx, Canvas *const canvas) {
  110. const GameState *game_state = ctx;
  111. if (game_state->settings.message_duration == 0)
  112. return;
  113. popup_frame(canvas);
  114. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Round started");
  115. }
  116. void before_start(void *ctx) {
  117. GameState *game_state = ctx;
  118. game_state->dealer_card_count = 0;
  119. game_state->player_card_count = 0;
  120. }
  121. void start(void *ctx) {
  122. GameState *game_state = ctx;
  123. start_round(game_state);
  124. }
  125. void draw(void *ctx) {
  126. GameState *game_state = ctx;
  127. game_state->player_score += game_state->bet;
  128. game_state->bet = 0;
  129. enqueue(&(game_state->queue_state), game_state, start, before_start, to_start,
  130. game_state->settings.message_duration);
  131. }
  132. void game_over(void *ctx) {
  133. GameState *game_state = ctx;
  134. game_state->state = GameStateGameOver;
  135. }
  136. void lose(void *ctx) {
  137. GameState *game_state = ctx;
  138. game_state->state = GameStatePlay;
  139. game_state->bet = 0;
  140. if (game_state->player_score >= game_state->settings.round_price) {
  141. enqueue(&(game_state->queue_state), game_state, start, before_start, to_start,
  142. game_state->settings.message_duration);
  143. } else {
  144. enqueue(&(game_state->queue_state), game_state, game_over, NULL, NULL,
  145. 0);
  146. }
  147. }
  148. void win(void *ctx) {
  149. dolphin_deed(DolphinDeedPluginGameWin);
  150. GameState *game_state = ctx;
  151. game_state->state = GameStatePlay;
  152. game_state->player_score += game_state->bet * 2;
  153. game_state->bet = 0;
  154. enqueue(&(game_state->queue_state), game_state, start, before_start, to_start,
  155. game_state->settings.message_duration);
  156. }
  157. void dealerTurn(void *ctx) {
  158. GameState *game_state = ctx;
  159. game_state->state = GameStateDealer;
  160. }
  161. float animationTime(const GameState *game_state){
  162. return (float) (furi_get_tick() - game_state->queue_state.start) /
  163. (float) (game_state->settings.animation_duration);
  164. }
  165. void dealer_card_animation(const void *ctx, Canvas *const canvas) {
  166. const GameState *game_state = ctx;
  167. float t = animationTime(game_state);
  168. Card animatingCard = game_state->deck.cards[game_state->deck.index];
  169. if (game_state->dealer_card_count > 1) {
  170. Vector end = card_pos_at_index(game_state->dealer_card_count);
  171. draw_card_animation(animatingCard,
  172. (Vector) {0, 64},
  173. (Vector) {0, 32},
  174. end,
  175. t,
  176. true,
  177. canvas);
  178. } else {
  179. draw_card_animation(animatingCard,
  180. (Vector) {32, -CARD_HEIGHT},
  181. (Vector) {64, 32},
  182. (Vector) {2, 2},
  183. t,
  184. false,
  185. canvas);
  186. }
  187. }
  188. void dealer_back_card_animation(const void *ctx, Canvas *const canvas) {
  189. const GameState *game_state = ctx;
  190. float t = animationTime(game_state);
  191. Vector currentPos = quadratic_2d((Vector) {32, -CARD_HEIGHT}, (Vector) {64, 32}, (Vector) {13, 5}, t);
  192. draw_card_back_at(currentPos.x, currentPos.y, canvas);
  193. }
  194. void player_card_animation(const void *ctx, Canvas *const canvas) {
  195. const GameState *game_state = ctx;
  196. float t = animationTime(game_state);
  197. Card animatingCard = game_state->deck.cards[game_state->deck.index];
  198. Vector end = card_pos_at_index(game_state->player_card_count);
  199. draw_card_animation(animatingCard,
  200. (Vector) {32, -CARD_HEIGHT},
  201. (Vector) {0, 32},
  202. end,
  203. t,
  204. true,
  205. canvas);
  206. }
  207. //endregion
  208. void player_tick(GameState *game_state) {
  209. uint8_t score = hand_count(game_state->player_cards, game_state->player_card_count);
  210. if ((game_state->doubled && score <= 21) || score == 21) {
  211. enqueue(&(game_state->queue_state), game_state, dealerTurn, NULL, to_dealer_turn,
  212. game_state->settings.message_duration);
  213. } else if (score > 21) {
  214. enqueue(&(game_state->queue_state), game_state, lose, NULL, to_bust_state,
  215. game_state->settings.message_duration);
  216. } else {
  217. if(game_state->selectDirection == DirectionUp || game_state->selectDirection == DirectionDown){
  218. move_menu(game_state->menu, game_state->selectDirection == DirectionUp ? -1 : 1);
  219. }
  220. if (game_state->selectDirection == Select){
  221. activate_menu(game_state->menu, game_state);
  222. }
  223. }
  224. }
  225. void dealer_tick(GameState *game_state) {
  226. uint8_t dealer_score = hand_count(game_state->dealer_cards, game_state->dealer_card_count);
  227. uint8_t player_score = hand_count(game_state->player_cards, game_state->player_card_count);
  228. if (dealer_score >= DEALER_MAX) {
  229. if (dealer_score > 21 || dealer_score < player_score) {
  230. enqueue(&(game_state->queue_state), game_state, win, NULL, to_win_state,
  231. game_state->settings.message_duration);
  232. } else if (dealer_score > player_score) {
  233. enqueue(&(game_state->queue_state), game_state, lose, NULL, to_lose_state,
  234. game_state->settings.message_duration);
  235. } else if (dealer_score == player_score) {
  236. enqueue(&(game_state->queue_state), game_state, draw, NULL, to_draw_state,
  237. game_state->settings.message_duration);
  238. }
  239. } else {
  240. enqueue(&(game_state->queue_state), game_state, drawDealerCard, NULL, dealer_card_animation,
  241. game_state->settings.animation_duration);
  242. }
  243. }
  244. void settings_tick(GameState *game_state) {
  245. if (game_state->selectDirection == DirectionDown && game_state->selectedMenu < 4) {
  246. game_state->selectedMenu++;
  247. }
  248. if (game_state->selectDirection == DirectionUp && game_state->selectedMenu > 0) {
  249. game_state->selectedMenu--;
  250. }
  251. if (game_state->selectDirection == DirectionLeft || game_state->selectDirection == DirectionRight) {
  252. int nextScore = 0;
  253. switch (game_state->selectedMenu) {
  254. case 0:
  255. nextScore = game_state->settings.starting_money;
  256. if (game_state->selectDirection == DirectionLeft)
  257. nextScore -= 10;
  258. else
  259. nextScore += 10;
  260. if (nextScore >= (int) game_state->settings.round_price && nextScore < 400)
  261. game_state->settings.starting_money = nextScore;
  262. break;
  263. case 1:
  264. nextScore = game_state->settings.round_price;
  265. if (game_state->selectDirection == DirectionLeft)
  266. nextScore -= 10;
  267. else
  268. nextScore += 10;
  269. if (nextScore >= 5 && nextScore <= (int) game_state->settings.starting_money)
  270. game_state->settings.round_price = nextScore;
  271. break;
  272. case 2:
  273. nextScore = game_state->settings.animation_duration;
  274. if (game_state->selectDirection == DirectionLeft)
  275. nextScore -= 100;
  276. else
  277. nextScore += 100;
  278. if (nextScore >= 0 && nextScore < 2000)
  279. game_state->settings.animation_duration = nextScore;
  280. break;
  281. case 3:
  282. nextScore = game_state->settings.message_duration;
  283. if (game_state->selectDirection == DirectionLeft)
  284. nextScore -= 100;
  285. else
  286. nextScore += 100;
  287. if (nextScore >= 0 && nextScore < 2000)
  288. game_state->settings.message_duration = nextScore;
  289. break;
  290. case 4:
  291. game_state->settings.sound_effects = !game_state->settings.sound_effects;
  292. default:
  293. break;
  294. }
  295. }
  296. }
  297. void tick(GameState *game_state) {
  298. game_state->last_tick = furi_get_tick();
  299. bool queue_ran = run_queue(&(game_state->queue_state), game_state);
  300. switch (game_state->state) {
  301. case GameStateGameOver:
  302. case GameStateStart:
  303. if (game_state->selectDirection == Select)
  304. init(game_state);
  305. else if (game_state->selectDirection == DirectionRight) {
  306. game_state->selectedMenu = 0;
  307. game_state->state = GameStateSettings;
  308. }
  309. break;
  310. case GameStatePlay:
  311. if (!game_state->started) {
  312. game_state->selectedMenu = 0;
  313. game_state->started = true;
  314. enqueue(&(game_state->queue_state), game_state, drawDealerCard, NULL, dealer_back_card_animation,
  315. game_state->settings.animation_duration);
  316. enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
  317. game_state->settings.animation_duration);
  318. enqueue(&(game_state->queue_state), game_state, drawDealerCard, NULL, dealer_card_animation,
  319. game_state->settings.animation_duration);
  320. enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
  321. game_state->settings.animation_duration);
  322. }
  323. if (!queue_ran)
  324. player_tick(game_state);
  325. break;
  326. case GameStateDealer:
  327. if (!queue_ran)
  328. dealer_tick(game_state);
  329. break;
  330. case GameStateSettings:
  331. settings_tick(game_state);
  332. break;
  333. default:
  334. break;
  335. }
  336. game_state->selectDirection = None;
  337. }
  338. void start_round(GameState *game_state) {
  339. game_state->menu->current_menu=1;
  340. game_state->player_card_count = 0;
  341. game_state->dealer_card_count = 0;
  342. set_menu_state(game_state->menu, 0, true);
  343. game_state->menu->enabled=true;
  344. game_state->started = false;
  345. game_state->doubled = false;
  346. game_state->queue_state.running = true;
  347. shuffle_deck(&(game_state->deck));
  348. game_state->doubled = false;
  349. game_state->bet = game_state->settings.round_price;
  350. if (game_state->player_score < game_state->settings.round_price) {
  351. game_state->state = GameStateGameOver;
  352. } else {
  353. game_state->player_score -= game_state->settings.round_price;
  354. }
  355. game_state->state = GameStatePlay;
  356. }
  357. void init(GameState *game_state) {
  358. set_menu_state(game_state->menu, 0, true);
  359. game_state->menu->enabled=true;
  360. game_state->menu->current_menu=1;
  361. game_state->settings = load_settings();
  362. game_state->last_tick = 0;
  363. game_state->processing = true;
  364. game_state->selectedMenu = 0;
  365. game_state->player_score = game_state->settings.starting_money;
  366. generate_deck(&(game_state->deck), 6);
  367. start_round(game_state);
  368. }
  369. static void input_callback(InputEvent *input_event, FuriMessageQueue *event_queue) {
  370. furi_assert(event_queue);
  371. AppEvent event = {.type = EventTypeKey, .input = *input_event};
  372. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  373. }
  374. static void update_timer_callback(FuriMessageQueue *event_queue) {
  375. furi_assert(event_queue);
  376. AppEvent event = {.type = EventTypeTick};
  377. furi_message_queue_put(event_queue, &event, 0);
  378. }
  379. void doubleAction(void *state){
  380. GameState *game_state = state;
  381. if (!game_state->doubled && game_state->player_score >= game_state->settings.round_price) {
  382. game_state->player_score -= game_state->settings.round_price;
  383. game_state->bet += game_state->settings.round_price;
  384. game_state->doubled = true;
  385. enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
  386. game_state->settings.animation_duration);
  387. game_state->player_cards[game_state->player_card_count] = game_state->deck.cards[game_state->deck.index];
  388. uint8_t score = hand_count(game_state->player_cards, game_state->player_card_count + 1);
  389. if (score > 21) {
  390. enqueue(&(game_state->queue_state), game_state, lose, NULL, to_bust_state,
  391. game_state->settings.message_duration);
  392. } else {
  393. enqueue(&(game_state->queue_state), game_state, dealerTurn, NULL, to_dealer_turn,
  394. game_state->settings.message_duration);
  395. }
  396. set_menu_state(game_state->menu, 0, false);
  397. }
  398. }
  399. void hitAction(void *state){
  400. GameState *game_state = state;
  401. enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
  402. game_state->settings.animation_duration);
  403. }
  404. void stayAction(void *state){
  405. GameState *game_state = state;
  406. enqueue(&(game_state->queue_state), game_state, dealerTurn, NULL, to_dealer_turn,
  407. game_state->settings.message_duration);
  408. }
  409. int32_t blackjack_app(void *p) {
  410. UNUSED(p);
  411. int32_t return_code = 0;
  412. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(AppEvent));
  413. dolphin_deed(DolphinDeedPluginGameStart);
  414. GameState *game_state = malloc(sizeof(GameState));
  415. game_state->menu= malloc(sizeof(Menu));
  416. game_state->menu->menu_width=40;
  417. init(game_state);
  418. add_menu(game_state->menu, "Double", doubleAction);
  419. add_menu(game_state->menu, "Hit", hitAction);
  420. add_menu(game_state->menu, "Stay", stayAction);
  421. set_card_graphics(&I_card_graphics);
  422. game_state->state = GameStateStart;
  423. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  424. if (!game_state->mutex) {
  425. FURI_LOG_E(APP_NAME, "cannot create mutex\r\n");
  426. return_code = 255;
  427. goto free_and_exit;
  428. }
  429. ViewPort *view_port = view_port_alloc();
  430. view_port_draw_callback_set(view_port, render_callback, game_state);
  431. view_port_input_callback_set(view_port, input_callback, event_queue);
  432. FuriTimer *timer =
  433. furi_timer_alloc(update_timer_callback, FuriTimerTypePeriodic, event_queue);
  434. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25);
  435. Gui *gui = furi_record_open("gui");
  436. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  437. AppEvent event;
  438. for (bool processing = true; processing;) {
  439. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  440. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  441. if (event_status == FuriStatusOk) {
  442. if (event.type == EventTypeKey) {
  443. if (event.input.type == InputTypePress) {
  444. switch (event.input.key) {
  445. case InputKeyUp:
  446. game_state->selectDirection = DirectionUp;
  447. break;
  448. case InputKeyDown:
  449. game_state->selectDirection = DirectionDown;
  450. break;
  451. case InputKeyRight:
  452. game_state->selectDirection = DirectionRight;
  453. break;
  454. case InputKeyLeft:
  455. game_state->selectDirection = DirectionLeft;
  456. break;
  457. case InputKeyBack:
  458. if (game_state->state == GameStateSettings) {
  459. game_state->state = GameStateStart;
  460. save_settings(game_state->settings);
  461. } else
  462. processing = false;
  463. break;
  464. case InputKeyOk:
  465. game_state->selectDirection = Select;
  466. break;
  467. default:
  468. break;
  469. }
  470. }
  471. } else if (event.type == EventTypeTick) {
  472. tick(game_state);
  473. processing = game_state->processing;
  474. }
  475. // } else {
  476. // FURI_LOG_D(APP_NAME, "osMessageQueue: event timeout");
  477. // event timeout
  478. }
  479. view_port_update(view_port);
  480. furi_mutex_release(game_state->mutex);
  481. }
  482. furi_timer_free(timer);
  483. view_port_enabled_set(view_port, false);
  484. gui_remove_view_port(gui, view_port);
  485. furi_record_close(RECORD_GUI);
  486. view_port_free(view_port);
  487. furi_mutex_free(game_state->mutex);
  488. free_and_exit:
  489. free(game_state->deck.cards);
  490. free_menu(game_state->menu);
  491. queue_clear(&(game_state->queue_state));
  492. free(game_state);
  493. furi_message_queue_free(event_queue);
  494. return return_code;
  495. }