doom.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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, FuriMessageQueue* event_queue) {
  692. furi_assert(event_queue);
  693. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  694. furi_message_queue_put(event_queue, &event, 0);
  695. }
  696. static void doom_state_init(PluginState* const plugin_state) {
  697. plugin_state->notify = furi_record_open(RECORD_NOTIFICATION);
  698. plugin_state->num_entities = 0;
  699. plugin_state->num_static_entities = 0;
  700. plugin_state->scene = INTRO;
  701. plugin_state->gun_pos = 0;
  702. plugin_state->view_height = 0;
  703. plugin_state->init = true;
  704. plugin_state->up = false;
  705. plugin_state->down = false;
  706. plugin_state->left = false;
  707. plugin_state->right = false;
  708. plugin_state->fired = false;
  709. plugin_state->gun_fired = false;
  710. #ifdef SOUND
  711. plugin_state->music_instance = malloc(sizeof(MusicPlayer));
  712. plugin_state->music_instance->model = malloc(sizeof(MusicPlayerModel));
  713. memset(
  714. plugin_state->music_instance->model->duration_history,
  715. 0xff,
  716. MUSIC_PLAYER_SEMITONE_HISTORY_SIZE);
  717. memset(
  718. plugin_state->music_instance->model->semitone_history,
  719. 0xff,
  720. MUSIC_PLAYER_SEMITONE_HISTORY_SIZE);
  721. plugin_state->music_instance->model->volume = 2;
  722. plugin_state->music_instance->model_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  723. //plugin_state->music_instance->view_port = view_port_alloc();
  724. plugin_state->music_instance->worker = music_player_worker_alloc();
  725. //music_player_worker_set_volume(plugin_state->music_instance->worker, 0.75);
  726. music_player_worker_set_volume(
  727. plugin_state->music_instance->worker,
  728. MUSIC_PLAYER_VOLUMES[plugin_state->music_instance->model->volume]);
  729. plugin_state->intro_sound = true;
  730. //init_sound(plugin_state->music_instance);
  731. #endif
  732. }
  733. static void doom_game_update_timer_callback(FuriMessageQueue* event_queue) {
  734. furi_assert(event_queue);
  735. PluginEvent event = {.type = EventTypeTick};
  736. furi_message_queue_put(event_queue, &event, 0);
  737. }
  738. static void doom_game_tick(PluginState* const plugin_state) {
  739. if(plugin_state->scene == GAME_PLAY) {
  740. //fps();
  741. //player is alive
  742. if(plugin_state->player.health > 0) {
  743. if(plugin_state->up) {
  744. plugin_state->player.velocity +=
  745. ((double)MOV_SPEED - plugin_state->player.velocity) * (double).4;
  746. plugin_state->jogging = fabs(plugin_state->player.velocity) * MOV_SPEED_INV;
  747. //plugin_state->up = false;
  748. } else if(plugin_state->down) {
  749. plugin_state->player.velocity +=
  750. (-(double)MOV_SPEED - plugin_state->player.velocity) * (double).4;
  751. plugin_state->jogging = fabs(plugin_state->player.velocity) * MOV_SPEED_INV;
  752. //plugin_state->down = false;
  753. } else {
  754. plugin_state->player.velocity *= (double).5;
  755. plugin_state->jogging = fabs(plugin_state->player.velocity) * MOV_SPEED_INV;
  756. }
  757. if(plugin_state->right) {
  758. plugin_state->rot_speed = (double)ROT_SPEED * delta;
  759. plugin_state->old_dir_x = plugin_state->player.dir.x;
  760. plugin_state->player.dir.x =
  761. plugin_state->player.dir.x * cos(-(plugin_state->rot_speed)) -
  762. plugin_state->player.dir.y * sin(-(plugin_state->rot_speed));
  763. plugin_state->player.dir.y =
  764. plugin_state->old_dir_x * sin(-(plugin_state->rot_speed)) +
  765. plugin_state->player.dir.y * cos(-(plugin_state->rot_speed));
  766. plugin_state->old_plane_x = plugin_state->player.plane.x;
  767. plugin_state->player.plane.x =
  768. plugin_state->player.plane.x * cos(-(plugin_state->rot_speed)) -
  769. plugin_state->player.plane.y * sin(-(plugin_state->rot_speed));
  770. plugin_state->player.plane.y =
  771. plugin_state->old_plane_x * sin(-(plugin_state->rot_speed)) +
  772. plugin_state->player.plane.y * cos(-(plugin_state->rot_speed));
  773. //plugin_state->right = false;
  774. } else if(plugin_state->left) {
  775. plugin_state->rot_speed = (double)ROT_SPEED * delta;
  776. plugin_state->old_dir_x = plugin_state->player.dir.x;
  777. plugin_state->player.dir.x =
  778. plugin_state->player.dir.x * cos(plugin_state->rot_speed) -
  779. plugin_state->player.dir.y * sin(plugin_state->rot_speed);
  780. plugin_state->player.dir.y =
  781. plugin_state->old_dir_x * sin(plugin_state->rot_speed) +
  782. plugin_state->player.dir.y * cos(plugin_state->rot_speed);
  783. plugin_state->old_plane_x = plugin_state->player.plane.x;
  784. plugin_state->player.plane.x =
  785. plugin_state->player.plane.x * cos(plugin_state->rot_speed) -
  786. plugin_state->player.plane.y * sin(plugin_state->rot_speed);
  787. plugin_state->player.plane.y =
  788. plugin_state->old_plane_x * sin(plugin_state->rot_speed) +
  789. plugin_state->player.plane.y * cos(plugin_state->rot_speed);
  790. //plugin_state->left = false;
  791. }
  792. plugin_state->view_height =
  793. fabs(sin((double)furi_get_tick() * (double)JOGGING_SPEED)) * 6 *
  794. plugin_state->jogging;
  795. if(plugin_state->gun_pos > GUN_TARGET_POS) {
  796. // Right after fire
  797. plugin_state->gun_pos -= 1;
  798. } else if(plugin_state->gun_pos < GUN_TARGET_POS) {
  799. plugin_state->gun_pos += 2;
  800. } else if(!plugin_state->gun_fired && plugin_state->fired) {
  801. //furi_hal_speaker_start(20480 / 10, 0.45f);
  802. /*#ifdef SOUND
  803. music_player_worker_start(plugin_state->music_instance->worker);
  804. #endif*/
  805. plugin_state->gun_pos = GUN_SHOT_POS;
  806. plugin_state->gun_fired = true;
  807. plugin_state->fired = false;
  808. fire(plugin_state);
  809. } else if(plugin_state->gun_fired && !plugin_state->fired) {
  810. //furi_hal_speaker_stop();
  811. plugin_state->gun_fired = false;
  812. notification_message(plugin_state->notify, &sequence_short_sound);
  813. /*#ifdef SOUND
  814. music_player_worker_stop(plugin_state->music_instance->worker);
  815. #endif*/
  816. }
  817. } else {
  818. // Player is dead
  819. if(plugin_state->view_height > -10) plugin_state->view_height--;
  820. if(plugin_state->gun_pos > 1) plugin_state->gun_pos -= 2;
  821. }
  822. if(fabs(plugin_state->player.velocity) > (double)0.003) {
  823. updatePosition(
  824. sto_level_1,
  825. &(plugin_state->player.pos),
  826. plugin_state->player.dir.x * plugin_state->player.velocity * delta,
  827. plugin_state->player.dir.y * plugin_state->player.velocity * delta,
  828. false,
  829. plugin_state);
  830. } else {
  831. plugin_state->player.velocity = 0;
  832. }
  833. }
  834. }
  835. int32_t doom_app() {
  836. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  837. PluginState* plugin_state = malloc(sizeof(PluginState));
  838. doom_state_init(plugin_state);
  839. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  840. if(!plugin_state->mutex) {
  841. FURI_LOG_E("Doom_game", "cannot create mutex\r\n");
  842. furi_record_close(RECORD_NOTIFICATION);
  843. furi_message_queue_free(event_queue);
  844. free(plugin_state);
  845. return 255;
  846. }
  847. FuriTimer* timer =
  848. furi_timer_alloc(doom_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  849. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 12);
  850. // Set system callbacks
  851. ViewPort* view_port = view_port_alloc();
  852. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  853. view_port_input_callback_set(view_port, input_callback, event_queue);
  854. // Open GUI and register view_port
  855. Gui* gui = furi_record_open(RECORD_GUI);
  856. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  857. //////////////////////////////////
  858. plugin_state->init = false;
  859. PluginEvent event;
  860. #ifdef SOUND
  861. music_player_worker_load_rtttl_from_string(plugin_state->music_instance->worker, dsintro);
  862. music_player_worker_start(plugin_state->music_instance->worker);
  863. #endif
  864. // Call dolphin deed on game start
  865. dolphin_deed(DolphinDeedPluginGameStart);
  866. for(bool processing = true; processing;) {
  867. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  868. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  869. #ifdef SOUND
  870. furi_check(
  871. furi_mutex_acquire(plugin_state->music_instance->model_mutex, FuriWaitForever) ==
  872. FuriStatusOk);
  873. #endif
  874. if(event_status == FuriStatusOk) {
  875. // press events
  876. if(event.type == EventTypeKey) {
  877. if(event.input.key == InputKeyBack) {
  878. processing = false;
  879. #ifdef SOUND
  880. if(plugin_state->intro_sound) {
  881. furi_mutex_release(plugin_state->music_instance->model_mutex);
  882. music_player_worker_stop(plugin_state->music_instance->worker);
  883. }
  884. #endif
  885. }
  886. if(event.input.type == InputTypePress) {
  887. if(plugin_state->scene == INTRO && event.input.key == InputKeyOk) {
  888. plugin_state->scene = GAME_PLAY;
  889. initializeLevel(sto_level_1, plugin_state);
  890. #ifdef SOUND
  891. furi_mutex_release(plugin_state->music_instance->model_mutex);
  892. music_player_worker_stop(plugin_state->music_instance->worker);
  893. plugin_state->intro_sound = false;
  894. #endif
  895. goto skipintro;
  896. }
  897. //While playing game
  898. if(plugin_state->scene == GAME_PLAY) {
  899. // If the player is alive
  900. if(plugin_state->player.health > 0) {
  901. //Player speed
  902. if(event.input.key == InputKeyUp) {
  903. plugin_state->up = true;
  904. } else if(event.input.key == InputKeyDown) {
  905. plugin_state->down = true;
  906. }
  907. // Player rotation
  908. if(event.input.key == InputKeyRight) {
  909. plugin_state->right = true;
  910. } else if(event.input.key == InputKeyLeft) {
  911. plugin_state->left = true;
  912. }
  913. if(event.input.key == InputKeyOk) {
  914. /*#ifdef SOUND
  915. music_player_worker_load_rtttl_from_string(plugin_state->music_instance->worker, dspistol);
  916. #endif*/
  917. if(plugin_state->fired) {
  918. plugin_state->fired = false;
  919. } else {
  920. plugin_state->fired = true;
  921. }
  922. }
  923. } else {
  924. // Player is dead
  925. if(event.input.key == InputKeyOk) plugin_state->scene = INTRO;
  926. }
  927. }
  928. }
  929. if(event.input.type == InputTypeRelease) {
  930. if(plugin_state->player.health > 0) {
  931. //Player speed
  932. if(event.input.key == InputKeyUp) {
  933. plugin_state->up = false;
  934. } else if(event.input.key == InputKeyDown) {
  935. plugin_state->down = false;
  936. }
  937. // Player rotation
  938. if(event.input.key == InputKeyRight) {
  939. plugin_state->right = false;
  940. } else if(event.input.key == InputKeyLeft) {
  941. plugin_state->left = false;
  942. }
  943. }
  944. }
  945. }
  946. skipintro:
  947. if(event.type == EventTypeTick) {
  948. doom_game_tick(plugin_state);
  949. }
  950. }
  951. #ifdef SOUND
  952. furi_mutex_release(plugin_state->music_instance->model_mutex);
  953. #endif
  954. furi_mutex_release(plugin_state->mutex);
  955. view_port_update(view_port);
  956. }
  957. #ifdef SOUND
  958. music_player_worker_free(plugin_state->music_instance->worker);
  959. furi_mutex_free(plugin_state->music_instance->model_mutex);
  960. free(plugin_state->music_instance->model);
  961. free(plugin_state->music_instance);
  962. #endif
  963. furi_record_close(RECORD_NOTIFICATION);
  964. furi_timer_free(timer);
  965. view_port_enabled_set(view_port, false);
  966. gui_remove_view_port(gui, view_port);
  967. furi_record_close(RECORD_GUI);
  968. view_port_free(view_port);
  969. furi_mutex_free(plugin_state->mutex);
  970. furi_message_queue_free(event_queue);
  971. free(plugin_state);
  972. return 0;
  973. }