123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- #include "BasicBlocks.h"
- #include "AddEntityUpdate.h"
- #include "Game.h"
- #include "ItemEntity.h"
- #include "ModelInfo.h"
- #include "TreeSeblingBlock.h"
- BasicBlock::BasicBlock(int typeId, Framework::Vec3<int> pos, int dimensionId)
- : BasicBlock(typeId, pos, dimensionId, false)
- {}
- BasicBlock::BasicBlock(
- int typeId, Framework::Vec3<int> pos, int dimensionId, bool hasInventory)
- : Block(typeId, pos, dimensionId, hasInventory)
- {}
- bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
- {
- return 0;
- }
- void BasicBlock::onPostTick() {}
- BasicBlockType::BasicBlockType(Framework::Text itemTypeName,
- ModelInfo* model,
- Framework::Text name,
- int mapColor,
- bool modelSubscription,
- float hardness,
- Framework::RCArray<Framework::Text> groupNames)
- : BlockType(0,
- model,
- 1,
- 100,
- 0,
- name,
- modelSubscription,
- mapColor,
- groupNames,
- hardness),
- itemTypeName(itemTypeName),
- transparent(0),
- passable(0),
- speedModifier(1.f),
- interactable(1)
- {}
- bool BasicBlockType::initialize(Game* zGame)
- {
- if (itemTypeName.getLength())
- {
- itemTypeId = zGame->getItemTypeId(itemTypeName);
- }
- else
- {
- itemTypeId = 0;
- }
- return itemTypeId >= 0 && BlockType::initialize(zGame);
- }
- void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
- {
- BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
- block->transparent = transparent;
- block->passable = passable;
- block->hp = (float)getInitialMaxHP();
- block->maxHP = (float)getInitialMaxHP();
- block->hardness = getHardness();
- block->speedModifier = speedModifier;
- block->interactable = interactable;
- BlockType::createSuperBlock(zBlock, zItem);
- }
- Block* BasicBlockType::createBlock(
- Framework::Vec3<int> position, int dimensionId) const
- {
- return new BasicBlock(getId(), position, dimensionId);
- }
- Item* BasicBlockType::createItem() const
- {
- return Game::INSTANCE->zItemType(itemTypeId)->createItem();
- }
- Framework::Text BasicBlockType::getItemTypeName() const
- {
- return itemTypeName;
- }
- ItemType* BasicBlockType::createItemType() const
- {
- return new BasicBlockItemType(getItemTypeName(),
- new ModelInfo(zModel()->getModelPath(), zModel()->getTexturePaths(), zModel()->isTransparent(), zModel()->getSize() / 2.f),
- transparent,
- passable,
- getHardness(),
- speedModifier,
- getItemTypeName(),
- 0, 50, getGroupNames());
- }
- int BasicBlockType::getItemTypeId() const
- {
- return itemTypeId;
- }
- void BasicBlockType::setTransparent(bool transparent) {
- this->transparent = transparent;
- }
- bool BasicBlockType::isTransparent() const
- {
- return transparent;
- }
- void BasicBlockType::setPassable(bool passable) {
- this->passable = passable;
- }
- bool BasicBlockType::isPassable() const
- {
- return passable;
- }
- void BasicBlockType::setSpeedModifier(float speedModifier) {
- this->speedModifier = speedModifier;
- }
- float BasicBlockType::getSpeedModifier() const
- {
- return speedModifier;
- }
- void BasicBlockType::setInteractable(bool interactable) {
- this->interactable = interactable;
- }
- BasicBlockTypeFactory::BasicBlockTypeFactory()
- : SubTypeFactory()
- {}
- BasicBlockType* BasicBlockTypeFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<Framework::Text> groupNames;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("groupNames")->asArray())
- {
- groupNames.add(new Framework::Text(value->asString()->getString()));
- }
- return new BasicBlockType(
- zJson->zValue("itemType")->asString()->getString(),
- Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- zJson->zValue("model")),
- zJson->zValue("name")->asString()->getString(),
- (int)zJson->zValue("mapColor")->asString()->getString(),
- zJson->zValue("modelSubscription")->asBool()->getBool(),
- (float)zJson->zValue("hardness")->asNumber()->getNumber(),
- groupNames);
- }
- Framework::JSON::JSONObject* BasicBlockTypeFactory::toJson(
- BasicBlockType* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("itemType",
- new Framework::JSON::JSONString(zObject->getItemTypeName()));
- result->addValue(
- "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
- result->addValue(
- "name", new Framework::JSON::JSONString(zObject->getName()));
- result->addValue(
- "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
- result->addValue("modelSubscription",
- new Framework::JSON::JSONBool(zObject->doesNeedModelSubscription()));
- result->addValue(
- "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
- Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
- for (Framework::Text* groupName : zObject->getGroupNames())
- {
- groupNames->addValue(new Framework::JSON::JSONString(*groupName));
- }
- result->addValue("groupNames", groupNames);
- return result;
- }
- Framework::JSON::Validator::JSONValidator* BasicBlockTypeFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredString("itemType")
- ->finishString()
- ->withRequiredAttribute(
- "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
- ->withRequiredString("name")
- ->finishString()
- ->withRequiredString("mapColor")
- ->finishString()
- ->withRequiredBool("modelSubscription")
- ->withDefault(false)
- ->finishBool()
- ->withRequiredNumber("hardness")
- ->withDefault(1.0)
- ->finishNumber()
- ->withRequiredArray("groupNames")
- ->withDefault(new Framework::JSON::JSONArray())
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray()
- ->finishObject();
- }
- Framework::Text BasicBlockTypeFactory::getTypeToken() const
- {
- return "basicBlock";
- }
- AdditionalItemSpawningBlock::AdditionalItemSpawningBlock(
- int typeId, Framework::Vec3<int> pos, int dimensionId)
- : BasicBlock(typeId, pos, dimensionId)
- {}
- void AdditionalItemSpawningBlock::addSpawn(SpawnConfig config)
- {
- spawns.add(config);
- }
- void AdditionalItemSpawningBlock::onDestroy()
- {
- for (const SpawnConfig& config : spawns)
- {
- if ((double)rand() / RAND_MAX < config.chance)
- {
- int amount = config.min
- + (int)((config.max - config.min)
- * ((double)rand() / RAND_MAX));
- if (amount > 0)
- {
- ItemStack* spawnedItems
- = Game::INSTANCE->zItemType(config.typeId)
- ->createItemStack(amount);
- if (spawnedItems)
- {
- ItemEntity* itemEntity
- = (ItemEntity*)Game::INSTANCE
- ->zEntityType(EntityTypeEnum::ITEM)
- ->createEntity(location
- + Framework::Vec3<float>(
- 0.5f, 0.5f, 0.5f),
- getDimensionId(),
- Game::INSTANCE->getNextEntityId());
- itemEntity->unsaveAddItem(spawnedItems, NO_DIRECTION, 0);
- spawnedItems->release();
- Game::INSTANCE->requestWorldUpdate(
- new AddEntityUpdate(itemEntity, getDimensionId()));
- }
- }
- }
- }
- BasicBlock::onDestroy();
- }
- void AdditionalItemSpawningBlockType::createSuperBlock(
- Block* zBlock, Item* zItem) const
- {
- AdditionalItemSpawningBlock* block
- = dynamic_cast<AdditionalItemSpawningBlock*>(zBlock);
- if (block)
- {
- for (const SpawnConfig& config : spawns)
- {
- block->addSpawn(config);
- }
- }
- BasicBlockType::createSuperBlock(zBlock, zItem);
- }
- AdditionalItemSpawningBlockType::AdditionalItemSpawningBlockType(
- Framework::Text itemTypeName,
- ModelInfo* model,
- Framework::Text name,
- int mapColor,
- bool modelSubscription,
- float hardness,
- Framework::Array<SpawnConfig> spawns,
- Framework::RCArray<Framework::Text> groupNames)
- : BasicBlockType(itemTypeName,
- model,
- name,
- mapColor,
- modelSubscription,
- hardness,
- groupNames),
- spawns(spawns)
- {}
- bool AdditionalItemSpawningBlockType::initialize(Game* zGame)
- {
- for (auto iterator = spawns.begin(); iterator; iterator++)
- {
- int itemTypeId
- = Game::INSTANCE->getItemTypeId(iterator.val().itemTypeName);
- if (itemTypeId < 0)
- {
- return false;
- }
- iterator.set({iterator.val().min,
- iterator.val().max,
- iterator.val().chance,
- iterator.val().itemTypeName,
- itemTypeId});
- }
- return BasicBlockType::initialize(zGame);
- }
- Block* AdditionalItemSpawningBlockType::createBlock(
- Framework::Vec3<int> position, int dimensionId) const
- {
- AdditionalItemSpawningBlock* block
- = new AdditionalItemSpawningBlock(getId(), position, dimensionId);
- return block;
- }
- const Framework::Array<SpawnConfig>&
- AdditionalItemSpawningBlockType::getSpawns() const
- {
- return spawns;
- }
- AdditionalItemSpawningBlockTypeFactory::AdditionalItemSpawningBlockTypeFactory()
- : SubTypeFactory()
- {}
- AdditionalItemSpawningBlockType*
- AdditionalItemSpawningBlockTypeFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<Framework::Text> groupNames;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("groupNames")->asArray())
- {
- groupNames.add(new Framework::Text(value->asString()->getString()));
- }
- Framework::Array<SpawnConfig> spawns;
- Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray();
- for (int i = 0; i < spawnsJson->getLength(); i++)
- {
- Framework::JSON::JSONObject* spawnJson
- = spawnsJson->zValue(i)->asObject();
- spawns.add(SpawnConfig{
- (int)spawnJson->zValue("min")->asNumber()->getNumber(),
- (int)spawnJson->zValue("max")->asNumber()->getNumber(),
- (float)spawnJson->zValue("chance")->asNumber()->getNumber(),
- spawnJson->zValue("itemType")->asString()->getString(),
- 0,
- });
- }
- return new AdditionalItemSpawningBlockType(
- zJson->zValue("itemType")->asString()->getString(),
- Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- zJson->zValue("model")),
- zJson->zValue("name")->asString()->getString(),
- (int)zJson->zValue("mapColor")->asString()->getString(),
- zJson->zValue("modelSubscription")->asBool()->getBool(),
- (float)zJson->zValue("hardness")->asNumber()->getNumber(),
- spawns,
- groupNames);
- }
- Framework::JSON::JSONObject* AdditionalItemSpawningBlockTypeFactory::toJson(
- AdditionalItemSpawningBlockType* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("itemType",
- new Framework::JSON::JSONString(zObject->getItemTypeName()));
- result->addValue(
- "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
- result->addValue(
- "name", new Framework::JSON::JSONString(zObject->getName()));
- result->addValue(
- "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
- result->addValue("modelSubscription",
- new Framework::JSON::JSONBool(zObject->doesNeedModelSubscription()));
- result->addValue(
- "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
- Framework::JSON::JSONArray* spawns = new Framework::JSON::JSONArray();
- for (const SpawnConfig& config : zObject->getSpawns())
- {
- Framework::JSON::JSONObject* spawn = new Framework::JSON::JSONObject();
- spawn->addValue(
- "itemType", new Framework::JSON::JSONString(config.itemTypeName));
- spawn->addValue(
- "chance", new Framework::JSON::JSONNumber(config.chance));
- spawn->addValue("min", new Framework::JSON::JSONNumber(config.min));
- spawn->addValue("max", new Framework::JSON::JSONNumber(config.max));
- spawns->addValue(spawn);
- }
- result->addValue("spawns", spawns);
- Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
- for (Framework::Text* groupName : zObject->getGroupNames())
- {
- groupNames->addValue(new Framework::JSON::JSONString(*groupName));
- }
- result->addValue("groupNames", groupNames);
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- AdditionalItemSpawningBlockTypeFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredString("itemType")
- ->finishString()
- ->withRequiredAttribute(
- "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
- ->withRequiredString("name")
- ->finishString()
- ->withRequiredString("mapColor")
- ->finishString()
- ->withRequiredBool("modelSubscription")
- ->withDefault(false)
- ->finishBool()
- ->withRequiredNumber("hardness")
- ->withDefault(1.0)
- ->finishNumber()
- ->withRequiredAttribute("spawns",
- JSON::Validator::JSONValidator::buildForArray()
- ->addAcceptedObjectInArray()
- ->withRequiredString("itemType")
- ->finishString()
- ->withRequiredNumber("chance")
- ->finishNumber()
- ->withRequiredNumber("min")
- ->finishNumber()
- ->withRequiredNumber("max")
- ->finishNumber()
- ->finishObject()
- ->finishArray())
- ->withRequiredArray("groupNames")
- ->withDefault(new Framework::JSON::JSONArray())
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray()
- ->finishObject();
- }
- Framework::Text AdditionalItemSpawningBlockTypeFactory::getTypeToken() const
- {
- return "additionalItemsBlockType";
- }
|