world.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <game/world.h>
  2. #include <game/storage.h>
  3. #include <flip_storage/storage.h>
  4. bool draw_json_world_furi(GameManager *manager, Level *level, const FuriString *json_data)
  5. {
  6. if (!json_data)
  7. {
  8. FURI_LOG_E("Game", "JSON data is NULL");
  9. return false;
  10. }
  11. int levels_added = 0;
  12. FURI_LOG_I("Game", "Looping through world data");
  13. for (int i = 0; i < MAX_WORLD_OBJECTS; i++)
  14. {
  15. FURI_LOG_I("Game", "Looping through world data: %d", i);
  16. FuriString *data = get_json_array_value_furi("json_data", i, json_data);
  17. if (!data)
  18. {
  19. break;
  20. }
  21. FuriString *icon = get_json_value_furi("icon", data);
  22. FuriString *x = get_json_value_furi("x", data);
  23. FuriString *y = get_json_value_furi("y", data);
  24. FuriString *amount = get_json_value_furi("amount", data);
  25. FuriString *horizontal = get_json_value_furi("horizontal", data);
  26. if (!icon || !x || !y || !amount || !horizontal)
  27. {
  28. FURI_LOG_E("Game", "Failed Data: %s", furi_string_get_cstr(data));
  29. if (data)
  30. furi_string_free(data);
  31. if (icon)
  32. furi_string_free(icon);
  33. if (x)
  34. furi_string_free(x);
  35. if (y)
  36. furi_string_free(y);
  37. if (amount)
  38. furi_string_free(amount);
  39. if (horizontal)
  40. furi_string_free(horizontal);
  41. level_clear(level);
  42. return false;
  43. }
  44. int count = atoi(furi_string_get_cstr(amount));
  45. if (count < 2)
  46. {
  47. // Just one icon
  48. spawn_icon(
  49. manager,
  50. level,
  51. furi_string_get_cstr(icon),
  52. atoi(furi_string_get_cstr(x)),
  53. atoi(furi_string_get_cstr(y)));
  54. }
  55. else
  56. {
  57. bool is_horizontal = (furi_string_cmp(horizontal, "true") == 0);
  58. spawn_icon_line(
  59. manager,
  60. level,
  61. furi_string_get_cstr(icon),
  62. atoi(furi_string_get_cstr(x)),
  63. atoi(furi_string_get_cstr(y)),
  64. count,
  65. is_horizontal);
  66. }
  67. furi_string_free(data);
  68. furi_string_free(icon);
  69. furi_string_free(x);
  70. furi_string_free(y);
  71. furi_string_free(amount);
  72. furi_string_free(horizontal);
  73. levels_added++;
  74. }
  75. FURI_LOG_I("Game", "Finished loading world data");
  76. return levels_added > 0;
  77. }
  78. void draw_town_world(GameManager *manager, Level *level)
  79. {
  80. // house-fence group 1
  81. spawn_icon(manager, level, "house", 164, 40);
  82. spawn_icon(manager, level, "fence", 148, 64);
  83. spawn_icon(manager, level, "fence", 164, 64);
  84. spawn_icon(manager, level, "fence_end", 180, 64);
  85. // house-fence group 4 (the left of group 1)
  86. spawn_icon(manager, level, "house", 110, 40);
  87. spawn_icon(manager, level, "fence", 96, 64);
  88. spawn_icon(manager, level, "fence", 110, 64);
  89. spawn_icon(manager, level, "fence_end", 126, 64);
  90. // house-fence group 5 (the left of group 4)
  91. spawn_icon(manager, level, "house", 56, 40);
  92. spawn_icon(manager, level, "fence", 40, 64);
  93. spawn_icon(manager, level, "fence", 56, 64);
  94. spawn_icon(manager, level, "fence_end", 72, 64);
  95. // line of fences on the 8th row (using spawn_icon_line)
  96. spawn_icon_line(manager, level, "fence", 8, 96, 10, true);
  97. // plants spaced out underneath the fences
  98. spawn_icon_line(manager, level, "plant", 40, 110, 6, true);
  99. spawn_icon_line(manager, level, "flower", 40, 140, 6, true);
  100. // man and woman
  101. spawn_icon(manager, level, "man", 156, 110);
  102. spawn_icon(manager, level, "woman", 164, 110);
  103. // lake
  104. // Top row
  105. spawn_icon(manager, level, "lake_top_left", 240, 62);
  106. spawn_icon(manager, level, "lake_top", 264, 57);
  107. spawn_icon(manager, level, "lake_top_right", 295, 62);
  108. // Middle row
  109. spawn_icon(manager, level, "lake_left", 231, 84);
  110. spawn_icon(manager, level, "lake_right", 304, 84);
  111. // Bottom row
  112. spawn_icon(manager, level, "lake_bottom_left", 240, 115);
  113. spawn_icon(manager, level, "lake_bottom", 264, 120);
  114. spawn_icon(manager, level, "lake_bottom_right", 295, 115);
  115. // Spawn two full left/up tree lines
  116. for (int i = 0; i < 2; i++)
  117. {
  118. // Horizontal line of 22 icons
  119. spawn_icon_line(manager, level, "tree", 5, 2 + i * 17, 22, true);
  120. // Vertical line of 11 icons
  121. spawn_icon_line(manager, level, "tree", 5 + i * 17, 2, 11, false);
  122. }
  123. // Spawn two full down tree lines
  124. for (int i = 9; i < 11; i++)
  125. {
  126. // Horizontal line of 22 icons
  127. spawn_icon_line(manager, level, "tree", 5, 2 + i * 17, 22, true);
  128. }
  129. // Spawn two full right tree lines
  130. for (int i = 20; i < 22; i++)
  131. {
  132. // Vertical line of 8 icons starting further down (y=50)
  133. spawn_icon_line(manager, level, "tree", 5 + i * 17, 50, 8, false);
  134. }
  135. }
  136. FuriString *fetch_world(const char *name)
  137. {
  138. if (!name)
  139. {
  140. FURI_LOG_E("Game", "World name is NULL");
  141. return NULL;
  142. }
  143. FlipperHTTP *fhttp = flipper_http_alloc();
  144. if (!fhttp)
  145. {
  146. FURI_LOG_E("Game", "Failed to allocate HTTP");
  147. return NULL;
  148. }
  149. char url[256];
  150. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/v4/get/world/%s/", name);
  151. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  152. fhttp->save_received_data = true;
  153. if (!flipper_http_get_request_with_headers(fhttp, url, "{\"Content-Type\": \"application/json\"}"))
  154. {
  155. FURI_LOG_E("Game", "Failed to send HTTP request");
  156. flipper_http_free(fhttp);
  157. return NULL;
  158. }
  159. fhttp->state = RECEIVING;
  160. furi_timer_start(fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
  161. while (fhttp->state == RECEIVING && furi_timer_is_running(fhttp->get_timeout_timer) > 0)
  162. {
  163. // Wait for the request to be received
  164. furi_delay_ms(100);
  165. }
  166. furi_timer_stop(fhttp->get_timeout_timer);
  167. if (fhttp->state != IDLE)
  168. {
  169. FURI_LOG_E("Game", "Failed to receive world data");
  170. flipper_http_free(fhttp);
  171. return NULL;
  172. }
  173. flipper_http_free(fhttp);
  174. FuriString *returned_data = load_furi_world(name);
  175. if (!returned_data)
  176. {
  177. FURI_LOG_E("Game", "Failed to load world data from file");
  178. return NULL;
  179. }
  180. if (!separate_world_data((char *)name, returned_data))
  181. {
  182. FURI_LOG_E("Game", "Failed to separate world data");
  183. furi_string_free(returned_data);
  184. return NULL;
  185. }
  186. return returned_data;
  187. }