Advertisement
Guest User

switch to TU

a guest
Jan 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. bool coordinate_is_on_map(int x, int y, Map& planet_map) {
  2. if (x < planet_map.get_width() and x >= 0) {
  3. if (y < planet_map.get_height() and y >= 0) {
  4. return true;
  5. }
  6. }
  7. return false;
  8. }
  9. Direction visibility_age_of_direction(MapLocation current_location, Map& planet_map) {
  10.  
  11. std::vector<int> total_visibility_age(8);
  12.  
  13. std::vector<std::vector<Square> > squares = planet_map.get_squares();
  14. int current_x = current_location.get_x();
  15. int current_y = current_location.get_y();
  16.  
  17. for (int y = 0; y<squares.size(); y++) {
  18. for (int x = 0; x<squares.size(); x++) {
  19. //if not passable, don't count
  20. if (!planet_map.get_square_at(x, y).is_passable) { continue; }
  21. int d_y = y - current_y;
  22. int d_x = x - current_x;
  23. if (d_x == 0) {
  24. if (d_y > 0) {
  25. total_visibility_age[0] += 1;
  26. continue;
  27. }
  28. else {
  29. total_visibility_age[4] += 1;
  30. continue;
  31. }
  32. }
  33. if (d_x == 0) {
  34. d_y = 0.0000001;
  35. }
  36. float ratio = d_y / (double)d_x;
  37. double pi = 3.1415926535897;
  38.  
  39. if (ratio <= tan(pi*22.5 / 180) and ratio >= -1 * tan(pi*22.5 / 180)) {
  40. if (d_x > 0) {
  41. total_visibility_age[2] += 1;
  42. continue;
  43. }
  44. else {
  45. total_visibility_age[6] += 1;
  46. continue;
  47. }
  48. }
  49. else if (ratio <= tan(pi*67.5 / 180) and ratio >= 1 * tan(pi*22.5 / 180)) {
  50. if (d_x > 0) {
  51. total_visibility_age[1] += 1;
  52. continue;
  53. }
  54. else {
  55. total_visibility_age[5] += 1;
  56. continue;
  57. }
  58. }
  59. else {
  60. if (d_x > 0) {
  61. total_visibility_age[3] += 1;
  62. continue;
  63. }
  64. else {
  65. total_visibility_age[7] += 1;
  66. continue;
  67. }
  68. }
  69. }
  70. }
  71. Planet this_planet = current_location.get_planet();
  72. while (total_visibility_age.size() != 0) {
  73. int max_age = total_visibility_age[0];
  74. int max_idx = 0;
  75.  
  76. for (int i = 1; i<total_visibility_age.size(); i++) {
  77. if (total_visibility_age[i] > max_age) {
  78. max_age = total_visibility_age[i];
  79. max_idx = i;
  80. }
  81. }
  82.  
  83. switch (max_idx) {
  84. case 0:
  85. if (planet_map.get_square_at(current_x, current_y+1).is_passable and coordinate_is_on_map(current_x, current_y+1, planet_map)) {
  86. return (Direction)max_idx;
  87. }
  88. break;
  89. case 1:
  90. if (planet_map.get_square_at(current_x+1, current_y+1).is_passable and coordinate_is_on_map(current_x+1, current_y+1, planet_map)) {
  91. return (Direction)max_idx;
  92. }
  93. break;
  94. case 2:
  95. if (planet_map.get_square_at(current_x+1, current_y).is_passable and coordinate_is_on_map(current_x+1, current_y, planet_map)) {
  96. return (Direction)max_idx;}
  97.  
  98. break;
  99. case 3:
  100. if (planet_map.get_square_at(current_x+1, current_y-1).is_passable and coordinate_is_on_map(current_x+1, current_y-1, planet_map)) {
  101. return (Direction)max_idx;
  102. }
  103. break;
  104. case 4:
  105. if (planet_map.get_square_at(current_x, current_y-1).is_passable and coordinate_is_on_map(current_x, current_y-1, planet_map)) {
  106. return (Direction)max_idx;
  107. }
  108. break;
  109. case 5:
  110. if (planet_map.get_square_at(current_x-1, current_y-1).is_passable and coordinate_is_on_map(current_x-1, current_y-1, planet_map)) {
  111. return (Direction)max_idx;
  112. }
  113. break;
  114. case 6:
  115. if (planet_map.get_square_at(current_x-1, current_y).is_passable and coordinate_is_on_map(current_x-1, current_y, planet_map)) {
  116. return (Direction)max_idx;
  117. }
  118. break;
  119. case 7:
  120. if (planet_map.get_square_at(current_x-1, current_y+1).is_passable and coordinate_is_on_map(current_x-1, current_y+1, planet_map)) {
  121. return (Direction)max_idx;
  122. }
  123. break;
  124. default:
  125. break;
  126. }
  127. total_visibility_age.erase(total_visibility_age.begin()+max_idx);
  128. }
  129. Direction smth_is_very_wrong = Center;
  130. return smth_is_very_wrong;
  131. //return total_visibility_age;
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement