world.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. 17 // set as 17 for now
  67. );
  68. }
  69. furi_string_free(data);
  70. furi_string_free(icon);
  71. furi_string_free(x);
  72. furi_string_free(y);
  73. furi_string_free(amount);
  74. furi_string_free(horizontal);
  75. levels_added++;
  76. }
  77. FURI_LOG_I("Game", "Finished loading world data");
  78. return levels_added > 0;
  79. }
  80. void draw_town_world(GameManager *manager, Level *level)
  81. {
  82. // house-fence group 1
  83. spawn_icon(manager, level, "house", 164, 40);
  84. spawn_icon(manager, level, "fence", 148, 64);
  85. spawn_icon(manager, level, "fence", 164, 64);
  86. spawn_icon(manager, level, "fence_end", 180, 64);
  87. // house-fence group 4 (the left of group 1)
  88. spawn_icon(manager, level, "house", 110, 40);
  89. spawn_icon(manager, level, "fence", 96, 64);
  90. spawn_icon(manager, level, "fence", 110, 64);
  91. spawn_icon(manager, level, "fence_end", 126, 64);
  92. // house-fence group 5 (the left of group 4)
  93. spawn_icon(manager, level, "house", 56, 40);
  94. spawn_icon(manager, level, "fence", 40, 64);
  95. spawn_icon(manager, level, "fence", 56, 64);
  96. spawn_icon(manager, level, "fence_end", 72, 64);
  97. // line of fences on the 8th row (using spawn_icon_line)
  98. spawn_icon_line(manager, level, "fence", 8, 96, 10, true, 17);
  99. // plants spaced out underneath the fences
  100. spawn_icon_line(manager, level, "plant", 40, 110, 6, true, 17);
  101. spawn_icon_line(manager, level, "flower", 40, 140, 6, true, 17);
  102. // man and woman
  103. spawn_icon(manager, level, "man", 156, 110);
  104. spawn_icon(manager, level, "woman", 164, 110);
  105. // lake
  106. // Top row
  107. spawn_icon(manager, level, "lake_top_left", 240, 62);
  108. spawn_icon(manager, level, "lake_top", 264, 57);
  109. spawn_icon(manager, level, "lake_top_right", 295, 62);
  110. // Middle row
  111. spawn_icon(manager, level, "lake_left", 231, 84);
  112. spawn_icon(manager, level, "lake_right", 304, 84);
  113. // Bottom row
  114. spawn_icon(manager, level, "lake_bottom_left", 240, 115);
  115. spawn_icon(manager, level, "lake_bottom", 264, 120);
  116. spawn_icon(manager, level, "lake_bottom_right", 295, 115);
  117. // Spawn two full left/up tree lines
  118. for (int i = 0; i < 2; i++)
  119. {
  120. // Horizontal line of 22 icons
  121. spawn_icon_line(manager, level, "tree", 5, 2 + i * 17, 22, true, 17);
  122. // Vertical line of 11 icons
  123. spawn_icon_line(manager, level, "tree", 5 + i * 17, 2, 11, false, 17);
  124. }
  125. // Spawn two full down tree lines
  126. for (int i = 9; i < 11; i++)
  127. {
  128. // Horizontal line of 22 icons
  129. spawn_icon_line(manager, level, "tree", 5, 2 + i * 17, 22, true, 17);
  130. }
  131. // Spawn two full right tree lines
  132. for (int i = 20; i < 22; i++)
  133. {
  134. // Vertical line of 8 icons starting further down (y=50)
  135. spawn_icon_line(manager, level, "tree", 5 + i * 17, 50, 8, false, 17);
  136. }
  137. }
  138. FuriString *fetch_world(const char *name)
  139. {
  140. if (!name)
  141. {
  142. FURI_LOG_E("Game", "World name is NULL");
  143. return NULL;
  144. }
  145. FlipperHTTP *fhttp = flipper_http_alloc();
  146. if (!fhttp)
  147. {
  148. FURI_LOG_E("Game", "Failed to allocate HTTP");
  149. return NULL;
  150. }
  151. char url[256];
  152. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/v4/get/world/%s/", name);
  153. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  154. fhttp->save_received_data = true;
  155. if (!flipper_http_get_request_with_headers(fhttp, url, "{\"Content-Type\": \"application/json\"}"))
  156. {
  157. FURI_LOG_E("Game", "Failed to send HTTP request");
  158. flipper_http_free(fhttp);
  159. return NULL;
  160. }
  161. fhttp->state = RECEIVING;
  162. furi_timer_start(fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
  163. while (fhttp->state == RECEIVING && furi_timer_is_running(fhttp->get_timeout_timer) > 0)
  164. {
  165. // Wait for the request to be received
  166. furi_delay_ms(100);
  167. }
  168. furi_timer_stop(fhttp->get_timeout_timer);
  169. if (fhttp->state != IDLE)
  170. {
  171. FURI_LOG_E("Game", "Failed to receive world data");
  172. flipper_http_free(fhttp);
  173. return NULL;
  174. }
  175. flipper_http_free(fhttp);
  176. FuriString *returned_data = load_furi_world(name);
  177. if (!returned_data)
  178. {
  179. FURI_LOG_E("Game", "Failed to load world data from file");
  180. return NULL;
  181. }
  182. if (!separate_world_data((char *)name, returned_data))
  183. {
  184. FURI_LOG_E("Game", "Failed to separate world data");
  185. furi_string_free(returned_data);
  186. return NULL;
  187. }
  188. return returned_data;
  189. }