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,4 +1,6 @@
#include "remorse/entity_dispatch_entry.h"
#include "remorse/collision.h"
#include "remorse/fixed_map.h"
#include <algorithm>
#include <cstring>
@ -122,11 +124,115 @@ EntityDispatchEntryBase* ProcessKernel::createItemProcess(u8 activeGroup, u16 it
setLastAllocationSize(sizeof(EntityDispatchEntryBase));
const u16 id = addProcess(*process);
processNames_[id] = "ItemProcess";
if (syncEnabled_) {
EntityDispatchEntryBase* current = currentProcessId_ == 0 ? nullptr : processAt(currentProcessId_);
if (current != nullptr && (current->bytes[0x16] & 0x08) == 0 && (current->bytes[0x18] & 0x02) != 0) {
current->bytes[0x18] |= 0x04;
}
if (id > currentProcessId_) {
process->bytes[0x16] |= 0x40;
}
}
EntityDispatchEntryBase* result = process.get();
ownedProcesses_.push_back(std::move(process));
return result;
}
u16 ProcessKernel::hurlItem(u8 activeGroup, u16 itemNumber, s16 xs, s16 ys, s16 zs, s16 gravity,
u32 timerTick, u16 (&groupCounts)[32])
{
if (itemNumber == 0 || itemNumber > maxTrackedItemNumber) {
throw std::out_of_range("gravity item number exceeds process lookup tables");
}
if (auto* existing = findProcessForItemAndType(itemNumber, 0x203)) {
const u16 processId = readLe16(*existing, 0x02);
if (allocationSizeAt(processId) != sizeof(GravityProcessEntry) ||
std::none_of(ownedGravityProcesses_.begin(), ownedGravityProcesses_.end(),
[existing](const auto& process) { return &process->base == existing; })) {
throw std::logic_error("gravity process ownership is not ported");
}
writeLe16(*existing, 0x32, static_cast<u16>(readLe16(*existing, 0x32) + xs));
writeLe16(*existing, 0x34, static_cast<u16>(readLe16(*existing, 0x34) + ys));
writeLe16(*existing, 0x36, static_cast<u16>(readLe16(*existing, 0x36) + zs));
return processId;
}
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
throw std::overflow_error("process table exhausted");
}
ownedGravityProcesses_.reserve(ownedGravityProcesses_.size() + 1);
auto process = std::make_unique<GravityProcessEntry>();
EntityDispatchEntry::initBase(&process->base, activeGroup, groupCounts);
writeLe16(process->base, 0x00, 0x0610);
setProcessSourceType(process->base, itemNumber);
setProcessEventTypeChecked(process->base, 0x203);
writeLe16(process->base, 0x32, static_cast<u16>(xs));
writeLe16(process->base, 0x34, static_cast<u16>(ys));
writeLe16(process->base, 0x36, static_cast<u16>(zs));
writeLe16(process->base, 0x38, static_cast<u16>(gravity));
writeLe16(process->base, 0x3b, 200);
process->base.bytes[0x3a] = 1;
process->base.bytes[0x3d] = 1;
setLastAllocationSize(sizeof(GravityProcessEntry));
const u16 id = addProcess(process->base);
processNames_[id] = "Gravity";
if (syncEnabled_) {
EntityDispatchEntryBase* current = currentProcessId_ == 0 ? nullptr : processAt(currentProcessId_);
if (current != nullptr && (current->bytes[0x16] & 0x08) == 0 && (current->bytes[0x18] & 0x02) != 0) {
current->bytes[0x18] |= 0x04;
}
if (id > currentProcessId_) {
process->base.bytes[0x16] |= 0x40;
}
}
EntityDispatchEntry::sleep(process->base, timerTick, 0x14);
ownedGravityProcesses_.push_back(std::move(process));
return id;
}
bool ProcessKernel::runUnobstructedGravity(u16 processId, LiveFixedMapItems& items,
u32 timeScale, u32 timerTick,
bool (*approveSweep)(u16, const WorldPoint&, const WorldPoint&, void*), void* context,
const std::vector<ShapeTypeInfo>* shapes, u16 (*groupCounts)[32])
{
const auto* process = processAt(processId);
if (process == nullptr || approveSweep == nullptr || allocationSizeAt(processId) != sizeof(GravityProcessEntry) ||
(process->bytes[0x16] & 0x08) != 0 || (process->bytes[0x19] & 0x10) != 0 ||
std::none_of(ownedGravityProcesses_.begin(), ownedGravityProcesses_.end(),
[process](const auto& owned) { return &owned->base == process; })) {
return false;
}
const u16 itemNumber = readLe16(*process, 0x04);
const auto* item = items.findItem(itemNumber);
const auto zSpeed = static_cast<s16>(readLe16(*process, 0x36));
if (item == nullptr || item->data.z > 250) {
return false;
}
if (zSpeed == 0) {
if (shapes == nullptr || groupCounts == nullptr ||
(readLe16(*process, 0x32) == 0 && readLe16(*process, 0x34) == 0) ||
!items.grabItem(itemNumber, *shapes, *this, *groupCounts, timerTick)) {
return false;
}
}
const WorldPoint from{item->data.x, item->data.y, item->data.z};
const auto step = planGravitySweep(from, static_cast<s16>(readLe16(*process, 0x32)),
static_cast<s16>(readLe16(*process, 0x34)), zSpeed,
static_cast<s16>(readLe16(*process, 0x38)), timeScale);
if (step.destination.x < 0 || step.destination.x > 0x7fff ||
step.destination.y < 0 || step.destination.y > 0x7fff || step.destination.z > 250 ||
!approveSweep(itemNumber, from, step.destination, context)) {
return false;
}
if (!items.moveItem(itemNumber, static_cast<u16>(step.destination.x),
static_cast<u16>(step.destination.y), static_cast<u8>(step.destination.z))) {
return false;
}
auto* mutableProcess = processAt(processId);
writeLe16(*mutableProcess, 0x36, static_cast<u16>(step.nextZSpeed));
EntityDispatchEntry::sleep(*mutableProcess, timerTick, 4 * std::clamp(timeScale, 1u, 4u));
return true;
}
SystemTimerProcessEntry* ProcessKernel::createSystemTimerProcess(u8 activeGroup, u16 (&groupCounts)[32])
{
if (systemTimerProcess_ != nullptr) {
@ -312,6 +418,12 @@ void ProcessKernel::releaseOwnedProcess(u16 id)
ownedStandProcesses_.erase(stand);
return;
}
const auto gravity = std::find_if(ownedGravityProcesses_.begin(), ownedGravityProcesses_.end(),
[entry](const auto& process) { return &process->base == entry; });
if (gravity != ownedGravityProcesses_.end()) {
ownedGravityProcesses_.erase(gravity);
return;
}
const auto owned = std::find_if(ownedProcesses_.begin(), ownedProcesses_.end(),
[entry](const auto& process) { return process.get() == entry; });
ownedProcesses_.erase(owned);
@ -336,15 +448,19 @@ void ProcessKernel::terminateProcess(u16 id, u16 (&groupCounts)[32])
}
const auto stand = std::find_if(ownedStandProcesses_.begin(), ownedStandProcesses_.end(),
[entry](const auto& process) { return &process->base == entry; });
const bool ownedGravityProcess = readLe16(*entry, 0x00) == 0x0610 &&
std::any_of(ownedGravityProcesses_.begin(), ownedGravityProcesses_.end(),
[entry](const auto& process) { return &process->base == entry; });
const bool ownedItemProcess = readLe16(*entry, 0x00) == ItemProcess::vtableOffset &&
std::any_of(ownedProcesses_.begin(), ownedProcesses_.end(),
[entry](const auto& process) { return process.get() == entry; });
if (itemNumber != 0 && (stand == ownedStandProcesses_.end() && !ownedItemProcess || id < 3 ||
if (itemNumber != 0 && (stand == ownedStandProcesses_.end() && !ownedItemProcess && !ownedGravityProcess || id < 3 ||
readLe16(*entry, 0x24) != 0 || readLe16(*entry, 0x26) != 0 ||
readLe16(*entry, 0x2e) != 0 || readLe16(*entry, 0x30) != 0)) {
throw std::logic_error("item process child cleanup is not ported");
}
if (stand == ownedStandProcesses_.end() && std::none_of(ownedProcesses_.begin(), ownedProcesses_.end(),
if (stand == ownedStandProcesses_.end() && !ownedGravityProcess &&
std::none_of(ownedProcesses_.begin(), ownedProcesses_.end(),
[entry](const auto& process) { return process.get() == entry; })) {
throw std::logic_error("external process ownership is not ported");
}
@ -533,6 +649,19 @@ EntityDispatchEntryBase* ProcessKernel::processAt(u16 id) const
return id < processes_.size() ? processes_[id] : nullptr;
}
EntityDispatchEntryBase* ProcessKernel::findProcessForItemAndType(u16 itemNumber, u16 eventType) const
{
for (std::size_t id = 3; id < processes_.size(); ++id) {
EntityDispatchEntryBase* process = processes_[id];
if (process != nullptr && (itemNumber == 0 || readLe16(*process, 0x04) == itemNumber) &&
(eventType == 0x0c || readLe16(*process, 0x06) == eventType) &&
(readLe16(*process, 0x16) & 0x08) == 0) {
return process;
}
}
return nullptr;
}
u16 ProcessKernel::allocationSizeAt(u16 id) const
{
return id < allocationSizes_.size() ? allocationSizes_[id] : 0;