123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #include "TreeTemplate.h"
- #include "Dimension.h"
- #include "Game.h"
- #include "MultiblockTree.h"
- TreeTemplate::TreeTemplate()
- : GeneratorTemplate(),
- woodBlockTypeId(0),
- leaveBlockType(0),
- minHeight(0),
- maxHeight(0)
- {
- setMinPosOffset(Framework::Vec3<int>(-2, -2, 0));
- }
- 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;
- }
- void TreeTemplate::setWoodTypeId(int id)
- {
- woodBlockTypeId = id;
- }
- void TreeTemplate::setLeavesTypeId(int id)
- {
- leaveBlockType = id;
- }
- const BlockType* TreeTemplate::zWoodType() const
- {
- return Game::INSTANCE->zBlockType(woodBlockTypeId);
- }
- const BlockType* TreeTemplate::zLeavesType() const
- {
- return Game::INSTANCE->zBlockType(leaveBlockType);
- }
- void TreeTemplate::setMinHeight(int height)
- {
- minHeight = height;
- }
- void TreeTemplate::setMaxHeight(int height)
- {
- maxHeight = height;
- Framework::Vec3<int>(5, 5, maxHeight);
- }
- int TreeTemplate::getMinHeight() const
- {
- return minHeight;
- }
- int TreeTemplate::getMaxHeight() const
- {
- return maxHeight;
- }
- TreeTemplateFactory::TreeTemplateFactory()
- : GeneratorTemplateFactory()
- {}
- TreeTemplate* TreeTemplateFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new TreeTemplate();
- }
- void TreeTemplateFactory::fromJson(
- TreeTemplate* zResult, Framework::JSON::JSONObject* zConfig) const
- {
- zResult->setWoodTypeId(BlockType::getTypeId(
- zConfig->asObject()->zValue("wood")->asString()->getString()));
- zResult->setLeavesTypeId(BlockType::getTypeId(
- zConfig->asObject()->zValue("leaves")->asString()->getString()));
- zResult->setMinHeight(
- (int)(zConfig->asObject()->zValue("minSize")->asNumber()->getNumber()));
- zResult->setMinHeight(
- (int)(zConfig->asObject()->zValue("maxSize")->asNumber()->getNumber()));
- GeneratorTemplateFactory::fromJson(zResult, zConfig);
- }
- void TreeTemplateFactory::toJson(
- TreeTemplate* zObject, Framework::JSON::JSONObject* zResult) const
- {
- zResult->addValue("propability",
- new Framework::JSON::JSONNumber((double)zObject->getPropability()));
- zResult->addValue("wood",
- new Framework::JSON::JSONString(zObject->zWoodType()->getName()));
- zResult->addValue("leaves",
- new Framework::JSON::JSONString(zObject->zLeavesType()->getName()));
- zResult->addValue("minSize",
- new Framework::JSON::JSONNumber((double)zObject->getMinHeight()));
- zResult->addValue("maxSize",
- new Framework::JSON::JSONNumber((double)zObject->getMaxHeight()));
- }
- JSONObjectValidationBuilder* TreeTemplateFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- Framework::RCArray<Framework::Text> blockTypeNames;
- for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
- {
- if (Game::INSTANCE->zBlockType(i))
- {
- blockTypeNames.add(
- new Framework::Text(Game::INSTANCE->zBlockType(i)->getName()));
- }
- }
- return GeneratorTemplateFactory::addToValidator(
- builder->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());
- }
- Framework::Text TreeTemplateFactory::getTypeToken() const
- {
- return "Tree";
- }
|