tanks_game.c 47 KB

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