world.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include <game/world.h>
  2. #include <game/storage.h>
  3. #include <flip_storage/storage.h>
  4. void draw_bounds(Canvas *canvas)
  5. {
  6. // Draw the outer bounds adjusted by camera offset
  7. // we draw this last to ensure users can see the bounds
  8. canvas_draw_frame(canvas, -camera_x, -camera_y, WORLD_WIDTH, WORLD_HEIGHT);
  9. }
  10. bool draw_json_world(Level *level, const char *json_data)
  11. {
  12. int levels_added = 0;
  13. for (int i = 0; i < MAX_WORLD_OBJECTS; i++)
  14. {
  15. char *data = get_json_array_value("json_data", i, json_data);
  16. if (!data)
  17. {
  18. break;
  19. }
  20. char *icon = get_json_value("icon", data);
  21. char *x = get_json_value("x", data);
  22. char *y = get_json_value("y", data);
  23. char *amount = get_json_value("amount", data);
  24. char *horizontal = get_json_value("horizontal", data);
  25. if (!icon || !x || !y || !amount || !horizontal)
  26. {
  27. FURI_LOG_E("Game", "Failed Data: %s", data);
  28. // Free everything carefully
  29. if (data)
  30. free(data);
  31. if (icon)
  32. free(icon);
  33. if (x)
  34. free(x);
  35. if (y)
  36. free(y);
  37. if (amount)
  38. free(amount);
  39. if (horizontal)
  40. free(horizontal);
  41. level_clear(level);
  42. return false;
  43. }
  44. int count = atoi(amount);
  45. if (count < 2)
  46. {
  47. // Just one icon
  48. spawn_icon(
  49. level,
  50. icon,
  51. atoi(x),
  52. atoi(y));
  53. }
  54. else
  55. {
  56. bool is_horizontal = (strcmp(horizontal, "true") == 0);
  57. spawn_icon_line(
  58. level,
  59. icon,
  60. atoi(x),
  61. atoi(y),
  62. count,
  63. is_horizontal);
  64. }
  65. free(data);
  66. free(icon);
  67. free(x);
  68. free(y);
  69. free(amount);
  70. free(horizontal);
  71. levels_added++;
  72. }
  73. return levels_added > 0;
  74. }
  75. bool draw_json_world_furi(Level *level, const FuriString *json_data)
  76. {
  77. if (!json_data)
  78. {
  79. FURI_LOG_E("Game", "JSON data is NULL");
  80. return false;
  81. }
  82. int levels_added = 0;
  83. FURI_LOG_I("Game", "Looping through world data");
  84. for (int i = 0; i < MAX_WORLD_OBJECTS; i++)
  85. {
  86. FURI_LOG_I("Game", "Looping through world data: %d", i);
  87. FuriString *data = get_json_array_value_furi("json_data", i, json_data);
  88. if (!data)
  89. {
  90. break;
  91. }
  92. FuriString *icon = get_json_value_furi("icon", data);
  93. FuriString *x = get_json_value_furi("x", data);
  94. FuriString *y = get_json_value_furi("y", data);
  95. FuriString *amount = get_json_value_furi("amount", data);
  96. FuriString *horizontal = get_json_value_furi("horizontal", data);
  97. if (!icon || !x || !y || !amount || !horizontal)
  98. {
  99. FURI_LOG_E("Game", "Failed Data: %s", furi_string_get_cstr(data));
  100. if (data)
  101. furi_string_free(data);
  102. if (icon)
  103. furi_string_free(icon);
  104. if (x)
  105. furi_string_free(x);
  106. if (y)
  107. furi_string_free(y);
  108. if (amount)
  109. furi_string_free(amount);
  110. if (horizontal)
  111. furi_string_free(horizontal);
  112. level_clear(level);
  113. return false;
  114. }
  115. int count = atoi(furi_string_get_cstr(amount));
  116. if (count < 2)
  117. {
  118. // Just one icon
  119. spawn_icon(
  120. level,
  121. furi_string_get_cstr(icon),
  122. atoi(furi_string_get_cstr(x)),
  123. atoi(furi_string_get_cstr(y)));
  124. }
  125. else
  126. {
  127. bool is_horizontal = (furi_string_cmp(horizontal, "true") == 0);
  128. spawn_icon_line(
  129. level,
  130. furi_string_get_cstr(icon),
  131. atoi(furi_string_get_cstr(x)),
  132. atoi(furi_string_get_cstr(y)),
  133. count,
  134. is_horizontal);
  135. }
  136. furi_string_free(data);
  137. furi_string_free(icon);
  138. furi_string_free(x);
  139. furi_string_free(y);
  140. furi_string_free(amount);
  141. furi_string_free(horizontal);
  142. levels_added++;
  143. }
  144. FURI_LOG_I("Game", "Finished loading world data");
  145. return levels_added > 0;
  146. }
  147. void draw_tree_world(Level *level)
  148. {
  149. // Spawn two full left/up tree lines
  150. for (int i = 0; i < 2; i++)
  151. {
  152. // Horizontal line of 22 icons
  153. spawn_icon_line(level, "tree", 5, 2 + i * 17, 22, true);
  154. // Vertical line of 11 icons
  155. spawn_icon_line(level, "tree", 5 + i * 17, 2, 11, false);
  156. }
  157. // Spawn two full down tree lines
  158. for (int i = 9; i < 11; i++)
  159. {
  160. // Horizontal line of 22 icons
  161. spawn_icon_line(level, "tree", 5, 2 + i * 17, 22, true);
  162. }
  163. // Spawn two full right tree lines
  164. for (int i = 20; i < 22; i++)
  165. {
  166. // Vertical line of 8 icons starting further down (y=50)
  167. spawn_icon_line(level, "tree", 5 + i * 17, 50, 8, false);
  168. }
  169. // Labyrinth lines
  170. // Third line (14 left, then a gap, then 3 middle)
  171. spawn_icon_line(level, "tree", 5, 2 + 2 * 17, 14, true);
  172. spawn_icon_line(level, "tree", 5 + 16 * 17, 2 + 2 * 17, 3, true);
  173. // Fourth line (3 left, 6 middle, 4 right)
  174. spawn_icon_line(level, "tree", 5, 2 + 3 * 17, 3, true); // 3 left
  175. spawn_icon_line(level, "tree", 5 + 7 * 17, 2 + 3 * 17, 6, true); // 6 middle
  176. spawn_icon_line(level, "tree", 5 + 15 * 17, 2 + 3 * 17, 4, true); // 4 right
  177. // Fifth line (6 left, 7 middle)
  178. spawn_icon_line(level, "tree", 5, 2 + 4 * 17, 6, true);
  179. spawn_icon_line(level, "tree", 5 + 7 * 17, 2 + 4 * 17, 7, true);
  180. // Sixth line (5 left, 3 middle, 7 right)
  181. spawn_icon_line(level, "tree", 5, 2 + 5 * 17, 5, true); // 5 left
  182. spawn_icon_line(level, "tree", 5 + 7 * 17, 2 + 5 * 17, 3, true); // 3 middle
  183. spawn_icon_line(level, "tree", 5 + 15 * 17, 2 + 5 * 17, 7, true); // 7 right
  184. // Seventh line (0 left, 7 middle, 4 right)
  185. spawn_icon_line(level, "tree", 5 + 6 * 17, 2 + 6 * 17, 7, true); // 7 middle
  186. spawn_icon_line(level, "tree", 5 + 14 * 17, 2 + 6 * 17, 4, true); // 4 right
  187. // Eighth line (4 left, 3 middle, 4 right)
  188. spawn_icon_line(level, "tree", 5, 2 + 7 * 17, 4, true); // 4 left
  189. spawn_icon_line(level, "tree", 5 + 7 * 17, 2 + 7 * 17, 3, true); // 3 middle
  190. spawn_icon_line(level, "tree", 5 + 15 * 17, 2 + 7 * 17, 4, true); // 4 right
  191. // Ninth line (3 left, 1 middle, 3 right)
  192. spawn_icon_line(level, "tree", 5, 2 + 8 * 17, 3, true); // 3 left
  193. spawn_icon_line(level, "tree", 5 + 5 * 17, 2 + 8 * 17, 1, true); // 1 middle
  194. spawn_icon_line(level, "tree", 5 + 11 * 17, 2 + 8 * 17, 3, true); // 3 right
  195. }
  196. void draw_town_world(Level *level)
  197. {
  198. // house-fence group 1
  199. spawn_icon(level, "house", 164, 40);
  200. spawn_icon(level, "fence", 148, 64);
  201. spawn_icon(level, "fence", 164, 64);
  202. spawn_icon(level, "fence_end", 180, 64);
  203. // house-fence group 4 (the left of group 1)
  204. spawn_icon(level, "house", 110, 40);
  205. spawn_icon(level, "fence", 96, 64);
  206. spawn_icon(level, "fence", 110, 64);
  207. spawn_icon(level, "fence_end", 126, 64);
  208. // house-fence group 5 (the left of group 4)
  209. spawn_icon(level, "house", 56, 40);
  210. spawn_icon(level, "fence", 40, 64);
  211. spawn_icon(level, "fence", 56, 64);
  212. spawn_icon(level, "fence_end", 72, 64);
  213. // line of fences on the 8th row (using spawn_icon_line)
  214. spawn_icon_line(level, "fence", 8, 96, 10, true);
  215. // plants spaced out underneath the fences
  216. spawn_icon_line(level, "plant", 40, 110, 6, true);
  217. spawn_icon_line(level, "flower", 40, 140, 6, true);
  218. // man and woman
  219. spawn_icon(level, "man", 156, 110);
  220. spawn_icon(level, "woman", 164, 110);
  221. // lake
  222. // Top row
  223. spawn_icon(level, "lake_top_left", 240, 62);
  224. spawn_icon(level, "lake_top", 264, 57);
  225. spawn_icon(level, "lake_top_right", 295, 62);
  226. // Middle row
  227. spawn_icon(level, "lake_left", 231, 84);
  228. spawn_icon(level, "lake_right", 304, 84);
  229. // Bottom row
  230. spawn_icon(level, "lake_bottom_left", 240, 115);
  231. spawn_icon(level, "lake_bottom", 264, 120);
  232. spawn_icon(level, "lake_bottom_right", 295, 115);
  233. // Spawn two full left/up tree lines
  234. for (int i = 0; i < 2; i++)
  235. {
  236. // Horizontal line of 22 icons
  237. spawn_icon_line(level, "tree", 5, 2 + i * 17, 22, true);
  238. // Vertical line of 11 icons
  239. spawn_icon_line(level, "tree", 5 + i * 17, 2, 11, false);
  240. }
  241. // Spawn two full down tree lines
  242. for (int i = 9; i < 11; i++)
  243. {
  244. // Horizontal line of 22 icons
  245. spawn_icon_line(level, "tree", 5, 2 + i * 17, 22, true);
  246. }
  247. // Spawn two full right tree lines
  248. for (int i = 20; i < 22; i++)
  249. {
  250. // Vertical line of 8 icons starting further down (y=50)
  251. spawn_icon_line(level, "tree", 5 + i * 17, 50, 8, false);
  252. }
  253. }
  254. FuriString *fetch_world(const char *name)
  255. {
  256. if (!name)
  257. {
  258. FURI_LOG_E("Game", "World name is NULL");
  259. return NULL;
  260. }
  261. if (!app_instance)
  262. {
  263. // as long as the game is running, app_instance should be non-NULL
  264. FURI_LOG_E("Game", "App instance is NULL");
  265. return NULL;
  266. }
  267. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  268. {
  269. FURI_LOG_E("Game", "Failed to initialize HTTP");
  270. return NULL;
  271. }
  272. char url[256];
  273. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/v2/get/world/%s/", name);
  274. snprintf(fhttp.file_path, sizeof(fhttp.file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  275. fhttp.save_received_data = true;
  276. if (!flipper_http_get_request_with_headers(url, "{\"Content-Type\": \"application/json\"}"))
  277. {
  278. FURI_LOG_E("Game", "Failed to send HTTP request");
  279. flipper_http_deinit();
  280. return NULL;
  281. }
  282. fhttp.state = RECEIVING;
  283. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  284. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  285. {
  286. // Wait for the request to be received
  287. furi_delay_ms(100);
  288. }
  289. furi_timer_stop(fhttp.get_timeout_timer);
  290. if (fhttp.state != IDLE)
  291. {
  292. FURI_LOG_E("Game", "Failed to receive world data");
  293. flipper_http_deinit();
  294. return NULL;
  295. }
  296. flipper_http_deinit();
  297. FuriString *returned_data = load_furi_world(name);
  298. if (!returned_data)
  299. {
  300. FURI_LOG_E("Game", "Failed to load world data from file");
  301. return NULL;
  302. }
  303. if (!separate_world_data((char *)name, returned_data))
  304. {
  305. FURI_LOG_E("Game", "Failed to separate world data");
  306. furi_string_free(returned_data);
  307. return NULL;
  308. }
  309. return returned_data;
  310. }