]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/game.cpp
make formspec textarea wordwrap
[dragonfireclient.git] / src / game.cpp
index 761f65f83061888e85479dce1db099a1f9d56308..6265c430a1a95a4dd7e1cab2728b7be2c06fb878 100644 (file)
@@ -32,7 +32,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "guiVolumeChange.h"
 #include "guiFormSpecMenu.h"
 #include "guiTextInputMenu.h"
-#include "guiDeathScreen.h"
 #include "tool.h"
 #include "guiChatConsole.h"
 #include "config.h"
@@ -132,7 +131,7 @@ struct LocalFormspecHandler : public TextDest
                m_client = client;
        }
 
-       void gotText(std::string message) {
+       void gotText(std::wstring message) {
                errorstream << "LocalFormspecHandler::gotText old style message received" << std::endl;
        }
 
@@ -154,6 +153,11 @@ struct LocalFormspecHandler : public TextDest
                                return;
                        }
 
+                       if (fields.find("btn_change_password") != fields.end()) {
+                               g_gamecallback->changePassword();
+                               return;
+                       }
+
                        if (fields.find("quit") != fields.end()) {
                                return;
                        }
@@ -177,6 +181,13 @@ struct LocalFormspecHandler : public TextDest
                        }
                }
 
+               if (m_formname == "MT_DEATH_SCREEN") {
+                       if ((fields.find("btn_respawn") != fields.end())) {
+                               m_client->sendRespawn();
+                               return;
+                       }
+               }
+
                errorstream << "LocalFormspecHandler::gotText unhandled >" << m_formname << "< event" << std::endl;
                int i = 0;
                for (std::map<std::string,std::string>::iterator iter = fields.begin();
@@ -189,26 +200,6 @@ struct LocalFormspecHandler : public TextDest
        Client *m_client;
 };
 
-/* Respawn menu callback */
-
-class MainRespawnInitiator: public IRespawnInitiator
-{
-public:
-       MainRespawnInitiator(bool *active, Client *client):
-               m_active(active), m_client(client)
-       {
-               *m_active = true;
-       }
-       void respawn()
-       {
-               *m_active = false;
-               m_client->sendRespawn();
-       }
-private:
-       bool *m_active;
-       Client *m_client;
-};
-
 /* Form update callback */
 
 class NodeMetadataFormSource: public IFormSource
@@ -997,10 +988,37 @@ static void show_chat_menu(FormspecFormSource* current_formspec,
        menu->drop();
 }
 
