123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- #include "BlockType.h"
- #include "BasicBlocks.h"
- #include "Block.h"
- #include "Game.h"
- #include "ItemType.h"
- #include "MultiblockStructure.h"
- #include "Dimension.h"
- using namespace Framework;
- BlockType::BlockType()
- : ReferenceCounter(),
- id(0),
- model(0),
- initialMaxHP(0),
- needsClientInstance(true),
- lightSource(false),
- name(),
- needModelSubscription(false),
- initialMapColor(0),
- defaultBlock(0),
- groupNames(),
- hardness(1.f)
- {}
- BlockType::~BlockType()
- {
- if (model) model->release();
- if (defaultBlock) defaultBlock->release();
- }
- void BlockType::loadSuperBlock(
- Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
- {
- zBlock->loadInventory(zReader);
- zReader->lese((char*)&zBlock->transparent, 1);
- zReader->lese((char*)&zBlock->passable, 1);
- zReader->lese((char*)&zBlock->hp, 4);
- zReader->lese((char*)&zBlock->maxHP, 4);
- zReader->lese((char*)&zBlock->hardness, 4);
- zReader->lese((char*)&zBlock->speedModifier, 4);
- zReader->lese((char*)&zBlock->mapColor, 4);
- zReader->lese((char*)&zBlock->interactable, 1);
- int strCount;
- zReader->lese((char*)&strCount, 4);
- for (int i = 0; i < strCount; i++)
- {
- __int64 id;
- zReader->lese((char*)&id, 8);
- MultiblockStructure* str
- = Game::INSTANCE->zDimension(dimensionId)->zStructureById(id);
- if (str)
- {
- zBlock->structures.add(
- dynamic_cast<MultiblockStructure*>(str->getThis()));
- }
- }
- }
- void BlockType::saveSuperBlock(
- Block* zBlock, Framework::StreamWriter* zWriter) const
- {
- zBlock->saveInventory(zWriter);
- zWriter->schreibe((char*)&zBlock->transparent, 1);
- zWriter->schreibe((char*)&zBlock->passable, 1);
- zWriter->schreibe((char*)&zBlock->hp, 4);
- zWriter->schreibe((char*)&zBlock->maxHP, 4);
- zWriter->schreibe((char*)&zBlock->hardness, 4);
- zWriter->schreibe((char*)&zBlock->speedModifier, 4);
- zWriter->schreibe((char*)&zBlock->mapColor, 4);
- zWriter->schreibe((char*)&zBlock->interactable, 1);
- int strCount = zBlock->structures.getEintragAnzahl();
- zWriter->schreibe((char*)&strCount, 4);
- for (MultiblockStructure* structure : zBlock->structures)
- {
- __int64 id = structure->getStructureId();
- zWriter->schreibe((char*)&id, 8);
- }
- }
- void BlockType::createSuperBlock(Block* zBlock, Item* zItem) const
- {
- if (zItem)
- {
- BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
- if (item)
- {
- zBlock->transparent = item->transparent;
- zBlock->passable = item->passable;
- zBlock->hardness = item->hardness;
- zBlock->speedModifier = item->speedModifier;
- zBlock->interactable = item->interactable;
- }
- }
- zBlock->mapColor = initialMapColor;
- }
- void BlockType::createSuperItem(Block* zBlock, Item* zItem) const
- {
- BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
- if (item)
- {
- item->transparent = zBlock->transparent;
- item->passable = zBlock->passable;
- item->hardness = zBlock->hardness;
- item->speedModifier = zBlock->speedModifier;
- item->interactable = zBlock->interactable;
- }
- }
- bool BlockType::initialize(Game* zGame)
- {
- return true;
- }
- BlockType* BlockType::initializeDefault()
- {
- if (!defaultBlock)
- {
- defaultBlock = createBlockAt({0, 0, 0}, 0, 0);
- }
- return this;
- }
- const Block* BlockType::zDefault() const
- {
- return defaultBlock;
- }
- void BlockType::writeTypeInfo(StreamWriter* zWriter) const
- {
- int id = getId();
- zWriter->schreibe((char*)&id, 4);
- bool inst = doesNeedClientInstance();
- zWriter->schreibe((char*)&inst, 1);
- bool sub = doesNeedModelSubscription();
- zWriter->schreibe((char*)&sub, 1);
- bool fluid = isFluid();
- zWriter->schreibe((char*)&fluid, 1);
- if (fluid)
- {
- char flowDist = getFlowDistance();
- zWriter->schreibe(&flowDist, 1);
- }
- int maxHp = getInitialMaxHP();
- zWriter->schreibe((char*)&maxHp, 4);
- zModel()->writeTo(zWriter);
- }
- Framework::Text BlockType::getTargetUIML() const
- {
- return Text("<targetInfo><text width=\"auto\" height=\"auto\">") + name
- + "</text></targetInfo>";
- }
- Block* BlockType::loadBlock(Framework::Vec3<int> position,
- Framework::StreamReader* zReader,
- int dimensionId) const
- {
- Block* result = createBlock(position, dimensionId);
- loadSuperBlock(result, zReader, dimensionId);
- return result;
- }
- void BlockType::saveBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
- {
- saveSuperBlock(zBlock, zWriter);
- }
- Item* BlockType::getItemFromBlock(Block* zBlock) const
- {
- Item* result = createItem();
- if (result)
- {
- createSuperItem(zBlock, result);
- }
- return result;
- }
- Block* BlockType::createBlockAt(
- Framework::Vec3<int> position, int dimensionId, Item* zUsedItem) const
- {
- Block* result = createBlock(position, dimensionId);
- createSuperBlock(result, zUsedItem);
- return result;
- }
- int BlockType::getId() const
- {
- return id;
- }
- bool BlockType::isFluid() const
- {
- return false;
- }
- unsigned char BlockType::getFlowDistance() const
- {
- return 0;
- }
- void BlockType::setTypeId(int id)
- {
- this->id = id;
- }
- void BlockType::setModel(ModelInfo* model)
- {
- this->model = model;
- }
- ModelInfo* BlockType::zModel() const
- {
- return model;
- }
- void BlockType::setInitialMaxHP(int initialMaxHP)
- {
- this->initialMaxHP = initialMaxHP;
- }
- int BlockType::getInitialMaxHP() const
- {
- return initialMaxHP;
- }
- void BlockType::setNeedsClientInstance(bool needsClientInstance)
- {
- this->needsClientInstance = needsClientInstance;
- }
- bool BlockType::doesNeedClientInstance() const
- {
- return needsClientInstance;
- }
- void BlockType::setLightSource(bool lightSource)
- {
- this->lightSource = lightSource;
- }
- bool BlockType::isLightSource() const
- {
- return lightSource;
- }
- void BlockType::setName(Framework::Text name)
- {
- this->name = name;
- }
- const char* BlockType::getName() const
- {
- return name;
- }
- void BlockType::setNeedModelSubscription(bool needModelSubscription)
- {
- this->needModelSubscription = needModelSubscription;
- }
- const bool BlockType::doesNeedModelSubscription() const
- {
- return needModelSubscription;
- }
- void BlockType::setMapColor(int mapColor)
- {
- this->initialMapColor = mapColor;
- }
- int BlockType::getMapColor() const
- {
- return initialMapColor;
- }
- void BlockType::setGroupNames(Framework::RCArray<Framework::Text> groupNames)
- {
- this->groupNames = groupNames;
- }
- const Framework::RCArray<Framework::Text>& BlockType::getGroupNames() const
- {
- return groupNames;
- }
- void BlockType::setHardness(float hardness)
- {
- this->hardness = hardness;
- }
- float BlockType::getHardness() const
- {
- return hardness;
- }
- int BlockType::getTypeId(const char* name)
- {
- Text n = name;
- for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
- {
- if (Game::INSTANCE->zBlockType(i)
- && n.istGleich(Game::INSTANCE->zBlockType(i)->getName()))
- return Game::INSTANCE->zBlockType(i)->getId();
- }
- return 0;
- }
- Framework::Text BlockType::getTypeName(int id)
- {
- for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
- {
- if (Game::INSTANCE->zBlockType(i)
- && Game::INSTANCE->zBlockType(i)->getId() == id)
- return Game::INSTANCE->zBlockType(i)->getName();
- }
- return 0;
- }
- const Block* getDefaultBlock(Either<Block*, int> b)
- {
- if (b.isA())
- return b;
- else
- return Game::INSTANCE->zBlockType(b)->zDefault();
- }
|