card.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "card.h"
  2. #include "dml.h"
  3. #include "ui.h"
  4. #define CARD_DRAW_X_START 108
  5. #define CARD_DRAW_Y_START 38
  6. #define CARD_DRAW_X_SPACE 10
  7. #define CARD_DRAW_Y_SPACE 8
  8. #define CARD_DRAW_X_OFFSET 4
  9. #define CARD_DRAW_FIRST_ROW_LENGTH 7
  10. uint8_t pips[4][3] = {
  11. {21, 10, 7}, //spades
  12. {7, 10, 7}, //hearts
  13. {0, 10, 7}, //diamonds
  14. {14, 10, 7}, //clubs
  15. };
  16. uint8_t letters[13][3] = {
  17. {0, 0, 5},
  18. {5, 0, 5},
  19. {10, 0, 5},
  20. {15, 0, 5},
  21. {20, 0, 5},
  22. {25, 0, 5},
  23. {30, 0, 5},
  24. {0, 5, 5},
  25. {5, 5, 5},
  26. {10, 5, 5},
  27. {15, 5, 5},
  28. {20, 5, 5},
  29. {25, 5, 5},
  30. };
  31. //region Player card positions
  32. uint8_t playerCardPositions[22][4] = {
  33. //first row
  34. {108, 38},
  35. {98, 38},
  36. {88, 38},
  37. {78, 38},
  38. {68, 38},
  39. {58, 38},
  40. {48, 38},
  41. {38, 38},
  42. //second row
  43. {104, 26},
  44. {94, 26},
  45. {84, 26},
  46. {74, 26},
  47. {64, 26},
  48. {54, 26},
  49. {44, 26},
  50. //third row
  51. {99, 14},
  52. {89, 14},
  53. {79, 14},
  54. {69, 14},
  55. {59, 14},
  56. {49, 14},
  57. };
  58. //endregion
  59. Icon* card_graphics = NULL;
  60. void set_card_graphics(const Icon* graphics) {
  61. card_graphics = (Icon*)graphics;
  62. }
  63. void draw_card_at_colored(
  64. int8_t pos_x,
  65. int8_t pos_y,
  66. uint8_t pip,
  67. uint8_t character,
  68. bool inverted,
  69. Canvas* const canvas) {
  70. DrawMode primary = inverted ? Black : White;
  71. DrawMode secondary = inverted ? White : Black;
  72. draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, primary);
  73. draw_rounded_box_frame(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
  74. uint8_t* drawInfo = pips[pip];
  75. uint8_t px = drawInfo[0], py = drawInfo[1], s = drawInfo[2];
  76. uint8_t left = pos_x + 2;
  77. uint8_t right = (pos_x + CARD_WIDTH - s - 2);
  78. uint8_t top = pos_y + 2;
  79. uint8_t bottom = (pos_y + CARD_HEIGHT - s - 2);
  80. draw_icon_clip(canvas, card_graphics, right, top, px, py, s, s, secondary);
  81. draw_icon_clip_flipped(canvas, card_graphics, left, bottom, px, py, s, s, secondary);
  82. drawInfo = letters[character];
  83. px = drawInfo[0], py = drawInfo[1], s = drawInfo[2];
  84. left = pos_x + 2;
  85. right = (pos_x + CARD_WIDTH - s - 2);
  86. top = pos_y + 2;
  87. bottom = (pos_y + CARD_HEIGHT - s - 2);
  88. draw_icon_clip(canvas, card_graphics, left, top + 1, px, py, s, s, secondary);
  89. draw_icon_clip_flipped(canvas, card_graphics, right, bottom - 1, px, py, s, s, secondary);
  90. }
  91. void draw_card_at(int8_t pos_x, int8_t pos_y, uint8_t pip, uint8_t character, Canvas* const canvas) {
  92. draw_card_at_colored(pos_x, pos_y, pip, character, false, canvas);
  93. }
  94. void draw_deck(const Card* cards, uint8_t count, Canvas* const canvas) {
  95. for(int i = count - 1; i >= 0; i--) {
  96. draw_card_at(
  97. playerCardPositions[i][0],
  98. playerCardPositions[i][1],
  99. cards[i].pip,
  100. cards[i].character,
  101. canvas);
  102. }
  103. }
  104. Vector card_pos_at_index(uint8_t index) {
  105. return (Vector){playerCardPositions[index][0], playerCardPositions[index][1]};
  106. }
  107. void draw_card_back_at(int8_t pos_x, int8_t pos_y, Canvas* const canvas) {
  108. draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, White);
  109. draw_rounded_box_frame(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
  110. draw_icon_clip(canvas, card_graphics, pos_x + 1, pos_y + 1, 35, 0, 15, 21, Black);
  111. }
  112. void generate_deck(Deck* deck_ptr, uint8_t deck_count) {
  113. uint16_t counter = 0;
  114. if(deck_ptr->cards != NULL) {
  115. free(deck_ptr->cards);
  116. }
  117. deck_ptr->deck_count = deck_count;
  118. deck_ptr->card_count = deck_count * 52;
  119. deck_ptr->cards = malloc(sizeof(Card) * deck_ptr->card_count);
  120. for(uint8_t deck = 0; deck < deck_count; deck++) {
  121. for(uint8_t pip = 0; pip < 4; pip++) {
  122. for(uint8_t label = 0; label < 13; label++) {
  123. deck_ptr->cards[counter] = (Card){pip, label, false, false};
  124. counter++;
  125. }
  126. }
  127. }
  128. }
  129. void shuffle_deck(Deck* deck_ptr) {
  130. srand(DWT->CYCCNT);
  131. deck_ptr->index = 0;
  132. int max = deck_ptr->deck_count * 52;
  133. for(int i = 0; i < max; i++) {
  134. int r = i + (rand() % (max - i));
  135. Card tmp = deck_ptr->cards[i];
  136. deck_ptr->cards[i] = deck_ptr->cards[r];
  137. deck_ptr->cards[r] = tmp;
  138. }
  139. }
  140. uint8_t hand_count(const Card* cards, uint8_t count) {
  141. uint8_t aceCount = 0;
  142. uint8_t score = 0;
  143. for(uint8_t i = 0; i < count; i++) {
  144. if(cards[i].character == 12)
  145. aceCount++;
  146. else {
  147. if(cards[i].character > 8)
  148. score += 10;
  149. else
  150. score += cards[i].character + 2;
  151. }
  152. }
  153. for(uint8_t i = 0; i < aceCount; i++) {
  154. if((score + 11) <= 21)
  155. score += 11;
  156. else
  157. score++;
  158. }
  159. return score;
  160. }
  161. void draw_card_animation(
  162. Card animatingCard,
  163. Vector from,
  164. Vector control,
  165. Vector to,
  166. float t,
  167. bool extra_margin,
  168. Canvas* const canvas) {
  169. float time = t;
  170. if(extra_margin) {
  171. time += 0.2;
  172. }
  173. Vector currentPos = quadratic_2d(from, control, to, time);
  174. if(t > 1) {
  175. draw_card_at(
  176. currentPos.x, currentPos.y, animatingCard.pip, animatingCard.character, canvas);
  177. } else {
  178. if(t < 0.5)
  179. draw_card_back_at(currentPos.x, currentPos.y, canvas);
  180. else
  181. draw_card_at(
  182. currentPos.x, currentPos.y, animatingCard.pip, animatingCard.character, canvas);
  183. }
  184. }
  185. void init_hand(Hand* hand_ptr, uint8_t count) {
  186. hand_ptr->cards = malloc(sizeof(Card) * count);
  187. hand_ptr->index = 0;
  188. hand_ptr->max = count;
  189. }
  190. void free_hand(Hand* hand_ptr) {
  191. FURI_LOG_D("CARD", "Freeing hand");
  192. free(hand_ptr->cards);
  193. }
  194. void add_to_hand(Hand* hand_ptr, Card card) {
  195. FURI_LOG_D("CARD", "Adding to hand");
  196. if(hand_ptr->index < hand_ptr->max) {
  197. hand_ptr->cards[hand_ptr->index] = card;
  198. hand_ptr->index++;
  199. }
  200. }
  201. void draw_card_space(int16_t pos_x, int16_t pos_y, bool highlighted, Canvas* const canvas) {
  202. if(highlighted) {
  203. draw_rounded_box_frame(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
  204. draw_rounded_box_frame(
  205. canvas, pos_x + 2, pos_y + 2, CARD_WIDTH - 4, CARD_HEIGHT - 4, White);
  206. } else {
  207. draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
  208. draw_rounded_box_frame(
  209. canvas, pos_x + 2, pos_y + 2, CARD_WIDTH - 4, CARD_HEIGHT - 4, White);
  210. }
  211. }
  212. int first_non_flipped_card(Hand hand) {
  213. for(int i = 0; i < hand.index; i++) {
  214. if(!hand.cards[i].flipped) {
  215. return i;
  216. }
  217. }
  218. return hand.index;
  219. }
  220. void draw_hand_column(
  221. Hand hand,
  222. int16_t pos_x,
  223. int16_t pos_y,
  224. int8_t highlight,
  225. Canvas* const canvas) {
  226. if(hand.index == 0) {
  227. draw_card_space(pos_x, pos_y, highlight > 0, canvas);
  228. if(highlight == 0)
  229. draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Inverse);
  230. return;
  231. }
  232. int loopEnd = hand.index;
  233. int hStart = max(loopEnd - 4, 0);
  234. int pos = 0;
  235. int first = first_non_flipped_card(hand);
  236. bool wastop = false;
  237. if(first >= 0 && first <= hStart && highlight != first) {
  238. if(first > 0) {
  239. draw_card_back_at(pos_x, pos_y + pos, canvas);
  240. pos += 4;
  241. hStart++;
  242. wastop = true;
  243. }
  244. draw_card_at_colored(
  245. pos_x, pos_y + pos, hand.cards[first].pip, hand.cards[first].character, false, canvas);
  246. pos += 8;
  247. hStart++;
  248. }
  249. if(hStart > highlight && highlight >= 0) {
  250. if(!wastop && first > 0) {
  251. draw_card_back_at(pos_x, pos_y + pos, canvas);
  252. pos += 4;
  253. hStart++;
  254. }
  255. draw_card_at_colored(
  256. pos_x,
  257. pos_y + pos,
  258. hand.cards[highlight].pip,
  259. hand.cards[highlight].character,
  260. true,
  261. canvas);
  262. pos += 8;
  263. hStart++;
  264. }
  265. for(int i = hStart; i < loopEnd; i++, pos += 4) {
  266. if(hand.cards[i].flipped) {
  267. draw_card_back_at(pos_x, pos_y + pos, canvas);
  268. if(i == highlight)
  269. draw_rounded_box(
  270. canvas, pos_x + 1, pos_y + pos + 1, CARD_WIDTH - 2, CARD_HEIGHT - 2, Inverse);
  271. } else {
  272. draw_card_at_colored(
  273. pos_x,
  274. pos_y + pos,
  275. hand.cards[i].pip,
  276. hand.cards[i].character,
  277. (i == highlight),
  278. canvas);
  279. if(i == highlight || i == first) pos += 4;
  280. }
  281. }
  282. }
  283. Card remove_from_deck(uint16_t index, Deck* deck) {
  284. FURI_LOG_D("CARD", "Removing from deck");
  285. Card result = {0, 0, true, false};
  286. if(deck->card_count > 0) {
  287. deck->card_count--;
  288. for(int i = 0, curr_index = 0; i <= deck->card_count; i++) {
  289. if(i != index) {
  290. deck->cards[curr_index] = deck->cards[i];
  291. curr_index++;
  292. } else {
  293. result = deck->cards[i];
  294. }
  295. }
  296. if(deck->index >= 0) {
  297. deck->index--;
  298. }
  299. }
  300. return result;
  301. }
  302. void extract_hand_region(Hand* hand, Hand* to, uint8_t start_index) {
  303. FURI_LOG_D("CARD", "Extracting hand region");
  304. if(start_index >= hand->index) return;
  305. for(uint8_t i = start_index; i < hand->index; i++) {
  306. add_to_hand(to, hand->cards[i]);
  307. }
  308. hand->index = start_index;
  309. }
  310. void add_hand_region(Hand* to, Hand* from) {
  311. FURI_LOG_D("CARD", "Adding hand region");
  312. if((to->index + from->index) <= to->max) {
  313. for(int i = 0; i < from->index; i++) {
  314. add_to_hand(to, from->cards[i]);
  315. }
  316. }
  317. }