Add "simple singleplayer mode"; Fix a number of GUI things

This commit is contained in:
Perttu Ahola 2012-03-15 15:20:20 +02:00
parent 899004207d
commit 6298878bfa
9 changed files with 101 additions and 48 deletions

View File

@ -658,7 +658,8 @@ void the_game(
std::wstring &error_message, std::wstring &error_message,
std::string configpath, std::string configpath,
ChatBackend &chat_backend, ChatBackend &chat_backend,
const SubgameSpec &gamespec // Used for local game const SubgameSpec &gamespec, // Used for local game,
bool simple_singleplayer_mode
) )
{ {
video::IVideoDriver* driver = device->getVideoDriver(); video::IVideoDriver* driver = device->getVideoDriver();
@ -709,7 +710,8 @@ void the_game(
if(address == ""){ if(address == ""){
draw_load_screen(L"Creating server...", driver, font); draw_load_screen(L"Creating server...", driver, font);
infostream<<"Creating server"<<std::endl; infostream<<"Creating server"<<std::endl;
server = new Server(map_dir, configpath, gamespec); server = new Server(map_dir, configpath, gamespec,
simple_singleplayer_mode);
server->start(port); server->start(port);
} }
@ -1357,7 +1359,7 @@ void the_game(
<<"Launching pause menu"<<std::endl; <<"Launching pause menu"<<std::endl;
// It will delete itself by itself // It will delete itself by itself
(new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback, (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
&g_menumgr))->drop(); &g_menumgr, simple_singleplayer_mode))->drop();
// Move mouse cursor on top of the disconnect button // Move mouse cursor on top of the disconnect button
input->setMousePos(displaycenter.X, displaycenter.Y+25); input->setMousePos(displaycenter.X, displaycenter.Y+25);

View File

@ -138,7 +138,8 @@ void the_game(
std::wstring &error_message, std::wstring &error_message,
std::string configpath, std::string configpath,
ChatBackend &chat_backend, ChatBackend &chat_backend,
const SubgameSpec &gamespec // Used for local game const SubgameSpec &gamespec, // Used for local game
bool simple_singleplayer_mode
); );
#endif #endif

View File

@ -720,15 +720,13 @@ void GUIMainMenu::readInput(MainMenuData *dst)
if(e != NULL && e->getType() == gui::EGUIET_TAB_CONTROL) if(e != NULL && e->getType() == gui::EGUIET_TAB_CONTROL)
dst->selected_tab = ((gui::IGUITabControl*)e)->getActiveTab(); dst->selected_tab = ((gui::IGUITabControl*)e)->getActiveTab();
} }
if(getTab() == TAB_SINGLEPLAYER) if(dst->selected_tab == TAB_SINGLEPLAYER)
{ {
dst->name = L"singleplayer"; dst->simple_singleplayer_mode = true;
dst->password = L"";
dst->address = L"";
dst->port = 30000;
} }
else else
{ {
dst->simple_singleplayer_mode = false;
{ {
gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT); gui::IGUIElement *e = getElementFromId(GUI_ID_NAME_INPUT);
if(e != NULL) if(e != NULL)

View File

@ -45,6 +45,7 @@ struct MainMenuData
bool creative_mode; bool creative_mode;
bool enable_damage; bool enable_damage;
int selected_world; int selected_world;
bool simple_singleplayer_mode;
// Actions // Actions
WorldSpec delete_world_spec; WorldSpec delete_world_spec;
std::wstring create_world_name; std::wstring create_world_name;
@ -62,7 +63,8 @@ struct MainMenuData
// Server opts // Server opts
creative_mode(false), creative_mode(false),
enable_damage(false), enable_damage(false),
selected_world(0) selected_world(0),
simple_singleplayer_mode(false)
{} {}
}; };

View File

@ -34,10 +34,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
GUIPauseMenu::GUIPauseMenu(gui::IGUIEnvironment* env, GUIPauseMenu::GUIPauseMenu(gui::IGUIEnvironment* env,
gui::IGUIElement* parent, s32 id, gui::IGUIElement* parent, s32 id,
IGameCallback *gamecallback, IGameCallback *gamecallback,
IMenuManager *menumgr): IMenuManager *menumgr,
GUIModalMenu(env, parent, id, menumgr) bool simple_singleplayer_mode):
GUIModalMenu(env, parent, id, menumgr),
m_gamecallback(gamecallback),
m_simple_singleplayer_mode(simple_singleplayer_mode)
{ {
m_gamecallback = gamecallback;
} }
GUIPauseMenu::~GUIPauseMenu() GUIPauseMenu::~GUIPauseMenu()
@ -106,7 +108,7 @@ void GUIPauseMenu::regenerateGui(v2u32 screensize)
*/ */
const s32 btn_height = 30; const s32 btn_height = 30;
const s32 btn_gap = 20; const s32 btn_gap = 20;
const s32 btn_num = 4; const s32 btn_num = m_simple_singleplayer_mode ? 3 : 4;
s32 btn_y = size.Y/2-((btn_num*btn_height+(btn_num-1)*btn_gap))/2; s32 btn_y = size.Y/2-((btn_num*btn_height+(btn_num-1)*btn_gap))/2;
changeCtype(""); changeCtype("");
{ {
@ -116,18 +118,21 @@ void GUIPauseMenu::regenerateGui(v2u32 screensize)
wgettext("Continue")); wgettext("Continue"));
} }
btn_y += btn_height + btn_gap; btn_y += btn_height + btn_gap;
if(!m_simple_singleplayer_mode)
{ {
core::rect<s32> rect(0, 0, 140, btn_height); {
rect = rect + v2s32(size.X/2-140/2, btn_y); core::rect<s32> rect(0, 0, 140, btn_height);
Environment->addButton(rect, this, 261, rect = rect + v2s32(size.X/2-140/2, btn_y);
wgettext("Change Password")); Environment->addButton(rect, this, 261,
wgettext("Change Password"));
}
btn_y += btn_height + btn_gap;
} }
btn_y += btn_height + btn_gap;
{ {
core::rect<s32> rect(0, 0, 140, btn_height); core::rect<s32> rect(0, 0, 140, btn_height);
rect = rect + v2s32(size.X/2-140/2, btn_y); rect = rect + v2s32(size.X/2-140/2, btn_y);
Environment->addButton(rect, this, 260, Environment->addButton(rect, this, 260,
wgettext("Disconnect")); wgettext("Exit to Menu"));
} }
btn_y += btn_height + btn_gap; btn_y += btn_height + btn_gap;
{ {

View File

@ -37,7 +37,8 @@ public:
GUIPauseMenu(gui::IGUIEnvironment* env, GUIPauseMenu(gui::IGUIEnvironment* env,
gui::IGUIElement* parent, s32 id, gui::IGUIElement* parent, s32 id,
IGameCallback *gamecallback, IGameCallback *gamecallback,
IMenuManager *menumgr); IMenuManager *menumgr,
bool simple_singleplayer_mode);
~GUIPauseMenu(); ~GUIPauseMenu();
void removeChildren(); void removeChildren();
@ -52,6 +53,7 @@ public:
private: private:
IGameCallback *m_gamecallback; IGameCallback *m_gamecallback;
bool m_simple_singleplayer_mode;
}; };
#endif #endif

