- 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.
439 lines
No EOL
15 KiB
C++
439 lines
No EOL
15 KiB
C++
#pragma once
|
|
|
|
#include "remorse/animation_archive.h"
|
|
#include "remorse/compat.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <vector>
|
|
|
|
namespace remorse {
|
|
|
|
inline constexpr std::array<u8, 25> controllerAnimationIndices{
|
|
0, 1, 2, 3, 4, 0x0b, 0x10, 0x17, 0x18, 0x1c, 0x1d, 0x20, 0x22,
|
|
0x24, 0x26, 0x28, 0x29, 0x2e, 0x30, 0x37, 0x38, 0x3a, 0x3b, 0x3d, 0x3e};
|
|
|
|
struct WorldPositionD {
|
|
double x = 0.0;
|
|
double y = 0.0;
|
|
};
|
|
|
|
inline bool controllerWalkRequested(u16 modifiers)
|
|
{
|
|
const bool shiftDown = (modifiers & 0x03) != 0;
|
|
const bool capsLockOn = (modifiers & 0x40) != 0;
|
|
return shiftDown == capsLockOn;
|
|
}
|
|
|
|
bool controllerFireAttemptSuppressed(bool previousAttemptSlept, u8 scanCode,
|
|
u8 eventMode, u8 keyState);
|
|
|
|
inline bool controllerAllowsInPlaceDirectionUpdate(u8 animation)
|
|
{
|
|
switch (animation) {
|
|
case 1: case 3: case 4: case 7: case 8: case 10: case 12: case 13: case 15:
|
|
case 25: case 34: case 35: case 36: case 37: case 38: case 39: case 42: case 43:
|
|
case 44: case 45: case 46: case 47: case 48: case 49: case 50: case 54:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
inline bool controllerLastAnimationIsRunning(u8 animation)
|
|
{
|
|
switch (animation) {
|
|
case 1: case 3: case 34: case 35: case 38: case 48: case 49: case 56:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
inline bool controllerLastAnimationIsRunningJumping(u8 animation)
|
|
{
|
|
switch (animation) {
|
|
case 3: case 48: case 49: case 56:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
inline bool controllerLastAnimationIsNotCombat(u8 animation)
|
|
{
|
|
switch (animation) {
|
|
case 0: case 1: case 11: case 16: case 32: case 55: case 58: case 59:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
inline std::optional<u8> controllerDirectionFromScan(u8 scanCode)
|
|
{
|
|
// Controller_PickNextAnimByKeyevent (1130:12d6), directional keys with Num Lock off.
|
|
switch (scanCode) {
|
|
case 0x47: return u8{12};
|
|
case 0x48: return u8{14};
|
|
case 0x49: return u8{0};
|
|
case 0x4b: return u8{10};
|
|
case 0x4d: return u8{2};
|
|
case 0x4f: return u8{8};
|
|
case 0x50: return u8{6};
|
|
case 0x51: return u8{4};
|
|
default: return std::nullopt;
|
|
}
|
|
}
|
|
|
|
struct ControllerMovementRequest {
|
|
u8 animation = 0;
|
|
u8 direction = 14;
|
|
bool oneShot = false;
|
|
bool updateDirectionInPlace = false;
|
|
};
|
|
|
|
enum class ControllerCommand : u8 {
|
|
FireWeapon,
|
|
SelectNextItem,
|
|
CycleWeaponForward,
|
|
OpenInventory
|
|
};
|
|
|
|
struct ControllerCommandRequest {
|
|
ControllerCommand command = ControllerCommand::FireWeapon;
|
|
};
|
|
|
|
struct ControllerActorState {
|
|
u16 npcNumber = 1;
|
|
u16 mapNumber = 1;
|
|
u8 direction = 14;
|
|
u8 lastAnimation = 0;
|
|
bool controllerEnabled = true;
|
|
bool dead = false;
|
|
bool avatarInStasis = false;
|
|
bool kneeling = false;
|
|
bool activeWeapon = false;
|
|
bool busy = false;
|
|
bool lastFireAttemptWentToSleep = false;
|
|
bool lastAnimationNotCombat = true;
|
|
bool avatarSomethingCombatty = false;
|
|
};
|
|
|
|
class ControllerMovementState {
|
|
public:
|
|
void setActorState(const ControllerActorState& state)
|
|
{
|
|
if (state.direction != actor_.direction) {
|
|
lastDirection_ = state.direction;
|
|
}
|
|
actor_ = state;
|
|
setFacingDirection(state.direction);
|
|
crouched_ = state.kneeling;
|
|
}
|
|
|
|
void handleDirectionalKey(u8 scanCode, u16 modifiers, bool pressed)
|
|
{
|
|
handleKeyEvent(scanCode, modifiers, 1, pressed ? 1 : 0, pressed ? 1 : 0);
|
|
}
|
|
|
|
void handleKeyEvent(u8 scanCode, u16 modifiers, u8 eventMode, u8 keyState,
|
|
u16 keyAction = 1, u16 eventFlags = 0)
|
|
{
|
|
if (!actor_.controllerEnabled || actor_.npcNumber == 0 || actor_.dead ||
|
|
(actor_.npcNumber == 1 && actor_.avatarInStasis)) {
|
|
return;
|
|
}
|
|
modifiers_ = modifiers;
|
|
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 &&
|
|
controllerLastAnimationIsRunningJumping(actor_.lastAnimation))) {
|
|
pendingAction_ = ControllerMovementRequest{56, actor_.direction, true};
|
|
}
|
|
return;
|
|
}
|
|
if (keyState != 0 && keyAction == 1 && actor_.npcNumber == 1) {
|
|
if ((scanCode == 0x39 || scanCode == 0x52) && actor_.mapNumber != 0x40 &&
|
|
!actor_.busy && (scanCode != 0x52 || eventFlags == 0)) {
|
|
pendingCommand_ = ControllerCommandRequest{ControllerCommand::FireWeapon};
|
|
return;
|
|
}
|
|
if (scanCode == 0x4a) {
|
|
pendingCommand_ = ControllerCommandRequest{ControllerCommand::SelectNextItem};
|
|
return;
|
|
}
|
|
if (scanCode == 0x37 && eventFlags == 0) {
|
|
pendingCommand_ = ControllerCommandRequest{ControllerCommand::CycleWeaponForward};
|
|
return;
|
|
}
|
|
if (scanCode == 0x4e || (scanCode == 0x53 && eventFlags == 0)) {
|
|
pendingCommand_ = ControllerCommandRequest{ControllerCommand::OpenInventory};
|
|
return;
|
|
}
|
|
}
|
|
const auto held = std::find(heldScans_.begin(), heldScans_.end(), scanCode);
|
|
if (held != heldScans_.end()) {
|
|
heldScans_.erase(held);
|
|
}
|
|
constexpr u16 actionModifierMask = 0x0f00;
|
|
if ((modifiers & actionModifierMask) != 0) {
|
|
if (keyState != 0) {
|
|
queueModifiedAction(scanCode, modifiers, keyAction);
|
|
}
|
|
return;
|
|
}
|
|
const bool numLockOn = (modifiers & 0x20) != 0;
|
|
if (keyState != 0 && !numLockOn && actor_.mapNumber != 0x40 && !actor_.busy &&
|
|
actor_.avatarSomethingCombatty && (scanCode == 0x4b || scanCode == 0x4d)) {
|
|
const int turn = scanCode == 0x4b ? -1 : 1;
|
|
lastDirection_ = static_cast<u8>((actor_.direction + turn + 16) & 0x0f);
|
|
pendingAction_ = ControllerMovementRequest{
|
|
static_cast<u8>(actor_.kneeling ? 0x2e : 4), lastDirection_, true};
|
|
return;
|
|
}
|
|
if (numLockOn && (scanCode == 0x4b || scanCode == 0x4d)) {
|
|
if (keyState != 0) {
|
|
if (actor_.busy) {
|
|
if (controllerAllowsInPlaceDirectionUpdate(actor_.lastAnimation)) {
|
|
pendingInPlaceDirection_ = static_cast<u8>(scanCode == 0x4b ? 0x12 : 0x11);
|
|
}
|
|
} else {
|
|
const int turn = scanCode == 0x4b ? -turnStep_ : turnStep_;
|
|
lastDirection_ = static_cast<u8>((facingDirection_ + turn + 16) & 0x0f);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
if (numLockOn && scanCode != 0x48 && scanCode != 0x50) {
|
|
return;
|
|
}
|
|
const auto direction = controllerDirectionFromScan(scanCode);
|
|
if (!direction) {
|
|
if (keyState != 0) {
|
|
queueModifiedAction(scanCode, modifiers, keyAction);
|
|
}
|
|
return;
|
|
}
|
|
if (keyState != 0) {
|
|
heldScans_.push_back(scanCode);
|
|
}
|
|
if (keyState != 0) {
|
|
queueModifiedAction(scanCode, modifiers, keyAction);
|
|
}
|
|
}
|
|
|
|
ControllerMovementRequest request() const
|
|
{
|
|
if (!actor_.controllerEnabled || actor_.npcNumber == 0 || actor_.dead ||
|
|
(actor_.npcNumber == 1 && actor_.avatarInStasis)) {
|
|
return {0, actor_.direction};
|
|
}
|
|
if (pendingInPlaceDirection_) {
|
|
return {actor_.lastAnimation, *pendingInPlaceDirection_, false, true};
|
|
}
|
|
if (heldScans_.empty()) {
|
|
return {0, lastDirection_};
|
|
}
|
|
const u8 runAnimation = actor_.activeWeapon ? 0x30 : 3;
|
|
const u8 scanCode = heldScans_.back();
|
|
const bool numLockOn = (modifiers_ & 0x20) != 0;
|
|
const u8 requestedDirection = numLockOn && (scanCode == 0x48 || scanCode == 0x50)
|
|
? facingDirection_ : *controllerDirectionFromScan(scanCode);
|
|
lastDirection_ = requestedDirection;
|
|
const bool walkRequested = controllerWalkRequested(modifiers_);
|
|
if (actor_.mapNumber == 0x40) {
|
|
if (controllerLastAnimationIsRunning(actor_.lastAnimation)) {
|
|
return {static_cast<u8>(walkRequested ? 1 : runAnimation), requestedDirection};
|
|
}
|
|
if (requestedDirection == actor_.direction) {
|
|
return {static_cast<u8>(walkRequested ? 1 : 0x26), requestedDirection};
|
|
}
|
|
return {0, requestedDirection};
|
|
}
|
|
if (!walkRequested && requestedDirection == actor_.direction &&
|
|
!controllerLastAnimationIsRunning(actor_.lastAnimation)) {
|
|
return {0x22, requestedDirection};
|
|
}
|
|
if ((modifiers_ & 0x20) != 0) {
|
|
if (scanCode == 0x48) {
|
|
return {static_cast<u8>(walkRequested ? 1 : runAnimation), requestedDirection};
|
|
}
|
|
if (scanCode == 0x50) {
|
|
return {2, requestedDirection};
|
|
}
|
|
}
|
|
return {static_cast<u8>(walkRequested ? 1 : runAnimation), requestedDirection};
|
|
}
|
|
|
|
std::optional<u8> takeInPlaceDirectionUpdate()
|
|
{
|
|
auto direction = pendingInPlaceDirection_;
|
|
pendingInPlaceDirection_.reset();
|
|
return direction;
|
|
}
|
|
|
|
void setFacingDirection(u8 direction)
|
|
{
|
|
if (direction <= 15) {
|
|
facingDirection_ = direction;
|
|
}
|
|
}
|
|
|
|
void setTurnStep(u8 step)
|
|
{
|
|
turnStep_ = step == 1 ? 1 : 2;
|
|
}
|
|
|
|
std::optional<ControllerMovementRequest> takeActionRequest()
|
|
{
|
|
auto action = pendingAction_;
|
|
pendingAction_.reset();
|
|
return action;
|
|
}
|
|
|
|
std::optional<ControllerCommandRequest> takeCommandRequest()
|
|
{
|
|
auto command = pendingCommand_;
|
|
pendingCommand_.reset();
|
|
return command;
|
|
}
|
|
|
|
void clear()
|
|
{
|
|
heldScans_.clear();
|
|
pendingAction_.reset();
|
|
pendingCommand_.reset();
|
|
pendingInPlaceDirection_.reset();
|
|
modifiers_ = 0;
|
|
}
|
|
|
|
private:
|
|
void queueModifiedAction(u8 scanCode, u16 modifiers, u16 keyAction)
|
|
{
|
|
constexpr u16 altMask = 0x0a00;
|
|
constexpr u16 ctrlMask = 0x0500;
|
|
constexpr u8 animationNone = 0x40;
|
|
u8 animation = animationNone;
|
|
u8 actionDirection = lastDirection_;
|
|
if (scanCode == 0x4c) {
|
|
if (keyAction == 1) {
|
|
animation = crouched_ ? 0x29 : 0x28;
|
|
}
|
|
} else if ((modifiers & (altMask | ctrlMask)) == 0 && scanCode == 0x35) {
|
|
if (actor_.mapNumber != 0x40 && !controllerLastAnimationIsRunning(actor_.lastAnimation)) {
|
|
animation = 0x24;
|
|
}
|
|
} else if ((modifiers & altMask) != 0) {
|
|
const bool numLockOn = (modifiers & 0x20) != 0;
|
|
const auto scanDirection = controllerDirectionFromScan(scanCode);
|
|
if (numLockOn && scanDirection) {
|
|
int newDirection = *scanDirection;
|
|
if (scanCode == 0x4b) {
|
|
newDirection = 0x12;
|
|
} else if (scanCode == 0x4d) {
|
|
newDirection = 0x11;
|
|
}
|
|
const int direction = actor_.direction;
|
|
const int directionDelta = newDirection < direction
|
|
? direction - newDirection : direction + 0x10 - newDirection;
|
|
if (0x10 - directionDelta < directionDelta) {
|
|
animation = 0x1d;
|
|
actionDirection = static_cast<u8>((newDirection - 4) & 0x0f);
|
|
} else {
|
|
animation = 0x1c;
|
|
actionDirection = static_cast<u8>((newDirection + 4) & 0x0f);
|
|
}
|
|
} else {
|
|
switch (scanCode) {
|
|
case 0x48: animation = 0x24; break;
|
|
case 0x4b: animation = 0x1c; break;
|
|
case 0x4d: animation = 0x1d; break;
|
|
case 0x50: animation = 2; break;
|
|
default: break;
|
|
}
|
|
}
|
|
} else if ((modifiers & ctrlMask) != 0) {
|
|
const auto requestedDirection = controllerDirectionFromScan(scanCode);
|
|
if ((modifiers & 0x20) == 0 && requestedDirection &&
|
|
(scanCode == 0x47 || scanCode == 0x49 || scanCode == 0x4f || scanCode == 0x51)) {
|
|
const int direction = actor_.direction;
|
|
const int directionDelta = *requestedDirection < direction
|
|
? direction - *requestedDirection : direction + 0x10 - *requestedDirection;
|
|
if (0x10 - directionDelta < directionDelta) {
|
|
animation = 0x18;
|
|
actionDirection = static_cast<u8>((*requestedDirection - 4) & 0x0f);
|
|
} else {
|
|
animation = 0x17;
|
|
actionDirection = static_cast<u8>((*requestedDirection + 4) & 0x0f);
|
|
}
|
|
} else switch (scanCode) {
|
|
case 0x48:
|
|
if (crouched_) {
|
|
animation = 0x29;
|
|
} else if (!actor_.lastAnimationNotCombat) {
|
|
animation = 0x0b;
|
|
} else if (actor_.npcNumber == 1) {
|
|
animation = 0x3a;
|
|
}
|
|
break;
|
|
case 0x4b: animation = crouched_ ? 0x17 : 0x3d; break;
|
|
case 0x4d: animation = crouched_ ? 0x18 : 0x3e; break;
|
|
case 0x50:
|
|
if (keyAction == 1) {
|
|
animation = crouched_ ? 0x29 : 0x28;
|
|
}
|
|
break;
|
|
default: break;
|
|
}
|
|
}
|
|
if (animation != animationNone) {
|
|
pendingAction_ = ControllerMovementRequest{animation, actionDirection, true};
|
|
if (animation == 0x28 || animation == 0x29) {
|
|
crouched_ = !crouched_;
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<u8> heldScans_;
|
|
std::optional<ControllerMovementRequest> pendingAction_;
|
|
std::optional<ControllerCommandRequest> pendingCommand_;
|
|
std::optional<u8> pendingInPlaceDirection_;
|
|
u16 modifiers_ = 0;
|
|
mutable u8 lastDirection_ = 14;
|
|
u8 facingDirection_ = 14;
|
|
u8 turnStep_ = 2;
|
|
bool crouched_ = false;
|
|
ControllerActorState actor_{};
|
|
};
|
|
|
|
inline std::optional<AnimationDisplacement> hostMovementStep(
|
|
const AnimationFrameMetadata& metadata, u8 direction, bool rotateDirection)
|
|
{
|
|
auto step = decodeAnimationDisplacement(metadata, direction, rotateDirection);
|
|
if (step && (metadata.bytes[5] & 1) != 0) {
|
|
step->x = 0;
|
|
step->y = 0;
|
|
}
|
|
return step;
|
|
}
|
|
|
|
inline WorldPositionD worldPositionAtScreenOffset(
|
|
s32 originX,
|
|
s32 originY,
|
|
double screenOffsetX,
|
|
double screenOffsetY)
|
|
{
|
|
return WorldPositionD{
|
|
originX + 2.0 * screenOffsetX + 4.0 * screenOffsetY,
|
|
originY - 2.0 * screenOffsetX + 4.0 * screenOffsetY};
|
|
}
|
|
|
|
} // namespace remorse
|