tanks_game.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <lib/toolbox/args.h>
  7. #include <lib/subghz/receiver.h>
  8. #include <lib/subghz/subghz_keystore.h>
  9. #include <lib/subghz/protocols/base.h>
  10. #include <lib/subghz/protocols/raw.h>
  11. #include <lib/subghz/protocols/princeton.h>
  12. #include <lib/subghz/subghz_tx_rx_worker.h>
  13. #include <tanks_icons.h>
  14. #include "helpers/radio_device_loader.h"
  15. #include "constants.h"
  16. typedef struct {
  17. // +-----x
  18. // |
  19. // |
  20. // y
  21. uint8_t x;
  22. uint8_t y;
  23. } Point;
  24. typedef enum {
  25. CellEmpty = 1,
  26. CellWall,
  27. CellExplosion,
  28. CellTankUp,
  29. CellTankRight,
  30. CellTankDown,
  31. CellTankLeft,
  32. CellEnemyUp,
  33. CellEnemyRight,
  34. CellEnemyDown,
  35. CellEnemyLeft,
  36. CellProjectileUp,
  37. CellProjectileRight,
  38. CellProjectileDown,
  39. CellProjectileLeft,
  40. } GameCellState;
  41. typedef enum {
  42. MenuStateSingleMode,
  43. MenuStateCooperativeServerMode,
  44. MenuStateCooperativeClientMode,
  45. } MenuState;
  46. typedef enum {
  47. GameStateMenu,
  48. GameStateSingle,
  49. GameStateCooperativeServer,
  50. GameStateCooperativeClient,
  51. GameStateGameOver,
  52. } GameState;
  53. typedef enum {
  54. DirectionUp,
  55. DirectionRight,
  56. DirectionDown,
  57. DirectionLeft,
  58. } Direction;
  59. typedef enum {
  60. ModeSingle,
  61. ModeCooperative,
  62. } Mode;
  63. typedef struct {
  64. Point coordinates;
  65. Direction direction;
  66. bool explosion;
  67. bool is_p1;
  68. bool is_p2;
  69. } ProjectileState;
  70. typedef struct {
  71. Point coordinates;
  72. uint16_t score;
  73. uint8_t lives;
  74. Direction direction;
  75. bool moving;
  76. bool shooting;
  77. bool live;
  78. uint8_t cooldown;
  79. uint8_t respawn_cooldown;
  80. } PlayerState;
  81. typedef struct {
  82. // char map[FIELD_WIDTH][FIELD_HEIGHT];
  83. char thisMap[16][11];
  84. Point team_one_respawn_points[3];
  85. Point team_two_respawn_points[3];
  86. Mode mode;
  87. bool server;
  88. GameState state;
  89. MenuState menu_state;
  90. ProjectileState* projectiles[100];
  91. PlayerState* bots[6];
  92. uint8_t enemies_left;
  93. uint8_t enemies_live;
  94. uint8_t enemies_respawn_cooldown;
  95. uint8_t received;
  96. uint8_t sent;
  97. PlayerState* p1;
  98. PlayerState* p2;
  99. FuriMutex* mutex;
  100. } TanksState;
  101. typedef enum {
  102. EventTypeTick,
  103. EventTypeKey,
  104. } EventType;
  105. typedef struct {
  106. EventType type;
  107. InputEvent input;
  108. } TanksEvent;
  109. typedef enum {
  110. GoesUp,
  111. GoesRight,
  112. GoesDown,
  113. GoesLeft,
  114. Shoots,
  115. } ClientAction;
  116. //char map[FIELD_HEIGHT][FIELD_WIDTH + 1] = {
  117. char thisMap[11][16 + 1] = {
  118. "* - * -",
  119. " - - = ",
  120. " - - 2",
  121. "1 = - -- ",
  122. "-- = - -- ",
  123. "a-1 = - = 2",
  124. "-- = - -- ",
  125. "1 = - -- ",
  126. " - - 2",
  127. " - - = ",
  128. "* - * -",
  129. };
  130. static void tanks_game_write_cell(unsigned char* data, int8_t x, int8_t y, GameCellState cell) {
  131. uint8_t index = y * 16 + x;
  132. data[index] = cell;
  133. // if (x % 2) {
  134. // data[index] = (data[index] & 0b00001111) + (cell << 4);
  135. // } else {
  136. // data[index] = (data[index] & 0b0000) + cell;
  137. // }
  138. }
  139. // Enum with < 16 items => 4 bits in cell, 2 cells in byte
  140. unsigned char* tanks_game_serialize(const TanksState* const tanks_state) {
  141. static unsigned char result[11 * 16 + 1];
  142. for(int8_t x = 0; x < FIELD_WIDTH; x++) {
  143. for(int8_t y = 0; y < FIELD_HEIGHT; y++) {
  144. result[(y * FIELD_WIDTH + x)] = 0;
  145. GameCellState cell = CellEmpty;
  146. if(tanks_state->thisMap[x][y] == '-') {
  147. cell = CellWall;
  148. tanks_game_write_cell(result, x, y, cell);
  149. }
  150. }
  151. }
  152. for(uint8_t i = 0; i < 6; i++) {
  153. if(tanks_state->bots[i] != NULL) {
  154. GameCellState cell = CellEmpty;
  155. switch(tanks_state->bots[i]->direction) {
  156. case DirectionUp:
  157. cell = CellEnemyUp;
  158. break;
  159. case DirectionDown:
  160. cell = CellEnemyDown;
  161. break;
  162. case DirectionRight:
  163. cell = CellEnemyRight;
  164. break;
  165. case DirectionLeft:
  166. cell = CellEnemyLeft;
  167. break;
  168. default:
  169. break;
  170. }
  171. tanks_game_write_cell(
  172. result,
  173. tanks_state->bots[i]->coordinates.x,
  174. tanks_state->bots[i]->coordinates.y,
  175. cell);
  176. }
  177. }
  178. for(int8_t x = 0; x < 100; x++) {
  179. if(tanks_state->projectiles[x] != NULL) {
  180. GameCellState cell = CellEmpty;
  181. switch(tanks_state->projectiles[x]->direction) {
  182. case DirectionUp:
  183. cell = CellProjectileUp;
  184. break;
  185. case DirectionDown:
  186. cell = CellProjectileDown;
  187. break;
  188. case DirectionRight:
  189. cell = CellProjectileRight;
  190. break;
  191. case DirectionLeft:
  192. cell = CellProjectileLeft;
  193. break;
  194. default:
  195. break;
  196. }
  197. tanks_game_write_cell(
  198. result,
  199. tanks_state->projectiles[x]->coordinates.x,
  200. tanks_state->projectiles[x]->coordinates.y,
  201. cell);
  202. }
  203. }
  204. if(tanks_state->p1 != NULL && tanks_state->p1->live) {
  205. GameCellState cell = CellEmpty;
  206. switch(tanks_state->p1->direction) {
  207. case DirectionUp:
  208. cell = CellTankUp;
  209. break;
  210. case DirectionDown:
  211. cell = CellTankDown;
  212. break;
  213. case DirectionRight:
  214. cell = CellTankRight;
  215. break;
  216. case DirectionLeft:
  217. cell = CellTankLeft;
  218. break;
  219. default:
  220. break;
  221. }
  222. tanks_game_write_cell(
  223. result, tanks_state->p1->coordinates.x, tanks_state->p1->coordinates.y, cell);
  224. }
  225. if(tanks_state->p2 != NULL && tanks_state->p2->live) {
  226. GameCellState cell = CellEmpty;
  227. switch(tanks_state->p2->direction) {
  228. case DirectionUp:
  229. cell = CellTankUp;
  230. break;
  231. case DirectionDown:
  232. cell = CellTankDown;
  233. break;
  234. case DirectionRight:
  235. cell = CellTankRight;
  236. break;
  237. case DirectionLeft:
  238. cell = CellTankLeft;
  239. break;
  240. default:
  241. break;
  242. }
  243. tanks_game_write_cell(
  244. result, tanks_state->p2->coordinates.x, tanks_state->p2->coordinates.y, cell);
  245. }
  246. return result;
  247. }
  248. static void
  249. tanks_game_render_cell(GameCellState cell, uint8_t x, uint8_t y, Canvas* const canvas) {
  250. const Icon* icon;
  251. if(cell == CellEmpty) {
  252. return;
  253. }
  254. switch(cell) {
  255. case CellWall:
  256. icon = &I_tank_wall;
  257. break;
  258. case CellExplosion:
  259. icon = &I_tank_explosion;
  260. break;
  261. case CellTankUp:
  262. icon = &I_tank_up;
  263. break;
  264. case CellTankRight:
  265. icon = &I_tank_right;
  266. break;
  267. case CellTankDown:
  268. icon = &I_tank_down;
  269. break;
  270. case CellTankLeft:
  271. icon = &I_tank_left;
  272. break;
  273. case CellEnemyUp:
  274. icon = &I_enemy_up;
  275. break;
  276. case CellEnemyRight:
  277. icon = &I_enemy_right;
  278. break;
  279. case CellEnemyDown:
  280. icon = &I_enemy_down;
  281. break;
  282. case CellEnemyLeft:
  283. icon = &I_enemy_left;
  284. break;
  285. case CellProjectileUp:
  286. icon = &I_projectile_up;
  287. break;
  288. case CellProjectileRight:
  289. icon = &I_projectile_right;
  290. break;
  291. case CellProjectileDown:
  292. icon = &I_projectile_down;
  293. break;
  294. case CellProjectileLeft:
  295. icon = &I_projectile_left;
  296. break;
  297. default:
  298. return;
  299. break;
  300. }
  301. canvas_draw_icon(canvas, x * CELL_LENGTH_PIXELS, y * CELL_LENGTH_PIXELS - 1, icon);
  302. }
  303. static void tanks_game_render_constant_cells(Canvas* const canvas) {
  304. for(int8_t x = 0; x < FIELD_WIDTH; x++) {
  305. for(int8_t y = 0; y < FIELD_HEIGHT; y++) {
  306. char cell = thisMap[y][x];
  307. if(cell == '=') {
  308. canvas_draw_icon(
  309. canvas, x * CELL_LENGTH_PIXELS, y * CELL_LENGTH_PIXELS - 1, &I_tank_stone);
  310. continue;
  311. }
  312. if(cell == '*') {
  313. canvas_draw_icon(
  314. canvas, x * CELL_LENGTH_PIXELS, y * CELL_LENGTH_PIXELS - 1, &I_tank_hedgehog);
  315. continue;
  316. }
  317. if(cell == 'a') {
  318. canvas_draw_icon(
  319. canvas, x * CELL_LENGTH_PIXELS, y * CELL_LENGTH_PIXELS - 1, &I_tank_base);
  320. continue;
  321. }
  322. }
  323. }
  324. }
  325. void tanks_game_deserialize_and_write_to_state(unsigned char* data, TanksState* const tanks_state) {
  326. for(uint8_t i = 0; i < 11 * 16; i++) {
  327. uint8_t x = i % 16;
  328. uint8_t y = i / 16;
  329. tanks_state->thisMap[x][y] = data[i];
  330. }
  331. }
  332. void tanks_game_deserialize_and_render(unsigned char* data, Canvas* const canvas) {
  333. //for (uint8_t i = 0; i < 11 * 16 / 2; i++) {
  334. for(uint8_t i = 0; i < 11 * 16; i++) {
  335. char cell = data[i];
  336. uint8_t x = i % 16; // One line (16 cells) = 8 bytes
  337. uint8_t y = i / 16;
  338. // GameCellState first = cell >> 4;
  339. // GameCellState second = cell & 0b00001111;
  340. tanks_game_render_cell(cell, x, y, canvas);
  341. // tanks_game_render_cell(second, x + 1, y, canvas);
  342. }
  343. tanks_game_render_constant_cells(canvas);
  344. }
  345. static void tanks_game_render_callback(Canvas* const canvas, void* ctx) {
  346. furi_assert(ctx);
  347. const TanksState* tanks_state = ctx;
  348. furi_mutex_acquire(tanks_state->mutex, FuriWaitForever);
  349. // Before the function is called, the state is set with the canvas_reset(canvas)
  350. if(tanks_state->state == GameStateMenu) {
  351. canvas_draw_icon(canvas, 0, 0, &I_TanksSplashScreen_128x64);
  352. canvas_set_font(canvas, FontPrimary);
  353. canvas_draw_str_aligned(canvas, 124, 10, AlignRight, AlignBottom, "Single");
  354. canvas_draw_str_aligned(canvas, 124, 25, AlignRight, AlignBottom, "Co-op S");
  355. canvas_draw_str_aligned(canvas, 124, 40, AlignRight, AlignBottom, "Co-op C");
  356. switch(tanks_state->menu_state) {
  357. case MenuStateSingleMode:
  358. canvas_draw_icon(canvas, 74, 3, &I_tank_right);
  359. break;
  360. case MenuStateCooperativeServerMode:
  361. canvas_draw_icon(canvas, 74, 18, &I_tank_right);
  362. break;
  363. case MenuStateCooperativeClientMode:
  364. canvas_draw_icon(canvas, 74, 33, &I_tank_right);
  365. break;
  366. }
  367. canvas_draw_frame(canvas, 0, 0, 128, 64);
  368. furi_mutex_release(tanks_state->mutex);
  369. return;
  370. }
  371. // Field right border
  372. canvas_draw_box(canvas, FIELD_WIDTH * CELL_LENGTH_PIXELS, 0, 2, SCREEN_HEIGHT_TANKS);
  373. // Cooperative client
  374. if(tanks_state->mode == ModeCooperative && !tanks_state->server) {
  375. for(int8_t x = 0; x < FIELD_WIDTH; x++) {
  376. for(int8_t y = 0; y < FIELD_HEIGHT; y++) {
  377. tanks_game_render_cell(tanks_state->thisMap[x][y], x, y, canvas);
  378. }
  379. }
  380. tanks_game_render_constant_cells(canvas);
  381. furi_mutex_release(tanks_state->mutex);
  382. return;
  383. }
  384. // Player
  385. // Point coordinates = tanks_state->p1->coordinates;
  386. // const Icon *icon;
  387. // switch (tanks_state->p1->direction) {
  388. // case DirectionUp:
  389. // icon = &I_tank_up;
  390. // break;
  391. // case DirectionDown:
  392. // icon = &I_tank_down;
  393. // break;
  394. // case DirectionRight:
  395. // icon = &I_tank_right;
  396. // break;
  397. // case DirectionLeft:
  398. // icon = &I_tank_left;
  399. // break;
  400. // default:
  401. // icon = &I_tank_explosion;
  402. // }
  403. // if (tanks_state->p1->live) {
  404. // canvas_draw_icon(canvas, coordinates.x * CELL_LENGTH_PIXELS, coordinates.y * CELL_LENGTH_PIXELS - 1, icon);
  405. // }
  406. //
  407. // for(int8_t x = 0; x < FIELD_WIDTH; x++) {
  408. // for(int8_t y = 0; y < FIELD_HEIGHT; y++) {
  409. // switch (tanks_state->thisMap[x][y]) {
  410. // case '-':
  411. // canvas_draw_icon(canvas, x * CELL_LENGTH_PIXELS, y * CELL_LENGTH_PIXELS - 1, &I_tank_wall);
  412. // break;
  413. //
  414. // case '=':
  415. // canvas_draw_icon(canvas, x * CELL_LENGTH_PIXELS, y * CELL_LENGTH_PIXELS - 1, &I_tank_stone);
  416. // break;
  417. //
  418. // case '*':
  419. // canvas_draw_icon(canvas, x * CELL_LENGTH_PIXELS, y * CELL_LENGTH_PIXELS - 1, &I_tank_hedgehog);
  420. // break;
  421. //
  422. // case 'a':
  423. // canvas_draw_icon(canvas, x * CELL_LENGTH_PIXELS, y * CELL_LENGTH_PIXELS - 1, &I_tank_base);
  424. // break;
  425. // }
  426. // }
  427. // }
  428. // for (
  429. // uint8_t i = 0;
  430. // i < 6;
  431. // i++
  432. // ) {
  433. // if (tanks_state->bots[i] != NULL) {
  434. // const Icon *icon;
  435. //
  436. // switch(tanks_state->bots[i]->direction) {
  437. // case DirectionUp:
  438. // icon = &I_enemy_up;
  439. // break;
  440. // case DirectionDown:
  441. // icon = &I_enemy_down;
  442. // break;
  443. // case DirectionRight:
  444. // icon = &I_enemy_right;
  445. // break;
  446. // case DirectionLeft:
  447. // icon = &I_enemy_left;
  448. // break;
  449. // default:
  450. // icon = &I_tank_explosion;
  451. // }
  452. //
  453. // canvas_draw_icon(
  454. // canvas,
  455. // tanks_state->bots[i]->coordinates.x * CELL_LENGTH_PIXELS,
  456. // tanks_state->bots[i]->coordinates.y * CELL_LENGTH_PIXELS - 1,
  457. // icon);
  458. // }
  459. // }
  460. // for(int8_t x = 0; x < 100; x++) {
  461. // if (tanks_state->projectiles[x] != NULL) {
  462. // ProjectileState *projectile = tanks_state->projectiles[x];
  463. //
  464. // if (projectile->explosion) {
  465. // canvas_draw_icon(
  466. // canvas,
  467. // projectile->coordinates.x * CELL_LENGTH_PIXELS,
  468. // projectile->coordinates.y * CELL_LENGTH_PIXELS - 1,
  469. // &I_tank_explosion);
  470. // continue;
  471. // }
  472. //
  473. // const Icon *icon;
  474. //
  475. // switch(projectile->direction) {
  476. // case DirectionUp:
  477. // icon = &I_projectile_up;
  478. // break;
  479. // case DirectionDown:
  480. // icon = &I_projectile_down;
  481. // break;
  482. // case DirectionRight:
  483. // icon = &I_projectile_right;
  484. // break;
  485. // case DirectionLeft:
  486. // icon = &I_projectile_left;
  487. // break;
  488. // default:
  489. // icon = &I_tank_explosion;
  490. // }
  491. //
  492. // canvas_draw_icon(
  493. // canvas,
  494. // projectile->coordinates.x * CELL_LENGTH_PIXELS,
  495. // projectile->coordinates.y * CELL_LENGTH_PIXELS - 1,
  496. // icon);
  497. // }
  498. // }
  499. // Info
  500. canvas_set_color(canvas, ColorBlack);
  501. canvas_set_font(canvas, FontSecondary);
  502. char buffer1[13];
  503. snprintf(buffer1, sizeof(buffer1), "live: %u", tanks_state->enemies_live);
  504. canvas_draw_str_aligned(canvas, 127, 8, AlignRight, AlignBottom, buffer1);
  505. snprintf(buffer1, sizeof(buffer1), "left: %u", tanks_state->enemies_left);
  506. canvas_draw_str_aligned(canvas, 127, 18, AlignRight, AlignBottom, buffer1);
  507. snprintf(buffer1, sizeof(buffer1), "p1 l: %u", tanks_state->p1->lives);
  508. canvas_draw_str_aligned(canvas, 127, 28, AlignRight, AlignBottom, buffer1);
  509. snprintf(buffer1, sizeof(buffer1), "p1 s: %u", tanks_state->p1->score);
  510. canvas_draw_str_aligned(canvas, 127, 38, AlignRight, AlignBottom, buffer1);
  511. if(tanks_state->state == GameStateCooperativeServer && tanks_state->p2) {
  512. snprintf(buffer1, sizeof(buffer1), "rec: %u", tanks_state->received);
  513. canvas_draw_str_aligned(canvas, 127, 48, AlignRight, AlignBottom, buffer1);
  514. snprintf(buffer1, sizeof(buffer1), "snt: %u", tanks_state->sent);
  515. canvas_draw_str_aligned(canvas, 127, 58, AlignRight, AlignBottom, buffer1);
  516. // snprintf(buffer1, sizeof(buffer1), "p2 l: %u", tanks_state->p2->lives);
  517. // canvas_draw_str_aligned(canvas, 127, 48, AlignRight, AlignBottom, buffer1);
  518. //
  519. // snprintf(buffer1, sizeof(buffer1), "p2 s: %u", tanks_state->p2->score);
  520. // canvas_draw_str_aligned(canvas, 127, 58, AlignRight, AlignBottom, buffer1);
  521. }
  522. if(tanks_state->state == GameStateCooperativeClient) {
  523. snprintf(buffer1, sizeof(buffer1), "rec: %u", tanks_state->received);
  524. canvas_draw_str_aligned(canvas, 127, 48, AlignRight, AlignBottom, buffer1);
  525. }
  526. // Game Over banner
  527. if(tanks_state->state == GameStateGameOver) {
  528. canvas_set_color(canvas, ColorWhite);
  529. canvas_draw_box(canvas, 34, 20, 62, 24);
  530. canvas_set_color(canvas, ColorBlack);
  531. canvas_draw_frame(canvas, 34, 20, 62, 24);
  532. canvas_set_font(canvas, FontPrimary);
  533. if(tanks_state->enemies_left == 0 && tanks_state->enemies_live == 0) {
  534. canvas_draw_str(canvas, 37, 31, "You win!");
  535. } else {
  536. canvas_draw_str(canvas, 37, 31, "Game Over");
  537. }
  538. canvas_set_font(canvas, FontSecondary);
  539. char buffer[13];
  540. snprintf(buffer, sizeof(buffer), "Score: %u", tanks_state->p1->score);
  541. canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignBottom, buffer);
  542. }
  543. // TEST start
  544. unsigned char* data = tanks_game_serialize(tanks_state);
  545. tanks_game_deserialize_and_render(data, canvas);
  546. // TEST enf
  547. furi_mutex_release(tanks_state->mutex);
  548. }
  549. static void tanks_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  550. furi_assert(event_queue);
  551. TanksEvent event = {.type = EventTypeKey, .input = *input_event};
  552. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  553. }
  554. static void tanks_game_update_timer_callback(FuriMessageQueue* event_queue) {
  555. furi_assert(event_queue);
  556. TanksEvent event = {.type = EventTypeTick};
  557. furi_message_queue_put(event_queue, &event, 0);
  558. }
  559. static bool tanks_get_cell_is_free(TanksState* const tanks_state, Point point) {
  560. // Tiles
  561. if(tanks_state->thisMap[point.x][point.y] != ' ') {
  562. return false;
  563. }
  564. // Projectiles
  565. for(int8_t x = 0; x < 100; x++) {
  566. if(tanks_state->projectiles[x] != NULL) {
  567. if(tanks_state->projectiles[x]->coordinates.x == point.x &&
  568. tanks_state->projectiles[x]->coordinates.y == point.y) {
  569. return false;
  570. }
  571. }
  572. }
  573. // Player 1
  574. if(tanks_state->p1 != NULL) {
  575. if(tanks_state->p1->coordinates.x == point.x &&
  576. tanks_state->p1->coordinates.y == point.y) {
  577. return false;
  578. }
  579. }
  580. // Player 2
  581. if(tanks_state->p2 != NULL) {
  582. if(tanks_state->p2->coordinates.x == point.x &&
  583. tanks_state->p2->coordinates.y == point.y) {
  584. return false;
  585. }
  586. }
  587. // Bots
  588. for(int8_t x = 0; x < 6; x++) {
  589. if(tanks_state->bots[x] != NULL) {
  590. if(tanks_state->bots[x]->coordinates.x == point.x &&
  591. tanks_state->bots[x]->coordinates.y == point.y) {
  592. return false;
  593. }
  594. }
  595. }
  596. return true;
  597. }
  598. static uint8_t tanks_get_random_free_respawn_point_index(
  599. TanksState* const tanks_state,
  600. Point respawn_points[3]) {
  601. uint8_t first = rand() % 3;
  602. int8_t add = rand() % 2 ? +1 : -1;
  603. int8_t second = first + add;
  604. uint8_t third;
  605. if(second == 4) {
  606. second = 0;
  607. } else if(second == -1) {
  608. second = 3;
  609. }
  610. for(uint8_t i = 0; i < 3; i++) {
  611. if(i != first && i != second) {
  612. third = i;
  613. }
  614. }
  615. if(tanks_get_cell_is_free(tanks_state, respawn_points[first])) {
  616. return first;
  617. }
  618. if(tanks_get_cell_is_free(tanks_state, respawn_points[second])) {
  619. return second;
  620. }
  621. if(tanks_get_cell_is_free(tanks_state, respawn_points[third])) {
  622. return third;
  623. }
  624. return -1;
  625. }
  626. static void tanks_game_init_game(TanksState* const tanks_state, GameState type) {
  627. srand(DWT->CYCCNT);
  628. tanks_state->state = type;
  629. for(int8_t x = 0; x < 100; x++) {
  630. if(tanks_state->projectiles[x] != NULL) {
  631. free(tanks_state->projectiles[x]);
  632. tanks_state->projectiles[x] = NULL;
  633. }
  634. }
  635. int8_t team_one_respawn_points_counter = 0;
  636. int8_t team_two_respawn_points_counter = 0;
  637. for(int8_t x = 0; x < FIELD_WIDTH; x++) {
  638. for(int8_t y = 0; y < FIELD_HEIGHT; y++) {
  639. tanks_state->thisMap[x][y] = ' ';
  640. if(thisMap[y][x] == '1') {
  641. Point respawn = {x, y};
  642. tanks_state->team_one_respawn_points[team_one_respawn_points_counter++] = respawn;
  643. }
  644. if(thisMap[y][x] == '2') {
  645. Point respawn = {x, y};
  646. tanks_state->team_two_respawn_points[team_two_respawn_points_counter++] = respawn;
  647. }
  648. if(thisMap[y][x] == '-') {
  649. tanks_state->thisMap[x][y] = '-';
  650. }
  651. if(thisMap[y][x] == '=') {
  652. tanks_state->thisMap[x][y] = '=';
  653. }
  654. if(thisMap[y][x] == '*') {
  655. tanks_state->thisMap[x][y] = '*';
  656. }
  657. if(thisMap[y][x] == 'a') {
  658. tanks_state->thisMap[x][y] = 'a';
  659. }
  660. }
  661. }
  662. uint8_t index1 = tanks_get_random_free_respawn_point_index(
  663. tanks_state, tanks_state->team_one_respawn_points);
  664. Point c = {
  665. tanks_state->team_one_respawn_points[index1].x,
  666. tanks_state->team_one_respawn_points[index1].y};
  667. PlayerState p1 = {
  668. c,
  669. 0,
  670. 4,
  671. DirectionRight,
  672. 0,
  673. 0,
  674. 1,
  675. SHOT_COOLDOWN,
  676. PLAYER_RESPAWN_COOLDOWN,
  677. };
  678. PlayerState* p1_state = malloc(sizeof(PlayerState));
  679. *p1_state = p1;
  680. tanks_state->p1 = p1_state;
  681. if(type == GameStateCooperativeServer) {
  682. int8_t index2 = tanks_get_random_free_respawn_point_index(
  683. tanks_state, tanks_state->team_one_respawn_points);
  684. Point c = {
  685. tanks_state->team_one_respawn_points[index2].x,
  686. tanks_state->team_one_respawn_points[index2].y};
  687. PlayerState p2 = {
  688. c,
  689. 0,
  690. 4,
  691. DirectionRight,
  692. 0,
  693. 0,
  694. 1,
  695. SHOT_COOLDOWN,
  696. PLAYER_RESPAWN_COOLDOWN,
  697. };
  698. PlayerState* p2_state = malloc(sizeof(PlayerState));
  699. *p2_state = p2;
  700. tanks_state->p2 = p2_state;
  701. }
  702. tanks_state->enemies_left = 5;
  703. tanks_state->enemies_live = 0;
  704. tanks_state->enemies_respawn_cooldown = RESPAWN_COOLDOWN;
  705. tanks_state->received = 0;
  706. tanks_state->sent = 0;
  707. if(type == GameStateCooperativeClient) {
  708. for(int8_t x = 0; x < FIELD_WIDTH; x++) {
  709. for(int8_t y = 0; y < FIELD_HEIGHT; y++) {
  710. tanks_state->thisMap[x][y] = CellEmpty;
  711. }
  712. }
  713. }
  714. }
  715. static bool
  716. tanks_game_collision(Point const next_step, bool shoot, TanksState const* const tanks_state) {
  717. if((int8_t)next_step.x < 0 || (int8_t)next_step.y < 0) {
  718. return true;
  719. }
  720. if(next_step.x >= FIELD_WIDTH || next_step.y >= FIELD_HEIGHT) {
  721. return true;
  722. }
  723. char tile = tanks_state->thisMap[next_step.x][next_step.y];
  724. if(tile == '*' && !shoot) {
  725. return true;
  726. }
  727. if(tile == '-' || tile == '=' || tile == 'a') {
  728. return true;
  729. }
  730. for(uint8_t i = 0; i < 6; i++) {
  731. if(tanks_state->bots[i] != NULL) {
  732. if(tanks_state->bots[i]->coordinates.x == next_step.x &&
  733. tanks_state->bots[i]->coordinates.y == next_step.y) {
  734. return true;
  735. }
  736. }
  737. }
  738. if(tanks_state->p1 != NULL && tanks_state->p1->live &&
  739. tanks_state->p1->coordinates.x == next_step.x &&
  740. tanks_state->p1->coordinates.y == next_step.y) {
  741. return true;
  742. }
  743. if(tanks_state->p2 != NULL && tanks_state->p2->live &&
  744. tanks_state->p2->coordinates.x == next_step.x &&
  745. tanks_state->p2->coordinates.y == next_step.y) {
  746. return true;
  747. }
  748. return false;
  749. }
  750. static Point tanks_game_get_next_step(Point coordinates, Direction direction) {
  751. Point next_step = {coordinates.x, coordinates.y};
  752. switch(direction) {
  753. // +-----x
  754. // |
  755. // |
  756. // y
  757. case DirectionUp:
  758. next_step.y--;
  759. break;
  760. case DirectionRight:
  761. next_step.x++;
  762. break;
  763. case DirectionDown:
  764. next_step.y++;
  765. break;
  766. case DirectionLeft:
  767. next_step.x--;
  768. break;
  769. default:
  770. break;
  771. }
  772. return next_step;
  773. }
  774. static uint8_t tanks_game_get_free_projectile_index(TanksState* const tanks_state) {
  775. uint8_t freeProjectileIndex;
  776. for(freeProjectileIndex = 0; freeProjectileIndex < 100; freeProjectileIndex++) {
  777. if(tanks_state->projectiles[freeProjectileIndex] == NULL) {
  778. return freeProjectileIndex;
  779. }
  780. }
  781. return 0;
  782. }
  783. static void tanks_game_shoot(
  784. TanksState* const tanks_state,
  785. PlayerState* tank_state,
  786. bool is_p1,
  787. bool is_p2) {
  788. tank_state->cooldown = SHOT_COOLDOWN;
  789. uint8_t freeProjectileIndex = tanks_game_get_free_projectile_index(tanks_state);
  790. ProjectileState* projectile_state = malloc(sizeof(ProjectileState));
  791. Point next_step = tanks_game_get_next_step(tank_state->coordinates, tank_state->direction);
  792. projectile_state->direction = tank_state->direction;
  793. projectile_state->coordinates = next_step;
  794. projectile_state->is_p1 = is_p1;
  795. projectile_state->is_p2 = is_p2;
  796. bool crush = tanks_game_collision(projectile_state->coordinates, true, tanks_state);
  797. projectile_state->explosion = crush;
  798. tanks_state->projectiles[freeProjectileIndex] = projectile_state;
  799. }
  800. static void tanks_game_process_game_step(TanksState* const tanks_state) {
  801. if(tanks_state->state == GameStateMenu) {
  802. return;
  803. }
  804. if(tanks_state->enemies_left == 0 && tanks_state->enemies_live == 0) {
  805. tanks_state->state = GameStateGameOver;
  806. }
  807. if(!tanks_state->p1->live && tanks_state->p1->lives == 0) {
  808. tanks_state->state = GameStateGameOver;
  809. }
  810. if(tanks_state->state == GameStateGameOver) {
  811. return;
  812. }
  813. if(tanks_state->p1 != NULL) {
  814. if(!tanks_state->p1->live && tanks_state->p1->respawn_cooldown > 0) {
  815. tanks_state->p1->respawn_cooldown--;
  816. }
  817. }
  818. // Player 1 spawn
  819. if(tanks_state->p1 && !tanks_state->p1->live && tanks_state->p1->lives > 0) {
  820. int8_t index = tanks_get_random_free_respawn_point_index(
  821. tanks_state, tanks_state->team_one_respawn_points);
  822. if(index != -1) {
  823. Point point = tanks_state->team_one_respawn_points[index];
  824. Point c = {point.x, point.y};
  825. tanks_state->p1->coordinates = c;
  826. tanks_state->p1->live = true;
  827. tanks_state->p1->direction = DirectionRight;
  828. tanks_state->p1->cooldown = SHOT_COOLDOWN;
  829. tanks_state->p1->respawn_cooldown = SHOT_COOLDOWN;
  830. }
  831. }
  832. // Player 2 spawn
  833. if(tanks_state->state == GameStateCooperativeServer && tanks_state->p2 &&
  834. !tanks_state->p2->live && tanks_state->p2->lives > 0) {
  835. int8_t index = tanks_get_random_free_respawn_point_index(
  836. tanks_state, tanks_state->team_one_respawn_points);
  837. if(index != -1) {
  838. Point point = tanks_state->team_one_respawn_points[index];
  839. Point c = {point.x, point.y};
  840. tanks_state->p2->coordinates = c;
  841. tanks_state->p2->live = true;
  842. tanks_state->p2->direction = DirectionRight;
  843. tanks_state->p2->cooldown = SHOT_COOLDOWN;
  844. tanks_state->p2->respawn_cooldown = SHOT_COOLDOWN;
  845. }
  846. }
  847. // Bot turn
  848. for(uint8_t i = 0; i < 6; i++) {
  849. if(tanks_state->bots[i] != NULL) {
  850. PlayerState* bot = tanks_state->bots[i];
  851. if(bot->cooldown) {
  852. bot->cooldown--;
  853. }
  854. // Rotate
  855. if(rand() % 3 == 0) {
  856. bot->direction = (rand() % 4);
  857. }
  858. // Move
  859. if(rand() % 2 == 0) {
  860. Point next_step = tanks_game_get_next_step(bot->coordinates, bot->direction);
  861. bool crush = tanks_game_collision(next_step, false, tanks_state);
  862. if(!crush) {
  863. bot->coordinates = next_step;
  864. }
  865. }
  866. // Shoot
  867. if(bot->cooldown == 0 && rand() % 3 != 0) {
  868. tanks_game_shoot(tanks_state, bot, false, false);
  869. }
  870. }
  871. }
  872. // Bot spawn
  873. if(tanks_state->enemies_respawn_cooldown) {
  874. tanks_state->enemies_respawn_cooldown--;
  875. }
  876. if(tanks_state->enemies_left > 0 && tanks_state->enemies_live <= 4 &&
  877. tanks_state->enemies_respawn_cooldown == 0) {
  878. int8_t index = tanks_get_random_free_respawn_point_index(
  879. tanks_state, tanks_state->team_two_respawn_points);
  880. if(index != -1) {
  881. tanks_state->enemies_left--;
  882. tanks_state->enemies_live++;
  883. tanks_state->enemies_respawn_cooldown = RESPAWN_COOLDOWN;
  884. Point point = tanks_state->team_two_respawn_points[index];
  885. Point c = {point.x, point.y};
  886. PlayerState bot = {
  887. c,
  888. 0,
  889. 0,
  890. DirectionLeft,
  891. 0,
  892. 0,
  893. 1,
  894. SHOT_COOLDOWN,
  895. PLAYER_RESPAWN_COOLDOWN,
  896. };
  897. uint8_t freeEnemyIndex;
  898. for(freeEnemyIndex = 0; freeEnemyIndex < 6; freeEnemyIndex++) {
  899. if(tanks_state->bots[freeEnemyIndex] == NULL) {
  900. break;
  901. }
  902. }
  903. PlayerState* bot_state = malloc(sizeof(PlayerState));
  904. *bot_state = bot;
  905. tanks_state->bots[freeEnemyIndex] = bot_state;
  906. }
  907. }
  908. if(tanks_state->p1 != NULL && tanks_state->p1->live && tanks_state->p1->moving) {
  909. Point next_step =
  910. tanks_game_get_next_step(tanks_state->p1->coordinates, tanks_state->p1->direction);
  911. bool crush = tanks_game_collision(next_step, false, tanks_state);
  912. if(!crush) {
  913. tanks_state->p1->coordinates = next_step;
  914. }
  915. }
  916. // Player 2 spawn
  917. if(tanks_state->state == GameStateCooperativeServer && tanks_state->p2 &&
  918. tanks_state->p2->live && tanks_state->p2->moving) {
  919. Point next_step =
  920. tanks_game_get_next_step(tanks_state->p2->coordinates, tanks_state->p2->direction);
  921. bool crush = tanks_game_collision(next_step, false, tanks_state);
  922. if(!crush) {
  923. tanks_state->p2->coordinates = next_step;
  924. }
  925. }
  926. for(int8_t x = 0; x < 100; x++) {
  927. if(tanks_state->projectiles[x] != NULL) {
  928. ProjectileState* projectile = tanks_state->projectiles[x];
  929. Point c = projectile->coordinates;
  930. if(projectile->explosion) {
  931. // Break a wall
  932. if(tanks_state->thisMap[c.x][c.y] == '-') {
  933. tanks_state->thisMap[c.x][c.y] = ' ';
  934. }
  935. // Kill a bot
  936. for(uint8_t i = 0; i < 6; i++) {
  937. if(tanks_state->bots[i] != NULL) {
  938. if(tanks_state->bots[i]->coordinates.x == c.x &&
  939. tanks_state->bots[i]->coordinates.y == c.y) {
  940. if(projectile->is_p1) {
  941. tanks_state->p1->score++;
  942. }
  943. if(projectile->is_p2) {
  944. tanks_state->p2->score++;
  945. }
  946. // No friendly fire
  947. if(projectile->is_p1 || projectile->is_p2) {
  948. tanks_state->enemies_live--;
  949. free(tanks_state->bots[i]);
  950. tanks_state->bots[i] = NULL;
  951. }
  952. }
  953. }
  954. }
  955. // Destroy the flag
  956. if(tanks_state->thisMap[c.x][c.y] == 'a') {
  957. tanks_state->state = GameStateGameOver;
  958. return;
  959. }
  960. // Kill a player 1
  961. if(tanks_state->p1 != NULL) {
  962. if(tanks_state->p1->live && tanks_state->p1->coordinates.x == c.x &&
  963. tanks_state->p1->coordinates.y == c.y) {
  964. tanks_state->p1->live = false;
  965. tanks_state->p1->lives--;
  966. tanks_state->p1->respawn_cooldown = PLAYER_RESPAWN_COOLDOWN;
  967. }
  968. }
  969. // Kill a player 2
  970. if(tanks_state->p2 != NULL) {
  971. if(tanks_state->p2->live && tanks_state->p2->coordinates.x == c.x &&
  972. tanks_state->p2->coordinates.y == c.y) {
  973. tanks_state->p2->live = false;
  974. tanks_state->p2->lives--;
  975. tanks_state->p2->respawn_cooldown = PLAYER_RESPAWN_COOLDOWN;
  976. }
  977. }
  978. // Delete projectile
  979. free(tanks_state->projectiles[x]);
  980. tanks_state->projectiles[x] = NULL;
  981. continue;
  982. }
  983. Point next_step =
  984. tanks_game_get_next_step(projectile->coordinates, projectile->direction);
  985. bool crush = tanks_game_collision(next_step, true, tanks_state);
  986. projectile->coordinates = next_step;
  987. if(crush) {
  988. projectile->explosion = true;
  989. }
  990. }
  991. }
  992. if(tanks_state->p1->cooldown > 0) {
  993. tanks_state->p1->cooldown--;
  994. }
  995. if(tanks_state->p2 != NULL && tanks_state->p2->cooldown > 0) {
  996. tanks_state->p2->cooldown--;
  997. }
  998. if(tanks_state->p1 != NULL && tanks_state->p1->live && tanks_state->p1->shooting &&
  999. tanks_state->p1->cooldown == 0) {
  1000. tanks_game_shoot(tanks_state, tanks_state->p1, true, false);
  1001. }
  1002. tanks_state->p1->moving = false;
  1003. tanks_state->p1->shooting = false;
  1004. if(tanks_state->p2 != NULL) {
  1005. tanks_state->p2->moving = false;
  1006. tanks_state->p2->shooting = false;
  1007. }
  1008. }
  1009. int32_t tanks_game_app(void* p) {
  1010. UNUSED(p);
  1011. srand(DWT->CYCCNT);
  1012. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TanksEvent));
  1013. TanksState* tanks_state = malloc(sizeof(TanksState));
  1014. tanks_state->state = GameStateMenu;
  1015. tanks_state->menu_state = MenuStateSingleMode;
  1016. tanks_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  1017. if(!tanks_state->mutex) {
  1018. FURI_LOG_E("Tanks", "cannot create mutex\r\n");
  1019. furi_message_queue_free(event_queue);
  1020. free(tanks_state);
  1021. return 255;
  1022. }
  1023. ViewPort* view_port = view_port_alloc();
  1024. view_port_draw_callback_set(view_port, tanks_game_render_callback, tanks_state);
  1025. view_port_input_callback_set(view_port, tanks_game_input_callback, event_queue);
  1026. FuriTimer* timer =
  1027. furi_timer_alloc(tanks_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  1028. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 4);
  1029. // Open GUI and register view_port
  1030. Gui* gui = furi_record_open(RECORD_GUI);
  1031. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  1032. TanksEvent event;
  1033. // Initialize network thing.
  1034. uint32_t frequency = 433920000;
  1035. size_t message_max_len = 180;
  1036. uint8_t incomingMessage[180] = {0};
  1037. SubGhzTxRxWorker* subghz_txrx = subghz_tx_rx_worker_alloc();
  1038. subghz_devices_init();
  1039. const SubGhzDevice* subghz_device =
  1040. radio_device_loader_set(NULL, SubGhzRadioDeviceTypeExternalCC1101);
  1041. subghz_devices_reset(subghz_device);
  1042. subghz_devices_load_preset(subghz_device, FuriHalSubGhzPresetOok650Async, NULL);
  1043. subghz_tx_rx_worker_start(subghz_txrx, subghz_device, frequency);
  1044. furi_hal_power_suppress_charge_enter();
  1045. for(bool processing = true; processing;) {
  1046. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  1047. furi_mutex_acquire(tanks_state->mutex, FuriWaitForever);
  1048. if(event_status == FuriStatusOk) {
  1049. // press events
  1050. if(event.type == EventTypeKey) {
  1051. if(event.input.type == InputTypePress) {
  1052. switch(event.input.key) {
  1053. case InputKeyUp:
  1054. if(tanks_state->state == GameStateMenu) {
  1055. if(tanks_state->menu_state == MenuStateCooperativeServerMode) {
  1056. tanks_state->menu_state = MenuStateSingleMode;
  1057. } else if(tanks_state->menu_state == MenuStateCooperativeClientMode) {
  1058. tanks_state->menu_state = MenuStateCooperativeServerMode;
  1059. }
  1060. } else if(tanks_state->state == GameStateCooperativeClient) {
  1061. FuriString* goesUp = furi_string_alloc();
  1062. char arr[2];
  1063. arr[0] = GoesUp;
  1064. arr[1] = 0;
  1065. furi_string_set(goesUp, (char*)&arr);
  1066. subghz_tx_rx_worker_write(
  1067. subghz_txrx,
  1068. (uint8_t*)furi_string_get_cstr(goesUp),
  1069. strlen(furi_string_get_cstr(goesUp)));
  1070. furi_string_free(goesUp);
  1071. } else {
  1072. tanks_state->p1->moving = true;
  1073. tanks_state->p1->direction = DirectionUp;
  1074. }
  1075. break;
  1076. case InputKeyDown:
  1077. if(tanks_state->state == GameStateMenu) {
  1078. if(tanks_state->menu_state == MenuStateSingleMode) {
  1079. tanks_state->menu_state = MenuStateCooperativeServerMode;
  1080. } else if(tanks_state->menu_state == MenuStateCooperativeServerMode) {
  1081. tanks_state->menu_state = MenuStateCooperativeClientMode;
  1082. }
  1083. } else if(tanks_state->state == GameStateCooperativeClient) {
  1084. FuriString* goesDown = furi_string_alloc();
  1085. char arr[2];
  1086. arr[0] = GoesDown;
  1087. arr[1] = 0;
  1088. furi_string_set(goesDown, (char*)&arr);
  1089. subghz_tx_rx_worker_write(
  1090. subghz_txrx,
  1091. (uint8_t*)furi_string_get_cstr(goesDown),
  1092. strlen(furi_string_get_cstr(goesDown)));
  1093. furi_string_free(goesDown);
  1094. } else {
  1095. tanks_state->p1->moving = true;
  1096. tanks_state->p1->direction = DirectionDown;
  1097. }
  1098. break;
  1099. case InputKeyRight:
  1100. if(!(tanks_state->state == GameStateMenu)) {
  1101. if(tanks_state->state == GameStateCooperativeClient) {
  1102. FuriString* goesRight = furi_string_alloc();
  1103. char arr[2];
  1104. arr[0] = GoesRight;
  1105. arr[1] = 0;
  1106. furi_string_set(goesRight, (char*)&arr);
  1107. subghz_tx_rx_worker_write(
  1108. subghz_txrx,
  1109. (uint8_t*)furi_string_get_cstr(goesRight),
  1110. strlen(furi_string_get_cstr(goesRight)));
  1111. furi_string_free(goesRight);
  1112. } else {
  1113. tanks_state->p1->moving = true;
  1114. tanks_state->p1->direction = DirectionRight;
  1115. }
  1116. }
  1117. break;
  1118. case InputKeyLeft:
  1119. if(!(tanks_state->state == GameStateMenu)) {
  1120. if(tanks_state->state == GameStateCooperativeClient) {
  1121. FuriString* goesLeft = furi_string_alloc();
  1122. char arr[2];
  1123. arr[0] = GoesLeft;
  1124. arr[1] = 0;
  1125. furi_string_set(goesLeft, (char*)&arr);
  1126. subghz_tx_rx_worker_write(
  1127. subghz_txrx,
  1128. (uint8_t*)furi_string_get_cstr(goesLeft),
  1129. strlen(furi_string_get_cstr(goesLeft)));
  1130. furi_string_free(goesLeft);
  1131. } else {
  1132. tanks_state->p1->moving = true;
  1133. tanks_state->p1->direction = DirectionLeft;
  1134. }
  1135. }
  1136. break;
  1137. case InputKeyOk:
  1138. if(tanks_state->state == GameStateMenu) {
  1139. if(tanks_state->menu_state == MenuStateSingleMode) {
  1140. tanks_state->server = true;
  1141. tanks_game_init_game(tanks_state, GameStateSingle);
  1142. break;
  1143. } else if(tanks_state->menu_state == MenuStateCooperativeServerMode) {
  1144. tanks_state->server = true;
  1145. tanks_game_init_game(tanks_state, GameStateCooperativeServer);
  1146. break;
  1147. } else if(tanks_state->menu_state == MenuStateCooperativeClientMode) {
  1148. tanks_state->server = false;
  1149. tanks_game_init_game(tanks_state, GameStateCooperativeClient);
  1150. break;
  1151. }
  1152. } else if(tanks_state->state == GameStateGameOver) {
  1153. tanks_game_init_game(tanks_state, tanks_state->state);
  1154. } else if(tanks_state->state == GameStateCooperativeClient) {
  1155. FuriString* shoots = furi_string_alloc();
  1156. char arr[2];
  1157. arr[0] = Shoots;
  1158. arr[1] = 0;
  1159. furi_string_set(shoots, (char*)&arr);
  1160. subghz_tx_rx_worker_write(
  1161. subghz_txrx,
  1162. (uint8_t*)furi_string_get_cstr(shoots),
  1163. strlen(furi_string_get_cstr(shoots)));
  1164. furi_string_free(shoots);
  1165. } else {
  1166. tanks_state->p1->shooting = true;
  1167. }
  1168. break;
  1169. case InputKeyBack:
  1170. processing = false;
  1171. break;
  1172. default:
  1173. break;
  1174. }
  1175. }
  1176. } else if(event.type == EventTypeTick) {
  1177. if(tanks_state->state == GameStateCooperativeServer) {
  1178. if(subghz_tx_rx_worker_available(subghz_txrx)) {
  1179. memset(incomingMessage, 0x00, message_max_len);
  1180. subghz_tx_rx_worker_read(subghz_txrx, incomingMessage, message_max_len);
  1181. if(incomingMessage != NULL) {
  1182. tanks_state->received++;
  1183. switch(incomingMessage[0]) {
  1184. case GoesUp:
  1185. tanks_state->p2->moving = true;
  1186. tanks_state->p2->direction = DirectionUp;
  1187. break;
  1188. case GoesRight:
  1189. tanks_state->p2->moving = true;
  1190. tanks_state->p2->direction = DirectionRight;
  1191. break;
  1192. case GoesDown:
  1193. tanks_state->p2->moving = true;
  1194. tanks_state->p2->direction = DirectionDown;
  1195. break;
  1196. case GoesLeft:
  1197. tanks_state->p2->moving = true;
  1198. tanks_state->p2->direction = DirectionLeft;
  1199. break;
  1200. case Shoots:
  1201. tanks_state->p2->shooting = true;
  1202. break;
  1203. default:
  1204. break;
  1205. }
  1206. }
  1207. }
  1208. tanks_game_process_game_step(tanks_state);
  1209. FuriString* serializedData = furi_string_alloc();
  1210. unsigned char* data = tanks_game_serialize(tanks_state);
  1211. char arr[11 * 16 + 1];
  1212. for(uint8_t i = 0; i < 11 * 16; i++) {
  1213. arr[i] = data[i];
  1214. }
  1215. arr[11 * 16] = 0;
  1216. furi_string_set(serializedData, (char*)&arr);
  1217. subghz_tx_rx_worker_write(
  1218. subghz_txrx,
  1219. (uint8_t*)furi_string_get_cstr(serializedData),
  1220. strlen(furi_string_get_cstr(serializedData)));
  1221. furi_string_free(serializedData);
  1222. tanks_state->sent++;
  1223. } else if(tanks_state->state == GameStateSingle) {
  1224. tanks_game_process_game_step(tanks_state);
  1225. } else if(tanks_state->state == GameStateCooperativeClient) {
  1226. if(subghz_tx_rx_worker_available(subghz_txrx)) {
  1227. memset(incomingMessage, 0x00, message_max_len);
  1228. subghz_tx_rx_worker_read(subghz_txrx, incomingMessage, message_max_len);
  1229. tanks_state->received++;
  1230. tanks_game_deserialize_and_write_to_state(
  1231. (unsigned char*)incomingMessage, tanks_state);
  1232. }
  1233. }
  1234. }
  1235. } else {
  1236. // event timeout
  1237. }
  1238. furi_mutex_release(tanks_state->mutex);
  1239. view_port_update(view_port);
  1240. furi_delay_ms(1);
  1241. }
  1242. furi_delay_ms(10);
  1243. furi_hal_power_suppress_charge_exit();
  1244. if(subghz_tx_rx_worker_is_running(subghz_txrx)) {
  1245. subghz_tx_rx_worker_stop(subghz_txrx);
  1246. subghz_tx_rx_worker_free(subghz_txrx);
  1247. }
  1248. subghz_devices_deinit();
  1249. furi_timer_free(timer);
  1250. view_port_enabled_set(view_port, false);
  1251. gui_remove_view_port(gui, view_port);
  1252. furi_record_close(RECORD_GUI);
  1253. view_port_free(view_port);
  1254. furi_message_queue_free(event_queue);
  1255. furi_mutex_free(tanks_state->mutex);
  1256. if(tanks_state->p1 != NULL) {
  1257. free(tanks_state->p1);
  1258. }
  1259. if(tanks_state->p2 != NULL) {
  1260. free(tanks_state->p2);
  1261. }
  1262. free(tanks_state);
  1263. return 0;
  1264. }