View File

@ -1063,7 +1063,7 @@ int main(int argc, char *argv[])
infostream<<"Using gamespec \""<<gamespec.id<<"\""<<std::endl; infostream<<"Using gamespec \""<<gamespec.id<<"\""<<std::endl;
// Create server // Create server
Server server(world_path, configpath, gamespec); Server server(world_path, configpath, gamespec, false);
server.start(port); server.start(port);
// Run server // Run server
@ -1253,6 +1253,13 @@ int main(int argc, char *argv[])
SubgameSpec gamespec; SubgameSpec gamespec;
WorldSpec worldspec; WorldSpec worldspec;
bool simple_singleplayer_mode = false;
// These are set up based on the menu and other things
std::string current_playername = "inv£lid";
std::string current_password = "";
std::string current_address = "does-not-exist";
int current_port = 0;
/* /*
Out-of-game menu loop. Out-of-game menu loop.
@ -1377,6 +1384,7 @@ int main(int argc, char *argv[])
int newport = stoi(wide_to_narrow(menudata.port)); int newport = stoi(wide_to_narrow(menudata.port));
if(newport != 0) if(newport != 0)
port = newport; port = newport;
simple_singleplayer_mode = menudata.simple_singleplayer_mode;
// Save settings // Save settings
g_settings->setS32("selected_mainmenu_tab", menudata.selected_tab); g_settings->setS32("selected_mainmenu_tab", menudata.selected_tab);
g_settings->set("new_style_leaves", itos(menudata.fancy_trees)); g_settings->set("new_style_leaves", itos(menudata.fancy_trees));
@ -1391,14 +1399,24 @@ int main(int argc, char *argv[])
if(menudata.selected_world != -1) if(menudata.selected_world != -1)
g_settings->set("selected_world_path", g_settings->set("selected_world_path",
worldspecs[menudata.selected_world].path); worldspecs[menudata.selected_world].path);
/*// Update configuration file
if(configpath != "")
g_settings->updateConfigFile(configpath.c_str());*/
// Break out of menu-game loop to shut down cleanly // Break out of menu-game loop to shut down cleanly
if(device->run() == false || kill == true) if(device->run() == false || kill == true)
break; break;
current_playername = playername;
current_password = password;
current_address = address;
current_port = port;
// If using simple singleplayer mode, override
if(simple_singleplayer_mode){
current_playername = "singleplayer";
current_password = "";
current_address = "";
current_port = 30011;
}
// Set world path to selected one // Set world path to selected one
if(menudata.selected_world != -1){ if(menudata.selected_world != -1){
worldspec = worldspecs[menudata.selected_world]; worldspec = worldspecs[menudata.selected_world];
@ -1435,7 +1453,7 @@ int main(int argc, char *argv[])
} }
// If local game // If local game
if(address == "") if(current_address == "")
{ {
if(menudata.selected_world == -1){ if(menudata.selected_world == -1){
error_message = L"No world selected and no address " error_message = L"No world selected and no address "
@ -1474,7 +1492,7 @@ int main(int argc, char *argv[])
// Break out of menu-game loop to shut down cleanly // Break out of menu-game loop to shut down cleanly
if(device->run() == false || kill == true) if(device->run() == false || kill == true)
break; break;
/* /*
Run game Run game
*/ */
@ -1485,14 +1503,15 @@ int main(int argc, char *argv[])
device, device,
font, font,
worldspec.path, worldspec.path,
playername, current_playername,
password, current_password,
address, current_address,
port, current_port,
error_message, error_message,
configpath, configpath,
chat_backend, chat_backend,
gamespec gamespec,
simple_singleplayer_mode
); );
} //try } //try

View File

@ -836,11 +836,13 @@ void PlayerInfo::PrintLine(std::ostream *s)
Server::Server( Server::Server(
const std::string &path_world, const std::string &path_world,
const std::string &path_config, const std::string &path_config,
const SubgameSpec &gamespec const SubgameSpec &gamespec,
bool simple_singleplayer_mode
): ):
m_path_world(path_world), m_path_world(path_world),
m_path_config(path_config), m_path_config(path_config),
m_gamespec(gamespec), m_gamespec(gamespec),
m_simple_singleplayer_mode(simple_singleplayer_mode),
m_async_fatal_error(""), m_async_fatal_error(""),
m_env(NULL), m_env(NULL),
m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this), m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
@ -880,7 +882,11 @@ Server::Server(
// share/server // share/server
m_path_share = porting::path_share + DIR_DELIM + "server"; m_path_share = porting::path_share + DIR_DELIM + "server";
infostream<<"Server created for gameid \""<<m_gamespec.id<<"\""<<std::endl; infostream<<"Server created for gameid \""<<m_gamespec.id<<"\"";
if(m_simple_singleplayer_mode)
infostream<<" in simple singleplayer mode"<<std::endl;
else
infostream<<std::endl;
infostream<<"- world: "<<m_path_world<<std::endl; infostream<<"- world: "<<m_path_world<<std::endl;
infostream<<"- config: "<<m_path_config<<std::endl; infostream<<"- config: "<<m_path_config<<std::endl;
infostream<<"- game: "<<m_gamespec.path<<std::endl; infostream<<"- game: "<<m_gamespec.path<<std::endl;
@ -2105,6 +2111,16 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
SendAccessDenied(m_con, peer_id, L"Invalid password"); SendAccessDenied(m_con, peer_id, L"Invalid password");
return; return;
} }
// Do not allow multiple players in simple singleplayer mode.
// This isn't a perfect way to do it, but will suffice for now.
if(m_simple_singleplayer_mode && m_clients.size() > 1){
infostream<<"Server: Not allowing another client to connect in"
<<" simple singleplayer mode"<<std::endl;
SendAccessDenied(m_con, peer_id,
L"Running in simple singleplayer mode.");
return;
}
// Enforce user limit. // Enforce user limit.
// Don't enforce for users that have some admin right // Don't enforce for users that have some admin right
@ -2204,21 +2220,25 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
m_con.Send(peer_id, 0, data, true); m_con.Send(peer_id, 0, data, true);
} }
// Send information about server to player in chat // Note things in chat if not in simple singleplayer mode
SendChatMessage(peer_id, getStatusString()); if(!m_simple_singleplayer_mode)
// Send information about joining in chat
{ {
std::wstring name = L"unknown"; // Send information about server to player in chat
Player *player = m_env->getPlayer(peer_id); SendChatMessage(peer_id, getStatusString());
if(player != NULL)
name = narrow_to_wide(player->getName());
std::wstring message; // Send information about joining in chat
message += L"*** "; {
message += name; std::wstring name = L"unknown";
message += L" joined game"; Player *player = m_env->getPlayer(peer_id);
BroadcastChatMessage(message); if(player != NULL)
name = narrow_to_wide(player->getName());
std::wstring message;
message += L"*** ";
message += name;
message += L" joined game";
BroadcastChatMessage(message);
}
} }
// Warnings about protocol version can be issued here // Warnings about protocol version can be issued here

View File

@ -410,7 +410,8 @@ public:
Server( Server(
const std::string &path_world, const std::string &path_world,
const std::string &path_config, const std::string &path_config,
const SubgameSpec &gamespec const SubgameSpec &gamespec,
bool simple_singleplayer_mode
); );
~Server(); ~Server();
void start(unsigned short port); void start(unsigned short port);
@ -659,6 +660,9 @@ private:
std::string m_path_config; std::string m_path_config;
// Subgame specification // Subgame specification
SubgameSpec m_gamespec; SubgameSpec m_gamespec;
// If true, do not allow multiple players and hide some multiplayer
// functionality
bool m_simple_singleplayer_mode;
// Equivalent of /usr/share/minetest/server // Equivalent of /usr/share/minetest/server
std::string m_path_share; std::string m_path_share;