table.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. #include <toolbox/dir_walk.h>
  2. #include <toolbox/path.h>
  3. #include <toolbox/stream/stream.h>
  4. #include <toolbox/stream/file_stream.h>
  5. #include <toolbox/args.h>
  6. #include "nxjson/nxjson.h"
  7. #include "pinball0.h"
  8. #include "graphics.h"
  9. #include "table.h"
  10. #include "notifications.h"
  11. // Table defaults
  12. #define LIVES 3
  13. #define LIVES_POS Vec2(20, 20)
  14. bool ON_TABLE(const Vec2& p) {
  15. return 0 <= p.x && p.x <= 630 && 0 <= p.y && p.y <= 1270;
  16. }
  17. void Lives::draw(Canvas* canvas) {
  18. // we don't draw the last one, as it's in play!
  19. constexpr float r = 20;
  20. if(display && value > 0) {
  21. float x = p.x;
  22. float y = p.y;
  23. float x_off = alignment == Align::Horizontal ? (2 * r) + r : 0;
  24. float y_off = alignment == Align::Vertical ? (2 * r) + r : 0;
  25. for(auto l = 0; l < value - 1; x += x_off, y += y_off, l++) {
  26. gfx_draw_disc(canvas, x + r, y + r, 20);
  27. }
  28. }
  29. }
  30. void Score::draw(Canvas* canvas) {
  31. if(display) {
  32. char buf[32];
  33. snprintf(buf, 32, "%d", value);
  34. gfx_draw_str(canvas, p.x, p.y, AlignRight, AlignTop, buf);
  35. }
  36. }
  37. Table::~Table() {
  38. for(size_t i = 0; i < objects.size(); i++) {
  39. delete objects[i];
  40. }
  41. if(plunger != nullptr) {
  42. delete plunger;
  43. }
  44. }
  45. void Table::draw(Canvas* canvas) {
  46. lives.draw(canvas);
  47. // da balls
  48. for(auto& b : balls) {
  49. b.draw(canvas);
  50. }
  51. // loop through objects on the table and draw them
  52. for(auto& o : objects) {
  53. o->draw(canvas);
  54. }
  55. for(auto& f : flippers) {
  56. f.draw(canvas);
  57. }
  58. if(plunger) {
  59. plunger->draw(canvas);
  60. }
  61. score.draw(canvas);
  62. }
  63. void table_table_list_init(void* ctx) {
  64. PinballApp* pb = (PinballApp*)ctx;
  65. // using the asset file path, read the table files, and for each one, extract their
  66. // display name (oof). let's just use their filenames for now (stripping any XX_ prefix)
  67. // sort tables by original filename
  68. const char* paths[] = {APP_ASSETS_PATH("tables"), APP_DATA_PATH("tables")};
  69. const size_t ext_len_max = 32;
  70. char ext[ext_len_max];
  71. for(size_t p = 0; p < 2; p++) {
  72. const char* path = paths[p];
  73. // const char* asset_path = APP_ASSETS_PATH("tables");
  74. FURI_LOG_I(TAG, "Loading table list from: %s", path);
  75. FuriString* table_path = furi_string_alloc();
  76. DirWalk* dir_walk = dir_walk_alloc(pb->storage);
  77. dir_walk_set_recursive(dir_walk, false);
  78. if(dir_walk_open(dir_walk, path)) {
  79. while(dir_walk_read(dir_walk, table_path, NULL) == DirWalkOK) {
  80. path_extract_extension(table_path, ext, ext_len_max);
  81. if(strcmp(ext, ".json") != 0) {
  82. FURI_LOG_W(
  83. TAG, "Skipping non-json file: %s", furi_string_get_cstr(table_path));
  84. continue;
  85. }
  86. const char* cpath = furi_string_get_cstr(table_path);
  87. FuriString* filename_no_ext = furi_string_alloc();
  88. path_extract_filename_no_ext(cpath, filename_no_ext);
  89. // If filename starts with XX_ (for custom sorting) strip the prefix
  90. char c = furi_string_get_char(filename_no_ext, 2);
  91. if(c == '_') {
  92. char a = furi_string_get_char(filename_no_ext, 0);
  93. char b = furi_string_get_char(filename_no_ext, 1);
  94. if(a >= '0' && a <= '9' && b >= '0' && b <= '9') {
  95. furi_string_right(filename_no_ext, 3);
  96. }
  97. }
  98. if(!pb->settings.debug_mode &&
  99. !strncmp("dbg", furi_string_get_cstr(filename_no_ext), 3)) {
  100. furi_string_free(filename_no_ext);
  101. continue;
  102. }
  103. FURI_LOG_I(
  104. TAG,
  105. "Found table: name=%s | path=%s",
  106. furi_string_get_cstr(filename_no_ext),
  107. furi_string_get_cstr(table_path));
  108. // set display 'name' and 'filename'
  109. TableMenuItem tmi;
  110. tmi.filename = furi_string_alloc_set_str(cpath);
  111. tmi.name = filename_no_ext;
  112. // Insert in sorted order
  113. size_t i = 0;
  114. auto it = pb->table_list.menu_items.begin();
  115. for(; it != pb->table_list.menu_items.end(); it++, i++) {
  116. if(strcmp(
  117. furi_string_get_cstr(tmi.filename),
  118. furi_string_get_cstr(it->filename)) > 0) {
  119. continue;
  120. }
  121. pb->table_list.menu_items.insert(it, tmi);
  122. break;
  123. }
  124. if(pb->table_list.menu_items.size() == i) {
  125. pb->table_list.menu_items.push_back(tmi);
  126. }
  127. }
  128. }
  129. furi_string_free(table_path);
  130. dir_walk_free(dir_walk);
  131. }
  132. // Add 'Settings' as last element
  133. TableMenuItem settings;
  134. settings.filename = furi_string_alloc_set_str("99_Settings");
  135. settings.name = furi_string_alloc_set_str("SETTINGS");
  136. pb->table_list.menu_items.push_back(settings);
  137. FURI_LOG_I(TAG, "Found %d tables", pb->table_list.menu_items.size());
  138. for(auto& tmi : pb->table_list.menu_items) {
  139. FURI_LOG_I(TAG, "%s", furi_string_get_cstr(tmi.name));
  140. }
  141. pb->table_list.display_size = 5; // how many tables to display at once
  142. pb->table_list.selected = 0;
  143. }
  144. // json parse helper function
  145. bool table_file_parse_vec2(const nx_json* json, const char* key, Vec2& v) {
  146. const nx_json* item = nx_json_get(json, key);
  147. if(!item || item->children.length != 2) {
  148. return false;
  149. }
  150. v.x = nx_json_item(item, 0)->num.dbl_value;
  151. v.y = nx_json_item(item, 1)->num.dbl_value;
  152. return true;
  153. }
  154. bool table_file_parse_int(const nx_json* json, const char* key, int& v) {
  155. const nx_json* item = nx_json_get(json, key);
  156. if(!item) return false;
  157. v = item->num.u_value;
  158. return true;
  159. }
  160. bool table_file_parse_bool(const nx_json* json, const char* key, bool& v) {
  161. int value = v == true ? 1 : 0; // set default value
  162. if(table_file_parse_int(json, key, value)) {
  163. v = value > 0 ? true : false;
  164. return true;
  165. }
  166. return false;
  167. }
  168. bool table_file_parse_float(const nx_json* json, const char* key, float& v) {
  169. const nx_json* item = nx_json_get(json, key);
  170. if(!item) return false;
  171. v = item->num.dbl_value;
  172. return true;
  173. }
  174. Table* table_load_table_from_file(PinballApp* pb, size_t index) {
  175. auto& tmi = pb->table_list.menu_items[index];
  176. FURI_LOG_I(TAG, "Reading file: %s", furi_string_get_cstr(tmi.filename));
  177. File* file = storage_file_alloc(pb->storage);
  178. FileInfo fileinfo;
  179. FS_Error error =
  180. storage_common_stat(pb->storage, furi_string_get_cstr(tmi.filename), &fileinfo);
  181. if(error != FSE_OK) {
  182. FURI_LOG_E(TAG, "Could not find file");
  183. storage_file_free(file);
  184. return NULL;
  185. }
  186. // TODO: determine an appropriate max file size and make configurable
  187. FURI_LOG_I(TAG, "Found file ok!");
  188. if(fileinfo.size >= 8192) {
  189. FURI_LOG_E(TAG, "Table file size too big");
  190. snprintf(pb->text, 256, "Table file\nis too big!\n> 8192 bytes");
  191. storage_file_free(file);
  192. return NULL;
  193. }
  194. FURI_LOG_I(TAG, "File size is ok!");
  195. bool ok =
  196. storage_file_open(file, furi_string_get_cstr(tmi.filename), FSAM_READ, FSOM_OPEN_EXISTING);
  197. FURI_LOG_I(TAG, "File opened? %s", ok ? "YES" : "NO");
  198. // read the file as a string
  199. uint8_t* buffer;
  200. uint64_t file_size = storage_file_size(file);
  201. if(file_size > 8192) { // TODO - what's the right size?
  202. FURI_LOG_E(TAG, "Table file is too large! (> 8192 bytes)");
  203. snprintf(pb->text, 256, "Table file\nis too big!\n> 8192 bytes");
  204. storage_file_free(file);
  205. return NULL;
  206. }
  207. buffer = (uint8_t*)malloc(file_size);
  208. size_t read_count = storage_file_read(file, buffer, file_size);
  209. // if(storage_file_get_error(file) != FSE_OK) {
  210. // FURI_LOG_E(TAG, "Um, couldn't read file");
  211. // storage_file_free(file);
  212. // return NULL;
  213. // }
  214. storage_file_free(file);
  215. if(read_count != file_size) {
  216. FURI_LOG_E(TAG, "Error reading file. expected %lld, got %d", file_size, read_count);
  217. free(buffer);
  218. return NULL;
  219. }
  220. FURI_LOG_I(TAG, "Read file into buffer! %d bytes", read_count);
  221. // let's parse this shit
  222. char* json_buffer = (char*)malloc(read_count * sizeof(char) + 1);
  223. for(uint16_t i = 0; i < read_count; i++) {
  224. json_buffer[i] = buffer[i];
  225. }
  226. json_buffer[read_count] = 0;
  227. free(buffer);
  228. const nx_json* json = nx_json_parse(json_buffer, 0);
  229. if(!json) {
  230. FURI_LOG_E(TAG, "Failed to parse table json!");
  231. snprintf(pb->text, 256, "Failed to\nparse table\njson!!");
  232. free(json_buffer);
  233. return NULL;
  234. }
  235. Table* table = new Table();
  236. do {
  237. const nx_json* lives = nx_json_get(json, "lives");
  238. if(lives) {
  239. table_file_parse_int(lives, "value", table->lives.value);
  240. table_file_parse_bool(lives, "display", table->lives.display);
  241. table_file_parse_vec2(lives, "position", table->lives.p);
  242. const nx_json* align = nx_json_get(lives, "align");
  243. if(align && !strcmp(align->text_value, "VERTICAL")) {
  244. table->lives.alignment = Lives::Vertical;
  245. }
  246. }
  247. const nx_json* score = nx_json_get(json, "score");
  248. if(score) {
  249. table_file_parse_bool(score, "display", table->score.display);
  250. table_file_parse_vec2(score, "position", table->score.p);
  251. }
  252. const nx_json* balls = nx_json_get(json, "balls");
  253. if(balls) {
  254. for(int i = 0; i < balls->children.length; i++) {
  255. const nx_json* ball = nx_json_item(balls, i);
  256. if(!ball) continue;
  257. Vec2 p;
  258. if(!table_file_parse_vec2(ball, "position", p)) {
  259. FURI_LOG_E(TAG, "Ball missing \"position\", skipping");
  260. continue;
  261. }
  262. if(!ON_TABLE(p)) {
  263. FURI_LOG_W(
  264. TAG,
  265. "Ball with position %.1f,%.1f is not on table!",
  266. (double)p.x,
  267. (double)p.y);
  268. }
  269. Ball new_ball(p);
  270. table_file_parse_float(ball, "radius", new_ball.r);
  271. Vec2 v = (Vec2){0, 0};
  272. table_file_parse_vec2(ball, "velocity", v);
  273. new_ball.accelerate(v);
  274. table->balls_initial.push_back(new_ball);
  275. table->balls.push_back(new_ball);
  276. }
  277. }
  278. if(table->balls.size() == 0) {
  279. FURI_LOG_E(TAG, "Table has NO BALLS");
  280. snprintf(pb->text, 256, "No balls\nfound in\ntable file!");
  281. delete table;
  282. table = NULL;
  283. break;
  284. }
  285. // TODO: plungers need work
  286. const nx_json* plunger = nx_json_get(json, "plunger");
  287. if(plunger) {
  288. Vec2 p;
  289. table_file_parse_vec2(plunger, "position", p);
  290. int s = 100;
  291. table_file_parse_int(plunger, "size", s);
  292. table->plunger = new Plunger(p);
  293. } else {
  294. FURI_LOG_W(
  295. TAG, "Table has NO PLUNGER - s'ok, we don't really support one anyway (yet)");
  296. }
  297. const nx_json* flippers = nx_json_get(json, "flippers");
  298. if(flippers) {
  299. for(int i = 0; i < flippers->children.length; i++) {
  300. const nx_json* flipper = nx_json_item(flippers, i);
  301. Vec2 p;
  302. if(!table_file_parse_vec2(flipper, "position", p)) {
  303. FURI_LOG_E(TAG, "Flipper missing \"position\", skipping");
  304. continue;
  305. }
  306. if(!ON_TABLE(p)) {
  307. FURI_LOG_W(
  308. TAG,
  309. "Flipper with position %.1f,%.1f is not on table!",
  310. (double)p.x,
  311. (double)p.y);
  312. }
  313. const nx_json* side = nx_json_get(flipper, "side");
  314. Flipper::Side sd = Flipper::LEFT;
  315. if(side && !strcmp(side->text_value, "RIGHT")) {
  316. sd = Flipper::RIGHT;
  317. }
  318. int sz = DEF_FLIPPER_SIZE;
  319. table_file_parse_int(flipper, "size", sz);
  320. Flipper flip(p, sd, sz);
  321. // flip.notification = &notify_flipper;
  322. table->flippers.push_back(flip);
  323. }
  324. }
  325. const nx_json* bumpers = nx_json_get(json, "bumpers");
  326. if(bumpers) {
  327. for(int i = 0; i < bumpers->children.length; i++) {
  328. const nx_json* bumper = nx_json_item(bumpers, i);
  329. Vec2 p;
  330. if(!table_file_parse_vec2(bumper, "position", p)) {
  331. FURI_LOG_E(TAG, "Bumper missing \"position\", skipping");
  332. continue;
  333. }
  334. if(!ON_TABLE(p)) {
  335. FURI_LOG_W(
  336. TAG,
  337. "Bumper with position %.1f,%.1f is not on table!",
  338. (double)p.x,
  339. (double)p.y);
  340. }
  341. int r = DEF_BUMPER_RADIUS;
  342. table_file_parse_int(bumper, "radius", r);
  343. float bnc = DEF_BUMPER_BOUNCE;
  344. table_file_parse_float(bumper, "bounce", bnc);
  345. Bumper* new_bumper = new Bumper(p, r);
  346. new_bumper->bounce = bnc;
  347. new_bumper->notification = notify_bumper_hit;
  348. table->objects.push_back(new_bumper);
  349. }
  350. }
  351. constexpr float pi_180 = M_PI / 180;
  352. const nx_json* arcs = nx_json_get(json, "arcs");
  353. if(arcs) {
  354. for(int i = 0; i < arcs->children.length; i++) {
  355. const nx_json* arc = nx_json_item(arcs, i);
  356. Vec2 p;
  357. if(!table_file_parse_vec2(arc, "position", p)) {
  358. FURI_LOG_E(TAG, "Arc missing \"position\"");
  359. continue;
  360. }
  361. if(!ON_TABLE(p)) {
  362. FURI_LOG_W(
  363. TAG,
  364. "Arc with position %.1f,%.1f is not on table!",
  365. (double)p.x,
  366. (double)p.y);
  367. }
  368. int r = DEF_BUMPER_RADIUS;
  369. table_file_parse_int(arc, "radius", r);
  370. float bnc = 0.95f; // DEF_BUMPER_BOUNCE?
  371. table_file_parse_float(arc, "bounce", bnc);
  372. float start_angle = 0.0;
  373. table_file_parse_float(arc, "start_angle", start_angle);
  374. start_angle *= pi_180;
  375. float end_angle = 0.0;
  376. table_file_parse_float(arc, "end_angle", end_angle);
  377. end_angle *= pi_180;
  378. Arc::Surface surface = Arc::OUTSIDE;
  379. const nx_json* stype = nx_json_get(arc, "surface");
  380. if(stype && !strcmp(stype->text_value, "INSIDE")) {
  381. surface = Arc::INSIDE;
  382. }
  383. Arc* new_bumper = new Arc(p, r, start_angle, end_angle, surface);
  384. new_bumper->bounce = bnc;
  385. table->objects.push_back(new_bumper);
  386. }
  387. }
  388. const nx_json* rails = nx_json_get(json, "rails");
  389. if(rails) {
  390. for(int i = 0; i < rails->children.length; i++) {
  391. const nx_json* rail = nx_json_item(rails, i);
  392. Vec2 s;
  393. if(!table_file_parse_vec2(rail, "start", s)) {
  394. FURI_LOG_E(TAG, "Rail missing \"start\", skipping");
  395. continue;
  396. }
  397. if(!ON_TABLE(s)) {
  398. FURI_LOG_W(
  399. TAG,
  400. "Rail with starting position %.1f,%.1f is not on table!",
  401. (double)s.x,
  402. (double)s.y);
  403. }
  404. Vec2 e;
  405. if(!table_file_parse_vec2(rail, "end", e)) {
  406. FURI_LOG_E(TAG, "Rail missing \"end\", skipping");
  407. continue;
  408. }
  409. if(!ON_TABLE(e)) {
  410. FURI_LOG_W(
  411. TAG,
  412. "Rail with ending position %.1f,%.1f is not on table!",
  413. (double)e.x,
  414. (double)e.y);
  415. }
  416. Polygon* new_rail = new Polygon();
  417. new_rail->add_point(s);
  418. new_rail->add_point(e);
  419. float bnc = DEF_RAIL_BOUNCE;
  420. table_file_parse_float(rail, "bounce", bnc);
  421. new_rail->bounce = bnc;
  422. int double_sided = 0;
  423. table_file_parse_int(rail, "double_sided", double_sided);
  424. new_rail->finalize();
  425. new_rail->notification = &notify_rail_hit;
  426. table->objects.push_back(new_rail);
  427. if(double_sided) {
  428. new_rail = new Polygon();
  429. new_rail->add_point(e);
  430. new_rail->add_point(s);
  431. new_rail->bounce = bnc;
  432. new_rail->finalize();
  433. new_rail->notification = &notify_rail_hit;
  434. table->objects.push_back(new_rail);
  435. }
  436. }
  437. }
  438. const nx_json* portals = nx_json_get(json, "portals");
  439. if(portals) {
  440. for(int i = 0; i < portals->children.length; i++) {
  441. const nx_json* portal = nx_json_item(portals, i);
  442. Vec2 a1;
  443. if(!table_file_parse_vec2(portal, "a_start", a1)) {
  444. FURI_LOG_E(TAG, "Portal missing \"a_start\", skipping");
  445. continue;
  446. }
  447. if(!ON_TABLE(a1)) {
  448. FURI_LOG_W(
  449. TAG,
  450. "Portal A with starting position %.1f,%.1f is not on table!",
  451. (double)a1.x,
  452. (double)a1.y);
  453. }
  454. Vec2 a2;
  455. if(!table_file_parse_vec2(portal, "a_end", a2)) {
  456. FURI_LOG_E(TAG, "Portal missing \"a_end\", skipping");
  457. continue;
  458. }
  459. if(!ON_TABLE(a2)) {
  460. FURI_LOG_W(
  461. TAG,
  462. "Portal A with ending position %.1f,%.1f is not on table!",
  463. (double)a2.x,
  464. (double)a2.y);
  465. }
  466. Vec2 b1;
  467. if(!table_file_parse_vec2(portal, "b_start", b1)) {
  468. FURI_LOG_E(TAG, "Portal missing \"b_start\", skipping");
  469. continue;
  470. }
  471. if(!ON_TABLE(b1)) {
  472. FURI_LOG_W(
  473. TAG,
  474. "Portal B with starting position %.1f,%.1f is not on table!",
  475. (double)b1.x,
  476. (double)b1.y);
  477. }
  478. Vec2 b2;
  479. if(!table_file_parse_vec2(portal, "b_end", b2)) {
  480. FURI_LOG_E(TAG, "Portal missing \"b_end\", skipping");
  481. continue;
  482. }
  483. if(!ON_TABLE(b2)) {
  484. FURI_LOG_W(
  485. TAG,
  486. "Portal B with ending position %.1f,%.1f is not on table!",
  487. (double)b2.x,
  488. (double)b2.y);
  489. }
  490. Portal* new_portal = new Portal(a1, a2, b1, b2);
  491. new_portal->finalize();
  492. new_portal->notification = &notify_portal;
  493. table->objects.push_back(new_portal);
  494. }
  495. }
  496. const nx_json* rollovers = nx_json_get(json, "rollovers");
  497. if(rollovers) {
  498. for(int i = 0; i < rollovers->children.length; i++) {
  499. const nx_json* rollover = nx_json_item(rollovers, i);
  500. Vec2 p;
  501. if(!table_file_parse_vec2(rollover, "position", p)) {
  502. FURI_LOG_E(TAG, "Rollover missing \"position\", skipping");
  503. continue;
  504. }
  505. if(!ON_TABLE(p)) {
  506. FURI_LOG_W(
  507. TAG,
  508. "Rollover with position %.1f,%.1f is not on table!",
  509. (double)p.x,
  510. (double)p.y);
  511. }
  512. char sym = '*';
  513. const nx_json* symbol = nx_json_get(rollover, "symbol");
  514. if(symbol) {
  515. sym = symbol->text_value[0];
  516. }
  517. Rollover* new_rollover = new Rollover(p, sym);
  518. table->objects.push_back(new_rollover);
  519. }
  520. }
  521. const nx_json* turbos = nx_json_get(json, "turbos");
  522. if(turbos) {
  523. for(int i = 0; i < turbos->children.length; i++) {
  524. const nx_json* turbo = nx_json_item(turbos, i);
  525. Vec2 p;
  526. if(!table_file_parse_vec2(turbo, "position", p)) {
  527. FURI_LOG_E(TAG, "Turbo missing \"position\"");
  528. continue;
  529. }
  530. if(!ON_TABLE(p)) {
  531. FURI_LOG_W(
  532. TAG,
  533. "Turbo with position %.1f,%.1f is not on table!",
  534. (double)p.x,
  535. (double)p.y);
  536. }
  537. float angle = 0;
  538. table_file_parse_float(turbo, "angle", angle);
  539. angle *= pi_180;
  540. float boost = 10;
  541. table_file_parse_float(turbo, "boost", boost);
  542. Turbo* new_turbo = new Turbo(p, angle, boost);
  543. table->objects.push_back(new_turbo);
  544. }
  545. }
  546. break;
  547. } while(false);
  548. nx_json_free(json);
  549. free(json_buffer);
  550. return table;
  551. }
  552. TableList::~TableList() {
  553. for(auto& mi : menu_items) {
  554. furi_string_free(mi.name);
  555. furi_string_free(mi.filename);
  556. }
  557. }
  558. Table* table_init_table_select(void* ctx) {
  559. UNUSED(ctx);
  560. Table* table = new Table();
  561. table->balls.push_back(Ball(Vec2(20, 880), 35));
  562. table->balls.back().add_velocity(Vec2(7, 0), .10f);
  563. table->balls.push_back(Ball(Vec2(610, 920), 30));
  564. table->balls.back().add_velocity(Vec2(-8, 0), .10f);
  565. table->balls.push_back(Ball(Vec2(250, 980), 20));
  566. table->balls.back().add_velocity(Vec2(10, 0), .10f);
  567. table->balls_released = true;
  568. Polygon* new_rail = new Polygon();
  569. new_rail->add_point({-1, 840});
  570. new_rail->add_point({-1, 1280});
  571. new_rail->finalize();
  572. new_rail->hidden = true;
  573. table->objects.push_back(new_rail);
  574. new_rail = new Polygon();
  575. new_rail->add_point({-1, 1280});
  576. new_rail->add_point({640, 1280});
  577. new_rail->finalize();
  578. new_rail->hidden = true;
  579. table->objects.push_back(new_rail);
  580. new_rail = new Polygon();
  581. new_rail->add_point({640, 1280});
  582. new_rail->add_point({640, 840});
  583. new_rail->finalize();
  584. new_rail->hidden = true;
  585. table->objects.push_back(new_rail);
  586. int gap = 8;
  587. int speed = 3;
  588. float top = 20;
  589. // right side
  590. table->objects.push_back(new Chaser(Vec2(32, top), Vec2(62, top), gap, speed));
  591. table->objects.push_back(new Chaser(Vec2(62, top), Vec2(62, 84), gap, speed));
  592. table->objects.push_back(new Chaser(Vec2(62, 84), Vec2(32, 84), gap, speed));
  593. // left side
  594. table->objects.push_back(new Chaser(Vec2(32, top), Vec2(1, top), gap, speed));
  595. table->objects.push_back(new Chaser(Vec2(1, top), Vec2(1, 84), gap, speed));
  596. table->objects.push_back(new Chaser(Vec2(1, 84), Vec2(32, 84), gap, speed));
  597. return table;
  598. }
  599. Table* table_init_table_error(void* ctx) {
  600. UNUSED(ctx);
  601. // PinballApp* pb = (PinballApp*)ctx;
  602. Table* table = new Table();
  603. table->balls.push_back(Ball(Vec2(20, 880), 30));
  604. table->balls.back().add_velocity(Vec2(7, 0), .10f);
  605. // table->balls.push_back(Ball(Vec2(610, 920), 30));
  606. // table->balls.back().add_velocity(Vec2(-8, 0), .10f);
  607. // table->balls.push_back(Ball(Vec2(250, 980), 20));
  608. // table->balls.back().add_velocity(Vec2(10, 0), .10f);
  609. table->balls_released = true;
  610. Polygon* new_rail = new Polygon();
  611. new_rail->add_point({-1, 840});
  612. new_rail->add_point({-1, 1280});
  613. new_rail->finalize();
  614. new_rail->hidden = true;
  615. table->objects.push_back(new_rail);
  616. new_rail = new Polygon();
  617. new_rail->add_point({-1, 1280});
  618. new_rail->add_point({640, 1280});
  619. new_rail->finalize();
  620. new_rail->hidden = true;
  621. table->objects.push_back(new_rail);
  622. new_rail = new Polygon();
  623. new_rail->add_point({640, 1280});
  624. new_rail->add_point({640, 840});
  625. new_rail->finalize();
  626. new_rail->hidden = true;
  627. table->objects.push_back(new_rail);
  628. int gap = 8;
  629. int speed = 3;
  630. float top = 20;
  631. table->objects.push_back(new Chaser(Vec2(2, top), Vec2(61, top), gap, speed, Chaser::SLASH));
  632. table->objects.push_back(new Chaser(Vec2(2, top), Vec2(2, 84), gap, speed, Chaser::SLASH));
  633. table->objects.push_back(new Chaser(Vec2(2, 84), Vec2(61, 84), gap, speed, Chaser::SLASH));
  634. table->objects.push_back(new Chaser(Vec2(61, top), Vec2(61, 84), gap, speed, Chaser::SLASH));
  635. return table;
  636. }
  637. Table* table_init_table_settings(void* ctx) {
  638. UNUSED(ctx);
  639. Table* table = new Table();
  640. // table->balls.push_back(Ball(Vec2(20, 880), 10));
  641. // table->balls.back().add_velocity(Vec2(7, 0), .10f);
  642. // table->balls.push_back(Ball(Vec2(610, 920), 10));
  643. // table->balls.back().add_velocity(Vec2(-8, 0), .10f);
  644. // table->balls.push_back(Ball(Vec2(250, 980), 10));
  645. // table->balls.back().add_velocity(Vec2(10, 0), .10f);
  646. table->balls_released = true;
  647. Polygon* new_rail = new Polygon();
  648. new_rail->add_point({-1, 840});
  649. new_rail->add_point({-1, 1280});
  650. new_rail->finalize();
  651. new_rail->hidden = true;
  652. table->objects.push_back(new_rail);
  653. new_rail = new Polygon();
  654. new_rail->add_point({-1, 1280});
  655. new_rail->add_point({640, 1280});
  656. new_rail->finalize();
  657. new_rail->hidden = true;
  658. table->objects.push_back(new_rail);
  659. new_rail = new Polygon();
  660. new_rail->add_point({640, 1280});
  661. new_rail->add_point({640, 840});
  662. new_rail->finalize();
  663. new_rail->hidden = true;
  664. table->objects.push_back(new_rail);
  665. return table;
  666. }
  667. bool table_load_table(void* ctx, size_t index) {
  668. PinballApp* pb = (PinballApp*)ctx;
  669. // read the index'th file in pb->table_list and allocate
  670. FURI_LOG_I(TAG, "Loading table %u", index);
  671. // if there's already a table loaded, free it
  672. if(pb->table) {
  673. delete pb->table;
  674. pb->table = nullptr;
  675. }
  676. pb->gameStarted = false;
  677. switch(index) {
  678. case TABLE_SELECT:
  679. pb->table = table_init_table_select(ctx);
  680. break;
  681. case TABLE_ERROR:
  682. pb->table = table_init_table_error(ctx);
  683. break;
  684. case TABLE_SETTINGS:
  685. pb->table = table_init_table_settings(ctx);
  686. break;
  687. default:
  688. pb->table = table_load_table_from_file(pb, index - TABLE_INDEX_OFFSET);
  689. break;
  690. }
  691. return pb->table != NULL;
  692. }