EntityGenerator.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "EntityGenerator.h"
  2. #include "EntityType.h"
  3. #include "Game.h"
  4. #include "JNoise.h"
  5. #include "JsonExpression.h"
  6. EntityGenerator::EntityGenerator()
  7. : ReferenceCounter(),
  8. noise(0),
  9. noiseConfig(0),
  10. threshold(0.0),
  11. zType(0),
  12. condition(0)
  13. {}
  14. EntityGenerator::~EntityGenerator()
  15. {
  16. if (condition)
  17. {
  18. condition->release();
  19. }
  20. if (noise)
  21. {
  22. noise->release();
  23. }
  24. if (noiseConfig)
  25. {
  26. noiseConfig->release();
  27. }
  28. }
  29. void EntityGenerator::initialize(JExpressionMemory* zMemory)
  30. {
  31. if (noiseConfig)
  32. {
  33. if (noise) noise->release();
  34. noise = JNoise::parseNoise(noiseConfig, zMemory);
  35. }
  36. }
  37. bool EntityGenerator::isGenerated(
  38. int x, int y, int z, int dimensionId, JExpressionMemory* zMemory)
  39. {
  40. return (!noise
  41. || noise->getNoise((double)x, (double)y, (double)z) <= threshold)
  42. && condition->getValue(zMemory);
  43. }
  44. Entity* EntityGenerator::generate(Framework::Vec3<float> pos, int dimesnionId)
  45. {
  46. return zType->createEntityAt(pos, dimesnionId);
  47. }
  48. EntityGeneratorFactory::EntityGeneratorFactory()
  49. : TypeFactory()
  50. {}
  51. EntityGenerator* EntityGeneratorFactory::createValue(
  52. Framework::JSON::JSONObject* zJson) const
  53. {
  54. return new EntityGenerator();
  55. }
  56. void EntityGeneratorFactory::fromJson(
  57. EntityGenerator* zResult, Framework::JSON::JSONObject* zJson) const
  58. {
  59. if (zJson->hasValue("noise"))
  60. {
  61. zResult->noiseConfig = zJson->getValue("noise")->asObject();
  62. }
  63. if (zJson->hasValue("threshold"))
  64. {
  65. zResult->threshold
  66. = zJson->zValue("threshold")->asNumber()->getNumber();
  67. }
  68. if (zJson->hasValue("type"))
  69. {
  70. zResult->zType
  71. = Game::INSTANCE->zEntityType(Game::INSTANCE->getEntityTypeId(
  72. zJson->zValue("type")->asString()->getString()));
  73. }
  74. if (zJson->hasValue("condition"))
  75. {
  76. zResult->condition
  77. = Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  78. zJson->zValue("condition"));
  79. }
  80. }
  81. void EntityGeneratorFactory::toJson(
  82. EntityGenerator* zObject, Framework::JSON::JSONObject* zResult) const
  83. {
  84. if (zObject->noiseConfig)
  85. {
  86. zResult->addValue("noise",
  87. dynamic_cast<Framework::JSON::JSONValue*>(
  88. zObject->noiseConfig->getThis()));
  89. }
  90. zResult->addValue(
  91. "threshold", new Framework::JSON::JSONNumber(zObject->threshold));
  92. zResult->addValue(
  93. "type", new Framework::JSON::JSONString(zObject->zType->getName()));
  94. zResult->addValue("condition",
  95. Game::INSTANCE->zTypeRegistry()->toJson(zObject->condition));
  96. }
  97. JSONObjectValidationBuilder* EntityGeneratorFactory::addToValidator(
  98. JSONObjectValidationBuilder* builder) const
  99. {
  100. Framework::RCArray<Framework::Text> entityTypeNames;
  101. for (int i = 0; i < Game::INSTANCE->getEntityTypeCount(); i++)
  102. {
  103. if (Game::INSTANCE->zEntityType(i))
  104. {
  105. entityTypeNames.add(
  106. new Framework::Text(Game::INSTANCE->zEntityType(i)->getName()));
  107. }
  108. }
  109. return builder->withRequiredAttribute("noise", JNoise::getValidator(true))
  110. ->withRequiredNumber("threshold")
  111. ->whichIsOptional()
  112. ->whichIsGreaterOrEqual(0.0)
  113. ->whichIsLessOrEqual(1.0)
  114. ->finishNumber()
  115. ->withRequiredString("type")
  116. ->whichIsOneOf(entityTypeNames)
  117. ->finishString()
  118. ->withRequiredAttribute("condition",
  119. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>());
  120. }