doom.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <sys/time.h>
  7. #include "sound.h"
  8. #include "display.h"
  9. #include "assets.h"
  10. #include "constants.h"
  11. #include "entities.h"
  12. #include "types.h"
  13. #include "level.h"
  14. #include <notification/notification.h>
  15. #include <notification/notification_messages.h>
  16. #include <dolphin/dolphin.h>
  17. #define SOUND
  18. // Useful macros
  19. #define swap(a, b) \
  20. do { \
  21. typeof(a) temp = a; \
  22. a = b; \
  23. b = temp; \
  24. } while(0)
  25. #define sign(a, b) (double)(a > b ? 1 : (b > a ? -1 : 0))
  26. #define pgm_read_byte(addr) (*(const unsigned char*)(addr))
  27. typedef enum {
  28. EventTypeTick,
  29. EventTypeKey,
  30. } EventType;
  31. typedef struct {
  32. EventType type;
  33. InputEvent input;
  34. } PluginEvent;
  35. typedef struct {
  36. FuriMutex* mutex;
  37. Player player;
  38. Entity entity[MAX_ENTITIES];
  39. StaticEntity static_entity[MAX_STATIC_ENTITIES];
  40. uint8_t num_entities;
  41. uint8_t num_static_entities;
  42. uint8_t scene;
  43. uint8_t gun_pos;
  44. double jogging;
  45. double view_height;
  46. bool init;
  47. bool up;
  48. bool down;
  49. bool left;
  50. bool right;
  51. bool fired;
  52. bool gun_fired;
  53. double rot_speed;
  54. double old_dir_x;
  55. double old_plane_x;
  56. NotificationApp* notify;
  57. #ifdef SOUND
  58. MusicPlayer* music_instance;
  59. bool intro_sound;
  60. #endif
  61. } PluginState;
  62. static const NotificationSequence sequence_short_sound = {
  63. &message_note_c5,
  64. &message_delay_50,
  65. &message_sound_off,
  66. NULL,
  67. };
  68. static const NotificationSequence sequence_long_sound = {
  69. &message_note_c3,
  70. &message_delay_100,
  71. &message_sound_off,
  72. NULL,
  73. };
  74. Coords translateIntoView(Coords* pos, PluginState* const plugin_state);
  75. void updateHud(Canvas* const canvas, PluginState* const plugin_state);
  76. // general
  77. bool invert_screen = false;
  78. uint8_t flash_screen = 0;
  79. // game
  80. // player and entities
  81. uint8_t getBlockAt(const uint8_t level[], uint8_t x, uint8_t y) {
  82. if(x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT) {
  83. return E_FLOOR;
  84. }
  85. // y is read in inverse order
  86. return pgm_read_byte(level + (((LEVEL_HEIGHT - 1 - y) * LEVEL_WIDTH + x) / 2)) >>
  87. (!(x % 2) * 4) // displace part of wanted bits
  88. & 0b1111; // mask wanted bits
  89. }
  90. // Finds the player in the map
  91. void initializeLevel(const uint8_t level[], PluginState* const plugin_state) {
  92. for(uint8_t y = LEVEL_HEIGHT - 1; y > 0; y--) {
  93. for(uint8_t x = 0; x < LEVEL_WIDTH; x++) {
  94. uint8_t block = getBlockAt(level, x, y);
  95. if(block == E_PLAYER) {
  96. plugin_state->player = create_player(x, y);
  97. return;
  98. }
  99. // todo create other static entities
  100. }
  101. }
  102. }
  103. bool isSpawned(UID uid, PluginState* const plugin_state) {
  104. for(uint8_t i = 0; i < plugin_state->num_entities; i++) {
  105. if(plugin_state->entity[i].uid == uid) return true;
  106. }
  107. return false;
  108. }
  109. bool isStatic(UID uid, PluginState* const plugin_state) {
  110. for(uint8_t i = 0; i < plugin_state->num_static_entities; i++) {
  111. if(plugin_state->static_entity[i].uid == uid) return true;
  112. }
  113. return false;
  114. }
  115. void spawnEntity(uint8_t type, uint8_t x, uint8_t y, PluginState* const plugin_state) {
  116. // Limit the number of spawned entities
  117. if(plugin_state->num_entities >= MAX_ENTITIES) {
  118. return;
  119. }
  120. // todo: read static entity status
  121. switch(type) {
  122. case E_ENEMY:
  123. plugin_state->entity[plugin_state->num_entities] = create_enemy(x, y);
  124. plugin_state->num_entities++;
  125. break;
  126. case E_KEY:
  127. plugin_state->entity[plugin_state->num_entities] = create_key(x, y);
  128. plugin_state->num_entities++;
  129. break;
  130. case E_MEDIKIT:
  131. plugin_state->entity[plugin_state->num_entities] = create_medikit(x, y);
  132. plugin_state->num_entities++;
  133. break;
  134. }
  135. }
  136. void spawnFireball(double x, double y, PluginState* const plugin_state) {
  137. // Limit the number of spawned entities
  138. if(plugin_state->num_entities >= MAX_ENTITIES) {
  139. return;
  140. }
  141. UID uid = create_uid(E_FIREBALL, x, y);
  142. // Remove if already exists, don't throw anything. Not the best, but shouldn't happen too often
  143. if(isSpawned(uid, plugin_state)) return;
  144. // Calculate direction. 32 angles
  145. int16_t dir =
  146. FIREBALL_ANGLES + atan2(y - plugin_state->player.pos.y, x - plugin_state->player.pos.x) /
  147. (double)PI * FIREBALL_ANGLES;
  148. if(dir < 0) dir += FIREBALL_ANGLES * 2;
  149. plugin_state->entity[plugin_state->num_entities] = create_fireball(x, y, dir);
  150. plugin_state->num_entities++;
  151. }
  152. void removeEntity(UID uid, PluginState* const plugin_state) {
  153. uint8_t i = 0;
  154. bool found = false;
  155. while(i < plugin_state->num_entities) {
  156. if(!found && plugin_state->entity[i].uid == uid) {
  157. // todo: doze it
  158. found = true;
  159. plugin_state->num_entities--;
  160. }
  161. // displace entities
  162. if(found) {
  163. plugin_state->entity[i] = plugin_state->entity[i + 1];
  164. }
  165. i++;
  166. }
  167. }
  168. void removeStaticEntity(UID uid, PluginState* const plugin_state) {
  169. uint8_t i = 0;
  170. bool found = false;
  171. while(i < plugin_state->num_static_entities) {
  172. if(!found && plugin_state->static_entity[i].uid == uid) {
  173. found = true;
  174. plugin_state->num_static_entities--;
  175. }
  176. // displace entities
  177. if(found) {
  178. plugin_state->static_entity[i] = plugin_state->static_entity[i + 1];
  179. }
  180. i++;
  181. }
  182. }
  183. UID detectCollision(
  184. const uint8_t level[],
  185. Coords* pos,
  186. double relative_x,
  187. double relative_y,
  188. bool only_walls,
  189. PluginState* const plugin_state) {
  190. // Wall collision
  191. uint8_t round_x = (int)(pos->x + relative_x);
  192. uint8_t round_y = (int)(pos->y + relative_y);
  193. uint8_t block = getBlockAt(level, round_x, round_y);
  194. if(block == E_WALL) {
  195. // playSound(hit_wall_snd, HIT_WALL_SND_LEN);
  196. return create_uid(block, round_x, round_y);
  197. }
  198. if(only_walls) {
  199. return UID_null;
  200. }
  201. // Entity collision
  202. for(uint8_t i = 0; i < plugin_state->num_entities; i++) {
  203. // Don't collide with itself
  204. if(&(plugin_state->entity[i].pos) == pos) {
  205. continue;
  206. }
  207. uint8_t type = uid_get_type(plugin_state->entity[i].uid);
  208. // Only ALIVE enemy collision
  209. if(type != E_ENEMY || plugin_state->entity[i].state == S_DEAD ||
  210. plugin_state->entity[i].state == S_HIDDEN) {
  211. continue;
  212. }
  213. Coords new_coords = {
  214. plugin_state->entity[i].pos.x - relative_x,
  215. plugin_state->entity[i].pos.y - relative_y};
  216. uint8_t distance = coords_distance(pos, &new_coords);
  217. // Check distance and if it's getting closer
  218. if(distance < ENEMY_COLLIDER_DIST && distance < plugin_state->entity[i].distance) {
  219. return plugin_state->entity[i].uid;
  220. }
  221. }
  222. return UID_null;
  223. }
  224. // Shoot
  225. void fire(PluginState* const plugin_state) {
  226. //playSound(shoot_snd, SHOOT_SND_LEN);
  227. for(uint8_t i = 0; i < plugin_state->num_entities; i++) {
  228. // Shoot only ALIVE enemies
  229. if(uid_get_type(plugin_state->entity[i].uid) != E_ENEMY ||
  230. plugin_state->entity[i].state == S_DEAD || plugin_state->entity[i].state == S_HIDDEN) {
  231. continue;
  232. }
  233. Coords transform = translateIntoView(&(plugin_state->entity[i].pos), plugin_state);
  234. if(fabs(transform.x) < 20 && transform.y > 0) {
  235. uint8_t damage = (double)fmin(
  236. GUN_MAX_DAMAGE,
  237. GUN_MAX_DAMAGE / (fabs(transform.x) * plugin_state->entity[i].distance) / 5);
  238. if(damage > 0) {
  239. plugin_state->entity[i].health = fmax(0, plugin_state->entity[i].health - damage);
  240. plugin_state->entity[i].state = S_HIT;
  241. plugin_state->entity[i].timer = 4;
  242. }
  243. }
  244. }
  245. }
  246. UID updatePosition(
  247. const uint8_t level[],
  248. Coords* pos,
  249. double relative_x,
  250. double relative_y,
  251. bool only_walls,
  252. PluginState* const plugin_state) {
  253. UID collide_x = detectCollision(level, pos, relative_x, 0, only_walls, plugin_state);
  254. UID collide_y = detectCollision(level, pos, 0, relative_y, only_walls, plugin_state);
  255. if(!collide_x) pos->x += relative_x;
  256. if(!collide_y) pos->y += relative_y;
  257. return collide_x || collide_y || UID_null;
  258. }
  259. void updateEntities(const uint8_t level[], Canvas* const canvas, PluginState* const plugin_state) {
  260. uint8_t i = 0;
  261. while(i < plugin_state->num_entities) {
  262. // update distance
  263. plugin_state->entity[i].distance =
  264. coords_distance(&(plugin_state->player.pos), &(plugin_state->entity[i].pos));
  265. // Run the timer. Works with actual frames.
  266. // Todo: use delta here. But needs double type and more memory
  267. if(plugin_state->entity[i].timer > 0) plugin_state->entity[i].timer--;
  268. // too far away. put it in doze mode
  269. if(plugin_state->entity[i].distance > MAX_ENTITY_DISTANCE) {
  270. removeEntity(plugin_state->entity[i].uid, plugin_state);
  271. // don't increase 'i', since current one has been removed
  272. continue;
  273. }
  274. // bypass render if hidden
  275. if(plugin_state->entity[i].state == S_HIDDEN) {
  276. i++;
  277. continue;
  278. }
  279. uint8_t type = uid_get_type(plugin_state->entity[i].uid);
  280. switch(type) {
  281. case E_ENEMY: {
  282. // Enemy "IA"
  283. if(plugin_state->entity[i].health == 0) {
  284. if(plugin_state->entity[i].state != S_DEAD) {
  285. plugin_state->entity[i].state = S_DEAD;
  286. plugin_state->entity[i].timer = 6;
  287. }
  288. } else if(plugin_state->entity[i].state == S_HIT) {
  289. if(plugin_state->entity[i].timer == 0) {
  290. // Back to alert state
  291. plugin_state->entity[i].state = S_ALERT;
  292. plugin_state->entity[i].timer = 40; // delay next fireball thrown
  293. }
  294. } else if(plugin_state->entity[i].state == S_FIRING) {
  295. if(plugin_state->entity[i].timer == 0) {
  296. // Back to alert state
  297. plugin_state->entity[i].state = S_ALERT;
  298. plugin_state->entity[i].timer = 40; // delay next fireball throwm
  299. }
  300. } else {
  301. // ALERT STATE
  302. if(plugin_state->entity[i].distance > ENEMY_MELEE_DIST &&
  303. plugin_state->entity[i].distance < MAX_ENEMY_VIEW) {
  304. if(plugin_state->entity[i].state != S_ALERT) {
  305. plugin_state->entity[i].state = S_ALERT;
  306. plugin_state->entity[i].timer = 20; // used to throw fireballs
  307. } else {
  308. if(plugin_state->entity[i].timer == 0) {
  309. // Throw a fireball
  310. spawnFireball(
  311. plugin_state->entity[i].pos.x,
  312. plugin_state->entity[i].pos.y,
  313. plugin_state);
  314. plugin_state->entity[i].state = S_FIRING;
  315. plugin_state->entity[i].timer = 6;
  316. } else {
  317. // move towards to the player.
  318. updatePosition(
  319. level,
  320. &(plugin_state->entity[i].pos),
  321. sign(plugin_state->player.pos.x, plugin_state->entity[i].pos.x) *
  322. (double)ENEMY_SPEED * 1, // NOT SURE (delta)
  323. sign(plugin_state->player.pos.y, plugin_state->entity[i].pos.y) *
  324. (double)ENEMY_SPEED * 1, // NOT SURE (delta)
  325. true,
  326. plugin_state);
  327. }
  328. }
  329. } else if(plugin_state->entity[i].distance <= ENEMY_MELEE_DIST) {
  330. if(plugin_state->entity[i].state != S_MELEE) {
  331. // Preparing the melee attack
  332. plugin_state->entity[i].state = S_MELEE;
  333. plugin_state->entity[i].timer = 10;
  334. } else if(plugin_state->entity[i].timer == 0) {
  335. // Melee attack
  336. plugin_state->player.health =
  337. fmax(0, plugin_state->player.health - ENEMY_MELEE_DAMAGE);
  338. plugin_state->entity[i].timer = 14;
  339. flash_screen = 1;
  340. updateHud(canvas, plugin_state);
  341. }
  342. } else {
  343. // stand
  344. plugin_state->entity[i].state = S_STAND;
  345. }
  346. }
  347. break;
  348. }
  349. case E_FIREBALL: {
  350. if(plugin_state->entity[i].distance < FIREBALL_COLLIDER_DIST) {
  351. // Hit the player and disappear
  352. plugin_state->player.health =
  353. fmax(0, plugin_state->player.health - ENEMY_FIREBALL_DAMAGE);
  354. flash_screen = 1;
  355. updateHud(canvas, plugin_state);
  356. removeEntity(plugin_state->entity[i].uid, plugin_state);
  357. continue; // continue in the loop
  358. } else {
  359. // Move. Only collide with walls.
  360. // Note: using health to store the angle of the movement
  361. UID collided = updatePosition(
  362. level,
  363. &(plugin_state->entity[i].pos),
  364. cos((double)plugin_state->entity[i].health / FIREBALL_ANGLES * (double)PI) *
  365. (double)FIREBALL_SPEED,
  366. sin((double)plugin_state->entity[i].health / FIREBALL_ANGLES * (double)PI) *
  367. (double)FIREBALL_SPEED,
  368. true,
  369. plugin_state);
  370. if(collided) {
  371. removeEntity(plugin_state->entity[i].uid, plugin_state);
  372. continue; // continue in the entity check loop
  373. }
  374. }
  375. break;
  376. }
  377. case E_MEDIKIT: {
  378. if(plugin_state->entity[i].distance < ITEM_COLLIDER_DIST) {
  379. // pickup
  380. notification_message(plugin_state->notify, &sequence_long_sound);
  381. //playSound(medkit_snd, MEDKIT_SND_LEN);
  382. plugin_state->entity[i].state = S_HIDDEN;
  383. plugin_state->player.health = fmin(100, plugin_state->player.health + 50);
  384. updateHud(canvas, plugin_state);
  385. flash_screen = 1;
  386. }
  387. break;
  388. }
  389. case E_KEY: {
  390. if(plugin_state->entity[i].distance < ITEM_COLLIDER_DIST) {
  391. // pickup
  392. notification_message(plugin_state->notify, &sequence_long_sound);
  393. //playSound(get_key_snd, GET_KEY_SND_LEN);
  394. plugin_state->entity[i].state = S_HIDDEN;
  395. plugin_state->player.keys++;
  396. updateHud(canvas, plugin_state);
  397. flash_screen = 1;
  398. }
  399. break;
  400. }
  401. }
  402. i++;
  403. }
  404. }
  405. // The map raycaster. Based on https://lodev.org/cgtutor/raycasting.html
  406. void renderMap(
  407. const uint8_t level[],
  408. double view_height,
  409. Canvas* const canvas,
  410. PluginState* const plugin_state) {
  411. UID last_uid = 0; // NOT SURE ?
  412. for(uint8_t x = 0; x < SCREEN_WIDTH; x += RES_DIVIDER) {
  413. double camera_x = 2 * (double)x / SCREEN_WIDTH - 1;
  414. double ray_x = plugin_state->player.dir.x + plugin_state->player.plane.x * camera_x;
  415. double ray_y = plugin_state->player.dir.y + plugin_state->player.plane.y * camera_x;
  416. uint8_t map_x = (uint8_t)plugin_state->player.pos.x;
  417. uint8_t map_y = (uint8_t)plugin_state->player.pos.y;
  418. Coords map_coords = {plugin_state->player.pos.x, plugin_state->player.pos.y};
  419. double delta_x = fabs(1 / ray_x);
  420. double delta_y = fabs(1 / ray_y);
  421. int8_t step_x;
  422. int8_t step_y;
  423. double side_x;
  424. double side_y;
  425. if(ray_x < 0) {
  426. step_x = -1;
  427. side_x = (plugin_state->player.pos.x - map_x) * delta_x;
  428. } else {
  429. step_x = 1;
  430. side_x = (map_x + (double)1.0 - plugin_state->player.pos.x) * delta_x;
  431. }
  432. if(ray_y < 0) {
  433. step_y = -1;
  434. side_y = (plugin_state->player.pos.y - map_y) * delta_y;
  435. } else {
  436. step_y = 1;
  437. side_y = (map_y + (double)1.0 - plugin_state->player.pos.y) * delta_y;
  438. }
  439. // Wall detection
  440. uint8_t depth = 0;
  441. bool hit = 0;
  442. bool side;
  443. while(!hit && depth < MAX_RENDER_DEPTH) {
  444. if(side_x < side_y) {
  445. side_x += delta_x;
  446. map_x += step_x;
  447. side = 0;
  448. } else {
  449. side_y += delta_y;
  450. map_y += step_y;
  451. side = 1;
  452. }
  453. uint8_t block = getBlockAt(level, map_x, map_y);
  454. if(block == E_WALL) {
  455. hit = 1;
  456. } else {
  457. // Spawning entities here, as soon they are visible for the
  458. // player. Not the best place, but would be a very performance
  459. // cost scan for them in another loop
  460. if(block == E_ENEMY || (block & 0b00001000) /* all collectable items */) {
  461. // Check that it's close to the player
  462. if(coords_distance(&(plugin_state->player.pos), &map_coords) <
  463. MAX_ENTITY_DISTANCE) {
  464. UID uid = create_uid(block, map_x, map_y);
  465. if(last_uid != uid && !isSpawned(uid, plugin_state)) {
  466. spawnEntity(block, map_x, map_y, plugin_state);
  467. last_uid = uid;
  468. }
  469. }
  470. }
  471. }
  472. depth++;
  473. }
  474. if(hit) {
  475. double distance;
  476. if(side == 0) {
  477. distance =
  478. fmax(1, (map_x - plugin_state->player.pos.x + (1 - step_x) / 2) / ray_x);
  479. } else {
  480. distance =
  481. fmax(1, (map_y - plugin_state->player.pos.y + (1 - step_y) / 2) / ray_y);
  482. }
  483. // store zbuffer value for the column
  484. zbuffer[x / Z_RES_DIVIDER] = fmin(distance * DISTANCE_MULTIPLIER, 255);
  485. // rendered line height
  486. uint8_t line_height = RENDER_HEIGHT / distance;
  487. drawVLine(
  488. x,
  489. view_height / distance - line_height / 2 + RENDER_HEIGHT / 2,
  490. view_height / distance + line_height / 2 + RENDER_HEIGHT / 2,
  491. GRADIENT_COUNT - (int)distance / MAX_RENDER_DEPTH * GRADIENT_COUNT - side * 2,
  492. canvas);
  493. }
  494. }
  495. }
  496. // Sort entities from far to close
  497. uint8_t sortEntities(PluginState* const plugin_state) {
  498. uint8_t gap = plugin_state->num_entities;
  499. bool swapped = false;
  500. while(gap > 1 || swapped) {
  501. //shrink factor 1.3
  502. gap = (gap * 10) / 13;
  503. if(gap == 9 || gap == 10) gap = 11;
  504. if(gap < 1) gap = 1;
  505. swapped = false;
  506. for(uint8_t i = 0; i < plugin_state->num_entities - gap; i++) {
  507. uint8_t j = i + gap;
  508. if(plugin_state->entity[i].distance < plugin_state->entity[j].distance) {
  509. swap(plugin_state->entity[i], plugin_state->entity[j]);
  510. swapped = true;
  511. }
  512. }
  513. }
  514. return swapped;
  515. }
  516. Coords translateIntoView(Coords* pos, PluginState* const plugin_state) {
  517. //translate sprite position to relative to camera
  518. double sprite_x = pos->x - plugin_state->player.pos.x;
  519. double sprite_y = pos->y - plugin_state->player.pos.y;
  520. //required for correct matrix multiplication
  521. double inv_det =
  522. ((double)1.0 /
  523. ((double)plugin_state->player.plane.x * (double)plugin_state->player.dir.y -
  524. (double)plugin_state->player.dir.x * (double)plugin_state->player.plane.y));
  525. double transform_x =
  526. inv_det * (plugin_state->player.dir.y * sprite_x - plugin_state->player.dir.x * sprite_y);
  527. double transform_y = inv_det * (-plugin_state->player.plane.y * sprite_x +
  528. plugin_state->player.plane.x * sprite_y); // Z in screen
  529. Coords res = {transform_x, transform_y};
  530. return res;
  531. }
  532. void renderEntities(double view_height, Canvas* const canvas, PluginState* const plugin_state) {
  533. sortEntities(plugin_state);
  534. for(uint8_t i = 0; i < plugin_state->num_entities; i++) {
  535. if(plugin_state->entity[i].state == S_HIDDEN) continue;
  536. Coords transform = translateIntoView(&(plugin_state->entity[i].pos), plugin_state);
  537. // don´t render if behind the player or too far away
  538. if(transform.y <= (double)0.1 || transform.y > MAX_SPRITE_DEPTH) {
  539. continue;
  540. }
  541. int16_t sprite_screen_x = HALF_WIDTH * ((double)1.0 + transform.x / transform.y);
  542. int8_t sprite_screen_y = RENDER_HEIGHT / 2 + view_height / transform.y;
  543. uint8_t type = uid_get_type(plugin_state->entity[i].uid);
  544. // don´t try to render if outside of screen
  545. // doing this pre-shortcut due int16 -> int8 conversion makes out-of-screen
  546. // values fit into the screen space
  547. if(sprite_screen_x < -HALF_WIDTH || sprite_screen_x > SCREEN_WIDTH + HALF_WIDTH) {
  548. continue;
  549. }
  550. switch(type) {
  551. case E_ENEMY: {
  552. uint8_t sprite;
  553. if(plugin_state->entity[i].state == S_ALERT) {
  554. // walking
  555. sprite = ((int)furi_get_tick() / 500) % 2;
  556. } else if(plugin_state->entity[i].state == S_FIRING) {
  557. // fireball
  558. sprite = 2;
  559. } else if(plugin_state->entity[i].state == S_HIT) {
  560. // hit
  561. sprite = 3;
  562. } else if(plugin_state->entity[i].state == S_MELEE) {
  563. // melee atack
  564. sprite = plugin_state->entity[i].timer > 10 ? 2 : 1;
  565. } else if(plugin_state->entity[i].state == S_DEAD) {
  566. // dying
  567. sprite = plugin_state->entity[i].timer > 0 ? 3 : 4;
  568. } else {
  569. // stand
  570. sprite = 0;
  571. }
  572. drawSprite(
  573. sprite_screen_x - BMP_IMP_WIDTH * (double).5 / transform.y,
  574. sprite_screen_y - 8 / transform.y,
  575. imp_inv,
  576. imp_mask_inv,
  577. BMP_IMP_WIDTH,
  578. BMP_IMP_HEIGHT,
  579. sprite,
  580. transform.y,
  581. canvas);
  582. break;
  583. }
  584. case E_FIREBALL: {
  585. drawSprite(
  586. sprite_screen_x - BMP_FIREBALL_WIDTH / 2 / transform.y,
  587. sprite_screen_y - BMP_FIREBALL_HEIGHT / 2 / transform.y,
  588. fireball,
  589. fireball_mask,
  590. BMP_FIREBALL_WIDTH,
  591. BMP_FIREBALL_HEIGHT,
  592. 0,
  593. transform.y,
  594. canvas);
  595. break;
  596. }
  597. case E_MEDIKIT: {
  598. drawSprite(
  599. sprite_screen_x - BMP_ITEMS_WIDTH / 2 / transform.y,
  600. sprite_screen_y + 5 / transform.y,
  601. item,
  602. item_mask,
  603. BMP_ITEMS_WIDTH,
  604. BMP_ITEMS_HEIGHT,
  605. 0,
  606. transform.y,
  607. canvas);
  608. break;
  609. }
  610. case E_KEY: {
  611. drawSprite(
  612. sprite_screen_x - BMP_ITEMS_WIDTH / 2 / transform.y,
  613. sprite_screen_y + 5 / transform.y,
  614. item,
  615. item_mask,
  616. BMP_ITEMS_WIDTH,
  617. BMP_ITEMS_HEIGHT,
  618. 1,
  619. transform.y,
  620. canvas);
  621. break;
  622. }
  623. }
  624. }
  625. }
  626. void renderGun(uint8_t gun_pos, double amount_jogging, Canvas* const canvas) {
  627. // jogging
  628. char x = 48 + sin((double)furi_get_tick() * (double)JOGGING_SPEED) * 10 * amount_jogging;
  629. char y = RENDER_HEIGHT - gun_pos +
  630. fabs(cos((double)furi_get_tick() * (double)JOGGING_SPEED)) * 8 * amount_jogging;
  631. if(gun_pos > GUN_SHOT_POS - 2) {
  632. // Gun fire
  633. drawBitmap(x + 6, y - 11, &I_fire_inv, BMP_FIRE_WIDTH, BMP_FIRE_HEIGHT, 1, canvas);
  634. }
  635. // Don't draw over the hud!
  636. uint8_t clip_height = fmax(0, fmin(y + BMP_GUN_HEIGHT, RENDER_HEIGHT) - y);
  637. // Draw the gun (black mask + actual sprite).
  638. drawBitmap(x, y, &I_gun_mask_inv, BMP_GUN_WIDTH, clip_height, 0, canvas);
  639. drawBitmap(x, y, &I_gun_inv, BMP_GUN_WIDTH, clip_height, 1, canvas);
  640. //drawGun(x,y,gun_mask, BMP_GUN_WIDTH, clip_height, 0, canvas);
  641. //drawGun(x,y,gun, BMP_GUN_WIDTH, clip_height, 1, canvas);
  642. }
  643. // Only needed first time
  644. void renderHud(Canvas* const canvas, PluginState* plugin_state) {
  645. drawTextSpace(2, 58, "{}", 0, canvas); // Health symbol
  646. drawTextSpace(40, 58, "[]", 0, canvas); // Keys symbol
  647. updateHud(canvas, plugin_state);
  648. }
  649. // Render values for the HUD
  650. void updateHud(Canvas* const canvas, PluginState* plugin_state) {
  651. clearRect(12, 58, 15, 6, canvas);
  652. clearRect(50, 58, 15, 6, canvas);
  653. drawText(12, 58, plugin_state->player.health, canvas);
  654. drawText(50, 58, plugin_state->player.keys, canvas);
  655. }
  656. // Debug stats
  657. void renderStats(Canvas* const canvas, PluginState* plugin_state) {
  658. clearRect(58, 58, 70, 6, canvas);
  659. drawText(114, 58, (int)getActualFps(), canvas);
  660. drawText(82, 58, plugin_state->num_entities, canvas);
  661. // drawText(94, 58, freeMemory());
  662. }
  663. // Intro screen
  664. void loopIntro(Canvas* const canvas) {
  665. canvas_draw_icon(canvas, 0, 0, &I_logo_inv);
  666. //drawTextSpace(SCREEN_WIDTH / 2 - 25, SCREEN_HEIGHT * .8, "PRESS FIRE", 1, canvas);
  667. }
  668. static void render_callback(Canvas* const canvas, void* ctx) {
  669. furi_assert(ctx);
  670. PluginState* plugin_state = ctx;
  671. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  672. canvas_set_font(canvas, FontPrimary);
  673. switch(plugin_state->scene) {
  674. case INTRO: {
  675. loopIntro(canvas);
  676. break;
  677. }
  678. case GAME_PLAY: {
  679. updateEntities(sto_level_1, canvas, plugin_state);
  680. renderGun(plugin_state->gun_pos, plugin_state->jogging, canvas);
  681. renderMap(sto_level_1, plugin_state->view_height, canvas, plugin_state);
  682. renderEntities(plugin_state->view_height, canvas, plugin_state);
  683. renderHud(canvas, plugin_state);
  684. updateHud(canvas, plugin_state);
  685. renderStats(canvas, plugin_state);
  686. break;
  687. }
  688. }
  689. furi_mutex_release(plugin_state->mutex);
  690. }
  691. static void input_callback(InputEvent* input_event, void* ctx) {
  692. FuriMessageQueue* event_queue = ctx;
  693. furi_assert(event_queue);
  694. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  695. furi_message_queue_put(event_queue, &event, 0);
  696. }
  697. static void doom_state_init(PluginState* const plugin_state) {
  698. plugin_state->notify = furi_record_open(RECORD_NOTIFICATION);
  699. plugin_state->num_entities = 0;
  700. plugin_state->num_static_entities = 0;
  701. plugin_state->scene = INTRO;
  702. plugin_state->gun_pos = 0;
  703. plugin_state->view_height = 0;
  704. plugin_state->init = true;
  705. plugin_state->up = false;
  706. plugin_state->down = false;
  707. plugin_state->left = false;
  708. plugin_state->right = false;
  709. plugin_state->fired = false;
  710. plugin_state->gun_fired = false;
  711. #ifdef SOUND
  712. plugin_state->music_instance = malloc(sizeof(MusicPlayer));
  713. plugin_state->music_instance->model = malloc(sizeof(MusicPlayerModel));
  714. memset(
  715. plugin_state->music_instance->model->duration_history,
  716. 0xff,
  717. MUSIC_PLAYER_SEMITONE_HISTORY_SIZE);
  718. memset(
  719. plugin_state->music_instance->model->semitone_history,
  720. 0xff,
  721. MUSIC_PLAYER_SEMITONE_HISTORY_SIZE);
  722. plugin_state->music_instance->model->volume = 2;
  723. plugin_state->music_instance->model_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  724. //plugin_state->music_instance->view_port = view_port_alloc();
  725. plugin_state->music_instance->worker = music_player_worker_alloc();
  726. //music_player_worker_set_volume(plugin_state->music_instance->worker, 0.75);
  727. music_player_worker_set_volume(
  728. plugin_state->music_instance->worker,
  729. MUSIC_PLAYER_VOLUMES[plugin_state->music_instance->model->volume]);
  730. plugin_state->intro_sound = true;
  731. //init_sound(plugin_state->music_instance);
  732. #endif
  733. }
  734. static void doom_game_update_timer_callback(void* ctx) {
  735. FuriMessageQueue* event_queue = ctx;
  736. furi_assert(event_queue);
  737. PluginEvent event = {.type = EventTypeTick};
  738. furi_message_queue_put(event_queue, &event, 0);
  739. }
  740. static void doom_game_tick(PluginState* const plugin_state) {
  741. if(plugin_state->scene == GAME_PLAY) {
  742. //fps();
  743. //player is alive
  744. if(plugin_state->player.health > 0) {
  745. if(plugin_state->up) {
  746. plugin_state->player.velocity +=
  747. ((double)MOV_SPEED - plugin_state->player.velocity) * (double).4;
  748. plugin_state->jogging = fabs(plugin_state->player.velocity) * MOV_SPEED_INV;
  749. //plugin_state->up = false;
  750. } else if(plugin_state->down) {
  751. plugin_state->player.velocity +=
  752. (-(double)MOV_SPEED - plugin_state->player.velocity) * (double).4;
  753. plugin_state->jogging = fabs(plugin_state->player.velocity) * MOV_SPEED_INV;
  754. //plugin_state->down = false;
  755. } else {
  756. plugin_state->player.velocity *= (double).5;
  757. plugin_state->jogging = fabs(plugin_state->player.velocity) * MOV_SPEED_INV;
  758. }
  759. if(plugin_state->right) {
  760. plugin_state->rot_speed = (double)ROT_SPEED * delta;
  761. plugin_state->old_dir_x = plugin_state->player.dir.x;
  762. plugin_state->player.dir.x =
  763. plugin_state->player.dir.x * cos(-(plugin_state->rot_speed)) -
  764. plugin_state->player.dir.y * sin(-(plugin_state->rot_speed));
  765. plugin_state->player.dir.y =
  766. plugin_state->old_dir_x * sin(-(plugin_state->rot_speed)) +
  767. plugin_state->player.dir.y * cos(-(plugin_state->rot_speed));
  768. plugin_state->old_plane_x = plugin_state->player.plane.x;
  769. plugin_state->player.plane.x =
  770. plugin_state->player.plane.x * cos(-(plugin_state->rot_speed)) -
  771. plugin_state->player.plane.y * sin(-(plugin_state->rot_speed));
  772. plugin_state->player.plane.y =
  773. plugin_state->old_plane_x * sin(-(plugin_state->rot_speed)) +
  774. plugin_state->player.plane.y * cos(-(plugin_state->rot_speed));
  775. //plugin_state->right = false;
  776. } else if(plugin_state->left) {
  777. plugin_state->rot_speed = (double)ROT_SPEED * delta;
  778. plugin_state->old_dir_x = plugin_state->player.dir.x;
  779. plugin_state->player.dir.x =
  780. plugin_state->player.dir.x * cos(plugin_state->rot_speed) -
  781. plugin_state->player.dir.y * sin(plugin_state->rot_speed);
  782. plugin_state->player.dir.y =
  783. plugin_state->old_dir_x * sin(plugin_state->rot_speed) +
  784. plugin_state->player.dir.y * cos(plugin_state->rot_speed);
  785. plugin_state->old_plane_x = plugin_state->player.plane.x;
  786. plugin_state->player.plane.x =
  787. plugin_state->player.plane.x * cos(plugin_state->rot_speed) -
  788. plugin_state->player.plane.y * sin(plugin_state->rot_speed);
  789. plugin_state->player.plane.y =
  790. plugin_state->old_plane_x * sin(plugin_state->rot_speed) +
  791. plugin_state->player.plane.y * cos(plugin_state->rot_speed);
  792. //plugin_state->left = false;
  793. }
  794. plugin_state->view_height =
  795. fabs(sin((double)furi_get_tick() * (double)JOGGING_SPEED)) * 6 *
  796. plugin_state->jogging;
  797. if(plugin_state->gun_pos > GUN_TARGET_POS) {
  798. // Right after fire
  799. plugin_state->gun_pos -= 1;
  800. } else if(plugin_state->gun_pos < GUN_TARGET_POS) {
  801. plugin_state->gun_pos += 2;
  802. } else if(!plugin_state->gun_fired && plugin_state->fired) {
  803. //furi_hal_speaker_start(20480 / 10, 0.45f);
  804. /*#ifdef SOUND
  805. music_player_worker_start(plugin_state->music_instance->worker);
  806. #endif*/
  807. plugin_state->gun_pos = GUN_SHOT_POS;
  808. plugin_state->gun_fired = true;
  809. plugin_state->fired = false;
  810. fire(plugin_state);
  811. } else if(plugin_state->gun_fired && !plugin_state->fired) {
  812. //furi_hal_speaker_stop();
  813. plugin_state->gun_fired = false;
  814. notification_message(plugin_state->notify, &sequence_short_sound);
  815. /*#ifdef SOUND
  816. music_player_worker_stop(plugin_state->music_instance->worker);
  817. #endif*/
  818. }
  819. } else {
  820. // Player is dead
  821. if(plugin_state->view_height > -10) plugin_state->view_height--;
  822. if(plugin_state->gun_pos > 1) plugin_state->gun_pos -= 2;
  823. }
  824. if(fabs(plugin_state->player.velocity) > (double)0.003) {
  825. updatePosition(
  826. sto_level_1,
  827. &(plugin_state->player.pos),
  828. plugin_state->player.dir.x * plugin_state->player.velocity * delta,
  829. plugin_state->player.dir.y * plugin_state->player.velocity * delta,
  830. false,
  831. plugin_state);
  832. } else {
  833. plugin_state->player.velocity = 0;
  834. }
  835. }
  836. }
  837. int32_t doom_app() {
  838. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  839. PluginState* plugin_state = malloc(sizeof(PluginState));
  840. doom_state_init(plugin_state);
  841. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  842. if(!plugin_state->mutex) {
  843. FURI_LOG_E("Doom_game", "cannot create mutex\r\n");
  844. furi_record_close(RECORD_NOTIFICATION);
  845. furi_message_queue_free(event_queue);
  846. free(plugin_state);
  847. return 255;
  848. }
  849. FuriTimer* timer =
  850. furi_timer_alloc(doom_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  851. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 12);
  852. // Set system callbacks
  853. ViewPort* view_port = view_port_alloc();
  854. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  855. view_port_input_callback_set(view_port, input_callback, event_queue);
  856. // Open GUI and register view_port
  857. Gui* gui = furi_record_open(RECORD_GUI);
  858. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  859. //////////////////////////////////
  860. plugin_state->init = false;
  861. PluginEvent event;
  862. #ifdef SOUND
  863. music_player_worker_load_rtttl_from_string(plugin_state->music_instance->worker, dsintro);
  864. music_player_worker_start(plugin_state->music_instance->worker);
  865. #endif
  866. // Call dolphin deed on game start
  867. dolphin_deed(DolphinDeedPluginGameStart);
  868. for(bool processing = true; processing;) {
  869. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  870. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  871. #ifdef SOUND
  872. furi_check(
  873. furi_mutex_acquire(plugin_state->music_instance->model_mutex, FuriWaitForever) ==
  874. FuriStatusOk);
  875. #endif
  876. if(event_status == FuriStatusOk) {
  877. // press events
  878. if(event.type == EventTypeKey) {
  879. if(event.input.key == InputKeyBack) {
  880. processing = false;
  881. #ifdef SOUND
  882. if(plugin_state->intro_sound) {
  883. furi_mutex_release(plugin_state->music_instance->model_mutex);
  884. music_player_worker_stop(plugin_state->music_instance->worker);
  885. }
  886. #endif
  887. }
  888. if(event.input.type == InputTypePress) {
  889. if(plugin_state->scene == INTRO && event.input.key == InputKeyOk) {
  890. plugin_state->scene = GAME_PLAY;
  891. initializeLevel(sto_level_1, plugin_state);
  892. #ifdef SOUND
  893. furi_mutex_release(plugin_state->music_instance->model_mutex);
  894. music_player_worker_stop(plugin_state->music_instance->worker);
  895. plugin_state->intro_sound = false;
  896. #endif
  897. goto skipintro;
  898. }
  899. //While playing game
  900. if(plugin_state->scene == GAME_PLAY) {
  901. // If the player is alive
  902. if(plugin_state->player.health > 0) {
  903. //Player speed
  904. if(event.input.key == InputKeyUp) {
  905. plugin_state->up = true;
  906. } else if(event.input.key == InputKeyDown) {
  907. plugin_state->down = true;
  908. }
  909. // Player rotation
  910. if(event.input.key == InputKeyRight) {
  911. plugin_state->right = true;
  912. } else if(event.input.key == InputKeyLeft) {
  913. plugin_state->left = true;
  914. }
  915. if(event.input.key == InputKeyOk) {
  916. /*#ifdef SOUND
  917. music_player_worker_load_rtttl_from_string(plugin_state->music_instance->worker, dspistol);
  918. #endif*/
  919. if(plugin_state->fired) {
  920. plugin_state->fired = false;
  921. } else {
  922. plugin_state->fired = true;
  923. }
  924. }
  925. } else {
  926. // Player is dead
  927. if(event.input.key == InputKeyOk) plugin_state->scene = INTRO;
  928. }
  929. }
  930. }
  931. if(event.input.type == InputTypeRelease) {
  932. if(plugin_state->player.health > 0) {
  933. //Player speed
  934. if(event.input.key == InputKeyUp) {
  935. plugin_state->up = false;
  936. } else if(event.input.key == InputKeyDown) {
  937. plugin_state->down = false;
  938. }
  939. // Player rotation
  940. if(event.input.key == InputKeyRight) {
  941. plugin_state->right = false;
  942. } else if(event.input.key == InputKeyLeft) {
  943. plugin_state->left = false;
  944. }
  945. }
  946. }
  947. }
  948. skipintro:
  949. if(event.type == EventTypeTick) {
  950. doom_game_tick(plugin_state);
  951. }
  952. }
  953. #ifdef SOUND
  954. furi_mutex_release(plugin_state->music_instance->model_mutex);
  955. #endif
  956. furi_mutex_release(plugin_state->mutex);
  957. view_port_update(view_port);
  958. }
  959. #ifdef SOUND
  960. music_player_worker_free(plugin_state->music_instance->worker);
  961. furi_mutex_free(plugin_state->music_instance->model_mutex);
  962. free(plugin_state->music_instance->model);
  963. free(plugin_state->music_instance);
  964. #endif
  965. furi_record_close(RECORD_NOTIFICATION);
  966. furi_timer_free(timer);
  967. view_port_enabled_set(view_port, false);
  968. gui_remove_view_port(gui, view_port);
  969. furi_record_close(RECORD_GUI);
  970. view_port_free(view_port);
  971. furi_mutex_free(plugin_state->mutex);
  972. furi_message_queue_free(event_queue);
  973. free(plugin_state);
  974. return 0;
  975. }