card.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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
  64. draw_card_at_colored(int8_t pos_x, int8_t pos_y, uint8_t pip, uint8_t character, bool inverted, Canvas *const canvas) {
  65. DrawMode primary = inverted ? Black : White;
  66. DrawMode secondary = inverted ? White : Black;
  67. draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, primary);
  68. draw_rounded_box_frame(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
  69. uint8_t *drawInfo = pips[pip];
  70. uint8_t px = drawInfo[0], py = drawInfo[1], s = drawInfo[2];
  71. uint8_t left = pos_x + 2;
  72. uint8_t right = (pos_x + CARD_WIDTH - s - 2);
  73. uint8_t top = pos_y + 2;
  74. uint8_t bottom = (pos_y + CARD_HEIGHT - s - 2);
  75. draw_icon_clip(canvas, card_graphics, right, top, px, py, s, s,
  76. secondary);
  77. draw_icon_clip_flipped(canvas, card_graphics, left, bottom, px, py, s, s,
  78. secondary);
  79. drawInfo = letters[character];
  80. px = drawInfo[0], py = drawInfo[1], s = drawInfo[2];
  81. left = pos_x + 2;
  82. right = (pos_x + CARD_WIDTH - s - 2);
  83. top = pos_y + 2;
  84. bottom = (pos_y + CARD_HEIGHT - s - 2);
  85. draw_icon_clip(canvas, card_graphics, left, top + 1, px, py, s, s,
  86. secondary);
  87. draw_icon_clip_flipped(canvas, card_graphics, right, bottom - 1, px, py, s, s,
  88. secondary);
  89. }
  90. void draw_card_at(int8_t pos_x, int8_t pos_y, uint8_t pip, uint8_t character, Canvas *const canvas) {
  91. draw_card_at_colored(pos_x, pos_y, pip, character, false, canvas);
  92. }
  93. void draw_deck(const Card *cards, uint8_t count, Canvas *const canvas) {
  94. for (int i = count - 1; i >= 0; i--) {
  95. draw_card_at(playerCardPositions[i][0], playerCardPositions[i][1], cards[i].pip, cards[i].character, canvas);
  96. }
  97. }
  98. Vector card_pos_at_index(uint8_t index) {
  99. return (Vector) {
  100. playerCardPositions[index][0],
  101. playerCardPositions[index][1]
  102. };
  103. }
  104. void draw_card_back_at(int8_t pos_x, int8_t pos_y, Canvas *const canvas) {
  105. draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, White);
  106. draw_rounded_box_frame(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
  107. draw_icon_clip(canvas, card_graphics, pos_x + 1, pos_y + 1, 35, 0, 15, 21, Black);
  108. }
  109. void generate_deck(Deck *deck_ptr, uint8_t deck_count) {
  110. uint16_t counter = 0;
  111. if (deck_ptr->cards != NULL) {
  112. free(deck_ptr->cards);
  113. }
  114. deck_ptr->deck_count = deck_count;
  115. deck_ptr->card_count = deck_count * 52;
  116. deck_ptr->cards = malloc(sizeof(Card) * deck_ptr->card_count);
  117. for (uint8_t deck = 0; deck < deck_count; deck++) {
  118. for (uint8_t pip = 0; pip < 4; pip++) {
  119. for (uint8_t label = 0; label < 13; label++) {
  120. deck_ptr->cards[counter] = (Card)
  121. {
  122. pip, label, false, false
  123. };
  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) score += 11;
  155. else score++;
  156. }
  157. return score;
  158. }
  159. void draw_card_animation(Card animatingCard, Vector from, Vector control, Vector to, float t, bool extra_margin,
  160. Canvas *const canvas) {
  161. float time = t;
  162. if (extra_margin) {
  163. time += 0.2;
  164. }
  165. Vector currentPos = quadratic_2d(from, control, to, time);
  166. if (t > 1) {
  167. draw_card_at(currentPos.x, currentPos.y, animatingCard.pip,
  168. animatingCard.character, canvas);
  169. } else {
  170. if (t < 0.5)
  171. draw_card_back_at(currentPos.x, currentPos.y, canvas);
  172. else
  173. draw_card_at(currentPos.x, currentPos.y, animatingCard.pip,
  174. animatingCard.character, canvas);
  175. }
  176. }
  177. void init_hand(Hand *hand_ptr, uint8_t count) {
  178. hand_ptr->cards = malloc(sizeof(Card) * count);
  179. hand_ptr->index = 0;
  180. hand_ptr->max = count;
  181. }
  182. void free_hand(Hand *hand_ptr) {
  183. FURI_LOG_D("CARD", "Freeing hand");
  184. free(hand_ptr->cards);
  185. }
  186. void add_to_hand(Hand *hand_ptr, Card card) {
  187. FURI_LOG_D("CARD", "Adding to hand");
  188. if (hand_ptr->index < hand_ptr->max) {
  189. hand_ptr->cards[hand_ptr->index] = card;
  190. hand_ptr->index++;
  191. }
  192. }
  193. void draw_card_space(int16_t pos_x, int16_t pos_y, bool highlighted, Canvas *const canvas) {
  194. if (highlighted) {
  195. draw_rounded_box_frame(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
  196. draw_rounded_box_frame(canvas, pos_x + 2, pos_y + 2, CARD_WIDTH - 4, CARD_HEIGHT - 4, White);
  197. } else {
  198. draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT, Black);
  199. draw_rounded_box_frame(canvas, pos_x + 2, pos_y + 2, CARD_WIDTH - 4, CARD_HEIGHT - 4, White);
  200. }
  201. }
  202. int first_non_flipped_card(Hand hand) {
  203. for (int i = 0; i < hand.index; i++) {
  204. if (!hand.cards[i].flipped) {
  205. return i;
  206. }
  207. }
  208. return hand.index;
  209. }
  210. void draw_hand_column(Hand hand, int16_t pos_x, int16_t pos_y, int8_t highlight, Canvas *const canvas) {
  211. if (hand.index == 0) {
  212. draw_card_space(pos_x, pos_y, highlight > 0, canvas);
  213. if(highlight==0)
  214. draw_rounded_box(canvas, pos_x, pos_y, CARD_WIDTH, CARD_HEIGHT,
  215. Inverse);
  216. return;
  217. }
  218. int loopEnd = hand.index;
  219. int hStart = max(loopEnd-4, 0);
  220. int pos = 0;
  221. int first= first_non_flipped_card(hand);
  222. bool wastop=false;
  223. if(first>=0 && first<=hStart && highlight!=first){
  224. if(first>0){
  225. draw_card_back_at(pos_x, pos_y + pos, canvas);
  226. pos+=4;
  227. hStart++;
  228. wastop=true;
  229. }
  230. draw_card_at_colored(pos_x, pos_y + pos, hand.cards[first].pip, hand.cards[first].character,
  231. false,
  232. canvas);
  233. pos+=8;
  234. hStart++;
  235. }
  236. if(hStart>highlight && highlight>=0){
  237. if(!wastop && first>0){
  238. draw_card_back_at(pos_x, pos_y + pos, canvas);
  239. pos+=4;
  240. hStart++;
  241. }
  242. draw_card_at_colored(pos_x, pos_y + pos, hand.cards[highlight].pip, hand.cards[highlight].character,
  243. true,
  244. canvas);
  245. pos+=8;
  246. hStart++;
  247. }
  248. for (int i = hStart; i < loopEnd; i++, pos+=4) {
  249. if (hand.cards[i].flipped) {
  250. draw_card_back_at(pos_x, pos_y + pos, canvas);
  251. if(i==highlight)
  252. draw_rounded_box(canvas, pos_x+1, pos_y + pos+1, CARD_WIDTH - 2, CARD_HEIGHT - 2,
  253. Inverse);
  254. } else {
  255. draw_card_at_colored(pos_x, pos_y + pos, hand.cards[i].pip, hand.cards[i].character,
  256. (i == highlight),
  257. canvas);
  258. if(i == highlight || i==first) pos+=4;
  259. }
  260. }
  261. }
  262. Card remove_from_deck(uint16_t index, Deck *deck) {
  263. FURI_LOG_D("CARD", "Removing from deck");
  264. Card result = {0, 0, true, false};
  265. if (deck->card_count > 0) {
  266. deck->card_count--;
  267. for (int i = 0, curr_index = 0; i <= deck->card_count; i++) {
  268. if (i != index) {
  269. deck->cards[curr_index] = deck->cards[i];
  270. curr_index++;
  271. } else {
  272. result = deck->cards[i];
  273. }
  274. }
  275. if (deck->index >= 0) {
  276. deck->index--;
  277. }
  278. }
  279. return result;
  280. }
  281. void extract_hand_region(Hand *hand, Hand *to, uint8_t start_index) {
  282. FURI_LOG_D("CARD", "Extracting hand region");
  283. if (start_index >= hand->index) return;
  284. for (uint8_t i = start_index; i < hand->index; i++) {
  285. add_to_hand(to, hand->cards[i]);
  286. }
  287. hand->index = start_index;
  288. }
  289. void add_hand_region(Hand *to, Hand *from) {
  290. FURI_LOG_D("CARD", "Adding hand region");
  291. if ((to->index + from->index) <= to->max) {
  292. for (int i = 0; i < from->index; i++) {
  293. add_to_hand(to, from->cards[i]);
  294. }
  295. }
  296. }