Add tests for controller fire attempt suppression and usecode interpreter functionality
- Enhanced isometric_controls_tests.cpp to include assertions for controller fire attempt suppression under various conditions. - Added tests for the Usecode interpreter in usecode_vm_tests.cpp, verifying script execution and stack manipulation. - Introduced a new PowerShell script, clean-old-build-artifacts.ps1, to facilitate the cleanup of old build artifacts in specified directories.
This commit is contained in:
parent
49a15602b6
commit
ffb1918f35
41 changed files with 3611 additions and 349 deletions
|
|
@ -14,7 +14,13 @@ add_library(remorse_core
|
|||
src/map_renderer.cpp
|
||||
src/map_sorter.cpp
|
||||
src/palette.cpp
|
||||
src/ported/controller_fire_sleep_gate.cpp
|
||||
src/ported/entity_dispatch_entry_set_group_id.cpp
|
||||
src/ported/game_start_fresh_map.cpp
|
||||
src/ported/kernel_keyboard_interrupt_dispatch.cpp
|
||||
src/ported/kernel_scheduler_group_selection.cpp
|
||||
src/ported/kernel_keyboard_interrupt_gate.cpp
|
||||
src/ported/kernel_scheduler_runnable_gate.cpp
|
||||
src/runtime.cpp
|
||||
src/shape_archive.cpp
|
||||
src/type_flags.cpp
|
||||
|
|
|
|||
|
|
@ -30,12 +30,12 @@ call "%VCVARS%" || exit /b 2
|
|||
|
||||
cd /d "%REPO_ROOT%" || exit /b 2
|
||||
|
||||
set "BUILD_DIR=rebuild/remorse/out/build-nmake"
|
||||
cmake -S rebuild/remorse -B "%BUILD_DIR%" -G "NMake Makefiles" -DCMAKE_BUILD_TYPE="%CONFIG%" || exit /b 1
|
||||
cmake --build "%BUILD_DIR%" || exit /b 1
|
||||
ctest --test-dir "%BUILD_DIR%" --output-on-failure || exit /b 1
|
||||
set "BUILD_DIR=rebuild/remorse/out/build"
|
||||
cmake -S rebuild/remorse -B "%BUILD_DIR%" -DCMAKE_GENERATOR_INSTANCE="%VSINSTALL%" || exit /b 1
|
||||
cmake --build "%BUILD_DIR%" --config "%CONFIG%" || exit /b 1
|
||||
ctest --test-dir "%BUILD_DIR%" -C "%CONFIG%" --output-on-failure || exit /b 1
|
||||
|
||||
set "EXE=%BUILD_DIR%\remorse_rebuild.exe"
|
||||
set "EXE=%BUILD_DIR%\%CONFIG%\remorse_rebuild.exe"
|
||||
if not exist "%EXE%" (
|
||||
echo Built executable was not found at "%EXE%".
|
||||
exit /b 1
|
||||
|
|
|
|||
|
|
@ -44,6 +44,8 @@ struct StagedContainedFixedMapItem {
|
|||
FixedMapEntry data;
|
||||
};
|
||||
|
||||
constexpr u16 liveItemEtherealFlag = 0x0800;
|
||||
|
||||
struct ItemFindArea {
|
||||
u16 minX = 0;
|
||||
u16 minY = 0;
|
||||
|
|
@ -129,6 +131,12 @@ public:
|
|||
std::optional<std::vector<u16>> sweepCandidateIds(const WorldRect& area, u16 excludedItemNumber) const;
|
||||
std::optional<u16> allocateItem(StagedFixedMapItem item);
|
||||
bool insertItem(StagedFixedMapItem item);
|
||||
bool pushItemToEtherealVoid(u16 itemNumber);
|
||||
std::optional<u16> popItemFromEtherealVoid();
|
||||
std::optional<u16> popItemToCoords(u16 x, u16 y, u8 z);
|
||||
bool destroyItem(u16 itemNumber);
|
||||
bool canCreateItemAt(u16 shape, u16 x, u16 y, u8 z,
|
||||
const std::vector<ShapeTypeInfo>& shapes) const;
|
||||
bool moveItemIntoContainer(u16 itemNumber, u16 containerItemNumber,
|
||||
const std::vector<ShapeTypeInfo>& shapes);
|
||||
bool moveContainedItemToWorld(u16 itemNumber, u16 x, u16 y, u8 z);
|
||||
|
|
@ -153,6 +161,7 @@ private:
|
|||
std::optional<StagedFixedMapItem> avatar_;
|
||||
std::vector<NpcAnimationState> npcAnimations_;
|
||||
std::vector<bool> allocatedIds_;
|
||||
std::vector<bool> destroyedIds_;
|
||||
std::vector<bool> hatchedGlobIds_;
|
||||
std::vector<u16> parentById_;
|
||||
std::vector<std::optional<FixedMapEntry>> containedItemData_;
|
||||
|
|
@ -160,6 +169,7 @@ private:
|
|||
std::vector<u16> containedHeadById_;
|
||||
std::vector<u16> containedTailById_;
|
||||
std::vector<u16> freeItemIds_;
|
||||
std::vector<u16> etherealStack_;
|
||||
u16 nextFreeItemNumber_ = 0x100;
|
||||
};
|
||||
|
||||
|
|
|
|||
16
rebuild/remorse/include/remorse/game_start.h
Normal file
16
rebuild/remorse/include/remorse/game_start.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace remorse {
|
||||
|
||||
class LiveFixedMapItems;
|
||||
class ProcessKernel;
|
||||
struct ShapeTypeInfo;
|
||||
|
||||
bool startFreshGameMapOne(ProcessKernel& kernel, LiveFixedMapItems& items,
|
||||
const std::vector<ShapeTypeInfo>& shapes, u16 (&groupCounts)[32]);
|
||||
|
||||
} // namespace remorse
|
||||
|
|
@ -25,6 +25,9 @@ inline bool controllerWalkRequested(u16 modifiers)
|
|||
return shiftDown == capsLockOn;
|
||||
}
|
||||
|
||||
bool controllerFireAttemptSuppressed(bool previousAttemptSlept, u8 scanCode,
|
||||
u8 eventMode, u8 keyState);
|
||||
|
||||
inline bool controllerAllowsInPlaceDirectionUpdate(u8 animation)
|
||||
{
|
||||
switch (animation) {
|
||||
|
|
@ -112,6 +115,7 @@ struct ControllerActorState {
|
|||
bool kneeling = false;
|
||||
bool activeWeapon = false;
|
||||
bool busy = false;
|
||||
bool lastFireAttemptWentToSleep = false;
|
||||
bool lastAnimationNotCombat = true;
|
||||
bool avatarSomethingCombatty = false;
|
||||
};
|
||||
|
|
@ -144,6 +148,10 @@ public:
|
|||
if (eventMode == 0) {
|
||||
return;
|
||||
}
|
||||
if (controllerFireAttemptSuppressed(actor_.lastFireAttemptWentToSleep,
|
||||
scanCode, eventMode, keyState)) {
|
||||
return;
|
||||
}
|
||||
if (keyState != 0 && keyAction == 1 && actor_.npcNumber == 1 &&
|
||||
(scanCode == 0x0f || scanCode == 0x24)) {
|
||||
if (!actor_.busy || (actor_.lastAnimation != 56 &&
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@ enum class UsecodeStepResult {
|
|||
|
||||
struct UsecodeOpcodeServices {
|
||||
std::function<std::optional<u16>(u16 offset, u16 segment)> readFarWord;
|
||||
std::function<std::optional<UsecodeEventBody>(u16 classId, u16 eventSlot)> callEvent;
|
||||
std::function<bool(u16 eventType, u16 sourceItem)> setInfo;
|
||||
std::function<u16()> processCount;
|
||||
std::function<std::optional<u16>(u16 classId, u16 eventSlot, u8 extraArgumentBytes,
|
||||
|
|
@ -58,18 +59,29 @@ struct UsecodeOpcodeServices {
|
|||
std::function<u16()> currentProcessId;
|
||||
};
|
||||
|
||||
struct UsecodeRoutineFrame {
|
||||
u16 classId = 0;
|
||||
u16 eventSlot = 0;
|
||||
std::vector<u8> code;
|
||||
std::vector<u8> frame;
|
||||
std::size_t frameBaseOffset = usecodeFrameBaseOffset;
|
||||
std::size_t returnProgramCounter = 0;
|
||||
};
|
||||
|
||||
class UsecodeInterpreterState {
|
||||
public:
|
||||
static std::optional<UsecodeInterpreterState> initialize(
|
||||
const std::vector<u8>& itemParameter, const std::vector<u8>& contextParameter = {},
|
||||
u16 modeFlags = 0, bool contextParameterPresent = false);
|
||||
bool pushCodeAddress(u16 offset, u16 segment);
|
||||
bool pushByte(u8 value);
|
||||
bool pushWord(u16 value);
|
||||
bool pushDword(u32 value);
|
||||
std::optional<u16> popWord();
|
||||
std::optional<u32> popDword();
|
||||
UsecodeStepResult step(const std::vector<u8>& code, std::size_t& programCounter,
|
||||
std::vector<u8>& frame, const UsecodeOpcodeServices& services);
|
||||
std::vector<u8>& frame, std::size_t frameBaseOffset,
|
||||
const UsecodeOpcodeServices& services);
|
||||
std::optional<std::array<u16, 2>> lastSetInfoOperands;
|
||||
|
||||
std::array<u8, 0xcc> valueStack{};
|
||||
|
|
@ -102,10 +114,14 @@ struct UsecodeProcessTask {
|
|||
std::vector<u8> itemMemory;
|
||||
UsecodeInterpreterState interpreter;
|
||||
std::size_t programCounter = usecodeEventBytecodeOffset;
|
||||
std::size_t frameBaseOffset = usecodeFrameBaseOffset;
|
||||
std::vector<UsecodeRoutineFrame> routineFrames;
|
||||
bool halted = false;
|
||||
bool timerDelayProcess = false;
|
||||
u32 wakeTick = 0;
|
||||
|
||||
std::optional<u16> readMappedFarWord(FarPtr16 address) const;
|
||||
bool writeMappedFarWord(FarPtr16 address, u16 value);
|
||||
std::optional<u16> readMappedItemWord(FarPtr16 address) const;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -776,15 +776,7 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
const u8 highestGroup = ported::selectHighestOrdinaryProcessGroup(processes_.data(), processes_.size());
|
||||
setActiveGroup(highestGroup);
|
||||
groupSelectionPending_ = false;
|
||||
}
|
||||
|
|
@ -1103,18 +1095,10 @@ void ProcessKernel::scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDisp
|
|||
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;
|
||||
}
|
||||
if (ported::prepareProcessForSchedulerRun(*entry, activeGroup_, waitingForProcess)) {
|
||||
currentProcessId_ = static_cast<u16>(id);
|
||||
run(*entry, context);
|
||||
currentProcessId_ = 0;
|
||||
}
|
||||
}
|
||||
clearTerminatedProcesses();
|
||||
|
|
@ -1236,13 +1220,7 @@ void EntityDispatchEntry::setGroupId(EntityDispatchEntryBase& entry, u8 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);
|
||||
ported::incrementEntityDispatchEntryGroupId(entry, kernel, groupCounts);
|
||||
}
|
||||
|
||||
void EntityDispatchEntry::setSourceType(EntityDispatchEntryBase& entry, u16 sourceType)
|
||||
|
|
@ -1375,24 +1353,4 @@ bool ProcessKernel::pendingKeyEventOverflowed() const
|
|||
return pendingKeyEventOverflowed_;
|
||||
}
|
||||
|
||||
void ProcessKernel::dispatchPendingKeyEvents(
|
||||
void (*deliver)(EntityDispatchEntryBase&, u16, u16, KeyEvent&, void*), void* context)
|
||||
{
|
||||
while (auto event = popPendingKeyEvent()) {
|
||||
const std::size_t subscriberCount = keyboardSubscriberIds_.size();
|
||||
for (std::size_t index = 0; index < subscriberCount; ++index) {
|
||||
const u16 id = keyboardSubscriberIds_[index];
|
||||
auto* process = processAt(id);
|
||||
if (!process || allocationSizeAt(id) < 0x34 ||
|
||||
(readLe16(*process, 0x16) & 0x08) != 0 ||
|
||||
(readLe16(*process, 0x18) & 0x180) != 0x80 ||
|
||||
(reinterpret_cast<const u8*>(process)[0x32] & 0x10) == 0) {
|
||||
continue;
|
||||
}
|
||||
KeyEvent copy = *event;
|
||||
deliver(*process, 0x10, 0, copy, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "remorse/fixed_map.h"
|
||||
#include "remorse/collision.h"
|
||||
#include "remorse/glob_archive.h"
|
||||
|
||||
#include "remorse/entity_dispatch_entry.h"
|
||||
|
|
@ -377,6 +378,9 @@ std::optional<std::vector<ItemFindZCandidate>> collectZMatches(const StagedFixed
|
|||
const auto area = itemFindAreaAroundItem(origin, shapes[origin.data.shape]);
|
||||
for (const auto itemNumber : itemFindCandidateOrder(area, *chains)) {
|
||||
const auto& candidate = *byId[itemNumber];
|
||||
if ((candidate.data.flags & liveItemEtherealFlag) != 0) {
|
||||
continue;
|
||||
}
|
||||
if (checkRectangle && (candidate.data.x < area.minX || candidate.data.x > area.maxX ||
|
||||
candidate.data.y < area.minY || candidate.data.y > area.maxY)) {
|
||||
continue;
|
||||
|
|
@ -455,7 +459,8 @@ std::optional<std::vector<u16>> dispatchStagedItemGrab(const StagedFixedMapItem&
|
|||
|
||||
LiveFixedMapItems::LiveFixedMapItems(std::vector<StagedFixedMapItem> items,
|
||||
std::vector<StagedContainedFixedMapItem> containedItems)
|
||||
: items_(std::move(items)), allocatedIds_(0x2800), hatchedGlobIds_(0x2800), parentById_(0x2800),
|
||||
: items_(std::move(items)), allocatedIds_(0x2800), destroyedIds_(0x2800),
|
||||
hatchedGlobIds_(0x2800), parentById_(0x2800),
|
||||
containedItemData_(0x2800), containedNextById_(0x2800), containedHeadById_(0x2800),
|
||||
containedTailById_(0x2800)
|
||||
{
|
||||
|
|
@ -734,6 +739,112 @@ bool LiveFixedMapItems::insertItem(StagedFixedMapItem item)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool LiveFixedMapItems::pushItemToEtherealVoid(u16 itemNumber)
|
||||
{
|
||||
const auto item = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& candidate) {
|
||||
return candidate.itemNumber == itemNumber;
|
||||
});
|
||||
if (item == items_.end()) {
|
||||
return false;
|
||||
}
|
||||
if ((item->data.flags & liveItemEtherealFlag) == 0) {
|
||||
item->data.flags |= liveItemEtherealFlag;
|
||||
etherealStack_.push_back(itemNumber);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<u16> LiveFixedMapItems::popItemFromEtherealVoid()
|
||||
{
|
||||
while (!etherealStack_.empty()) {
|
||||
const u16 itemNumber = etherealStack_.back();
|
||||
etherealStack_.pop_back();
|
||||
const auto item = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& candidate) {
|
||||
return candidate.itemNumber == itemNumber;
|
||||
});
|
||||
if (item == items_.end()) {
|
||||
continue;
|
||||
}
|
||||
item->data.flags &= static_cast<u16>(~liveItemEtherealFlag);
|
||||
return itemNumber;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<u16> LiveFixedMapItems::popItemToCoords(u16 x, u16 y, u8 z)
|
||||
{
|
||||
if (x > 0x7fff || y > 0x7fff) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto itemNumber = popItemFromEtherealVoid();
|
||||
if (!itemNumber) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto* item = findItem(*itemNumber);
|
||||
if (item && item->data.shape == 1408 && z > 0) {
|
||||
--z;
|
||||
}
|
||||
return moveItem(*itemNumber, x, y, z) ? itemNumber : std::nullopt;
|
||||
}
|
||||
|
||||
bool LiveFixedMapItems::destroyItem(u16 itemNumber)
|
||||
{
|
||||
if (itemNumber < 0x100 || itemNumber >= allocatedIds_.size() || !allocatedIds_[itemNumber] ||
|
||||
destroyedIds_[itemNumber] || containedHeadById_[itemNumber] != 0) {
|
||||
return false;
|
||||
}
|
||||
const auto worldItem = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& item) {
|
||||
return item.itemNumber == itemNumber;
|
||||
});
|
||||
const bool wasContained = worldItem == items_.end() && parentById_[itemNumber] != 0 &&
|
||||
containedItemData_[itemNumber].has_value();
|
||||
if (worldItem == items_.end() && !wasContained) {
|
||||
return false;
|
||||
}
|
||||
if (wasContained && !unlinkContainedItem(itemNumber)) {
|
||||
return false;
|
||||
}
|
||||
if (worldItem != items_.end()) {
|
||||
items_.erase(worldItem);
|
||||
}
|
||||
containedItemData_[itemNumber].reset();
|
||||
containedNextById_[itemNumber] = 0;
|
||||
parentById_[itemNumber] = 0;
|
||||
destroyedIds_[itemNumber] = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LiveFixedMapItems::canCreateItemAt(u16 shape, u16 x, u16 y, u8 z,
|
||||
const std::vector<ShapeTypeInfo>& shapes) const
|
||||
{
|
||||
constexpr u32 blockingShapeFlags = 0x0002 | 0x0004;
|
||||
if (shape >= shapes.size() || x > 0x7fff || y > 0x7fff) {
|
||||
return false;
|
||||
}
|
||||
const auto& createdShape = shapes[shape];
|
||||
if ((createdShape.flags & blockingShapeFlags) == 0) {
|
||||
return true;
|
||||
}
|
||||
const auto createdBody = makeCollisionBody(WorldPoint{static_cast<s32>(x) * crusaderCoordinateScale,
|
||||
static_cast<s32>(y) * crusaderCoordinateScale, z}, createdShape);
|
||||
for (const auto& item : items_) {
|
||||
if ((item.data.flags & liveItemEtherealFlag) != 0 || item.data.shape >= shapes.size()) {
|
||||
continue;
|
||||
}
|
||||
const auto& obstacleShape = shapes[item.data.shape];
|
||||
if ((obstacleShape.flags & blockingShapeFlags) == 0) {
|
||||
continue;
|
||||
}
|
||||
const WorldPoint itemLocation{static_cast<s32>(item.data.x) * crusaderCoordinateScale,
|
||||
static_cast<s32>(item.data.y) * crusaderCoordinateScale, item.data.z};
|
||||
if (collisionBodiesOverlap(createdBody, makeCollisionBody(itemLocation, obstacleShape,
|
||||
(item.data.flags & 0x0020) != 0))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LiveFixedMapItems::moveItemIntoContainer(u16 itemNumber, u16 containerItemNumber,
|
||||
const std::vector<ShapeTypeInfo>& shapes)
|
||||
{
|
||||
|
|
@ -809,6 +920,12 @@ bool LiveFixedMapItems::releaseItem(u16 itemNumber, const ProcessKernel& kernel)
|
|||
hatchedGlobIds_[itemNumber]) {
|
||||
return false;
|
||||
}
|
||||
if (destroyedIds_[itemNumber]) {
|
||||
destroyedIds_[itemNumber] = false;
|
||||
allocatedIds_[itemNumber] = false;
|
||||
freeItemIds_.push_back(itemNumber);
|
||||
return true;
|
||||
}
|
||||
const auto worldItem = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& item) {
|
||||
return item.itemNumber == itemNumber;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ namespace {
|
|||
|
||||
constexpr u16 flagInvisible = 0x0010;
|
||||
constexpr u16 flagFlipped = 0x0020;
|
||||
constexpr u16 flagEthereal = liveItemEtherealFlag;
|
||||
constexpr u32 siSolid = 0x0002;
|
||||
constexpr u32 siLand = 0x0008;
|
||||
constexpr u32 siOccl = 0x0010;
|
||||
|
|
@ -135,6 +136,9 @@ std::vector<SceneItem> collectSceneItems(
|
|||
if ((item.flags & flagInvisible) != 0 && !options.includeHiddenMarkers) {
|
||||
continue;
|
||||
}
|
||||
if ((item.flags & flagEthereal) != 0) {
|
||||
continue;
|
||||
}
|
||||
if ((info.flags & siRoof) != 0 && !options.includeRoofs) {
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
12
rebuild/remorse/src/ported/controller_fire_sleep_gate.cpp
Normal file
12
rebuild/remorse/src/ported/controller_fire_sleep_gate.cpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include "remorse/isometric_controls.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
bool controllerFireAttemptSuppressed(bool previousAttemptSlept, u8 scanCode,
|
||||
u8 eventMode, u8 keyState)
|
||||
{
|
||||
return previousAttemptSlept && eventMode != 0 &&
|
||||
(scanCode == 0x39 || (scanCode == 0x52 && keyState == 0));
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
|
|
@ -6,5 +6,13 @@ namespace remorse::ported {
|
|||
|
||||
void setEntityDispatchEntryGroupId(EntityDispatchEntryBase& entry, u8 groupId,
|
||||
u16 (&groupCounts)[32]);
|
||||
void incrementEntityDispatchEntryGroupId(EntityDispatchEntryBase& entry, ProcessKernel& kernel,
|
||||
u16 (&groupCounts)[32]);
|
||||
bool prepareProcessForSchedulerRun(EntityDispatchEntryBase& entry, u8 activeGroup,
|
||||
bool waitingForProcess);
|
||||
u8 selectHighestOrdinaryProcessGroup(EntityDispatchEntryBase* const* processes,
|
||||
std::size_t processCount);
|
||||
bool isKeyboardInterruptDispatchEligible(const EntityDispatchEntryBase& process,
|
||||
std::size_t allocationSize, u8 activeGroup);
|
||||
|
||||
} // namespace remorse::ported
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
#include "entity_dispatch_entry_port.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace remorse::ported {
|
||||
|
||||
void setEntityDispatchEntryGroupId(EntityDispatchEntryBase& entry, u8 groupId,
|
||||
|
|
@ -15,4 +17,19 @@ void setEntityDispatchEntryGroupId(EntityDispatchEntryBase& entry, u8 groupId,
|
|||
}
|
||||
}
|
||||
|
||||
void incrementEntityDispatchEntryGroupId(EntityDispatchEntryBase& entry, ProcessKernel& kernel,
|
||||
u16 (&groupCounts)[32])
|
||||
{
|
||||
const u8 previousGroup = entry.bytes[0x08] & 0x1f;
|
||||
const u8 nextGroup = static_cast<u8>(previousGroup + 1);
|
||||
if (nextGroup >= 30) {
|
||||
throw std::invalid_argument("process group cannot advance beyond group 29");
|
||||
}
|
||||
if (kernel.activeGroup() != previousGroup) {
|
||||
throw std::invalid_argument("process group must match the active group");
|
||||
}
|
||||
setEntityDispatchEntryGroupId(entry, nextGroup, groupCounts);
|
||||
kernel.setActiveGroup(nextGroup);
|
||||
}
|
||||
|
||||
} // namespace remorse::ported
|
||||
29
rebuild/remorse/src/ported/game_start_fresh_map.cpp
Normal file
29
rebuild/remorse/src/ported/game_start_fresh_map.cpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include "remorse/game_start.h"
|
||||
|
||||
#include "remorse/entity_dispatch_entry.h"
|
||||
#include "remorse/fixed_map.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
bool startFreshGameMapOne(ProcessKernel& kernel, LiveFixedMapItems& items,
|
||||
const std::vector<ShapeTypeInfo>& shapes, u16 (&groupCounts)[32])
|
||||
{
|
||||
if (!items.initializeAvatar() ||
|
||||
!runFreshGameTeleporterStartup(kernel, items, 1, 0x1e, shapes, groupCounts)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const StagedFixedMapItem* avatar = items.findItem(1);
|
||||
if (avatar == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const auto stagingTeleporter = findNearbyActiveTeleportEgg(*avatar, items.mapItems(), shapes, 1);
|
||||
if (!stagingTeleporter) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return runFreshGameTeleporterStartup(kernel, items, stagingTeleporter->data.mapNumber,
|
||||
static_cast<u8>(stagingTeleporter->data.quality & 0xff), shapes, groupCounts);
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#include "remorse/entity_dispatch_entry.h"
|
||||
#include "entity_dispatch_entry_port.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
void ProcessKernel::dispatchPendingKeyEvents(
|
||||
void (*deliver)(EntityDispatchEntryBase&, u16, u16, KeyEvent&, void*), void* context)
|
||||
{
|
||||
while (auto event = popPendingKeyEvent()) {
|
||||
const std::size_t subscriberCount = keyboardSubscriberIds_.size();
|
||||
for (std::size_t index = 0; index < subscriberCount; ++index) {
|
||||
const u16 processId = keyboardSubscriberIds_[index];
|
||||
auto* process = processAt(processId);
|
||||
if (process == nullptr || !ported::isKeyboardInterruptDispatchEligible(
|
||||
*process, allocationSizeAt(processId), activeGroup_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
KeyEvent processEvent = *event;
|
||||
deliver(*process, 0x10, 0, processEvent, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#include "entity_dispatch_entry_port.h"
|
||||
|
||||
namespace remorse::ported {
|
||||
namespace {
|
||||
|
||||
u16 readLe16(const EntityDispatchEntryBase& process, u16 offset)
|
||||
{
|
||||
return static_cast<u16>(process.bytes[offset] |
|
||||
(static_cast<u16>(process.bytes[offset + 1]) << 8));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool isKeyboardInterruptDispatchEligible(const EntityDispatchEntryBase& process,
|
||||
std::size_t allocationSize, u8 activeGroup)
|
||||
{
|
||||
const u16 processFlags = readLe16(process, 0x16);
|
||||
const u16 extensionFlags = readLe16(process, 0x18);
|
||||
const u8 processGroup = process.bytes[0x08] & 0x1f;
|
||||
return allocationSize >= 0x34 && (processFlags & 0x08) == 0 &&
|
||||
(extensionFlags & 0x180) == 0x80 &&
|
||||
(process.bytes[0x32] & 0x10) != 0 &&
|
||||
(processGroup >= activeGroup || processGroup == 30 || processGroup == 31);
|
||||
}
|
||||
|
||||
} // namespace remorse::ported
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#include "entity_dispatch_entry_port.h"
|
||||
|
||||
namespace remorse::ported {
|
||||
|
||||
u8 selectHighestOrdinaryProcessGroup(EntityDispatchEntryBase* const* processes,
|
||||
std::size_t processCount)
|
||||
{
|
||||
u8 highestGroup = 1;
|
||||
for (std::size_t processId = 0; processId < processCount; ++processId) {
|
||||
const EntityDispatchEntryBase* process = processes[processId];
|
||||
if (process == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const u8 groupId = process->bytes[0x08] & 0x1f;
|
||||
if (groupId > highestGroup && groupId != 30 && groupId != 31) {
|
||||
highestGroup = groupId;
|
||||
}
|
||||
}
|
||||
return highestGroup;
|
||||
}
|
||||
|
||||
} // namespace remorse::ported
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
#include "entity_dispatch_entry_port.h"
|
||||
|
||||
namespace remorse::ported {
|
||||
namespace {
|
||||
|
||||
u16 readLe16(const EntityDispatchEntryBase& entry, u16 offset)
|
||||
{
|
||||
return static_cast<u16>(entry.bytes[offset] | (static_cast<u16>(entry.bytes[offset + 1]) << 8));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool prepareProcessForSchedulerRun(EntityDispatchEntryBase& entry, u8 activeGroup,
|
||||
bool waitingForProcess)
|
||||
{
|
||||
const u16 processFlags = readLe16(entry, 0x16);
|
||||
if ((processFlags & 0x40) != 0) {
|
||||
writeLe16(entry, 0x16, static_cast<u16>(processFlags & ~0x40));
|
||||
return false;
|
||||
}
|
||||
|
||||
const u8 groupId = entry.bytes[0x08] & 0x1f;
|
||||
return !waitingForProcess && (processFlags & 0x08) == 0 &&
|
||||
(readLe16(entry, 0x18) & 0x1000) == 0 && readLe16(entry, 0x24) == 0 &&
|
||||
readLe16(entry, 0x26) == 0 && (processFlags & 1) == 0 &&
|
||||
((processFlags & 2) != 0 || groupId == activeGroup || groupId == 30 || groupId == 31);
|
||||
}
|
||||
|
||||
} // namespace remorse::ported
|
||||
|
|
@ -68,6 +68,15 @@ bool UsecodeInterpreterState::pushWord(u16 value)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool UsecodeInterpreterState::pushByte(u8 value)
|
||||
{
|
||||
if (stackTop == 0) {
|
||||
return false;
|
||||
}
|
||||
valueStack[--stackTop] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UsecodeInterpreterState::pushDword(u32 value)
|
||||
{
|
||||
if (stackTop < sizeof(value)) {
|
||||
|
|
@ -107,7 +116,7 @@ std::optional<u32> UsecodeInterpreterState::popDword()
|
|||
}
|
||||
|
||||
UsecodeStepResult UsecodeInterpreterState::step(const std::vector<u8>& code,
|
||||
std::size_t& programCounter, std::vector<u8>& frame,
|
||||
std::size_t& programCounter, std::vector<u8>& frame, std::size_t frameBaseOffset,
|
||||
const UsecodeOpcodeServices& services)
|
||||
{
|
||||
if (programCounter >= code.size()) {
|
||||
|
|
@ -119,7 +128,7 @@ UsecodeStepResult UsecodeInterpreterState::step(const std::vector<u8>& code,
|
|||
return UsecodeStepResult::InvalidInstruction;
|
||||
}
|
||||
const auto rawOffset = static_cast<s8>(code[programCounter]);
|
||||
const auto localIndex = static_cast<std::ptrdiff_t>(usecodeFrameBaseOffset) + rawOffset;
|
||||
const auto localIndex = static_cast<std::ptrdiff_t>(frameBaseOffset) + rawOffset;
|
||||
const bool byteOperation = opcode == 0x00 || opcode == 0x3e;
|
||||
const std::size_t valueSize = byteOperation ? sizeof(u8) : sizeof(u16);
|
||||
if (localIndex < 0 || static_cast<std::size_t>(localIndex) > frame.size() ||
|
||||
|
|
@ -194,7 +203,7 @@ UsecodeStepResult UsecodeInterpreterState::step(const std::vector<u8>& code,
|
|||
temporaryWord = *result;
|
||||
return UsecodeStepResult::Advanced;
|
||||
}
|
||||
if (opcode == 0x14 || opcode == 0x1c || opcode == 0x24 || opcode == 0x2a ||
|
||||
if (opcode == 0x14 || opcode == 0x1c || opcode == 0x1e || opcode == 0x24 || opcode == 0x2a ||
|
||||
opcode == 0x2c || opcode == 0x36) {
|
||||
const auto right = popWord();
|
||||
const auto left = right ? popWord() : std::nullopt;
|
||||
|
|
@ -205,6 +214,8 @@ UsecodeStepResult UsecodeInterpreterState::step(const std::vector<u8>& code,
|
|||
u16 result = 0;
|
||||
if (opcode == 0x14) result = static_cast<u16>(*left + *right);
|
||||
else if (opcode == 0x1c) result = static_cast<u16>(*left - *right);
|
||||
else if (opcode == 0x1e) result = static_cast<u16>(
|
||||
static_cast<s16>(*left) * static_cast<s16>(*right));
|
||||
else if (opcode == 0x24) result = static_cast<u16>(*left == *right);
|
||||
else if (opcode == 0x2a) result = static_cast<u16>(*left <= *right);
|
||||
else if (opcode == 0x2c) result = static_cast<u16>(static_cast<s16>(*left) > static_cast<s16>(*right));
|
||||
|
|
@ -277,6 +288,13 @@ UsecodeStepResult UsecodeInterpreterState::step(const std::vector<u8>& code,
|
|||
stackHighWatermark = std::max(stackHighWatermark, stackTop);
|
||||
return UsecodeStepResult::Advanced;
|
||||
}
|
||||
if (opcode == 0x74) {
|
||||
if (programCounter >= code.size()) {
|
||||
return UsecodeStepResult::InvalidInstruction;
|
||||
}
|
||||
const u8 value = code[programCounter++];
|
||||
return pushByte(value) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
||||
}
|
||||
if (opcode == 0x12) {
|
||||
const auto value = popWord();
|
||||
if (!value) {
|
||||
|
|
@ -298,17 +316,36 @@ UsecodeStepResult UsecodeInterpreterState::step(const std::vector<u8>& code,
|
|||
if (programCounter >= code.size()) {
|
||||
return UsecodeStepResult::InvalidInstruction;
|
||||
}
|
||||
const std::size_t localOffset = code[programCounter++];
|
||||
const std::size_t localIndex = usecodeFrameBaseOffset + localOffset;
|
||||
if (localIndex > frame.size() || frame.size() - localIndex < sizeof(u32)) {
|
||||
const std::size_t operandOffset = code[programCounter++];
|
||||
const auto localIndex = static_cast<std::ptrdiff_t>(frameBaseOffset) + operandOffset;
|
||||
if (localIndex < 0 || static_cast<std::size_t>(localIndex) > frame.size() ||
|
||||
frame.size() - static_cast<std::size_t>(localIndex) < sizeof(u32)) {
|
||||
return UsecodeStepResult::InvalidInstruction;
|
||||
}
|
||||
const u32 value = static_cast<u32>(frame[localIndex]) |
|
||||
(static_cast<u32>(frame[localIndex + 1]) << 8) |
|
||||
(static_cast<u32>(frame[localIndex + 2]) << 16) |
|
||||
(static_cast<u32>(frame[localIndex + 3]) << 24);
|
||||
const std::size_t localOffset = static_cast<std::size_t>(localIndex);
|
||||
const u32 value = static_cast<u32>(frame[localOffset]) |
|
||||
(static_cast<u32>(frame[localOffset + 1]) << 8) |
|
||||
(static_cast<u32>(frame[localOffset + 2]) << 16) |
|
||||
(static_cast<u32>(frame[localOffset + 3]) << 24);
|
||||
return pushDword(value) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
||||
}
|
||||
if (opcode == 0x4b) {
|
||||
if (programCounter >= code.size()) {
|
||||
return UsecodeStepResult::InvalidInstruction;
|
||||
}
|
||||
if (!services.currentProcessId) {
|
||||
--programCounter;
|
||||
return UsecodeStepResult::UnsupportedOpcode;
|
||||
}
|
||||
const auto displacement = static_cast<s8>(code[programCounter++]);
|
||||
const auto localIndex = static_cast<std::ptrdiff_t>(frameBaseOffset) + displacement;
|
||||
if (localIndex < 0 || static_cast<std::size_t>(localIndex) >= frame.size()) {
|
||||
return UsecodeStepResult::InvalidInstruction;
|
||||
}
|
||||
const u32 address = (static_cast<u32>(services.currentProcessId()) << 16) |
|
||||
static_cast<u16>(localIndex);
|
||||
return pushDword(address) ? UsecodeStepResult::Advanced : UsecodeStepResult::StackFault;
|
||||
}
|
||||
if (opcode == 0x4c) {
|
||||
if (programCounter >= code.size()) {
|
||||
return UsecodeStepResult::InvalidInstruction;
|
||||
|
|
@ -482,6 +519,35 @@ std::optional<u16> UsecodeProcessTask::readMappedItemWord(FarPtr16 address) cons
|
|||
(static_cast<u16>(itemMemory[address.offset + 1]) << 8));
|
||||
}
|
||||
|
||||
std::optional<u16> UsecodeProcessTask::readMappedFarWord(FarPtr16 address) const
|
||||
{
|
||||
if (address.segment == processId) {
|
||||
if (address.offset > frame.size() || frame.size() - address.offset < sizeof(u16)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return static_cast<u16>(frame[address.offset] |
|
||||
(static_cast<u16>(frame[address.offset + 1]) << 8));
|
||||
}
|
||||
return readMappedItemWord(address);
|
||||
}
|
||||
|
||||
bool UsecodeProcessTask::writeMappedFarWord(FarPtr16 address, u16 value)
|
||||
{
|
||||
std::vector<u8>* memory = nullptr;
|
||||
if (address.segment == processId) {
|
||||
memory = &frame;
|
||||
} else if (address.segment == 0) {
|
||||
memory = &itemMemory;
|
||||
}
|
||||
if (memory == nullptr || address.offset > memory->size() ||
|
||||
memory->size() - address.offset < sizeof(u16)) {
|
||||
return false;
|
||||
}
|
||||
(*memory)[address.offset] = static_cast<u8>(value);
|
||||
(*memory)[address.offset + 1] = static_cast<u8>(value >> 8);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool writeUsecodeContextFactoryFields(u8* processBytes, std::size_t processSize,
|
||||
const UsecodeContextFactoryFields& fields)
|
||||
{
|
||||
|
|
@ -691,7 +757,62 @@ UsecodeStepResult UsecodeProcessTask::step(const UsecodeOpcodeServices& services
|
|||
if (halted) {
|
||||
return UsecodeStepResult::Returned;
|
||||
}
|
||||
const auto result = interpreter.step(code, programCounter, frame, services);
|
||||
if (programCounter < code.size() && code[programCounter] == 0x11) {
|
||||
if (code.size() - programCounter < 5) {
|
||||
return UsecodeStepResult::InvalidInstruction;
|
||||
}
|
||||
const u16 calleeClassId = static_cast<u16>(code[programCounter + 1] |
|
||||
(static_cast<u16>(code[programCounter + 2]) << 8));
|
||||
const u16 calleeEventSlot = static_cast<u16>(code[programCounter + 3] |
|
||||
(static_cast<u16>(code[programCounter + 4]) << 8));
|
||||
if (!services.callEvent) {
|
||||
return UsecodeStepResult::UnsupportedOpcode;
|
||||
}
|
||||
if (routineFrames.size() >= 31) {
|
||||
return UsecodeStepResult::StackFault;
|
||||
}
|
||||
auto calleeBody = services.callEvent(calleeClassId, calleeEventSlot);
|
||||
if (!calleeBody || calleeBody->bytes.size() <= usecodeEventBytecodeOffset) {
|
||||
return UsecodeStepResult::ServiceRejected;
|
||||
}
|
||||
constexpr std::size_t callFrameBytes = 3 * sizeof(u16);
|
||||
if (frameBaseOffset < callFrameBytes) {
|
||||
return UsecodeStepResult::StackFault;
|
||||
}
|
||||
routineFrames.push_back({classId, eventSlot, std::move(code), frame, frameBaseOffset,
|
||||
programCounter + 5});
|
||||
if (!interpreter.pushCodeAddress(calleeClassId, calleeEventSlot)) {
|
||||
code = std::move(routineFrames.back().code);
|
||||
frame = std::move(routineFrames.back().frame);
|
||||
routineFrames.pop_back();
|
||||
return UsecodeStepResult::StackFault;
|
||||
}
|
||||
const std::size_t calleeArgumentOffset = frameBaseOffset;
|
||||
classId = calleeClassId;
|
||||
eventSlot = calleeEventSlot;
|
||||
frameBaseOffset -= callFrameBytes;
|
||||
std::fill(frame.begin(), frame.end(), u8{0});
|
||||
const std::size_t copiedArgumentBytes = std::min(frame.size() - calleeArgumentOffset,
|
||||
interpreter.valueStack.size() - interpreter.stackTop);
|
||||
std::copy_n(interpreter.valueStack.begin() + interpreter.stackTop,
|
||||
copiedArgumentBytes, frame.begin() + calleeArgumentOffset);
|
||||
code = std::move(calleeBody->bytes);
|
||||
programCounter = usecodeEventBytecodeOffset;
|
||||
return UsecodeStepResult::Advanced;
|
||||
}
|
||||
const auto result = interpreter.step(code, programCounter, frame, frameBaseOffset, services);
|
||||
if (result == UsecodeStepResult::Returned && !routineFrames.empty()) {
|
||||
UsecodeRoutineFrame caller = std::move(routineFrames.back());
|
||||
routineFrames.pop_back();
|
||||
--interpreter.callFrameCount;
|
||||
classId = caller.classId;
|
||||
eventSlot = caller.eventSlot;
|
||||
frameBaseOffset = caller.frameBaseOffset;
|
||||
frame = std::move(caller.frame);
|
||||
code = std::move(caller.code);
|
||||
programCounter = caller.returnProgramCounter;
|
||||
return UsecodeStepResult::Advanced;
|
||||
}
|
||||
halted = result == UsecodeStepResult::Returned;
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
#include "remorse/entity_dispatch_entry.h"
|
||||
#include "remorse/file_system.h"
|
||||
#include "remorse/fixed_map.h"
|
||||
#include "remorse/game_start.h"
|
||||
#include "remorse/glob_archive.h"
|
||||
#include "remorse/isometric_controls.h"
|
||||
#include "remorse/map_renderer.h"
|
||||
|
|
@ -342,7 +343,16 @@ void runUsecodeTasks(AppState& state)
|
|||
remorse::UsecodeOpcodeServices services;
|
||||
services.readFarWord = [task](remorse::u16 offset, remorse::u16 segment)
|
||||
-> std::optional<remorse::u16> {
|
||||
return task->readMappedItemWord(remorse::FarPtr16{offset, segment});
|
||||
return task->readMappedFarWord(remorse::FarPtr16{offset, segment});
|
||||
};
|
||||
services.callEvent = [&app](remorse::u16 classId, remorse::u16 eventSlot)
|
||||
-> std::optional<remorse::UsecodeEventBody> {
|
||||
if (!app.usecodeArchive) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto usecodeClass = remorse::loadUsecodeClass(*app.usecodeArchive, classId);
|
||||
return usecodeClass
|
||||
? remorse::readUsecodeEventBody(*usecodeClass, eventSlot) : std::nullopt;
|
||||
};
|
||||
services.setInfo = [&app, processId](remorse::u16 eventType, remorse::u16 sourceItem) {
|
||||
return app.diagnosticKernel.setProcessInfo(processId, eventType, sourceItem);
|
||||
|
|
@ -360,6 +370,12 @@ void runUsecodeTasks(AppState& state)
|
|||
return static_cast<remorse::u16>(keycard < 64 &&
|
||||
(app.heldKeycards & (std::uint64_t{1} << keycard)) != 0);
|
||||
}
|
||||
if (ordinal == 0xac && argumentBytes == 2) {
|
||||
const remorse::u16 shape = static_cast<remorse::u16>(arguments[0] |
|
||||
(static_cast<remorse::u16>(arguments[1]) << 8));
|
||||
return shape < app.shapeTypes.size()
|
||||
? std::optional<remorse::u16>{app.shapeTypes[shape].family} : std::nullopt;
|
||||
}
|
||||
if (ordinal == 0x15 && argumentBytes == 6) {
|
||||
return remorse::u16{0};
|
||||
}
|
||||
|
|
@ -373,10 +389,88 @@ void runUsecodeTasks(AppState& state)
|
|||
(static_cast<remorse::u16>(arguments[1]) << 8));
|
||||
const remorse::u16 segment = static_cast<remorse::u16>(arguments[2] |
|
||||
(static_cast<remorse::u16>(arguments[3]) << 8));
|
||||
const auto itemNumber = task->readMappedItemWord(remorse::FarPtr16{offset, segment});
|
||||
if (ordinal == 0x0a && argumentBytes == 4) {
|
||||
const auto itemNumber = task->readMappedFarWord(remorse::FarPtr16{offset, segment});
|
||||
if (itemNumber && app.liveItems && app.liveItems->destroyItem(*itemNumber)) {
|
||||
app.usecodeWorldMutationPending = true;
|
||||
}
|
||||
return remorse::u16{0};
|
||||
}
|
||||
if (ordinal == 0x30 && argumentBytes == 4) {
|
||||
const auto poppedItem = app.liveItems
|
||||
? app.liveItems->popItemFromEtherealVoid() : std::nullopt;
|
||||
if (poppedItem) {
|
||||
app.usecodeWorldMutationPending = true;
|
||||
}
|
||||
return poppedItem.value_or(0);
|
||||
}
|
||||
if (ordinal == 0x20 && argumentBytes == 10) {
|
||||
const remorse::u16 x = static_cast<remorse::u16>(arguments[4] |
|
||||
(static_cast<remorse::u16>(arguments[5]) << 8));
|
||||
const remorse::u16 y = static_cast<remorse::u16>(arguments[6] |
|
||||
(static_cast<remorse::u16>(arguments[7]) << 8));
|
||||
const auto poppedItem = app.liveItems
|
||||
? app.liveItems->popItemToCoords(x, y, arguments[8]) : std::nullopt;
|
||||
if (poppedItem) {
|
||||
app.usecodeWorldMutationPending = true;
|
||||
}
|
||||
return poppedItem.value_or(0);
|
||||
}
|
||||
if (ordinal == 0x19 && argumentBytes == 14) {
|
||||
const remorse::u16 shape = static_cast<remorse::u16>(arguments[4] |
|
||||
(static_cast<remorse::u16>(arguments[5]) << 8));
|
||||
const remorse::u16 frame = static_cast<remorse::u16>(arguments[6] |
|
||||
(static_cast<remorse::u16>(arguments[7]) << 8));
|
||||
const remorse::u16 x = static_cast<remorse::u16>(arguments[8] |
|
||||
(static_cast<remorse::u16>(arguments[9]) << 8));
|
||||
const remorse::u16 y = static_cast<remorse::u16>(arguments[10] |
|
||||
(static_cast<remorse::u16>(arguments[11]) << 8));
|
||||
const remorse::u8 z = arguments[12];
|
||||
if (!app.liveItems || frame > 0xff ||
|
||||
!app.liveItems->canCreateItemAt(shape, x, y, z, app.shapeTypes)) {
|
||||
return remorse::u16{0};
|
||||
}
|
||||
remorse::StagedFixedMapItem createdItem;
|
||||
createdItem.recordIndex = static_cast<std::size_t>(-1);
|
||||
createdItem.data.x = x;
|
||||
createdItem.data.y = y;
|
||||
createdItem.data.z = z;
|
||||
createdItem.data.shape = shape;
|
||||
createdItem.data.frame = static_cast<remorse::u8>(frame);
|
||||
const auto itemNumber = app.liveItems->allocateItem(createdItem);
|
||||
if (!itemNumber || !task->writeMappedFarWord(
|
||||
remorse::FarPtr16{offset, segment}, *itemNumber)) {
|
||||
return remorse::u16{0};
|
||||
}
|
||||
app.usecodeWorldMutationPending = true;
|
||||
return remorse::u16{1};
|
||||
}
|
||||
const auto itemNumber = task->readMappedFarWord(remorse::FarPtr16{offset, segment});
|
||||
if (ordinal == 0x10 && argumentBytes == 4) {
|
||||
const auto* item = itemNumber && app.liveItems
|
||||
? app.liveItems->findItem(*itemNumber) : nullptr;
|
||||
return item ? static_cast<remorse::u16>(item->data.quality & 0xff)
|
||||
: remorse::u16{0};
|
||||
}
|
||||
if (!itemNumber) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (ordinal == 0x3c && argumentBytes == 4) {
|
||||
const auto* item = app.liveItems ? app.liveItems->findItem(*itemNumber) : nullptr;
|
||||
return item && item->data.shape < app.shapeTypes.size()
|
||||
? std::optional<remorse::u16>{app.shapeTypes[item->data.shape].family}
|
||||
: std::nullopt;
|
||||
}
|
||||
if (ordinal == 0x22 && argumentBytes == 4) {
|
||||
if (!app.liveItems || !app.liveItems->pushItemToEtherealVoid(*itemNumber)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
app.usecodeWorldMutationPending = true;
|
||||
return remorse::u16{0};
|
||||
}
|
||||
if (ordinal == 0x25) {
|
||||
return remorse::u16{0};
|
||||
}
|
||||
if (ordinal == 0xea && argumentBytes == 4) {
|
||||
const auto* item = app.liveItems ? app.liveItems->findItem(*itemNumber) : nullptr;
|
||||
return item ? std::optional<remorse::u16>{static_cast<remorse::u16>(item->data.quality & 0xff)}
|
||||
|
|
@ -390,6 +484,18 @@ void runUsecodeTasks(AppState& state)
|
|||
const auto* item = app.liveItems ? app.liveItems->findItem(*itemNumber) : nullptr;
|
||||
return item ? std::optional<remorse::u16>{item->data.mapNumber} : std::nullopt;
|
||||
}
|
||||
if (ordinal == 0x09 && argumentBytes == 4) {
|
||||
const auto* item = app.liveItems ? app.liveItems->findItem(*itemNumber) : nullptr;
|
||||
return item ? std::optional<remorse::u16>{item->data.z} : std::nullopt;
|
||||
}
|
||||
if (ordinal == 0x13 && argumentBytes == 4) {
|
||||
const auto* item = app.liveItems ? app.liveItems->findItem(*itemNumber) : nullptr;
|
||||
return item ? std::optional<remorse::u16>{item->data.x} : std::nullopt;
|
||||
}
|
||||
if (ordinal == 0x14 && argumentBytes == 4) {
|
||||
const auto* item = app.liveItems ? app.liveItems->findItem(*itemNumber) : nullptr;
|
||||
return item ? std::optional<remorse::u16>{item->data.y} : std::nullopt;
|
||||
}
|
||||
if (ordinal == 0x24 && argumentBytes == 6) {
|
||||
const remorse::u16 shape = static_cast<remorse::u16>(arguments[4] |
|
||||
(static_cast<remorse::u16>(arguments[5]) << 8));
|
||||
|
|
@ -399,7 +505,7 @@ void runUsecodeTasks(AppState& state)
|
|||
app.usecodeWorldMutationPending = true;
|
||||
return remorse::u16{0};
|
||||
}
|
||||
if (ordinal == 0x59 && argumentBytes == 6) {
|
||||
if ((ordinal == 0x02 || ordinal == 0x59) && argumentBytes == 6) {
|
||||
const remorse::u8 frame = static_cast<remorse::u8>(arguments[4] |
|
||||
(static_cast<remorse::u16>(arguments[5]) << 8));
|
||||
if (!app.liveItems || !app.liveItems->setItemFrame(*itemNumber, frame)) {
|
||||
|
|
@ -621,32 +727,24 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
: remorse::WorldPoint{(minX + maxX) / 2, (minY + maxY) / 2, 0};
|
||||
remorse::MapRenderOptions options = fullSceneOptions(state.mapIndex, spawn);
|
||||
remorse::LiveFixedMapItems liveItems(std::move(*stagedItems), std::move(containedItems));
|
||||
liveItems.initializeAvatar();
|
||||
if (startingEgg) {
|
||||
std::lock_guard<std::mutex> kernelLock(state.kernelMutex);
|
||||
if (!remorse::runFreshGameTeleporterStartup(state.diagnosticKernel, liveItems,
|
||||
static_cast<remorse::u16>(state.mapIndex), 0x1e, *shapeTypes,
|
||||
state.diagnosticGroupCounts)) {
|
||||
setLoadFailure(state, L"The teleporter process could not resolve its map and egg destination.");
|
||||
if (state.mapIndex != 1 || !remorse::startFreshGameMapOne(state.diagnosticKernel,
|
||||
liveItems, *shapeTypes, state.diagnosticGroupCounts)) {
|
||||
setLoadFailure(state, L"The source-derived fresh-game start could not resolve map 1's level destination.");
|
||||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
return;
|
||||
}
|
||||
const auto* avatar = liveItems.findItem(1);
|
||||
const auto stagingTeleporter = avatar ? remorse::findNearbyActiveTeleportEgg(
|
||||
*avatar, liveItems.mapItems(), *shapeTypes, static_cast<remorse::u8>(state.mapIndex))
|
||||
: std::nullopt;
|
||||
if (!stagingTeleporter || !remorse::runFreshGameTeleporterStartup(
|
||||
state.diagnosticKernel, liveItems, stagingTeleporter->data.mapNumber,
|
||||
static_cast<remorse::u8>(stagingTeleporter->data.quality & 0xff), *shapeTypes,
|
||||
state.diagnosticGroupCounts)) {
|
||||
setLoadFailure(state, L"The staging teleporter could not resolve its level destination.");
|
||||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
return;
|
||||
}
|
||||
avatar = liveItems.findItem(1);
|
||||
spawn = remorse::WorldPoint{avatar->data.x * 2, avatar->data.y * 2, avatar->data.z};
|
||||
options.viewCenter = spawn;
|
||||
} else {
|
||||
if (!liveItems.initializeAvatar()) {
|
||||
setLoadFailure(state, L"The player item could not be initialized.");
|
||||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
return;
|
||||
}
|
||||
remorse::StagedFixedMapItem directStart;
|
||||
directStart.data.x = static_cast<remorse::u16>(spawn.x / 2);
|
||||
directStart.data.y = static_cast<remorse::u16>(spawn.y / 2);
|
||||
|
|
|
|||
|
|
@ -271,6 +271,15 @@ int main()
|
|||
groupKernel.refreshActiveGroup(groupTable);
|
||||
assert(groupKernel.activeGroup() == 30 && groupKernel.groupSelectionPending());
|
||||
|
||||
remorse::ProcessKernel retainedGroupKernel;
|
||||
remorse::u16 retainedGroups[32]{};
|
||||
retainedGroupKernel.createProcess(2, retainedGroups);
|
||||
retainedGroupKernel.createProcess(5, retainedGroups);
|
||||
retainedGroupKernel.terminateProcess(1, retainedGroups);
|
||||
retainedGroupKernel.setActiveGroup(3);
|
||||
retainedGroupKernel.refreshActiveGroup(retainedGroups);
|
||||
assert(retainedGroupKernel.activeGroup() == 5 && !retainedGroupKernel.groupSelectionPending());
|
||||
|
||||
remorse::ProcessKernel scheduler;
|
||||
remorse::u16 scheduledGroups[32]{};
|
||||
assert(scheduler.activeGroup() == 1 && !scheduler.groupSelectionPending());
|
||||
|
|
@ -296,6 +305,21 @@ int main()
|
|||
assert((ran == std::vector<remorse::u16>{2, 3}));
|
||||
assert(specialGroup->bytes[0x08] == 30);
|
||||
|
||||
remorse::ProcessKernel gateKernel;
|
||||
remorse::u16 gateGroups[32]{};
|
||||
auto* linkedAt24 = gateKernel.createProcess(2, gateGroups);
|
||||
auto* linkedAt26 = gateKernel.createProcess(2, gateGroups);
|
||||
auto* readyProcess = gateKernel.createProcess(2, gateGroups);
|
||||
auto* daemonProcess = gateKernel.createProcess(3, gateGroups);
|
||||
linkedAt24->bytes[0x24] = 1;
|
||||
linkedAt26->bytes[0x26] = 1;
|
||||
daemonProcess->bytes[0x16] |= 2;
|
||||
std::vector<remorse::u16> gatedRuns;
|
||||
gateKernel.scanRunnableProcesses(2, recordRun, &gatedRuns);
|
||||
assert(gatedRuns.size() == 2);
|
||||
assert(gateKernel.processAt(gatedRuns[0]) == readyProcess);
|
||||
assert(gateKernel.processAt(gatedRuns[1]) == daemonProcess);
|
||||
|
||||
remorse::ProcessKernel automaticGroupKernel;
|
||||
remorse::u16 automaticGroupCounts[32]{};
|
||||
automaticGroupKernel.createProcess(3, automaticGroupCounts);
|
||||
|
|
@ -965,6 +989,29 @@ int main()
|
|||
event.scanCode, event.modifiers, event.field8 != 0);
|
||||
}, &deliveredMovement);
|
||||
assert(deliveredMovement.request().animation == 1 && deliveredMovement.request().direction == 14);
|
||||
inputKernel.setActiveGroup(2);
|
||||
std::vector<remorse::u8> groupFilteredScans;
|
||||
assert(inputKernel.queuePendingKeyEvent(wrapped));
|
||||
inputKernel.dispatchPendingKeyEvents([](remorse::EntityDispatchEntryBase&,
|
||||
remorse::u16, remorse::u16, remorse::KeyEvent& event, void* context) {
|
||||
static_cast<std::vector<remorse::u8>*>(context)->push_back(event.scanCode);
|
||||
}, &groupFilteredScans);
|
||||
assert(groupFilteredScans.empty());
|
||||
remorse::EntityDispatchEntry::setGroupId(inputController->base, 2, inputGroups);
|
||||
assert(inputKernel.queuePendingKeyEvent(wrapped));
|
||||
inputKernel.dispatchPendingKeyEvents([](remorse::EntityDispatchEntryBase&,
|
||||
remorse::u16, remorse::u16, remorse::KeyEvent& event, void* context) {
|
||||
static_cast<std::vector<remorse::u8>*>(context)->push_back(event.scanCode);
|
||||
}, &groupFilteredScans);
|
||||
assert((groupFilteredScans == std::vector<remorse::u8>{0x48}));
|
||||
inputKernel.setActiveGroup(31);
|
||||
remorse::EntityDispatchEntry::setGroupId(inputController->base, 30, inputGroups);
|
||||
assert(inputKernel.queuePendingKeyEvent(wrapped));
|
||||
inputKernel.dispatchPendingKeyEvents([](remorse::EntityDispatchEntryBase&,
|
||||
remorse::u16, remorse::u16, remorse::KeyEvent& event, void* context) {
|
||||
static_cast<std::vector<remorse::u8>*>(context)->push_back(event.scanCode);
|
||||
}, &groupFilteredScans);
|
||||
assert((groupFilteredScans == std::vector<remorse::u8>{0x48, 0x48}));
|
||||
wrapped.field8 = 0;
|
||||
assert(inputKernel.queuePendingKeyEvent(wrapped));
|
||||
inputKernel.dispatchPendingKeyEvents([](remorse::EntityDispatchEntryBase&,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
#include "remorse/collision.h"
|
||||
#include "remorse/animation_archive.h"
|
||||
#include "remorse/entity_dispatch_entry.h"
|
||||
#include "remorse/game_start.h"
|
||||
#include "remorse/file_system.h"
|
||||
#include "remorse/flex_archive.h"
|
||||
#include "remorse/fixed_map.h"
|
||||
#include "remorse/glob_archive.h"
|
||||
#include "remorse/isometric_controls.h"
|
||||
#include "remorse/map_renderer.h"
|
||||
#include "remorse/type_flags.h"
|
||||
#include "remorse/usecode_archive.h"
|
||||
|
|
@ -14,6 +17,11 @@
|
|||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
int main()
|
||||
{
|
||||
|
|
@ -360,6 +368,80 @@ int main()
|
|||
integratedStartupKernel.processAt(processIdOf(integratedTimer)) == &integratedTimer->base &&
|
||||
integratedStartupKernel.processAt(processIdOf(integratedKeyboard)) == &integratedKeyboard->base &&
|
||||
integratedStartupKernel.processAt(processIdOf(integratedController)) == &integratedController->base);
|
||||
remorse::LiveFixedMapItems freshGameWorld(*stagedItems);
|
||||
remorse::ProcessKernel freshGameKernel;
|
||||
remorse::u16 freshGameGroups[32]{};
|
||||
for (int reservedSlot = 0; reservedSlot < 3; ++reservedSlot) {
|
||||
freshGameKernel.createProcess(1, freshGameGroups);
|
||||
}
|
||||
auto* freshGameTimer = freshGameKernel.createSystemTimerProcess(1, freshGameGroups);
|
||||
auto* freshGameKeyboard = freshGameKernel.createKeyboardProcess(1, freshGameGroups);
|
||||
auto* freshGameController = freshGameKernel.createControllerProcess(1, freshGameGroups);
|
||||
assert(remorse::startFreshGameMapOne(freshGameKernel, freshGameWorld, *typeFlags, freshGameGroups));
|
||||
const auto* freshGameAvatar = freshGameWorld.findItem(1);
|
||||
const auto* expectedLevelStart = freshGameWorld.findItem(6812);
|
||||
assert(freshGameAvatar && expectedLevelStart && freshGameAvatar->data.x == expectedLevelStart->data.x &&
|
||||
freshGameAvatar->data.y == expectedLevelStart->data.y && freshGameAvatar->data.z == expectedLevelStart->data.z &&
|
||||
freshGameAvatar->data.mapNumber == 1 && freshGameGroups[1] == 6 && freshGameTimer &&
|
||||
freshGameKeyboard && freshGameController &&
|
||||
freshGameKernel.processAt(processIdOf(freshGameTimer)) == &freshGameTimer->base &&
|
||||
freshGameKernel.processAt(processIdOf(freshGameKeyboard)) == &freshGameKeyboard->base &&
|
||||
freshGameKernel.processAt(processIdOf(freshGameController)) == &freshGameController->base);
|
||||
remorse::ControllerActorState bootTickActor;
|
||||
bootTickActor.mapNumber = 1;
|
||||
bootTickActor.direction = 14;
|
||||
remorse::ControllerMovementState bootTickController;
|
||||
bootTickController.setActorState(bootTickActor);
|
||||
remorse::KeyEvent bootInput{};
|
||||
bootInput.scanCode = 0x48;
|
||||
bootInput.field8 = 1;
|
||||
bootInput.field9 = 1;
|
||||
bootInput.fieldA = 1;
|
||||
assert(freshGameKernel.queuePendingKeyEvent(bootInput));
|
||||
freshGameKernel.dispatchPendingKeyEvents([](remorse::EntityDispatchEntryBase& process,
|
||||
remorse::u16 selector, remorse::u16 filter, remorse::KeyEvent& event, void* context) {
|
||||
if (process.bytes[0] == 0xa6 && process.bytes[1] == 0x3a && selector == 0x10 && filter == 0) {
|
||||
static_cast<remorse::ControllerMovementState*>(context)->handleKeyEvent(
|
||||
event.scanCode, event.modifiers, event.field8, event.field9,
|
||||
event.fieldA, event.field6);
|
||||
}
|
||||
}, &bootTickController);
|
||||
const auto bootMovement = bootTickController.request();
|
||||
assert(bootMovement.animation == 1 && bootMovement.direction == 14);
|
||||
remorse::AnimationClip bootTickAnimation{};
|
||||
bootTickAnimation.header = {17, 4, 0, 0};
|
||||
bootTickAnimation.frameCount = 17;
|
||||
bootTickAnimation.frames.resize(17 * 8);
|
||||
auto* bootTickProcess = freshGameKernel.createAnimPrimitiveProcess(1, 1,
|
||||
bootMovement.animation, bootMovement.direction,
|
||||
bootTickAnimation, freshGameGroups);
|
||||
const remorse::u16 bootTickProcessId = static_cast<remorse::u16>(bootTickProcess->base.bytes[2] |
|
||||
(static_cast<remorse::u16>(bootTickProcess->base.bytes[3]) << 8));
|
||||
assert(freshGameKernel.startAnimPrimitiveProcess(bootTickProcessId, freshGameWorld, bootTickAnimation));
|
||||
struct FreshGameTickContext {
|
||||
remorse::ProcessKernel& kernel;
|
||||
remorse::LiveFixedMapItems& items;
|
||||
const remorse::AnimationClip& clip;
|
||||
const std::vector<remorse::ShapeTypeInfo>& shapes;
|
||||
remorse::u16 processId;
|
||||
std::optional<bool> advanced;
|
||||
} freshGameTick{freshGameKernel, freshGameWorld, bootTickAnimation, *typeFlags, bootTickProcessId};
|
||||
freshGameKernel.deliverSystemTimerEvent();
|
||||
freshGameKernel.runTimedSchedulerPass(freshGameKernel.activeGroup(),
|
||||
[](remorse::EntityDispatchEntryBase& process, void* opaque) {
|
||||
auto& tick = *static_cast<FreshGameTickContext*>(opaque);
|
||||
if (&process == tick.kernel.processAt(tick.processId)) {
|
||||
const auto* avatar = tick.items.findItem(1);
|
||||
assert(avatar);
|
||||
tick.advanced = tick.kernel.advanceGroundedAnimPrimitive(tick.processId,
|
||||
tick.items, tick.clip, tick.shapes,
|
||||
remorse::calculateFastAreaRange({avatar->data.x * 2, avatar->data.y * 2,
|
||||
avatar->data.z}));
|
||||
}
|
||||
}, &freshGameTick);
|
||||
assert(freshGameKernel.systemTimerTick() == 1 && freshGameTick.advanced == true &&
|
||||
freshGameWorld.findNpcAnimation(1)->frame == 1 &&
|
||||
freshGameWorld.findItem(1)->itemNumber == 1);
|
||||
const auto destinationUseCandidates = remorse::itemGrabSearchCandidates(
|
||||
*integratedAvatar, integratedStartupWorld.mapItems(), *typeFlags);
|
||||
assert(destinationUseCandidates);
|
||||
|
|
@ -417,6 +499,530 @@ int main()
|
|||
}
|
||||
}
|
||||
assert(eligibleMapUseItems == 218 && keycardUseItems == 10 && boxItem);
|
||||
struct RankedUseTarget {
|
||||
remorse::u32 distance = 0;
|
||||
remorse::u16 itemNumber = 0;
|
||||
remorse::u16 classId = 0;
|
||||
std::string className;
|
||||
};
|
||||
std::vector<RankedUseTarget> rankedUseTargets;
|
||||
for (const auto& item : *stagedItems) {
|
||||
if (item.data.shape >= typeFlags->size()) continue;
|
||||
const auto& shape = (*typeFlags)[item.data.shape];
|
||||
const remorse::UsecodeItemIdentity targetIdentity{item.itemNumber, item.data.shape,
|
||||
item.data.flags, item.data.quality, shape.family, false};
|
||||
const auto eligible = remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
||||
targetIdentity, remorse::UsecodeClassBases{0, 0x800, 0x900}, 0x02, 1);
|
||||
if (!eligible) continue;
|
||||
const remorse::s32 targetX = item.data.x;
|
||||
const remorse::s32 targetY = item.data.y;
|
||||
const remorse::s32 startX = integratedAvatar->data.x;
|
||||
const remorse::s32 startY = integratedAvatar->data.y;
|
||||
const auto absDelta = [](remorse::s32 left, remorse::s32 right) {
|
||||
return left < right ? right - left : left - right;
|
||||
};
|
||||
rankedUseTargets.push_back({static_cast<remorse::u32>(
|
||||
absDelta(targetX, startX) + absDelta(targetY, startY)), item.itemNumber,
|
||||
eligible->classId, eligible->className});
|
||||
}
|
||||
std::sort(rankedUseTargets.begin(), rankedUseTargets.end(), [](const auto& left, const auto& right) {
|
||||
return left.distance < right.distance;
|
||||
});
|
||||
std::cout << "Nearest map-1 use targets from destination pad:\n";
|
||||
for (std::size_t index = 0; index < std::min<std::size_t>(12, rankedUseTargets.size()); ++index) {
|
||||
const auto& target = rankedUseTargets[index];
|
||||
const auto* item = integratedStartupWorld.findItem(target.itemNumber);
|
||||
assert(item);
|
||||
std::cout << " item " << target.itemNumber << " distance " << target.distance << " xy "
|
||||
<< item->data.x << ',' << item->data.y << ',' << static_cast<unsigned>(item->data.z)
|
||||
<< " shape/frame/quality/flags " << item->data.shape << '/' << static_cast<unsigned>(item->data.frame)
|
||||
<< '/' << item->data.quality << '/' << item->data.flags << " class " << target.className
|
||||
<< " (" << target.classId << ")\n";
|
||||
}
|
||||
constexpr remorse::s32 routeStep = 16;
|
||||
constexpr remorse::s32 routeRadius = 8192;
|
||||
constexpr remorse::s32 routeSide = routeRadius * 2 / routeStep + 1;
|
||||
constexpr remorse::s32 routeCenter = routeRadius / routeStep;
|
||||
constexpr std::size_t maximumExpandedNodes = 30000;
|
||||
remorse::LiveFixedMapItems approachWorld(*stagedItems);
|
||||
assert(approachWorld.initializeAvatar() && approachWorld.teleportAvatar(*levelStartEgg, 1));
|
||||
const auto* approachAvatar = approachWorld.findItem(1);
|
||||
assert(approachAvatar);
|
||||
const remorse::StagedFixedMapItem searchOrigin = *approachAvatar;
|
||||
const remorse::WorldPoint routeStart{searchOrigin.data.x, searchOrigin.data.y, searchOrigin.data.z};
|
||||
std::vector<const remorse::StagedFixedMapItem*> elevationCompatibleTargets;
|
||||
for (const auto& target : rankedUseTargets) {
|
||||
const auto* item = approachWorld.findItem(target.itemNumber);
|
||||
assert(item);
|
||||
if (remorse::classifyItemFindZMatch(searchOrigin, (*typeFlags)[searchOrigin.data.shape],
|
||||
*item, (*typeFlags)[item->data.shape], 3) != 0) {
|
||||
elevationCompatibleTargets.push_back(item);
|
||||
}
|
||||
}
|
||||
std::cout << "Use targets matching spawn elevation: " << elevationCompatibleTargets.size() << '\n';
|
||||
for (std::size_t index = 0; index < std::min<std::size_t>(12, elevationCompatibleTargets.size()); ++index) {
|
||||
const auto* item = elevationCompatibleTargets[index];
|
||||
std::cout << " item " << item->itemNumber << " at " << item->data.x << ',' << item->data.y
|
||||
<< ',' << static_cast<unsigned>(item->data.z) << '\n';
|
||||
}
|
||||
struct RouteGoalArea {
|
||||
remorse::s32 minX;
|
||||
remorse::s32 maxX;
|
||||
remorse::s32 minY;
|
||||
remorse::s32 maxY;
|
||||
};
|
||||
const auto& avatarShape = (*typeFlags)[searchOrigin.data.shape];
|
||||
std::vector<RouteGoalArea> routeGoalAreas;
|
||||
for (const auto* target : elevationCompatibleTargets) {
|
||||
const auto& targetShape = (*typeFlags)[target->data.shape];
|
||||
const remorse::s32 minX = std::max({static_cast<remorse::s32>(target->data.x) - 0x100,
|
||||
static_cast<remorse::s32>(target->data.x) - static_cast<remorse::s32>(targetShape.x) * 16 + 1, 0});
|
||||
const remorse::s32 maxX = std::min(static_cast<remorse::s32>(target->data.x) +
|
||||
static_cast<remorse::s32>(avatarShape.x) * 16 - 1, 0x7fff);
|
||||
const remorse::s32 minY = std::max({static_cast<remorse::s32>(target->data.y) - 0x100,
|
||||
static_cast<remorse::s32>(target->data.y) - static_cast<remorse::s32>(targetShape.y) * 16 + 1, 0});
|
||||
const remorse::s32 maxY = std::min(static_cast<remorse::s32>(target->data.y) +
|
||||
static_cast<remorse::s32>(avatarShape.y) * 16 - 1, 0x7fff);
|
||||
if (minX <= maxX && minY <= maxY) {
|
||||
routeGoalAreas.push_back({minX, maxX, minY, maxY});
|
||||
}
|
||||
}
|
||||
const auto routeCell = [&](const remorse::WorldPoint& point) {
|
||||
const remorse::s32 gridX = (point.x - routeStart.x) / routeStep + routeCenter;
|
||||
const remorse::s32 gridY = (point.y - routeStart.y) / routeStep + routeCenter;
|
||||
assert(gridX >= 0 && gridX < routeSide && gridY >= 0 && gridY < routeSide);
|
||||
return static_cast<std::size_t>(gridY * routeSide + gridX);
|
||||
};
|
||||
const auto pointForRouteCell = [&](int cell) {
|
||||
const auto index = static_cast<std::size_t>(cell);
|
||||
const remorse::s32 gridX = static_cast<remorse::s32>(index % routeSide) - routeCenter;
|
||||
const remorse::s32 gridY = static_cast<remorse::s32>(index / routeSide) - routeCenter;
|
||||
return remorse::WorldPoint{routeStart.x + gridX * routeStep,
|
||||
routeStart.y + gridY * routeStep, routeStart.z};
|
||||
};
|
||||
const auto distanceToRange = [](remorse::s32 value, remorse::s32 minimum, remorse::s32 maximum) {
|
||||
return value < minimum ? minimum - value : value > maximum ? value - maximum : 0;
|
||||
};
|
||||
const auto routeHeuristic = [&](const remorse::WorldPoint& point) {
|
||||
remorse::s32 bestDistance = std::numeric_limits<remorse::s32>::max();
|
||||
for (const auto& area : routeGoalAreas) {
|
||||
const remorse::s32 distance = distanceToRange(point.x, area.minX, area.maxX) +
|
||||
distanceToRange(point.y, area.minY, area.maxY);
|
||||
bestDistance = std::min(bestDistance, (distance + routeStep - 1) / routeStep);
|
||||
}
|
||||
return bestDistance == std::numeric_limits<remorse::s32>::max() ? 0 : bestDistance;
|
||||
};
|
||||
struct RouteOpenEntry {
|
||||
remorse::s32 estimate;
|
||||
remorse::s32 distance;
|
||||
int cell;
|
||||
};
|
||||
struct RouteOpenOrder {
|
||||
bool operator()(const RouteOpenEntry& left, const RouteOpenEntry& right) const
|
||||
{
|
||||
if (left.estimate != right.estimate) return left.estimate > right.estimate;
|
||||
return left.distance < right.distance;
|
||||
}
|
||||
};
|
||||
const std::size_t routeCellCount = static_cast<std::size_t>(routeSide * routeSide);
|
||||
std::vector<int> routeDistances(routeCellCount, std::numeric_limits<int>::max());
|
||||
std::vector<int> routeParents(routeCellCount, -2);
|
||||
const int startCell = static_cast<int>(routeCell(routeStart));
|
||||
routeDistances[static_cast<std::size_t>(startCell)] = 0;
|
||||
routeParents[static_cast<std::size_t>(startCell)] = -1;
|
||||
std::priority_queue<RouteOpenEntry, std::vector<RouteOpenEntry>, RouteOpenOrder> openRoutes;
|
||||
openRoutes.push({routeHeuristic(routeStart), 0, startCell});
|
||||
int targetCell = -1;
|
||||
remorse::u16 approachTargetItem = 0;
|
||||
std::size_t expandedNodes = 0;
|
||||
constexpr std::array<std::pair<remorse::s32, remorse::s32>, 4> routeDirections{{
|
||||
{routeStep, 0}, {-routeStep, 0}, {0, routeStep}, {0, -routeStep}}};
|
||||
while (!openRoutes.empty() && targetCell < 0 && expandedNodes < maximumExpandedNodes) {
|
||||
const RouteOpenEntry entry = openRoutes.top();
|
||||
openRoutes.pop();
|
||||
const std::size_t currentCellIndex = static_cast<std::size_t>(entry.cell);
|
||||
if (entry.distance != routeDistances[currentCellIndex]) continue;
|
||||
++expandedNodes;
|
||||
const remorse::WorldPoint current = pointForRouteCell(entry.cell);
|
||||
remorse::StagedFixedMapItem currentOrigin = searchOrigin;
|
||||
currentOrigin.data.x = static_cast<remorse::u16>(current.x);
|
||||
currentOrigin.data.y = static_cast<remorse::u16>(current.y);
|
||||
const auto& originShape = (*typeFlags)[currentOrigin.data.shape];
|
||||
const auto interactionArea = remorse::itemFindAreaAroundItem(currentOrigin, originShape);
|
||||
const auto targetCandidate = std::find_if(elevationCompatibleTargets.begin(),
|
||||
elevationCompatibleTargets.end(), [&](const auto* candidate) {
|
||||
return candidate->data.x >= interactionArea.minX && candidate->data.x <= interactionArea.maxX &&
|
||||
candidate->data.y >= interactionArea.minY && candidate->data.y <= interactionArea.maxY &&
|
||||
remorse::classifyItemFindZMatch(currentOrigin, originShape, *candidate,
|
||||
(*typeFlags)[candidate->data.shape], 3) != 0;
|
||||
});
|
||||
if (targetCandidate != elevationCompatibleTargets.end()) {
|
||||
approachTargetItem = (*targetCandidate)->itemNumber;
|
||||
targetCell = entry.cell;
|
||||
break;
|
||||
}
|
||||
assert(approachWorld.moveItem(1, static_cast<remorse::u16>(current.x),
|
||||
static_cast<remorse::u16>(current.y), static_cast<remorse::u8>(current.z)));
|
||||
for (const auto [deltaX, deltaY] : routeDirections) {
|
||||
const remorse::WorldPoint destination{current.x + deltaX, current.y + deltaY, current.z};
|
||||
if (destination.x < 0 || destination.x > 0x7fff || destination.y < 0 || destination.y > 0x7fff ||
|
||||
destination.x < routeStart.x - routeRadius || destination.x > routeStart.x + routeRadius ||
|
||||
destination.y < routeStart.y - routeRadius || destination.y > routeStart.y + routeRadius) {
|
||||
continue;
|
||||
}
|
||||
const std::size_t cell = routeCell(destination);
|
||||
const int nextDistance = entry.distance + 1;
|
||||
if (nextDistance >= routeDistances[cell]) continue;
|
||||
const auto sweep = remorse::resolveSolidLiveSweep(approachWorld, 1, destination,
|
||||
*typeFlags, remorse::calculateFastAreaRange(current));
|
||||
if (!sweep || sweep->failed || sweep->failedDirections != 0 ||
|
||||
sweep->destination.x != destination.x || sweep->destination.y != destination.y ||
|
||||
sweep->destination.z != destination.z) {
|
||||
continue;
|
||||
}
|
||||
routeDistances[cell] = nextDistance;
|
||||
routeParents[cell] = entry.cell;
|
||||
openRoutes.push({nextDistance + routeHeuristic(destination), nextDistance,
|
||||
static_cast<int>(cell)});
|
||||
}
|
||||
}
|
||||
std::cout << "Route search expanded " << expandedNodes << " nodes\n";
|
||||
assert(targetCell >= 0);
|
||||
std::vector<remorse::WorldPoint> approachRoute;
|
||||
for (int cell = targetCell; cell >= 0;) {
|
||||
approachRoute.push_back(pointForRouteCell(cell));
|
||||
cell = routeParents[static_cast<std::size_t>(cell)];
|
||||
}
|
||||
std::reverse(approachRoute.begin(), approachRoute.end());
|
||||
const remorse::u16 routeGoalItem = approachTargetItem;
|
||||
assert(approachWorld.teleportAvatar(*levelStartEgg, 1));
|
||||
for (std::size_t index = 1; index < approachRoute.size(); ++index) {
|
||||
const auto& current = approachRoute[index - 1];
|
||||
const auto& destination = approachRoute[index];
|
||||
const auto sweep = remorse::resolveSolidLiveSweep(approachWorld, 1, destination,
|
||||
*typeFlags, remorse::calculateFastAreaRange(current));
|
||||
assert(sweep && !sweep->failed && sweep->failedDirections == 0 &&
|
||||
sweep->destination.x == destination.x && sweep->destination.y == destination.y &&
|
||||
sweep->destination.z == destination.z);
|
||||
assert(approachWorld.moveItem(1, static_cast<remorse::u16>(destination.x),
|
||||
static_cast<remorse::u16>(destination.y), static_cast<remorse::u8>(destination.z)));
|
||||
}
|
||||
const auto* endpointAvatar = approachWorld.findItem(1);
|
||||
assert(endpointAvatar);
|
||||
const auto endpointCandidates = remorse::itemGrabSearchCandidates(*endpointAvatar,
|
||||
approachWorld.mapItems(), *typeFlags);
|
||||
assert(endpointCandidates && std::any_of(endpointCandidates->begin(), endpointCandidates->end(),
|
||||
[&](const auto& candidate) { return candidate.itemNumber == routeGoalItem; }));
|
||||
std::optional<remorse::EligibleUsecodeEvent> approachEvent;
|
||||
approachTargetItem = 0;
|
||||
for (const auto& candidate : *endpointCandidates) {
|
||||
const auto* item = approachWorld.findItem(candidate.itemNumber);
|
||||
if (item == nullptr || item->data.shape >= typeFlags->size()) continue;
|
||||
const auto& shape = (*typeFlags)[item->data.shape];
|
||||
const remorse::UsecodeItemIdentity interactionIdentity{item->itemNumber, item->data.shape,
|
||||
item->data.flags, item->data.quality, shape.family, false};
|
||||
auto eligible = remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
||||
interactionIdentity, remorse::UsecodeClassBases{0, 0x800, 0x900}, 0x02, 1);
|
||||
if (eligible) {
|
||||
approachTargetItem = item->itemNumber;
|
||||
approachEvent = std::move(*eligible);
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert(approachEvent);
|
||||
const auto* approachTarget = approachWorld.findItem(approachTargetItem);
|
||||
assert(approachTarget);
|
||||
auto targetRenderer = remorse::loadMapSceneRenderer(*root, 1);
|
||||
assert(targetRenderer);
|
||||
remorse::MapRenderOptions targetRenderOptions;
|
||||
targetRenderOptions.mapIndex = 1;
|
||||
targetRenderOptions.maxWidth = 320;
|
||||
targetRenderOptions.maxHeight = 200;
|
||||
const remorse::WorldPoint targetWorldLocation{approachTarget->data.x * 2,
|
||||
approachTarget->data.y * 2, approachTarget->data.z};
|
||||
targetRenderOptions.viewCenter = targetWorldLocation;
|
||||
targetRenderOptions.worldRect = remorse::WorldRect{targetWorldLocation.x - 1,
|
||||
targetWorldLocation.y - 1, targetWorldLocation.x + 1, targetWorldLocation.y + 1};
|
||||
const auto targetBefore = targetRenderer->render(targetRenderOptions, &approachWorld);
|
||||
assert(targetBefore);
|
||||
const remorse::u16 originalTargetShape = approachTarget->data.shape;
|
||||
const remorse::u8 originalTargetFrame = approachTarget->data.frame;
|
||||
constexpr remorse::u16 interactionProcessId = 0x0340;
|
||||
remorse::UsecodeTaskRegistry interactionTasks;
|
||||
auto interactionContext = remorse::createUsecodeEventContext(interactionTasks.runtimeMemory(),
|
||||
*retailOwnerRows, approachEvent->classId, 1, approachTargetItem, approachEvent->body.bytes, 2);
|
||||
auto interactionTaskValue = remorse::UsecodeProcessTask::create(interactionProcessId,
|
||||
approachEvent->classId, 1, approachTargetItem, approachEvent->body.bytes, 2);
|
||||
assert(interactionContext && interactionTaskValue);
|
||||
const std::vector<remorse::u8> interactionProcessIdBytes{0x40, 0x03};
|
||||
assert(interactionTasks.runtimeMemory().write(interactionContext->processIdOutput,
|
||||
interactionProcessIdBytes));
|
||||
assert(interactionTasks.addWithContext(std::move(*interactionTaskValue), std::move(*interactionContext)));
|
||||
auto* interactionTask = interactionTasks.find(interactionProcessId);
|
||||
assert(interactionTask);
|
||||
remorse::UsecodeOpcodeServices interactionServices;
|
||||
interactionServices.readFarWord = [interactionTask](remorse::u16 offset, remorse::u16 segment)
|
||||
-> std::optional<remorse::u16> {
|
||||
return interactionTask->readMappedFarWord(remorse::FarPtr16{offset, segment});
|
||||
};
|
||||
interactionServices.setInfo = [](remorse::u16, remorse::u16) { return true; };
|
||||
interactionServices.processCount = []() { return remorse::u16{2}; };
|
||||
interactionServices.currentProcessId = []() { return interactionProcessId; };
|
||||
std::optional<std::pair<remorse::u16, remorse::u16>> rejectedEvent;
|
||||
interactionServices.callEvent = [&usecode, interactionTask, &rejectedEvent](
|
||||
remorse::u16 classId, remorse::u16 eventSlot)
|
||||
-> std::optional<remorse::UsecodeEventBody> {
|
||||
std::cout << "Nested event call from " << interactionTask->classId << '/'
|
||||
<< interactionTask->eventSlot << " pc " << interactionTask->programCounter
|
||||
<< " BP " << interactionTask->frameBaseOffset << " stack "
|
||||
<< interactionTask->interpreter.stackTop << ':';
|
||||
for (std::size_t index = interactionTask->interpreter.stackTop;
|
||||
index < std::min(interactionTask->interpreter.valueStack.size(),
|
||||
interactionTask->interpreter.stackTop + 12); ++index) {
|
||||
std::cout << ' ' << static_cast<unsigned>(interactionTask->interpreter.valueStack[index]);
|
||||
}
|
||||
std::cout << " frame:";
|
||||
for (std::size_t index = 0x30; index < 0x50; ++index) {
|
||||
std::cout << ' ' << static_cast<unsigned>(interactionTask->frame[index]);
|
||||
}
|
||||
std::cout << '\n';
|
||||
const auto usecodeClass = remorse::loadUsecodeClass(*usecode, classId);
|
||||
auto body = usecodeClass
|
||||
? remorse::readUsecodeEventBody(*usecodeClass, eventSlot) : std::nullopt;
|
||||
if (!body) rejectedEvent = std::pair{classId, eventSlot};
|
||||
return body;
|
||||
};
|
||||
remorse::u16 nextInteractionChildId = 0x0350;
|
||||
std::vector<std::pair<remorse::u16, remorse::u16>> spawnedInteractionEvents;
|
||||
std::optional<std::pair<remorse::u16, remorse::u16>> rejectedSpawn;
|
||||
interactionServices.spawn = [&usecode, &nextInteractionChildId, &spawnedInteractionEvents,
|
||||
&rejectedSpawn](remorse::u16 classId,
|
||||
remorse::u16 eventSlot, remorse::u8, remorse::u8, remorse::u16, const remorse::u8*)
|
||||
-> std::optional<remorse::u16> {
|
||||
const auto usecodeClass = remorse::loadUsecodeClass(*usecode, classId);
|
||||
const auto body = usecodeClass
|
||||
? remorse::readUsecodeEventBody(*usecodeClass, eventSlot) : std::nullopt;
|
||||
if (!body) {
|
||||
rejectedSpawn = std::pair{classId, eventSlot};
|
||||
return std::nullopt;
|
||||
}
|
||||
spawnedInteractionEvents.emplace_back(classId, eventSlot);
|
||||
return nextInteractionChildId++;
|
||||
};
|
||||
std::optional<remorse::u16> rejectedIntrinsic;
|
||||
std::vector<std::pair<remorse::u16, remorse::u8>> interactionIntrinsics;
|
||||
std::vector<std::pair<remorse::u16, remorse::u16>> queriedShapeFamilies;
|
||||
std::vector<std::pair<remorse::u16, remorse::u16>> queriedItemFamilies;
|
||||
interactionServices.callIntrinsic = [interactionTask, &approachWorld, &typeFlags,
|
||||
&rejectedIntrinsic, &interactionIntrinsics, &queriedShapeFamilies,
|
||||
&queriedItemFamilies](
|
||||
remorse::u16 ordinal, remorse::u8 argumentBytes, const remorse::u8* arguments)
|
||||
-> std::optional<remorse::u16> {
|
||||
interactionIntrinsics.emplace_back(ordinal, argumentBytes);
|
||||
if (ordinal == 0x00 && argumentBytes == 0) return remorse::u16{0};
|
||||
if (ordinal == 0xfd && argumentBytes == 2) return remorse::u16{0};
|
||||
if (ordinal == 0xac && argumentBytes == 2) {
|
||||
const remorse::u16 shape = static_cast<remorse::u16>(arguments[0] |
|
||||
(static_cast<remorse::u16>(arguments[1]) << 8));
|
||||
if (shape >= typeFlags->size()) return std::nullopt;
|
||||
const remorse::u16 family = (*typeFlags)[shape].family;
|
||||
queriedShapeFamilies.emplace_back(shape, family);
|
||||
return family;
|
||||
}
|
||||
if ((ordinal == 0x15 || ordinal == 0x3a) && argumentBytes == 6) return remorse::u16{0};
|
||||
if (argumentBytes < 4) {
|
||||
rejectedIntrinsic = ordinal;
|
||||
return std::nullopt;
|
||||
}
|
||||
const remorse::u16 offset = static_cast<remorse::u16>(arguments[0] |
|
||||
(static_cast<remorse::u16>(arguments[1]) << 8));
|
||||
const remorse::u16 segment = static_cast<remorse::u16>(arguments[2] |
|
||||
(static_cast<remorse::u16>(arguments[3]) << 8));
|
||||
if (ordinal == 0x0a && argumentBytes == 4) {
|
||||
const auto itemNumber = interactionTask->readMappedFarWord(remorse::FarPtr16{offset, segment});
|
||||
if (itemNumber) approachWorld.destroyItem(*itemNumber);
|
||||
return remorse::u16{0};
|
||||
}
|
||||
if (ordinal == 0x30 && argumentBytes == 4) {
|
||||
return approachWorld.popItemFromEtherealVoid().value_or(0);
|
||||
}
|
||||
if (ordinal == 0x20 && argumentBytes == 10) {
|
||||
const remorse::u16 x = static_cast<remorse::u16>(arguments[4] |
|
||||
(static_cast<remorse::u16>(arguments[5]) << 8));
|
||||
const remorse::u16 y = static_cast<remorse::u16>(arguments[6] |
|
||||
(static_cast<remorse::u16>(arguments[7]) << 8));
|
||||
return approachWorld.popItemToCoords(x, y, arguments[8]).value_or(0);
|
||||
}
|
||||
if (ordinal == 0x19 && argumentBytes == 14) {
|
||||
const remorse::u16 shape = static_cast<remorse::u16>(arguments[4] |
|
||||
(static_cast<remorse::u16>(arguments[5]) << 8));
|
||||
const remorse::u16 frame = static_cast<remorse::u16>(arguments[6] |
|
||||
(static_cast<remorse::u16>(arguments[7]) << 8));
|
||||
const remorse::u16 x = static_cast<remorse::u16>(arguments[8] |
|
||||
(static_cast<remorse::u16>(arguments[9]) << 8));
|
||||
const remorse::u16 y = static_cast<remorse::u16>(arguments[10] |
|
||||
(static_cast<remorse::u16>(arguments[11]) << 8));
|
||||
const remorse::u8 z = arguments[12];
|
||||
const bool legalPosition = frame <= 0xff &&
|
||||
approachWorld.canCreateItemAt(shape, x, y, z, *typeFlags);
|
||||
if (!legalPosition) {
|
||||
return remorse::u16{0};
|
||||
}
|
||||
remorse::StagedFixedMapItem createdItem;
|
||||
createdItem.recordIndex = static_cast<std::size_t>(-1);
|
||||
createdItem.data.x = x;
|
||||
createdItem.data.y = y;
|
||||
createdItem.data.z = z;
|
||||
createdItem.data.shape = shape;
|
||||
createdItem.data.frame = static_cast<remorse::u8>(frame);
|
||||
const auto itemNumber = approachWorld.allocateItem(createdItem);
|
||||
if (!itemNumber || !interactionTask->writeMappedFarWord(
|
||||
remorse::FarPtr16{offset, segment}, *itemNumber)) {
|
||||
return remorse::u16{0};
|
||||
}
|
||||
return remorse::u16{1};
|
||||
}
|
||||
const auto itemNumber = interactionTask->readMappedFarWord(remorse::FarPtr16{offset, segment});
|
||||
if (ordinal == 0x10 && argumentBytes == 4) {
|
||||
const auto* item = itemNumber ? approachWorld.findItem(*itemNumber) : nullptr;
|
||||
return item ? static_cast<remorse::u16>(item->data.quality & 0xff) : remorse::u16{0};
|
||||
}
|
||||
if (!itemNumber) return std::nullopt;
|
||||
const auto* item = approachWorld.findItem(*itemNumber);
|
||||
if (ordinal == 0x3c && argumentBytes == 4 && item && item->data.shape < typeFlags->size()) {
|
||||
const remorse::u16 family = (*typeFlags)[item->data.shape].family;
|
||||
queriedItemFamilies.emplace_back(*itemNumber, family);
|
||||
return family;
|
||||
}
|
||||
if (ordinal == 0x22 && argumentBytes == 4 && item) {
|
||||
return approachWorld.pushItemToEtherealVoid(*itemNumber)
|
||||
? std::optional<remorse::u16>{remorse::u16{0}} : std::nullopt;
|
||||
}
|
||||
if (ordinal == 0x25) return remorse::u16{0};
|
||||
if (ordinal == 0xea && argumentBytes == 4 && item) {
|
||||
return static_cast<remorse::u16>(item->data.quality & 0xff);
|
||||
}
|
||||
if (ordinal == 0x01 && argumentBytes == 4 && item) return item->data.frame;
|
||||
if (ordinal == 0x03 && argumentBytes == 4 && item) return item->data.mapNumber;
|
||||
if (ordinal == 0x09 && argumentBytes == 4 && item) return item->data.z;
|
||||
if (ordinal == 0x13 && argumentBytes == 4 && item) return item->data.x;
|
||||
if (ordinal == 0x14 && argumentBytes == 4 && item) return item->data.y;
|
||||
if (ordinal == 0x24 && argumentBytes == 6) {
|
||||
const remorse::u16 shape = static_cast<remorse::u16>(arguments[4] |
|
||||
(static_cast<remorse::u16>(arguments[5]) << 8));
|
||||
return approachWorld.setItemShape(*itemNumber, shape)
|
||||
? std::optional<remorse::u16>{remorse::u16{0}} : std::nullopt;
|
||||
}
|
||||
if ((ordinal == 0x02 || ordinal == 0x59) && argumentBytes == 6) {
|
||||
const remorse::u8 frame = static_cast<remorse::u8>(arguments[4] |
|
||||
(static_cast<remorse::u16>(arguments[5]) << 8));
|
||||
return approachWorld.setItemFrame(*itemNumber, frame)
|
||||
? std::optional<remorse::u16>{remorse::u16{0}} : std::nullopt;
|
||||
}
|
||||
rejectedIntrinsic = ordinal;
|
||||
return std::nullopt;
|
||||
};
|
||||
remorse::UsecodeStepResult lastInteractionResult = remorse::UsecodeStepResult::Advanced;
|
||||
bool interactionStoppedOnError = false;
|
||||
bool reachedInteractionMilestone = false;
|
||||
bool targetVisualChanged = false;
|
||||
const remorse::u16 originalTargetFlags = approachTarget->data.flags;
|
||||
const std::size_t originalWorldItemCount = approachWorld.mapItems().size();
|
||||
const auto targetRenderHasChanged = [&]() {
|
||||
const auto image = targetRenderer->render(targetRenderOptions, &approachWorld);
|
||||
return image && image->pixels.size() == targetBefore->pixels.size() &&
|
||||
!std::equal(targetBefore->pixels.begin(), targetBefore->pixels.end(), image->pixels.begin(),
|
||||
image->pixels.end(), [](const auto& left, const auto& right) {
|
||||
return left.red == right.red && left.green == right.green && left.blue == right.blue;
|
||||
});
|
||||
};
|
||||
for (unsigned tick = 0; tick < 8 && !reachedInteractionMilestone && !interactionTask->halted &&
|
||||
!interactionStoppedOnError; ++tick) {
|
||||
interactionTask->interpreter.impliedProcessId = 0;
|
||||
for (unsigned instruction = 0; instruction < 256; ++instruction) {
|
||||
lastInteractionResult = interactionTask->step(interactionServices);
|
||||
const auto* item = approachWorld.findItem(approachTargetItem);
|
||||
targetVisualChanged = !item || item->data.shape != originalTargetShape ||
|
||||
item->data.frame != originalTargetFrame;
|
||||
if (lastInteractionResult == remorse::UsecodeStepResult::Advanced ||
|
||||
lastInteractionResult == remorse::UsecodeStepResult::SetInfo ||
|
||||
lastInteractionResult == remorse::UsecodeStepResult::Spawned) {
|
||||
if (!spawnedInteractionEvents.empty() && targetRenderHasChanged()) {
|
||||
targetVisualChanged = true;
|
||||
reachedInteractionMilestone = true;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (lastInteractionResult != remorse::UsecodeStepResult::Yielded &&
|
||||
lastInteractionResult != remorse::UsecodeStepResult::Returned) {
|
||||
interactionStoppedOnError = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
const auto* finalInteractionTarget = approachWorld.findItem(approachTargetItem);
|
||||
targetVisualChanged = targetVisualChanged || finalInteractionTarget == nullptr ||
|
||||
finalInteractionTarget->data.shape != originalTargetShape ||
|
||||
finalInteractionTarget->data.frame != originalTargetFrame ||
|
||||
finalInteractionTarget->data.flags != originalTargetFlags ||
|
||||
approachWorld.mapItems().size() != originalWorldItemCount;
|
||||
std::cout << "Reachable target use event: class " << approachEvent->className << " ("
|
||||
<< approachEvent->classId << ") shape/frame " << originalTargetShape << '/'
|
||||
<< static_cast<unsigned>(originalTargetFrame) << " body bytes " << approachEvent->body.bytes.size()
|
||||
<< " VM result " << static_cast<unsigned>(lastInteractionResult) << " PC "
|
||||
<< interactionTask->programCounter << " class/event " << interactionTask->classId << '/'
|
||||
<< interactionTask->eventSlot << " call depth " << interactionTask->routineFrames.size()
|
||||
<< " rejected intrinsic " << rejectedIntrinsic.value_or(0xffff) << " rejected event "
|
||||
<< (rejectedEvent ? rejectedEvent->first : 0xffff) << '/'
|
||||
<< (rejectedEvent ? rejectedEvent->second : 0xffff) << " rejected spawn "
|
||||
<< (rejectedSpawn ? rejectedSpawn->first : 0xffff) << '/'
|
||||
<< (rejectedSpawn ? rejectedSpawn->second : 0xffff) << " spawned "
|
||||
<< spawnedInteractionEvents.size() << " changed " << targetVisualChanged << '\n';
|
||||
std::cout << "Selected target item " << approachTargetItem << " quality/flags "
|
||||
<< (finalInteractionTarget ? finalInteractionTarget->data.quality : 0) << '/'
|
||||
<< (finalInteractionTarget ? finalInteractionTarget->data.flags : 0) << '\n';
|
||||
for (const auto& [classId, eventSlot] : spawnedInteractionEvents) {
|
||||
std::cout << " spawned usecode " << classId << '/' << eventSlot << '\n';
|
||||
}
|
||||
std::cout << "Executed intrinsics:";
|
||||
for (const auto& [ordinal, argumentBytes] : interactionIntrinsics) {
|
||||
std::cout << ' ' << ordinal << '/' << static_cast<unsigned>(argumentBytes);
|
||||
}
|
||||
std::cout << '\n';
|
||||
for (const auto& [shape, family] : queriedShapeFamilies) {
|
||||
std::cout << " shape family " << shape << '=' << family << '\n';
|
||||
}
|
||||
for (const auto& [itemNumber, family] : queriedItemFamilies) {
|
||||
std::cout << " item family " << itemNumber << '=' << family << '\n';
|
||||
}
|
||||
if (lastInteractionResult == remorse::UsecodeStepResult::UnsupportedOpcode) {
|
||||
const std::size_t windowStart = interactionTask->programCounter > 4
|
||||
? interactionTask->programCounter - 4 : remorse::usecodeEventBytecodeOffset;
|
||||
const std::size_t windowEnd = std::min(interactionTask->code.size(),
|
||||
interactionTask->programCounter + 8);
|
||||
std::cout << "Unsupported usecode bytes:";
|
||||
for (std::size_t index = windowStart; index < windowEnd; ++index) {
|
||||
std::cout << ' ' << index << ':' << static_cast<unsigned>(interactionTask->code[index]);
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
assert(reachedInteractionMilestone && targetVisualChanged && !interactionStoppedOnError);
|
||||
const auto targetAfter = targetRenderer->render(targetRenderOptions, &approachWorld);
|
||||
assert(targetAfter && targetBefore->pixels.size() == targetAfter->pixels.size());
|
||||
const bool targetPixelsChanged = !std::equal(targetBefore->pixels.begin(), targetBefore->pixels.end(),
|
||||
targetAfter->pixels.begin(), targetAfter->pixels.end(), [](const auto& left, const auto& right) {
|
||||
return left.red == right.red && left.green == right.green && left.blue == right.blue;
|
||||
});
|
||||
assert(targetPixelsChanged);
|
||||
std::cout << "Visible target mutation: shape/frame "
|
||||
<< (finalInteractionTarget ? finalInteractionTarget->data.shape : 0xffff) << '/'
|
||||
<< (finalInteractionTarget ? static_cast<unsigned>(finalInteractionTarget->data.frame) : 0xff)
|
||||
<< " pixels changed " << targetPixelsChanged << '\n';
|
||||
std::cout << "Reachable target Item_Grab route: item " << approachTargetItem
|
||||
<< " steps " << approachRoute.size() - 1 << " expanded " << expandedNodes << " endpoint "
|
||||
<< approachRoute.back().x
|
||||
<< ',' << approachRoute.back().y << ',' << approachRoute.back().z << '\n';
|
||||
const auto globPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "GLOB.FLX");
|
||||
const auto globs = globPath ? remorse::loadGlobObjects(*globPath) : std::nullopt;
|
||||
assert(globs);
|
||||
|
|
|
|||
|
|
@ -51,6 +51,18 @@ int main()
|
|||
assert(commandController.takeCommandRequest()->command == remorse::ControllerCommand::OpenInventory);
|
||||
commandController.handleKeyEvent(0x39, 0, 1, 1, 0);
|
||||
assert(!commandController.takeCommandRequest());
|
||||
assert(remorse::controllerFireAttemptSuppressed(true, 0x39, 1, 1));
|
||||
assert(remorse::controllerFireAttemptSuppressed(true, 0x52, 1, 0));
|
||||
assert(!remorse::controllerFireAttemptSuppressed(true, 0x52, 1, 1));
|
||||
assert(!remorse::controllerFireAttemptSuppressed(true, 0x39, 0, 1));
|
||||
remorse::ControllerActorState sleepingFireActor;
|
||||
sleepingFireActor.lastFireAttemptWentToSleep = true;
|
||||
remorse::ControllerMovementState sleepingFireController;
|
||||
sleepingFireController.setActorState(sleepingFireActor);
|
||||
sleepingFireController.handleKeyEvent(0x39, 0, 1, 1, 1);
|
||||
assert(!sleepingFireController.takeCommandRequest());
|
||||
sleepingFireController.handleKeyEvent(0x52, 0, 1, 1, 1);
|
||||
assert(sleepingFireController.takeCommandRequest()->command == remorse::ControllerCommand::FireWeapon);
|
||||
remorse::ControllerActorState restrictedCommandActor;
|
||||
restrictedCommandActor.mapNumber = 0x40;
|
||||
commandController.setActorState(restrictedCommandActor);
|
||||
|
|
|
|||
|
|
@ -10,6 +10,26 @@ int main()
|
|||
assert(emptyStack.has_value());
|
||||
assert(!emptyStack->popWord().has_value());
|
||||
assert(!emptyStack->popDword().has_value());
|
||||
auto loopScriptState = remorse::UsecodeInterpreterState::initialize({});
|
||||
assert(loopScriptState);
|
||||
std::size_t loopScriptPc = 0;
|
||||
std::vector<remorse::u8> loopScriptCode{0x74, 0x24};
|
||||
std::vector<remorse::u8> loopScriptFrame;
|
||||
remorse::UsecodeOpcodeServices loopScriptServices;
|
||||
assert(loopScriptState->step(loopScriptCode, loopScriptPc, loopScriptFrame,
|
||||
remorse::usecodeFrameBaseOffset, loopScriptServices) == remorse::UsecodeStepResult::Advanced);
|
||||
assert(loopScriptPc == 2 && loopScriptState->stackTop == 0xcb &&
|
||||
loopScriptState->valueStack[0xcb] == 0x24);
|
||||
|
||||
auto multiplyState = remorse::UsecodeInterpreterState::initialize({});
|
||||
assert(multiplyState && multiplyState->pushWord(7) && multiplyState->pushWord(6));
|
||||
std::size_t multiplyPc = 0;
|
||||
std::vector<remorse::u8> multiplyCode{0x1e};
|
||||
std::vector<remorse::u8> multiplyFrame;
|
||||
assert(multiplyState->step(multiplyCode, multiplyPc, multiplyFrame,
|
||||
remorse::usecodeFrameBaseOffset, loopScriptServices) == remorse::UsecodeStepResult::Advanced);
|
||||
assert(multiplyState->popWord() == 42);
|
||||
|
||||
assert(emptyStack->pushDword(0x12345678));
|
||||
const auto pushedDword = emptyStack->popDword();
|
||||
assert(pushedDword.has_value() && *pushedDword == static_cast<remorse::u32>(0x12345678));
|
||||
|
|
@ -173,6 +193,21 @@ int main()
|
|||
assert(factoryTask->readMappedItemWord(remorse::FarPtr16{0x0100, 0}) == 0x1234 &&
|
||||
!factoryTask->readMappedItemWord(remorse::FarPtr16{0x0100, 1}) &&
|
||||
!factoryTask->readMappedItemWord(remorse::FarPtr16{0x0101, 0}));
|
||||
std::vector<remorse::u8> addressCode(remorse::usecodeEventBytecodeOffset, 0);
|
||||
addressCode.insert(addressCode.end(), {0x4b, 0xfe, 0x4c, 0x02, 0x50});
|
||||
auto addressTask = remorse::UsecodeProcessTask::create(6, 0x361, 0x0a, 0x1234,
|
||||
std::move(addressCode), 0);
|
||||
assert(addressTask);
|
||||
addressTask->frame[remorse::usecodeFrameBaseOffset - 2] = 0x78;
|
||||
addressTask->frame[remorse::usecodeFrameBaseOffset - 1] = 0x56;
|
||||
remorse::UsecodeOpcodeServices addressServices;
|
||||
addressServices.currentProcessId = [addressTask]() { return addressTask->processId; };
|
||||
addressServices.readFarWord = [addressTask](remorse::u16 offset, remorse::u16 segment) {
|
||||
return addressTask->readMappedFarWord(remorse::FarPtr16{offset, segment});
|
||||
};
|
||||
assert(addressTask->step(addressServices) == remorse::UsecodeStepResult::Advanced);
|
||||
assert(addressTask->step(addressServices) == remorse::UsecodeStepResult::Advanced);
|
||||
assert(addressTask->interpreter.popWord() == 0x5678);
|
||||
auto noItemTask = remorse::UsecodeProcessTask::create(5, 0x361, 0x0a, 0x1234,
|
||||
std::vector<remorse::u8>(15, 0), 0);
|
||||
assert(noItemTask && noItemTask->interpreter.stackTop == 0xcc &&
|
||||
|
|
|
|||
56
rebuild/remorse/tools/clean-old-build-artifacts.ps1
Normal file
56
rebuild/remorse/tools/clean-old-build-artifacts.ps1
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
|
||||
param(
|
||||
[switch]$Apply
|
||||
)
|
||||
|
||||
$repoRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..\..\..'))
|
||||
$outputRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..\out'))
|
||||
$allowedBuildDirectories = @('build-nmake', 'build-track-b')
|
||||
|
||||
if (-not (Test-Path -LiteralPath $outputRoot -PathType Container)) {
|
||||
throw "Expected generated output directory was not found: $outputRoot"
|
||||
}
|
||||
|
||||
$outputRootInfo = Get-Item -LiteralPath $outputRoot -Force
|
||||
if (($outputRootInfo.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
|
||||
throw "Refusing to use a reparse-point output directory: $outputRoot"
|
||||
}
|
||||
|
||||
$repoPrefix = $repoRoot.TrimEnd('\') + '\'
|
||||
if (-not $outputRoot.StartsWith($repoPrefix, [System.StringComparison]::OrdinalIgnoreCase)) {
|
||||
throw "Output directory is outside the repository: $outputRoot"
|
||||
}
|
||||
|
||||
$outputPrefix = $outputRoot.TrimEnd('\') + '\'
|
||||
$targets = [System.Collections.Generic.List[System.IO.DirectoryInfo]]::new()
|
||||
foreach ($directoryName in $allowedBuildDirectories) {
|
||||
$targetPath = [System.IO.Path]::GetFullPath((Join-Path $outputRoot $directoryName))
|
||||
if (-not $targetPath.StartsWith($outputPrefix, [System.StringComparison]::OrdinalIgnoreCase)) {
|
||||
throw "Refusing to process a path outside the generated output directory: $targetPath"
|
||||
}
|
||||
if (-not (Test-Path -LiteralPath $targetPath -PathType Container)) {
|
||||
Write-Output "Skip (not present): $targetPath"
|
||||
continue
|
||||
}
|
||||
|
||||
$targetInfo = Get-Item -LiteralPath $targetPath -Force
|
||||
if (($targetInfo.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
|
||||
throw "Refusing to remove a reparse-point build directory: $targetPath"
|
||||
}
|
||||
if (-not (Test-Path -LiteralPath (Join-Path $targetPath 'CMakeCache.txt') -PathType Leaf)) {
|
||||
throw "Refusing to remove a directory without a CMake cache: $targetPath"
|
||||
}
|
||||
$targets.Add($targetInfo)
|
||||
}
|
||||
|
||||
foreach ($target in $targets) {
|
||||
if (-not $Apply) {
|
||||
Write-Output "Dry run; would remove generated CMake build directory: $($target.FullName)"
|
||||
continue
|
||||
}
|
||||
|
||||
if ($PSCmdlet.ShouldProcess($target.FullName, 'Remove generated CMake build directory')) {
|
||||
Remove-Item -LiteralPath $target.FullName -Recurse -Force
|
||||
Write-Output "Removed: $($target.FullName)"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue