PlaceableProof.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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::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. : proofs(proofs)
  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. proof(proof)
  135. {}
  136. PlaceableProofNot::~PlaceableProofNot()
  137. {
  138. proof->release();
  139. }
  140. bool PlaceableProofNot::isPlacable(
  141. const Item* item, Framework::Vec3<float> pos, int dimensionId)
  142. {
  143. return !proof->isPlacable(item, pos, dimensionId);
  144. }
  145. void PlaceableProofNot::setProof(PlaceableProof* proof) {}
  146. PlaceableProof* PlaceableProofNot::zProof() const
  147. {
  148. return proof;
  149. }
  150. PlaceableProofNotFactory::PlaceableProofNotFactory()
  151. : SubTypeFactory()
  152. {}
  153. PlaceableProofNot* PlaceableProofNotFactory::fromJson(
  154. Framework::JSON::JSONObject* zJson) const
  155. {
  156. PlaceableProofNot* result = new PlaceableProofNot();
  157. result->setProof(Game::INSTANCE->zTypeRegistry()->fromJson<PlaceableProof>(
  158. zJson->zValue("proof")));
  159. return result;
  160. }
  161. Framework::JSON::JSONObject* PlaceableProofNotFactory::toJsonObject(
  162. PlaceableProofNot* zObject) const
  163. {
  164. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  165. result->addValue(
  166. "proof", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zProof()));
  167. return result;
  168. }
  169. JSONObjectValidationBuilder* PlaceableProofNotFactory::addToValidator(
  170. JSONObjectValidationBuilder* builder) const
  171. {
  172. return builder->withRequiredAttribute("proof",
  173. Game::INSTANCE->zTypeRegistry()->getValidator<PlaceableProof>());
  174. }
  175. const char* PlaceableProofNotFactory::getTypeToken() const
  176. {
  177. return "not";
  178. }
  179. PlaceableProofBlockFilter::PlaceableProofBlockFilter()
  180. : PlaceableProof(),
  181. direction(NO_DIRECTION),
  182. distance(1),
  183. filter(0)
  184. {}
  185. PlaceableProofBlockFilter::~PlaceableProofBlockFilter()
  186. {
  187. filter->release();
  188. }
  189. bool PlaceableProofBlockFilter::isPlacable(
  190. const Item* item, Framework::Vec3<float> pos, int dimensionId)
  191. {
  192. pos += (Framework::Vec3<float>)(::getDirection(direction) * distance);
  193. Dimension* dim = Game::INSTANCE->zDimension(dimensionId);
  194. return dim && pos.z >= 0 && pos.z < WORLD_HEIGHT
  195. && filter->test(dim->zBlockOrDefault(pos));
  196. }
  197. void PlaceableProofBlockFilter::setDirection(Direction direction)
  198. {
  199. this->direction = direction;
  200. }
  201. Direction PlaceableProofBlockFilter::getDirection() const
  202. {
  203. return direction;
  204. }
  205. void PlaceableProofBlockFilter::setDistance(int distance)
  206. {
  207. this->distance = distance;
  208. }
  209. int PlaceableProofBlockFilter::getDistance() const
  210. {
  211. return distance;
  212. }
  213. void PlaceableProofBlockFilter::setFilter(BlockFilter* filter)
  214. {
  215. this->filter = filter;
  216. }
  217. BlockFilter* PlaceableProofBlockFilter::zFilter() const
  218. {
  219. return filter;
  220. }
  221. PlaceableProofBlockFilterFactory::PlaceableProofBlockFilterFactory()
  222. : SubTypeFactory()
  223. {}
  224. PlaceableProofBlockFilter* PlaceableProofBlockFilterFactory::fromJson(
  225. Framework::JSON::JSONObject* zJson) const
  226. {
  227. PlaceableProofBlockFilter* result = new PlaceableProofBlockFilter();
  228. Direction dir = NO_DIRECTION;
  229. if (zJson->zValue("direction")->asString()->getString().istGleich("top"))
  230. {
  231. dir = TOP;
  232. }
  233. else if (zJson->zValue("direction")
  234. ->asString()
  235. ->getString()
  236. .istGleich("bottom"))
  237. {
  238. dir = BOTTOM;
  239. }
  240. else if (zJson->zValue("direction")
  241. ->asString()
  242. ->getString()
  243. .istGleich("north"))
  244. {
  245. dir = NORTH;
  246. }
  247. else if (zJson->zValue("direction")
  248. ->asString()
  249. ->getString()
  250. .istGleich("east"))
  251. {
  252. dir = EAST;
  253. }
  254. else if (zJson->zValue("direction")
  255. ->asString()
  256. ->getString()
  257. .istGleich("south"))
  258. {
  259. dir = SOUTH;
  260. }
  261. else if (zJson->zValue("direction")
  262. ->asString()
  263. ->getString()
  264. .istGleich("west"))
  265. {
  266. dir = WEST;
  267. }
  268. result->setDirection(dir);
  269. result->setFilter(Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
  270. zJson->zValue("filter")));
  271. return result;
  272. }
  273. Framework::JSON::JSONObject* PlaceableProofBlockFilterFactory::toJsonObject(
  274. PlaceableProofBlockFilter* zObject) const
  275. {
  276. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  277. Framework::Text dir = "";
  278. if (zObject->getDirection() == NORTH)
  279. {
  280. dir = "north";
  281. }
  282. else if (zObject->getDirection() == EAST)
  283. {
  284. dir = "east";
  285. }
  286. else if (zObject->getDirection() == SOUTH)
  287. {
  288. dir = "south";
  289. }
  290. else if (zObject->getDirection() == WEST)
  291. {
  292. dir = "west";
  293. }
  294. else if (zObject->getDirection() == TOP)
  295. {
  296. dir = "top";
  297. }
  298. else if (zObject->getDirection() == BOTTOM)
  299. {
  300. dir = "bottom";
  301. }
  302. result->addValue("direction", new Framework::JSON::JSONString(dir));
  303. result->addValue(
  304. "distance", new Framework::JSON::JSONNumber(zObject->getDistance()));
  305. result->addValue(
  306. "filter", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zFilter()));
  307. return result;
  308. }
  309. JSONObjectValidationBuilder* PlaceableProofBlockFilterFactory::addToValidator(
  310. JSONObjectValidationBuilder* builder) const
  311. {
  312. return builder->withRequiredString("direction")
  313. ->whichIsOneOf({"top", "bottom", "north", "east", "south", "west"})
  314. ->finishString()
  315. ->withRequiredNumber("distance")
  316. ->whichIsGreaterOrEqual(0.0)
  317. ->withDefault(1.0)
  318. ->finishNumber()
  319. ->withRequiredAttribute("filter",
  320. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>());
  321. }
  322. const char* PlaceableProofBlockFilterFactory::getTypeToken() const
  323. {
  324. return "blockFilter";
  325. }