Refactor collision handling and item management in the Remorse project

- Introduced new structures for handling sweep impacts and collision results, including SweepItemImpact, SweepCandidateImpact, and SweepCollisionResult.
- Implemented functions to check if a world point or sweep footprint is within a defined fast area.
- Enhanced LiveFixedMapItems to manage contained items and support item allocation with parent-child relationships.
- Updated collision detection logic to handle solid item impacts and collect sweep impacts from live items.
- Added tests for new collision functionalities, including sweep impact resolution and candidate selection.
- Modified existing tests to validate new item management features and ensure proper integration with the collision system.
This commit is contained in:
Maddo 2026-09-26 19:50:26 +02:00
commit b3975ca41b
8 changed files with 467 additions and 8 deletions

View file

@ -2,6 +2,7 @@
#include "remorse/entity_dispatch_entry.h"
#include "remorse/file_system.h"
#include "remorse/map_renderer.h"
#include "remorse/type_flags.h"
#include <algorithm>
@ -412,11 +413,30 @@ std::optional<std::vector<u16>> dispatchStagedItemGrab(const StagedFixedMapItem&
return processIds;
}
LiveFixedMapItems::LiveFixedMapItems(std::vector<StagedFixedMapItem> items) : items_(std::move(items))
LiveFixedMapItems::LiveFixedMapItems(std::vector<StagedFixedMapItem> items,
std::vector<StagedContainedFixedMapItem> containedItems)
: items_(std::move(items)), allocatedIds_(0x2800), parentById_(0x2800)
{
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;
}
}
const StagedFixedMapItem* LiveFixedMapItems::findItem(u16 itemNumber) const
@ -429,12 +449,61 @@ const StagedFixedMapItem* LiveFixedMapItems::findItem(u16 itemNumber) const
return nullptr;
}
u16 LiveFixedMapItems::containedParent(u16 itemNumber) const
{
return itemNumber < parentById_.size() ? parentById_[itemNumber] : 0;
}
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 (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 >= 0x2800 || item.data.x > 0x7fff ||
@ -443,6 +512,8 @@ bool LiveFixedMapItems::insertItem(StagedFixedMapItem item)
}
item.data.z = std::min<u8>(item.data.z, 0xfa);
items_.push_back(item);
allocatedIds_[item.itemNumber] = true;
parentById_[item.itemNumber] = 0;
return true;
}