Skip to content

Commit

Permalink
Maniac: Support Mouse reading from KeyInputProc
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Aug 28, 2021
1 parent 6229cea commit 7845ab3
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 21 deletions.
145 changes: 125 additions & 20 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ void Game_Interpreter::Push(
void Game_Interpreter::KeyInputState::fromSave(const lcf::rpg::SaveEventExecState& save) {
*this = {};

// Maniac Patch aware check functions for parameters that handle
// keyboard and mouse through a bitmask
bool is_maniac = Player::IsPatchManiac();
auto check_key = [&](auto& var) {
if (is_maniac) {
return (var & 1) != 0;
} else {
return var != 0;
}
};
auto check_mouse = [&](auto& var) {
return (var & 2) != 0;
};

wait = save.keyinput_wait;
// FIXME: There is an RPG_RT bug where keyinput_variable is uint8_t
// which we currently have to emulate. So the value from the save could be wrong.
Expand All @@ -136,27 +150,35 @@ void Game_Interpreter::KeyInputState::fromSave(const lcf::rpg::SaveEventExecStat
keys[Keys::eUp] = true;
} else {
if (Player::IsRPG2k3()) {
keys[Keys::eDown] = save.keyinput_2k3down;
keys[Keys::eLeft] = save.keyinput_2k3left;
keys[Keys::eRight] = save.keyinput_2k3right;
keys[Keys::eUp] = save.keyinput_2k3up;
keys[Keys::eDown] = check_key(save.keyinput_2k3down);
keys[Keys::eLeft] = check_key(save.keyinput_2k3left);
keys[Keys::eRight] = check_key(save.keyinput_2k3right);
keys[Keys::eUp] = check_key(save.keyinput_2k3up);
} else {
keys[Keys::eDown] = save.keyinput_2kdown_2k3operators ;
keys[Keys::eLeft] = save.keyinput_2kleft_2k3shift;
keys[Keys::eRight] = save.keyinput_2kright;
keys[Keys::eUp] = save.keyinput_2kup;
keys[Keys::eDown] = check_key(save.keyinput_2kdown_2k3operators);
keys[Keys::eLeft] = check_key(save.keyinput_2kleft_2k3shift);
keys[Keys::eRight] = check_key(save.keyinput_2kright);
keys[Keys::eUp] = check_key(save.keyinput_2kup);
}
}

keys[Keys::eDecision] = save.keyinput_decision;
keys[Keys::eCancel] = save.keyinput_cancel;
keys[Keys::eDecision] = check_key(save.keyinput_decision);
keys[Keys::eCancel] = check_key(save.keyinput_cancel);

if (Player::IsRPG2k3()) {
keys[Keys::eShift] = save.keyinput_2kleft_2k3shift;
keys[Keys::eNumbers] = save.keyinput_2kshift_2k3numbers;
keys[Keys::eOperators] = save.keyinput_2kdown_2k3operators;
keys[Keys::eShift] = check_key(save.keyinput_2kleft_2k3shift);
keys[Keys::eNumbers] = check_key(save.keyinput_2kshift_2k3numbers);
keys[Keys::eOperators] = check_key(save.keyinput_2kdown_2k3operators);

if (is_maniac) {
keys[Keys::eMouseLeft] = check_mouse(save.keyinput_decision);
keys[Keys::eMouseRight] = check_mouse(save.keyinput_cancel);
keys[Keys::eMouseMiddle] = check_mouse(save.keyinput_2kleft_2k3shift);
keys[Keys::eMouseScrollUp] = check_mouse(save.keyinput_2k3up);
keys[Keys::eMouseScrollDown] = check_mouse(save.keyinput_2k3down);
}
} else {
keys[Keys::eShift] = save.keyinput_2kshift_2k3numbers;
keys[Keys::eShift] = check_key(save.keyinput_2kshift_2k3numbers);
}

time_variable = save.keyinput_time_variable;
Expand Down Expand Up @@ -215,6 +237,24 @@ void Game_Interpreter::KeyInputState::toSave(lcf::rpg::SaveEventExecState& save)
save.keyinput_2kleft_2k3shift = keys[Keys::eShift];
save.keyinput_2kshift_2k3numbers = keys[Keys::eNumbers];
save.keyinput_2kdown_2k3operators = keys[Keys::eOperators];

if (Player::IsPatchManiac()) {
if (keys[Keys::eMouseLeft]) {
save.keyinput_decision |= 2;
}
if (keys[Keys::eMouseRight]) {
save.keyinput_cancel |= 2;
}
if (keys[Keys::eMouseMiddle]) {
save.keyinput_2kleft_2k3shift |= 2;
}
if (keys[Keys::eMouseScrollUp]) {
save.keyinput_2k3up |= 2;
}
if (keys[Keys::eMouseScrollDown]) {
save.keyinput_2k3down |= 2;
}
}
} else {
save.keyinput_2kshift_2k3numbers = keys[Keys::eShift];
}
Expand Down Expand Up @@ -2827,9 +2867,42 @@ bool Game_Interpreter::CommandPlayMemorizedBGM(lcf::rpg::EventCommand const& /*
return true;
}


int Game_Interpreter::KeyInputState::CheckInput() const {
auto check = wait ? Input::IsTriggered : Input::IsPressed;
// FIXME: Refactor input system to make mouse buttons individual keys
auto check_raw = wait ? Input::IsRawKeyTriggered : Input::IsRawKeyPressed;

// Mouse buttons checked first (Maniac checks them last) to prevent conflict
// with DECISION that is mapped to MOUSE_LEFT but the order of checking matches
if (keys[Keys::eMouseScrollDown]) {
if (check_raw(Input::Keys::MOUSE_SCROLLDOWN)) {
return 1001;
}
}

if (keys[Keys::eMouseScrollUp]) {
if (check_raw(Input::Keys::MOUSE_SCROLLDOWN)) {
return 1004;
}
}

if (keys[Keys::eMouseMiddle]) {
if (check_raw(Input::Keys::MOUSE_MIDDLE)) {
return 1007;
}
}

if (keys[Keys::eMouseRight]) {
if (check_raw(Input::Keys::MOUSE_LEFT)) {
return 1006;
}
}

if (keys[Keys::eMouseLeft]) {
if (check_raw(Input::Keys::MOUSE_LEFT)) {
return 1005;
}
}

// RPG processes keys from highest variable value to lowest.
if (keys[Keys::eOperators]) {
Expand Down Expand Up @@ -2892,8 +2965,19 @@ bool Game_Interpreter::CommandKeyInputProc(lcf::rpg::EventCommand const& com) {
_keyinput.wait = wait;
_keyinput.variable = var_id;

_keyinput.keys[Keys::eDecision] = com.parameters[3] != 0;
_keyinput.keys[Keys::eCancel] = com.parameters[4] != 0;
// Maniac Patch aware check functions for parameters that handle
// keyboard and mouse through a bitmask
bool is_maniac = Player::IsPatchManiac();
auto check_key = [&](auto idx) {
if (is_maniac) {
return (com.parameters[idx] & 1) != 0;
} else {
return com.parameters[idx] != 0;
}
};

_keyinput.keys[Keys::eDecision] = check_key(3);
_keyinput.keys[Keys::eCancel] = check_key(4);

const size_t param_size = com.parameters.size();

Expand Down Expand Up @@ -2931,11 +3015,13 @@ bool Game_Interpreter::CommandKeyInputProc(lcf::rpg::EventCommand const& com) {
_keyinput.timed = param_size > 8 && com.parameters[8] != 0;
if (param_size > 10 && Player::IsMajorUpdatedVersion()) {
// For Rpg2k3 >=1.05
_keyinput.keys[Keys::eShift] = com.parameters[9] != 0;
_keyinput.keys[Keys::eDown] = com.parameters[10] != 0;
// ManiacPatch Middle & Wheel only handled for 2k3 Major Updated,
// the only version that has this patch
_keyinput.keys[Keys::eShift] = check_key(9);
_keyinput.keys[Keys::eDown] = check_key(10);
_keyinput.keys[Keys::eLeft] = param_size > 11 && com.parameters[11] != 0;
_keyinput.keys[Keys::eRight] = param_size > 12 && com.parameters[12] != 0;
_keyinput.keys[Keys::eUp] = param_size > 13 && com.parameters[13] != 0;
_keyinput.keys[Keys::eUp] = param_size > 13 && check_key(13);
}
} else {
// Since RPG2k3 1.05
Expand All @@ -2948,6 +3034,25 @@ bool Game_Interpreter::CommandKeyInputProc(lcf::rpg::EventCommand const& com) {
}
}

if (is_maniac) {
auto check_mouse = [&](auto idx) {
bool result = (com.parameters[idx] & 2) != 0;
#if !defined(USE_MOUSE) || !defined(SUPPORT_MOUSE)
if (result) {
Output::Warning("ManiacPatch: Mouse input is not supported on this platform");
return false;
}
#else
return result;
#endif
};
_keyinput.keys[Keys::eMouseLeft] = check_mouse(3);
_keyinput.keys[Keys::eMouseRight] = check_mouse(4);
_keyinput.keys[Keys::eMouseMiddle] = param_size > 9 && check_mouse(9);
_keyinput.keys[Keys::eMouseScrollDown] = param_size > 10 && check_mouse(10);
_keyinput.keys[Keys::eMouseScrollUp] = param_size > 13 && check_mouse(13);
}

if (_keyinput.wait) {
// RPG_RT will reset all trigger key states when a waiting key input proc command is executed,
// which means we always wait at least 1 frame to continue. Keys which are held down are not reset.
Expand Down
8 changes: 7 additions & 1 deletion src/game_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,13 @@ class Game_Interpreter
eCancel,
eShift,
eNumbers,
eOperators
eOperators,
// ManiacPatch
eMouseLeft,
eMouseRight,
eMouseMiddle,
eMouseScrollDown,
eMouseScrollUp
};

struct KeyInputState {
Expand Down

0 comments on commit 7845ab3

Please sign in to comment.