123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #include "TreeTemplate.h"
- #include "Game.h"
- #include "MultiblockTree.h"
- TreeTemplate::TreeTemplate(float propability,
- int woodBlockTypeId,
- int leaveBlockType,
- int minHeight,
- int maxHeight)
- : GeneratorTemplate(propability,
- Framework::Vec3<int>(-2, -2, 0),
- Framework::Vec3<int>(5, 5, maxHeight)),
- woodBlockTypeId(woodBlockTypeId),
- leaveBlockType(leaveBlockType),
- minHeight(minHeight),
- maxHeight(maxHeight)
- {}
- GeneratedStructure* TreeTemplate::generateAt(
- Framework::Vec3<int> location, Noise* zNoise, int dimensionId)
- {
- double noise = zNoise->getNoise((double)location.x + 40,
- (double)location.y + 70,
- (double)location.z - 20);
- int height = (int)(minHeight + (noise * (maxHeight - minHeight)));
- Dimension* zDim = Game::INSTANCE->zDimension(dimensionId);
- MultiblockStructure* treeStructure = zDim->zStructureByPosition(location);
- if (!treeStructure)
- {
- treeStructure = new MultiblockTree(
- dimensionId, zDim->getNextStructureId(), location);
- zDim->addStructure(treeStructure);
- }
- Framework::Vec3<int> minAffected
- = Framework::Vec3<int>(-2, -2, 0) + location;
- GeneratedStructure* generated
- = new GeneratedStructure(dynamic_cast<GeneratorTemplate*>(getThis()),
- location,
- Framework::Vec3<int>(5, 5, height),
- minAffected);
- for (int x = 1; x < 4; x++)
- {
- for (int y = 1; y < 4; y++)
- {
- generated->setBlockAt(
- leaveBlockType, Framework::Vec3<int>(x, y, height - 1));
- generated->setBlockAt(
- leaveBlockType, Framework::Vec3<int>(x, y, height - 5));
- }
- }
- for (int z = height - 2; z >= height - 4; z--)
- {
- for (int x = 1; x < 4; x++)
- {
- generated->setBlockAt(
- leaveBlockType, Framework::Vec3<int>(x, 0, z));
- generated->setBlockAt(
- leaveBlockType, Framework::Vec3<int>(0, x, z));
- generated->setBlockAt(
- leaveBlockType, Framework::Vec3<int>(x, 4, z));
- generated->setBlockAt(
- leaveBlockType, Framework::Vec3<int>(4, x, z));
- for (int y = 1; y < 4; y++)
- generated->setBlockAt(
- leaveBlockType, Framework::Vec3<int>(x, y, z));
- }
- }
- for (int i = 0; i < height - 1; i++)
- generated->setBlockAt(woodBlockTypeId, Framework::Vec3<int>(2, 2, i));
- generated->addAllBlocksToStructure(
- dynamic_cast<MultiblockStructure*>(treeStructure->getThis()));
- return generated;
- }
- const BlockType* TreeTemplate::getWoodType() const
- {
- return StaticRegistry<BlockType>::INSTANCE.zElement(woodBlockTypeId);
- }
- const BlockType* TreeTemplate::getLeavesType() const
- {
- return StaticRegistry<BlockType>::INSTANCE.zElement(leaveBlockType);
- }
- TreeTemplateFactory::TreeTemplateFactory()
- : SubTypeFactory<GeneratorTemplate, TreeTemplate>()
- {}
- TreeTemplate* TreeTemplateFactory::fromJson(
- Framework::JSON::JSONObject* zConfig) const
- {
- return new TreeTemplate((float)zConfig->asObject()
- ->zValue("propability")
- ->asNumber()
- ->getNumber(),
- BlockType::getTypeId(
- zConfig->asObject()->zValue("wood")->asString()->getString()),
- BlockType::getTypeId(
- zConfig->asObject()->zValue("leaves")->asString()->getString()),
- (int)(round(
- zConfig->asObject()->zValue("minSize")->asNumber()->getNumber())),
- (int)(round(
- zConfig->asObject()->zValue("maxSize")->asNumber()->getNumber())));
- }
- Framework::JSON::JSONObject* TreeTemplateFactory::toJson(
- TreeTemplate* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue(
- "propability", new Framework::JSON::JSONNumber((double)zObject->getPropability()));
- result->addValue("wood", new Framework::JSON::JSONString(BlockType::getTypeName(zObject->woodBlockTypeId)));
- result->addValue("leaves",
- new Framework::JSON::JSONString(
- BlockType::getTypeName(zObject->leaveBlockType)));
- result->addValue(
- "minSize", new Framework::JSON::JSONNumber((double)zObject->minHeight));
- result->addValue(
- "maxSize", new Framework::JSON::JSONNumber((double)zObject->maxHeight));
- return result;
- }
- Framework::JSON::Validator::JSONValidator* TreeTemplateFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- Framework::RCArray<Framework::Text> blockTypeNames;
- for (int i = 0; i < StaticRegistry<BlockType>::INSTANCE.getCount(); i++)
- {
- if (StaticRegistry<BlockType>::INSTANCE.zElement(i))
- {
- blockTypeNames.add(new Framework::Text(
- StaticRegistry<BlockType>::INSTANCE.zElement(i)->getName()));
- }
- }
- return builder
- ->withRequiredString("type")
- ->withExactMatch("Tree")
- ->finishString()
- ->withRequiredString("wood")
- ->whichIsOneOf(blockTypeNames)
- ->finishString()
- ->withRequiredString("leaves")
- ->whichIsOneOf(blockTypeNames)
- ->finishString()
- ->withRequiredNumber("minSize")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredNumber("maxSize")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredNumber("propability")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->finishObject();
- }
- Framework::Text TreeTemplateFactory::getTypeToken() const
- {
- return "Tree";
- }
|