world.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. IconContext *icon_context = get_icon_context(icon);
  45. if (!icon_context)
  46. {
  47. FURI_LOG_E("Game", "Failed Icon: %s", icon);
  48. free(data);
  49. free(icon);
  50. free(x);
  51. free(y);
  52. free(amount);
  53. free(horizontal);
  54. level_clear(level);
  55. return false;
  56. }
  57. int count = atoi(amount);
  58. if (count < 2)
  59. {
  60. // Just one icon
  61. spawn_icon(
  62. level,
  63. icon_context->icon,
  64. atoi(x),
  65. atoi(y),
  66. icon_context->width,
  67. icon_context->height);
  68. }
  69. else
  70. {
  71. bool is_horizontal = (strcmp(horizontal, "true") == 0);
  72. spawn_icon_line(
  73. level,
  74. icon_context->icon,
  75. atoi(x),
  76. atoi(y),
  77. icon_context->width,
  78. icon_context->height,
  79. count,
  80. is_horizontal);
  81. }
  82. free(data);
  83. free(icon);
  84. free(x);
  85. free(y);
  86. free(amount);
  87. free(horizontal);
  88. free(icon_context);
  89. levels_added++;
  90. }
  91. return levels_added > 0;
  92. }
  93. bool draw_json_world_furi(Level *level, FuriString *json_data)
  94. {
  95. int levels_added = 0;
  96. for (int i = 0; i < MAX_WORLD_OBJECTS; i++)
  97. {
  98. FuriString *data = get_json_array_value_furi("json_data", i, json_data);
  99. if (!data)
  100. {
  101. break;
  102. }
  103. FuriString *icon = get_json_value_furi("icon", data);
  104. FuriString *x = get_json_value_furi("x", data);
  105. FuriString *y = get_json_value_furi("y", data);
  106. FuriString *amount = get_json_value_furi("amount", data);
  107. FuriString *horizontal = get_json_value_furi("horizontal", data);
  108. if (!icon || !x || !y || !amount || !horizontal)
  109. {
  110. FURI_LOG_E("Game", "Failed Data: %s", furi_string_get_cstr(data));
  111. if (data)
  112. furi_string_free(data);
  113. if (icon)
  114. furi_string_free(icon);
  115. if (x)
  116. furi_string_free(x);
  117. if (y)
  118. furi_string_free(y);
  119. if (amount)
  120. furi_string_free(amount);
  121. if (horizontal)
  122. furi_string_free(horizontal);
  123. level_clear(level);
  124. return false;
  125. }
  126. IconContext *icon_context = get_icon_context_furi(icon);
  127. if (!icon_context)
  128. {
  129. FURI_LOG_E("Game", "Failed Icon: %s", furi_string_get_cstr(icon));
  130. furi_string_free(data);
  131. furi_string_free(icon);
  132. furi_string_free(x);
  133. furi_string_free(y);
  134. furi_string_free(amount);
  135. furi_string_free(horizontal);
  136. level_clear(level);
  137. return false;
  138. }
  139. int count = atoi(furi_string_get_cstr(amount));
  140. if (count < 2)
  141. {
  142. // Just one icon
  143. spawn_icon(
  144. level,
  145. icon_context->icon,
  146. atoi(furi_string_get_cstr(x)),
  147. atoi(furi_string_get_cstr(y)),
  148. icon_context->width,
  149. icon_context->height);
  150. }
  151. else
  152. {
  153. bool is_horizontal = (furi_string_cmp(horizontal, "true") == 0);
  154. spawn_icon_line(
  155. level,
  156. icon_context->icon,
  157. atoi(furi_string_get_cstr(x)),
  158. atoi(furi_string_get_cstr(y)),
  159. icon_context->width,
  160. icon_context->height,
  161. count,
  162. is_horizontal);
  163. }
  164. furi_string_free(data);
  165. furi_string_free(icon);
  166. furi_string_free(x);
  167. furi_string_free(y);
  168. furi_string_free(amount);
  169. furi_string_free(horizontal);
  170. free(icon_context);
  171. levels_added++;
  172. }
  173. return levels_added > 0;
  174. }
  175. void draw_tree_world(Level *level)
  176. {
  177. IconContext *tree_icon = get_icon_context("tree");
  178. if (!tree_icon)
  179. {
  180. FURI_LOG_E("Game", "Failed to get tree icon context");
  181. return;
  182. }
  183. // Spawn two full left/up tree lines
  184. for (int i = 0; i < 2; i++)
  185. {
  186. // Horizontal line of 22 icons
  187. spawn_icon_line(level, tree_icon->icon, 5, 2 + i * 17, tree_icon->width, tree_icon->height, 22, true);
  188. // Vertical line of 11 icons
  189. spawn_icon_line(level, tree_icon->icon, 5 + i * 17, 2, tree_icon->width, tree_icon->height, 11, false);
  190. }
  191. // Spawn two full down tree lines
  192. for (int i = 9; i < 11; i++)
  193. {
  194. // Horizontal line of 22 icons
  195. spawn_icon_line(level, tree_icon->icon, 5, 2 + i * 17, tree_icon->width, tree_icon->height, 22, true);
  196. }
  197. // Spawn two full right tree lines
  198. for (int i = 20; i < 22; i++)
  199. {
  200. // Vertical line of 8 icons starting further down (y=50)
  201. spawn_icon_line(level, tree_icon->icon, 5 + i * 17, 50, tree_icon->width, tree_icon->height, 8, false);
  202. }
  203. // Labyrinth lines
  204. // Third line (14 left, then a gap, then 3 middle)
  205. spawn_icon_line(level, tree_icon->icon, 5, 2 + 2 * 17, tree_icon->width, tree_icon->height, 14, true);
  206. spawn_icon_line(level, tree_icon->icon, 5 + 16 * 17, 2 + 2 * 17, tree_icon->width, tree_icon->height, 3, true);
  207. // Fourth line (3 left, 6 middle, 4 right)
  208. spawn_icon_line(level, tree_icon->icon, 5, 2 + 3 * 17, tree_icon->width, tree_icon->height, 3, true); // 3 left
  209. spawn_icon_line(level, tree_icon->icon, 5 + 7 * 17, 2 + 3 * 17, tree_icon->width, tree_icon->height, 6, true); // 6 middle
  210. spawn_icon_line(level, tree_icon->icon, 5 + 15 * 17, 2 + 3 * 17, tree_icon->width, tree_icon->height, 4, true); // 4 right
  211. // Fifth line (6 left, 7 middle)
  212. spawn_icon_line(level, tree_icon->icon, 5, 2 + 4 * 17, tree_icon->width, tree_icon->height, 6, true);
  213. spawn_icon_line(level, tree_icon->icon, 5 + 7 * 17, 2 + 4 * 17, tree_icon->width, tree_icon->height, 7, true);
  214. // Sixth line (5 left, 3 middle, 7 right)
  215. spawn_icon_line(level, tree_icon->icon, 5, 2 + 5 * 17, tree_icon->width, tree_icon->height, 5, true); // 5 left
  216. spawn_icon_line(level, tree_icon->icon, 5 + 7 * 17, 2 + 5 * 17, tree_icon->width, tree_icon->height, 3, true); // 3 middle
  217. spawn_icon_line(level, tree_icon->icon, 5 + 15 * 17, 2 + 5 * 17, tree_icon->width, tree_icon->height, 7, true); // 7 right
  218. // Seventh line (0 left, 7 middle, 4 right)
  219. spawn_icon_line(level, tree_icon->icon, 5 + 6 * 17, 2 + 6 * 17, tree_icon->width, tree_icon->height, 7, true); // 7 middle
  220. spawn_icon_line(level, tree_icon->icon, 5 + 14 * 17, 2 + 6 * 17, tree_icon->width, tree_icon->height, 4, true); // 4 right
  221. // Eighth line (4 left, 3 middle, 4 right)
  222. spawn_icon_line(level, tree_icon->icon, 5, 2 + 7 * 17, tree_icon->width, tree_icon->height, 4, true); // 4 left
  223. spawn_icon_line(level, tree_icon->icon, 5 + 7 * 17, 2 + 7 * 17, tree_icon->width, tree_icon->height, 3, true); // 3 middle
  224. spawn_icon_line(level, tree_icon->icon, 5 + 15 * 17, 2 + 7 * 17, tree_icon->width, tree_icon->height, 4, true); // 4 right
  225. // Ninth line (3 left, 1 middle, 3 right)
  226. spawn_icon_line(level, tree_icon->icon, 5, 2 + 8 * 17, tree_icon->width, tree_icon->height, 3, true); // 3 left
  227. spawn_icon_line(level, tree_icon->icon, 5 + 5 * 17, 2 + 8 * 17, tree_icon->width, tree_icon->height, 1, true); // 1 middle
  228. spawn_icon_line(level, tree_icon->icon, 5 + 11 * 17, 2 + 8 * 17, tree_icon->width, tree_icon->height, 3, true); // 3 right
  229. free(tree_icon);
  230. }
  231. void draw_town_world(Level *level)
  232. {
  233. // define all the icons
  234. IconContext *house_icon = get_icon_context("house");
  235. IconContext *fence_icon = get_icon_context("fence");
  236. IconContext *fence_end_icon = get_icon_context("fence_end");
  237. IconContext *plant_icon = get_icon_context("plant");
  238. IconContext *flower_icon = get_icon_context("flower");
  239. IconContext *man_icon = get_icon_context("man");
  240. IconContext *woman_icon = get_icon_context("woman");
  241. IconContext *lake_top_left_icon = get_icon_context("lake_top_left");
  242. IconContext *lake_top_icon = get_icon_context("lake_top");
  243. IconContext *lake_top_right_icon = get_icon_context("lake_top_right");
  244. IconContext *lake_left_icon = get_icon_context("lake_left");
  245. IconContext *lake_right_icon = get_icon_context("lake_right");
  246. IconContext *lake_bottom_left_icon = get_icon_context("lake_bottom_left");
  247. IconContext *lake_bottom_icon = get_icon_context("lake_bottom");
  248. IconContext *lake_bottom_right_icon = get_icon_context("lake_bottom_right");
  249. IconContext *tree_icon = get_icon_context("tree");
  250. // check if any of the icons are NULL
  251. if (!house_icon || !fence_icon || !fence_end_icon || !plant_icon || !flower_icon ||
  252. !man_icon || !woman_icon || !lake_top_left_icon || !lake_top_icon || !lake_top_right_icon ||
  253. !lake_left_icon || !lake_right_icon || !lake_bottom_left_icon || !lake_bottom_icon || !lake_bottom_right_icon || !tree_icon)
  254. {
  255. FURI_LOG_E("Game", "Failed to get icon context");
  256. return;
  257. }
  258. // house-fence group 1
  259. spawn_icon(level, house_icon->icon, 148, 36, house_icon->width, house_icon->height);
  260. spawn_icon(level, fence_icon->icon, 148, 72, fence_icon->width, fence_icon->height);
  261. spawn_icon(level, fence_icon->icon, 164, 72, fence_icon->width, fence_icon->height);
  262. spawn_icon(level, fence_end_icon->icon, 180, 72, fence_end_icon->width, fence_end_icon->height);
  263. // house-fence group 4 (the left of group 1)
  264. spawn_icon(level, house_icon->icon, 96, 36, house_icon->width, house_icon->height);
  265. spawn_icon(level, fence_icon->icon, 96, 72, fence_icon->width, fence_icon->height);
  266. spawn_icon(level, fence_icon->icon, 110, 72, fence_icon->width, fence_icon->height);
  267. spawn_icon(level, fence_end_icon->icon, 126, 72, fence_end_icon->width, fence_end_icon->height);
  268. // house-fence group 5 (the left of group 4)
  269. spawn_icon(level, house_icon->icon, 40, 36, house_icon->width, house_icon->height);
  270. spawn_icon(level, fence_icon->icon, 40, 72, fence_icon->width, fence_icon->height);
  271. spawn_icon(level, fence_icon->icon, 56, 72, fence_icon->width, fence_icon->height);
  272. spawn_icon(level, fence_end_icon->icon, 72, 72, fence_end_icon->width, fence_end_icon->height);
  273. // line of fences on the 8th row (using spawn_icon_line)
  274. spawn_icon_line(level, fence_icon->icon, 8, 100, fence_icon->width, fence_icon->height, 10, true);
  275. // plants spaced out underneath the fences
  276. spawn_icon_line(level, plant_icon->icon, 40, 110, plant_icon->width, plant_icon->height, 6, true);
  277. spawn_icon_line(level, flower_icon->icon, 40, 140, flower_icon->width, flower_icon->height, 6, true);
  278. // man and woman
  279. spawn_icon(level, man_icon->icon, 156, 110, man_icon->width, man_icon->height);
  280. spawn_icon(level, woman_icon->icon, 164, 110, woman_icon->width, woman_icon->height);
  281. // lake
  282. // Top row
  283. spawn_icon(level, lake_top_left_icon->icon, 240, 52, lake_top_left_icon->width, lake_top_left_icon->height);
  284. spawn_icon(level, lake_top_icon->icon, 264, 52, lake_top_icon->width, lake_top_icon->height);
  285. spawn_icon(level, lake_top_right_icon->icon, 295, 52, lake_top_right_icon->width, lake_top_right_icon->height);
  286. // Middle row
  287. spawn_icon(level, lake_left_icon->icon, 231, 74, lake_left_icon->width, lake_left_icon->height);
  288. spawn_icon(level, lake_right_icon->icon, 317, 74, lake_right_icon->width, lake_right_icon->height);
  289. // Bottom row
  290. spawn_icon(level, lake_bottom_left_icon->icon, 240, 105, lake_bottom_left_icon->width, lake_bottom_left_icon->height);
  291. spawn_icon(level, lake_bottom_icon->icon, 264, 124, lake_bottom_icon->width, lake_bottom_icon->height);
  292. spawn_icon(level, lake_bottom_right_icon->icon, 295, 105, lake_bottom_right_icon->width, lake_bottom_right_icon->height);
  293. // Spawn two full left/up tree lines
  294. for (int i = 0; i < 2; i++)
  295. {
  296. // Horizontal line of 22 icons
  297. spawn_icon_line(level, tree_icon->icon, 5, 2 + i * 17, tree_icon->width, tree_icon->height, 22, true);
  298. // Vertical line of 11 icons
  299. spawn_icon_line(level, tree_icon->icon, 5 + i * 17, 2, tree_icon->width, tree_icon->height, 11, false);
  300. }
  301. // Spawn two full down tree lines
  302. for (int i = 9; i < 11; i++)
  303. {
  304. // Horizontal line of 22 icons
  305. spawn_icon_line(level, tree_icon->icon, 5, 2 + i * 17, tree_icon->width, tree_icon->height, 22, true);
  306. }
  307. // Spawn two full right tree lines
  308. for (int i = 20; i < 22; i++)
  309. {
  310. // Vertical line of 8 icons starting further down (y=50)
  311. spawn_icon_line(level, tree_icon->icon, 5 + i * 17, 50, tree_icon->width, tree_icon->height, 8, false);
  312. }
  313. free(house_icon);
  314. free(fence_icon);
  315. free(fence_end_icon);
  316. free(plant_icon);
  317. free(flower_icon);
  318. free(man_icon);
  319. free(woman_icon);
  320. free(lake_top_left_icon);
  321. free(lake_top_icon);
  322. free(lake_top_right_icon);
  323. free(lake_left_icon);
  324. free(lake_right_icon);
  325. free(lake_bottom_left_icon);
  326. free(lake_bottom_icon);
  327. free(lake_bottom_right_icon);
  328. free(tree_icon);
  329. }
  330. FuriString *fetch_world(const char *name)
  331. {
  332. if (!name)
  333. {
  334. FURI_LOG_E("Game", "World name is NULL");
  335. return NULL;
  336. }
  337. if (!app_instance)
  338. {
  339. // as long as the game is running, app_instance should be non-NULL
  340. FURI_LOG_E("Game", "App instance is NULL");
  341. return NULL;
  342. }
  343. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  344. {
  345. FURI_LOG_E("Game", "Failed to initialize HTTP");
  346. return NULL;
  347. }
  348. char url[256];
  349. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/v2/get/world/%s/", name);
  350. snprintf(fhttp.file_path, sizeof(fhttp.file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  351. fhttp.save_received_data = true;
  352. if (!flipper_http_get_request_with_headers(url, "{\"Content-Type\": \"application/json\"}"))
  353. {
  354. FURI_LOG_E("Game", "Failed to send HTTP request");
  355. flipper_http_deinit();
  356. return NULL;
  357. }
  358. fhttp.state = RECEIVING;
  359. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  360. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  361. {
  362. // Wait for the request to be received
  363. furi_delay_ms(100);
  364. }
  365. furi_timer_stop(fhttp.get_timeout_timer);
  366. if (fhttp.state != IDLE)
  367. {
  368. FURI_LOG_E("Game", "Failed to receive world data");
  369. flipper_http_deinit();
  370. return NULL;
  371. }
  372. flipper_http_deinit();
  373. FuriString *returned_data = load_furi_world(name);
  374. if (!returned_data)
  375. {
  376. FURI_LOG_E("Game", "Failed to load world data from file");
  377. return NULL;
  378. }
  379. if (!separate_world_data((char *)name, returned_data))
  380. {
  381. FURI_LOG_E("Game", "Failed to separate world data");
  382. furi_string_free(returned_data);
  383. return NULL;
  384. }
  385. return returned_data;
  386. }