level.c 5.4 KB

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