world.c 6.5 KB

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