level.c 6.4 KB

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