move.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 dx = atol(argv[3]);
  13. float dy = 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 length = sqrt(dx * dx + dy * dy);
  33. if (length != 0) {
  34. dx /= length;
  35. dy /= length;
  36. }
  37. fprintf(unit_move_file, "%ul %ul %.3f %.3f %.3f", get_unix_timestamp(), ULONG_MAX, dx, dy, 0.0f);
  38. return 0;
  39. }