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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, j;
  8. float asteroidradius = 0;
  9. float planetradius = 0;
  10. for(i = 0; i < n_planets-1; i++) {
  11. if(planets[i].safety_radius > 0) {
  12. if(planets[i].safety_radius > asteroidradius) {
  13. if(planets[i].safety_radius >= planetradius) {
  14. planetradius = planets[i].safety_radius;
  15. } else {
  16. asteroidradius = planets[i].safety_radius;
  17. }
  18. } else {
  19. asteroidradius = planets[i].safety_radius;
  20. }
  21. }
  22. }
  23. fprintf(stderr, "asteroids are %f and planets are %f\n", asteroidradius, planetradius);
  24. int asteroidseen = 0;
  25. int asteroidmax = 0;
  26. for(i = 0; i < n_planets-1; i++) {
  27. if(planets[i].safety_radius == asteroidradius) {
  28. asteroidmax++;
  29. }
  30. }
  31. int planetseen = 0;
  32. int planetmax = 0;
  33. for(i = 0; i < n_planets-1; i++) {
  34. if(planets[i].safety_radius == planetradius) {
  35. planetmax++;
  36. }
  37. }
  38. printf("{ \"world\":\n");
  39. printf(" { \"bounding-box\": { \"xmin\": 0, \"xmax\": %d, \"ymin\": 0, \"ymax\": %d},\n", GLOBALS.WIDTH, GLOBALS.HEIGHT);
  40. printf(" \"planets\": [\n");
  41. for(i = 0; i < n_planets-1; i++) {
  42. if(planets[i].safety_radius >= planetradius) {
  43. printf(" {\"id\":%d, \"x\": %f, \"y\": %f, \"owner\": null}%s\n", i+1, planets[i].center.x, planets[i].center.y, planetseen < planetmax-1 ? "," : "");
  44. planetseen++;
  45. }
  46. }
  47. printf(" ],\n");
  48. printf(" \"asteroids\": [\n");
  49. for(i = 0; i < n_planets-1; i++) {
  50. if(planets[i].safety_radius >= asteroidradius && planets[i].safety_radius < planetradius) {
  51. char contents[8] = " ";
  52. for(j = 0; j < i%7; j++) {
  53. contents[j] = 'R';
  54. }
  55. printf(" {\"id\":%d, \"x\": %f, \"y\": %f, \"contents\": \"%s\"}%s\n", i+1, planets[i].center.x, planets[i].center.y, contents, asteroidseen < asteroidmax-1 ? "," : "");
  56. asteroidseen++;
  57. }
  58. }
  59. printf(" ],\n");
  60. printf(" \"ships\": [ ],\n");
  61. printf(" \"players\": [\n");
  62. printf(" {\"id\": 1, \"name\": \"SDL\"},\n");
  63. printf(" {\"id\": 2, \"name\": \"Test\"}\n");
  64. printf(" ]\n");
  65. printf(" }\n");
  66. printf("}\n\n");
  67. }
  68. void json_update(waypoint_t* route1, waypoint_t* route2, int n_planets) {
  69. waypoint_t* t1 = route1;
  70. waypoint_t* t2 = route2;
  71. while(t1 || t2) {
  72. printf("{ \"update\":\n");
  73. printf(" { \"bounding-box\": { \"xmin\": 0, \"xmax\": %d, \"ymin\": 0, \"ymax\": %d},\n", GLOBALS.WIDTH, GLOBALS.HEIGHT);
  74. printf(" \"ships\": [ \n");
  75. if(t1) {
  76. 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 ? ",": "");
  77. }
  78. if(t2) {
  79. 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);
  80. }
  81. if((t1 && ! t1->next) || (t2 && ! t2->next)) {
  82. printf(" ], \n");
  83. printf(" \"explosions\": [\n");
  84. if(t1 && ! t1->next) {
  85. printf(" {\"id\": %d, \"x\": %f, \"y\": %f}%s\n", n_planets+1, t1->point.x, t1->point.y, (t2 && !t2->next) ? ",": "");
  86. }
  87. if(t2 && ! t2->next) {
  88. printf(" {\"id\": %d, \"x\": %f, \"y\": %f}\n", n_planets+2, t2->point.x, t2->point.y);
  89. }
  90. }
  91. printf(" ],\n");
  92. if(t1) {
  93. t1 = t1->next;
  94. }
  95. if(t2) {
  96. t2 = t2->next;
  97. }
  98. printf(" \"shots\": [ ],\n");
  99. printf(" \"hits\": [ ]\n");
  100. printf(" }\n");
  101. printf("}\n\n");
  102. }
  103. }