ffeatures.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #ifndef __FEATURES_H
  2. #define __FEATURES_H
  3. #include <unordered_map>
  4. namespace features {
  5. typedef std::pair<unsigned int, unsigned int> pt;
  6. struct Feature {
  7. tag_t tag;
  8. pt xy;
  9. unsigned int decay;
  10. unsigned int charges;
  11. Feature() : xy(0, 0), decay(0), charges(0) {}
  12. Feature(tag_t _tag, const pt& _xy, unsigned int d, unsigned int c) :
  13. tag(_tag), xy(_xy), decay(d), charges(c)
  14. {}
  15. };
  16. }
  17. namespace serialize {
  18. template <>
  19. struct reader<features::Feature> {
  20. void read(Source& s, features::Feature& m) {
  21. serialize::read(s, m.tag);
  22. serialize::read(s, m.xy);
  23. serialize::read(s, m.decay);
  24. serialize::read(s, m.charges);
  25. }
  26. };
  27. template <>
  28. struct writer<features::Feature> {
  29. void write(Sink& s, const features::Feature& m) {
  30. serialize::write(s, m.tag);
  31. serialize::write(s, m.xy);
  32. serialize::write(s, m.decay);
  33. serialize::write(s, m.charges);
  34. }
  35. };
  36. }
  37. namespace features {
  38. struct Features {
  39. std::unordered_map<pt, Feature> feats;
  40. std::unordered_map<pt, std::string> labels;
  41. void init() {
  42. feats.clear();
  43. labels.clear();
  44. }
  45. void clear() {
  46. init();
  47. }
  48. bool x_set(unsigned int x, unsigned int y, tag_t tag, grender::Grid& render) {
  49. pt xy(x, y);
  50. if (feats.find(xy) == feats.end()) {
  51. set(x, y, tag, render);
  52. return true;
  53. }
  54. return false;
  55. }
  56. void x_unset(unsigned int x, unsigned int y, tag_t tag, grender::Grid& render) {
  57. pt xy(x, y);
  58. auto i = feats.find(xy);
  59. if (i != feats.end() && i->second.tag == tag) {
  60. feats.erase(i);
  61. render.invalidate(x, y);
  62. }
  63. }
  64. void label(unsigned int x, unsigned int y, const std::string& s) {
  65. pt xy(x, y);
  66. if (s.empty()) {
  67. labels.erase(xy);
  68. } else {
  69. labels[xy] = s;
  70. }
  71. }
  72. std::string label(unsigned int x, unsigned int y) {
  73. pt xy(x, y);
  74. auto i = labels.find(xy);
  75. if (i != labels.end()) {
  76. return i->second;
  77. }
  78. return "";
  79. }
  80. void set(unsigned int x, unsigned int y, tag_t tag, grender::Grid& render) {
  81. const Terrain& t = terrain().get(tag);
  82. pt xy(x, y);
  83. feats[xy] = Feature(tag, xy, t.decay, t.charges);
  84. render.invalidate(x, y);
  85. }
  86. void unset(unsigned int x, unsigned int y, grender::Grid& render) {
  87. pt xy(x, y);
  88. feats.erase(xy);
  89. render.invalidate(x, y);
  90. }
  91. template <typename T>
  92. bool get_placement(rnd::Generator& rng, grid::Map& grid, T& ptsource,
  93. Terrain::placement_t p, pt& ret) {
  94. switch (p) {
  95. case Terrain::placement_t::floor:
  96. if (!ptsource.one_of_floor(rng, ret)) return false;
  97. break;
  98. case Terrain::placement_t::water:
  99. if (!ptsource.one_of_lake(rng, ret)) return false;
  100. break;
  101. case Terrain::placement_t::corner:
  102. if (!ptsource.one_of_corner(rng, ret)) return false;
  103. break;
  104. case Terrain::placement_t::lowlands:
  105. if (!ptsource.one_of_lowlands(rng, ret)) return false;
  106. break;
  107. case Terrain::placement_t::shoreline:
  108. if (!ptsource.one_of_shore(rng, ret)) return false;
  109. break;
  110. }
  111. ptsource.add_nogen(ret.first, ret.second);
  112. return true;
  113. }
  114. template <typename T>
  115. void generate(rnd::Generator& rng, grid::Map& grid, T& ptsource,
  116. tag_t tag, unsigned int n) {
  117. // Check that the tag exists.
  118. const Terrain& ter = terrain().get(tag);
  119. for (unsigned int i = 0; i < n; ++i) {
  120. pt xy;
  121. if (!get_placement(rng, grid, ptsource, ter.placement, xy))
  122. break;
  123. const Terrain& t = terrain().get(tag);
  124. feats[xy] = Feature(tag, xy, t.decay, t.charges);
  125. }
  126. }
  127. bool uncharge(unsigned int x, unsigned int y, grender::Grid& render) {
  128. auto i = feats.find(pt(x, y));
  129. if (i == feats.end())
  130. return false;
  131. if (i->second.charges > 0) {
  132. --(i->second.charges);
  133. if (i->second.charges == 0) {
  134. feats.erase(i);
  135. render.invalidate(x, y);
  136. return false;
  137. }
  138. }
  139. return true;
  140. }
  141. bool get(unsigned int x, unsigned int y, Feature& ret) const {
  142. auto i = feats.find(pt(x, y));
  143. if (i == feats.end()) {
  144. return false;
  145. }
  146. ret = i->second;
  147. return true;
  148. }
  149. template <typename FUNC>
  150. void process(grender::Grid& render, FUNC f) {
  151. std::unordered_set<pt> wipe;
  152. for (auto& i : feats) {
  153. const Terrain& t = terrain().get(i.second.tag);
  154. if (!f(i.second, t)) {
  155. wipe.insert(i.first);
  156. }
  157. }
  158. for (const auto& xy : wipe) {
  159. feats.erase(xy);
  160. render.invalidate(xy.first, xy.second);
  161. }
  162. }
  163. };
  164. }
  165. namespace serialize {
  166. template <>
  167. struct reader<features::Features> {
  168. void read(Source& s, features::Features& t) {
  169. serialize::read(s, t.feats);
  170. serialize::read(s, t.labels);
  171. }
  172. };
  173. template <>
  174. struct writer<features::Features> {
  175. void write(Sink& s, const features::Features& t) {
  176. serialize::write(s, t.feats);
  177. serialize::write(s, t.labels);
  178. }
  179. };
  180. }
  181. #endif