- Added initialization of startup processes in flex_archive_tests to ensure proper setup before teleporter operations. - Updated assertions to verify the correct allocation and state of processes during teleporter execution. - Introduced new tests in map_renderer_tests to validate shape rendering, including isolated shape rendering and player sprite integration. - Enhanced map_sorter_tests with additional checks for occlusion handling and boundary conditions in render order resolution.
953 lines
34 KiB
C++
953 lines
34 KiB
C++
#include "remorse/fixed_map.h"
|
|
#include "remorse/glob_archive.h"
|
|
|
|
#include "remorse/entity_dispatch_entry.h"
|
|
#include "remorse/file_system.h"
|
|
#include "remorse/map_renderer.h"
|
|
#include "remorse/type_flags.h"
|
|
|
|
#include <algorithm>
|
|
#include <stdexcept>
|
|
#include <utility>
|
|
|
|
namespace remorse {
|
|
namespace {
|
|
|
|
constexpr u32 fixedMapCountOffset = 0x54;
|
|
constexpr u32 fixedMapTableOffset = 0x80;
|
|
constexpr u32 fixedMapTableRowSize = 8;
|
|
constexpr u32 fixedMapRecordSize = 16;
|
|
constexpr u16 crusaderCoordinateScale = 2;
|
|
|
|
std::optional<u16> readU16Le(const std::vector<u8>& bytes, u32 offset)
|
|
{
|
|
if (offset + 2 > bytes.size()) {
|
|
return std::nullopt;
|
|
}
|
|
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
|
}
|
|
|
|
std::optional<u32> readU32Le(const std::vector<u8>& bytes, u32 offset)
|
|
{
|
|
if (offset + 4 > bytes.size()) {
|
|
return std::nullopt;
|
|
}
|
|
return static_cast<u32>(bytes[offset]) |
|
|
(static_cast<u32>(bytes[offset + 1]) << 8) |
|
|
(static_cast<u32>(bytes[offset + 2]) << 16) |
|
|
(static_cast<u32>(bytes[offset + 3]) << 24);
|
|
}
|
|
|
|
std::optional<FixedMapEntry> readFixedMapEntry(const std::vector<u8>& bytes, u32 offset)
|
|
{
|
|
if (offset + fixedMapRecordSize > bytes.size()) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
const auto diskX = readU16Le(bytes, offset);
|
|
const auto diskY = readU16Le(bytes, offset + 2);
|
|
const auto shape = readU16Le(bytes, offset + 5);
|
|
const auto flags = readU16Le(bytes, offset + 8);
|
|
const auto quality = readU16Le(bytes, offset + 10);
|
|
const auto next = readU16Le(bytes, offset + 14);
|
|
if (!diskX || !diskY || !shape || !flags || !quality || !next) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
FixedMapEntry entry;
|
|
entry.x = static_cast<u16>(*diskX * crusaderCoordinateScale);
|
|
entry.y = static_cast<u16>(*diskY * crusaderCoordinateScale);
|
|
entry.z = bytes[offset + 4];
|
|
entry.shape = *shape;
|
|
entry.frame = bytes[offset + 7];
|
|
entry.flags = *flags;
|
|
entry.quality = *quality;
|
|
entry.npcNumber = bytes[offset + 12];
|
|
entry.mapNumber = bytes[offset + 13];
|
|
entry.next = *next;
|
|
return entry;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::optional<FixedMapArchive> loadFixedMapArchive(const std::filesystem::path& path)
|
|
{
|
|
auto bytes = readBinaryFile(path);
|
|
if (!bytes) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
const auto mapCount = readU16Le(*bytes, fixedMapCountOffset);
|
|
if (!mapCount) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
const u32 tableEnd = fixedMapTableOffset + (static_cast<u32>(*mapCount) * fixedMapTableRowSize);
|
|
if (tableEnd > bytes->size()) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
FixedMapArchive archive;
|
|
archive.mapCount = *mapCount;
|
|
archive.bytes = *bytes;
|
|
return archive;
|
|
}
|
|
|
|
std::optional<FixedMap> loadFixedMap(const FixedMapArchive& archive, u32 mapIndex)
|
|
{
|
|
if (mapIndex >= archive.mapCount) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
const u32 tableOffset = fixedMapTableOffset + (mapIndex * fixedMapTableRowSize);
|
|
const auto mapOffset = readU32Le(archive.bytes, tableOffset);
|
|
const auto mapSize = readU32Le(archive.bytes, tableOffset + 4);
|
|
if (!mapOffset || !mapSize) {
|
|
return std::nullopt;
|
|
}
|
|
if (*mapOffset > archive.bytes.size() || *mapSize > archive.bytes.size() - *mapOffset) {
|
|
return std::nullopt;
|
|
}
|
|
if ((*mapSize % fixedMapRecordSize) != 0) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
FixedMap map;
|
|
map.index = mapIndex;
|
|
for (u32 offset = *mapOffset; offset < *mapOffset + *mapSize; offset += fixedMapRecordSize) {
|
|
const auto entry = readFixedMapEntry(archive.bytes, offset);
|
|
if (!entry) {
|
|
return std::nullopt;
|
|
}
|
|
map.entries.push_back(*entry);
|
|
}
|
|
return map;
|
|
}
|
|
|
|
std::optional<std::vector<StagedFixedMapItem>> stageFixedMapItems(const FixedMap& map)
|
|
{
|
|
constexpr std::size_t firstFreeItemNumber = 0x100;
|
|
constexpr std::size_t freeItemCount = 0x2700;
|
|
std::vector<StagedFixedMapItem> items;
|
|
items.reserve(map.entries.size());
|
|
std::size_t allocated = 0;
|
|
for (std::size_t index = 0; index < map.entries.size(); ++index) {
|
|
if (map.entries[index].shape == 0) {
|
|
continue;
|
|
}
|
|
if (allocated == freeItemCount) {
|
|
return std::nullopt;
|
|
}
|
|
const u16 itemNumber = static_cast<u16>(firstFreeItemNumber + allocated++);
|
|
if ((map.entries[index].flags & 8) != 0) {
|
|
continue;
|
|
}
|
|
StagedFixedMapItem item;
|
|
item.itemNumber = itemNumber;
|
|
item.recordIndex = index;
|
|
item.data = map.entries[index];
|
|
item.data.x = static_cast<u16>(item.data.x / crusaderCoordinateScale);
|
|
item.data.y = static_cast<u16>(item.data.y / crusaderCoordinateScale);
|
|
item.data.flags &= static_cast<u16>(~1u);
|
|
items.push_back(item);
|
|
}
|
|
return items;
|
|
}
|
|
|
|
std::optional<StagedFixedMapItem> findTeleportEgg(
|
|
const std::vector<StagedFixedMapItem>& items, const std::vector<ShapeTypeInfo>& shapes, u8 eggId)
|
|
{
|
|
std::optional<StagedFixedMapItem> match;
|
|
for (const auto& item : items) {
|
|
if (item.data.shape >= shapes.size() || shapes[item.data.shape].family != 8 ||
|
|
item.data.frame != 1 || (item.data.quality & 0xff) != eggId) {
|
|
continue;
|
|
}
|
|
if (match) {
|
|
return std::nullopt;
|
|
}
|
|
match = item;
|
|
}
|
|
return match;
|
|
}
|
|
|
|
std::optional<StagedFixedMapItem> findNearbyActiveTeleportEgg(const StagedFixedMapItem& origin,
|
|
const std::vector<StagedFixedMapItem>& items, const std::vector<ShapeTypeInfo>& shapes,
|
|
u8 currentMapNumber)
|
|
{
|
|
for (const auto& item : items) {
|
|
if (item.data.shape >= shapes.size() || shapes[item.data.shape].family != 8 ||
|
|
item.data.frame == 1 || item.data.mapNumber != currentMapNumber) {
|
|
continue;
|
|
}
|
|
const s32 xRange = static_cast<s32>((item.data.npcNumber >> 4) & 0x0f) * 64;
|
|
const s32 yRange = static_cast<s32>(item.data.npcNumber & 0x0f) * 64;
|
|
const s32 deltaX = static_cast<s32>(origin.data.x) - item.data.x;
|
|
const s32 deltaY = static_cast<s32>(origin.data.y) - item.data.y;
|
|
const s32 deltaZ = static_cast<s32>(origin.data.z) - item.data.z;
|
|
if (deltaX >= -xRange && deltaX <= xRange && deltaY >= -yRange && deltaY <= yRange &&
|
|
deltaZ >= -48 && deltaZ <= 48) {
|
|
return item;
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<std::vector<StagedContainedFixedMapItem>> stageContainedFixedMapItems(
|
|
const FixedMap& map, const std::vector<ShapeTypeInfo>& shapes)
|
|
{
|
|
if (map.index == 0) {
|
|
return std::nullopt;
|
|
}
|
|
std::vector<StagedContainedFixedMapItem> contained;
|
|
std::vector<u16> parentById(0x2800);
|
|
std::vector<u16> shapeById(0x2800);
|
|
u16 previousItemNumber = 0;
|
|
u16 currentParent = 0;
|
|
u16 currentDepth = 0;
|
|
std::size_t allocated = 0;
|
|
for (std::size_t index = 0; index < map.entries.size(); ++index) {
|
|
const auto& record = map.entries[index];
|
|
if (record.shape == 0) {
|
|
continue;
|
|
}
|
|
if (allocated == 0x2700 || record.shape >= shapes.size()) {
|
|
return std::nullopt;
|
|
}
|
|
const auto itemNumber = static_cast<u16>(0x100 + allocated++);
|
|
shapeById[itemNumber] = record.shape;
|
|
if ((record.flags & 8) != 0) {
|
|
const u16 depth = record.x / crusaderCoordinateScale;
|
|
if (depth > currentDepth) {
|
|
currentParent = previousItemNumber;
|
|
currentDepth = depth;
|
|
}
|
|
while (depth < currentDepth) {
|
|
currentParent = parentById[currentParent];
|
|
--currentDepth;
|
|
}
|
|
if (currentParent == 0 || (shapes[shapeById[currentParent]].family != 6 &&
|
|
shapes[shapeById[currentParent]].family != 7)) {
|
|
return std::nullopt;
|
|
}
|
|
parentById[itemNumber] = currentParent;
|
|
contained.push_back({itemNumber, currentParent, index, record});
|
|
} else {
|
|
currentParent = 0;
|
|
currentDepth = 0;
|
|
}
|
|
previousItemNumber = itemNumber;
|
|
}
|
|
return contained;
|
|
}
|
|
|
|
std::optional<StagedFixedMapItem> nearestStagedFixedMapItem(
|
|
const std::vector<StagedFixedMapItem>& items, s32 worldX, s32 worldY, s32 maxDistance)
|
|
{
|
|
if (maxDistance < 0) {
|
|
return std::nullopt;
|
|
}
|
|
const auto limit = static_cast<std::int64_t>(maxDistance) * maxDistance;
|
|
auto bestDistance = limit + 1;
|
|
const StagedFixedMapItem* best = nullptr;
|
|
for (const auto& item : items) {
|
|
const auto deltaX = static_cast<std::int64_t>(item.data.x) * crusaderCoordinateScale - worldX;
|
|
const auto deltaY = static_cast<std::int64_t>(item.data.y) * crusaderCoordinateScale - worldY;
|
|
const auto distance = deltaX * deltaX + deltaY * deltaY;
|
|
if (distance <= limit && distance < bestDistance) {
|
|
bestDistance = distance;
|
|
best = &item;
|
|
}
|
|
}
|
|
return best == nullptr ? std::nullopt : std::optional<StagedFixedMapItem>(*best);
|
|
}
|
|
|
|
bool itemGrabCanCreateProcess(const StagedFixedMapItem& item)
|
|
{
|
|
return (item.data.flags & 0x02) == 0;
|
|
}
|
|
|
|
bool itemFallStartsHurl(const ShapeTypeInfo& shape)
|
|
{
|
|
return (shape.flags & 1) == 0 && shape.weight != 0;
|
|
}
|
|
|
|
u8 classifyItemFindZMatch(const StagedFixedMapItem& origin, const ShapeTypeInfo& originShape,
|
|
const StagedFixedMapItem& candidate, const ShapeTypeInfo& candidateShape, u8 zMatchFlags)
|
|
{
|
|
if (candidate.itemNumber == 0) {
|
|
return 0;
|
|
}
|
|
const auto height = [](const ShapeTypeInfo& shape) {
|
|
return shape.z == 0 && (shape.flags & 1) == 0 ? 1 : static_cast<int>(shape.z) * 8;
|
|
};
|
|
const int minimumX = static_cast<int>(candidate.data.x) - static_cast<int>(candidateShape.x) * 16 + 1;
|
|
const int minimumY = static_cast<int>(candidate.data.y) - static_cast<int>(candidateShape.y) * 16 + 1;
|
|
if (minimumX > origin.data.x || minimumY > origin.data.y) {
|
|
return 0;
|
|
}
|
|
const u8 originTop = static_cast<u8>(static_cast<int>(origin.data.z) + height(originShape));
|
|
if ((zMatchFlags & 1) != 0 && candidate.data.z == originTop) {
|
|
return 1;
|
|
}
|
|
if ((zMatchFlags & 2) != 0 && origin.data.z == static_cast<int>(candidate.data.z) + height(candidateShape)) {
|
|
return 2;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
ItemFindArea itemFindAreaAroundItem(const StagedFixedMapItem& origin, const ShapeTypeInfo& originShape)
|
|
{
|
|
const auto normalize = [](u16 value, u16 negativeValue) {
|
|
return static_cast<s16>(value) < 0 ? negativeValue : value;
|
|
};
|
|
const u16 minX = static_cast<u16>(static_cast<int>(origin.data.x) - originShape.x * 16 + 1);
|
|
const u16 minY = static_cast<u16>(static_cast<int>(origin.data.y) - originShape.y * 16 + 1);
|
|
const u16 maxX = static_cast<u16>(origin.data.x + 0x100);
|
|
const u16 maxY = static_cast<u16>(origin.data.y + 0x100);
|
|
return {normalize(minX, 0), normalize(minY, 0),
|
|
normalize(maxX, 0x7fff), normalize(maxY, 0x7fff)};
|
|
}
|
|
|
|
std::vector<u16> itemFindBlockOrder(const ItemFindArea& area)
|
|
{
|
|
std::vector<u16> blocks;
|
|
if (area.minX > area.maxX || area.minY > area.maxY) {
|
|
return blocks;
|
|
}
|
|
for (u16 blockY = area.minY >> 9; blockY <= (area.maxY >> 9); ++blockY) {
|
|
for (u16 blockX = area.minX >> 9; blockX <= (area.maxX >> 9); ++blockX) {
|
|
blocks.push_back(static_cast<u16>(blockX * 0x40 + blockY));
|
|
}
|
|
}
|
|
return blocks;
|
|
}
|
|
|
|
std::optional<StagedItemBlockChains> buildStagedItemBlockChains(const std::vector<StagedFixedMapItem>& items)
|
|
{
|
|
StagedItemBlockChains chains;
|
|
chains.heads.resize(0x1000);
|
|
chains.next.resize(0x2800);
|
|
std::vector<bool> seen(chains.next.size());
|
|
for (const auto& item : items) {
|
|
if (item.itemNumber < 0x100 || item.itemNumber >= chains.next.size() || seen[item.itemNumber] ||
|
|
item.data.x > 0x7fff || item.data.y > 0x7fff) {
|
|
return std::nullopt;
|
|
}
|
|
seen[item.itemNumber] = true;
|
|
const auto block = (item.data.x >> 9) * 0x40 + (item.data.y >> 9);
|
|
chains.next[item.itemNumber] = chains.heads[block];
|
|
chains.heads[block] = item.itemNumber;
|
|
}
|
|
return chains;
|
|
}
|
|
|
|
std::vector<u16> itemFindCandidateOrder(const ItemFindArea& area, const StagedItemBlockChains& chains)
|
|
{
|
|
std::vector<u16> candidates;
|
|
for (const auto block : itemFindBlockOrder(area)) {
|
|
for (u16 itemNumber = chains.heads[block]; itemNumber != 0; itemNumber = chains.next[itemNumber]) {
|
|
candidates.push_back(itemNumber);
|
|
}
|
|
}
|
|
return candidates;
|
|
}
|
|
|
|
namespace {
|
|
|
|
std::optional<std::vector<ItemFindZCandidate>> collectZMatches(const StagedFixedMapItem& origin,
|
|
const std::vector<StagedFixedMapItem>& items, const std::vector<ShapeTypeInfo>& shapes,
|
|
u8 zMatchFlags, bool checkRectangle)
|
|
{
|
|
if (origin.data.shape >= shapes.size()) {
|
|
return std::nullopt;
|
|
}
|
|
const auto chains = buildStagedItemBlockChains(items);
|
|
if (!chains) {
|
|
return std::nullopt;
|
|
}
|
|
std::vector<const StagedFixedMapItem*> byId(chains->next.size());
|
|
for (const auto& item : items) {
|
|
if (item.data.shape >= shapes.size()) {
|
|
return std::nullopt;
|
|
}
|
|
byId[item.itemNumber] = &item;
|
|
}
|
|
std::vector<ItemFindZCandidate> matches;
|
|
const auto area = itemFindAreaAroundItem(origin, shapes[origin.data.shape]);
|
|
for (const auto itemNumber : itemFindCandidateOrder(area, *chains)) {
|
|
const auto& candidate = *byId[itemNumber];
|
|
if (checkRectangle && (candidate.data.x < area.minX || candidate.data.x > area.maxX ||
|
|
candidate.data.y < area.minY || candidate.data.y > area.maxY)) {
|
|
continue;
|
|
}
|
|
const auto matchType = classifyItemFindZMatch(origin, shapes[origin.data.shape],
|
|
candidate, shapes[candidate.data.shape], zMatchFlags);
|
|
if (matchType != 0) {
|
|
matches.push_back({itemNumber, matchType});
|
|
}
|
|
}
|
|
return matches;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::optional<std::vector<ItemFindZCandidate>> itemFindZMatches(const StagedFixedMapItem& origin,
|
|
const std::vector<StagedFixedMapItem>& items, const std::vector<ShapeTypeInfo>& shapes, u8 zMatchFlags)
|
|
{
|
|
return collectZMatches(origin, items, shapes, zMatchFlags, false);
|
|
}
|
|
|
|
std::optional<std::vector<ItemFindZCandidate>> itemGrabSearchCandidates(const StagedFixedMapItem& origin,
|
|
const std::vector<StagedFixedMapItem>& items, const std::vector<ShapeTypeInfo>& shapes)
|
|
{
|
|
return collectZMatches(origin, items, shapes, 3, true);
|
|
}
|
|
|
|
std::optional<std::vector<ItemGrabAction>> planItemGrabActions(const StagedFixedMapItem& origin,
|
|
const std::vector<StagedFixedMapItem>& items, const std::vector<ShapeTypeInfo>& shapes)
|
|
{
|
|
const auto candidates = itemGrabSearchCandidates(origin, items, shapes);
|
|
if (!candidates) {
|
|
return std::nullopt;
|
|
}
|
|
std::vector<const StagedFixedMapItem*> byId(0x2800);
|
|
for (const auto& item : items) {
|
|
byId[item.itemNumber] = &item;
|
|
}
|
|
std::vector<ItemGrabAction> actions;
|
|
for (const auto& candidate : *candidates) {
|
|
const auto& item = *byId[candidate.itemNumber];
|
|
if (candidate.matchType == 1 && itemFallStartsHurl(shapes[item.data.shape])) {
|
|
actions.push_back({candidate.itemNumber, ItemGrabActionKind::Hurl});
|
|
} else if (candidate.matchType == 2 && itemGrabCanCreateProcess(item)) {
|
|
actions.push_back({candidate.itemNumber, ItemGrabActionKind::CreateProcess});
|
|
}
|
|
}
|
|
return actions;
|
|
}
|
|
|
|
std::optional<std::vector<u16>> dispatchStagedItemGrab(const StagedFixedMapItem& origin,
|
|
const std::vector<StagedFixedMapItem>& items, const std::vector<ShapeTypeInfo>& shapes,
|
|
ProcessKernel& kernel, u16 (&groupCounts)[32], u32 timerTick)
|
|
{
|
|
const auto actions = planItemGrabActions(origin, items, shapes);
|
|
if (!actions) {
|
|
return std::nullopt;
|
|
}
|
|
for (const auto& action : *actions) {
|
|
if (action.itemNumber > ProcessKernel::maxTrackedItemNumber) {
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
std::vector<u16> processIds;
|
|
for (const auto& action : *actions) {
|
|
if (action.kind == ItemGrabActionKind::Hurl) {
|
|
processIds.push_back(kernel.hurlItem(kernel.activeGroup(), action.itemNumber, 0, 0, 0, 2,
|
|
timerTick, groupCounts));
|
|
} else {
|
|
auto* process = kernel.createItemProcess(kernel.activeGroup(), action.itemNumber, groupCounts);
|
|
processIds.push_back(static_cast<u16>(process->bytes[2] | (static_cast<u16>(process->bytes[3]) << 8)));
|
|
}
|
|
}
|
|
return processIds;
|
|
}
|
|
|
|
LiveFixedMapItems::LiveFixedMapItems(std::vector<StagedFixedMapItem> items,
|
|
std::vector<StagedContainedFixedMapItem> containedItems)
|
|
: items_(std::move(items)), allocatedIds_(0x2800), hatchedGlobIds_(0x2800), parentById_(0x2800),
|
|
containedItemData_(0x2800), containedNextById_(0x2800), containedHeadById_(0x2800),
|
|
containedTailById_(0x2800)
|
|
{
|
|
freeItemIds_.reserve(allocatedIds_.size());
|
|
if (!buildStagedItemBlockChains(items_)) {
|
|
throw std::invalid_argument("invalid live item IDs or coordinates");
|
|
}
|
|
for (const auto& item : items_) {
|
|
allocatedIds_[item.itemNumber] = true;
|
|
}
|
|
for (const auto& contained : containedItems) {
|
|
const u16 itemNumber = contained.itemNumber;
|
|
if (itemNumber < 0x100 || itemNumber >= allocatedIds_.size() || allocatedIds_[itemNumber]) {
|
|
throw std::invalid_argument("invalid contained item ID");
|
|
}
|
|
allocatedIds_[itemNumber] = true;
|
|
}
|
|
for (const auto& contained : containedItems) {
|
|
if (contained.parentItemNumber < 0x100 || contained.parentItemNumber >= contained.itemNumber ||
|
|
!allocatedIds_[contained.parentItemNumber]) {
|
|
throw std::invalid_argument("invalid contained parent ID");
|
|
}
|
|
parentById_[contained.itemNumber] = contained.parentItemNumber;
|
|
containedItemData_[contained.itemNumber] = contained.data;
|
|
const u16 previousTail = containedTailById_[contained.parentItemNumber];
|
|
if (previousTail == 0) {
|
|
containedHeadById_[contained.parentItemNumber] = contained.itemNumber;
|
|
} else {
|
|
containedNextById_[previousTail] = contained.itemNumber;
|
|
}
|
|
containedTailById_[contained.parentItemNumber] = contained.itemNumber;
|
|
refreshContainerQuality(contained.parentItemNumber);
|
|
}
|
|
}
|
|
|
|
bool LiveFixedMapItems::initializeAvatar()
|
|
{
|
|
if (avatar_) {
|
|
return false;
|
|
}
|
|
avatar_.emplace();
|
|
avatar_->itemNumber = 1;
|
|
avatar_->data.shape = 1;
|
|
avatar_->data.npcNumber = 1;
|
|
npcAnimations_.resize(2);
|
|
return true;
|
|
}
|
|
|
|
bool LiveFixedMapItems::teleportAvatar(const StagedFixedMapItem& egg, u8 mapNumber)
|
|
{
|
|
if (!avatar_) {
|
|
return false;
|
|
}
|
|
avatar_->data.x = egg.data.x;
|
|
avatar_->data.y = egg.data.y;
|
|
avatar_->data.z = egg.data.z;
|
|
avatar_->data.mapNumber = mapNumber;
|
|
return true;
|
|
}
|
|
|
|
const StagedFixedMapItem* LiveFixedMapItems::findItem(u16 itemNumber) const
|
|
{
|
|
if (itemNumber == 1) {
|
|
return avatar_ ? &*avatar_ : nullptr;
|
|
}
|
|
for (const auto& item : items_) {
|
|
if (item.itemNumber == itemNumber) {
|
|
return &item;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
const NpcAnimationState* LiveFixedMapItems::findNpcAnimation(u16 itemNumber) const
|
|
{
|
|
const auto* item = findItem(itemNumber);
|
|
return itemNumber < npcAnimations_.size() && item != nullptr && item->data.npcNumber != 0
|
|
? &npcAnimations_[itemNumber] : nullptr;
|
|
}
|
|
|
|
NpcAnimationState* LiveFixedMapItems::editNpcAnimation(u16 itemNumber)
|
|
{
|
|
return const_cast<NpcAnimationState*>(
|
|
static_cast<const LiveFixedMapItems*>(this)->findNpcAnimation(itemNumber));
|
|
}
|
|
|
|
u16 LiveFixedMapItems::containedParent(u16 itemNumber) const
|
|
{
|
|
return itemNumber < parentById_.size() ? parentById_[itemNumber] : 0;
|
|
}
|
|
|
|
std::vector<u16> LiveFixedMapItems::containedChildren(u16 parentItemNumber) const
|
|
{
|
|
std::vector<u16> children;
|
|
if (parentItemNumber >= containedHeadById_.size()) {
|
|
return children;
|
|
}
|
|
for (u16 itemNumber = containedHeadById_[parentItemNumber]; itemNumber != 0;
|
|
itemNumber = containedNextById_[itemNumber]) {
|
|
if (itemNumber >= containedNextById_.size() ||
|
|
parentById_[itemNumber] != parentItemNumber || children.size() >= containedNextById_.size()) {
|
|
children.clear();
|
|
return children;
|
|
}
|
|
children.push_back(itemNumber);
|
|
}
|
|
return children;
|
|
}
|
|
|
|
bool LiveFixedMapItems::unlinkContainedItem(u16 itemNumber)
|
|
{
|
|
if (itemNumber >= parentById_.size()) {
|
|
return false;
|
|
}
|
|
const u16 parentItemNumber = parentById_[itemNumber];
|
|
if (parentItemNumber == 0 || parentItemNumber >= containedHeadById_.size()) {
|
|
return false;
|
|
}
|
|
u16 previousItemNumber = 0;
|
|
u16 currentItemNumber = containedHeadById_[parentItemNumber];
|
|
while (currentItemNumber != 0 && currentItemNumber != itemNumber) {
|
|
if (currentItemNumber >= containedNextById_.size()) {
|
|
return false;
|
|
}
|
|
previousItemNumber = currentItemNumber;
|
|
currentItemNumber = containedNextById_[currentItemNumber];
|
|
}
|
|
if (currentItemNumber != itemNumber) {
|
|
return false;
|
|
}
|
|
const u16 nextItemNumber = containedNextById_[itemNumber];
|
|
if (previousItemNumber == 0) {
|
|
containedHeadById_[parentItemNumber] = nextItemNumber;
|
|
} else {
|
|
containedNextById_[previousItemNumber] = nextItemNumber;
|
|
}
|
|
if (containedTailById_[parentItemNumber] == itemNumber) {
|
|
containedTailById_[parentItemNumber] = previousItemNumber;
|
|
}
|
|
if (containedHeadById_[parentItemNumber] == 0) {
|
|
containedTailById_[parentItemNumber] = 0;
|
|
}
|
|
parentById_[itemNumber] = 0;
|
|
containedNextById_[itemNumber] = 0;
|
|
refreshContainerQuality(parentItemNumber);
|
|
return true;
|
|
}
|
|
|
|
void LiveFixedMapItems::refreshContainerQuality(u16 parentItemNumber)
|
|
{
|
|
if (parentItemNumber >= containedHeadById_.size()) {
|
|
return;
|
|
}
|
|
const u16 firstChild = containedHeadById_[parentItemNumber];
|
|
const auto worldParent = std::find_if(items_.begin(), items_.end(), [parentItemNumber](const auto& item) {
|
|
return item.itemNumber == parentItemNumber;
|
|
});
|
|
if (worldParent != items_.end()) {
|
|
worldParent->data.quality = firstChild;
|
|
} else if (containedItemData_[parentItemNumber]) {
|
|
containedItemData_[parentItemNumber]->quality = firstChild;
|
|
}
|
|
}
|
|
|
|
bool LiveFixedMapItems::insertContainedItemAtHead(u16 parentItemNumber, u16 itemNumber)
|
|
{
|
|
if (parentItemNumber >= allocatedIds_.size() || itemNumber >= allocatedIds_.size() ||
|
|
parentItemNumber == itemNumber || !allocatedIds_[parentItemNumber] || !allocatedIds_[itemNumber] ||
|
|
parentById_[itemNumber] != 0 || containedNextById_[itemNumber] != 0) {
|
|
return false;
|
|
}
|
|
const u16 previousHead = containedHeadById_[parentItemNumber];
|
|
containedNextById_[itemNumber] = previousHead;
|
|
containedHeadById_[parentItemNumber] = itemNumber;
|
|
if (previousHead == 0) {
|
|
containedTailById_[parentItemNumber] = itemNumber;
|
|
}
|
|
parentById_[itemNumber] = parentItemNumber;
|
|
refreshContainerQuality(parentItemNumber);
|
|
return true;
|
|
}
|
|
|
|
std::optional<StagedFixedMapItem> LiveFixedMapItems::nearestItem(s32 worldX, s32 worldY,
|
|
s32 maxDistance) const
|
|
{
|
|
return nearestStagedFixedMapItem(items_, worldX, worldY, maxDistance);
|
|
}
|
|
|
|
std::optional<std::vector<u16>> LiveFixedMapItems::sweepCandidateIds(const WorldRect& area,
|
|
u16 excludedItemNumber) const
|
|
{
|
|
if (area.minX < 0 || area.minY < 0 || area.maxX > 0x7fff || area.maxY > 0x7fff ||
|
|
area.minX > area.maxX || area.minY > area.maxY) {
|
|
return std::nullopt;
|
|
}
|
|
const auto chains = buildStagedItemBlockChains(items_);
|
|
if (!chains) {
|
|
return std::nullopt;
|
|
}
|
|
std::vector<const StagedFixedMapItem*> byId(chains->next.size());
|
|
for (const auto& item : items_) {
|
|
byId[item.itemNumber] = &item;
|
|
}
|
|
const ItemFindArea itemArea{static_cast<u16>(area.minX), static_cast<u16>(area.minY),
|
|
static_cast<u16>(area.maxX), static_cast<u16>(area.maxY)};
|
|
std::vector<u16> candidates;
|
|
for (const u16 itemNumber : itemFindCandidateOrder(itemArea, *chains)) {
|
|
const auto& item = *byId[itemNumber];
|
|
if (itemNumber != excludedItemNumber && item.data.x >= area.minX && item.data.x <= area.maxX &&
|
|
item.data.y >= area.minY && item.data.y <= area.maxY) {
|
|
candidates.push_back(itemNumber);
|
|
}
|
|
}
|
|
return candidates;
|
|
}
|
|
|
|
std::optional<u16> LiveFixedMapItems::allocateItem(StagedFixedMapItem item)
|
|
{
|
|
if (item.data.x > 0x7fff || item.data.y > 0x7fff) {
|
|
return std::nullopt;
|
|
}
|
|
while (!freeItemIds_.empty()) {
|
|
const u16 itemNumber = freeItemIds_.back();
|
|
freeItemIds_.pop_back();
|
|
if (itemNumber >= allocatedIds_.size() || allocatedIds_[itemNumber]) {
|
|
continue;
|
|
}
|
|
item.itemNumber = itemNumber;
|
|
if (insertItem(item)) {
|
|
return itemNumber;
|
|
}
|
|
freeItemIds_.push_back(itemNumber);
|
|
return std::nullopt;
|
|
}
|
|
while (nextFreeItemNumber_ < allocatedIds_.size() && allocatedIds_[nextFreeItemNumber_]) {
|
|
++nextFreeItemNumber_;
|
|
}
|
|
if (nextFreeItemNumber_ == allocatedIds_.size()) {
|
|
return std::nullopt;
|
|
}
|
|
item.itemNumber = nextFreeItemNumber_;
|
|
insertItem(item);
|
|
return item.itemNumber;
|
|
}
|
|
|
|
bool LiveFixedMapItems::insertItem(StagedFixedMapItem item)
|
|
{
|
|
if (item.itemNumber < 0x100 || item.itemNumber >= allocatedIds_.size() || item.data.x > 0x7fff ||
|
|
item.data.y > 0x7fff || findItem(item.itemNumber)) {
|
|
return false;
|
|
}
|
|
const bool wasContained = parentById_[item.itemNumber] != 0 &&
|
|
containedItemData_[item.itemNumber].has_value();
|
|
if (parentById_[item.itemNumber] != 0 && !wasContained) {
|
|
return false;
|
|
}
|
|
if (allocatedIds_[item.itemNumber] != wasContained) {
|
|
return false;
|
|
}
|
|
if (wasContained) {
|
|
item.data.flags &= static_cast<u16>(~8u);
|
|
}
|
|
item.data.z = std::min<u8>(item.data.z, 0xfa);
|
|
items_.push_back(item);
|
|
if (wasContained && !unlinkContainedItem(item.itemNumber)) {
|
|
items_.pop_back();
|
|
return false;
|
|
}
|
|
containedItemData_[item.itemNumber].reset();
|
|
const auto freeItem = std::find(freeItemIds_.begin(), freeItemIds_.end(), item.itemNumber);
|
|
if (freeItem != freeItemIds_.end()) {
|
|
freeItemIds_.erase(freeItem);
|
|
}
|
|
allocatedIds_[item.itemNumber] = true;
|
|
return true;
|
|
}
|
|
|
|
bool LiveFixedMapItems::moveItemIntoContainer(u16 itemNumber, u16 containerItemNumber,
|
|
const std::vector<ShapeTypeInfo>& shapes)
|
|
{
|
|
if (itemNumber < 0x100 || containerItemNumber < 0x100 || itemNumber == containerItemNumber ||
|
|
itemNumber >= allocatedIds_.size() || containerItemNumber >= allocatedIds_.size() ||
|
|
!allocatedIds_[itemNumber] || !allocatedIds_[containerItemNumber]) {
|
|
return false;
|
|
}
|
|
const auto worldContainer = std::find_if(items_.begin(), items_.end(), [containerItemNumber](const auto& item) {
|
|
return item.itemNumber == containerItemNumber;
|
|
});
|
|
const FixedMapEntry* containerData = worldContainer == items_.end()
|
|
? (containedItemData_[containerItemNumber] ? &*containedItemData_[containerItemNumber] : nullptr)
|
|
: &worldContainer->data;
|
|
if (containerData == nullptr || containerData->shape >= shapes.size() ||
|
|
shapes[containerData->shape].family != 6) {
|
|
return false;
|
|
}
|
|
if (parentById_[itemNumber] == containerItemNumber) {
|
|
return true;
|
|
}
|
|
for (u16 ancestor = containerItemNumber; ancestor != 0; ancestor = parentById_[ancestor]) {
|
|
if (ancestor == itemNumber || ancestor >= parentById_.size()) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
const auto worldItem = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& item) {
|
|
return item.itemNumber == itemNumber;
|
|
});
|
|
const bool wasContained = worldItem == items_.end() && parentById_[itemNumber] != 0 &&
|
|
containedItemData_[itemNumber].has_value();
|
|
if (worldItem == items_.end() && !wasContained) {
|
|
return false;
|
|
}
|
|
FixedMapEntry itemData = worldItem == items_.end() ? *containedItemData_[itemNumber] : worldItem->data;
|
|
if (wasContained && !unlinkContainedItem(itemNumber)) {
|
|
return false;
|
|
}
|
|
if (worldItem != items_.end()) {
|
|
items_.erase(worldItem);
|
|
}
|
|
itemData.flags |= 8;
|
|
containedItemData_[itemNumber] = itemData;
|
|
return insertContainedItemAtHead(containerItemNumber, itemNumber);
|
|
}
|
|
|
|
bool LiveFixedMapItems::moveContainedItemToWorld(u16 itemNumber, u16 x, u16 y, u8 z)
|
|
{
|
|
if (itemNumber < 0x100 || itemNumber >= allocatedIds_.size() || !allocatedIds_[itemNumber] ||
|
|
parentById_[itemNumber] == 0 || !containedItemData_[itemNumber] || x > 0x7fff || y > 0x7fff) {
|
|
return false;
|
|
}
|
|
FixedMapEntry itemData = *containedItemData_[itemNumber];
|
|
itemData.x = x;
|
|
itemData.y = y;
|
|
itemData.z = std::min<u8>(z, 0xfa);
|
|
itemData.flags &= static_cast<u16>(~9u);
|
|
items_.push_back({itemNumber, static_cast<std::size_t>(-1), itemData});
|
|
if (!unlinkContainedItem(itemNumber)) {
|
|
items_.pop_back();
|
|
return false;
|
|
}
|
|
containedItemData_[itemNumber].reset();
|
|
return true;
|
|
}
|
|
|
|
bool LiveFixedMapItems::releaseItem(u16 itemNumber, const ProcessKernel& kernel)
|
|
{
|
|
if (itemNumber < 0x100 || itemNumber > ProcessKernel::maxTrackedItemNumber ||
|
|
itemNumber >= allocatedIds_.size() || !allocatedIds_[itemNumber] ||
|
|
kernel.itemHasProcess(itemNumber) || containedHeadById_[itemNumber] != 0 ||
|
|
hatchedGlobIds_[itemNumber]) {
|
|
return false;
|
|
}
|
|
const auto worldItem = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& item) {
|
|
return item.itemNumber == itemNumber;
|
|
});
|
|
const bool wasContained = worldItem == items_.end() && parentById_[itemNumber] != 0 &&
|
|
containedItemData_[itemNumber].has_value();
|
|
if (worldItem == items_.end() && !wasContained) {
|
|
return false;
|
|
}
|
|
if (wasContained && !unlinkContainedItem(itemNumber)) {
|
|
return false;
|
|
}
|
|
if (worldItem != items_.end()) {
|
|
items_.erase(worldItem);
|
|
}
|
|
containedItemData_[itemNumber].reset();
|
|
containedNextById_[itemNumber] = 0;
|
|
containedHeadById_[itemNumber] = 0;
|
|
containedTailById_[itemNumber] = 0;
|
|
parentById_[itemNumber] = 0;
|
|
allocatedIds_[itemNumber] = false;
|
|
freeItemIds_.push_back(itemNumber);
|
|
return true;
|
|
}
|
|
|
|
std::optional<std::vector<u16>> LiveFixedMapItems::hatchGlobEgg(u16 eggItemNumber,
|
|
const GlobObject& glob, const std::vector<ShapeTypeInfo>& shapes)
|
|
{
|
|
const auto* egg = findItem(eggItemNumber);
|
|
if (!egg || egg->data.shape >= shapes.size() || shapes[egg->data.shape].family != 3 ||
|
|
egg->data.quality == 0 || egg->data.quality != glob.index ||
|
|
isGlobHatched(eggItemNumber)) {
|
|
return std::nullopt;
|
|
}
|
|
std::vector<StagedFixedMapItem> children;
|
|
children.reserve(glob.entries.size());
|
|
for (const auto& entry : glob.entries) {
|
|
StagedFixedMapItem child;
|
|
child.recordIndex = static_cast<std::size_t>(-1);
|
|
child.data.x = static_cast<u16>((egg->data.x & ~0x1ff) + entry.x * 2 + 1);
|
|
child.data.y = static_cast<u16>((egg->data.y & ~0x1ff) + entry.y * 2 + 1);
|
|
child.data.z = static_cast<u8>(egg->data.z + entry.z);
|
|
child.data.shape = entry.shape;
|
|
child.data.frame = entry.frame;
|
|
child.data.flags = 0x82;
|
|
if (entry.shape == 0 || child.data.x > 0x7fff || child.data.y > 0x7fff) {
|
|
return std::nullopt;
|
|
}
|
|
children.push_back(child);
|
|
}
|
|
const std::size_t freeCount = static_cast<std::size_t>(std::count(
|
|
allocatedIds_.begin() + nextFreeItemNumber_, allocatedIds_.end(), false));
|
|
if (freeCount < children.size()) {
|
|
return std::nullopt;
|
|
}
|
|
std::vector<u16> childIds;
|
|
childIds.reserve(children.size());
|
|
for (auto& child : children) {
|
|
childIds.push_back(*allocateItem(child));
|
|
}
|
|
hatchedGlobIds_[eggItemNumber] = true;
|
|
return childIds;
|
|
}
|
|
|
|
bool LiveFixedMapItems::isGlobHatched(u16 eggItemNumber) const
|
|
{
|
|
return eggItemNumber < hatchedGlobIds_.size() && hatchedGlobIds_[eggItemNumber];
|
|
}
|
|
|
|
bool LiveFixedMapItems::setItemShape(u16 itemNumber, u16 shape)
|
|
{
|
|
const auto item = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& candidate) {
|
|
return candidate.itemNumber == itemNumber;
|
|
});
|
|
if (item == items_.end()) {
|
|
return false;
|
|
}
|
|
item->data.shape = shape;
|
|
return true;
|
|
}
|
|
|
|
bool LiveFixedMapItems::setItemFrame(u16 itemNumber, u8 frame)
|
|
{
|
|
const auto item = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& candidate) {
|
|
return candidate.itemNumber == itemNumber;
|
|
});
|
|
if (item == items_.end()) {
|
|
return false;
|
|
}
|
|
item->data.frame = frame;
|
|
return true;
|
|
}
|
|
|
|
bool LiveFixedMapItems::moveItem(u16 itemNumber, u16 x, u16 y, u8 z)
|
|
{
|
|
if (x > 0x7fff || y > 0x7fff) {
|
|
return false;
|
|
}
|
|
if (itemNumber == 1) {
|
|
if (!avatar_) {
|
|
return false;
|
|
}
|
|
avatar_->data.x = x;
|
|
avatar_->data.y = y;
|
|
avatar_->data.z = std::min<u8>(z, 0xfa);
|
|
return true;
|
|
}
|
|
const auto item = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& candidate) {
|
|
return candidate.itemNumber == itemNumber;
|
|
});
|
|
if (item == items_.end()) {
|
|
return false;
|
|
}
|
|
item->data.x = x;
|
|
item->data.y = y;
|
|
item->data.z = std::min<u8>(z, 0xfa);
|
|
std::rotate(item, item + 1, items_.end());
|
|
return true;
|
|
}
|
|
|
|
std::optional<std::vector<u16>> LiveFixedMapItems::grabItem(u16 itemNumber,
|
|
const std::vector<ShapeTypeInfo>& shapes, ProcessKernel& kernel,
|
|
u16 (&groupCounts)[32], u32 timerTick) const
|
|
{
|
|
const auto* origin = findItem(itemNumber);
|
|
return origin == nullptr ? std::nullopt : dispatchStagedItemGrab(*origin, items_, shapes,
|
|
kernel, groupCounts, timerTick);
|
|
}
|
|
|
|
std::optional<std::vector<u16>> LiveFixedMapItems::moveApprovedItem(u16 itemNumber, u16 x, u16 y,
|
|
u8 z, const std::vector<ShapeTypeInfo>& shapes, ProcessKernel& kernel,
|
|
u16 (&groupCounts)[32], u32 timerTick)
|
|
{
|
|
if (x > 0x7fff || y > 0x7fff || !findItem(itemNumber)) {
|
|
return std::nullopt;
|
|
}
|
|
auto processIds = grabItem(itemNumber, shapes, kernel, groupCounts, timerTick);
|
|
if (processIds) {
|
|
moveItem(itemNumber, x, y, z);
|
|
}
|
|
return processIds;
|
|
}
|
|
|
|
} // namespace remorse
|