Add comprehensive tests for entity dispatch, file system, fixed map, flex archive, isometric controls, and crash setup
- Enhance `entity_dispatch_entry_tests.cpp` with additional assertions for event type checks, group ID increments, and error handling. - Refactor `file_system_tests.cpp` to streamline probe execution and enhance output validation. - Extend `fixed_map_tests.cpp` to cover retail map loading, item classification, and process creation scenarios. - Introduce new checks in `flex_archive_tests.cpp` for usecode loading and event handling, ensuring robust validation of game data. - Add tests in `isometric_controls_tests.cpp` for controller direction mapping based on scan codes. - Implement `test_crash_setup.cpp` to initialize crash diagnostics for improved error handling during tests.
This commit is contained in:
parent
202679f857
commit
c346b0e1a7
17 changed files with 1207 additions and 92 deletions
|
|
@ -17,6 +17,7 @@ add_library(remorse_core
|
|||
src/runtime.cpp
|
||||
src/shape_archive.cpp
|
||||
src/type_flags.cpp
|
||||
src/usecode_archive.cpp
|
||||
)
|
||||
|
||||
target_compile_features(remorse_core PUBLIC cxx_std_17)
|
||||
|
|
@ -155,6 +156,16 @@ target_link_libraries(combat_tactics_tests PRIVATE remorse_core)
|
|||
|
||||
add_test(NAME combat_tactics_tests COMMAND combat_tactics_tests)
|
||||
|
||||
add_library(remorse_test_crash_setup OBJECT tests/test_crash_setup.cpp)
|
||||
target_link_libraries(remorse_test_crash_setup PRIVATE remorse_core)
|
||||
foreach(test_target IN ITEMS
|
||||
entity_dispatch_entry_tests file_system_tests palette_tests flex_archive_tests
|
||||
fixed_map_tests glob_archive_tests type_flags_tests shape_archive_tests
|
||||
map_renderer_tests map_sorter_tests isometric_controls_tests
|
||||
animation_archive_tests collision_tests combat_tactics_tests)
|
||||
target_sources(${test_target} PRIVATE $<TARGET_OBJECTS:remorse_test_crash_setup>)
|
||||
endforeach()
|
||||
|
||||
if(MSVC)
|
||||
target_compile_options(remorse_core PRIVATE /W4 /permissive-)
|
||||
target_compile_options(combat_tactics_tests PRIVATE /W4 /permissive-)
|
||||
|
|
|
|||
|
|
@ -67,8 +67,10 @@ struct NpcBehaviorServices {
|
|||
|
||||
class ProcessKernel {
|
||||
public:
|
||||
static constexpr u16 maxTrackedItemNumber = 0x23ff;
|
||||
EntityDispatchEntryBase* createProcess(u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
StandProcessEntry* createStandProcess(u8 activeGroup, u16 npcNumber, u16 (&groupCounts)[32]);
|
||||
EntityDispatchEntryBase* createItemProcess(u8 activeGroup, u16 itemNumber, u16 (&groupCounts)[32]);
|
||||
SystemTimerProcessEntry* createSystemTimerProcess(u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
KeyboardProcessEntry* createKeyboardProcess(u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
KeyDaemonProcessEntry* createKeyDaemonProcess(u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
|
|
@ -76,16 +78,25 @@ public:
|
|||
u32 systemTimerTick() const;
|
||||
void setSyncContext(bool enabled, u16 currentProcessId);
|
||||
void setMarkNewProcesses(bool enabled);
|
||||
void setActiveGroup(u8 groupId);
|
||||
u8 activeGroup() const;
|
||||
bool groupSelectionPending() const;
|
||||
void refreshActiveGroup(const u16 (&groupCounts)[32]);
|
||||
void setLastAllocationSize(u16 size);
|
||||
u16 addProcess(EntityDispatchEntryBase& entry);
|
||||
void clearProcessSlot(u16 id);
|
||||
void terminateProcess(u16 id, u16 (&groupCounts)[32]);
|
||||
void clearTerminatedProcesses();
|
||||
void setProcessSourceType(EntityDispatchEntryBase& entry, u16 itemNumber);
|
||||
void setProcessEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType);
|
||||
void scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDispatchEntryBase&, void*), void* context, u32 timerTick = 0);
|
||||
u32 runTimedSchedulerPass(u8 activeGroup, void (*run)(EntityDispatchEntryBase&, void*), void* context,
|
||||
void (*afterScan)(void*) = nullptr, void* afterContext = nullptr);
|
||||
EntityDispatchEntryBase* processAt(u16 id) const;
|
||||
u16 allocationSizeAt(u16 id) const;
|
||||
const char* processNameAt(u16 id) const;
|
||||
bool itemHasProcess(u16 itemNumber) const;
|
||||
u8 itemProcessTypes(u16 itemNumber) const;
|
||||
u16 processCount() const;
|
||||
u16 terminatedProcessCount() const;
|
||||
u16 newlyCreatedCount() const;
|
||||
|
|
@ -93,6 +104,8 @@ public:
|
|||
|
||||
private:
|
||||
void releaseOwnedProcess(u16 id);
|
||||
void refreshItemPresence(u16 itemNumber);
|
||||
void refreshItemProcessType(u16 itemNumber, u16 eventType);
|
||||
std::vector<EntityDispatchEntryBase*> processes_;
|
||||
std::vector<u16> allocationSizes_;
|
||||
std::vector<const char*> processNames_;
|
||||
|
|
@ -103,11 +116,14 @@ private:
|
|||
std::unique_ptr<KeyDaemonProcessEntry> keyDaemonProcess_;
|
||||
u16 systemTimerProcessId_ = 0xffff;
|
||||
std::vector<u8> itemProcessPresence_;
|
||||
std::vector<u8> itemProcessTypes_;
|
||||
u16 lastAllocationSize_ = 0;
|
||||
u16 processCount_ = 0;
|
||||
u16 terminatedProcessCount_ = 0;
|
||||
u16 newlyCreatedCount_ = 0;
|
||||
u16 currentProcessId_ = 0;
|
||||
u8 activeGroup_ = 1;
|
||||
bool groupSelectionPending_ = false;
|
||||
bool syncEnabled_ = false;
|
||||
bool markNewProcesses_ = false;
|
||||
};
|
||||
|
|
@ -119,7 +135,7 @@ public:
|
|||
|
||||
static EntityDispatchEntryBase* initBase(EntityDispatchEntryBase* entry, u8 activeGroup, u16 (&groupCounts)[32]);
|
||||
static void setGroupId(EntityDispatchEntryBase& entry, u8 groupId, u16 (&groupCounts)[32]);
|
||||
static void incrementGroupId(EntityDispatchEntryBase& entry);
|
||||
static void incrementGroupId(EntityDispatchEntryBase& entry, ProcessKernel& kernel, u16 (&groupCounts)[32]);
|
||||
static void setSourceType(EntityDispatchEntryBase& entry, u16 sourceType);
|
||||
static void setEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType);
|
||||
static void sleep(EntityDispatchEntryBase& entry, u32 timerTick, u32 duration);
|
||||
|
|
@ -136,6 +152,15 @@ public:
|
|||
static void run(StandProcessEntry& entry, const NpcBehaviorServices& services, u32 timerTick);
|
||||
};
|
||||
|
||||
class ItemProcess {
|
||||
public:
|
||||
static constexpr u16 runOffset = 0x0078;
|
||||
static constexpr u16 vtableOffset = 0x102f;
|
||||
|
||||
static void run(EntityDispatchEntryBase& entry, ProcessKernel& kernel, u16 (&groupCounts)[32],
|
||||
void (*release)(u16 itemNumber, u32 eventMask, u16 eventId, void*), void* context);
|
||||
};
|
||||
|
||||
class SystemTimerProcess {
|
||||
public:
|
||||
static u32 tick(const SystemTimerProcessEntry& entry);
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
namespace remorse {
|
||||
|
||||
struct ShapeTypeInfo;
|
||||
|
||||
struct FixedMapEntry {
|
||||
u16 x = 0;
|
||||
u16 y = 0;
|
||||
|
|
@ -26,6 +28,19 @@ struct FixedMap {
|
|||
std::vector<FixedMapEntry> entries;
|
||||
};
|
||||
|
||||
struct StagedFixedMapItem {
|
||||
u16 itemNumber = 0;
|
||||
std::size_t recordIndex = 0;
|
||||
FixedMapEntry data;
|
||||
};
|
||||
|
||||
struct ItemFindArea {
|
||||
u16 minX = 0;
|
||||
u16 minY = 0;
|
||||
u16 maxX = 0;
|
||||
u16 maxY = 0;
|
||||
};
|
||||
|
||||
struct FixedMapArchive {
|
||||
u32 mapCount = 0;
|
||||
std::vector<u8> bytes;
|
||||
|
|
@ -33,5 +48,13 @@ struct FixedMapArchive {
|
|||
|
||||
std::optional<FixedMapArchive> loadFixedMapArchive(const std::filesystem::path& path);
|
||||
std::optional<FixedMap> loadFixedMap(const FixedMapArchive& archive, u32 mapIndex);
|
||||
std::optional<std::vector<StagedFixedMapItem>> stageFixedMapItems(const FixedMap& map);
|
||||
std::optional<StagedFixedMapItem> nearestStagedFixedMapItem(
|
||||
const std::vector<StagedFixedMapItem>& items, s32 worldX, s32 worldY, s32 maxDistance);
|
||||
bool itemGrabCanCreateProcess(const StagedFixedMapItem& item);
|
||||
u8 classifyItemFindZMatch(const StagedFixedMapItem& origin, const ShapeTypeInfo& originShape,
|
||||
const StagedFixedMapItem& candidate, const ShapeTypeInfo& candidateShape, u8 zMatchFlags);
|
||||
ItemFindArea itemFindAreaAroundItem(const StagedFixedMapItem& origin, const ShapeTypeInfo& originShape);
|
||||
std::vector<u16> itemFindBlockOrder(const ItemFindArea& area);
|
||||
|
||||
} // namespace remorse
|
||||
|
|
|
|||
|
|
@ -17,6 +17,22 @@ inline bool controllerWalkRequested(u16 modifiers)
|
|||
return shiftDown == capsLockOn;
|
||||
}
|
||||
|
||||
inline std::optional<u8> controllerDirectionFromScan(u8 scanCode)
|
||||
{
|
||||
// Controller_PickNextAnimByKeyevent (1130:12d6), directional keys with Num Lock off.
|
||||
switch (scanCode) {
|
||||
case 0x47: return u8{12};
|
||||
case 0x48: return u8{14};
|
||||
case 0x49: return u8{0};
|
||||
case 0x4b: return u8{10};
|
||||
case 0x4d: return u8{2};
|
||||
case 0x4f: return u8{8};
|
||||
case 0x50: return u8{6};
|
||||
case 0x51: return u8{4};
|
||||
default: return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
inline std::optional<AnimationDisplacement> hostMovementStep(
|
||||
const AnimationFrameMetadata& metadata, u8 direction, bool rotateDirection)
|
||||
{
|
||||
|
|
|
|||
54
rebuild/remorse/include/remorse/usecode_archive.h
Normal file
54
rebuild/remorse/include/remorse/usecode_archive.h
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#pragma once
|
||||
|
||||
#include "remorse/flex_archive.h"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct UsecodeClass {
|
||||
u32 classId = 0;
|
||||
std::string name;
|
||||
u32 codeBase = 0;
|
||||
std::vector<u8> bytes;
|
||||
};
|
||||
|
||||
struct UsecodeEventBody {
|
||||
u16 length = 0;
|
||||
u32 relativeOffset = 0;
|
||||
std::vector<u8> bytes;
|
||||
};
|
||||
|
||||
struct UsecodeClassBases {
|
||||
u16 regularShape = 0;
|
||||
u16 npc = 0;
|
||||
u16 unknownEgg = 0;
|
||||
};
|
||||
|
||||
struct UsecodeItemIdentity {
|
||||
u16 itemNumber = 0;
|
||||
u16 shape = 0;
|
||||
u16 status = 0;
|
||||
u16 quality = 0;
|
||||
u8 family = 0;
|
||||
bool isNpc = false;
|
||||
};
|
||||
|
||||
struct EligibleUsecodeEvent {
|
||||
u16 classId = 0;
|
||||
std::string className;
|
||||
UsecodeEventBody body;
|
||||
};
|
||||
|
||||
u16 selectUsecodeClassId(const UsecodeItemIdentity& item, const UsecodeClassBases& bases);
|
||||
std::optional<std::vector<u8>> loadUsecodeOwnerRows(const FlexArchive& archive);
|
||||
bool ownerRowAllowsEvent(const std::vector<u8>& ownerRows, u16 classId, u32 eventMask);
|
||||
std::optional<UsecodeClass> loadUsecodeClass(const FlexArchive& archive, u32 classId);
|
||||
std::optional<UsecodeEventBody> readUsecodeEventBody(const UsecodeClass& usecodeClass, u32 eventId);
|
||||
std::optional<EligibleUsecodeEvent> selectEligibleUsecodeEvent(const FlexArchive& archive,
|
||||
const std::vector<u8>& ownerRows, const UsecodeItemIdentity& item,
|
||||
const UsecodeClassBases& bases, u32 eventMask, u16 eventId);
|
||||
|
||||
} // namespace remorse
|
||||
|
|
@ -61,6 +61,9 @@ EntityDispatchEntryBase* ProcessKernel::createProcess(u8 activeGroup, u16 (&grou
|
|||
|
||||
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");
|
||||
}
|
||||
|
|
@ -102,6 +105,28 @@ StandProcessEntry* ProcessKernel::createStandProcess(u8 activeGroup, u16 npcNumb
|
|||
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";
|
||||
EntityDispatchEntryBase* result = process.get();
|
||||
ownedProcesses_.push_back(std::move(process));
|
||||
return result;
|
||||
}
|
||||
|
||||
SystemTimerProcessEntry* ProcessKernel::createSystemTimerProcess(u8 activeGroup, u16 (&groupCounts)[32])
|
||||
{
|
||||
if (systemTimerProcess_ != nullptr) {
|
||||
|
|
@ -195,6 +220,45 @@ 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;
|
||||
|
|
@ -260,6 +324,7 @@ void ProcessKernel::terminateProcess(u16 id, u16 (&groupCounts)[32])
|
|||
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");
|
||||
}
|
||||
|
|
@ -271,7 +336,10 @@ 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; });
|
||||
if (itemNumber != 0 && (stand == ownedStandProcesses_.end() || id < 3 ||
|
||||
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 ||
|
||||
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");
|
||||
|
|
@ -292,15 +360,92 @@ void ProcessKernel::terminateProcess(u16 id, u16 (&groupCounts)[32])
|
|||
++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 bool stillPresent = std::any_of(processes_.begin() + 3, processes_.end(),
|
||||
[itemNumber](const EntityDispatchEntryBase* process) {
|
||||
return process != nullptr && readLe16(*process, 0x04) == itemNumber &&
|
||||
(readLe16(*process, 0x16) & 0x08) == 0;
|
||||
});
|
||||
if (!stillPresent) {
|
||||
itemProcessPresence_[itemNumber >> 3] &= static_cast<u8>(~(1u << (itemNumber & 7)));
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -324,6 +469,7 @@ void ProcessKernel::scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDisp
|
|||
if (run == nullptr) {
|
||||
throw std::invalid_argument("process run slot is required");
|
||||
}
|
||||
setActiveGroup(activeGroup);
|
||||
const bool priorSyncEnabled = syncEnabled_;
|
||||
const u16 priorProcessId = currentProcessId_;
|
||||
syncEnabled_ = true;
|
||||
|
|
@ -353,7 +499,7 @@ void ProcessKernel::scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDisp
|
|||
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)) {
|
||||
((flags & 2) != 0 || groupId == activeGroup_ || groupId == 30 || groupId == 31)) {
|
||||
currentProcessId_ = static_cast<u16>(id);
|
||||
run(*entry, context);
|
||||
currentProcessId_ = 0;
|
||||
|
|
@ -371,6 +517,17 @@ void ProcessKernel::scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDisp
|
|||
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;
|
||||
|
|
@ -393,6 +550,11 @@ bool ProcessKernel::itemHasProcess(u16 itemNumber) const
|
|||
(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_;
|
||||
|
|
@ -449,9 +611,15 @@ void EntityDispatchEntry::setGroupId(EntityDispatchEntryBase& entry, u8 groupId,
|
|||
++groupCounts[groupId];
|
||||
}
|
||||
|
||||
void EntityDispatchEntry::incrementGroupId(EntityDispatchEntryBase& entry)
|
||||
void EntityDispatchEntry::incrementGroupId(EntityDispatchEntryBase& entry, ProcessKernel& kernel, u16 (&groupCounts)[32])
|
||||
{
|
||||
++entry.bytes[groupIdOffset];
|
||||
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)
|
||||
|
|
@ -462,6 +630,9 @@ void EntityDispatchEntry::setSourceType(EntityDispatchEntryBase& entry, u16 sour
|
|||
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)
|
||||
|
|
@ -511,6 +682,17 @@ void StandProcess::run(StandProcessEntry& entry, const NpcBehaviorServices& serv
|
|||
}, &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)) |
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "remorse/fixed_map.h"
|
||||
|
||||
#include "remorse/file_system.h"
|
||||
#include "remorse/type_flags.h"
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
|
@ -116,4 +117,111 @@ std::optional<FixedMap> loadFixedMap(const FixedMapArchive& archive, u32 mapInde
|
|||
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> 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
#include "remorse/palette.h"
|
||||
#include "remorse/runtime.h"
|
||||
#include "remorse/type_flags.h"
|
||||
#include "remorse/usecode_archive.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
|
@ -27,7 +28,7 @@ int main(int argc, char** argv)
|
|||
remorse::printStartupReport(*manifest);
|
||||
remorse::ProcessKernel kernel;
|
||||
remorse::u16 groupCounts[32]{};
|
||||
const auto* entry = kernel.createProcess(1, groupCounts);
|
||||
auto* entry = kernel.createProcess(1, groupCounts);
|
||||
std::cout << "Registered process ID: " << (entry ? entry->bytes[0x02] : 0) << '\n';
|
||||
for (int index = 0; index < 3; ++index) {
|
||||
kernel.createProcess(1, groupCounts);
|
||||
|
|
@ -57,20 +58,25 @@ int main(int argc, char** argv)
|
|||
kernel.deliverSystemTimerEvent();
|
||||
}
|
||||
scanContext.timerTick = kernel.systemTimerTick();
|
||||
kernel.scanRunnableProcesses(1, [](remorse::EntityDispatchEntryBase& process, void* context) {
|
||||
const remorse::u32 schedulerElapsed = kernel.runTimedSchedulerPass(1, [](remorse::EntityDispatchEntryBase& process, void* context) {
|
||||
auto& scan = *static_cast<ScanContext*>(context);
|
||||
if (process.bytes[0] == 0x0f && process.bytes[1] == 0x1f) {
|
||||
remorse::StandProcess::run(*scan.standing, *scan.npcServices, scan.timerTick);
|
||||
} else {
|
||||
++scan.hostCallbacks;
|
||||
}
|
||||
}, &scanContext, scanContext.timerTick);
|
||||
}, &scanContext);
|
||||
kernel.refreshActiveGroup(groupCounts);
|
||||
remorse::EntityDispatchEntry::incrementGroupId(*entry, kernel, groupCounts);
|
||||
std::cout << "Advanced process group: " << static_cast<int>(kernel.activeGroup()) << '\n';
|
||||
std::cout << "Scheduler scan host callbacks: " << scanContext.hostCallbacks << '\n';
|
||||
std::cout << "Scheduler pass elapsed ticks: " << schedulerElapsed << '\n';
|
||||
std::cout << "StandProcess Run sleep ticks: " << static_cast<int>(reused->base.bytes[0x0e]) << '\n';
|
||||
|
||||
const auto gameDataRoot = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
std::cout << "Game data root: " << (gameDataRoot ? gameDataRoot->path.string() : "not found") << '\n';
|
||||
if (gameDataRoot) {
|
||||
std::optional<remorse::StagedFixedMapItem> firstStagedItem;
|
||||
const auto palettePath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "GAMEPAL.PAL");
|
||||
const auto paletteBytes = palettePath ? remorse::readBinaryFile(*palettePath) : std::nullopt;
|
||||
std::cout << "GAMEPAL.PAL bytes: " << (paletteBytes ? paletteBytes->size() : 0) << '\n';
|
||||
|
|
@ -92,6 +98,33 @@ int main(int argc, char** argv)
|
|||
if (map && !map->entries.empty()) {
|
||||
std::cout << "FIXED.DAT first non-empty map: " << index << '\n';
|
||||
std::cout << "FIXED.DAT first non-empty entries: " << map->entries.size() << '\n';
|
||||
const auto items = remorse::stageFixedMapItems(*map);
|
||||
if (items && !items->empty()) {
|
||||
firstStagedItem = items->front();
|
||||
struct ItemScanContext {
|
||||
remorse::ProcessKernel* kernel;
|
||||
remorse::u16 (*groups)[32];
|
||||
remorse::u16 releaseEventItem = 0;
|
||||
} itemScan{&kernel, &groupCounts};
|
||||
auto* itemProcess = kernel.createItemProcess(kernel.activeGroup(), items->front().itemNumber, groupCounts);
|
||||
const remorse::u16 itemProcessId = static_cast<remorse::u16>(
|
||||
itemProcess->bytes[2] | (static_cast<remorse::u16>(itemProcess->bytes[3]) << 8));
|
||||
kernel.scanRunnableProcesses(kernel.activeGroup(), [](remorse::EntityDispatchEntryBase& process, void* context) {
|
||||
auto& scan = *static_cast<ItemScanContext*>(context);
|
||||
if (process.bytes[0] == 0x2f && process.bytes[1] == 0x10) {
|
||||
remorse::ItemProcess::run(process, *scan.kernel, *scan.groups,
|
||||
[](remorse::u16 itemNumber, remorse::u32 eventMask, remorse::u16 eventId, void* releaseContext) {
|
||||
if (eventMask == 0x200 && eventId == 9) {
|
||||
static_cast<ItemScanContext*>(releaseContext)->releaseEventItem = itemNumber;
|
||||
}
|
||||
}, &scan);
|
||||
}
|
||||
}, &itemScan, kernel.systemTimerTick());
|
||||
kernel.refreshActiveGroup(groupCounts);
|
||||
std::cout << "Staged retail item shape: " << items->front().data.shape << '\n';
|
||||
std::cout << "ItemProcess release event boundary item: " << itemScan.releaseEventItem << '\n';
|
||||
std::cout << "ItemProcess terminated: " << (kernel.processAt(itemProcessId) == nullptr) << '\n';
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -104,6 +137,26 @@ int main(int argc, char** argv)
|
|||
const auto typeFlagPath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
|
||||
const auto typeFlags = typeFlagPath ? remorse::loadTypeFlags(*typeFlagPath) : std::nullopt;
|
||||
std::cout << "TYPEFLAG.DAT shapes: " << (typeFlags ? typeFlags->size() : 0) << '\n';
|
||||
if (firstStagedItem && typeFlags && firstStagedItem->data.shape < typeFlags->size()) {
|
||||
const auto area = remorse::itemFindAreaAroundItem(
|
||||
*firstStagedItem, (*typeFlags)[firstStagedItem->data.shape]);
|
||||
std::cout << "Staged item ItemFind blocks: " << remorse::itemFindBlockOrder(area).size() << '\n';
|
||||
}
|
||||
|
||||
const auto usecodePath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("USECODE") / "EUSECODE.FLX");
|
||||
const auto usecodeArchive = usecodePath ? remorse::loadFlexArchive(*usecodePath) : std::nullopt;
|
||||
const auto eventClass = usecodeArchive ? remorse::loadUsecodeClass(*usecodeArchive, 0x361) : std::nullopt;
|
||||
const auto eventBody = eventClass ? remorse::readUsecodeEventBody(*eventClass, 0x0a) : std::nullopt;
|
||||
std::cout << "EUSECODE EVENT slot 0x0a body bytes: " << (eventBody ? eventBody->bytes.size() : 0) << '\n';
|
||||
if (firstStagedItem && typeFlags && usecodeArchive && firstStagedItem->data.shape < typeFlags->size()) {
|
||||
const auto& item = *firstStagedItem;
|
||||
const remorse::UsecodeItemIdentity identity{item.itemNumber, item.data.shape,
|
||||
item.data.flags, item.data.quality, (*typeFlags)[item.data.shape].family, false};
|
||||
const remorse::UsecodeClassBases bases{0, 0x800, 0x900};
|
||||
const auto itemClass = remorse::loadUsecodeClass(*usecodeArchive,
|
||||
remorse::selectUsecodeClassId(identity, bases));
|
||||
std::cout << "Staged item USECODE class: " << (itemClass ? itemClass->name : "not present") << '\n';
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
109
rebuild/remorse/src/usecode_archive.cpp
Normal file
109
rebuild/remorse/src/usecode_archive.cpp
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
#include "remorse/usecode_archive.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
||||
u16 readWord(const std::vector<u8>& bytes, std::size_t offset)
|
||||
{
|
||||
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
||||
}
|
||||
|
||||
u32 readDword(const std::vector<u8>& bytes, std::size_t offset)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
u16 selectUsecodeClassId(const UsecodeItemIdentity& item, const UsecodeClassBases& bases)
|
||||
{
|
||||
if (item.isNpc && (item.status & 2) == 0) {
|
||||
return static_cast<u16>(item.itemNumber + bases.npc);
|
||||
}
|
||||
if (item.family == 4) {
|
||||
return static_cast<u16>((item.quality & 0xff) + bases.unknownEgg);
|
||||
}
|
||||
return static_cast<u16>(item.shape + bases.regularShape);
|
||||
}
|
||||
|
||||
std::optional<std::vector<u8>> loadUsecodeOwnerRows(const FlexArchive& archive)
|
||||
{
|
||||
auto rows = readFlexEntry(archive, 1);
|
||||
if (!rows || rows->size() % 13 != 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
bool ownerRowAllowsEvent(const std::vector<u8>& ownerRows, u16 classId, u32 eventMask)
|
||||
{
|
||||
constexpr std::size_t rowSize = 13;
|
||||
const std::size_t row = static_cast<std::size_t>(classId) * rowSize;
|
||||
if (eventMask == 0 || row > ownerRows.size() || ownerRows.size() - row < rowSize) {
|
||||
return false;
|
||||
}
|
||||
return (readDword(ownerRows, row) & eventMask) != 0;
|
||||
}
|
||||
|
||||
std::optional<UsecodeClass> loadUsecodeClass(const FlexArchive& archive, u32 classId)
|
||||
{
|
||||
if (classId >= archive.entryCount || archive.entryCount - classId <= 2) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto names = loadUsecodeOwnerRows(archive);
|
||||
auto bytes = readFlexEntry(archive, classId + 2);
|
||||
if (!names || !bytes || bytes->size() < 20 || classId >= names->size() / 13) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const std::size_t nameOffset = 4 + static_cast<std::size_t>(classId) * 13;
|
||||
const u32 codeBase = readDword(*bytes, 8);
|
||||
if (codeBase < 20 || codeBase > bytes->size() || (codeBase - 20) % 6 != 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
std::string name;
|
||||
for (std::size_t index = 0; index < 9 && (*names)[nameOffset + index] != 0; ++index) {
|
||||
name.push_back(static_cast<char>((*names)[nameOffset + index]));
|
||||
}
|
||||
return UsecodeClass{classId, std::move(name), codeBase, std::move(*bytes)};
|
||||
}
|
||||
|
||||
std::optional<UsecodeEventBody> readUsecodeEventBody(const UsecodeClass& usecodeClass, u32 eventId)
|
||||
{
|
||||
if (usecodeClass.codeBase < 20 || usecodeClass.codeBase > usecodeClass.bytes.size() ||
|
||||
eventId >= (usecodeClass.codeBase - 20) / 6) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const std::size_t row = 20 + static_cast<std::size_t>(eventId) * 6;
|
||||
const u16 length = readWord(usecodeClass.bytes, row);
|
||||
const u32 offset = readDword(usecodeClass.bytes, row + 2);
|
||||
if (length == 0 || offset == 0 || offset > usecodeClass.bytes.size() - usecodeClass.codeBase + 1) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const std::size_t start = usecodeClass.codeBase + static_cast<std::size_t>(offset) - 1;
|
||||
if (length > usecodeClass.bytes.size() - start) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return UsecodeEventBody{length, offset,
|
||||
std::vector<u8>(usecodeClass.bytes.begin() + start, usecodeClass.bytes.begin() + start + length)};
|
||||
}
|
||||
|
||||
std::optional<EligibleUsecodeEvent> selectEligibleUsecodeEvent(const FlexArchive& archive,
|
||||
const std::vector<u8>& ownerRows, const UsecodeItemIdentity& item,
|
||||
const UsecodeClassBases& bases, u32 eventMask, u16 eventId)
|
||||
{
|
||||
const u16 classId = selectUsecodeClassId(item, bases);
|
||||
if (!ownerRowAllowsEvent(ownerRows, classId, eventMask)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto usecodeClass = loadUsecodeClass(archive, classId);
|
||||
const auto body = usecodeClass ? readUsecodeEventBody(*usecodeClass, eventId) : std::nullopt;
|
||||
if (!body) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return EligibleUsecodeEvent{classId, usecodeClass->name, *body};
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
|
|
@ -14,6 +14,7 @@
|
|||
#include "remorse/runtime.h"
|
||||
#include "remorse/shape_archive.h"
|
||||
#include "remorse/type_flags.h"
|
||||
#include "remorse/usecode_archive.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
|
@ -37,6 +38,7 @@ constexpr remorse::u32 backingHeight = 1440;
|
|||
constexpr int initialClientWidth = 1280;
|
||||
constexpr int initialClientHeight = 760;
|
||||
constexpr double simulationTickSeconds = 0.025;
|
||||
constexpr remorse::s32 diagnosticItemRange = 48;
|
||||
|
||||
struct AppState {
|
||||
remorse::GameDataRoot root;
|
||||
|
|
@ -45,6 +47,13 @@ struct AppState {
|
|||
remorse::u16 diagnosticGroupCounts[32]{};
|
||||
remorse::StandProcessEntry* diagnosticStand = nullptr;
|
||||
remorse::KeyboardProcessEntry* diagnosticKeyboard = nullptr;
|
||||
std::vector<remorse::StagedFixedMapItem> stagedItems;
|
||||
std::optional<remorse::StagedFixedMapItem> diagnosticItem;
|
||||
remorse::u16 diagnosticItemProcessId = 0;
|
||||
remorse::u16 diagnosticReleaseEventItem = 0;
|
||||
std::string diagnosticReleaseClass;
|
||||
std::size_t diagnosticReleaseBodyBytes = 0;
|
||||
bool diagnosticItemCompleted = false;
|
||||
remorse::u32 standWakeCount = 0;
|
||||
remorse::u32 diagnosticKeyEvents = 0;
|
||||
std::mutex sceneMutex;
|
||||
|
|
@ -55,6 +64,9 @@ struct AppState {
|
|||
std::vector<std::optional<remorse::DecodedShapeFrame>> playerFrames;
|
||||
remorse::CollisionBody playerCollisionBody{};
|
||||
std::vector<remorse::CollisionBody> collisionBodies;
|
||||
std::vector<remorse::ShapeTypeInfo> shapeTypes;
|
||||
std::optional<remorse::FlexArchive> usecodeArchive;
|
||||
std::vector<remorse::u8> usecodeOwnerRows;
|
||||
remorse::Palette256 playerPalette{};
|
||||
std::vector<std::uint8_t> dibPixels;
|
||||
std::vector<std::uint8_t> viewPixels;
|
||||
|
|
@ -139,6 +151,7 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
return;
|
||||
}
|
||||
const auto stagedItems = remorse::stageFixedMapItems(*map);
|
||||
|
||||
const auto shapeTypes = remorse::loadTypeFlags(*typeFlagsPath);
|
||||
const auto globObjects = remorse::loadGlobObjects(*globPath);
|
||||
|
|
@ -148,6 +161,11 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
return;
|
||||
}
|
||||
auto collisionBodies = remorse::collectMapCollisionBodies(*map, *shapeTypes, *globObjects);
|
||||
const auto usecodePath = remorse::resolveGameDataPath(
|
||||
state.root, std::filesystem::path("USECODE") / "EUSECODE.FLX");
|
||||
auto usecodeArchive = usecodePath ? remorse::loadFlexArchive(*usecodePath) : std::nullopt;
|
||||
const auto usecodeOwnerRows = usecodeArchive
|
||||
? remorse::loadUsecodeOwnerRows(*usecodeArchive) : std::nullopt;
|
||||
|
||||
const auto animationPath = remorse::resolveGameDataPath(
|
||||
state.root, std::filesystem::path("STATIC") / "ANIM.DAT");
|
||||
|
|
@ -246,6 +264,11 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
state.playerWorld = spawn;
|
||||
state.playerCollisionBody = remorse::makeCollisionBody(spawn, (*shapeTypes)[1]);
|
||||
state.collisionBodies = std::move(collisionBodies);
|
||||
state.shapeTypes = *shapeTypes;
|
||||
state.usecodeArchive = std::move(usecodeArchive);
|
||||
if (usecodeOwnerRows) {
|
||||
state.usecodeOwnerRows = *usecodeOwnerRows;
|
||||
}
|
||||
state.minWorldX = minX;
|
||||
state.minWorldY = minY;
|
||||
state.maxWorldX = maxX;
|
||||
|
|
@ -257,6 +280,9 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
state.runAnimation = *runAnimation;
|
||||
state.playerFrames = std::move(playerFrames);
|
||||
state.playerPalette = *palette;
|
||||
if (stagedItems) {
|
||||
state.stagedItems = std::move(*stagedItems);
|
||||
}
|
||||
state.sceneLoaded = true;
|
||||
}
|
||||
} catch (const std::exception& error) {
|
||||
|
|
@ -285,23 +311,23 @@ bool isKeyDown(int key)
|
|||
|
||||
std::optional<remorse::u8> readMovementDirection()
|
||||
{
|
||||
if (isKeyDown(VK_HOME) || isKeyDown(VK_NUMPAD7) || isKeyDown('Q')) return static_cast<remorse::u8>(12);
|
||||
if (isKeyDown(VK_PRIOR) || isKeyDown(VK_NUMPAD9) || isKeyDown('E')) return static_cast<remorse::u8>(0);
|
||||
if (isKeyDown(VK_END) || isKeyDown(VK_NUMPAD1) || isKeyDown('Z')) return static_cast<remorse::u8>(8);
|
||||
if (isKeyDown(VK_NEXT) || isKeyDown(VK_NUMPAD3) || isKeyDown('C')) return static_cast<remorse::u8>(4);
|
||||
if (isKeyDown(VK_HOME) || isKeyDown(VK_NUMPAD7) || isKeyDown('Q')) return remorse::controllerDirectionFromScan(0x47);
|
||||
if (isKeyDown(VK_PRIOR) || isKeyDown(VK_NUMPAD9) || isKeyDown('E')) return remorse::controllerDirectionFromScan(0x49);
|
||||
if (isKeyDown(VK_END) || isKeyDown(VK_NUMPAD1) || isKeyDown('Z')) return remorse::controllerDirectionFromScan(0x4f);
|
||||
if (isKeyDown(VK_NEXT) || isKeyDown(VK_NUMPAD3) || isKeyDown('C')) return remorse::controllerDirectionFromScan(0x51);
|
||||
|
||||
const bool up = isKeyDown(VK_UP) || isKeyDown('W') || isKeyDown(VK_NUMPAD8);
|
||||
const bool right = isKeyDown(VK_RIGHT) || isKeyDown('D') || isKeyDown(VK_NUMPAD6);
|
||||
const bool down = isKeyDown(VK_DOWN) || isKeyDown('S') || isKeyDown(VK_NUMPAD2);
|
||||
const bool left = isKeyDown(VK_LEFT) || isKeyDown('A') || isKeyDown(VK_NUMPAD4);
|
||||
if (up && left) return static_cast<remorse::u8>(12);
|
||||
if (up && right) return static_cast<remorse::u8>(0);
|
||||
if (down && left) return static_cast<remorse::u8>(8);
|
||||
if (down && right) return static_cast<remorse::u8>(4);
|
||||
if (up) return static_cast<remorse::u8>(14);
|
||||
if (right) return static_cast<remorse::u8>(2);
|
||||
if (down) return static_cast<remorse::u8>(6);
|
||||
if (left) return static_cast<remorse::u8>(10);
|
||||
if (up && left) return remorse::controllerDirectionFromScan(0x47);
|
||||
if (up && right) return remorse::controllerDirectionFromScan(0x49);
|
||||
if (down && left) return remorse::controllerDirectionFromScan(0x4f);
|
||||
if (down && right) return remorse::controllerDirectionFromScan(0x51);
|
||||
if (up) return remorse::controllerDirectionFromScan(0x48);
|
||||
if (right) return remorse::controllerDirectionFromScan(0x4d);
|
||||
if (down) return remorse::controllerDirectionFromScan(0x50);
|
||||
if (left) return remorse::controllerDirectionFromScan(0x4b);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
|
@ -488,7 +514,7 @@ void paintWindow(HWND window, AppState& state)
|
|||
}
|
||||
compositePlayer(device, state, viewWidth, viewHeight, sourceLeft, sourceTop, viewStride);
|
||||
|
||||
RECT hud{0, 0, viewWidth, 62};
|
||||
RECT hud{0, 0, viewWidth, 80};
|
||||
HBRUSH hudBrush = CreateSolidBrush(RGB(14, 24, 29));
|
||||
FillRect(device, &hud, hudBrush);
|
||||
DeleteObject(hudBrush);
|
||||
|
|
@ -510,7 +536,7 @@ void paintWindow(HWND window, AppState& state)
|
|||
HFONT hudFont = CreateFontW(-16, 0, 0, 0, FW_MEDIUM, FALSE, FALSE, FALSE,
|
||||
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY,
|
||||
DEFAULT_PITCH | FF_DONTCARE, L"Consolas");
|
||||
RECT labelBounds{20, 0, viewWidth - 12, 36};
|
||||
RECT labelBounds{20, 0, viewWidth - 12, 30};
|
||||
const int saved = SaveDC(device);
|
||||
SetBkMode(device, TRANSPARENT);
|
||||
SetTextColor(device, RGB(224, 229, 214));
|
||||
|
|
@ -528,9 +554,27 @@ void paintWindow(HWND window, AppState& state)
|
|||
: 0)
|
||||
<< L" | WAKEUPS " << state.standWakeCount
|
||||
<< L" | KEY EVENTS " << state.diagnosticKeyEvents;
|
||||
RECT diagnosticsBounds{20, 36, viewWidth - 12, hud.bottom - 1};
|
||||
RECT diagnosticsBounds{20, 30, viewWidth - 12, 54};
|
||||
SetTextColor(device, RGB(74, 207, 184));
|
||||
DrawTextW(device, diagnostics.str().c_str(), -1, &diagnosticsBounds, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
|
||||
const auto nearbyItem = remorse::nearestStagedFixedMapItem(
|
||||
state.stagedItems, state.playerWorld.x, state.playerWorld.y, diagnosticItemRange);
|
||||
if (state.diagnosticItem || nearbyItem) {
|
||||
const auto& displayItem = state.diagnosticItem ? state.diagnosticItem : nearbyItem;
|
||||
std::wostringstream itemStatus;
|
||||
itemStatus << L"MAP ITEM " << displayItem->itemNumber
|
||||
<< L" | SHAPE " << displayItem->data.shape
|
||||
<< L" | RELEASE REQUEST " << state.diagnosticReleaseEventItem
|
||||
<< L" | " << (state.diagnosticReleaseBodyBytes != 0
|
||||
? widen(state.diagnosticReleaseClass) + L" SCRIPT " + std::to_wstring(state.diagnosticReleaseBodyBytes) + L" BYTES (NOT RUN)"
|
||||
: L"NO RELEASE SCRIPT")
|
||||
<< L" | PROCESS " << (state.diagnosticItemCompleted ? L"DONE" :
|
||||
state.diagnosticItemProcessId != 0 ? L"QUEUED" :
|
||||
displayItem->itemNumber > remorse::ProcessKernel::maxTrackedItemNumber ? L"ITEM OUT OF RANGE" :
|
||||
!remorse::itemGrabCanCreateProcess(*displayItem) ? L"ITEM FLAG BLOCKS" : L"F: TEST RELEASE");
|
||||
RECT itemBounds{20, 54, viewWidth - 12, hud.bottom - 1};
|
||||
DrawTextW(device, itemStatus.str().c_str(), -1, &itemBounds, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
|
||||
}
|
||||
RestoreDC(device, saved);
|
||||
DeleteObject(hudFont);
|
||||
|
||||
|
|
@ -567,14 +611,38 @@ void updatePlayer(HWND window, AppState& state)
|
|||
state.diagnosticKernel.deliverSystemTimerEvent();
|
||||
const bool wasSleeping = (state.diagnosticStand->base.bytes[0x19] & 0x10) != 0;
|
||||
const remorse::u32 previousSleepStart = standSleepStart(*state.diagnosticStand);
|
||||
state.diagnosticKernel.scanRunnableProcesses(1, [](remorse::EntityDispatchEntryBase& entry, void* context) {
|
||||
state.diagnosticKernel.runTimedSchedulerPass(state.diagnosticKernel.activeGroup(), [](remorse::EntityDispatchEntryBase& entry, void* context) {
|
||||
auto& app = *static_cast<AppState*>(context);
|
||||
if (&entry == &app.diagnosticStand->base) {
|
||||
remorse::StandProcess::run(entry,
|
||||
[](const remorse::EntityDispatchEntryBase&, void*) { return false; },
|
||||
nullptr, app.diagnosticKernel.systemTimerTick());
|
||||
} else if (entry.bytes[0] == 0x2f && entry.bytes[1] == 0x10) {
|
||||
remorse::ItemProcess::run(entry, app.diagnosticKernel, app.diagnosticGroupCounts,
|
||||
[](remorse::u16 itemNumber, remorse::u32 eventMask, remorse::u16 eventId, void* releaseContext) {
|
||||
auto& app = *static_cast<AppState*>(releaseContext);
|
||||
app.diagnosticReleaseEventItem = itemNumber;
|
||||
if (app.diagnosticItem && app.usecodeArchive &&
|
||||
app.diagnosticItem->data.shape < app.shapeTypes.size()) {
|
||||
const auto& item = *app.diagnosticItem;
|
||||
const remorse::UsecodeItemIdentity identity{item.itemNumber, item.data.shape,
|
||||
item.data.flags, item.data.quality, app.shapeTypes[item.data.shape].family, false};
|
||||
const remorse::UsecodeClassBases bases{0, 0x800, 0x900};
|
||||
const auto eligible = remorse::selectEligibleUsecodeEvent(*app.usecodeArchive,
|
||||
app.usecodeOwnerRows, identity, bases, eventMask, eventId);
|
||||
if (eligible) {
|
||||
app.diagnosticReleaseClass = eligible->className;
|
||||
app.diagnosticReleaseBodyBytes = eligible->body.bytes.size();
|
||||
}
|
||||
}
|
||||
}, &app);
|
||||
}
|
||||
}, &state, state.diagnosticKernel.systemTimerTick());
|
||||
}, &state);
|
||||
state.diagnosticKernel.refreshActiveGroup(state.diagnosticGroupCounts);
|
||||
if (state.diagnosticItemProcessId != 0 &&
|
||||
state.diagnosticKernel.processAt(state.diagnosticItemProcessId) == nullptr) {
|
||||
state.diagnosticItemCompleted = true;
|
||||
}
|
||||
if (wasSleeping && standSleepStart(*state.diagnosticStand) != previousSleepStart) {
|
||||
++state.standWakeCount;
|
||||
}
|
||||
|
|
@ -656,6 +724,26 @@ LRESULT CALLBACK windowProcedure(HWND window, UINT message, WPARAM wParam, LPARA
|
|||
updatePlayer(window, *state);
|
||||
}
|
||||
return 0;
|
||||
case WM_KEYDOWN:
|
||||
if (wParam == 'F') {
|
||||
std::lock_guard<std::mutex> lock(state->sceneMutex);
|
||||
if (state->sceneLoaded && state->scene && !state->diagnosticItemCompleted &&
|
||||
state->diagnosticItemProcessId == 0) {
|
||||
state->diagnosticItem = remorse::nearestStagedFixedMapItem(
|
||||
state->stagedItems, state->playerWorld.x, state->playerWorld.y, diagnosticItemRange);
|
||||
}
|
||||
if (state->diagnosticItem && state->diagnosticItemProcessId == 0 &&
|
||||
state->diagnosticItem->itemNumber <= remorse::ProcessKernel::maxTrackedItemNumber &&
|
||||
remorse::itemGrabCanCreateProcess(*state->diagnosticItem)) {
|
||||
auto* itemProcess = state->diagnosticKernel.createItemProcess(
|
||||
state->diagnosticKernel.activeGroup(), state->diagnosticItem->itemNumber,
|
||||
state->diagnosticGroupCounts);
|
||||
state->diagnosticItemProcessId = static_cast<remorse::u16>(
|
||||
itemProcess->bytes[2] | (static_cast<remorse::u16>(itemProcess->bytes[3]) << 8));
|
||||
InvalidateRect(window, nullptr, FALSE);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
case WM_CHAR:
|
||||
if (wParam > 0 && wParam <= 0xff) {
|
||||
remorse::KeyEvent lastEvent{};
|
||||
|
|
@ -677,9 +765,10 @@ LRESULT CALLBACK windowProcedure(HWND window, UINT message, WPARAM wParam, LPARA
|
|||
case WM_PAINT:
|
||||
paintWindow(window, *state);
|
||||
return 0;
|
||||
case sceneReadyMessage:
|
||||
case sceneReadyMessage: {
|
||||
InvalidateRect(window, nullptr, FALSE);
|
||||
return 0;
|
||||
}
|
||||
case WM_DESTROY:
|
||||
KillTimer(window, timerId);
|
||||
if (state->sceneThread.joinable()) {
|
||||
|
|
|
|||
|
|
@ -33,6 +33,15 @@ int main()
|
|||
remorse::EntityDispatchEntry::setEventTypeChecked(entry, 0x00f2);
|
||||
assert(entry.bytes[0x06] == 0xf2);
|
||||
assert(entry.bytes[0x07] == 0x00);
|
||||
remorse::EntityDispatchEntry::setEventTypeChecked(entry, 0x0fff);
|
||||
assert(entry.bytes[0x06] == 0xff && entry.bytes[0x07] == 0x0f);
|
||||
bool invalidEventType = false;
|
||||
try {
|
||||
remorse::EntityDispatchEntry::setEventTypeChecked(entry, 0x1000);
|
||||
} catch (const std::invalid_argument&) {
|
||||
invalidEventType = true;
|
||||
}
|
||||
assert(invalidEventType && entry.bytes[0x06] == 0 && entry.bytes[0x07] == 0x10);
|
||||
|
||||
remorse::EntityDispatchEntry::sleep(entry, 0x12345678, 0x0001003c);
|
||||
assert(entry.bytes[0x18] == 0x01 && entry.bytes[0x19] == 0x10);
|
||||
|
|
@ -63,8 +72,34 @@ int main()
|
|||
assert(rejected && entry.bytes[0x08] == 4 && groupCounts[4] == 1);
|
||||
}
|
||||
|
||||
remorse::EntityDispatchEntry::incrementGroupId(entry);
|
||||
assert(entry.bytes[0x08] == 5);
|
||||
remorse::ProcessKernel incrementKernel;
|
||||
incrementKernel.setActiveGroup(4);
|
||||
remorse::EntityDispatchEntry::incrementGroupId(entry, incrementKernel, groupCounts);
|
||||
assert(entry.bytes[0x08] == 5 && groupCounts[4] == 0 && groupCounts[5] == 1);
|
||||
assert(incrementKernel.activeGroup() == 5 && incrementKernel.groupSelectionPending());
|
||||
bool wrongActiveGroup = false;
|
||||
try {
|
||||
remorse::EntityDispatchEntry::incrementGroupId(entry, incrementKernel, groupCounts);
|
||||
} catch (const std::invalid_argument&) {
|
||||
wrongActiveGroup = true;
|
||||
}
|
||||
assert(!wrongActiveGroup && entry.bytes[0x08] == 6 && incrementKernel.activeGroup() == 6);
|
||||
incrementKernel.setActiveGroup(7);
|
||||
try {
|
||||
remorse::EntityDispatchEntry::incrementGroupId(entry, incrementKernel, groupCounts);
|
||||
} catch (const std::invalid_argument&) {
|
||||
wrongActiveGroup = true;
|
||||
}
|
||||
assert(wrongActiveGroup && entry.bytes[0x08] == 6 && groupCounts[6] == 1);
|
||||
remorse::EntityDispatchEntry::setGroupId(entry, 29, groupCounts);
|
||||
incrementKernel.setActiveGroup(29);
|
||||
bool groupLimitReached = false;
|
||||
try {
|
||||
remorse::EntityDispatchEntry::incrementGroupId(entry, incrementKernel, groupCounts);
|
||||
} catch (const std::invalid_argument&) {
|
||||
groupLimitReached = true;
|
||||
}
|
||||
assert(groupLimitReached && entry.bytes[0x08] == 29 && groupCounts[29] == 1);
|
||||
|
||||
remorse::ProcessKernel kernel;
|
||||
kernel.setLastAllocationSize(sizeof(entry));
|
||||
|
|
@ -171,8 +206,43 @@ int main()
|
|||
}
|
||||
assert(exhausted && fullKernel.processCount() == 1020 && fullGroups[2] == 0);
|
||||
|
||||
remorse::ProcessKernel groupKernel;
|
||||
remorse::u16 groupTable[32]{};
|
||||
for (int index = 0; index < 3; ++index) {
|
||||
groupKernel.createProcess(1, groupTable);
|
||||
}
|
||||
groupKernel.createProcess(2, groupTable);
|
||||
groupKernel.createProcess(3, groupTable);
|
||||
groupKernel.createProcess(30, groupTable);
|
||||
groupKernel.createProcess(31, groupTable);
|
||||
groupKernel.refreshActiveGroup(groupTable);
|
||||
assert(groupKernel.activeGroup() == 1 && !groupKernel.groupSelectionPending());
|
||||
groupKernel.setActiveGroup(4);
|
||||
assert(groupKernel.activeGroup() == 4 && groupKernel.groupSelectionPending());
|
||||
groupKernel.refreshActiveGroup(groupTable);
|
||||
assert(groupKernel.activeGroup() == 3 && !groupKernel.groupSelectionPending());
|
||||
groupKernel.terminateProcess(4, groupTable);
|
||||
groupKernel.clearTerminatedProcesses();
|
||||
groupKernel.refreshActiveGroup(groupTable);
|
||||
assert(groupKernel.activeGroup() == 2 && !groupKernel.groupSelectionPending());
|
||||
for (remorse::u8 invalidGroup : {remorse::u8{0}, remorse::u8{32}}) {
|
||||
bool rejected = false;
|
||||
try {
|
||||
groupKernel.setActiveGroup(invalidGroup);
|
||||
} catch (const std::invalid_argument&) {
|
||||
rejected = true;
|
||||
}
|
||||
assert(rejected && groupKernel.activeGroup() == 2 && !groupKernel.groupSelectionPending());
|
||||
}
|
||||
groupKernel.setActiveGroup(30);
|
||||
groupKernel.refreshActiveGroup(groupTable);
|
||||
assert(groupKernel.activeGroup() == 30 && groupKernel.groupSelectionPending());
|
||||
|
||||
remorse::ProcessKernel scheduler;
|
||||
remorse::u16 scheduledGroups[32]{};
|
||||
assert(scheduler.activeGroup() == 1 && !scheduler.groupSelectionPending());
|
||||
scheduler.setActiveGroup(1);
|
||||
assert(!scheduler.groupSelectionPending());
|
||||
auto* otherGroup = scheduler.createProcess(1, scheduledGroups);
|
||||
auto* matching = scheduler.createProcess(2, scheduledGroups);
|
||||
auto* syncingProcess = scheduler.createProcess(2, scheduledGroups);
|
||||
|
|
@ -185,6 +255,7 @@ int main()
|
|||
};
|
||||
scheduler.scanRunnableProcesses(2, recordRun, &ran);
|
||||
assert((ran == std::vector<remorse::u16>{1, 3}));
|
||||
assert(scheduler.activeGroup() == 2 && scheduler.groupSelectionPending());
|
||||
assert(syncingProcess->bytes[0x16] == 0 && otherGroup->bytes[0x16] == 0);
|
||||
ran.clear();
|
||||
matching->bytes[0x16] = 1;
|
||||
|
|
@ -246,10 +317,37 @@ int main()
|
|||
assert(sleeper->bytes[offset] == 0);
|
||||
}
|
||||
|
||||
remorse::ProcessKernel timedPassKernel;
|
||||
remorse::u16 timedPassGroups[32]{};
|
||||
auto* timedPassSleeper = timedPassKernel.createProcess(1, timedPassGroups);
|
||||
timedPassKernel.createSystemTimerProcess(1, timedPassGroups);
|
||||
timedPassKernel.deliverSystemTimerEvent();
|
||||
remorse::EntityDispatchEntry::sleep(*timedPassSleeper, 1, 1);
|
||||
std::vector<remorse::u16> timedPassRuns;
|
||||
const auto advanceTimer = [](void* context) {
|
||||
static_cast<remorse::ProcessKernel*>(context)->deliverSystemTimerEvent();
|
||||
};
|
||||
assert(timedPassKernel.runTimedSchedulerPass(1, recordRun, &timedPassRuns,
|
||||
advanceTimer, &timedPassKernel) == 1);
|
||||
assert(timedPassRuns.empty() && timedPassKernel.systemTimerTick() == 2);
|
||||
assert(timedPassKernel.runTimedSchedulerPass(1, recordRun, &timedPassRuns,
|
||||
advanceTimer, &timedPassKernel) == 1);
|
||||
assert(timedPassRuns.empty() && timedPassKernel.systemTimerTick() == 3);
|
||||
assert(timedPassKernel.runTimedSchedulerPass(1, recordRun, &timedPassRuns,
|
||||
advanceTimer, &timedPassKernel) == 1);
|
||||
assert((timedPassRuns == std::vector<remorse::u16>{0}));
|
||||
|
||||
remorse::ProcessKernel standKernel;
|
||||
remorse::u16 standGroups[32]{};
|
||||
standKernel.createProcess(2, standGroups);
|
||||
assert(!standKernel.itemHasProcess(0x1234) && !standKernel.itemHasProcess(0x1235));
|
||||
bool oversizedStandRejected = false;
|
||||
try {
|
||||
standKernel.createStandProcess(2, 0x2400, standGroups);
|
||||
} catch (const std::out_of_range&) {
|
||||
oversizedStandRejected = true;
|
||||
}
|
||||
assert(oversizedStandRejected && standGroups[2] == 1 && standKernel.processCount() == 1);
|
||||
auto* standEntry = standKernel.createStandProcess(2, 0x1234, standGroups);
|
||||
auto* standing = &standEntry->base;
|
||||
assert(standKernel.processAt(1) == standing && standKernel.allocationSizeAt(1) == 0x42);
|
||||
|
|
@ -260,6 +358,72 @@ int main()
|
|||
assert(standing->bytes[0x17] == 0x03 && standEntry->extra[0] == 0x01 && standEntry->extra[1] == 0x01);
|
||||
assert(standEntry->extra[0x0e] == 0x34 && standEntry->extra[0x0f] == 0x12);
|
||||
assert(standKernel.itemHasProcess(0x1234) && !standKernel.itemHasProcess(0x1235));
|
||||
|
||||
remorse::ProcessKernel cachedItemKernel;
|
||||
remorse::u16 cachedItemGroups[32]{};
|
||||
for (int index = 0; index < 3; ++index) {
|
||||
cachedItemKernel.createProcess(1, cachedItemGroups);
|
||||
}
|
||||
auto* itemProcess = cachedItemKernel.createItemProcess(1, 0x100, cachedItemGroups);
|
||||
assert(cachedItemKernel.processAt(3) == itemProcess && cachedItemKernel.allocationSizeAt(3) == 0x32);
|
||||
assert(std::strcmp(cachedItemKernel.processNameAt(3), "ItemProcess") == 0);
|
||||
assert(itemProcess->bytes[0] == 0x2f && itemProcess->bytes[1] == 0x10);
|
||||
assert(itemProcess->bytes[4] == 0 && itemProcess->bytes[5] == 1);
|
||||
assert(itemProcess->bytes[6] == 0xf1 && itemProcess->bytes[7] == 0);
|
||||
assert(cachedItemKernel.itemHasProcess(0x100) && cachedItemKernel.itemProcessTypes(0x100) == 2);
|
||||
assert(cachedItemGroups[1] == 4);
|
||||
auto* sharedItemProcess = cachedItemKernel.createItemProcess(1, 0x100, cachedItemGroups);
|
||||
assert(cachedItemKernel.itemProcessTypes(0x100) == 2);
|
||||
cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0x101);
|
||||
assert(cachedItemKernel.itemHasProcess(0x100) && cachedItemKernel.itemHasProcess(0x101));
|
||||
assert(cachedItemKernel.itemProcessTypes(0x100) == 2 && cachedItemKernel.itemProcessTypes(0x101) == 2);
|
||||
cachedItemKernel.setProcessEventTypeChecked(*sharedItemProcess, 0xf2);
|
||||
assert(cachedItemKernel.itemProcessTypes(0x101) == 4);
|
||||
cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0);
|
||||
assert(!cachedItemKernel.itemHasProcess(0x101) && cachedItemKernel.itemProcessTypes(0x101) == 0);
|
||||
cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0x102);
|
||||
assert(cachedItemKernel.itemHasProcess(0x102) && cachedItemKernel.itemProcessTypes(0x102) == 4);
|
||||
cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0x23ff);
|
||||
assert(cachedItemKernel.itemHasProcess(0x23ff) && cachedItemKernel.itemProcessTypes(0x23ff) == 4);
|
||||
bool invalidItemNumber = false;
|
||||
try {
|
||||
cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0x2400);
|
||||
} catch (const std::out_of_range&) {
|
||||
invalidItemNumber = true;
|
||||
}
|
||||
assert(invalidItemNumber && cachedItemKernel.itemHasProcess(0x23ff));
|
||||
assert(!cachedItemKernel.itemHasProcess(0x2400) && cachedItemKernel.itemProcessTypes(0x2400) == 0);
|
||||
cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0x102);
|
||||
cachedItemKernel.terminateProcess(4, cachedItemGroups);
|
||||
cachedItemKernel.clearTerminatedProcesses();
|
||||
assert(!cachedItemKernel.itemHasProcess(0x102) && cachedItemKernel.itemProcessTypes(0x102) == 0);
|
||||
bool missingReleaseRejected = false;
|
||||
try {
|
||||
remorse::ItemProcess::run(*itemProcess, cachedItemKernel, cachedItemGroups, nullptr, nullptr);
|
||||
} catch (const std::invalid_argument&) {
|
||||
missingReleaseRejected = true;
|
||||
}
|
||||
assert(missingReleaseRejected && cachedItemKernel.itemHasProcess(0x100) && cachedItemGroups[1] == 4);
|
||||
remorse::u16 releasedItem = 0;
|
||||
struct ItemRunContext {
|
||||
remorse::ProcessKernel* kernel;
|
||||
remorse::u16 (*groups)[32];
|
||||
remorse::u16* released;
|
||||
} itemRunContext{&cachedItemKernel, &cachedItemGroups, &releasedItem};
|
||||
cachedItemKernel.scanRunnableProcesses(1, [](remorse::EntityDispatchEntryBase& process, void* context) {
|
||||
auto& state = *static_cast<ItemRunContext*>(context);
|
||||
if (process.bytes[0] == 0x2f && process.bytes[1] == 0x10) {
|
||||
remorse::ItemProcess::run(process, *state.kernel, *state.groups,
|
||||
[](remorse::u16 itemNumber, remorse::u32 eventMask, remorse::u16 eventId, void* result) {
|
||||
assert(eventMask == 0x200 && eventId == 9);
|
||||
*static_cast<remorse::u16*>(result) = itemNumber;
|
||||
},
|
||||
state.released);
|
||||
}
|
||||
}, &itemRunContext);
|
||||
assert(releasedItem == 0x100 && !cachedItemKernel.itemHasProcess(0x100));
|
||||
assert(cachedItemKernel.itemProcessTypes(0x100) == 0);
|
||||
assert(cachedItemKernel.processAt(3) == nullptr && cachedItemGroups[1] == 3);
|
||||
bool itemTeardownRejected = false;
|
||||
try {
|
||||
standKernel.terminateProcess(1, standGroups);
|
||||
|
|
|
|||
|
|
@ -29,6 +29,10 @@ int main(int argc, char** argv)
|
|||
}
|
||||
}
|
||||
#ifdef _DEBUG
|
||||
if (argc == 2 && std::strcmp(argv[1], "--assert-probe") == 0) {
|
||||
assert(false && "diagnostic assert probe");
|
||||
return 0;
|
||||
}
|
||||
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_WNDW);
|
||||
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW);
|
||||
#endif
|
||||
|
|
@ -44,52 +48,63 @@ int main(int argc, char** argv)
|
|||
if (GetModuleFileNameW(nullptr, executable, MAX_PATH) == 0) {
|
||||
return 1;
|
||||
}
|
||||
std::wstring commandLine = L"\"" + std::wstring(executable) + L"\" --crash-probe";
|
||||
SECURITY_ATTRIBUTES pipeSecurity{sizeof(SECURITY_ATTRIBUTES), nullptr, TRUE};
|
||||
HANDLE readPipe = nullptr;
|
||||
HANDLE writePipe = nullptr;
|
||||
if (!CreatePipe(&readPipe, &writePipe, &pipeSecurity, 0)) {
|
||||
return 1;
|
||||
}
|
||||
if (!SetHandleInformation(readPipe, HANDLE_FLAG_INHERIT, 0)) {
|
||||
CloseHandle(readPipe);
|
||||
auto runProbe = [&](const wchar_t* option, const char* expectedOutput, bool expectExitOne) {
|
||||
std::wstring commandLine = L"\"" + std::wstring(executable) + L"\" " + option;
|
||||
SECURITY_ATTRIBUTES pipeSecurity{sizeof(SECURITY_ATTRIBUTES), nullptr, TRUE};
|
||||
HANDLE readPipe = nullptr;
|
||||
HANDLE writePipe = nullptr;
|
||||
if (!CreatePipe(&readPipe, &writePipe, &pipeSecurity, 0)) {
|
||||
return false;
|
||||
}
|
||||
if (!SetHandleInformation(readPipe, HANDLE_FLAG_INHERIT, 0)) {
|
||||
CloseHandle(readPipe);
|
||||
CloseHandle(writePipe);
|
||||
return false;
|
||||
}
|
||||
STARTUPINFOW startup{};
|
||||
startup.cb = sizeof(startup);
|
||||
startup.dwFlags = STARTF_USESTDHANDLES;
|
||||
startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
||||
startup.hStdOutput = writePipe;
|
||||
startup.hStdError = writePipe;
|
||||
PROCESS_INFORMATION child{};
|
||||
if (!CreateProcessW(nullptr, commandLine.data(), nullptr, nullptr, TRUE, CREATE_NO_WINDOW,
|
||||
nullptr, nullptr, &startup, &child)) {
|
||||
CloseHandle(readPipe);
|
||||
CloseHandle(writePipe);
|
||||
return false;
|
||||
}
|
||||
CloseHandle(writePipe);
|
||||
return 1;
|
||||
}
|
||||
STARTUPINFOW startup{};
|
||||
startup.cb = sizeof(startup);
|
||||
startup.dwFlags = STARTF_USESTDHANDLES;
|
||||
startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
||||
startup.hStdOutput = writePipe;
|
||||
startup.hStdError = writePipe;
|
||||
PROCESS_INFORMATION child{};
|
||||
if (!CreateProcessW(nullptr, commandLine.data(), nullptr, nullptr, TRUE, CREATE_NO_WINDOW,
|
||||
nullptr, nullptr, &startup, &child)) {
|
||||
const DWORD waitResult = WaitForSingleObject(child.hProcess, 5000);
|
||||
if (waitResult == WAIT_TIMEOUT) {
|
||||
TerminateProcess(child.hProcess, 2);
|
||||
WaitForSingleObject(child.hProcess, INFINITE);
|
||||
}
|
||||
char buffer[512];
|
||||
DWORD count = 0;
|
||||
std::string crashOutput;
|
||||
while (ReadFile(readPipe, buffer, sizeof(buffer), &count, nullptr) && count != 0) {
|
||||
crashOutput.append(buffer, count);
|
||||
}
|
||||
DWORD exitCode = 0;
|
||||
const BOOL gotExitCode = GetExitCodeProcess(child.hProcess, &exitCode);
|
||||
CloseHandle(readPipe);
|
||||
CloseHandle(writePipe);
|
||||
return 1;
|
||||
}
|
||||
CloseHandle(writePipe);
|
||||
const DWORD waitResult = WaitForSingleObject(child.hProcess, 5000);
|
||||
if (waitResult == WAIT_TIMEOUT) {
|
||||
TerminateProcess(child.hProcess, 2);
|
||||
WaitForSingleObject(child.hProcess, INFINITE);
|
||||
}
|
||||
char buffer[512];
|
||||
DWORD count = 0;
|
||||
std::string crashOutput;
|
||||
while (ReadFile(readPipe, buffer, sizeof(buffer), &count, nullptr) && count != 0) {
|
||||
crashOutput.append(buffer, count);
|
||||
}
|
||||
DWORD exitCode = 0;
|
||||
const BOOL gotExitCode = GetExitCodeProcess(child.hProcess, &exitCode);
|
||||
CloseHandle(readPipe);
|
||||
CloseHandle(child.hThread);
|
||||
CloseHandle(child.hProcess);
|
||||
if (!gotExitCode || waitResult != WAIT_OBJECT_0 || exitCode != 1 ||
|
||||
crashOutput.find("Unhandled C++ exception: diagnostic probe") == std::string::npos) {
|
||||
std::fprintf(stderr, "Crash probe wait=%lu exit=0x%08lX output=%s\n",
|
||||
waitResult, exitCode, crashOutput.c_str());
|
||||
CloseHandle(child.hThread);
|
||||
CloseHandle(child.hProcess);
|
||||
const bool passed = gotExitCode && waitResult == WAIT_OBJECT_0 &&
|
||||
(expectExitOne ? exitCode == 1 : exitCode != 0) &&
|
||||
crashOutput.find(expectedOutput) != std::string::npos;
|
||||
if (!passed) {
|
||||
std::fprintf(stderr, "Probe %ls wait=%lu exit=0x%08lX output=%s\n",
|
||||
option, waitResult, exitCode, crashOutput.c_str());
|
||||
}
|
||||
return passed;
|
||||
};
|
||||
if (!runProbe(L"--crash-probe", "Unhandled C++ exception: diagnostic probe", true)
|
||||
#ifdef _DEBUG
|
||||
|| !runProbe(L"--assert-probe", "diagnostic assert probe", false)
|
||||
#endif
|
||||
) {
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
#include "remorse/entity_dispatch_entry.h"
|
||||
#include "remorse/file_system.h"
|
||||
#include "remorse/fixed_map.h"
|
||||
#include "remorse/type_flags.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
|
||||
int main()
|
||||
{
|
||||
|
|
@ -31,6 +34,126 @@ int main()
|
|||
}
|
||||
assert(foundNonEmptyMap);
|
||||
|
||||
const auto retailMap = remorse::loadFixedMap(*archive, 1);
|
||||
assert(retailMap.has_value() && !retailMap->entries.empty());
|
||||
const auto cachedItems = remorse::stageFixedMapItems(*retailMap);
|
||||
assert(cachedItems.has_value() && !cachedItems->empty());
|
||||
assert(cachedItems->back().itemNumber <= remorse::ProcessKernel::maxTrackedItemNumber);
|
||||
const auto typeFlagPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
|
||||
const auto typeFlags = typeFlagPath ? remorse::loadTypeFlags(*typeFlagPath) : std::nullopt;
|
||||
assert(typeFlags && cachedItems->front().data.shape < typeFlags->size());
|
||||
const auto& originShape = (*typeFlags)[cachedItems->front().data.shape];
|
||||
auto origin = cachedItems->front();
|
||||
origin.data.z = 80;
|
||||
auto candidate = origin;
|
||||
candidate.itemNumber = static_cast<remorse::u16>(origin.itemNumber + 1);
|
||||
candidate.data.z = static_cast<remorse::u8>(origin.data.z + (originShape.z == 0 && (originShape.flags & 1) == 0 ? 1 : originShape.z * 8));
|
||||
remorse::ShapeTypeInfo footprint{0, 0, 1, 1, 0, 0};
|
||||
assert(remorse::classifyItemFindZMatch(origin, originShape, candidate, footprint, 3) == 1);
|
||||
assert(remorse::classifyItemFindZMatch(origin, originShape, candidate, footprint, 2) == 0);
|
||||
candidate.data.z = 79;
|
||||
assert(remorse::classifyItemFindZMatch(origin, originShape, candidate, footprint, 3) == 2);
|
||||
remorse::ShapeTypeInfo flatShape{0, 1, 1, 1, 0, 0};
|
||||
candidate.data.z = 80;
|
||||
assert(remorse::classifyItemFindZMatch(origin, flatShape, candidate, flatShape, 3) == 1);
|
||||
candidate.data.x = static_cast<remorse::u16>(origin.data.x + 16);
|
||||
assert(remorse::classifyItemFindZMatch(origin, originShape, candidate, footprint, 3) == 0);
|
||||
const auto retailSearchArea = remorse::itemFindAreaAroundItem(cachedItems->front(), originShape);
|
||||
const auto retailSearchBlocks = remorse::itemFindBlockOrder(retailSearchArea);
|
||||
assert(!retailSearchBlocks.empty() && retailSearchBlocks.front() ==
|
||||
(retailSearchArea.minX >> 9) * 0x40 + (retailSearchArea.minY >> 9));
|
||||
origin.data.x = 511;
|
||||
origin.data.y = 511;
|
||||
const auto crossingArea = remorse::itemFindAreaAroundItem(origin, footprint);
|
||||
assert(crossingArea.minX == 496 && crossingArea.minY == 496);
|
||||
assert(crossingArea.maxX == 767 && crossingArea.maxY == 767);
|
||||
assert((remorse::itemFindBlockOrder(crossingArea) == std::vector<remorse::u16>{0, 64, 1, 65}));
|
||||
origin.data.x = 0;
|
||||
origin.data.y = 0;
|
||||
const auto edgeArea = remorse::itemFindAreaAroundItem(origin, footprint);
|
||||
assert(edgeArea.minX == 0 && edgeArea.minY == 0);
|
||||
assert((remorse::itemFindBlockOrder(edgeArea) == std::vector<remorse::u16>{0}));
|
||||
candidate.itemNumber = 0;
|
||||
assert(remorse::classifyItemFindZMatch(origin, originShape, candidate, footprint, 3) == 0);
|
||||
assert(cachedItems->front().recordIndex < retailMap->entries.size());
|
||||
for (const auto& item : *cachedItems) {
|
||||
const auto& record = retailMap->entries[item.recordIndex];
|
||||
assert(record.shape != 0 && (record.flags & 8) == 0);
|
||||
assert(item.data.x == record.x / 2 && item.data.y == record.y / 2);
|
||||
assert(item.data.shape == record.shape);
|
||||
assert(item.data.npcNumber == record.npcNumber);
|
||||
assert(item.data.flags == (record.flags & ~1u));
|
||||
}
|
||||
remorse::ProcessKernel retailKernel;
|
||||
remorse::u16 retailGroups[32]{};
|
||||
for (int index = 0; index < 3; ++index) {
|
||||
retailKernel.createProcess(1, retailGroups);
|
||||
}
|
||||
const auto retailItemNumber = cachedItems->front().itemNumber;
|
||||
auto* retailProcess = retailKernel.createItemProcess(1, retailItemNumber, retailGroups);
|
||||
assert(retailProcess->bytes[4] == static_cast<remorse::u8>(retailItemNumber));
|
||||
assert(retailKernel.itemHasProcess(retailItemNumber));
|
||||
struct RetailScan {
|
||||
remorse::ProcessKernel* kernel;
|
||||
remorse::u16 (*groups)[32];
|
||||
remorse::u16 released = 0;
|
||||
int releaseCount = 0;
|
||||
} retailScan{&retailKernel, &retailGroups};
|
||||
auto runRetailItem = [](remorse::EntityDispatchEntryBase& process, void* context) {
|
||||
auto& scan = *static_cast<RetailScan*>(context);
|
||||
if (process.bytes[0] == 0x2f && process.bytes[1] == 0x10) {
|
||||
remorse::ItemProcess::run(process, *scan.kernel, *scan.groups,
|
||||
[](remorse::u16 itemNumber, remorse::u32 eventMask, remorse::u16 eventId, void* releaseContext) {
|
||||
assert(eventMask == 0x200 && eventId == 9);
|
||||
auto& result = *static_cast<RetailScan*>(releaseContext);
|
||||
result.released = itemNumber;
|
||||
++result.releaseCount;
|
||||
}, &scan);
|
||||
}
|
||||
};
|
||||
retailKernel.scanRunnableProcesses(1, runRetailItem, &retailScan, 1);
|
||||
retailKernel.scanRunnableProcesses(1, runRetailItem, &retailScan, 2);
|
||||
assert(retailScan.released == retailItemNumber && retailScan.releaseCount == 1);
|
||||
assert(retailKernel.processAt(3) == nullptr && !retailKernel.itemHasProcess(retailItemNumber));
|
||||
assert(retailGroups[1] == 3);
|
||||
remorse::FixedMap stagedMap;
|
||||
stagedMap.entries.resize(4);
|
||||
stagedMap.entries[1].shape = 16;
|
||||
stagedMap.entries[2].shape = 16;
|
||||
stagedMap.entries[2].flags = 8;
|
||||
stagedMap.entries[3].shape = 16;
|
||||
stagedMap.entries[3].flags = 1;
|
||||
auto stagedItems = remorse::stageFixedMapItems(stagedMap);
|
||||
assert(stagedItems && stagedItems->size() == 2);
|
||||
assert((*stagedItems)[0].recordIndex == 1 && (*stagedItems)[0].itemNumber == 0x100);
|
||||
assert((*stagedItems)[1].recordIndex == 3 && (*stagedItems)[1].itemNumber == 0x102);
|
||||
assert((*stagedItems)[1].data.flags == 0);
|
||||
assert(remorse::itemGrabCanCreateProcess((*stagedItems)[0]));
|
||||
(*stagedItems)[0].data.flags |= 0x02;
|
||||
assert(!remorse::itemGrabCanCreateProcess((*stagedItems)[0]));
|
||||
(*stagedItems)[0].data.x = 20;
|
||||
(*stagedItems)[0].data.y = 10;
|
||||
(*stagedItems)[1].data.x = 30;
|
||||
(*stagedItems)[1].data.y = 10;
|
||||
const auto nearby = remorse::nearestStagedFixedMapItem(*stagedItems, 59, 20, 16);
|
||||
assert(nearby && nearby->itemNumber == 0x102);
|
||||
assert(!remorse::nearestStagedFixedMapItem(*stagedItems, 100, 20, 16));
|
||||
assert(!remorse::nearestStagedFixedMapItem(*stagedItems, 60, 20, -1));
|
||||
remorse::FixedMap oversizedMap;
|
||||
oversizedMap.entries.resize(0x2701);
|
||||
for (auto& record : oversizedMap.entries) {
|
||||
record.shape = 16;
|
||||
}
|
||||
assert(!remorse::stageFixedMapItems(oversizedMap).has_value());
|
||||
remorse::FixedMap widerCacheMap;
|
||||
widerCacheMap.entries.resize(0x2301);
|
||||
for (auto& record : widerCacheMap.entries) {
|
||||
record.shape = 16;
|
||||
}
|
||||
const auto widerCacheItems = remorse::stageFixedMapItems(widerCacheMap);
|
||||
assert(widerCacheItems && widerCacheItems->back().itemNumber == 0x2400);
|
||||
assert(widerCacheItems->back().itemNumber > remorse::ProcessKernel::maxTrackedItemNumber);
|
||||
|
||||
const auto outOfRange = remorse::loadFixedMap(*archive, archive->mapCount);
|
||||
assert(!outOfRange.has_value());
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
#include "remorse/file_system.h"
|
||||
#include "remorse/flex_archive.h"
|
||||
#include "remorse/fixed_map.h"
|
||||
#include "remorse/type_flags.h"
|
||||
#include "remorse/usecode_archive.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
|
@ -23,5 +26,91 @@ int main()
|
|||
assert((*firstEntry)[1] == 'u');
|
||||
assert((*firstEntry)[2] == 'm');
|
||||
assert((*firstEntry)[3] == 'b');
|
||||
const auto usecodePath = remorse::resolveGameDataPath(*root, std::filesystem::path("USECODE") / "EUSECODE.FLX");
|
||||
assert(usecodePath);
|
||||
const auto usecode = remorse::loadFlexArchive(*usecodePath);
|
||||
assert(usecode);
|
||||
const auto retailOwnerRows = remorse::loadUsecodeOwnerRows(*usecode);
|
||||
assert(retailOwnerRows && retailOwnerRows->size() % 13 == 0);
|
||||
const auto eventClass = remorse::loadUsecodeClass(*usecode, 0x361);
|
||||
assert(eventClass && eventClass->name == "EVENT" && eventClass->codeBase == 0xd4);
|
||||
const auto eventBody = remorse::readUsecodeEventBody(*eventClass, 0x0a);
|
||||
assert(eventBody && eventBody->length == 0x1fd6 && eventBody->relativeOffset == 1);
|
||||
assert(eventBody->bytes.size() == 0x1fd6 && eventBody->bytes.front() == eventClass->bytes[0xd4]);
|
||||
assert(!remorse::readUsecodeEventBody(*eventClass, 0x20));
|
||||
assert(!remorse::loadUsecodeClass(*usecode, usecode->entryCount));
|
||||
auto truncated = *eventClass;
|
||||
truncated.bytes.resize(truncated.codeBase + 1);
|
||||
assert(!remorse::readUsecodeEventBody(truncated, 0x0a));
|
||||
auto invalidOffset = *eventClass;
|
||||
invalidOffset.bytes[20 + 0x0a * 6 + 2] = 0;
|
||||
assert(!remorse::readUsecodeEventBody(invalidOffset, 0x0a));
|
||||
const auto rechargeClass = remorse::loadUsecodeClass(*usecode, 0x138);
|
||||
assert(rechargeClass && rechargeClass->name == "RECHFLNS");
|
||||
const auto releaseBody = remorse::readUsecodeEventBody(*rechargeClass, 9);
|
||||
assert(releaseBody && releaseBody->length == 102 && releaseBody->relativeOffset == 0x256);
|
||||
assert(remorse::ownerRowAllowsEvent(*retailOwnerRows, 0x138, 0x200));
|
||||
const remorse::UsecodeClassBases bases{0x200, 0x400, 0x600};
|
||||
remorse::UsecodeItemIdentity identity{7, 0x45, 0, 0x1278, 4, true};
|
||||
assert(remorse::selectUsecodeClassId(identity, bases) == 0x407);
|
||||
identity.status = 2;
|
||||
assert(remorse::selectUsecodeClassId(identity, bases) == 0x678);
|
||||
identity.family = 0;
|
||||
assert(remorse::selectUsecodeClassId(identity, bases) == 0x245);
|
||||
identity.isNpc = false;
|
||||
identity.status = 0;
|
||||
assert(remorse::selectUsecodeClassId(identity, bases) == 0x245);
|
||||
const auto fixedPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "FIXED.DAT");
|
||||
const auto flagsPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
|
||||
assert(fixedPath && flagsPath);
|
||||
const auto fixedArchive = remorse::loadFixedMapArchive(*fixedPath);
|
||||
const auto fixedMap = fixedArchive ? remorse::loadFixedMap(*fixedArchive, 1) : std::nullopt;
|
||||
const auto typeFlags = remorse::loadTypeFlags(*flagsPath);
|
||||
assert(fixedMap && typeFlags);
|
||||
const auto stagedItems = remorse::stageFixedMapItems(*fixedMap);
|
||||
assert(stagedItems && !stagedItems->empty());
|
||||
const auto& staged = stagedItems->front();
|
||||
assert(staged.data.shape < typeFlags->size());
|
||||
const remorse::UsecodeItemIdentity stagedIdentity{staged.itemNumber, staged.data.shape,
|
||||
staged.data.flags, staged.data.quality, (*typeFlags)[staged.data.shape].family, false};
|
||||
const remorse::UsecodeClassBases retailBases{0, 0x800, 0x900};
|
||||
assert(remorse::selectUsecodeClassId(stagedIdentity, retailBases) == staged.data.shape);
|
||||
const auto stagedClass = remorse::loadUsecodeClass(*usecode,
|
||||
remorse::selectUsecodeClassId(stagedIdentity, retailBases));
|
||||
assert(!stagedClass);
|
||||
assert(!remorse::ownerRowAllowsEvent(*retailOwnerRows, staged.data.shape, 0x200));
|
||||
assert(!remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
||||
stagedIdentity, retailBases, 0x200, 9));
|
||||
bool foundStagedRelease = false;
|
||||
for (const auto& item : *stagedItems) {
|
||||
if (item.data.shape >= typeFlags->size()) {
|
||||
continue;
|
||||
}
|
||||
const remorse::UsecodeItemIdentity mapItemIdentity{item.itemNumber, item.data.shape,
|
||||
item.data.flags, item.data.quality, (*typeFlags)[item.data.shape].family, false};
|
||||
if (remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
||||
mapItemIdentity, retailBases, 0x200, 9)) {
|
||||
foundStagedRelease = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert(foundStagedRelease);
|
||||
const remorse::UsecodeItemIdentity rechargeItem{0x100, 0x138, 0, 0, 0, false};
|
||||
const auto eligibleRelease = remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
||||
rechargeItem, retailBases, 0x200, 9);
|
||||
assert(eligibleRelease && eligibleRelease->classId == 0x138 &&
|
||||
eligibleRelease->className == "RECHFLNS" && eligibleRelease->body.bytes.size() == 102);
|
||||
assert(!remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
||||
rechargeItem, retailBases, 0, 9));
|
||||
std::vector<remorse::u8> ownerRows(26);
|
||||
ownerRows[13] = 0x00;
|
||||
ownerRows[14] = 0x02;
|
||||
ownerRows[16] = 0x01;
|
||||
assert(remorse::ownerRowAllowsEvent(ownerRows, 1, 0x200));
|
||||
assert(remorse::ownerRowAllowsEvent(ownerRows, 1, 0x1000000));
|
||||
assert(!remorse::ownerRowAllowsEvent(ownerRows, 1, 0x400));
|
||||
assert(!remorse::ownerRowAllowsEvent(ownerRows, 0, 0x200));
|
||||
assert(!remorse::ownerRowAllowsEvent(ownerRows, 2, 0x200));
|
||||
assert(!remorse::ownerRowAllowsEvent(ownerRows, 1, 0));
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,12 @@ int main()
|
|||
assert(!remorse::controllerWalkRequested(0x40));
|
||||
assert(remorse::controllerWalkRequested(0x41));
|
||||
assert(remorse::controllerWalkRequested(0x42));
|
||||
constexpr remorse::u8 scans[] = {0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f, 0x50, 0x51};
|
||||
constexpr remorse::u8 directions[] = {12, 14, 0, 10, 2, 8, 6, 4};
|
||||
for (std::size_t index = 0; index < std::size(scans); ++index) {
|
||||
assert(remorse::controllerDirectionFromScan(scans[index]) == directions[index]);
|
||||
}
|
||||
assert(!remorse::controllerDirectionFromScan(0x52));
|
||||
constexpr remorse::s32 originX = 1024;
|
||||
constexpr remorse::s32 originY = 2048;
|
||||
const remorse::ScreenPoint origin = remorse::projectToScreen({originX, originY, 0});
|
||||
|
|
|
|||
11
rebuild/remorse/tests/test_crash_setup.cpp
Normal file
11
rebuild/remorse/tests/test_crash_setup.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include "remorse/runtime.h"
|
||||
|
||||
namespace {
|
||||
|
||||
struct TestCrashSetup {
|
||||
TestCrashSetup() { remorse::installCrashDiagnostics(); }
|
||||
};
|
||||
|
||||
TestCrashSetup testCrashSetup;
|
||||
|
||||
} // namespace
|
||||
Loading…
Add table
Add a link
Reference in a new issue