tanks_game.c 47 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, void* ctx) {
  550. furi_assert(ctx);
  551. FuriMessageQueue* event_queue = ctx;
  552. TanksEvent event = {.type = EventTypeKey, .input = *input_event};
  553. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  554. }
  555. static void tanks_game_update_timer_callback(void* ctx) {
  556. furi_assert(ctx);
  557. FuriMessageQueue* event_queue = ctx;
  558. TanksEvent event = {.type = EventTypeTick};
  559. furi_message_queue_put(event_queue, &event, 0);
  560. }
  561. static bool tanks_get_cell_is_free(TanksState* const tanks_state, Point point) {
  562. // Tiles
  563. if(tanks_state->thisMap[point.x][point.y] != ' ') {
  564. return false;
  565. }
  566. // Projectiles
  567. for(int8_t x = 0; x < 100; x++) {
  568. if(tanks_state->projectiles[x] != NULL) {
  569. if(tanks_state->projectiles[x]->coordinates.x == point.x &&
  570. tanks_state->projectiles[x]->coordinates.y == point.y) {
  571. return false;
  572. }
  573. }
  574. }
  575. // Player 1
  576. if(tanks_state->p1 != NULL) {
  577. if(tanks_state->p1->coordinates.x == point.x &&
  578. tanks_state->p1->coordinates.y == point.y) {
  579. return false;
  580. }
  581. }
  582. // Player 2
  583. if(tanks_state->p2 != NULL) {
  584. if(tanks_state->p2->coordinates.x == point.x &&
  585. tanks_state->p2->coordinates.y == point.y) {
  586. return false;
  587. }
  588. }
  589. // Bots
  590. for(int8_t x = 0; x < 6; x++) {
  591. if(tanks_state->bots[x] != NULL) {
  592. if(tanks_state->bots[x]->coordinates.x == point.x &&
  593. tanks_state->bots[x]->coordinates.y == point.y) {
  594. return false;
  595. }
  596. }
  597. }
  598. return true;
  599. }
  600. static uint8_t tanks_get_random_free_respawn_point_index(
  601. TanksState* const tanks_state,
  602. Point respawn_points[3]) {
  603. uint8_t first = rand() % 3;
  604. int8_t add = rand() % 2 ? +1 : -1;
  605. int8_t second = first + add;
  606. uint8_t third;
  607. if(second == 4) {
  608. second = 0;
  609. } else if(second == -1) {
  610. second = 3;
  611. }
  612. for(uint8_t i = 0; i < 3; i++) {
  613. if(i != first && i != second) {
  614. third = i;
  615. }
  616. }
  617. if(tanks_get_cell_is_free(tanks_state, respawn_points[first])) {
  618. return first;
  619. }
  620. if(tanks_get_cell_is_free(tanks_state, respawn_points[second])) {
  621. return second;
  622. }
  623. if(tanks_get_cell_is_free(tanks_state, respawn_points[third])) {
  624. return third;
  625. }
  626. return -1;
  627. }
  628. static void tanks_game_init_game(TanksState* const tanks_state, GameState type) {
  629. srand(DWT->CYCCNT);
  630. tanks_state->state = type;
  631. for(int8_t x = 0; x < 100; x++) {
  632. if(tanks_state->projectiles[x] != NULL) {
  633. free(tanks_state->projectiles[x]);
  634. tanks_state->projectiles[x] = NULL;
  635. }
  636. }
  637. int8_t team_one_respawn_points_counter = 0;
  638. int8_t team_two_respawn_points_counter = 0;
  639. for(int8_t x = 0; x < FIELD_WIDTH; x++) {
  640. for(int8_t y = 0; y < FIELD_HEIGHT; y++) {
  641. tanks_state->thisMap[x][y] = ' ';
  642. if(thisMap[y][x] == '1') {
  643. Point respawn = {x, y};
  644. tanks_state->team_one_respawn_points[team_one_respawn_points_counter++] = respawn;
  645. }
  646. if(thisMap[y][x] == '2') {
  647. Point respawn = {x, y};
  648. tanks_state->team_two_respawn_points[team_two_respawn_points_counter++] = respawn;
  649. }
  650. if(thisMap[y][x] == '-') {
  651. tanks_state->thisMap[x][y] = '-';
  652. }
  653. if(thisMap[y][x] == '=') {
  654. tanks_state->thisMap[x][y] = '=';
  655. }
  656. if(thisMap[y][x] == '*') {
  657. tanks_state->thisMap[x][y] = '*';
  658. }
  659. if(thisMap[y][x] == 'a') {
  660. tanks_state->thisMap[x][y] = 'a';
  661. }
  662. }
  663. }
  664. uint8_t index1 = tanks_get_random_free_respawn_point_index(
  665. tanks_state, tanks_state->team_one_respawn_points);
  666. Point c = {
  667. tanks_state->team_one_respawn_points[index1].x,
  668. tanks_state->team_one_respawn_points[index1].y};
  669. PlayerState p1 = {
  670. c,
  671. 0,
  672. 4,
  673. DirectionRight,
  674. 0,
  675. 0,
  676. 1,
  677. SHOT_COOLDOWN,
  678. PLAYER_RESPAWN_COOLDOWN,
  679. };
  680. PlayerState* p1_state = malloc(sizeof(PlayerState));
  681. *p1_state = p1;
  682. tanks_state->p1 = p1_state;
  683. if(type == GameStateCooperativeServer) {
  684. int8_t index2 = tanks_get_random_free_respawn_point_index(
  685. tanks_state, tanks_state->team_one_respawn_points);
  686. Point c = {
  687. tanks_state->team_one_respawn_points[index2].x,
  688. tanks_state->team_one_respawn_points[index2].y};
  689. PlayerState p2 = {
  690. c,
  691. 0,
  692. 4,
  693. DirectionRight,
  694. 0,
  695. 0,
  696. 1,
  697. SHOT_COOLDOWN,
  698. PLAYER_RESPAWN_COOLDOWN,
  699. };
  700. PlayerState* p2_state = malloc(sizeof(PlayerState));
  701. *p2_state = p2;
  702. tanks_state->p2 = p2_state;
  703. }
  704. tanks_state->enemies_left = 5;
  705. tanks_state->enemies_live = 0;
  706. tanks_state->enemies_respawn_cooldown = RESPAWN_COOLDOWN;
  707. tanks_state->received = 0;
  708. tanks_state->sent = 0;
  709. if(type == GameStateCooperativeClient) {
  710. for(int8_t x = 0; x < FIELD_WIDTH; x++) {
  711. for(int8_t y = 0; y < FIELD_HEIGHT; y++) {
  712. tanks_state->thisMap[x][y] = CellEmpty;
  713. }
  714. }
  715. }
  716. }
  717. static bool
  718. tanks_game_collision(Point const next_step, bool shoot, TanksState const* const tanks_state) {
  719. if((int8_t)next_step.x < 0 || (int8_t)next_step.y < 0) {
  720. return true;
  721. }
  722. if(next_step.x >= FIELD_WIDTH || next_step.y >= FIELD_HEIGHT) {
  723. return true;
  724. }
  725. char tile = tanks_state->thisMap[next_step.x][next_step.y];
  726. if(tile == '*' && !shoot) {
  727. return true;
  728. }
  729. if(tile == '-' || tile == '=' || tile == 'a') {
  730. return true;
  731. }
  732. for(uint8_t i = 0; i < 6; i++) {
  733. if(tanks_state->bots[i] != NULL) {
  734. if(tanks_state->bots[i]->coordinates.x == next_step.x &&
  735. tanks_state->bots[i]->coordinates.y == next_step.y) {
  736. return true;
  737. }
  738. }
  739. }
  740. if(tanks_state->p1 != NULL && tanks_state->p1->live &&
  741. tanks_state->p1->coordinates.x == next_step.x &&
  742. tanks_state->p1->coordinates.y == next_step.y) {
  743. return true;
  744. }
  745. if(tanks_state->p2 != NULL && tanks_state->p2->live &&
  746. tanks_state->p2->coordinates.x == next_step.x &&
  747. tanks_state->p2->coordinates.y == next_step.y) {
  748. return true;
  749. }
  750. return false;
  751. }
  752. static Point tanks_game_get_next_step(Point coordinates, Direction direction) {
  753. Point next_step = {coordinates.x, coordinates.y};
  754. switch(direction) {
  755. // +-----x
  756. // |
  757. // |
  758. // y
  759. case DirectionUp:
  760. next_step.y--;
  761. break;
  762. case DirectionRight:
  763. next_step.x++;
  764. break;
  765. case DirectionDown:
  766. next_step.y++;
  767. break;
  768. case DirectionLeft:
  769. next_step.x--;
  770. break;
  771. default:
  772. break;
  773. }
  774. return next_step;
  775. }
  776. static uint8_t tanks_game_get_free_projectile_index(TanksState* const tanks_state) {
  777. uint8_t freeProjectileIndex;
  778. for(freeProjectileIndex = 0; freeProjectileIndex < 100; freeProjectileIndex++) {
  779. if(tanks_state->projectiles[freeProjectileIndex] == NULL) {
  780. return freeProjectileIndex;
  781. }
  782. }
  783. return 0;
  784. }
  785. static void tanks_game_shoot(
  786. TanksState* const tanks_state,
  787. PlayerState* tank_state,
  788. bool is_p1,
  789. bool is_p2) {
  790. tank_state->cooldown = SHOT_COOLDOWN;
  791. uint8_t freeProjectileIndex = tanks_game_get_free_projectile_index(tanks_state);
  792. ProjectileState* projectile_state = malloc(sizeof(ProjectileState));
  793. Point next_step = tanks_game_get_next_step(tank_state->coordinates, tank_state->direction);
  794. projectile_state->direction = tank_state->direction;
  795. projectile_state->coordinates = next_step;
  796. projectile_state->is_p1 = is_p1;
  797. projectile_state->is_p2 = is_p2;
  798. bool crush = tanks_game_collision(projectile_state->coordinates, true, tanks_state);
  799. projectile_state->explosion = crush;
  800. tanks_state->projectiles[freeProjectileIndex] = projectile_state;
  801. }
  802. static void tanks_game_process_game_step(TanksState* const tanks_state) {
  803. if(tanks_state->state == GameStateMenu) {
  804. return;
  805. }
  806. if(tanks_state->enemies_left == 0 && tanks_state->enemies_live == 0) {
  807. tanks_state->state = GameStateGameOver;
  808. }
  809. if(!tanks_state->p1->live && tanks_state->p1->lives == 0) {
  810. tanks_state->state = GameStateGameOver;
  811. }
  812. if(tanks_state->state == GameStateGameOver) {
  813. return;
  814. }
  815. if(tanks_state->p1 != NULL) {
  816. if(!tanks_state->p1->live && tanks_state->p1->respawn_cooldown > 0) {
  817. tanks_state->p1->respawn_cooldown--;
  818. }
  819. }
  820. // Player 1 spawn
  821. if(tanks_state->p1 && !tanks_state->p1->live && tanks_state->p1->lives > 0) {
  822. int8_t index = tanks_get_random_free_respawn_point_index(
  823. tanks_state, tanks_state->team_one_respawn_points);
  824. if(index != -1) {
  825. Point point = tanks_state->team_one_respawn_points[index];
  826. Point c = {point.x, point.y};
  827. tanks_state->p1->coordinates = c;
  828. tanks_state->p1->live = true;
  829. tanks_state->p1->direction = DirectionRight;
  830. tanks_state->p1->cooldown = SHOT_COOLDOWN;
  831. tanks_state->p1->respawn_cooldown = SHOT_COOLDOWN;
  832. }
  833. }
  834. // Player 2 spawn
  835. if(tanks_state->state == GameStateCooperativeServer && tanks_state->p2 &&
  836. !tanks_state->p2->live && tanks_state->p2->lives > 0) {
  837. int8_t index = tanks_get_random_free_respawn_point_index(
  838. tanks_state, tanks_state->team_one_respawn_points);
  839. if(index != -1) {
  840. Point point = tanks_state->team_one_respawn_points[index];
  841. Point c = {point.x, point.y};
  842. tanks_state->p2->coordinates = c;
  843. tanks_state->p2->live = true;
  844. tanks_state->p2->direction = DirectionRight;
  845. tanks_state->p2->cooldown = SHOT_COOLDOWN;
  846. tanks_state->p2->respawn_cooldown = SHOT_COOLDOWN;
  847. }
  848. }
  849. // Bot turn
  850. for(uint8_t i = 0; i < 6; i++) {
  851. if(tanks_state->bots[i] != NULL) {
  852. PlayerState* bot = tanks_state->bots[i];
  853. if(bot->cooldown) {
  854. bot->cooldown--;
  855. }
  856. // Rotate
  857. if(rand() % 3 == 0) {
  858. bot->direction = (rand() % 4);
  859. }
  860. // Move
  861. if(rand() % 2 == 0) {
  862. Point next_step = tanks_game_get_next_step(bot->coordinates, bot->direction);
  863. bool crush = tanks_game_collision(next_step, false, tanks_state);
  864. if(!crush) {
  865. bot->coordinates = next_step;
  866. }
  867. }
  868. // Shoot
  869. if(bot->cooldown == 0 && rand() % 3 != 0) {
  870. tanks_game_shoot(tanks_state, bot, false, false);
  871. }
  872. }
  873. }
  874. // Bot spawn
  875. if(tanks_state->enemies_respawn_cooldown) {
  876. tanks_state->enemies_respawn_cooldown--;
  877. }
  878. if(tanks_state->enemies_left > 0 && tanks_state->enemies_live <= 4 &&
  879. tanks_state->enemies_respawn_cooldown == 0) {
  880. int8_t index = tanks_get_random_free_respawn_point_index(
  881. tanks_state, tanks_state->team_two_respawn_points);
  882. if(index != -1) {
  883. tanks_state->enemies_left--;
  884. tanks_state->enemies_live++;
  885. tanks_state->enemies_respawn_cooldown = RESPAWN_COOLDOWN;
  886. Point point = tanks_state->team_two_respawn_points[index];
  887. Point c = {point.x, point.y};
  888. PlayerState bot = {
  889. c,
  890. 0,
  891. 0,
  892. DirectionLeft,
  893. 0,
  894. 0,
  895. 1,
  896. SHOT_COOLDOWN,
  897. PLAYER_RESPAWN_COOLDOWN,
  898. };
  899. uint8_t freeEnemyIndex;
  900. for(freeEnemyIndex = 0; freeEnemyIndex < 6; freeEnemyIndex++) {
  901. if(tanks_state->bots[freeEnemyIndex] == NULL) {
  902. break;
  903. }
  904. }
  905. PlayerState* bot_state = malloc(sizeof(PlayerState));
  906. *bot_state = bot;
  907. tanks_state->bots[freeEnemyIndex] = bot_state;
  908. }
  909. }
  910. if(tanks_state->p1 != NULL && tanks_state->p1->live && tanks_state->p1->moving) {
  911. Point next_step =
  912. tanks_game_get_next_step(tanks_state->p1->coordinates, tanks_state->p1->direction);
  913. bool crush = tanks_game_collision(next_step, false, tanks_state);
  914. if(!crush) {
  915. tanks_state->p1->coordinates = next_step;
  916. }
  917. }
  918. // Player 2 spawn
  919. if(tanks_state->state == GameStateCooperativeServer && tanks_state->p2 &&
  920. tanks_state->p2->live && tanks_state->p2->moving) {
  921. Point next_step =
  922. tanks_game_get_next_step(tanks_state->p2->coordinates, tanks_state->p2->direction);
  923. bool crush = tanks_game_collision(next_step, false, tanks_state);
  924. if(!crush) {
  925. tanks_state->p2->coordinates = next_step;
  926. }
  927. }
  928. for(int8_t x = 0; x < 100; x++) {
  929. if(tanks_state->projectiles[x] != NULL) {
  930. ProjectileState* projectile = tanks_state->projectiles[x];
  931. Point c = projectile->coordinates;
  932. if(projectile->explosion) {
  933. // Break a wall
  934. if(tanks_state->thisMap[c.x][c.y] == '-') {
  935. tanks_state->thisMap[c.x][c.y] = ' ';
  936. }
  937. // Kill a bot
  938. for(uint8_t i = 0; i < 6; i++) {
  939. if(tanks_state->bots[i] != NULL) {
  940. if(tanks_state->bots[i]->coordinates.x == c.x &&
  941. tanks_state->bots[i]->coordinates.y == c.y) {
  942. if(projectile->is_p1) {
  943. tanks_state->p1->score++;
  944. }
  945. if(projectile->is_p2) {
  946. tanks_state->p2->score++;
  947. }
  948. // No friendly fire
  949. if(projectile->is_p1 || projectile->is_p2) {
  950. tanks_state->enemies_live--;
  951. free(tanks_state->bots[i]);
  952. tanks_state->bots[i] = NULL;
  953. }
  954. }
  955. }
  956. }
  957. // Destroy the flag
  958. if(tanks_state->thisMap[c.x][c.y] == 'a') {
  959. tanks_state->state = GameStateGameOver;
  960. return;
  961. }
  962. // Kill a player 1
  963. if(tanks_state->p1 != NULL) {
  964. if(tanks_state->p1->live && tanks_state->p1->coordinates.x == c.x &&
  965. tanks_state->p1->coordinates.y == c.y) {
  966. tanks_state->p1->live = false;
  967. tanks_state->p1->lives--;
  968. tanks_state->p1->respawn_cooldown = PLAYER_RESPAWN_COOLDOWN;
  969. }
  970. }
  971. // Kill a player 2
  972. if(tanks_state->p2 != NULL) {
  973. if(tanks_state->p2->live && tanks_state->p2->coordinates.x == c.x &&
  974. tanks_state->p2->coordinates.y == c.y) {
  975. tanks_state->p2->live = false;
  976. tanks_state->p2->lives--;
  977. tanks_state->p2->respawn_cooldown = PLAYER_RESPAWN_COOLDOWN;
  978. }
  979. }
  980. // Delete projectile
  981. free(tanks_state->projectiles[x]);
  982. tanks_state->projectiles[x] = NULL;
  983. continue;
  984. }
  985. Point next_step =
  986. tanks_game_get_next_step(projectile->coordinates, projectile->direction);
  987. bool crush = tanks_game_collision(next_step, true, tanks_state);
  988. projectile->coordinates = next_step;
  989. if(crush) {
  990. projectile->explosion = true;
  991. }
  992. }
  993. }
  994. if(tanks_state->p1->cooldown > 0) {
  995. tanks_state->p1->cooldown--;
  996. }
  997. if(tanks_state->p2 != NULL && tanks_state->p2->cooldown > 0) {
  998. tanks_state->p2->cooldown--;
  999. }
  1000. if(tanks_state->p1 != NULL && tanks_state->p1->live && tanks_state->p1->shooting &&
  1001. tanks_state->p1->cooldown == 0) {
  1002. tanks_game_shoot(tanks_state, tanks_state->p1, true, false);
  1003. }
  1004. tanks_state->p1->moving = false;
  1005. tanks_state->p1->shooting = false;
  1006. if(tanks_state->p2 != NULL) {
  1007. tanks_state->p2->moving = false;
  1008. tanks_state->p2->shooting = false;
  1009. }
  1010. }
  1011. int32_t tanks_game_app(void* p) {
  1012. UNUSED(p);
  1013. srand(DWT->CYCCNT);
  1014. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TanksEvent));
  1015. TanksState* tanks_state = malloc(sizeof(TanksState));
  1016. tanks_state->state = GameStateMenu;
  1017. tanks_state->menu_state = MenuStateSingleMode;
  1018. tanks_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  1019. if(!tanks_state->mutex) {
  1020. FURI_LOG_E("Tanks", "cannot create mutex\r\n");
  1021. furi_message_queue_free(event_queue);
  1022. free(tanks_state);
  1023. return 255;
  1024. }
  1025. ViewPort* view_port = view_port_alloc();
  1026. view_port_draw_callback_set(view_port, tanks_game_render_callback, tanks_state);
  1027. view_port_input_callback_set(view_port, tanks_game_input_callback, event_queue);
  1028. FuriTimer* timer =
  1029. furi_timer_alloc(tanks_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  1030. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 4);
  1031. // Open GUI and register view_port
  1032. Gui* gui = furi_record_open(RECORD_GUI);
  1033. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  1034. TanksEvent event;
  1035. // Initialize network thing.
  1036. uint32_t frequency = 433920000;
  1037. size_t message_max_len = 180;
  1038. uint8_t incomingMessage[180] = {0};
  1039. SubGhzTxRxWorker* subghz_txrx = subghz_tx_rx_worker_alloc();
  1040. subghz_devices_init();
  1041. const SubGhzDevice* subghz_device =
  1042. radio_device_loader_set(NULL, SubGhzRadioDeviceTypeExternalCC1101);
  1043. subghz_devices_reset(subghz_device);
  1044. subghz_devices_load_preset(subghz_device, FuriHalSubGhzPresetOok650Async, NULL);
  1045. subghz_tx_rx_worker_start(subghz_txrx, subghz_device, frequency);
  1046. furi_hal_power_suppress_charge_enter();
  1047. for(bool processing = true; processing;) {
  1048. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  1049. furi_mutex_acquire(tanks_state->mutex, FuriWaitForever);
  1050. if(event_status == FuriStatusOk) {
  1051. // press events
  1052. if(event.type == EventTypeKey) {
  1053. if(event.input.type == InputTypePress) {
  1054. switch(event.input.key) {
  1055. case InputKeyUp:
  1056. if(tanks_state->state == GameStateMenu) {
  1057. if(tanks_state->menu_state == MenuStateCooperativeServerMode) {
  1058. tanks_state->menu_state = MenuStateSingleMode;
  1059. } else if(tanks_state->menu_state == MenuStateCooperativeClientMode) {
  1060. tanks_state->menu_state = MenuStateCooperativeServerMode;
  1061. }
  1062. } else if(tanks_state->state == GameStateCooperativeClient) {
  1063. FuriString* goesUp = furi_string_alloc();
  1064. char arr[2];
  1065. arr[0] = GoesUp;
  1066. arr[1] = 0;
  1067. furi_string_set(goesUp, (char*)&arr);
  1068. subghz_tx_rx_worker_write(
  1069. subghz_txrx,
  1070. (uint8_t*)furi_string_get_cstr(goesUp),
  1071. strlen(furi_string_get_cstr(goesUp)));
  1072. furi_string_free(goesUp);
  1073. } else {
  1074. tanks_state->p1->moving = true;
  1075. tanks_state->p1->direction = DirectionUp;
  1076. }
  1077. break;
  1078. case InputKeyDown:
  1079. if(tanks_state->state == GameStateMenu) {
  1080. if(tanks_state->menu_state == MenuStateSingleMode) {
  1081. tanks_state->menu_state = MenuStateCooperativeServerMode;
  1082. } else if(tanks_state->menu_state == MenuStateCooperativeServerMode) {
  1083. tanks_state->menu_state = MenuStateCooperativeClientMode;
  1084. }
  1085. } else if(tanks_state->state == GameStateCooperativeClient) {
  1086. FuriString* goesDown = furi_string_alloc();
  1087. char arr[2];
  1088. arr[0] = GoesDown;
  1089. arr[1] = 0;
  1090. furi_string_set(goesDown, (char*)&arr);
  1091. subghz_tx_rx_worker_write(
  1092. subghz_txrx,
  1093. (uint8_t*)furi_string_get_cstr(goesDown),
  1094. strlen(furi_string_get_cstr(goesDown)));
  1095. furi_string_free(goesDown);
  1096. } else {
  1097. tanks_state->p1->moving = true;
  1098. tanks_state->p1->direction = DirectionDown;
  1099. }
  1100. break;
  1101. case InputKeyRight:
  1102. if(!(tanks_state->state == GameStateMenu)) {
  1103. if(tanks_state->state == GameStateCooperativeClient) {
  1104. FuriString* goesRight = furi_string_alloc();
  1105. char arr[2];
  1106. arr[0] = GoesRight;
  1107. arr[1] = 0;
  1108. furi_string_set(goesRight, (char*)&arr);
  1109. subghz_tx_rx_worker_write(
  1110. subghz_txrx,
  1111. (uint8_t*)furi_string_get_cstr(goesRight),
  1112. strlen(furi_string_get_cstr(goesRight)));
  1113. furi_string_free(goesRight);
  1114. } else {
  1115. tanks_state->p1->moving = true;
  1116. tanks_state->p1->direction = DirectionRight;
  1117. }
  1118. }
  1119. break;
  1120. case InputKeyLeft:
  1121. if(!(tanks_state->state == GameStateMenu)) {
  1122. if(tanks_state->state == GameStateCooperativeClient) {
  1123. FuriString* goesLeft = furi_string_alloc();
  1124. char arr[2];
  1125. arr[0] = GoesLeft;
  1126. arr[1] = 0;
  1127. furi_string_set(goesLeft, (char*)&arr);
  1128. subghz_tx_rx_worker_write(
  1129. subghz_txrx,
  1130. (uint8_t*)furi_string_get_cstr(goesLeft),
  1131. strlen(furi_string_get_cstr(goesLeft)));
  1132. furi_string_free(goesLeft);
  1133. } else {
  1134. tanks_state->p1->moving = true;
  1135. tanks_state->p1->direction = DirectionLeft;
  1136. }
  1137. }
  1138. break;
  1139. case InputKeyOk:
  1140. if(tanks_state->state == GameStateMenu) {
  1141. if(tanks_state->menu_state == MenuStateSingleMode) {
  1142. tanks_state->server = true;
  1143. tanks_game_init_game(tanks_state, GameStateSingle);
  1144. break;
  1145. } else if(tanks_state->menu_state == MenuStateCooperativeServerMode) {
  1146. tanks_state->server = true;
  1147. tanks_game_init_game(tanks_state, GameStateCooperativeServer);
  1148. break;
  1149. } else if(tanks_state->menu_state == MenuStateCooperativeClientMode) {
  1150. tanks_state->server = false;
  1151. tanks_game_init_game(tanks_state, GameStateCooperativeClient);
  1152. break;
  1153. }
  1154. } else if(tanks_state->state == GameStateGameOver) {
  1155. tanks_game_init_game(tanks_state, tanks_state->state);
  1156. } else if(tanks_state->state == GameStateCooperativeClient) {
  1157. FuriString* shoots = furi_string_alloc();
  1158. char arr[2];
  1159. arr[0] = Shoots;
  1160. arr[1] = 0;
  1161. furi_string_set(shoots, (char*)&arr);
  1162. subghz_tx_rx_worker_write(
  1163. subghz_txrx,
  1164. (uint8_t*)furi_string_get_cstr(shoots),
  1165. strlen(furi_string_get_cstr(shoots)));
  1166. furi_string_free(shoots);
  1167. } else {
  1168. tanks_state->p1->shooting = true;
  1169. }
  1170. break;
  1171. case InputKeyBack:
  1172. processing = false;
  1173. break;
  1174. default:
  1175. break;
  1176. }
  1177. }
  1178. } else if(event.type == EventTypeTick) {
  1179. if(tanks_state->state == GameStateCooperativeServer) {
  1180. if(subghz_tx_rx_worker_available(subghz_txrx)) {
  1181. memset(incomingMessage, 0x00, message_max_len);
  1182. subghz_tx_rx_worker_read(subghz_txrx, incomingMessage, message_max_len);
  1183. tanks_state->received++;
  1184. switch(incomingMessage[0]) {
  1185. case GoesUp:
  1186. tanks_state->p2->moving = true;
  1187. tanks_state->p2->direction = DirectionUp;
  1188. break;
  1189. case GoesRight:
  1190. tanks_state->p2->moving = true;
  1191. tanks_state->p2->direction = DirectionRight;
  1192. break;
  1193. case GoesDown:
  1194. tanks_state->p2->moving = true;
  1195. tanks_state->p2->direction = DirectionDown;
  1196. break;
  1197. case GoesLeft:
  1198. tanks_state->p2->moving = true;
  1199. tanks_state->p2->direction = DirectionLeft;
  1200. break;
  1201. case Shoots:
  1202. tanks_state->p2->shooting = true;
  1203. break;
  1204. default:
  1205. break;
  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. }