Cleanup ClientLauncher structure (#10160)

Remove duplicated variables and unify the startup data into a new (inherited) struct.
This commit is contained in:
SmallJoker 2020-07-14 19:10:37 +02:00 committed by GitHub
parent 2bec83eec0
commit 4fa1e03f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 197 additions and 200 deletions

@ -56,6 +56,20 @@ bool isMenuActive()
// Passed to menus to allow disconnecting and exiting // Passed to menus to allow disconnecting and exiting
MainGameCallback *g_gamecallback = nullptr; MainGameCallback *g_gamecallback = nullptr;
#if 0
// This can be helpful for the next code cleanup
static void dump_start_data(const GameStartData &data)
{
std::cout <<
"\ndedicated " << (int)data.is_dedicated_server <<
"\nport " << data.socket_port <<
"\nworld_path " << data.world_spec.path <<
"\nworld game " << data.world_spec.gameid <<
"\ngame path " << data.game_spec.path <<
"\nplayer name " << data.name <<
"\naddress " << data.address << std::endl;
}
#endif
ClientLauncher::~ClientLauncher() ClientLauncher::~ClientLauncher()
{ {
@ -74,9 +88,16 @@ ClientLauncher::~ClientLauncher()
} }
bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args) bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
{ {
init_args(game_params, cmd_args); /* This function is called when a client must be started.
* Covered cases:
* - Singleplayer (address but map provided)
* - Join server (no map but address provided)
* - Local server (for main menu only)
*/
init_args(start_data, cmd_args);
// List video modes if requested // List video modes if requested
if (list_video_modes) if (list_video_modes)
@ -216,7 +237,7 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
core::rect<s32>(0, 0, 10000, 10000)); core::rect<s32>(0, 0, 10000, 10000));
bool game_has_run = launch_game(error_message, reconnect_requested, bool game_has_run = launch_game(error_message, reconnect_requested,
game_params, cmd_args); start_data, cmd_args);
// Reset the reconnect_requested flag // Reset the reconnect_requested flag
reconnect_requested = false; reconnect_requested = false;
@ -241,13 +262,6 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
break; break;
} }
if (current_playername.length() > PLAYERNAME_SIZE-1) {
error_message = gettext("Player name too long.");
playername = current_playername.substr(0, PLAYERNAME_SIZE-1);
g_settings->set("name", playername);
continue;
}
RenderingEngine::get_video_driver()->setTextureCreationFlag( RenderingEngine::get_video_driver()->setTextureCreationFlag(
video::ETCF_CREATE_MIP_MAPS, g_settings->getBool("mip_map")); video::ETCF_CREATE_MIP_MAPS, g_settings->getBool("mip_map"));
@ -258,18 +272,11 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
the_game( the_game(
kill, kill,
random_input,
input, input,
worldspec.path, start_data,
current_playername,
current_password,
current_address,
current_port,
error_message, error_message,
chat_backend, chat_backend,
&reconnect_requested, &reconnect_requested
gamespec,
simple_singleplayer_mode
); );
RenderingEngine::get_scene_manager()->clear(); RenderingEngine::get_scene_manager()->clear();
@ -311,31 +318,27 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
return retval; return retval;
} }
void ClientLauncher::init_args(GameParams &game_params, const Settings &cmd_args) void ClientLauncher::init_args(GameStartData &start_data, const Settings &cmd_args)
{ {
skip_main_menu = cmd_args.getFlag("go"); skip_main_menu = cmd_args.getFlag("go");
// FIXME: This is confusing (but correct) start_data.address = g_settings->get("address");
if (cmd_args.exists("address")) {
// Join a remote server
start_data.address = cmd_args.get("address");
start_data.world_path.clear();
}
if (!start_data.world_path.empty()) {
// Start a singleplayer instance
start_data.address = "";
}
/* If world_path is set then override it unless skipping the main menu using start_data.name = g_settings->get("name");
* the --go command line param. Else, give preference to the address
* supplied on the command line
*/
address = g_settings->get("address");
if (!game_params.world_path.empty() && !skip_main_menu)
address = "";
else if (cmd_args.exists("address"))
address = cmd_args.get("address");
playername = g_settings->get("name");
if (cmd_args.exists("name")) if (cmd_args.exists("name"))
playername = cmd_args.get("name"); start_data.name = cmd_args.get("name");
list_video_modes = cmd_args.getFlag("videomodes"); list_video_modes = cmd_args.getFlag("videomodes");
use_freetype = g_settings->getBool("freetype");
random_input = g_settings->getBool("random_input") random_input = g_settings->getBool("random_input")
|| cmd_args.getFlag("random-input"); || cmd_args.getFlag("random-input");
} }
@ -375,28 +378,20 @@ void ClientLauncher::init_input()
} }
bool ClientLauncher::launch_game(std::string &error_message, bool ClientLauncher::launch_game(std::string &error_message,
bool reconnect_requested, GameParams &game_params, bool reconnect_requested, GameStartData &start_data,
const Settings &cmd_args) const Settings &cmd_args)
{ {
// Initialize menu data // Prepare and check the start data to launch a game
MainMenuData menudata; std::string error_message_lua = error_message;
menudata.address = address;
menudata.name = playername;
menudata.password = password;
menudata.port = itos(game_params.socket_port);
menudata.script_data.errormessage = error_message;
menudata.script_data.reconnect_requested = reconnect_requested;
error_message.clear(); error_message.clear();
if (cmd_args.exists("password")) if (cmd_args.exists("password"))
menudata.password = cmd_args.get("password"); start_data.password = cmd_args.get("password");
if (cmd_args.exists("password-file")) { if (cmd_args.exists("password-file")) {
std::ifstream passfile(cmd_args.get("password-file")); std::ifstream passfile(cmd_args.get("password-file"));
if (passfile.good()) { if (passfile.good()) {
getline(passfile, menudata.password); getline(passfile, start_data.password);
} else { } else {
error_message = gettext("Provided password file " error_message = gettext("Provided password file "
"failed to open: ") "failed to open: ")
@ -407,88 +402,112 @@ bool ClientLauncher::launch_game(std::string &error_message,
} }
// If a world was commanded, append and select it // If a world was commanded, append and select it
if (!game_params.world_path.empty()) { // This is provieded by "get_world_from_cmdline()", main.cpp
worldspec.gameid = getWorldGameId(game_params.world_path, true); if (!start_data.world_path.empty()) {
worldspec.name = _("[--world parameter]"); auto &spec = start_data.world_spec;
if (worldspec.gameid.empty()) { // Create new spec.path = start_data.world_path;
worldspec.gameid = g_settings->get("default_game"); spec.gameid = getWorldGameId(spec.path, true);
worldspec.name += " [new]"; spec.name = _("[--world parameter]");
if (spec.gameid.empty()) { // Create new
spec.gameid = g_settings->get("default_game");
spec.name += " [new]";
} }
worldspec.path = game_params.world_path;
} }
/* Show the GUI menu /* Show the GUI menu
*/ */
std::string server_name, server_description;
start_data.local_server = false;
if (!skip_main_menu) { if (!skip_main_menu) {
// Initialize menu data
// TODO: Re-use existing structs (GameStartData)
MainMenuData menudata;
menudata.address = start_data.address;
menudata.name = start_data.name;
menudata.password = start_data.password;
menudata.port = itos(start_data.socket_port);
menudata.script_data.errormessage = error_message_lua;
menudata.script_data.reconnect_requested = reconnect_requested;
main_menu(&menudata); main_menu(&menudata);
// Skip further loading if there was an exit signal. // Skip further loading if there was an exit signal.
if (*porting::signal_handler_killstatus()) if (*porting::signal_handler_killstatus())
return false; return false;
address = menudata.address; if (!menudata.script_data.errormessage.empty()) {
/* The calling function will pass this back into this function upon the
* next iteration (if any) causing it to be displayed by the GUI
*/
error_message = menudata.script_data.errormessage;
return false;
}
int newport = stoi(menudata.port); int newport = stoi(menudata.port);
if (newport != 0) if (newport != 0)
game_params.socket_port = newport; start_data.socket_port = newport;
simple_singleplayer_mode = menudata.simple_singleplayer_mode;
// Update world information using main menu data
std::vector<WorldSpec> worldspecs = getAvailableWorlds(); std::vector<WorldSpec> worldspecs = getAvailableWorlds();
if (menudata.selected_world >= 0 int world_index = menudata.selected_world;
&& menudata.selected_world < (int)worldspecs.size()) { if (world_index >= 0 && world_index < (int)worldspecs.size()) {
g_settings->set("selected_world_path", g_settings->set("selected_world_path",
worldspecs[menudata.selected_world].path); worldspecs[world_index].path);
worldspec = worldspecs[menudata.selected_world]; start_data.world_spec = worldspecs[world_index];
} }
start_data.name = menudata.name;
start_data.password = menudata.password;
start_data.address = std::move(menudata.address);
server_name = menudata.servername;
server_description = menudata.serverdescription;
start_data.local_server = !menudata.simple_singleplayer_mode &&
start_data.address.empty();
} }
if (!menudata.script_data.errormessage.empty()) { if (!RenderingEngine::run())
/* The calling function will pass this back into this function upon the
* next iteration (if any) causing it to be displayed by the GUI
*/
error_message = menudata.script_data.errormessage;
return false; return false;
}
if (menudata.name.empty() && !simple_singleplayer_mode) { if (!start_data.isSinglePlayer() && start_data.name.empty()) {
error_message = gettext("Please choose a name!"); error_message = gettext("Please choose a name!");
errorstream << error_message << std::endl; errorstream << error_message << std::endl;
return false; return false;
} }
playername = menudata.name;
password = menudata.password;
current_playername = playername;
current_password = password;
current_address = address;
current_port = game_params.socket_port;
// If using simple singleplayer mode, override // If using simple singleplayer mode, override
if (simple_singleplayer_mode) { if (start_data.isSinglePlayer()) {
assert(!skip_main_menu); start_data.name = "singleplayer";
current_playername = "singleplayer"; start_data.password = "";
current_password = ""; start_data.socket_port = myrand_range(49152, 65535);
current_address = "";
current_port = myrand_range(49152, 65535);
} else { } else {
g_settings->set("name", playername); g_settings->set("name", start_data.name);
if (!address.empty()) { if (!start_data.address.empty()) {
ServerListSpec server; ServerListSpec server;
server["name"] = menudata.servername; server["name"] = server_name;
server["address"] = menudata.address; server["address"] = start_data.address;
server["port"] = menudata.port; server["port"] = itos(start_data.socket_port);
server["description"] = menudata.serverdescription; server["description"] = server_description;
ServerList::insert(server); ServerList::insert(server);
} }
} }
if (start_data.name.length() > PLAYERNAME_SIZE - 1) {
error_message = gettext("Player name too long.");
start_data.name.resize(PLAYERNAME_SIZE);
g_settings->set("name", start_data.name);
return false;
}
auto &worldspec = start_data.world_spec;
infostream << "Selected world: " << worldspec.name infostream << "Selected world: " << worldspec.name
<< " [" << worldspec.path << "]" << std::endl; << " [" << worldspec.path << "]" << std::endl;
if (current_address.empty()) { // If local game if (start_data.address.empty()) {
// For singleplayer and local server
if (worldspec.path.empty()) { if (worldspec.path.empty()) {
error_message = gettext("No world selected and no address " error_message = gettext("No world selected and no address "
"provided. Nothing to do."); "provided. Nothing to do.");
@ -504,8 +523,8 @@ bool ClientLauncher::launch_game(std::string &error_message,
} }
// Load gamespec for required game // Load gamespec for required game
gamespec = findWorldSubgame(worldspec.path); start_data.game_spec = findWorldSubgame(worldspec.path);
if (!gamespec.isValid() && !game_params.game_spec.isValid()) { if (!start_data.game_spec.isValid()) {
error_message = gettext("Could not find or load game \"") error_message = gettext("Could not find or load game \"")
+ worldspec.gameid + "\""; + worldspec.gameid + "\"";
errorstream << error_message << std::endl; errorstream << error_message << std::endl;
@ -515,15 +534,7 @@ bool ClientLauncher::launch_game(std::string &error_message,
if (porting::signal_handler_killstatus()) if (porting::signal_handler_killstatus())
return true; return true;
if (game_params.game_spec.isValid() && if (!start_data.game_spec.isValid()) {
game_params.game_spec.id != worldspec.gameid) {
warningstream << "Overriding gamespec from \""
<< worldspec.gameid << "\" to \""
<< game_params.game_spec.id << "\"" << std::endl;
gamespec = game_params.game_spec;
}
if (!gamespec.isValid()) {
error_message = gettext("Invalid gamespec."); error_message = gettext("Invalid gamespec.");
error_message += " (world.gameid=" + worldspec.gameid + ")"; error_message += " (world.gameid=" + worldspec.gameid + ")";
errorstream << error_message << std::endl; errorstream << error_message << std::endl;
@ -531,6 +542,7 @@ bool ClientLauncher::launch_game(std::string &error_message,
} }
} }
start_data.world_path = start_data.world_spec.path;
return true; return true;
} }

@ -32,15 +32,15 @@ public:
~ClientLauncher(); ~ClientLauncher();
bool run(GameParams &game_params, const Settings &cmd_args); bool run(GameStartData &start_data, const Settings &cmd_args);
protected: private:
void init_args(GameParams &game_params, const Settings &cmd_args); void init_args(GameStartData &start_data, const Settings &cmd_args);
bool init_engine(); bool init_engine();
void init_input(); void init_input();
bool launch_game(std::string &error_message, bool reconnect_requested, bool launch_game(std::string &error_message, bool reconnect_requested,
GameParams &game_params, const Settings &cmd_args); GameStartData &start_data, const Settings &cmd_args);
void main_menu(MainMenuData *menudata); void main_menu(MainMenuData *menudata);
@ -48,23 +48,8 @@ protected:
bool list_video_modes = false; bool list_video_modes = false;
bool skip_main_menu = false; bool skip_main_menu = false;
bool use_freetype = false;
bool random_input = false; bool random_input = false;
std::string address = "";
std::string playername = "";
std::string password = "";
InputHandler *input = nullptr; InputHandler *input = nullptr;
MyEventReceiver *receiver = nullptr; MyEventReceiver *receiver = nullptr;
gui::IGUISkin *skin = nullptr; gui::IGUISkin *skin = nullptr;
gui::IGUIFont *font = nullptr;
SubgameSpec gamespec;
WorldSpec worldspec;
bool simple_singleplayer_mode = false;
// These are set up based on the menu and other things
// TODO: Are these required since there's already playername, password, etc
std::string current_playername = "inv£lid";
std::string current_password = "";
std::string current_address = "does-not-exist";
int current_port = 0;
}; };

@ -34,11 +34,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "clouds.h" #include "clouds.h"
#include "config.h" #include "config.h"
#include "content_cao.h" #include "content_cao.h"
#include "content/subgames.h"
#include "client/event_manager.h" #include "client/event_manager.h"
#include "fontengine.h" #include "fontengine.h"
#include "itemdef.h" #include "itemdef.h"
#include "log.h" #include "log.h"
#include "filesys.h" #include "filesys.h"
#include "gameparams.h"
#include "gettext.h" #include "gettext.h"
#include "gui/guiChatConsole.h" #include "gui/guiChatConsole.h"
#include "gui/guiConfirmRegistration.h" #include "gui/guiConfirmRegistration.h"
@ -669,19 +671,11 @@ public:
~Game(); ~Game();
bool startup(bool *kill, bool startup(bool *kill,
bool random_input,
InputHandler *input, InputHandler *input,
const std::string &map_dir, const GameStartData &game_params,
const std::string &playername,
const std::string &password,
// If address is "", local server is used and address is updated
std::string *address,
u16 port,
std::string &error_message, std::string &error_message,
bool *reconnect, bool *reconnect,
ChatBackend *chat_backend, ChatBackend *chat_backend);
const SubgameSpec &gamespec, // Used for local game
bool simple_singleplayer_mode);
void run(); void run();
void shutdown(); void shutdown();
@ -691,21 +685,18 @@ protected:
void extendedResourceCleanup(); void extendedResourceCleanup();
// Basic initialisation // Basic initialisation
bool init(const std::string &map_dir, std::string *address, bool init(const std::string &map_dir, const std::string &address,
u16 port, u16 port, const SubgameSpec &gamespec);
const SubgameSpec &gamespec);
bool initSound(); bool initSound();
bool createSingleplayerServer(const std::string &map_dir, bool createSingleplayerServer(const std::string &map_dir,
const SubgameSpec &gamespec, u16 port, std::string *address); const SubgameSpec &gamespec, u16 port);
// Client creation // Client creation
bool createClient(const std::string &playername, bool createClient(const GameStartData &start_data);
const std::string &password, std::string *address, u16 port);
bool initGui(); bool initGui();
// Client connection // Client connection
bool connectToServer(const std::string &playername, bool connectToServer(const GameStartData &start_data,
const std::string &password, std::string *address, u16 port,
bool *connect_ok, bool *aborted); bool *connect_ok, bool *aborted);
bool getServerContent(bool *aborted); bool getServerContent(bool *aborted);
@ -885,7 +876,6 @@ private:
bool *reconnect_requested; bool *reconnect_requested;
scene::ISceneNode *skybox; scene::ISceneNode *skybox;
bool random_input;
bool simple_singleplayer_mode; bool simple_singleplayer_mode;
/* End 'cache' */ /* End 'cache' */
@ -1017,28 +1007,21 @@ Game::~Game()
} }
bool Game::startup(bool *kill, bool Game::startup(bool *kill,
bool random_input,
InputHandler *input, InputHandler *input,
const std::string &map_dir, const GameStartData &start_data,
const std::string &playername,
const std::string &password,
std::string *address, // can change if simple_singleplayer_mode
u16 port,
std::string &error_message, std::string &error_message,
bool *reconnect, bool *reconnect,
ChatBackend *chat_backend, ChatBackend *chat_backend)
const SubgameSpec &gamespec,
bool simple_singleplayer_mode)
{ {
// "cache" // "cache"
this->device = RenderingEngine::get_raw_device(); this->device = RenderingEngine::get_raw_device();
this->kill = kill; this->kill = kill;
this->error_message = &error_message; this->error_message = &error_message;
this->reconnect_requested = reconnect; this->reconnect_requested = reconnect;
this->random_input = random_input;
this->input = input; this->input = input;
this->chat_backend = chat_backend; this->chat_backend = chat_backend;
this->simple_singleplayer_mode = simple_singleplayer_mode; this->simple_singleplayer_mode = start_data.isSinglePlayer();
input->keycache.populate(); input->keycache.populate();
@ -1059,10 +1042,12 @@ bool Game::startup(bool *kill,
g_client_translations->clear(); g_client_translations->clear();
if (!init(map_dir, address, port, gamespec)) // address can change if simple_singleplayer_mode
if (!init(start_data.world_spec.path, start_data.address,
start_data.socket_port, start_data.game_spec))
return false; return false;
if (!createClient(playername, password, address, port)) if (!createClient(start_data))
return false; return false;
RenderingEngine::initialize(client, hud); RenderingEngine::initialize(client, hud);
@ -1221,7 +1206,7 @@ void Game::shutdown()
bool Game::init( bool Game::init(
const std::string &map_dir, const std::string &map_dir,
std::string *address, const std::string &address,
u16 port, u16 port,
const SubgameSpec &gamespec) const SubgameSpec &gamespec)
{ {
@ -1245,8 +1230,8 @@ bool Game::init(
return false; return false;
// Create a server if not connecting to an existing one // Create a server if not connecting to an existing one
if (address->empty()) { if (address.empty()) {
if (!createSingleplayerServer(map_dir, gamespec, port, address)) if (!createSingleplayerServer(map_dir, gamespec, port))
return false; return false;
} }
@ -1281,7 +1266,7 @@ bool Game::initSound()
} }
bool Game::createSingleplayerServer(const std::string &map_dir, bool Game::createSingleplayerServer(const std::string &map_dir,
const SubgameSpec &gamespec, u16 port, std::string *address) const SubgameSpec &gamespec, u16 port)
{ {
showOverlayMessage(N_("Creating server..."), 0, 5); showOverlayMessage(N_("Creating server..."), 0, 5);
@ -1314,8 +1299,7 @@ bool Game::createSingleplayerServer(const std::string &map_dir,
return true; return true;
} }
bool Game::createClient(const std::string &playername, bool Game::createClient(const GameStartData &start_data)
const std::string &password, std::string *address, u16 port)
{ {
showOverlayMessage(N_("Creating client..."), 0, 10); showOverlayMessage(N_("Creating client..."), 0, 10);
@ -1330,8 +1314,7 @@ bool Game::createClient(const std::string &playername,
g_touchscreengui->hide(); g_touchscreengui->hide();
} }
#endif #endif
if (!connectToServer(playername, password, address, port, if (!connectToServer(start_data, &could_connect, &connect_aborted))
&could_connect, &connect_aborted))
return false; return false;
if (!could_connect) { if (!could_connect) {
@ -1463,8 +1446,7 @@ bool Game::initGui()
return true; return true;
} }
bool Game::connectToServer(const std::string &playername, bool Game::connectToServer(const GameStartData &start_data,
const std::string &password, std::string *address, u16 port,
bool *connect_ok, bool *connection_aborted) bool *connect_ok, bool *connection_aborted)
{ {
*connect_ok = false; // Let's not be overly optimistic *connect_ok = false; // Let's not be overly optimistic
@ -1473,10 +1455,10 @@ bool Game::connectToServer(const std::string &playername,
showOverlayMessage(N_("Resolving address..."), 0, 15); showOverlayMessage(N_("Resolving address..."), 0, 15);
Address connect_address(0, 0, 0, 0, port); Address connect_address(0, 0, 0, 0, start_data.socket_port);
try { try {
connect_address.Resolve(address->c_str()); connect_address.Resolve(start_data.address.c_str());
if (connect_address.isZero()) { // i.e. INADDR_ANY, IN6ADDR_ANY if (connect_address.isZero()) { // i.e. INADDR_ANY, IN6ADDR_ANY
//connect_address.Resolve("localhost"); //connect_address.Resolve("localhost");
@ -1503,7 +1485,8 @@ bool Game::connectToServer(const std::string &playername,
return false; return false;
} }
client = new Client(playername.c_str(), password, *address, client = new Client(start_data.name.c_str(),
start_data.password, start_data.address,
*draw_control, texture_src, shader_src, *draw_control, texture_src, shader_src,
itemdef_manager, nodedef_manager, sound, eventmgr, itemdef_manager, nodedef_manager, sound, eventmgr,
connect_address.isIPv6(), m_game_ui.get()); connect_address.isIPv6(), m_game_ui.get());
@ -1574,13 +1557,13 @@ bool Game::connectToServer(const std::string &playername,
} else { } else {
registration_confirmation_shown = true; registration_confirmation_shown = true;
(new GUIConfirmRegistration(guienv, guienv->getRootGUIElement(), -1, (new GUIConfirmRegistration(guienv, guienv->getRootGUIElement(), -1,
&g_menumgr, client, playername, password, &g_menumgr, client, start_data.name, start_data.password,
connection_aborted, texture_src))->drop(); connection_aborted, texture_src))->drop();
} }
} else { } else {
wait_time += dtime; wait_time += dtime;
// Only time out if we aren't waiting for the server we started // Only time out if we aren't waiting for the server we started
if (!address->empty() && wait_time > 10) { if (!start_data.isSinglePlayer() && wait_time > 10) {
*error_message = "Connection timed out."; *error_message = "Connection timed out.";
errorstream << *error_message << std::endl; errorstream << *error_message << std::endl;
break; break;
@ -2399,10 +2382,10 @@ void Game::checkZoomEnabled()
void Game::updateCameraDirection(CameraOrientation *cam, float dtime) void Game::updateCameraDirection(CameraOrientation *cam, float dtime)
{ {
if ((device->isWindowActive() && device->isWindowFocused() if ((device->isWindowActive() && device->isWindowFocused()
&& !isMenuActive()) || random_input) { && !isMenuActive()) || input->isRandom()) {
#ifndef __ANDROID__ #ifndef __ANDROID__
if (!random_input) { if (!input->isRandom()) {
// Mac OSX gets upset if this is set every frame // Mac OSX gets upset if this is set every frame
if (device->getCursorControl()->isVisible()) if (device->getCursorControl()->isVisible())
device->getCursorControl()->setVisible(false); device->getCursorControl()->setVisible(false);
@ -3349,7 +3332,7 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
} }
// formspec in meta // formspec in meta
if (meta && !meta->getString("formspec").empty() && !random_input if (meta && !meta->getString("formspec").empty() && !input->isRandom()
&& !isKeyDown(KeyType::SNEAK)) { && !isKeyDown(KeyType::SNEAK)) {
// on_rightclick callbacks are called anyway // on_rightclick callbacks are called anyway
if (nodedef_manager->get(map.getNode(nodepos)).rightclickable) if (nodedef_manager->get(map.getNode(nodepos)).rightclickable)
@ -4260,19 +4243,11 @@ void Game::showPauseMenu()
/****************************************************************************/ /****************************************************************************/
void the_game(bool *kill, void the_game(bool *kill,
bool random_input,
InputHandler *input, InputHandler *input,
const std::string &map_dir, const GameStartData &start_data,
const std::string &playername,
const std::string &password,
const std::string &address, // If empty local server is created
u16 port,
std::string &error_message, std::string &error_message,
ChatBackend &chat_backend, ChatBackend &chat_backend,
bool *reconnect_requested, bool *reconnect_requested) // Used for local game
const SubgameSpec &gamespec, // Used for local game
bool simple_singleplayer_mode)
{ {
Game game; Game game;
@ -4280,14 +4255,11 @@ void the_game(bool *kill,
* is created then this is updated and we don't want to change the value * is created then this is updated and we don't want to change the value
* passed to us by the calling function * passed to us by the calling function
*/ */
std::string server_address = address;
try { try {
if (game.startup(kill, random_input, input, map_dir, if (game.startup(kill, input, start_data, error_message,
playername, password, &server_address, port, error_message, reconnect_requested, &chat_backend)) {
reconnect_requested, &chat_backend, gamespec,
simple_singleplayer_mode)) {
game.run(); game.run();
} }

@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class InputHandler; class InputHandler;
class ChatBackend; /* to avoid having to include chat.h */ class ChatBackend; /* to avoid having to include chat.h */
struct SubgameSpec; struct SubgameSpec;
struct GameStartData;
struct Jitter { struct Jitter {
f32 max, min, avg, counter, max_sample, min_sample, max_fraction; f32 max, min, avg, counter, max_sample, min_sample, max_fraction;
@ -41,16 +42,10 @@ struct CameraOrientation {
f32 camera_pitch; // "up/down" f32 camera_pitch; // "up/down"
}; };
void the_game(bool *kill, void the_game(bool *kill,
bool random_input,
InputHandler *input, InputHandler *input,
const std::string &map_dir, const GameStartData &start_data,
const std::string &playername,
const std::string &password,
const std::string &address, // If "", local server is used
u16 port,
std::string &error_message, std::string &error_message,
ChatBackend &chat_backend, ChatBackend &chat_backend,
bool *reconnect_requested, bool *reconnect_requested);
const SubgameSpec &gamespec, // Used for local game
bool simple_singleplayer_mode);

@ -219,6 +219,11 @@ public:
virtual ~InputHandler() = default; virtual ~InputHandler() = default;
virtual bool isRandom() const
{
return false;
}
virtual bool isKeyDown(GameKeyType k) = 0; virtual bool isKeyDown(GameKeyType k) = 0;
virtual bool wasKeyDown(GameKeyType k) = 0; virtual bool wasKeyDown(GameKeyType k) = 0;
virtual bool cancelPressed() = 0; virtual bool cancelPressed() = 0;
@ -372,6 +377,11 @@ class RandomInputHandler : public InputHandler
public: public:
RandomInputHandler() = default; RandomInputHandler() = default;
bool isRandom() const
{
return true;
}
virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; } virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
virtual bool wasKeyDown(GameKeyType k) { return false; } virtual bool wasKeyDown(GameKeyType k) { return false; }
virtual bool cancelPressed() { return false; } virtual bool cancelPressed() { return false; }

@ -23,10 +23,29 @@ with this program; if not, write to the Free Software Foundation, Inc.,
struct SubgameSpec; struct SubgameSpec;
// Information provided from "main"
struct GameParams struct GameParams
{ {
GameParams() = default;
u16 socket_port; u16 socket_port;
std::string world_path; std::string world_path;
SubgameSpec game_spec; SubgameSpec game_spec;
bool is_dedicated_server; bool is_dedicated_server;
}; };
// Information processed by main menu
struct GameStartData : GameParams
{
GameStartData() = default;
bool isSinglePlayer() const { return address.empty() && !local_server; }
std::string name;
std::string password;
std::string address;
bool local_server;
// "world_path" must be kept in sync!
WorldSpec world_spec;
};

@ -197,7 +197,7 @@ int main(int argc, char *argv[])
} }
#endif #endif
GameParams game_params; GameStartData game_params;
#ifdef SERVER #ifdef SERVER
porting::attachOrCreateConsole(); porting::attachOrCreateConsole();
game_params.is_dedicated_server = true; game_params.is_dedicated_server = true;
@ -604,10 +604,14 @@ static bool game_configure(GameParams *game_params, const Settings &cmd_args)
static void game_configure_port(GameParams *game_params, const Settings &cmd_args) static void game_configure_port(GameParams *game_params, const Settings &cmd_args)
{ {
if (cmd_args.exists("port")) if (cmd_args.exists("port")) {
game_params->socket_port = cmd_args.getU16("port"); game_params->socket_port = cmd_args.getU16("port");
else } else {
game_params->socket_port = g_settings->getU16("port"); if (game_params->is_dedicated_server)
game_params->socket_port = g_settings->getU16("port");
else
game_params->socket_port = g_settings->getU16("remote_port");
}
if (game_params->socket_port == 0) if (game_params->socket_port == 0)
game_params->socket_port = DEFAULT_SERVER_PORT; game_params->socket_port = DEFAULT_SERVER_PORT;