Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable additional built-in commands for single player only #280

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Version 1.9.0 (not released yet)
- Do not load unnecessary VPPs in dedicated server mode
- Add level filename to "Level Initializing" console message
- Properly handle WM_PAINT in dedicated server, may improve performance (DF bug)
- Enable commands `drop_clutter`, `drop_entity`, `drop_item`, `teleport`, `pcollide` in single player

Version 1.8.0 (released 2022-09-17)
-----------------------------------
Expand Down
2 changes: 2 additions & 0 deletions game_patch/multi/multi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ CodeInjection multi_start_injection{
[]() {
void debug_multi_init();
debug_multi_init();
void reset_restricted_cmds_on_init_multi();
reset_restricted_cmds_on_init_multi();
},
};

Expand Down
64 changes: 58 additions & 6 deletions game_patch/os/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../misc/vpackfile.h"
#include <common/utils/list-utils.h>
#include <algorithm>
#include <patch_common/FunHook.h>
#include <patch_common/CallHook.h>
#include <patch_common/AsmWriter.h>

Expand Down Expand Up @@ -91,6 +92,45 @@ DcCommandAlias map_info_cmd{
level_info_cmd,
};

ConsoleCommand2 pcollide_cmd{
"pcollide",
[]() {
if (rf::is_multi) {
rf::console::print("That command can't be used in multiplayer.");
return;
}
else {
rf::local_player->collides_with_world = !rf::local_player->collides_with_world;
rf::console::print("Player collision with the world is set to {}", rf::local_player->collides_with_world);
}
},
"Toggles player collision with the world",
};

void reset_restricted_cmds_on_init_multi()
{
// ensure player collides with the world (pcollide = true)
if (rf::local_player && !rf::local_player->collides_with_world) {
rf::console::print("Player collision with the world is set to true.");
rf::local_player->collides_with_world = true;
}
}

void restrict_mp_command(FunHook<void()>& hook)
{
if (rf::is_multi) {
rf::console::print("That command can't be used in multiplayer.");
return;
}
hook.call_target();
}

FunHook<void()> drop_clutter_hook{0x0040F0A0, []() { restrict_mp_command(drop_clutter_hook); }};
FunHook<void()> drop_entity_hook{0x00418740, []() { restrict_mp_command(drop_entity_hook); }};
FunHook<void()> drop_item_hook{0x00458530, []() { restrict_mp_command(drop_item_hook); }};
//FunHook<void()> pcollide_hook{0x004A0F60, []() { restrict_mp_command(pcollide_hook); }};
FunHook<void()> teleport_hook{0x004A0FC0, []() { restrict_mp_command(teleport_hook); }};

static void register_builtin_command(const char* name, const char* description, uintptr_t addr)
{
static std::vector<std::unique_ptr<rf::console::Command>> builtin_commands;
Expand All @@ -103,6 +143,15 @@ void console_commands_apply_patches()
{
// Allow 'level' command outside of multiplayer game
AsmWriter(0x00434FEC, 0x00434FF2).nop();

// restrict risky commands in mp unless debug build
#ifdef NDEBUG
drop_clutter_hook.install();
drop_entity_hook.install();
drop_item_hook.install();
//pcollide_hook.install();
teleport_hook.install();
#endif
}

void console_commands_init()
Expand Down Expand Up @@ -130,35 +179,37 @@ void console_commands_init()
register_builtin_command("trilinear_filtering", "Toggle trilinear filtering", 0x0054F050);
register_builtin_command("detail_textures", "Toggle detail textures", 0x0054F0B0);

// risky commands, restricted in MP unless debug build
register_builtin_command("drop_clutter", "Spawn a clutter object by class name", 0x0040F0A0);
register_builtin_command("drop_entity", "Spawn an entity by class name", 0x00418740);
register_builtin_command("drop_item", "Spawn an item by class name", 0x00458530);
// register_builtin_command("pcollide", "Toggle if player collides with the world", 0x004A0F60);
register_builtin_command("teleport", "Teleport player to specific coordinates (format: X Y Z)", 0x004A0FC0);


#ifdef DEBUG
register_builtin_command("drop_fixed_cam", "Drop a fixed camera", 0x0040D220);
register_builtin_command("orbit_cam", "Orbit camera around current target", 0x0040D2A0);
register_builtin_command("drop_clutter", "Drop any clutter", 0x0040F0A0);
register_builtin_command("glares_toggle", "toggle glares", 0x00414830);
register_builtin_command("set_vehicle_bounce", "set the elasticity of vehicle collisions", 0x004184B0);
register_builtin_command("set_mass", "set the mass of the targeted object", 0x004184F0);
register_builtin_command("set_skin", "Set the skin of the current player target", 0x00418610);
register_builtin_command("set_life", "Set the life of the player's current target", 0x00418680);
register_builtin_command("set_armor", "Set the armor of the player's current target", 0x004186E0);
register_builtin_command("drop_entity", "Drop any entity", 0x00418740);
register_builtin_command("set_weapon", "set the current weapon for the targeted entity", 0x00418AA0);
register_builtin_command("jump_height", "set the height the player can jump", 0x00418B80);
register_builtin_command("fall_factor", nullptr, 0x004282F0);
register_builtin_command("toggle_crouch", nullptr, 0x00430C50);
register_builtin_command("player_step", nullptr, 0x00433DB0);
register_builtin_command("difficulty", nullptr, 0x00434EB0);
register_builtin_command("mouse_cursor", "Sets the mouse cursor", 0x00435210);
register_builtin_command("fogme", "Fog everything", 0x004352E0);
register_builtin_command("mouse_look", nullptr, 0x0043CF30);
register_builtin_command("drop_item", "Drop any item", 0x00458530);
register_builtin_command("set_georegion_hardness", "Set default hardness for geomods", 0x004663E0);
register_builtin_command("make_myself", nullptr, 0x00475040);
register_builtin_command("splitscreen", nullptr, 0x00480AA0);
register_builtin_command("splitscreen_swap", "Swap splitscreen players", 0x00480AB0);
register_builtin_command("splitscreen_bot_test", "Start a splitscreen game in mk_circuits3.rfl", 0x00480AE0);
register_builtin_command("max_plankton", "set the max number of plankton bits", 0x00497FC0);
register_builtin_command("pcollide", "Toggle if player collides with the world", 0x004A0F60);
register_builtin_command("teleport", "Teleport to an x,y,z", 0x004A0FC0);
register_builtin_command("body", "Set player entity type", 0x004A11C0);
register_builtin_command("invulnerable", "Make self invulnerable", 0x004A12A0);
register_builtin_command("freelook", "Toggle between freelook and first person", 0x004A1340);
Expand All @@ -184,6 +235,7 @@ void console_commands_init()
#endif // DEBUG

// Custom Dash Faction commands
pcollide_cmd.register_cmd();
dot_cmd.register_cmd();
vli_cmd.register_cmd();
player_count_cmd.register_cmd();
Expand Down
1 change: 1 addition & 0 deletions game_patch/rf/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ namespace rf
};

static auto& entity_from_handle = addr_as_ref<Entity*(int handle)>(0x00426FC0);
static auto& entity_lookup_type = addr_as_ref<int(const char* name)>(0x004251C0);
static auto& entity_create =
addr_as_ref<Entity*(int entity_type, const char* name, int parent_handle, const Vector3& pos,
const Matrix3& orient, int create_flags, int mp_character)>(0x00422360);
Expand Down