From 345820145e594ae170aec73cd41a86c47a451d09 Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Fri, 1 Apr 2022 00:45:50 -0700 Subject: [PATCH] implement switch assist --- src/ChatCommands.cc | 11 +++++++++++ src/Client.cc | 3 ++- src/Client.hh | 3 +++ src/ReceiveSubcommands.cc | 21 ++++++++++++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/ChatCommands.cc b/src/ChatCommands.cc index 9e4a366c..75f39ebd 100644 --- a/src/ChatCommands.cc +++ b/src/ChatCommands.cc @@ -459,6 +459,7 @@ static void command_cheat(shared_ptr, shared_ptr l, } c->infinite_hp = false; c->infinite_tp = false; + c->switch_assist = false; } memset(&l->next_drop_item, 0, sizeof(l->next_drop_item)); } @@ -866,6 +867,15 @@ static void command_infinite_tp(shared_ptr, shared_ptr l, send_text_message_printf(c, "$C6Infinite TP %s", c->infinite_tp ? "enabled" : "disabled"); } +static void command_switch_assist(shared_ptr, shared_ptr l, + shared_ptr c, const char16_t*) { + check_is_game(l, true); + check_cheats_enabled(l); + + c->switch_assist = !c->switch_assist; + send_text_message_printf(c, "$C6Switch assist %s", c->switch_assist ? "enabled" : "disabled"); +} + static void command_item(shared_ptr, shared_ptr l, shared_ptr c, const char16_t* args) { check_is_game(l, true); @@ -927,6 +937,7 @@ static const unordered_map chat_commands({ {u"secid" , {command_secid , u"Usage:\nsecid [section ID]\nomit section ID to\nrevert to normal"}}, {u"silence" , {command_silence , u"Usage:\nsilence "}}, {u"song" , {command_song , u"Usage:\nsong "}}, + {u"swa" , {command_switch_assist , u"Usage:\nswa"}}, {u"type" , {command_lobby_type , u"Usage:\ntype "}}, {u"warp" , {command_warp , u"Usage:\nwarp "}}, }); diff --git a/src/Client.cc b/src/Client.cc index 674e6897..aa1cd7b8 100644 --- a/src/Client.cc +++ b/src/Client.cc @@ -41,8 +41,9 @@ Client::Client( override_section_id(-1), infinite_hp(false), infinite_tp(false), + switch_assist(false), can_chat(true) { - + memset(&this->last_switch_enabled_subcommand, 0, sizeof(this->last_switch_enabled_subcommand)); int fd = bufferevent_getfd(this->bev); if (fd < 0) { this->is_virtual_connection = true; diff --git a/src/Client.hh b/src/Client.hh index 904771d3..76b34e6e 100644 --- a/src/Client.hh +++ b/src/Client.hh @@ -9,6 +9,7 @@ #include "PSOEncryption.hh" #include "Text.hh" +#include "PSOProtocol.hh" @@ -80,6 +81,8 @@ struct Client { int16_t override_section_id; // valid if >= 0 bool infinite_hp; // cheats enabled bool infinite_tp; // cheats enabled + bool switch_assist; // cheats enabled + PSOSubcommand last_switch_enabled_subcommand[3]; bool can_chat; std::string pending_bb_save_username; uint8_t pending_bb_save_player_index; diff --git a/src/ReceiveSubcommands.cc b/src/ReceiveSubcommands.cc index 415dd926..7ec9ee02 100644 --- a/src/ReceiveSubcommands.cc +++ b/src/ReceiveSubcommands.cc @@ -200,6 +200,25 @@ static void process_subcommand_use_technique(shared_ptr, } } +static void process_subcommand_switch_state_changed(shared_ptr, + shared_ptr l, shared_ptr c, uint8_t command, uint8_t flag, + const PSOSubcommand* p, size_t count) { + if (!l->is_game() || (p->byte[1] != count)) { + return; + } + forward_subcommand(l, c, command, flag, p, count); + if ((count == 3) && (p[2].byte[3] == 1)) { // If this is a switch enable command + if ((l->flags & LobbyFlag::CHEATS_ENABLED) && c->switch_assist && + (c->last_switch_enabled_subcommand[0].byte[0] == 0x05)) { + log(INFO, "[Switch assist] Replaying previous enable command"); + forward_subcommand(l, c, command, flag, c->last_switch_enabled_subcommand, 3); + send_command(c, command, flag, c->last_switch_enabled_subcommand, + sizeof(c->last_switch_enabled_subcommand)); + } + memcpy(&c->last_switch_enabled_subcommand, p, sizeof(c->last_switch_enabled_subcommand)); + } +} + //////////////////////////////////////////////////////////////////////////////// // BB Item commands @@ -922,7 +941,7 @@ subcommand_handler_t subcommand_handlers[0x100] = { /* 02 */ process_subcommand_unimplemented, /* 03 */ process_subcommand_unimplemented, /* 04 */ process_subcommand_unimplemented, - /* 05 */ process_subcommand_forward_check_size_game, // Switch flipped (door lock / lights / poison gas), or room unlocked when all enemied defeated + /* 05 */ process_subcommand_switch_state_changed, /* 06 */ process_subcommand_send_guild_card, /* 07 */ process_subcommand_symbol_chat, /* 08 */ process_subcommand_unimplemented,