Enhance item handling and collision detection in remorse

- Introduced new functionality for staging and managing fixed map items, including the ability to build block chains and find candidates for item interactions.
- Updated the main application logic to utilize staged item block chains for improved item finding and grabbing mechanics.
- Added weight attribute to shape type information, allowing for more nuanced item interactions based on weight.
- Enhanced collision tests to cover new item handling scenarios, including gravity sweeps and item grabbing.
- Implemented additional tests for entity dispatch entries and fixed map items to ensure robustness of item spawning and movement logic.
- Improved type flags loading to validate weight against binary data, ensuring consistency in item attributes.
This commit is contained in:
Maddo 2026-09-26 19:20:20 +02:00
commit b99fb59c12
15 changed files with 961 additions and 18 deletions

View file

@ -1,8 +1,13 @@
#include "remorse/fixed_map.h"
#include "remorse/entity_dispatch_entry.h"
#include "remorse/file_system.h"
#include "remorse/type_flags.h"
#include <algorithm>
#include <stdexcept>
#include <utility>
namespace remorse {
namespace {
@ -147,6 +152,54 @@ std::optional<std::vector<StagedFixedMapItem>> stageFixedMapItems(const FixedMap
return items;
}
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});
} else {
currentParent = 0;
currentDepth = 0;
}
previousItemNumber = itemNumber;
}
return contained;
}
std::optional<StagedFixedMapItem> nearestStagedFixedMapItem(
const std::vector<StagedFixedMapItem>& items, s32 worldX, s32 worldY, s32 maxDistance)
{
@ -173,6 +226,11 @@ 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)
{
@ -224,4 +282,209 @@ std::vector<u16> itemFindBlockOrder(const ItemFindArea& area)
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) : items_(std::move(items))
{
if (!buildStagedItemBlockChains(items_)) {
throw std::invalid_argument("invalid live item IDs or coordinates");
}
}
const StagedFixedMapItem* LiveFixedMapItems::findItem(u16 itemNumber) const
{
for (const auto& item : items_) {
if (item.itemNumber == itemNumber) {
return &item;
}
}
return nullptr;
}
std::optional<StagedFixedMapItem> LiveFixedMapItems::nearestItem(s32 worldX, s32 worldY,
s32 maxDistance) const
{
return nearestStagedFixedMapItem(items_, worldX, worldY, maxDistance);
}
bool LiveFixedMapItems::insertItem(StagedFixedMapItem item)
{
if (item.itemNumber < 0x100 || item.itemNumber >= 0x2800 || item.data.x > 0x7fff ||
item.data.y > 0x7fff || findItem(item.itemNumber)) {
return false;
}
item.data.z = std::min<u8>(item.data.z, 0xfa);
items_.push_back(item);
return true;
}
bool LiveFixedMapItems::moveItem(u16 itemNumber, u16 x, u16 y, u8 z)
{
if (x > 0x7fff || y > 0x7fff) {
return false;
}
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