utilstuff.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef __UTILSTUFF_H
  2. #define __UTILSTUFF_H
  3. inline double pow2(double a) {
  4. return a*a;
  5. }
  6. inline double gaussian_function(double a, double b, double c, double x) {
  7. // a * e ^ ( - ((x-b)^2) / (2*c^2) )
  8. return a * ::exp(-(pow2(x - b) / (2*pow2(c))));
  9. }
  10. inline double distance(double ax, double ay, double bx, double by) {
  11. double q = (ax - bx);
  12. double p = (ay - by);
  13. return ::sqrt(q*q + p*p);
  14. }
  15. inline bool player_walkable_aux(GameState& state, unsigned int x, unsigned int y, bool walk_rock = false) {
  16. if (!walk_rock && !state.grid.is_walk(x, y))
  17. return false;
  18. features::Feature feat;
  19. if (state.features.get(x, y, feat)) {
  20. const Terrain& t = terrain().get(feat.tag);
  21. if (t.viewblock || t.walkblock)
  22. return false;
  23. }
  24. return true;
  25. }
  26. inline bool player_walkable(GameState& state, unsigned int x, unsigned int y) {
  27. return player_walkable_aux(state, x, y);
  28. }
  29. inline bool monster_walkable(GameState& state, const Species& s, unsigned int x, unsigned int y) {
  30. if (!monsters::Monsters::is_walkable(state.grid, s, x, y))
  31. return false;
  32. features::Feature feat;
  33. if (state.features.get(x, y, feat)) {
  34. const Terrain& t = terrain().get(feat.tag);
  35. if (t.walkblock)
  36. return false;
  37. }
  38. return true;
  39. }
  40. template <typename FUNC>
  41. bool reachable(GameState& state, unsigned int ax, unsigned int ay, unsigned int bx, unsigned int by,
  42. FUNC f) {
  43. bresenham::Line line(ax, ay, bx, by);
  44. unsigned int x = ax;
  45. unsigned int y = ay;
  46. while (1) {
  47. bool ok = f(state, x, y);
  48. if (!ok)
  49. break;
  50. bool ret = line.step((int&)x, (int&)y);
  51. if (!ret)
  52. return true;
  53. }
  54. return false;
  55. }
  56. template <typename FUNC>
  57. void radial_points(unsigned int px, unsigned int py, GameState& state, unsigned int radius,
  58. std::unordered_set<neighbors::pt>& points, FUNC f) {
  59. unsigned int r2 = radius * radius;
  60. for (int dx = -radius; dx <= (int)radius; ++dx) {
  61. unsigned int dy = ::sqrt(r2 - dx*dx);
  62. int x = px + dx;
  63. if (x < 0 || x > (int)state.neigh.w)
  64. continue;
  65. int y1 = py + dy;
  66. if (y1 >= 0 && y1 < (int)state.neigh.h) {
  67. if (reachable(state, x, y1, px, py, f)) {
  68. points.insert(neighbors::pt(x, y1));
  69. }
  70. }
  71. int y2 = py - dy;
  72. if (y2 >= 0 && y2 < (int)state.neigh.h) {
  73. if (reachable(state, x, y2, px, py, f)) {
  74. points.insert(neighbors::pt(x, y2));
  75. }
  76. }
  77. }
  78. }
  79. template <typename FUNC>
  80. bool path_walk(GameState& state,
  81. unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1,
  82. unsigned int n, unsigned int cutoff, FUNC cost,
  83. unsigned int& xo, unsigned int& yo) {
  84. bool tmp = state.path.compute(x0, y0, x1, y1, 1.41, cutoff, cost);
  85. if (!tmp) return false;
  86. for (unsigned int i = 0; i < n; ++i) {
  87. if (!state.path.walk(xo, yo))
  88. return false;
  89. }
  90. return true;
  91. }
  92. inline bool digging_step(GameState& state, unsigned int x, unsigned int y, double speed) {
  93. float& height = state.grid._get(x, y);
  94. height -= speed;
  95. if (height < -10) {
  96. height = -10;
  97. bool water = state.grid.is_water(x, y);
  98. state.grid.set_walk_water(state.neigh, x, y, true, water);
  99. state.render.invalidate(x, y);
  100. return true;
  101. }
  102. return false;
  103. }
  104. #endif