query.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include "util.h"
  5. #ifndef QUERY_H
  6. #define QUERY_H
  7. void query_user_position(char* user, float* x, float* y, float* z) {
  8. char* root = getenv("RELAT_ROOT");
  9. char user_position_file_name[256];
  10. sprintf(user_position_file_name, "%s/%s/position", root, user);
  11. FILE* user_position_file;
  12. if ((user_position_file = fopen(user_position_file_name, "r")) == NULL) {
  13. perror("could not open user position file");
  14. exit(-1);
  15. }
  16. if (fscanf(user_position_file, "%f %f %f\n", x, y, z) != 3) {
  17. exit(-1);
  18. }
  19. fclose(user_position_file);
  20. }
  21. void query_unit_position(char* user, char* unit, float* x, float* y, float* z) {
  22. char* root = getenv("RELAT_ROOT");
  23. char unit_position_file_name[256];
  24. sprintf(unit_position_file_name, "%s/%s/%s/position", root, user, unit);
  25. FILE* unit_position_file;
  26. if ((unit_position_file = fopen(unit_position_file_name, "r")) == NULL) {
  27. perror("could not open unit position file");
  28. exit(-1);
  29. }
  30. char unit_move_file_name[256];
  31. sprintf(unit_move_file_name, "%s/%s/%s/move", root, user, unit);
  32. FILE* unit_move_file;
  33. if ((unit_move_file = fopen(unit_move_file_name, "r")) == NULL) {
  34. perror("could not open unit move file");
  35. exit(-1);
  36. }
  37. if (fscanf(unit_position_file, "%f %f %f\n", x, y, z) != 3) {
  38. exit(-1);
  39. }
  40. float dx, dy, dz;
  41. unsigned long from, to;
  42. if (fscanf(unit_move_file, "%ul %ul %f %f %f", &from, &to, &dx, &dy, &dz) != 5) {
  43. printf("awoo\n");
  44. exit(-1);
  45. }
  46. unsigned long current_time = get_unix_timestamp();
  47. float dt = ((current_time < to ? current_time : to) - from) / 1000.0;
  48. *x += dx * dt;
  49. *y += dy * dt;
  50. *z += dz * dt;
  51. fclose(unit_position_file);
  52. fclose(unit_move_file);
  53. }
  54. #endif