Das Programmierspiel von der Gulasch-Programmier-Nacht 11

(Latest mirror + merged latest fork by qr4 on Lua 5.3)

Entropia info page https://entropia.de/GPN11:Programmierspiel (dead links)
Origin Gitlab
https://code.nerd2nerd.org/n2n/WeltraumProgrammierNacht

json_output.c 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "globals.h"
  5. #include "json_output.h"
  6. void json_output(cluster_t* planets, int n_planets) {
  7. int i;
  8. printf("{ \"world\":\n");
  9. printf(" { \"bounding-box\": { \"xmin\": 0, \"xmax\": %d, \"ymin\": 0, \"ymax\": %d},\n", GLOBALS.WIDTH, GLOBALS.HEIGHT);
  10. printf(" \"planets\": [\n");
  11. for(i = 0; i < n_planets-1; i++) {
  12. printf(" {\"id\":%d, \"x\": %f, \"y\": %f, \"owner\": null},\n", i, planets[i].center.x, planets[i].center.y);
  13. }
  14. printf(" {\"id\":%d, \"x\": %f, \"y\": %f, \"owner\": null}\n", i, planets[n_planets-1].center.x, planets[n_planets-1].center.y);
  15. printf(" ],\n");
  16. printf(" \"ships\": [ ],\n");
  17. printf(" \"asteroids\": [ ],\n");
  18. printf(" \"players\": [\n");
  19. printf(" {\"id\": 1, \"name\": \"SDL\"},\n");
  20. printf(" {\"id\": 2, \"name\": \"Test\"}\n");
  21. printf(" ]\n");
  22. printf(" }\n");
  23. printf("}\n");
  24. }
  25. void json_update(waypoint_t* route1, waypoint_t* route2, int n_planets) {
  26. waypoint_t* t1 = route1;
  27. waypoint_t* t2 = route2;
  28. while(t1 || t2) {
  29. printf("{ \"update\":\n");
  30. printf(" { \"bounding-box\": { \"xmin\": 0, \"xmax\": %d, \"ymin\": 0, \"ymax\": %d},\n", GLOBALS.WIDTH, GLOBALS.HEIGHT);
  31. printf(" \"ships\": [ \n");
  32. if(t1) {
  33. printf(" {\"id\": %d, \"x\": %f, \"y\": %f, \"owner\": 1, \"size\": 3, \"contents\": \"T \", \"docked_to\": null}%s\n", n_planets+1, t1->point.x, t1->point.y, t2 ? ",": "");
  34. }
  35. if(t2) {
  36. printf(" {\"id\": %d, \"x\": %f, \"y\": %f, \"owner\": 2, \"size\": 6, \"contents\": \"TTLRR \", \"docked_to\": null}\n", n_planets+2, t2->point.x, t2->point.y);
  37. }
  38. if((t1 && ! t1->next) || (t2 && ! t2->next)) {
  39. printf(" \"], \n");
  40. printf(" \"explosions\": [\n");
  41. if(t1 && ! t1->next) {
  42. printf(" {\"id\": %d, \"x\": %f, \"y\": %f}%s\n", n_planets+1, t1->point.x, t1->point.y, (t2 && !t2->next) ? ",": "");
  43. }
  44. if(t2 && ! t2->next) {
  45. printf(" {\"id\": %d, \"x\": %f, \"y\": %f}\n", n_planets+2, t2->point.x, t2->point.y);
  46. }
  47. }
  48. printf(" ],\n");
  49. if(t1) {
  50. t1 = t1->next;
  51. }
  52. if(t2) {
  53. t2 = t2->next;
  54. }
  55. printf(" \"shots\": [ ],\n");
  56. printf(" \"hits\": [ ]\n");
  57. printf(" }\n");
  58. printf("}\n");
  59. }
  60. }