Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

LandcellId.cpp 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "LandcellId.h"
  19. LandcellId::LandcellId() :
  20. value_(0)
  21. {}
  22. LandcellId::LandcellId(uint32_t value) :
  23. value_(value)
  24. {}
  25. LandcellId::LandcellId(int x, int y) :
  26. value_((x << 24) | (y << 16) | 0xFFFFu)
  27. {
  28. assert(x >= 0 && x <= 0xFF);
  29. assert(y >= 0 && y <= 0xFF);
  30. }
  31. LandcellId::LandcellId(int x, int y, int n) :
  32. value_((x << 24) | (y << 16) | n)
  33. {
  34. assert(x >= 0 && x <= 0xFF);
  35. assert(y >= 0 && y <= 0xFF);
  36. assert(n >= 0 && n <= 0xFFFF);
  37. }
  38. int LandcellId::calcSquareDistance(LandcellId other) const
  39. {
  40. int dx = other.x() - x();
  41. int dy = other.y() - y();
  42. return dx * dx + dy * dy;
  43. }
  44. int LandcellId::x() const
  45. {
  46. return value_ >> 24;
  47. }
  48. int LandcellId::y() const
  49. {
  50. return (value_ >> 16) & 0xFF;
  51. }
  52. int LandcellId::n() const
  53. {
  54. return value_ & 0xFFFF;
  55. }
  56. uint32_t LandcellId::value() const
  57. {
  58. return value_;
  59. }
  60. bool LandcellId::isStructure() const
  61. {
  62. return n() >= 0x0100 && n() < 0xFFFE;
  63. }
  64. bool LandcellId::operator==(LandcellId other) const
  65. {
  66. return value_ == other.value_;
  67. }
  68. bool LandcellId::operator!=(LandcellId other) const
  69. {
  70. return value_ != other.value_;
  71. }