+static void show_deathscreen(FormspecFormSource* current_formspec,
+               TextDest* current_textdest, IWritableTextureSource* tsrc,
+               IrrlichtDevice * device, Client* client)
+{
+       std::string formspec =
+               std::string("") +
+               "size[11,5.5,true]"
+               "bgcolor[#320000b4;true]"
+               "label[4.85,1.35;You died.]"
+               "button_exit[4,3;3,0.5;btn_respawn;" + gettext("Respawn") + "]"
+               ;
+
+       /* Create menu */
+       /* Note: FormspecFormSource and LocalFormspecHandler
+        * are deleted by guiFormSpecMenu                     */
+       current_formspec = new FormspecFormSource(formspec,&current_formspec);
+       current_textdest = new LocalFormspecHandler("MT_DEATH_SCREEN",client);
+       GUIFormSpecMenu *menu =
+                       new GUIFormSpecMenu(device, guiroot, -1,
+                                       &g_menumgr,
+                                       NULL, NULL, tsrc);
+       menu->doPause = false;
+       menu->setFormSource(current_formspec);
+       menu->setTextDest(current_textdest);
+       menu->drop();
+}
+
 /******************************************************************************/
 static void show_pause_menu(FormspecFormSource* current_formspec,
                TextDest* current_textdest, IWritableTextureSource* tsrc,
-               IrrlichtDevice * device)
+               IrrlichtDevice * device, bool singleplayermode)
 {
 
        std::string control_text = wide_to_narrow(wstrgettext("Default Controls:\n"
@@ -1016,25 +1034,34 @@ static void show_pause_menu(FormspecFormSource* current_formspec,
                        "- T: chat\n"
                        ));
 
+       float ypos = singleplayermode ? 1.0 : 0.5;
        std::ostringstream os;
-       os<<"Minetest\n";
-       os<<minetest_build_info<<"\n";
-       os<<"path_user = "<<wrap_rows(porting::path_user, 20)<<"\n";
 
-       std::string formspec =
-               "size[11,5.5,true]"
-               "button_exit[4,1;3,0.5;btn_continue;"  + wide_to_narrow(wstrgettext("Continue"))     + "]"
-               "button_exit[4,2;3,0.5;btn_sound;"     + wide_to_narrow(wstrgettext("Sound Volume")) + "]"
-               "button_exit[4,3;3,0.5;btn_exit_menu;" + wide_to_narrow(wstrgettext("Exit to Menu")) + "]"
-               "button_exit[4,4;3,0.5;btn_exit_os;"   + wide_to_narrow(wstrgettext("Exit to OS"))   + "]"
-               "textarea[7.5,0.25;3.75,6;;" + control_text + ";]"
-               "textarea[0.4,0.25;3.5,6;;" + os.str() + ";]"
-               ;
+       os << "size[11,5.5,true]"
+                       << "button_exit[4," << (ypos++) << ";3,0.5;btn_continue;"
+                                       << wide_to_narrow(wstrgettext("Continue"))     << "]";
+
+       if (!singleplayermode) {
+               os << "button_exit[4," << (ypos++) << ";3,0.5;btn_change_password;"
+                                       << wide_to_narrow(wstrgettext("Change Password")) << "]";
+               }
+
+       os              << "button_exit[4," << (ypos++) << ";3,0.5;btn_sound;"
+                                       << wide_to_narrow(wstrgettext("Sound Volume")) << "]";
+       os              << "button_exit[4," << (ypos++) << ";3,0.5;btn_exit_menu;"
+                                       << wide_to_narrow(wstrgettext("Exit to Menu")) << "]";
+       os              << "button_exit[4," << (ypos++) << ";3,0.5;btn_exit_os;"
+                                       << wide_to_narrow(wstrgettext("Exit to OS"))   << "]"
+                       << "textarea[7.5,0.25;3.75,6;;" << control_text << ";]"
+                       << "textarea[0.4,0.25;3.5,6;;" << "Minetest\n"
+                       << minetest_build_info << "\n"
+                       << "path_user = " << wrap_rows(porting::path_user, 20)
+                       << "\n;]";
 
        /* Create menu */
        /* Note: FormspecFormSource and LocalFormspecHandler  *
         * are deleted by guiFormSpecMenu                     */
-       current_formspec = new FormspecFormSource(formspec,&current_formspec);
+       current_formspec = new FormspecFormSource(os.str(),&current_formspec);
        current_textdest = new LocalFormspecHandler("MT_PAUSE_MENU");
        GUIFormSpecMenu *menu =
                new GUIFormSpecMenu(device, guiroot, -1, &g_menumgr, NULL, NULL, tsrc);
@@ -1133,28 +1160,35 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                draw_load_screen(text, device, font,0,25);
                delete[] text;
                infostream<<"Creating server"<<std::endl;
-               server = new Server(map_dir, gamespec,
-                               simple_singleplayer_mode);
 
                std::string bind_str = g_settings->get("bind_address");
                Address bind_addr(0,0,0,0, port);
 
-               if (bind_str != "")
-               {
-                       try {
-                               bind_addr.Resolve(bind_str.c_str());
-                               address = bind_str;
-                       } catch (ResolveError &e) {
-                               infostream << "Resolving bind address \"" << bind_str
-                                                  << "\" failed: " << e.what()
-                                                  << " -- Listening on all addresses." << std::endl;
+               if (g_settings->getBool("ipv6_server")) {
+                       bind_addr.setAddress((IPv6AddressBytes*) NULL);
+               }
+               try {
+                       bind_addr.Resolve(bind_str.c_str());
+                       address = bind_str;
+               } catch (ResolveError &e) {
+                       infostream << "Resolving bind address \"" << bind_str
+                                          << "\" failed: " << e.what()
+                                          << " -- Listening on all addresses." << std::endl;
+               }
 
-                               if (g_settings->getBool("ipv6_server")) {
-                                       bind_addr.setAddress((IPv6AddressBytes*) NULL);
-                               }
-                       }
+               if(bind_addr.isIPv6() && !g_settings->getBool("enable_ipv6")) {
+                       error_message = L"Unable to listen on " +
+                               narrow_to_wide(bind_addr.serializeString()) +
+                               L" because IPv6 is disabled";
+                       errorstream<<wide_to_narrow(error_message)<<std::endl;
+                       // Break out of client scope
+                       return;
                }
 
+               server = new Server(map_dir, gamespec,
+                               simple_singleplayer_mode,
+                               bind_addr.isIPv6());
+
                server->start(bind_addr);
        }
 
@@ -1179,31 +1213,33 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                delete[] text;
        }
        Address connect_address(0,0,0,0, port);
-       try{
-               if(address == "")
-               {
+       try {
+               connect_address.Resolve(address.c_str());
+               if (connect_address.isZero()) { // i.e. INADDR_ANY, IN6ADDR_ANY
                        //connect_address.Resolve("localhost");
-                       if(g_settings->getBool("enable_ipv6") && g_settings->getBool("ipv6_server"))
-                       {
+                       if (connect_address.isIPv6()) {
                                IPv6AddressBytes addr_bytes;
                                addr_bytes.bytes[15] = 1;
                                connect_address.setAddress(&addr_bytes);
-                       }
-                       else
-                       {
+                       } else {
                                connect_address.setAddress(127,0,0,1);
                        }
                }
-               else
-                       connect_address.Resolve(address.c_str());
        }
-       catch(ResolveError &e)
-       {
+       catch(ResolveError &e) {
                error_message = L"Couldn't resolve address: " + narrow_to_wide(e.what());
                errorstream<<wide_to_narrow(error_message)<<std::endl;
                // Break out of client scope
                break;
        }
+       if(connect_address.isIPv6() && !g_settings->getBool("enable_ipv6")) {
+               error_message = L"Unable to connect to " +
+                       narrow_to_wide(connect_address.serializeString()) +
+                       L" because IPv6 is disabled";
+               errorstream<<wide_to_narrow(error_message)<<std::endl;
+               // Break out of client scope
+               break;
+       }
        
        /*
                Create client
@@ -1252,7 +1288,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                server->step(dtime);
                        
                        // End condition
-                       if(client.connectedAndInitialized()){
+                       if(client.getState() == LC_Init){
                                could_connect = true;
                                break;
                        }
@@ -1359,7 +1395,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                errorstream<<wide_to_narrow(error_message)<<std::endl;
                                break;
                        }
-                       if(!client.connectedAndInitialized()){
+                       if(client.getState() < LC_Init){
                                error_message = L"Client disconnected";
                                errorstream<<wide_to_narrow(error_message)<<std::endl;
                                break;
@@ -1447,6 +1483,8 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
        f32 camera_yaw = 0; // "right/left"
        f32 camera_pitch = 0; // "up/down"
 
+       int current_camera_mode = CAMERA_MODE_FIRST; // start in first-person view
+
        /*
                Clouds
        */
@@ -1568,7 +1606,6 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
 
        bool invert_mouse = g_settings->getBool("invert_mouse");
 
-       bool respawn_menu_active = false;
        bool update_wielded_item_trigger = true;
 
        bool show_hud = true;
@@ -1617,8 +1654,6 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
        Hud hud(driver, smgr, guienv, font, text_height,
                        gamedef, player, &local_inventory);
 
-       bool use_weather = g_settings->getBool("weather");
-
        core::stringw str = L"Minetest [";
        str += driver->getName();
        str += "]";
@@ -1894,7 +1929,8 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                }
                else if(input->wasKeyDown(EscapeKey))
                {
-                       show_pause_menu(current_formspec, current_textdest, tsrc, device);
+                       show_pause_menu(current_formspec, current_textdest, tsrc, device,
+                                       simple_singleplayer_mode);
                }
                else if(input->wasKeyDown(getKeySetting("keymap_chat")))
                {
@@ -2227,7 +2263,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                        else{
                                s32 dx = input->getMousePos().X - displaycenter.X;
                                s32 dy = input->getMousePos().Y - displaycenter.Y;
-                               if(invert_mouse)
+                               if(invert_mouse || player->camera_mode == CAMERA_MODE_THIRD_FRONT)
                                        dy = -dy;
                                //infostream<<"window active, pos difference "<<dx<<","<<dy<<std::endl;
                                
@@ -2292,18 +2328,17 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                camera_yaw
                        );
                        client.setPlayerControl(control);
-                       u32 keyPressed=
-                       1*(int)input->isKeyDown(getKeySetting("keymap_forward"))+
-                       2*(int)input->isKeyDown(getKeySetting("keymap_backward"))+
-                       4*(int)input->isKeyDown(getKeySetting("keymap_left"))+
-                       8*(int)input->isKeyDown(getKeySetting("keymap_right"))+
-                       16*(int)input->isKeyDown(getKeySetting("keymap_jump"))+
-                       32*(int)input->isKeyDown(getKeySetting("keymap_special1"))+
-                       64*(int)input->isKeyDown(getKeySetting("keymap_sneak"))+
-                       128*(int)input->getLeftState()+
-                       256*(int)input->getRightState();
                        LocalPlayer* player = client.getEnv().getLocalPlayer();
-                       player->keyPressed=keyPressed;
+                       player->keyPressed=
+                       (((int)input->isKeyDown(getKeySetting("keymap_forward"))  & 0x1) << 0) |
+                       (((int)input->isKeyDown(getKeySetting("keymap_backward")) & 0x1) << 1) |
+                       (((int)input->isKeyDown(getKeySetting("keymap_left"))     & 0x1) << 2) |
+                       (((int)input->isKeyDown(getKeySetting("keymap_right"))    & 0x1) << 3) |
+                       (((int)input->isKeyDown(getKeySetting("keymap_jump"))     & 0x1) << 4) |
+                       (((int)input->isKeyDown(getKeySetting("keymap_special1")) & 0x1) << 5) |
+                       (((int)input->isKeyDown(getKeySetting("keymap_sneak"))    & 0x1) << 6) |
+                       (((int)input->getLeftState()  & 0x1) << 7) |
+                       (((int)input->getRightState() & 0x1) << 8);
                }
 
                /*
@@ -2361,36 +2396,18 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                }
                                else if(event.type == CE_DEATHSCREEN)
                                {
-                                       if(respawn_menu_active)
-                                               continue;
+                                       show_deathscreen(current_formspec, current_textdest,
+                                                       tsrc, device, &client);
 
-                                       /*bool set_camera_point_target =
-                                                       event.deathscreen.set_camera_point_target;
-                                       v3f camera_point_target;
-                                       camera_point_target.X = event.deathscreen.camera_point_target_x;
-                                       camera_point_target.Y = event.deathscreen.camera_point_target_y;
-                                       camera_point_target.Z = event.deathscreen.camera_point_target_z;*/
-                                       MainRespawnInitiator *respawner =
-                                                       new MainRespawnInitiator(
-                                                                       &respawn_menu_active, &client);
-                                       GUIDeathScreen *menu =
-                                                       new GUIDeathScreen(guienv, guiroot, -1,
-                                                               &g_menumgr, respawner);
-                                       menu->drop();
-                                       
                                        chat_backend.addMessage(L"", L"You died.");
 
                                        /* Handle visualization */
-
                                        damage_flash = 0;
 
                                        LocalPlayer* player = client.getEnv().getLocalPlayer();
                                        player->hurt_tilt_timer = 0;
                                        player->hurt_tilt_strength = 0;
 
-                                       /*LocalPlayer* player = client.getLocalPlayer();
-                                       player->setPosition(player->getPosition() + v3f(0,-BS,0));
-                                       camera.update(player, busytime, screensize);*/
                                }
                                else if (event.type == CE_SHOW_FORMSPEC)
                                {
@@ -2636,13 +2653,24 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                LocalPlayer* player = client.getEnv().getLocalPlayer();
                float full_punch_interval = playeritem_toolcap.full_punch_interval;
                float tool_reload_ratio = time_from_last_punch / full_punch_interval;
+
+               if(input->wasKeyDown(getKeySetting("keymap_camera_mode"))) {
+
+                       if (current_camera_mode == CAMERA_MODE_FIRST)
+                               current_camera_mode = CAMERA_MODE_THIRD;
+                       else if (current_camera_mode == CAMERA_MODE_THIRD)
+                               current_camera_mode = CAMERA_MODE_THIRD_FRONT;
+                       else
+                               current_camera_mode = CAMERA_MODE_FIRST;
+
+               }
+               player->camera_mode = current_camera_mode;
                tool_reload_ratio = MYMIN(tool_reload_ratio, 1.0);
-               camera.update(player, dtime, busytime, screensize,
-                               tool_reload_ratio);
+               camera.update(player, dtime, busytime, screensize, tool_reload_ratio,
+                               current_camera_mode, client.getEnv());
                camera.step(dtime);
 
                v3f player_position = player->getPosition();
-               v3s16 pos_i = floatToInt(player_position, BS);
                v3f camera_position = camera.getPosition();
                v3f camera_direction = camera.getDirection();
                f32 camera_fov = camera.getFovMax();
@@ -2694,6 +2722,10 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                core::line3d<f32> shootline(camera_position,
                                camera_position + camera_direction * BS * (d+1));
 
+               // prevent player pointing anything in front-view
+               if (current_camera_mode == CAMERA_MODE_THIRD_FRONT)
+                       shootline = core::line3d<f32>(0,0,0,0,0,0);
+
                ClientActiveObject *selected_object = NULL;
 
                PointedThing pointed = getPointedThing(
@@ -3070,8 +3102,6 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                        fog_range = 100000*BS;
                else {
                        fog_range = draw_control.wanted_range*BS + 0.0*MAP_BLOCKSIZE*BS;
-                       if(use_weather)
-                               fog_range *= (1.5 - 1.4*(float)client.getEnv().getClientMap().getHumidity(pos_i)/100);
                        fog_range = MYMIN(fog_range, (draw_control.farthest_drawn+20)*BS);
                        fog_range *= 0.9;
                }
@@ -3221,9 +3251,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                <<", "<<(player_position.Y/BS)
                                <<", "<<(player_position.Z/BS)
                                <<") (yaw="<<(wrapDegrees_0_360(camera_yaw))
-                               <<") (t="<<client.getEnv().getClientMap().getHeat(pos_i)
-                               <<"C, h="<<client.getEnv().getClientMap().getHumidity(pos_i)
-                               <<"%) (seed = "<<((u64)client.getMapSeed())
+                               <<") (seed = "<<((u64)client.getMapSeed())
                                <<")";
                        guitext2->setText(narrow_to_wide(os.str()).c_str());
                        guitext2->setVisible(true);
@@ -3484,7 +3512,9 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                /*
                        Wielded tool
                */
-               if(show_hud && (player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE))
+               if(show_hud &&
+                       (player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE) &&
+                       current_camera_mode < CAMERA_MODE_THIRD)
                {
                        // Warning: This clears the Z buffer.
                        camera.drawWieldedTool();