world.c 11 KB

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