world.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include <game/world.h>
  2. #include <game/storage.h>
  3. #include <flip_storage/storage.h>
  4. #include "game/icon.h"
  5. static IconSpec world_get_icon_spec(const char *name)
  6. {
  7. if (is_str(name, "house"))
  8. return (IconSpec){.id = ICON_ID_HOUSE, .icon = &I_icon_house_48x32px, .size = (Vector){48, 32}};
  9. else if (is_str(name, "man"))
  10. return (IconSpec){.id = ICON_ID_MAN, .icon = &I_icon_man_7x16, .size = (Vector){7, 16}};
  11. else if (is_str(name, "plant"))
  12. return (IconSpec){.id = ICON_ID_PLANT, .icon = &I_icon_plant_16x16, .size = (Vector){16, 16}};
  13. else if (is_str(name, "tree"))
  14. return (IconSpec){.id = ICON_ID_TREE, .icon = &I_icon_tree_16x16, .size = (Vector){16, 16}};
  15. else if (is_str(name, "woman"))
  16. return (IconSpec){.id = ICON_ID_WOMAN, .icon = &I_icon_woman_9x16, .size = (Vector){9, 16}};
  17. else if (is_str(name, "fence"))
  18. return (IconSpec){.id = ICON_ID_FENCE, .icon = &I_icon_fence_16x8px, .size = (Vector){16, 8}};
  19. else if (is_str(name, "fence_end"))
  20. return (IconSpec){.id = ICON_ID_FENCE_END, .icon = &I_icon_fence_end_16x8px, .size = (Vector){16, 8}};
  21. else if (is_str(name, "fence_vertical_end"))
  22. return (IconSpec){.id = ICON_ID_FENCE_VERTICAL_END, .icon = &I_icon_fence_vertical_end_6x8px, .size = (Vector){6, 8}};
  23. else if (is_str(name, "fence_vertical_start"))
  24. return (IconSpec){.id = ICON_ID_FENCE_VERTICAL_START, .icon = &I_icon_fence_vertical_start_6x15px, .size = (Vector){6, 15}};
  25. else if (is_str(name, "flower"))
  26. return (IconSpec){.id = ICON_ID_FLOWER, .icon = &I_icon_flower_16x16, .size = (Vector){16, 16}};
  27. else if (is_str(name, "lake_bottom"))
  28. return (IconSpec){.id = ICON_ID_LAKE_BOTTOM, .icon = &I_icon_lake_bottom_31x12px, .size = (Vector){31, 12}};
  29. else if (is_str(name, "lake_bottom_left"))
  30. return (IconSpec){.id = ICON_ID_LAKE_BOTTOM_LEFT, .icon = &I_icon_lake_bottom_left_24x22px, .size = (Vector){24, 22}};
  31. else if (is_str(name, "lake_bottom_right"))
  32. return (IconSpec){.id = ICON_ID_LAKE_BOTTOM_RIGHT, .icon = &I_icon_lake_bottom_right_24x22px, .size = (Vector){24, 22}};
  33. else if (is_str(name, "lake_left"))
  34. return (IconSpec){.id = ICON_ID_LAKE_LEFT, .icon = &I_icon_lake_left_11x31px, .size = (Vector){11, 31}};
  35. else if (is_str(name, "lake_right"))
  36. return (IconSpec){.id = ICON_ID_LAKE_RIGHT, .icon = &I_icon_lake_right_11x31, .size = (Vector){11, 31}};
  37. else if (is_str(name, "lake_top"))
  38. return (IconSpec){.id = ICON_ID_LAKE_TOP, .icon = &I_icon_lake_top_31x12px, .size = (Vector){31, 12}};
  39. else if (is_str(name, "lake_top_left"))
  40. return (IconSpec){.id = ICON_ID_LAKE_TOP_LEFT, .icon = &I_icon_lake_top_left_24x22px, .size = (Vector){24, 22}};
  41. else if (is_str(name, "lake_top_right"))
  42. return (IconSpec){.id = ICON_ID_LAKE_TOP_RIGHT, .icon = &I_icon_lake_top_right_24x22px, .size = (Vector){24, 22}};
  43. else if (is_str(name, "rock_large"))
  44. return (IconSpec){.id = ICON_ID_ROCK_LARGE, .icon = &I_icon_rock_large_18x19px, .size = (Vector){18, 19}};
  45. else if (is_str(name, "rock_medium"))
  46. return (IconSpec){.id = ICON_ID_ROCK_MEDIUM, .icon = &I_icon_rock_medium_16x14px, .size = (Vector){16, 14}};
  47. else if (is_str(name, "rock_small"))
  48. return (IconSpec){.id = ICON_ID_ROCK_SMALL, .icon = &I_icon_rock_small_10x8px, .size = (Vector){10, 8}};
  49. return (IconSpec){.id = -1, .icon = NULL, .size = (Vector){0, 0}};
  50. }
  51. bool world_json_draw(GameManager *manager, Level *level, const FuriString *json_data)
  52. {
  53. if (!json_data)
  54. {
  55. FURI_LOG_E("Game", "JSON data is NULL");
  56. return false;
  57. }
  58. // Pass 1: Count the total number of icons.
  59. int total_icons = 0;
  60. for (int i = 0; i < MAX_WORLD_OBJECTS; i++)
  61. {
  62. FuriString *data = get_json_array_value_furi("json_data", i, json_data);
  63. if (!data)
  64. break;
  65. FuriString *amount = get_json_value_furi("a", data);
  66. if (amount)
  67. {
  68. int count = atoi(furi_string_get_cstr(amount));
  69. if (count < 1)
  70. count = 1;
  71. total_icons += count;
  72. furi_string_free(amount);
  73. }
  74. furi_string_free(data);
  75. }
  76. FURI_LOG_I("Game", "Total icons to spawn: %d", total_icons);
  77. // Allocate the icon group context (local instance)
  78. IconGroupContext igctx;
  79. igctx.count = total_icons;
  80. igctx.icons = malloc(total_icons * sizeof(IconSpec));
  81. if (!igctx.icons)
  82. {
  83. FURI_LOG_E("Game", "Failed to allocate icon group array for %d icons", total_icons);
  84. return false;
  85. }
  86. GameContext *game_context = game_manager_game_context_get(manager);
  87. game_context->icon_count = total_icons;
  88. // Pass 2: Parse the JSON to fill the icon specs.
  89. int spec_index = 0;
  90. for (int i = 0; i < MAX_WORLD_OBJECTS; i++)
  91. {
  92. FuriString *data = get_json_array_value_furi("json_data", i, json_data);
  93. if (!data)
  94. break;
  95. /*
  96. i - icon name
  97. x - x position
  98. y - y position
  99. a - amount
  100. h - horizontal (true/false)
  101. */
  102. FuriString *icon_str = get_json_value_furi("i", data);
  103. FuriString *x_str = get_json_value_furi("x", data);
  104. FuriString *y_str = get_json_value_furi("y", data);
  105. FuriString *amount_str = get_json_value_furi("a", data);
  106. FuriString *horizontal_str = get_json_value_furi("h", data);
  107. if (!icon_str || !x_str || !y_str || !amount_str || !horizontal_str)
  108. {
  109. FURI_LOG_E("Game", "Incomplete icon data: %s", furi_string_get_cstr(data));
  110. if (icon_str)
  111. furi_string_free(icon_str);
  112. if (x_str)
  113. furi_string_free(x_str);
  114. if (y_str)
  115. furi_string_free(y_str);
  116. if (amount_str)
  117. furi_string_free(amount_str);
  118. if (horizontal_str)
  119. furi_string_free(horizontal_str);
  120. furi_string_free(data);
  121. continue;
  122. }
  123. int count = atoi(furi_string_get_cstr(amount_str));
  124. if (count < 1)
  125. count = 1;
  126. float base_x = atof_furi(x_str);
  127. float base_y = atof_furi(y_str);
  128. bool is_horizontal = (furi_string_cmp(horizontal_str, "true") == 0);
  129. int spacing = 17;
  130. for (int j = 0; j < count; j++)
  131. {
  132. IconSpec spec = world_get_icon_spec(furi_string_get_cstr(icon_str));
  133. if (!spec.icon)
  134. {
  135. FURI_LOG_E("Game", "Icon name not recognized: %s", furi_string_get_cstr(icon_str));
  136. continue;
  137. }
  138. if (is_horizontal)
  139. {
  140. spec.pos.x = base_x + (j * spacing);
  141. spec.pos.y = base_y;
  142. }
  143. else
  144. {
  145. spec.pos.x = base_x;
  146. spec.pos.y = base_y + (j * spacing);
  147. }
  148. igctx.icons[spec_index++] = spec;
  149. }
  150. furi_string_free(icon_str);
  151. furi_string_free(x_str);
  152. furi_string_free(y_str);
  153. furi_string_free(amount_str);
  154. furi_string_free(horizontal_str);
  155. furi_string_free(data);
  156. }
  157. // Spawn one icon group entity.
  158. Entity *groupEntity = level_add_entity(level, &icon_desc);
  159. IconGroupContext *entityContext = (IconGroupContext *)entity_context_get(groupEntity);
  160. if (entityContext)
  161. {
  162. memcpy(entityContext, &igctx, sizeof(IconGroupContext));
  163. }
  164. else
  165. {
  166. FURI_LOG_E("Game", "Failed to get entity context for icon group");
  167. free(igctx.icons);
  168. return false;
  169. }
  170. // Set the global pointer so that player collision logic can use it.
  171. g_current_icon_group = entityContext;
  172. FURI_LOG_I("Game", "Finished loading world data");
  173. return true;
  174. }
  175. static void world_draw_pvp(Level *level, GameManager *manager, void *context)
  176. {
  177. UNUSED(context);
  178. if (!manager || !level)
  179. {
  180. FURI_LOG_E("Game", "Manager or level is NULL");
  181. return;
  182. }
  183. GameContext *game_context = game_manager_game_context_get(manager);
  184. level_clear(level);
  185. FuriString *json_data_str = furi_string_alloc();
  186. furi_string_cat_str(json_data_str, "{\"name\":\"pvp_world\",\"author\":\"ChatGPT\",\"json_data\":[{\"i\":\"rock_medium\",\"x\":100,\"y\":100,\"a\":10,\"h\":true},{\"i\":\"rock_medium\",\"x\":400,\"y\":300,\"a\":6,\"h\":true},{\"i\":\"rock_small\",\"x\":600,\"y\":200,\"a\":8,\"h\":true},{\"i\":\"fence\",\"x\":50,\"y\":50,\"a\":10,\"h\":true},{\"i\":\"fence\",\"x\":250,\"y\":150,\"a\":12,\"h\":true},{\"i\":\"fence\",\"x\":550,\"y\":350,\"a\":12,\"h\":true},{\"i\":\"rock_large\",\"x\":400,\"y\":70,\"a\":12,\"h\":true},{\"i\":\"rock_large\",\"x\":200,\"y\":200,\"a\":6,\"h\":false},{\"i\":\"tree\",\"x\":5,\"y\":5,\"a\":45,\"h\":true},{\"i\":\"tree\",\"x\":5,\"y\":5,\"a\":20,\"h\":false},{\"i\":\"tree\",\"x\":22,\"y\":22,\"a\":44,\"h\":true},{\"i\":\"tree\",\"x\":22,\"y\":22,\"a\":20,\"h\":false},{\"i\":\"tree\",\"x\":5,\"y\":347,\"a\":45,\"h\":true},{\"i\":\"tree\",\"x\":5,\"y\":364,\"a\":45,\"h\":true},{\"i\":\"tree\",\"x\":735,\"y\":37,\"a\":18,\"h\":false},{\"i\":\"tree\",\"x\":752,\"y\":37,\"a\":18,\"h\":false}]}");
  187. if (!separate_world_data("pvp_world", json_data_str))
  188. {
  189. FURI_LOG_E("Game", "Failed to separate world data");
  190. }
  191. furi_string_free(json_data_str);
  192. level_set_world(level, manager, "pvp_world");
  193. game_context->is_switching_level = false;
  194. game_context->icon_offset = 0;
  195. if (!game_context->imu_present)
  196. {
  197. game_context->icon_offset += ((game_context->icon_count / 10) / 15);
  198. }
  199. player_spawn(level, manager);
  200. }
  201. static const LevelBehaviour _world_pvp = {
  202. .alloc = NULL,
  203. .free = NULL,
  204. .start = world_draw_pvp,
  205. .stop = NULL,
  206. .context_size = 0,
  207. };
  208. const LevelBehaviour *world_pvp()
  209. {
  210. return &_world_pvp;
  211. }
  212. FuriString *world_fetch(FlipperHTTP *fhttp, const char *name)
  213. {
  214. if (!name)
  215. {
  216. FURI_LOG_E("Game", "World name is NULL");
  217. return NULL;
  218. }
  219. if (!fhttp)
  220. {
  221. FURI_LOG_E("Game", "world_fetch: FlipperHTTP is NULL");
  222. return NULL;
  223. }
  224. char url[256];
  225. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/v8/get/world/%s/", name);
  226. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  227. fhttp->save_received_data = true;
  228. fhttp->state = IDLE;
  229. if (!flipper_http_request(fhttp, GET, url, "{\"Content-Type\": \"application/json\"}", NULL))
  230. {
  231. FURI_LOG_E("Game", "world_fetch: Failed to send HTTP request");
  232. return NULL;
  233. }
  234. fhttp->state = RECEIVING;
  235. while (fhttp->state != IDLE)
  236. {
  237. furi_delay_ms(100);
  238. }
  239. FuriString *returned_data = load_furi_world(name);
  240. if (!returned_data)
  241. {
  242. FURI_LOG_E("Game", "Failed to load world data from file");
  243. return NULL;
  244. }
  245. if (!separate_world_data((char *)name, returned_data))
  246. {
  247. FURI_LOG_E("Game", "Failed to separate world data");
  248. furi_string_free(returned_data);
  249. return NULL;
  250. }
  251. return returned_data;
  252. }