EntityGenerator.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. : ObjectTypeFactory()
  50. {}
  51. EntityGenerator* EntityGeneratorFactory::fromJson(
  52. Framework::JSON::JSONObject* zJson) const
  53. {
  54. EntityGenerator* result = new EntityGenerator();
  55. if (zJson->hasValue("noise"))
  56. {
  57. result->noiseConfig = zJson->getValue("noise")->asObject();
  58. }
  59. if (zJson->hasValue("threshold"))
  60. {
  61. result->threshold = zJson->zValue("threshold")->asNumber()->getNumber();
  62. }
  63. if (zJson->hasValue("type"))
  64. {
  65. result->zType
  66. = Game::INSTANCE->zEntityType(Game::INSTANCE->getEntityTypeId(
  67. zJson->zValue("type")->asString()->getString()));
  68. }
  69. if (zJson->hasValue("condition"))
  70. {
  71. result->condition
  72. = Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  73. zJson->zValue("condition"));
  74. }
  75. return result;
  76. }
  77. Framework::JSON::JSONObject* EntityGeneratorFactory::toJsonObject(
  78. EntityGenerator* zObject) const
  79. {
  80. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  81. if (zObject->noiseConfig)
  82. {
  83. result->addValue("noise",
  84. dynamic_cast<Framework::JSON::JSONValue*>(
  85. zObject->noiseConfig->getThis()));
  86. }
  87. result->addValue(
  88. "threshold", new Framework::JSON::JSONNumber(zObject->threshold));
  89. result->addValue(
  90. "type", new Framework::JSON::JSONString(zObject->zType->getName()));
  91. result->addValue("condition",
  92. Game::INSTANCE->zTypeRegistry()->toJson(zObject->condition));
  93. return result;
  94. }
  95. JSONObjectValidationBuilder* EntityGeneratorFactory::addToValidator(
  96. JSONObjectValidationBuilder* builder) const
  97. {
  98. Framework::RCArray<Framework::Text> entityTypeNames;
  99. for (int i = 0; i < Game::INSTANCE->getEntityTypeCount(); i++)
  100. {
  101. if (Game::INSTANCE->zEntityType(i))
  102. {
  103. entityTypeNames.add(
  104. new Framework::Text(Game::INSTANCE->zEntityType(i)->getName()));
  105. }
  106. }
  107. // TODO: add EntityTypeNameFactory
  108. return builder->withRequiredAttribute("noise", JNoise::getValidator(true))
  109. ->withRequiredNumber("threshold")
  110. ->whichIsOptional()
  111. ->whichIsGreaterOrEqual(0.0)
  112. ->whichIsLessOrEqual(1.0)
  113. ->finishNumber()
  114. ->withRequiredString("type")
  115. ->whichIsOneOf(entityTypeNames)
  116. ->finishString()
  117. ->withRequiredAttribute("condition",
  118. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>());
  119. }