level.c 7.2 KB

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