see.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <dirent.h>
  6. #include <sys/stat.h>
  7. #include "help/query.h"
  8. int main(int argc, char* argv[]) {
  9. if (argc < 3) {
  10. return 1;
  11. }
  12. char* user = argv[1];
  13. char* unit = argv[2];
  14. char* root = getenv("RELAT_ROOT");
  15. float user_x = 0, user_y = 0, user_z = 0;
  16. query_user_position(user, &user_x, &user_y, &user_z);
  17. float x = 0, y = 0, z = 0;
  18. query_unit_position(user, unit, &x, &y, &z);
  19. DIR* root_dir;
  20. while((root_dir = opendir(root)) == NULL) {
  21. perror("could not open root dir");
  22. return 1;
  23. }
  24. struct dirent* root_entry;
  25. while ((root_entry = readdir(root_dir)) != NULL) {
  26. char root_entry_path[256];
  27. sprintf(root_entry_path, "%s/%s", root, root_entry->d_name);
  28. struct stat root_entry_stat;
  29. if (stat(root_entry_path, &root_entry_stat) != 0) {
  30. perror("Could not read stats from entry");
  31. return 1;
  32. }
  33. if (S_ISDIR(root_entry_stat.st_mode)) {
  34. continue;
  35. }
  36. // TODO: better check whether directory is really a user
  37. if (root_entry->d_namlen < 3) {
  38. continue;
  39. }
  40. if (strcmp(root_entry->d_name, user) == 0) {
  41. continue;
  42. }
  43. char* another_user = root_entry->d_name;
  44. float another_user_x = 0, another_user_y = 0, another_user_z = 0;
  45. query_user_position(another_user, &another_user_x, &another_user_y, &another_user_z);
  46. char another_user_dir_name[256];
  47. sprintf(another_user_dir_name, "%s/%s", root, another_user);
  48. DIR* another_user_dir;
  49. if ((another_user_dir = opendir(another_user_dir_name)) == NULL) {
  50. perror("could not open user directory");
  51. return 1;
  52. }
  53. struct dirent* another_user_entry;
  54. while((another_user_entry = readdir(another_user_dir)) != NULL) {
  55. char entry_path[256];
  56. sprintf(entry_path, "%s/%s", root, another_user_entry->d_name);
  57. struct stat* entry_stat;
  58. if (stat(entry_path, entry_stat) != 0) {
  59. perror("Could not read stats from entry");
  60. return 1;
  61. }
  62. if (S_ISDIR(entry_stat->st_mode)) {
  63. continue;
  64. }
  65. // TODO: better check whether directory is really a user
  66. if (another_user_entry->d_namlen < 3) {
  67. continue;
  68. }
  69. char* another_unit = another_user_entry->d_name;
  70. float another_x = 0, another_y = 0, another_z = 0;
  71. query_unit_position(another_user, another_unit, &another_x, &another_y, &another_z);
  72. another_x += another_user_x - user_x;
  73. another_y += another_user_y - user_y;
  74. another_z += another_user_z - user_z;
  75. float dx = x - another_x;
  76. float dy = y - another_y;
  77. float dz = z - another_z;
  78. float distance2 = dx*dx + dy*dy + dz*dz;
  79. // TODO: get rid of static view distance
  80. if (distance2 < 10 * 10) {
  81. printf("%s: %s/%s: %.3f %.3f %.3f\n", unit, another_user, another_unit, another_x, another_y, another_z);
  82. }
  83. }
  84. closedir(another_user_dir);
  85. }
  86. closedir(root_dir);
  87. return 0;
  88. }