goto.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include "help/query.h"
  5. #include "help/util.h"
  6. int main(int argc, char* argv[]) {
  7. if (argc < 5) {
  8. return 1;
  9. }
  10. char* user = argv[1];
  11. char* unit = argv[2];
  12. float dest_x = atol(argv[3]);
  13. float dest_y = atol(argv[4]);
  14. char* root = getenv("RELAT_ROOT");
  15. float x, y, z;
  16. query_unit_position(user, unit, &x, &y, &z);
  17. char unit_pos_file_name[256];
  18. sprintf(unit_pos_file_name, "%s/%s/%s/position", root, user, unit);
  19. FILE* unit_pos_file;
  20. if ((unit_pos_file = fopen(unit_pos_file_name, "w+")) == NULL) {
  21. perror("could not open position file for unit");
  22. exit(-1);
  23. }
  24. fprintf(unit_pos_file, "%.3f %.3f %.3f", x, y, z);
  25. char unit_move_file_name[256];
  26. sprintf(unit_move_file_name, "%s/%s/%s/move", root, user, unit);
  27. FILE* unit_move_file;
  28. if ((unit_move_file = fopen(unit_move_file_name, "w+")) == NULL) {
  29. perror("could not open move file for unit");
  30. exit(-1);
  31. }
  32. float dx = dest_x - x;
  33. float dy = dest_y - y;
  34. float length = sqrt(dx * dx + dy * dy);
  35. if (length != 0) {
  36. dx /= length;
  37. dy /= length;
  38. }
  39. unsigned long current_time = get_unix_timestamp();
  40. unsigned long to = current_time + (unsigned long)(length * 1000);
  41. fprintf(unit_move_file, "%ul %ul %.3f %.3f %.3f", current_time, to, dx, dy, 0.0f);
  42. return 0;
  43. }