PlaceableProof.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include "PlaceableProof.h"
  2. #include "BlockFilter.h"
  3. #include "Dimension.h"
  4. #include "Game.h"
  5. PlaceableProof::PlaceableProof()
  6. : ReferenceCounter()
  7. {}
  8. PlaceableProofAnd::PlaceableProofAnd()
  9. : PlaceableProof()
  10. {}
  11. bool PlaceableProofAnd::isPlacable(
  12. const Item* item, Framework::Vec3<float> pos, int dimensionId)
  13. {
  14. for (PlaceableProof* proof : proofs)
  15. {
  16. if (!proof->isPlacable(item, pos, dimensionId))
  17. {
  18. return false;
  19. }
  20. }
  21. return true;
  22. }
  23. void PlaceableProofAnd::addProof(PlaceableProof* proof)
  24. {
  25. proofs.add(proof);
  26. }
  27. const Framework::RCArray<PlaceableProof>& PlaceableProofAnd::getProofs() const
  28. {
  29. return proofs;
  30. }
  31. PlaceableProofAndFactory::PlaceableProofAndFactory()
  32. : SubTypeFactory()
  33. {}
  34. PlaceableProofAnd* PlaceableProofAndFactory::fromJson(
  35. Framework::JSON::JSONObject* zJson) const
  36. {
  37. PlaceableProofAnd* result = new PlaceableProofAnd();
  38. for (Framework::JSON::JSONValue* zProof :
  39. *zJson->zValue("proofs")->asArray())
  40. {
  41. result->addProof(
  42. Game::INSTANCE->zTypeRegistry()->fromJson<PlaceableProof>(zProof));
  43. }
  44. return result;
  45. }
  46. Framework::JSON::JSONObject* PlaceableProofAndFactory::toJsonObject(
  47. PlaceableProofAnd* zObject) const
  48. {
  49. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  50. Framework::JSON::JSONArray* proofs = new Framework::JSON::JSONArray();
  51. for (PlaceableProof* proof : zObject->getProofs())
  52. {
  53. proofs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(proof));
  54. }
  55. result->addValue("proofs", proofs);
  56. return result;
  57. }
  58. JSONObjectValidationBuilder* PlaceableProofAndFactory::addToValidator(
  59. JSONObjectValidationBuilder* builder) const
  60. {
  61. return builder->withRequiredArray("proofs")
  62. ->addAcceptedTypeInArray(
  63. Game::INSTANCE->zTypeRegistry()->getValidator<PlaceableProof>())
  64. ->finishArray();
  65. }
  66. const char* PlaceableProofAndFactory::getTypeToken() const
  67. {
  68. return "and";
  69. }
  70. PlaceableProofOr::PlaceableProofOr()
  71. : PlaceableProof()
  72. {}
  73. bool PlaceableProofOr::isPlacable(
  74. const Item* item, Framework::Vec3<float> pos, int dimensionId)
  75. {
  76. for (PlaceableProof* proof : proofs)
  77. {
  78. if (proof->isPlacable(item, pos, dimensionId))
  79. {
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. void PlaceableProofOr::addProof(PlaceableProof* proof)
  86. {
  87. proofs.add(proof);
  88. }
  89. const Framework::RCArray<PlaceableProof>& PlaceableProofOr::getProofs() const
  90. {
  91. return proofs;
  92. }
  93. PlaceableProofOrFactory::PlaceableProofOrFactory()
  94. : SubTypeFactory()
  95. {}
  96. PlaceableProofOr* PlaceableProofOrFactory::fromJson(
  97. Framework::JSON::JSONObject* zJson) const
  98. {
  99. PlaceableProofOr* result = new PlaceableProofOr();
  100. for (Framework::JSON::JSONValue* zProof :
  101. *zJson->zValue("proofs")->asArray())
  102. {
  103. result->addProof(
  104. Game::INSTANCE->zTypeRegistry()->fromJson<PlaceableProof>(zProof));
  105. }
  106. return result;
  107. }
  108. Framework::JSON::JSONObject* PlaceableProofOrFactory::toJsonObject(
  109. PlaceableProofOr* zObject) const
  110. {
  111. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  112. Framework::JSON::JSONArray* proofs = new Framework::JSON::JSONArray();
  113. for (PlaceableProof* proof : zObject->getProofs())
  114. {
  115. proofs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(proof));
  116. }
  117. result->addValue("proofs", proofs);
  118. return result;
  119. }
  120. JSONObjectValidationBuilder* PlaceableProofOrFactory::addToValidator(
  121. JSONObjectValidationBuilder* builder) const
  122. {
  123. return builder->withRequiredArray("proofs")
  124. ->addAcceptedTypeInArray(
  125. Game::INSTANCE->zTypeRegistry()->getValidator<PlaceableProof>())
  126. ->finishArray();
  127. }
  128. const char* PlaceableProofOrFactory::getTypeToken() const
  129. {
  130. return "or";
  131. }
  132. PlaceableProofNot::PlaceableProofNot()
  133. : PlaceableProof()
  134. {}
  135. PlaceableProofNot::~PlaceableProofNot()
  136. {
  137. proof->release();
  138. }
  139. bool PlaceableProofNot::isPlacable(
  140. const Item* item, Framework::Vec3<float> pos, int dimensionId)
  141. {
  142. return !proof->isPlacable(item, pos, dimensionId);
  143. }
  144. void PlaceableProofNot::setProof(PlaceableProof* proof) {}
  145. PlaceableProof* PlaceableProofNot::zProof() const
  146. {
  147. return proof;
  148. }
  149. PlaceableProofNotFactory::PlaceableProofNotFactory()
  150. : SubTypeFactory()
  151. {}
  152. PlaceableProofNot* PlaceableProofNotFactory::fromJson(
  153. Framework::JSON::JSONObject* zJson) const
  154. {
  155. PlaceableProofNot* result = new PlaceableProofNot();
  156. result->setProof(Game::INSTANCE->zTypeRegistry()->fromJson<PlaceableProof>(
  157. zJson->zValue("proof")));
  158. return result;
  159. }
  160. Framework::JSON::JSONObject* PlaceableProofNotFactory::toJsonObject(
  161. PlaceableProofNot* zObject) const
  162. {
  163. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  164. result->addValue(
  165. "proof", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zProof()));
  166. return result;
  167. }
  168. JSONObjectValidationBuilder* PlaceableProofNotFactory::addToValidator(
  169. JSONObjectValidationBuilder* builder) const
  170. {
  171. return builder->withRequiredAttribute("proof",
  172. Game::INSTANCE->zTypeRegistry()->getValidator<PlaceableProof>());
  173. }
  174. const char* PlaceableProofNotFactory::getTypeToken() const
  175. {
  176. return "not";
  177. }
  178. PlaceableProofBlockFilter::PlaceableProofBlockFilter()
  179. : PlaceableProof(),
  180. direction(NO_DIRECTION),
  181. distance(1),
  182. filter(0)
  183. {}
  184. PlaceableProofBlockFilter::~PlaceableProofBlockFilter()
  185. {
  186. filter->release();
  187. }
  188. bool PlaceableProofBlockFilter::isPlacable(
  189. const Item* item, Framework::Vec3<float> pos, int dimensionId)
  190. {
  191. pos += (Framework::Vec3<float>)(::getDirection(direction) * distance);
  192. Dimension* dim = Game::INSTANCE->zDimension(dimensionId);
  193. return dim && pos.z >= 0 && pos.z < WORLD_HEIGHT
  194. && filter->test(dim->zBlockOrDefault(pos));
  195. }
  196. void PlaceableProofBlockFilter::setDirection(Direction direction)
  197. {
  198. this->direction = direction;
  199. }
  200. Direction PlaceableProofBlockFilter::getDirection() const
  201. {
  202. return direction;
  203. }
  204. void PlaceableProofBlockFilter::setDistance(int distance)
  205. {
  206. this->distance = distance;
  207. }
  208. int PlaceableProofBlockFilter::getDistance() const
  209. {
  210. return distance;
  211. }
  212. void PlaceableProofBlockFilter::setFilter(BlockFilter* filter)
  213. {
  214. this->filter = filter;
  215. }
  216. BlockFilter* PlaceableProofBlockFilter::zFilter() const
  217. {
  218. return filter;
  219. }
  220. PlaceableProofBlockFilterFactory::PlaceableProofBlockFilterFactory()
  221. : SubTypeFactory()
  222. {}
  223. PlaceableProofBlockFilter* PlaceableProofBlockFilterFactory::fromJson(
  224. Framework::JSON::JSONObject* zJson) const
  225. {
  226. PlaceableProofBlockFilter* result = new PlaceableProofBlockFilter();
  227. Direction dir = NO_DIRECTION;
  228. if (zJson->zValue("direction")->asString()->getString().isEqual("top"))
  229. {
  230. dir = TOP;
  231. }
  232. else if (zJson->zValue("direction")
  233. ->asString()
  234. ->getString()
  235. .isEqual("bottom"))
  236. {
  237. dir = BOTTOM;
  238. }
  239. else if (zJson->zValue("direction")
  240. ->asString()
  241. ->getString()
  242. .isEqual("north"))
  243. {
  244. dir = NORTH;
  245. }
  246. else if (zJson->zValue("direction")
  247. ->asString()
  248. ->getString()
  249. .isEqual("east"))
  250. {
  251. dir = EAST;
  252. }
  253. else if (zJson->zValue("direction")
  254. ->asString()
  255. ->getString()
  256. .isEqual("south"))
  257. {
  258. dir = SOUTH;
  259. }
  260. else if (zJson->zValue("direction")
  261. ->asString()
  262. ->getString()
  263. .isEqual("west"))
  264. {
  265. dir = WEST;
  266. }
  267. result->setDirection(dir);
  268. result->setFilter(Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
  269. zJson->zValue("filter")));
  270. return result;
  271. }
  272. Framework::JSON::JSONObject* PlaceableProofBlockFilterFactory::toJsonObject(
  273. PlaceableProofBlockFilter* zObject) const
  274. {
  275. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  276. Framework::Text dir = "";
  277. if (zObject->getDirection() == NORTH)
  278. {
  279. dir = "north";
  280. }
  281. else if (zObject->getDirection() == EAST)
  282. {
  283. dir = "east";
  284. }
  285. else if (zObject->getDirection() == SOUTH)
  286. {
  287. dir = "south";
  288. }
  289. else if (zObject->getDirection() == WEST)
  290. {
  291. dir = "west";
  292. }
  293. else if (zObject->getDirection() == TOP)
  294. {
  295. dir = "top";
  296. }
  297. else if (zObject->getDirection() == BOTTOM)
  298. {
  299. dir = "bottom";
  300. }
  301. result->addValue("direction", new Framework::JSON::JSONString(dir));
  302. result->addValue(
  303. "distance", new Framework::JSON::JSONNumber(zObject->getDistance()));
  304. result->addValue(
  305. "filter", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zFilter()));
  306. return result;
  307. }
  308. JSONObjectValidationBuilder* PlaceableProofBlockFilterFactory::addToValidator(
  309. JSONObjectValidationBuilder* builder) const
  310. {
  311. return builder->withRequiredString("direction")
  312. ->whichIsOneOf({"top", "bottom", "north", "east", "south", "west"})
  313. ->finishString()
  314. ->withRequiredNumber("distance")
  315. ->whichIsGreaterOrEqual(0.0)
  316. ->withDefault(1.0)
  317. ->finishNumber()
  318. ->withRequiredAttribute("filter",
  319. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>());
  320. }
  321. const char* PlaceableProofBlockFilterFactory::getTypeToken() const
  322. {
  323. return "blockFilter";
  324. }