PlaceableProof.cpp 9.3 KB

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