level.c 8.5 KB

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