level.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include <game/level.h>
  2. #include <flip_storage/storage.h>
  3. #include <game/storage.h>
  4. #include <callback/game.h>
  5. bool allocate_level(GameManager *manager, int index)
  6. {
  7. GameContext *game_context = game_manager_game_context_get(manager);
  8. // open the world list from storage, then create a level for each world
  9. char file_path[128];
  10. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  11. FuriString *world_list = flipper_http_load_from_file(file_path);
  12. if (!world_list)
  13. {
  14. FURI_LOG_E("Game", "Failed to load world list");
  15. game_context->levels[0] = game_manager_add_level(manager, story_world());
  16. game_context->level_count = 1;
  17. return false;
  18. }
  19. FuriString *world_name = get_json_array_value_furi("worlds", index, world_list);
  20. if (!world_name)
  21. {
  22. FURI_LOG_E("Game", "Failed to get world name");
  23. furi_string_free(world_list);
  24. return false;
  25. }
  26. FURI_LOG_I("Game", "Allocating level %d for world %s", index, furi_string_get_cstr(world_name));
  27. game_context->levels[index] = game_manager_add_level(manager, generic_level(furi_string_get_cstr(world_name), index));
  28. furi_string_free(world_name);
  29. furi_string_free(world_list);
  30. return true;
  31. }
  32. void level_set_world(Level *level, GameManager *manager, char *id)
  33. {
  34. char file_path[256];
  35. snprintf(file_path, sizeof(file_path),
  36. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json",
  37. id, id);
  38. FuriString *json_data_str = flipper_http_load_from_file(file_path);
  39. if (!json_data_str || furi_string_empty(json_data_str))
  40. {
  41. FURI_LOG_E("Game", "Failed to load json data from file");
  42. if (json_data_str)
  43. furi_string_free(json_data_str);
  44. return;
  45. }
  46. if (!is_enough_heap(20000, true))
  47. {
  48. FURI_LOG_E("Game", "level_set_world: Not enough heap memory.. ending game early.");
  49. GameContext *game_context = game_manager_game_context_get(manager);
  50. game_context->end_reason = GAME_END_MEMORY;
  51. game_context->ended_early = true;
  52. game_manager_game_stop(manager); // end game early
  53. furi_string_free(json_data_str);
  54. return;
  55. }
  56. FURI_LOG_I("Game", "Drawing world");
  57. if (!world_json_draw(manager, level, json_data_str))
  58. {
  59. FURI_LOG_E("Game", "Failed to draw world");
  60. furi_string_free(json_data_str);
  61. }
  62. else
  63. {
  64. FURI_LOG_I("Game", "Drawing enemies");
  65. furi_string_free(json_data_str);
  66. snprintf(file_path, sizeof(file_path),
  67. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  68. id, id);
  69. FuriString *enemy_data_str = flipper_http_load_from_file(file_path);
  70. if (!enemy_data_str || furi_string_empty(enemy_data_str))
  71. {
  72. FURI_LOG_E("Game", "Failed to get enemy data");
  73. if (enemy_data_str)
  74. furi_string_free(enemy_data_str);
  75. return;
  76. }
  77. // Loop through the array
  78. for (int i = 0; i < MAX_ENEMIES; i++)
  79. {
  80. FuriString *single_enemy_data = get_json_array_value_furi("enemy_data", i, enemy_data_str);
  81. if (!single_enemy_data || furi_string_empty(single_enemy_data))
  82. {
  83. // No more enemy elements found
  84. if (single_enemy_data)
  85. furi_string_free(single_enemy_data);
  86. break;
  87. }
  88. enemy_spawn(level, manager, single_enemy_data);
  89. furi_string_free(single_enemy_data);
  90. }
  91. furi_string_free(enemy_data_str);
  92. // Draw NPCs
  93. FURI_LOG_I("Game", "Drawing NPCs");
  94. snprintf(file_path, sizeof(file_path),
  95. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_npc_data.json",
  96. id, id);
  97. FuriString *npc_data_str = flipper_http_load_from_file(file_path);
  98. if (!npc_data_str || furi_string_empty(npc_data_str))
  99. {
  100. FURI_LOG_E("Game", "Failed to get npc data");
  101. if (npc_data_str)
  102. furi_string_free(npc_data_str);
  103. return;
  104. }
  105. // Loop through the array
  106. for (int i = 0; i < MAX_NPCS; i++)
  107. {
  108. FuriString *single_npc_data = get_json_array_value_furi("npc_data", i, npc_data_str);
  109. if (!single_npc_data || furi_string_empty(single_npc_data))
  110. {
  111. // No more npc elements found
  112. if (single_npc_data)
  113. furi_string_free(single_npc_data);
  114. break;
  115. }
  116. npc_spawn(level, manager, single_npc_data);
  117. furi_string_free(single_npc_data);
  118. }
  119. furi_string_free(npc_data_str);
  120. FURI_LOG_I("Game", "World drawn");
  121. }
  122. }
  123. static void level_start(Level *level, GameManager *manager, void *context)
  124. {
  125. if (!manager || !level || !context)
  126. {
  127. FURI_LOG_E("Game", "Manager, level, or context is NULL");
  128. return;
  129. }
  130. GameContext *game_context = game_manager_game_context_get(manager);
  131. if (!level || !context)
  132. {
  133. FURI_LOG_E("Game", "Level, context, or manager is NULL");
  134. game_context->is_switching_level = false;
  135. return;
  136. }
  137. level_clear(level);
  138. LevelContext *level_context = context;
  139. if (!level_context)
  140. {
  141. FURI_LOG_E("Game", "Level context is NULL");
  142. game_context->is_switching_level = false;
  143. return;
  144. }
  145. // check if the world exists
  146. if (!world_exists(level_context->id))
  147. {
  148. FURI_LOG_E("Game", "World does not exist.. downloading now");
  149. FuriString *world_data = NULL;
  150. if (game_context->game_mode != GAME_MODE_STORY)
  151. {
  152. if (game_context->fhttp)
  153. {
  154. flipper_http_free(game_context->fhttp);
  155. }
  156. game_context->fhttp = flipper_http_alloc();
  157. if (!game_context->fhttp)
  158. {
  159. FURI_LOG_E("Game", "Failed to allocate FlipperHTTP");
  160. game_context->is_switching_level = false;
  161. game_context->ended_early = true;
  162. game_context->end_reason = GAME_END_MEMORY;
  163. player_spawn(level, manager);
  164. return;
  165. }
  166. flipper_http_websocket_stop(game_context->fhttp); // close websocket if open
  167. furi_delay_ms(200);
  168. world_data = world_fetch(game_context->fhttp, level_context->id);
  169. furi_delay_ms(200);
  170. game_start_ws(game_context->fhttp, game_ws_lobby_name); // start websocket again
  171. }
  172. else
  173. {
  174. FlipperHTTP *fhttp = flipper_http_alloc();
  175. if (!fhttp)
  176. {
  177. FURI_LOG_E("Game", "Failed to allocate FlipperHTTP");
  178. game_context->is_switching_level = false;
  179. game_context->ended_early = true;
  180. game_context->end_reason = GAME_END_MEMORY;
  181. player_spawn(level, manager);
  182. return;
  183. }
  184. world_data = world_fetch(fhttp, "pvp_world");
  185. flipper_http_free(fhttp);
  186. }
  187. if (!world_data)
  188. {
  189. FURI_LOG_E("Game", "Failed to fetch world data");
  190. // draw_town_world(manager, level);
  191. game_context->is_switching_level = false;
  192. game_context->ended_early = true;
  193. game_context->end_reason = GAME_END_NETWORK;
  194. // furi_delay_ms(1000);
  195. player_spawn(level, manager);
  196. return;
  197. }
  198. furi_string_free(world_data);
  199. level_set_world(level, manager, level_context->id);
  200. FURI_LOG_I("Game", "World set.");
  201. // furi_delay_ms(1000);
  202. game_context->is_switching_level = false;
  203. }
  204. else
  205. {
  206. FURI_LOG_I("Game", "World exists.. loading now");
  207. level_set_world(level, manager, level_context->id);
  208. FURI_LOG_I("Game", "World set.");
  209. // furi_delay_ms(1000);
  210. game_context->is_switching_level = false;
  211. }
  212. game_context->icon_offset = 0;
  213. if (!game_context->imu_present)
  214. {
  215. game_context->icon_offset += ((game_context->icon_count / 10) / 15);
  216. }
  217. player_spawn(level, manager);
  218. }
  219. static LevelContext *level_context_generic;
  220. static LevelContext *level_generic_alloc(const char *id, int index)
  221. {
  222. if (level_context_generic == NULL)
  223. {
  224. size_t heap_size = memmgr_get_free_heap();
  225. if (heap_size < sizeof(LevelContext))
  226. {
  227. FURI_LOG_E("Game", "Not enough heap to allocate level context");
  228. return NULL;
  229. }
  230. level_context_generic = malloc(sizeof(LevelContext));
  231. }
  232. snprintf(level_context_generic->id, sizeof(level_context_generic->id), "%s", id);
  233. level_context_generic->index = index;
  234. return level_context_generic;
  235. }
  236. static void level_generic_free()
  237. {
  238. if (level_context_generic != NULL)
  239. {
  240. free(level_context_generic);
  241. level_context_generic = NULL;
  242. }
  243. }
  244. static void free_level(Level *level, GameManager *manager, void *context)
  245. {
  246. UNUSED(level);
  247. UNUSED(manager);
  248. UNUSED(context);
  249. level_generic_free();
  250. }
  251. static void level_alloc_generic_world(Level *level, GameManager *manager, void *context)
  252. {
  253. UNUSED(manager);
  254. UNUSED(level);
  255. if (!level_context_generic)
  256. {
  257. FURI_LOG_E("Game", "Generic level context not set");
  258. return;
  259. }
  260. if (!context)
  261. {
  262. FURI_LOG_E("Game", "Context is NULL");
  263. return;
  264. }
  265. LevelContext *level_context = context;
  266. snprintf(level_context->id, sizeof(level_context->id), "%s", level_context_generic->id);
  267. level_context->index = level_context_generic->index;
  268. }
  269. const LevelBehaviour _generic_level = {
  270. .alloc = level_alloc_generic_world,
  271. .free = free_level,
  272. .start = level_start,
  273. .stop = NULL,
  274. .context_size = sizeof(LevelContext),
  275. };
  276. const LevelBehaviour *generic_level(const char *id, int index)
  277. {
  278. // free any old context before allocating a new one
  279. level_generic_free();
  280. level_context_generic = level_generic_alloc(id, index);
  281. return &_generic_level;
  282. }