level.c 5.5 KB

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