PlaceableProof.cpp 9.3 KB

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