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

route.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "route.h"
  5. #include "globals.h"
  6. // Abstand zwischen zwei Punkten
  7. double dist(pixel_t* A, pixel_t* B) {
  8. return hypotf(B->x - A->x, B->y - A->y);
  9. }
  10. // Wo auf der Linie zwischen A und B sitzt der Fußpunkt des Lots von C auf AB?
  11. // Werte kleiner Null bedeuten vor A entlang AB
  12. // Werte zwischen 0 und 1 geben an nach welchem Bruchteil von AB der Fußpunkt kommt
  13. // Werte größer 1 bedeuten daß der Fußpunkt auf der Verlängerung von AB hinter B liegt
  14. // Wert den Code mit A=B aufruf ist doof und verdient daß alles platzt
  15. double dividing_ratio(pixel_t* A, pixel_t* B, pixel_t* C) {
  16. return ((C->x - A->x)*(B->x - A->x) + (C->y - A->y)*(B->y - A->y))/pow(dist(A, B),2);
  17. }
  18. // Was ist der Minimal-Abstand von C zum Linien AB?
  19. // Achtung dies liefert den vorzeichenbehafteten Abstand
  20. // Werte kleiner Null bedeuten das C "links" der Verbindungslinie liegt wenn man von A Richtung B schaut
  21. // Werte größer Null dementsprechend recht, exakt null auf der Verbindungslinie
  22. double dist_to_line(pixel_t* A, pixel_t* B, pixel_t* C) {
  23. //return ((A->y - B->y)*C->x + (A->x - B->x)*C->y + (A->x * B->y - A->y * B->x))/dist(A,B);
  24. //return ((B->y - A->y)*C->x + (A->x - B->x)* C->y - ((B->y - A->y)* A->x + (A->x - B->x)* A->y))/dist(A,B);
  25. return ((A->y - B->y)*C->x - (A->x - B->x)* C->y + ((B->y - A->y)* A->x + (A->x - B->x)* A->y))/dist(A,B);
  26. }
  27. // Was ist der Minimal-Abstand von C zum Liniensegment AB?
  28. // Die ist der Abstand zwischen C und dem Fußpunkt des Lots auf AB fall dieser zwischen A und B fällt
  29. // Ansonsten der Abstand zu A btw B
  30. double dist_to_seg(pixel_t* A, pixel_t* B, pixel_t* C) {
  31. double r = dividing_ratio(A, B, C);
  32. if(r <= 0) {
  33. return dist(A, C);
  34. } else if (r >= 1) {
  35. return dist(B, C);
  36. } else {
  37. return fabs(dist_to_line(A, B, C));
  38. }
  39. }
  40. waypoint_t* go_around(pixel_t* A, pixel_t* B, pixel_t* C, double r) {
  41. pixel_t X = { A->x + r*(B->x - A->x), A->y + r*(B->y - A->y) };
  42. double d = dist(&X, C);
  43. pixel_t W = {C->x + safety_radius*sqrt(2)*(X.x - C->x) / d, C->y + safety_radius*sqrt(2)*(X.y - C->y) / d};
  44. waypoint_t* wp = malloc(sizeof(waypoint_t));
  45. wp->next = NULL;
  46. wp->point.x = W.x;
  47. wp->point.y = W.y;
  48. return wp;
  49. }
  50. waypoint_t* route(pixel_t* start, pixel_t* stop, pixel_t* points, int n_points) {
  51. int i;
  52. int i_min = -1;
  53. double r_min = 1;
  54. for(i = 0; i < n_points; i++) {
  55. if((points[i].x < start->x - safety_radius) && (points[i].x < stop->x - safety_radius)) {
  56. continue;
  57. }
  58. if((points[i].x > start->x + safety_radius) && (points[i].x > stop->x + safety_radius)) {
  59. continue;
  60. }
  61. if((points[i].y < start->y - safety_radius) && (points[i].y < stop->y - safety_radius)) {
  62. continue;
  63. }
  64. if((points[i].y > start->y + safety_radius) && (points[i].y > stop->y + safety_radius)) {
  65. continue;
  66. }
  67. double r = dividing_ratio(start, stop, &(points[i]));
  68. if (r > 0 && r < 1) {
  69. double d = dist_to_line(start, stop, &(points[i]));
  70. if (fabs(d) < safety_radius) {
  71. if(fabs(r-0.5) < fabs(r_min-0.5)) {
  72. i_min = i;
  73. r_min = r;
  74. }
  75. }
  76. }
  77. }
  78. if(i_min >= 0) {
  79. waypoint_t* wp = go_around(start, stop, &(points[i_min]), r_min);
  80. waypoint_t* part1 = route(start, &(wp->point), points, n_points);
  81. if(part1 == NULL) {
  82. part1 = wp;
  83. } else {
  84. waypoint_t* t = part1;
  85. while(t->next != NULL) {
  86. t = t->next;
  87. }
  88. t->next = wp;
  89. }
  90. waypoint_t* part2 = route(&(wp->point), stop, points, n_points);
  91. if(part2 != 0) {
  92. waypoint_t* t = part1;
  93. while(t->next != NULL) {
  94. t = t->next;
  95. }
  96. t->next = part2;
  97. }
  98. return part1;
  99. } else {
  100. return NULL;
  101. }
  102. }
  103. waypoint_t* plotCourse(pixel_t* start, pixel_t* stop, pixel_t* points, int n) {
  104. int n_points = n*n;
  105. waypoint_t* wp_start = malloc(sizeof(waypoint_t));
  106. waypoint_t* wp_stop = malloc(sizeof(waypoint_t));
  107. wp_start->point.x = start->x;
  108. wp_start->point.y = start->y;
  109. wp_stop->point.x = stop->x;
  110. wp_stop->point.y = stop->y;
  111. wp_start->next = route(start, stop, points, n_points);
  112. wp_stop->next = NULL;
  113. waypoint_t* t = wp_start;
  114. while (t->next != NULL) {
  115. t = t->next;
  116. }
  117. t->next = wp_stop;
  118. return wp_start;
  119. }
  120. void get_surrounding_points(pixel_t *surrounding_points, pixel_t *points, int n, int face_x, int face_y) {
  121. surrounding_points[0] = points[(face_y - 1) * n + (face_x - 1)];
  122. surrounding_points[1] = points[(face_y) * n + (face_x - 1)];
  123. surrounding_points[2] = points[(face_y) * n + (face_x)];
  124. surrounding_points[3] = points[(face_y - 1) * n + (face_x)];
  125. }
  126. int get_line_intersection(pixel_t *P0, pixel_t *P1, pixel_t *P2, pixel_t *P3, pixel_t *result)
  127. {
  128. float s1_x, s1_y, s2_x, s2_y;
  129. s1_x = P1->x - P0->x;
  130. s1_y = P1->y - P0->y;
  131. s2_x = P3->x - P2->x;
  132. s2_y = P3->y - P2->y;
  133. float s, t;
  134. s = (-s1_y * (P0->x - P2->x) + s1_x * (P0->y - P2->y)) / (-s2_x * s1_y + s1_x * s2_y);
  135. t = ( s2_x * (P0->y - P2->y) - s2_y * (P0->x - P2->x)) / (-s2_x * s1_y + s1_x * s2_y);
  136. if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
  137. {
  138. if (result) {
  139. result->x = P0->x + (t * s1_x);
  140. result->y = P0->y + (t * s1_y);
  141. }
  142. return 1;
  143. }
  144. return 0;
  145. }
  146. void find_face(pixel_t *p, pixel_t *points, int n, int *x, int *y) {
  147. int face_x = p->x / (GLOBALS.WIDTH / (n + 1));
  148. int face_y = p->y / (GLOBALS.HEIGHT / (n + 1));
  149. int edge;
  150. int retry = 1;
  151. int count = 0;
  152. pixel_t surrounding[4];
  153. while (retry && count < 4) {
  154. retry = 0;
  155. get_surrounding_points(surrounding, points, n, face_x, face_y);
  156. for (edge = 0; edge < 4; edge++) {
  157. double r = dist_to_line(surrounding + edge, surrounding + (edge + 1) % 4, p);
  158. if (r > 0) {
  159. fprintf(stderr, "edge: %d, r %f, x %d, y %d\n", edge, r, face_x, face_y);
  160. retry = 1;
  161. count++;
  162. switch (edge) {
  163. case 0 :
  164. face_x--;
  165. break;
  166. case 1 :
  167. face_y++;
  168. break;
  169. case 2 :
  170. face_x++;
  171. break;
  172. case 3 :
  173. face_y--;
  174. break;
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. *x = face_x;
  181. *y = face_y;
  182. }
  183. waypoint_t *smooth(waypoint_t *way, int res) {
  184. waypoint_t *end = way;
  185. waypoint_t *working_start;
  186. waypoint_t *working;
  187. pixel_t startp;
  188. pixel_t midp;
  189. pixel_t endp;
  190. pixel_t t1, t2;
  191. pixel_t v1, v2;
  192. pixel_t s;
  193. int i;
  194. working_start = malloc(sizeof(waypoint_t));
  195. *working_start = *way;
  196. working = working_start;
  197. if (end) {
  198. midp = end->point;
  199. end = end->next;
  200. } else return way;
  201. if (end) {
  202. endp = end->point;
  203. end = end->next;
  204. } else return way;
  205. while (end != NULL) {
  206. startp = midp;
  207. midp = endp;
  208. endp = end->point;
  209. t1.x = (startp.x + midp.x) / 2.;
  210. t1.y = (startp.y + midp.y) / 2.;
  211. t2 = midp;
  212. v1.x = (midp.x - startp.x) / 2. / res;
  213. v1.y = (midp.y - startp.y) / 2. / res;
  214. v2.x = (endp.x - midp.x) / 2. / res;
  215. v2.y = (endp.y - midp.y) / 2. / res;
  216. for (i = 0; i < res; i++) {
  217. s.x = t1.x + ((t2.x - t1.x) * i) / res;
  218. s.y = t1.y + ((t2.y - t1.y) * i) / res;
  219. t1.x += v1.x;
  220. t1.y += v1.y;
  221. t2.x += v2.x;
  222. t2.y += v2.y;
  223. working->next = malloc (sizeof(waypoint_t));
  224. working = working->next;
  225. working->point = s;
  226. }
  227. working->next = end;
  228. end = end->next;
  229. }
  230. return working_start;
  231. }
  232. waypoint_t* plotCourse_gridbased(pixel_t *start, pixel_t *stop, pixel_t *points, int n) {
  233. int face_x, face_y, face_s_x, face_s_y;
  234. int edge;
  235. int last_edge = -1;
  236. find_face(start, points, n, &face_x, &face_y);
  237. find_face(stop, points, n, &face_s_x, &face_s_y);
  238. pixel_t surrounding[4];
  239. waypoint_t *wp_start = malloc (sizeof(waypoint_t));
  240. wp_start->point = *start;
  241. wp_start->next = NULL;
  242. waypoint_t *working = wp_start;
  243. while (face_x != face_s_x || face_y != face_s_y) {
  244. pixel_t c;
  245. get_surrounding_points(surrounding, points, n, face_x, face_y);
  246. for (edge = 0; edge < 4; edge++) {
  247. if (edge == last_edge) continue;
  248. c = working->point;
  249. if (get_line_intersection(surrounding + edge, surrounding + (edge + 1)%4, &c, stop, NULL)) {
  250. c.x = (surrounding[edge].x + surrounding[(edge + 1) % 4].x) / 2;
  251. c.y = (surrounding[edge].y + surrounding[(edge + 1) % 4].y) / 2;
  252. break;
  253. }
  254. }
  255. working->next = malloc(sizeof(waypoint_t));
  256. working = working->next;
  257. working->point = c;
  258. working->next = NULL;
  259. switch (edge) {
  260. case 0 :
  261. face_x--;
  262. break;
  263. case 1 :
  264. face_y++;
  265. break;
  266. case 2 :
  267. face_x++;
  268. break;
  269. case 3 :
  270. face_y--;
  271. break;
  272. default:
  273. return wp_start;
  274. }
  275. last_edge = (edge + 2) % 4;
  276. }
  277. working->next = malloc(sizeof(waypoint_t));
  278. working = working->next;
  279. working->point = *stop;
  280. working->next = NULL;
  281. return wp_start;
  282. }