level.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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, training_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 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(28400, 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->ended_early = true;
  50. game_manager_game_stop(manager); // end game early
  51. furi_string_free(json_data_str);
  52. return;
  53. }
  54. FURI_LOG_I("Game", "Drawing world");
  55. if (!draw_json_world_furi(manager, level, json_data_str))
  56. {
  57. FURI_LOG_E("Game", "Failed to draw world");
  58. furi_string_free(json_data_str);
  59. }
  60. else
  61. {
  62. FURI_LOG_I("Game", "Drawing enemies");
  63. furi_string_free(json_data_str);
  64. snprintf(file_path, sizeof(file_path),
  65. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  66. id, id);
  67. FuriString *enemy_data_str = flipper_http_load_from_file(file_path);
  68. if (!enemy_data_str || furi_string_empty(enemy_data_str))
  69. {
  70. FURI_LOG_E("Game", "Failed to get enemy data");
  71. if (enemy_data_str)
  72. furi_string_free(enemy_data_str);
  73. return;
  74. }
  75. // Loop through the array
  76. for (int i = 0; i < MAX_ENEMIES; i++)
  77. {
  78. FuriString *single_enemy_data = get_json_array_value_furi("enemy_data", i, enemy_data_str);
  79. if (!single_enemy_data || furi_string_empty(single_enemy_data))
  80. {
  81. // No more enemy elements found
  82. if (single_enemy_data)
  83. furi_string_free(single_enemy_data);
  84. break;
  85. }
  86. spawn_enemy(level, manager, single_enemy_data);
  87. furi_string_free(single_enemy_data);
  88. }
  89. furi_string_free(enemy_data_str);
  90. // Draw NPCs
  91. FURI_LOG_I("Game", "Drawing NPCs");
  92. snprintf(file_path, sizeof(file_path),
  93. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_npc_data.json",
  94. id, id);
  95. FuriString *npc_data_str = flipper_http_load_from_file(file_path);
  96. if (!npc_data_str || furi_string_empty(npc_data_str))
  97. {
  98. FURI_LOG_E("Game", "Failed to get npc data");
  99. if (npc_data_str)
  100. furi_string_free(npc_data_str);
  101. return;
  102. }
  103. // Loop through the array
  104. for (int i = 0; i < MAX_NPCS; i++)
  105. {
  106. FuriString *single_npc_data = get_json_array_value_furi("npc_data", i, npc_data_str);
  107. if (!single_npc_data || furi_string_empty(single_npc_data))
  108. {
  109. // No more npc elements found
  110. if (single_npc_data)
  111. furi_string_free(single_npc_data);
  112. break;
  113. }
  114. spawn_npc(level, manager, single_npc_data);
  115. furi_string_free(single_npc_data);
  116. }
  117. furi_string_free(npc_data_str);
  118. FURI_LOG_I("Game", "World drawn");
  119. }
  120. }
  121. static void level_start(Level *level, GameManager *manager, void *context)
  122. {
  123. if (!manager || !level || !context)
  124. {
  125. FURI_LOG_E("Game", "Manager, level, or context is NULL");
  126. return;
  127. }
  128. GameContext *game_context = game_manager_game_context_get(manager);
  129. if (!level || !context)
  130. {
  131. FURI_LOG_E("Game", "Level, context, or manager is NULL");
  132. game_context->is_switching_level = false;
  133. return;
  134. }
  135. level_clear(level);
  136. LevelContext *level_context = context;
  137. if (!level_context)
  138. {
  139. FURI_LOG_E("Game", "Level context is NULL");
  140. game_context->is_switching_level = false;
  141. return;
  142. }
  143. // check if the world exists
  144. if (!world_exists(level_context->id))
  145. {
  146. FURI_LOG_E("Game", "World does not exist.. downloading now");
  147. FuriString *world_data = fetch_world(level_context->id);
  148. if (!world_data)
  149. {
  150. FURI_LOG_E("Game", "Failed to fetch world data");
  151. // draw_town_world(manager, level);
  152. game_context->is_switching_level = false;
  153. // furi_delay_ms(1000);
  154. player_spawn(level, manager);
  155. return;
  156. }
  157. furi_string_free(world_data);
  158. set_world(level, manager, level_context->id);
  159. FURI_LOG_I("Game", "World set.");
  160. // furi_delay_ms(1000);
  161. game_context->is_switching_level = false;
  162. }
  163. else
  164. {
  165. FURI_LOG_I("Game", "World exists.. loading now");
  166. set_world(level, manager, level_context->id);
  167. FURI_LOG_I("Game", "World set.");
  168. // furi_delay_ms(1000);
  169. game_context->is_switching_level = false;
  170. }
  171. game_context->icon_offset = 0;
  172. if (!game_context->imu_present)
  173. {
  174. game_context->icon_offset += ((game_context->icon_count / 10) / 15);
  175. }
  176. player_spawn(level, manager);
  177. }
  178. static LevelContext *level_context_generic;
  179. static LevelContext *level_generic_alloc(const char *id, int index)
  180. {
  181. if (level_context_generic == NULL)
  182. {
  183. size_t heap_size = memmgr_get_free_heap();
  184. if (heap_size < sizeof(LevelContext))
  185. {
  186. FURI_LOG_E("Game", "Not enough heap to allocate level context");
  187. return NULL;
  188. }
  189. level_context_generic = malloc(sizeof(LevelContext));
  190. }
  191. snprintf(level_context_generic->id, sizeof(level_context_generic->id), "%s", id);
  192. level_context_generic->index = index;
  193. return level_context_generic;
  194. }
  195. static void level_generic_free()
  196. {
  197. if (level_context_generic != NULL)
  198. {
  199. free(level_context_generic);
  200. level_context_generic = NULL;
  201. }
  202. }
  203. static void free_level(Level *level, GameManager *manager, void *context)
  204. {
  205. UNUSED(level);
  206. UNUSED(manager);
  207. UNUSED(context);
  208. level_generic_free();
  209. }
  210. static void level_alloc_generic_world(Level *level, GameManager *manager, void *context)
  211. {
  212. UNUSED(manager);
  213. UNUSED(level);
  214. if (!level_context_generic)
  215. {
  216. FURI_LOG_E("Game", "Generic level context not set");
  217. return;
  218. }
  219. if (!context)
  220. {
  221. FURI_LOG_E("Game", "Context is NULL");
  222. return;
  223. }
  224. LevelContext *level_context = context;
  225. snprintf(level_context->id, sizeof(level_context->id), "%s", level_context_generic->id);
  226. level_context->index = level_context_generic->index;
  227. }
  228. const LevelBehaviour _generic_level = {
  229. .alloc = level_alloc_generic_world,
  230. .free = free_level,
  231. .start = level_start,
  232. .stop = NULL,
  233. .context_size = sizeof(LevelContext),
  234. };
  235. const LevelBehaviour *generic_level(const char *id, int index)
  236. {
  237. // free any old context before allocating a new one
  238. level_generic_free();
  239. level_context_generic = level_generic_alloc(id, index);
  240. return &_generic_level;
  241. }