- 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.
851 lines
34 KiB
C++
851 lines
34 KiB
C++
#include "remorse/entity_dispatch_entry.h"
|
|
#include "remorse/collision.h"
|
|
#include "remorse/fixed_map.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
|
|
namespace remorse {
|
|
|
|
namespace {
|
|
constexpr u16 sourceItemOffset = 0x04;
|
|
constexpr u16 eventTypeOffset = 0x06;
|
|
constexpr u16 groupIdOffset = 0x08;
|
|
|
|
void writeLe16(EntityDispatchEntryBase& entry, u16 offset, u16 value)
|
|
{
|
|
entry.bytes[offset] = static_cast<u8>(value & 0xff);
|
|
entry.bytes[offset + 1] = static_cast<u8>(value >> 8);
|
|
}
|
|
|
|
u16 readLe16(const EntityDispatchEntryBase& entry, u16 offset)
|
|
{
|
|
return static_cast<u16>(entry.bytes[offset] | (static_cast<u16>(entry.bytes[offset + 1]) << 8));
|
|
}
|
|
|
|
void clearBytes(EntityDispatchEntryBase& entry, u16 begin, u16 end)
|
|
{
|
|
for (u16 offset = begin; offset < end; ++offset) {
|
|
entry.bytes[offset] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
EntityDispatchEntryBase* ProcessKernel::createProcess(u8 activeGroup, u16 (&groupCounts)[32])
|
|
{
|
|
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
|
|
throw std::overflow_error("process table exhausted");
|
|
}
|
|
if (std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
|
|
processes_.reserve(processes_.size() + 30);
|
|
allocationSizes_.reserve(allocationSizes_.size() + 30);
|
|
processNames_.reserve(processNames_.size() + 30);
|
|
}
|
|
ownedProcesses_.reserve(ownedProcesses_.size() + 1);
|
|
auto entry = std::make_unique<EntityDispatchEntryBase>();
|
|
EntityDispatchEntry::initBase(entry.get(), activeGroup, groupCounts);
|
|
setLastAllocationSize(sizeof(EntityDispatchEntryBase));
|
|
const u16 id = addProcess(*entry);
|
|
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_) {
|
|
entry->bytes[0x16] |= 0x40;
|
|
}
|
|
}
|
|
EntityDispatchEntryBase* result = entry.get();
|
|
ownedProcesses_.push_back(std::move(entry));
|
|
return result;
|
|
}
|
|
|
|
StandProcessEntry* ProcessKernel::createStandProcess(u8 activeGroup, u16 npcNumber, u16 (&groupCounts)[32])
|
|
{
|
|
if (npcNumber > maxTrackedItemNumber) {
|
|
throw std::out_of_range("NPC item number exceeds process lookup tables");
|
|
}
|
|
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
|
|
throw std::overflow_error("process table exhausted");
|
|
}
|
|
if (std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
|
|
processes_.reserve(processes_.size() + 30);
|
|
allocationSizes_.reserve(allocationSizes_.size() + 30);
|
|
processNames_.reserve(processNames_.size() + 30);
|
|
}
|
|
ownedStandProcesses_.reserve(ownedStandProcesses_.size() + 1);
|
|
auto process = std::make_unique<StandProcessEntry>();
|
|
EntityDispatchEntry::initBase(&process->base, activeGroup, groupCounts);
|
|
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x300));
|
|
writeLe16(process->base, 0x04, npcNumber);
|
|
writeLe16(process->base, 0x06, 0x258);
|
|
writeLe16(process->base, 0x00, StandProcess::vtableOffset);
|
|
process->extra[0x00] |= 0x01;
|
|
process->extra[0x01] |= 0x01;
|
|
process->extra[0x0e] = static_cast<u8>(npcNumber);
|
|
process->extra[0x0f] = static_cast<u8>(npcNumber >> 8);
|
|
setLastAllocationSize(sizeof(StandProcessEntry));
|
|
const u16 id = addProcess(process->base);
|
|
processNames_[id] = "Stand";
|
|
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;
|
|
}
|
|
}
|
|
StandProcessEntry* result = process.get();
|
|
ownedStandProcesses_.push_back(std::move(process));
|
|
const std::size_t itemByte = npcNumber >> 3;
|
|
if (itemProcessPresence_.size() <= itemByte) {
|
|
itemProcessPresence_.resize(itemByte + 1, 0);
|
|
}
|
|
itemProcessPresence_[itemByte] |= static_cast<u8>(1u << (npcNumber & 7));
|
|
return result;
|
|
}
|
|
|
|
EntityDispatchEntryBase* ProcessKernel::createItemProcess(u8 activeGroup, u16 itemNumber, u16 (&groupCounts)[32])
|
|
{
|
|
if (itemNumber == 0) {
|
|
throw std::invalid_argument("item process needs an item number");
|
|
}
|
|
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
|
|
throw std::overflow_error("process table exhausted");
|
|
}
|
|
ownedProcesses_.reserve(ownedProcesses_.size() + 1);
|
|
auto process = std::make_unique<EntityDispatchEntryBase>();
|
|
EntityDispatchEntry::initBase(process.get(), activeGroup, groupCounts);
|
|
writeLe16(*process, 0x00, ItemProcess::vtableOffset);
|
|
setProcessSourceType(*process, itemNumber);
|
|
setProcessEventTypeChecked(*process, 0xf1);
|
|
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) {
|
|
throw std::logic_error("system timer process already exists");
|
|
}
|
|
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
|
|
throw std::overflow_error("process table exhausted");
|
|
}
|
|
auto process = std::make_unique<SystemTimerProcessEntry>();
|
|
EntityDispatchEntry::initBase(&process->base, activeGroup, groupCounts);
|
|
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x300));
|
|
process->extra[0] = 0x02;
|
|
process->extra[4] = 0x3c;
|
|
setLastAllocationSize(sizeof(SystemTimerProcessEntry));
|
|
const u16 id = addProcess(process->base);
|
|
processNames_[id] = "SystemTimer";
|
|
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x2403));
|
|
systemTimerProcessId_ = id;
|
|
systemTimerProcess_ = std::move(process);
|
|
return systemTimerProcess_.get();
|
|
}
|
|
|
|
KeyboardProcessEntry* ProcessKernel::createKeyboardProcess(u8 activeGroup, u16 (&groupCounts)[32])
|
|
{
|
|
if (keyboardProcess_ != nullptr) {
|
|
throw std::logic_error("keyboard process already exists");
|
|
}
|
|
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
|
|
throw std::overflow_error("process table exhausted");
|
|
}
|
|
auto process = std::make_unique<KeyboardProcessEntry>();
|
|
EntityDispatchEntry::initBase(&process->base, activeGroup, groupCounts);
|
|
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x103));
|
|
setLastAllocationSize(sizeof(KeyboardProcessEntry));
|
|
const u16 id = addProcess(process->base);
|
|
processNames_[id] = "KeyboardProcess";
|
|
writeLe16(process->base, 0x18, static_cast<u16>(readLe16(process->base, 0x18) | 0x180));
|
|
keyboardProcess_ = std::move(process);
|
|
return keyboardProcess_.get();
|
|
}
|
|
|
|
KeyDaemonProcessEntry* ProcessKernel::createKeyDaemonProcess(u8 activeGroup, u16 (&groupCounts)[32])
|
|
{
|
|
if (keyDaemonProcess_ != nullptr) {
|
|
throw std::logic_error("key daemon process already exists");
|
|
}
|
|
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
|
|
throw std::overflow_error("process table exhausted");
|
|
}
|
|
auto process = std::make_unique<KeyDaemonProcessEntry>();
|
|
EntityDispatchEntry::initBase(&process->base, activeGroup, groupCounts);
|
|
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x300));
|
|
process->extra[0] = 0x02;
|
|
process->extra[4] = 0x2d;
|
|
setLastAllocationSize(sizeof(KeyDaemonProcessEntry));
|
|
const u16 id = addProcess(process->base);
|
|
processNames_[id] = "KeyDaemon";
|
|
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x2403));
|
|
keyDaemonProcess_ = std::move(process);
|
|
return keyDaemonProcess_.get();
|
|
}
|
|
|
|
void ProcessKernel::deliverSystemTimerEvent()
|
|
{
|
|
if (systemTimerProcess_ == nullptr) {
|
|
throw std::logic_error("system timer process is not registered");
|
|
}
|
|
if (processAt(systemTimerProcessId_) != &systemTimerProcess_->base ||
|
|
(readLe16(systemTimerProcess_->base, 0x16) & 0x400) == 0) {
|
|
throw std::logic_error("system timer event registration is invalid");
|
|
}
|
|
SystemTimerProcess::increment(*systemTimerProcess_);
|
|
}
|
|
|
|
u32 ProcessKernel::systemTimerTick() const
|
|
{
|
|
if (systemTimerProcess_ == nullptr) {
|
|
throw std::logic_error("system timer process is not registered");
|
|
}
|
|
return SystemTimerProcess::tick(*systemTimerProcess_);
|
|
}
|
|
|
|
void ProcessKernel::setSyncContext(bool enabled, u16 currentProcessId)
|
|
{
|
|
syncEnabled_ = enabled;
|
|
currentProcessId_ = currentProcessId;
|
|
}
|
|
|
|
void ProcessKernel::setMarkNewProcesses(bool enabled)
|
|
{
|
|
markNewProcesses_ = enabled;
|
|
}
|
|
|
|
void ProcessKernel::setActiveGroup(u8 groupId)
|
|
{
|
|
if (groupId == 0 || groupId > 31) {
|
|
throw std::invalid_argument("active process group must be in 1..31");
|
|
}
|
|
if (activeGroup_ != groupId) {
|
|
activeGroup_ = groupId;
|
|
groupSelectionPending_ = true;
|
|
}
|
|
}
|
|
|
|
u8 ProcessKernel::activeGroup() const
|
|
{
|
|
return activeGroup_;
|
|
}
|
|
|
|
bool ProcessKernel::groupSelectionPending() const
|
|
{
|
|
return groupSelectionPending_;
|
|
}
|
|
|
|
void ProcessKernel::refreshActiveGroup(const u16 (&groupCounts)[32])
|
|
{
|
|
if (groupCounts[activeGroup_] != 0) {
|
|
return;
|
|
}
|
|
u8 highestGroup = 1;
|
|
for (const EntityDispatchEntryBase* entry : processes_) {
|
|
if (entry != nullptr) {
|
|
const u8 groupId = entry->bytes[0x08] & 0x1f;
|
|
if (groupId > highestGroup && groupId != 30 && groupId != 31) {
|
|
highestGroup = groupId;
|
|
}
|
|
}
|
|
}
|
|
setActiveGroup(highestGroup);
|
|
groupSelectionPending_ = false;
|
|
}
|
|
|
|
void ProcessKernel::setLastAllocationSize(u16 size)
|
|
{
|
|
lastAllocationSize_ = size;
|
|
}
|
|
|
|
u16 ProcessKernel::addProcess(EntityDispatchEntryBase& entry)
|
|
{
|
|
auto freeSlot = std::find(processes_.begin(), processes_.end(), nullptr);
|
|
if (freeSlot == processes_.end()) {
|
|
if (processes_.size() + 30 > 1023) {
|
|
throw std::overflow_error("process table exhausted");
|
|
}
|
|
const std::size_t oldCapacity = processes_.size();
|
|
processes_.resize(oldCapacity + 30, nullptr);
|
|
allocationSizes_.resize(oldCapacity + 30, 0);
|
|
processNames_.resize(oldCapacity + 30, nullptr);
|
|
freeSlot = processes_.begin() + oldCapacity;
|
|
}
|
|
const u16 id = static_cast<u16>(freeSlot - processes_.begin());
|
|
*freeSlot = &entry;
|
|
entry.bytes[0x02] = static_cast<u8>(id & 0xff);
|
|
entry.bytes[0x03] = static_cast<u8>(id >> 8);
|
|
allocationSizes_[id] = lastAllocationSize_;
|
|
lastAllocationSize_ = 0;
|
|
++processCount_;
|
|
if (markNewProcesses_) {
|
|
++newlyCreatedCount_;
|
|
entry.bytes[0x19] |= 0x02;
|
|
}
|
|
processNames_[id] = "Default";
|
|
return id;
|
|
}
|
|
|
|
void ProcessKernel::clearProcessSlot(u16 id)
|
|
{
|
|
if (id >= processes_.size()) {
|
|
throw std::out_of_range("process ID outside table");
|
|
}
|
|
processes_[id] = nullptr;
|
|
allocationSizes_[id] = 0;
|
|
processNames_[id] = nullptr;
|
|
}
|
|
|
|
void ProcessKernel::releaseOwnedProcess(u16 id)
|
|
{
|
|
EntityDispatchEntryBase* entry = processes_[id];
|
|
clearProcessSlot(id);
|
|
const auto stand = std::find_if(ownedStandProcesses_.begin(), ownedStandProcesses_.end(),
|
|
[entry](const auto& process) { return &process->base == entry; });
|
|
if (stand != ownedStandProcesses_.end()) {
|
|
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);
|
|
}
|
|
|
|
void ProcessKernel::terminateProcess(u16 id, u16 (&groupCounts)[32])
|
|
{
|
|
EntityDispatchEntryBase* entry = processAt(id);
|
|
if (id == 0 || entry == nullptr) {
|
|
throw std::out_of_range("process ID is not addressable");
|
|
}
|
|
const u16 itemNumber = readLe16(*entry, 0x04);
|
|
const u16 eventType = readLe16(*entry, 0x06);
|
|
if (systemTimerProcess_ != nullptr && entry == &systemTimerProcess_->base) {
|
|
throw std::logic_error("system timer teardown is not ported");
|
|
}
|
|
if (keyboardProcess_ != nullptr && entry == &keyboardProcess_->base) {
|
|
throw std::logic_error("keyboard process teardown is not ported");
|
|
}
|
|
if (keyDaemonProcess_ != nullptr && entry == &keyDaemonProcess_->base) {
|
|
throw std::logic_error("key daemon process teardown is not ported");
|
|
}
|
|
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 && !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() && !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");
|
|
}
|
|
const bool immediate = (entry->bytes[0x18] & 0x04) != 0;
|
|
if (!immediate && (entry->bytes[0x16] & 0x08) != 0) {
|
|
throw std::logic_error("process is already terminated");
|
|
}
|
|
--groupCounts[entry->bytes[0x08] & 0x1f];
|
|
if (immediate) {
|
|
releaseOwnedProcess(id);
|
|
} else {
|
|
entry->bytes[0x16] |= 0x08;
|
|
++terminatedProcessCount_;
|
|
}
|
|
--processCount_;
|
|
refreshItemPresence(itemNumber);
|
|
refreshItemProcessType(itemNumber, eventType);
|
|
}
|
|
|
|
void ProcessKernel::refreshItemPresence(u16 itemNumber)
|
|
{
|
|
if (itemNumber == 0) {
|
|
return;
|
|
}
|
|
const bool present = processes_.size() > 3 && std::any_of(processes_.begin() + 3, processes_.end(),
|
|
[itemNumber](const EntityDispatchEntryBase* process) {
|
|
return process != nullptr && readLe16(*process, 0x04) == itemNumber &&
|
|
(readLe16(*process, 0x16) & 0x08) == 0;
|
|
});
|
|
const std::size_t itemByte = itemNumber >> 3;
|
|
if (itemProcessPresence_.size() <= itemByte) {
|
|
itemProcessPresence_.resize(itemByte + 1, 0);
|
|
}
|
|
if (present) {
|
|
itemProcessPresence_[itemByte] |= static_cast<u8>(1u << (itemNumber & 7));
|
|
} else {
|
|
itemProcessPresence_[itemByte] &= static_cast<u8>(~(1u << (itemNumber & 7)));
|
|
}
|
|
}
|
|
|
|
void ProcessKernel::refreshItemProcessType(u16 itemNumber, u16 eventType)
|
|
{
|
|
if (itemNumber == 0 || eventType < 0xf0 || eventType > 0xf7) {
|
|
return;
|
|
}
|
|
const bool present = processes_.size() > 3 && std::any_of(processes_.begin() + 3, processes_.end(),
|
|
[itemNumber, eventType](const EntityDispatchEntryBase* process) {
|
|
return process != nullptr && readLe16(*process, 0x04) == itemNumber &&
|
|
readLe16(*process, 0x06) == eventType && (readLe16(*process, 0x16) & 0x08) == 0;
|
|
});
|
|
if (itemProcessTypes_.size() <= itemNumber) {
|
|
itemProcessTypes_.resize(static_cast<std::size_t>(itemNumber) + 1, 0);
|
|
}
|
|
const u8 bit = static_cast<u8>(1u << (eventType - 0xf0));
|
|
if (present) {
|
|
itemProcessTypes_[itemNumber] |= bit;
|
|
} else {
|
|
itemProcessTypes_[itemNumber] &= static_cast<u8>(~bit);
|
|
}
|
|
}
|
|
|
|
void ProcessKernel::setProcessSourceType(EntityDispatchEntryBase& entry, u16 itemNumber)
|
|
{
|
|
if (itemNumber > maxTrackedItemNumber) {
|
|
throw std::out_of_range("item number exceeds process lookup tables");
|
|
}
|
|
const u16 previousItem = readLe16(entry, 0x04);
|
|
const u16 eventType = readLe16(entry, 0x06);
|
|
EntityDispatchEntry::setSourceType(entry, itemNumber);
|
|
if (itemNumber != 0) {
|
|
const std::size_t itemByte = itemNumber >> 3;
|
|
if (itemProcessPresence_.size() <= itemByte) {
|
|
itemProcessPresence_.resize(itemByte + 1, 0);
|
|
}
|
|
itemProcessPresence_[itemByte] |= static_cast<u8>(1u << (itemNumber & 7));
|
|
if (eventType >= 0xf0 && eventType <= 0xf7) {
|
|
if (itemProcessTypes_.size() <= itemNumber) {
|
|
itemProcessTypes_.resize(static_cast<std::size_t>(itemNumber) + 1, 0);
|
|
}
|
|
itemProcessTypes_[itemNumber] |= static_cast<u8>(1u << (eventType - 0xf0));
|
|
}
|
|
}
|
|
if (previousItem != 0 && previousItem != itemNumber) {
|
|
refreshItemPresence(previousItem);
|
|
refreshItemProcessType(previousItem, eventType);
|
|
}
|
|
}
|
|
|
|
void ProcessKernel::setProcessEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType)
|
|
{
|
|
const u16 previousType = readLe16(entry, 0x06);
|
|
const u16 itemNumber = readLe16(entry, 0x04);
|
|
EntityDispatchEntry::setEventTypeChecked(entry, eventType);
|
|
if (itemNumber != 0 && eventType >= 0xf0 && eventType <= 0xf7) {
|
|
if (itemProcessTypes_.size() <= itemNumber) {
|
|
itemProcessTypes_.resize(static_cast<std::size_t>(itemNumber) + 1, 0);
|
|
}
|
|
itemProcessTypes_[itemNumber] |= static_cast<u8>(1u << (eventType - 0xf0));
|
|
}
|
|
if (previousType != eventType) {
|
|
refreshItemProcessType(itemNumber, previousType);
|
|
}
|
|
}
|
|
|
|
void ProcessKernel::clearTerminatedProcesses()
|
|
{
|
|
for (std::size_t id = 3; id < processes_.size() && terminatedProcessCount_ != 0; ++id) {
|
|
EntityDispatchEntryBase* entry = processes_[id];
|
|
if (entry == nullptr || (entry->bytes[0x16] & 0x08) == 0) {
|
|
continue;
|
|
}
|
|
if (entry->bytes[0x1a] != 0 || entry->bytes[0x1b] != 0) {
|
|
throw std::logic_error("terminated process has linked state");
|
|
}
|
|
releaseOwnedProcess(static_cast<u16>(id));
|
|
--terminatedProcessCount_;
|
|
}
|
|
}
|
|
|
|
void ProcessKernel::scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDispatchEntryBase&, void*), void* context, u32 timerTick)
|
|
{
|
|
if (run == nullptr) {
|
|
throw std::invalid_argument("process run slot is required");
|
|
}
|
|
setActiveGroup(activeGroup);
|
|
const bool priorSyncEnabled = syncEnabled_;
|
|
const u16 priorProcessId = currentProcessId_;
|
|
syncEnabled_ = true;
|
|
try {
|
|
for (std::size_t id = 0; id < processes_.size(); ++id) {
|
|
EntityDispatchEntryBase* entry = processes_[id];
|
|
if (entry != nullptr && (readLe16(*entry, 0x16) & 0x08) == 0) {
|
|
if ((readLe16(*entry, 0x18) & 0x1000) != 0) {
|
|
const u32 sleepStart = readLe16(*entry, 0x0a) | (static_cast<u32>(readLe16(*entry, 0x0c)) << 16);
|
|
const u32 sleepLength = readLe16(*entry, 0x0e) | (static_cast<u32>(readLe16(*entry, 0x10)) << 16);
|
|
if (sleepStart + sleepLength < timerTick) {
|
|
if ((readLe16(*entry, 0x16) & 0x100) != 0) {
|
|
const auto stand = std::find_if(ownedStandProcesses_.begin(), ownedStandProcesses_.end(),
|
|
[entry](const auto& process) { return &process->base == entry; });
|
|
if (stand == ownedStandProcesses_.end() ||
|
|
readLe16(*entry, 0x00) != StandProcess::vtableOffset) {
|
|
throw std::logic_error("sleep completion dispatch is not ported");
|
|
}
|
|
}
|
|
EntityDispatchEntry::unlinkSleep(*entry);
|
|
}
|
|
}
|
|
if ((readLe16(*entry, 0x16) & 0x40) != 0) {
|
|
writeLe16(*entry, 0x16, static_cast<u16>(readLe16(*entry, 0x16) & ~0x40));
|
|
} else {
|
|
const u8 groupId = entry->bytes[0x08] & 0x1f;
|
|
const u16 flags = readLe16(*entry, 0x16);
|
|
if ((readLe16(*entry, 0x18) & 0x1000) == 0 && readLe16(*entry, 0x24) == 0 &&
|
|
readLe16(*entry, 0x26) == 0 && (flags & 1) == 0 &&
|
|
((flags & 2) != 0 || groupId == activeGroup_ || groupId == 30 || groupId == 31)) {
|
|
currentProcessId_ = static_cast<u16>(id);
|
|
run(*entry, context);
|
|
currentProcessId_ = 0;
|
|
}
|
|
}
|
|
}
|
|
clearTerminatedProcesses();
|
|
}
|
|
} catch (...) {
|
|
syncEnabled_ = priorSyncEnabled;
|
|
currentProcessId_ = priorProcessId;
|
|
throw;
|
|
}
|
|
syncEnabled_ = priorSyncEnabled;
|
|
currentProcessId_ = priorProcessId;
|
|
}
|
|
|
|
u32 ProcessKernel::runTimedSchedulerPass(u8 activeGroup, void (*run)(EntityDispatchEntryBase&, void*), void* context,
|
|
void (*afterScan)(void*), void* afterContext)
|
|
{
|
|
const u32 startTick = systemTimerTick();
|
|
scanRunnableProcesses(activeGroup, run, context, startTick);
|
|
if (afterScan != nullptr) {
|
|
afterScan(afterContext);
|
|
}
|
|
return systemTimerTick() - startTick;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
const char* ProcessKernel::processNameAt(u16 id) const
|
|
{
|
|
return id < processNames_.size() ? processNames_[id] : nullptr;
|
|
}
|
|
|
|
bool ProcessKernel::itemHasProcess(u16 itemNumber) const
|
|
{
|
|
const std::size_t itemByte = itemNumber >> 3;
|
|
return itemByte < itemProcessPresence_.size() &&
|
|
(itemProcessPresence_[itemByte] & (1u << (itemNumber & 7))) != 0;
|
|
}
|
|
|
|
u8 ProcessKernel::itemProcessTypes(u16 itemNumber) const
|
|
{
|
|
return itemNumber < itemProcessTypes_.size() ? itemProcessTypes_[itemNumber] : 0;
|
|
}
|
|
|
|
u16 ProcessKernel::processCount() const
|
|
{
|
|
return processCount_;
|
|
}
|
|
|
|
u16 ProcessKernel::terminatedProcessCount() const
|
|
{
|
|
return terminatedProcessCount_;
|
|
}
|
|
|
|
u16 ProcessKernel::newlyCreatedCount() const
|
|
{
|
|
return newlyCreatedCount_;
|
|
}
|
|
|
|
std::size_t ProcessKernel::capacity() const
|
|
{
|
|
return processes_.size();
|
|
}
|
|
|
|
EntityDispatchEntryBase* EntityDispatchEntry::initBase(EntityDispatchEntryBase* entry, u8 activeGroup, u16 (&groupCounts)[32])
|
|
{
|
|
if (entry == nullptr) {
|
|
return nullptr;
|
|
}
|
|
|
|
writeLe16(*entry, 0x00, 0x3b06);
|
|
writeLe16(*entry, 0x1e, 0x2d10);
|
|
writeLe16(*entry, 0x28, 0x3afe);
|
|
clearBytes(*entry, 0x02, 0x06);
|
|
writeLe16(*entry, 0x06, 1);
|
|
entry->bytes[0x08] = 0;
|
|
clearBytes(*entry, 0x0a, 0x1e);
|
|
writeLe16(*entry, 0x18, 1);
|
|
clearBytes(*entry, 0x20, 0x28);
|
|
clearBytes(*entry, 0x2a, 0x32);
|
|
setGroupId(*entry, activeGroup, groupCounts);
|
|
return entry;
|
|
}
|
|
|
|
void EntityDispatchEntry::setGroupId(EntityDispatchEntryBase& entry, u8 groupId, u16 (&groupCounts)[32])
|
|
{
|
|
if (groupId == 0 || groupId > 31) {
|
|
throw std::invalid_argument("process group ID must be in 1..31");
|
|
}
|
|
const u8 oldGroupId = entry.bytes[groupIdOffset] & 0x1f;
|
|
if (oldGroupId == groupId) {
|
|
return;
|
|
}
|
|
if (oldGroupId != 0) {
|
|
--groupCounts[oldGroupId];
|
|
}
|
|
entry.bytes[groupIdOffset] = groupId;
|
|
++groupCounts[groupId];
|
|
}
|
|
|
|
void EntityDispatchEntry::incrementGroupId(EntityDispatchEntryBase& entry, ProcessKernel& kernel, u16 (&groupCounts)[32])
|
|
{
|
|
const u8 previousGroup = entry.bytes[groupIdOffset] & 0x1f;
|
|
const u8 nextGroup = static_cast<u8>(previousGroup + 1);
|
|
if (nextGroup >= 30 || kernel.activeGroup() != previousGroup) {
|
|
throw std::invalid_argument("process group cannot advance from the current active group");
|
|
}
|
|
setGroupId(entry, nextGroup, groupCounts);
|
|
kernel.setActiveGroup(nextGroup);
|
|
}
|
|
|
|
void EntityDispatchEntry::setSourceType(EntityDispatchEntryBase& entry, u16 sourceType)
|
|
{
|
|
writeLe16(entry, sourceItemOffset, sourceType);
|
|
}
|
|
|
|
void EntityDispatchEntry::setEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType)
|
|
{
|
|
writeLe16(entry, eventTypeOffset, eventType);
|
|
if (eventType > 0x0fff) {
|
|
throw std::invalid_argument("process event type exceeds 0x0fff");
|
|
}
|
|
}
|
|
|
|
void EntityDispatchEntry::sleep(EntityDispatchEntryBase& entry, u32 timerTick, u32 duration)
|
|
{
|
|
writeLe16(entry, 0x18, static_cast<u16>(readLe16(entry, 0x18) | 0x1000));
|
|
writeLe16(entry, 0x0a, static_cast<u16>(timerTick));
|
|
writeLe16(entry, 0x0c, static_cast<u16>(timerTick >> 16));
|
|
writeLe16(entry, 0x0e, static_cast<u16>(duration));
|
|
writeLe16(entry, 0x10, static_cast<u16>(duration >> 16));
|
|
}
|
|
|
|
void EntityDispatchEntry::unlinkSleep(EntityDispatchEntryBase& entry)
|
|
{
|
|
writeLe16(entry, 0x18, static_cast<u16>(readLe16(entry, 0x18) & ~0x1000));
|
|
clearBytes(entry, 0x0a, 0x12);
|
|
}
|
|
|
|
void StandProcess::run(EntityDispatchEntryBase& entry,
|
|
bool (*startBehavior)(const EntityDispatchEntryBase&, void*), void* context, u32 timerTick)
|
|
{
|
|
if (startBehavior == nullptr) {
|
|
throw std::invalid_argument("NPC behavior boundary is required");
|
|
}
|
|
if (!startBehavior(entry, context)) {
|
|
EntityDispatchEntry::sleep(entry, timerTick, 0x3c);
|
|
}
|
|
}
|
|
|
|
void StandProcess::run(StandProcessEntry& entry, const NpcBehaviorServices& services, u32 timerTick)
|
|
{
|
|
if (services.isEntirelyOnScreen == nullptr || services.getActivity == nullptr ||
|
|
services.isCurrentActivitySpecifiedBy == nullptr || services.canSeeControlledNPC == nullptr ||
|
|
services.startNextBehavior == nullptr) {
|
|
throw std::invalid_argument("NPC behavior services are required");
|
|
}
|
|
const u16 npcNumber = static_cast<u16>(entry.extra[0x0e] | (static_cast<u16>(entry.extra[0x0f]) << 8));
|
|
bool started = false;
|
|
if (services.isEntirelyOnScreen(npcNumber, services.context) &&
|
|
services.getActivity(npcNumber, services.context) != 0 &&
|
|
!services.isCurrentActivitySpecifiedBy(npcNumber, 0x71, services.context) &&
|
|
services.canSeeControlledNPC(npcNumber, services.context)) {
|
|
services.startNextBehavior(npcNumber, 0x71, services.context);
|
|
started = true;
|
|
}
|
|
run(entry.base, [](const EntityDispatchEntryBase&, void* context) {
|
|
return *static_cast<bool*>(context);
|
|
}, &started, timerTick);
|
|
}
|
|
|
|
void ItemProcess::run(EntityDispatchEntryBase& entry, ProcessKernel& kernel, u16 (&groupCounts)[32],
|
|
void (*release)(u16 itemNumber, u32 eventMask, u16 eventId, void*), void* context)
|
|
{
|
|
if (release == nullptr) {
|
|
throw std::invalid_argument("item release boundary is required");
|
|
}
|
|
const u16 itemNumber = readLe16(entry, 0x04);
|
|
release(itemNumber, 0x200, 9, context);
|
|
kernel.terminateProcess(readLe16(entry, 0x02), groupCounts);
|
|
}
|
|
|
|
u32 SystemTimerProcess::tick(const SystemTimerProcessEntry& entry)
|
|
{
|
|
return static_cast<u32>(entry.extra[0x0e] | (static_cast<u16>(entry.extra[0x0f]) << 8)) |
|
|
(static_cast<u32>(entry.extra[0x10] | (static_cast<u16>(entry.extra[0x11]) << 8)) << 16);
|
|
}
|
|
|
|
void SystemTimerProcess::increment(SystemTimerProcessEntry& entry)
|
|
{
|
|
const u32 nextTick = tick(entry) + 1;
|
|
entry.extra[0x0e] = static_cast<u8>(nextTick);
|
|
entry.extra[0x0f] = static_cast<u8>(nextTick >> 8);
|
|
entry.extra[0x10] = static_cast<u8>(nextTick >> 16);
|
|
entry.extra[0x11] = static_cast<u8>(nextTick >> 24);
|
|
}
|
|
|
|
bool KeyboardProcess::handleKeyEvent(KeyboardProcessEntry& process, u16 selector, u16 filter,
|
|
KeyEvent& event, const KeyEvent& lastEvent)
|
|
{
|
|
if (selector != 0x10 || filter != 0 || event.character == 0 || event.field8 == 0) {
|
|
return false;
|
|
}
|
|
std::memcpy(&event, &lastEvent, sizeof(event));
|
|
process.extra[0] &= static_cast<u8>(~0x10);
|
|
return true;
|
|
}
|
|
|
|
} // namespace remorse
|