level.c 6.7 KB

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