level.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. return;
  79. }
  80. FURI_LOG_I("Game", "Drawing world");
  81. if (!draw_json_world_furi(level, json_data_str))
  82. {
  83. FURI_LOG_E("Game", "Failed to draw world");
  84. draw_town_world(level);
  85. furi_string_free(json_data_str);
  86. }
  87. else
  88. {
  89. FURI_LOG_I("Game", "Drawing enemies");
  90. furi_string_free(json_data_str);
  91. snprintf(file_path, sizeof(file_path),
  92. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  93. id, id);
  94. FURI_LOG_I("Game", "Loading enemy data from %s", file_path);
  95. FuriString *enemy_data_str = flipper_http_load_from_file(file_path);
  96. if (!enemy_data_str || furi_string_empty(enemy_data_str))
  97. {
  98. FURI_LOG_E("Game", "Failed to get enemy data");
  99. draw_town_world(level);
  100. return;
  101. }
  102. FURI_LOG_I("Game", "Looping through enemy data");
  103. // Loop through the array
  104. for (int i = 0; i < MAX_ENEMIES; i++)
  105. {
  106. FuriString *single_enemy_data = get_json_array_value_furi("enemy_data", i, enemy_data_str);
  107. if (!single_enemy_data || furi_string_empty(single_enemy_data))
  108. {
  109. // No more enemy elements found
  110. if (single_enemy_data)
  111. furi_string_free(single_enemy_data);
  112. break;
  113. }
  114. spawn_enemy_json_furi(level, manager, single_enemy_data);
  115. furi_string_free(single_enemy_data);
  116. }
  117. furi_string_free(enemy_data_str);
  118. FURI_LOG_I("Game", "Finished loading world data");
  119. }
  120. }
  121. static void level_start(Level *level, GameManager *manager, void *context)
  122. {
  123. if (!level || !context || !manager)
  124. {
  125. FURI_LOG_E("Game", "Level, context, or manager is NULL");
  126. return;
  127. }
  128. level_clear(level);
  129. player_spawn(level, manager);
  130. LevelContext *level_context = context;
  131. if (!level_context)
  132. {
  133. FURI_LOG_E("Game", "Level context is NULL");
  134. return;
  135. }
  136. // check if the world exists
  137. if (!world_exists(level_context->id))
  138. {
  139. FURI_LOG_E("Game", "World does not exist.. downloading now");
  140. FuriString *world_data = fetch_world(level_context->id);
  141. if (!world_data)
  142. {
  143. FURI_LOG_E("Game", "Failed to fetch world data");
  144. draw_town_world(level);
  145. return;
  146. }
  147. furi_string_free(world_data);
  148. set_world(level, manager, level_context->id);
  149. FURI_LOG_I("Game", "World set.");
  150. }
  151. else
  152. {
  153. FURI_LOG_I("Game", "World exists.. loading now");
  154. set_world(level, manager, level_context->id);
  155. FURI_LOG_I("Game", "World set.");
  156. }
  157. }
  158. static LevelContext *level_context_generic;
  159. static LevelContext *level_generic_alloc(const char *id, int index)
  160. {
  161. if (level_context_generic == NULL)
  162. {
  163. size_t heap_size = memmgr_get_free_heap();
  164. if (heap_size < sizeof(LevelContext))
  165. {
  166. FURI_LOG_E("Game", "Not enough heap to allocate level context");
  167. return NULL;
  168. }
  169. level_context_generic = malloc(sizeof(LevelContext));
  170. }
  171. snprintf(level_context_generic->id, sizeof(level_context_generic->id), "%s", id);
  172. level_context_generic->index = index;
  173. return level_context_generic;
  174. }
  175. static void level_generic_free()
  176. {
  177. if (level_context_generic != NULL)
  178. {
  179. free(level_context_generic);
  180. level_context_generic = NULL;
  181. }
  182. }
  183. static void free_level(Level *level, GameManager *manager, void *context)
  184. {
  185. UNUSED(level);
  186. UNUSED(manager);
  187. UNUSED(context);
  188. level_generic_free();
  189. }
  190. static void level_alloc_generic_world(Level *level, GameManager *manager, void *context)
  191. {
  192. UNUSED(manager);
  193. UNUSED(level);
  194. if (!level_context_generic)
  195. {
  196. FURI_LOG_E("Game", "Generic level context not set");
  197. return;
  198. }
  199. if (!context)
  200. {
  201. FURI_LOG_E("Game", "Context is NULL");
  202. return;
  203. }
  204. LevelContext *level_context = context;
  205. snprintf(level_context->id, sizeof(level_context->id), "%s", level_context_generic->id);
  206. level_context->index = level_context_generic->index;
  207. }
  208. const LevelBehaviour _generic_level = {
  209. .alloc = level_alloc_generic_world,
  210. .free = free_level,
  211. .start = level_start,
  212. .stop = NULL,
  213. .context_size = sizeof(LevelContext),
  214. };
  215. const LevelBehaviour *generic_level(const char *id, int index)
  216. {
  217. // free any old context before allocating a new one
  218. level_generic_free();
  219. level_context_generic = level_generic_alloc(id, index);
  220. return &_generic_level;
  221. }