void FreeSpaceListing::claimSpace( FreeSpaceList::iterator& position, int length) { // Throw if trying to claim more space than exists if (length > position->length()) { throw TGenericException(TALES_SRCANDLINE, "FreeSpaceListing::claimSpace(" "FreeSpaceList::iterator,int", std::string("Tried to claim ") + StringConversion::toString(length) + std::string(" bytes at position ") + StringConversion::toString(position->address()) + std::string(" when only ") + StringConversion::toString(position->length()) + std::string(" were available")); } // Increase address position->setAddress(position->address() + length); // Decrease length position->setLength(position->length() - length); // If all space at this address is claimed, remove it from the list if (position->length() == 0) { freeSpaceList_.erase(position); } }
void MetatileBehavior::setSolidity( MetatileSolidities::MetatileSolidity solidity) { switch (solidity) { case MetatileSolidities::nonsolid: fullySolid_ = false; solidOnTop_ = false; break; case MetatileSolidities::solidOnTop: fullySolid_ = false; solidOnTop_ = true; break; case MetatileSolidities::fullySolid: fullySolid_ = true; solidOnTop_ = false; break; default: throw TGenericException(TALES_SRCANDLINE, "MetatileBehavior::changeSolidity(" "MetatileSolidities::MetatileSolidity)", std::string("Invalid solidity type: ") + StringConversion::toString( static_cast<int>(solidity))); break; } }
const MetatileStructureSet& MetatileStructures::metatileStructureSet(int index) const { // Throw if index out of range if ((unsigned int)index >= metatileStructureSets_.size()) { throw TGenericException(TALES_SRCANDLINE, "MetatileStructures::metatileStructureSet" "(int)", std::string("Out of range index: ") + StringConversion::toString(index)); } return metatileStructureSets_[index]; }
void MetatileStructures::exportToROM(WritableROM& rom) { // Write each structure table for (AddressMetatileStructureIndexMap::iterator it = indexMap_.begin(); it != indexMap_.end(); it++) { // Throw if index out of range if ((unsigned int)(it->second) >= metatileStructureSets_.size()) { throw TGenericException(TALES_SRCANDLINE, "MetatileStructures::exportToROM" "(WritableROM&)", std::string("Out of range index: ") + StringConversion::toString(it->second)); } // Get structure table at index MetatileStructureSet& metatiles = metatileStructureSets_[it->second]; metatiles.exportToROM(rom); // Write each metatile structure /* int metatileNum = 0; for (MetatileStructureSet::iterator jt = metatiles.begin(); jt != metatiles.end(); jt++) { // Get base address of table content Taddress address = it->first + (metatiles.size() * ByteSizes::uint16Size); // Add data position of metatile to address address += (metatileNum * MetatileStructure::dataSize); // Create buffer to hold data Tbyte output[MetatileStructure::dataSize]; // Write to buffer jt->writeToData(output); // Write to ROM rom.directWrite(address, output, MetatileStructure::dataSize); // Move to next metatile ++metatileNum; } std::cout << metatileNum << std::endl; */ } }
MetatileStructure& MetatileStructureSet ::metatileMapping(int indexNum) { // Throw if out of range if ((unsigned int)(indexNum) >= index_.size()) { throw TGenericException(TALES_SRCANDLINE, "MetatileStructureSet::metatileMapping(" "int)", std::string("Out of range index: ") + StringConversion::toString(indexNum)); } MetatileIndexToStructureMap::iterator it = index_.findOrThrow(indexNum); return primaryStorage_[it->second]; }
void EditableLevelObjectEntryGroups::addEntry(int groupIndex, const LevelObjectEntry& entry) { // Throw if index out of range if ((unsigned int)groupIndex >= primaryStorage_.size()) { throw OutOfRangeIndexException(TALES_SRCANDLINE, "EditableLevelObjectEntryGroups" "::addEntry(int, const LevelObjectEntry&)", groupIndex); } // Throw if max number of objects reached if (totalEntries() >= maxTotalEntries()) { throw TGenericException(TALES_SRCANDLINE, "EditableLevelObjectEntryGroups" "::addEntry(int, const LevelObjectEntry&)", "Max object limit exceeded"); } primaryStorage_[groupIndex].addEntry(entry); }
void WritableROM::expandROM(unsigned int newSize) { // Throw if new size is smaller than existing if (newSize < romSize_) { throw TGenericException(TALES_SRCANDLINE, "WritableROM::expandROM(unsigned int)", std::string("Tried to expand ROM to a smaller" "size: ") + StringConversion::toString(romSize_) + " bytes to " + StringConversion::toString(newSize) + " bytes"); } // Create array for newly-sized data Tbyte* newrom = new Tbyte[newSize]; // Fill any unused part of new ROM with 0xFF if (newSize > romSize_) { std::memset(newrom + romSize_, 0xFF, newSize - romSize_); } // Copy as much of existing ROM data as new size allows std::memcpy(newrom, rom_, (romSize_ < newSize) ? romSize_ : newSize); // Delete existing ROM data delete rom_; // Add free space as needed // Free space must fall within bank boundaries for (int i = romSize_; i < newSize; i += LoadedROM::bankSize) { freeSpace_.giveSpace(i, LoadedROM::bankSize); } // Replace existing data with new rom_ = newrom; romSize_ = newSize; }
void MetatileBehavior::readFromData(const Tbyte* src) { // Get raw behavior byte Tbyte rawBehavior = src[0]; // Check bitmasked behaviors if (rawBehavior & fullySolidMask) { fullySolid_ = true; } if (rawBehavior & solidOnTopMask) { solidOnTop_ = true; } if (rawBehavior & unknownSettingMask) { unknownSetting_ = true; } // Get tile type int rawType = (rawBehavior & metatileTypeMask); // Throw if type is out of valid range if (rawType >= MetatileTypes::numMetatileTypes) { throw TGenericException(TALES_SRCANDLINE, "MetatileBehavior::readFromData(const Tbyte*)", std::string("Invalid tile type: ") + StringConversion::toString(rawType)); } // Cast to type metatileType_ = static_cast<MetatileTypes::MetatileType>(rawType); // Get slope speed index (converting from raw index; each table entry is // a 16-bit number, and the game indexes into it using this value as-is, so // we divide by 2 to get the actual index number) slopeSpeedIndex_ = (src[1]) / 2; // Get raw height map address int heightMapAddr = ByteConversion::fromBytes(src + 2, ByteSizes::uint16Size, EndiannessTypes::little, SignednessTypes::nosign); // Convert to index by dividing by size of each map heightMapIndex_ = LoadedROM::toIndex(TailsAdvAddresses::HeightMapTable, heightMapAddr, MetatileHeightMap::dataSize); // Get raw width map address int widthMapAddr = ByteConversion::fromBytes(src + 4, ByteSizes::uint16Size, EndiannessTypes::little, SignednessTypes::nosign); // Convert to index by dividing by size of each map widthMapIndex_ = LoadedROM::toIndex(TailsAdvAddresses::WidthMapTable, widthMapAddr, MetatileWidthMap::dataSize); /* // Convert width map table start address to banked form int widthMapTableBankedAddr = LoadedROM::directToBankedAddress(TailsAdvAddresses::WidthMapTable); // Get the position in the table (in bytes) int widthPos = widthMapAddr - widthMapTableBankedAddr; // Convert to index by dividing by size of each map // TODO: use named constant in WidthMap or whatever instead widthMapIndex_ = widthPos / 0x10; */ // Get terminator terminator_ = src[6]; }