#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(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(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(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("") + name + ""; } Block* BlockType::loadBlock(Framework::Vec3 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 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 groupNames) { this->groupNames = groupNames; } const Framework::RCArray& 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 b) { if (b.isA()) return b; else return Game::INSTANCE->zBlockType(b)->zDefault(); }