]> git.lizzy.rs Git - minetest.git/blobdiff - src/game.cpp
Warnings fix
[minetest.git] / src / game.cpp
index 22bd8c4292fbb02f55d8d9b715500c6d5cb7fd74..0c7d15d0cde86f8a50e9333fd94d4f04ea68f095 100644 (file)
@@ -208,33 +208,6 @@ class PlayerInventoryFormSource: public IFormSource
        Client *m_client;
 };
 
-class FormspecFormSource: public IFormSource
-{
-public:
-       FormspecFormSource(std::string formspec,FormspecFormSource** game_formspec)
-       {
-               m_formspec = formspec;
-               m_game_formspec = game_formspec;
-       }
-
-       ~FormspecFormSource()
-       {
-               *m_game_formspec = 0;
-       }
-
-       void setForm(std::string formspec) {
-               m_formspec = formspec;
-       }
-
-       std::string getForm()
-       {
-               return m_formspec;
-       }
-
-       std::string m_formspec;
-       FormspecFormSource** m_game_formspec;
-};
-
 /*
        Check if a node is pointable
 */
@@ -729,6 +702,18 @@ class SoundMaker
                sm->m_sound->playSound(sm->m_ndef->get(nde->n).sound_dug, false);
        }
 
+       static void playerDamage(MtEvent *e, void *data)
+       {
+               SoundMaker *sm = (SoundMaker*)data;
+               sm->m_sound->playSound(SimpleSoundSpec("player_damage", 0.5), false);
+       }
+
+       static void playerFallingDamage(MtEvent *e, void *data)
+       {
+               SoundMaker *sm = (SoundMaker*)data;
+               sm->m_sound->playSound(SimpleSoundSpec("player_falling_damage", 0.5), false);
+       }
+
        void registerReceiver(MtEventManager *mgr)
        {
                mgr->reg("ViewBobbingStep", SoundMaker::viewBobbingStep, this);
@@ -737,6 +722,8 @@ class SoundMaker
                mgr->reg("CameraPunchLeft", SoundMaker::cameraPunchLeft, this);
                mgr->reg("CameraPunchRight", SoundMaker::cameraPunchRight, this);
                mgr->reg("NodeDug", SoundMaker::nodeDug, this);
+               mgr->reg("PlayerDamage", SoundMaker::playerDamage, this);
+               mgr->reg("PlayerFallingDamage", SoundMaker::playerFallingDamage, this);
        }
 
        void step(float dtime)
@@ -817,10 +804,14 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
                u32 daynight_ratio = m_client->getEnv().getDayNightRatio();
                float daynight_ratio_f = (float)daynight_ratio / 1000.0;
                services->setPixelShaderConstant("dayNightRatio", &daynight_ratio_f, 1);
+               
+               // Normal map texture layer
+               int layer = 1;
+               services->setPixelShaderConstant("normalTexture" , (irr::f32*)&layer, 1);
        }
 };
 
-void nodePlacementPrediction(Client &client,
+bool nodePlacementPrediction(Client &client,
                const ItemDefinition &playeritem_def,
                v3s16 nodepos, v3s16 neighbourpos)
 {
@@ -840,7 +831,7 @@ void nodePlacementPrediction(Client &client,
                        if(nodedef->get(n_under).buildable_to)
                                p = nodepos;
                        else if (!nodedef->get(map.getNode(p)).buildable_to)
-                               return;
+                               return false;
                }catch(InvalidPositionException &e){}
                // Find id of predicted node
                content_t id;
@@ -850,7 +841,7 @@ void nodePlacementPrediction(Client &client,
                                        <<playeritem_def.name<<" (places "
                                        <<prediction
                                        <<") - Name not known"<<std::endl;
-                       return;
+                       return false;
                }
                // Predict param2 for facedir and wallmounted nodes
                u8 param2 = 0;
@@ -889,13 +880,14 @@ void nodePlacementPrediction(Client &client,
                        else
                                pp = p + v3s16(0,-1,0);
                        if(!nodedef->get(map.getNode(pp)).walkable)
-                               return;
+                               return false;
                }
                // Add node to client map
                MapNode n(id, 0, param2);
                try{
                        // This triggers the required mesh update too
                        client.addNode(p, n);
+                       return true;
                }catch(InvalidPositionException &e){
                        errorstream<<"Node placement prediction failed for "
                                        <<playeritem_def.name<<" (places "
@@ -903,6 +895,7 @@ void nodePlacementPrediction(Client &client,
                                        <<") - Position not loaded"<<std::endl;
                }
        }
+       return false;
 }
 
 
@@ -1023,12 +1016,6 @@ void the_game(
        infostream<<"Creating client"<<std::endl;
        
        MapDrawControl draw_control;
-
-       Client client(device, playername.c_str(), password, draw_control,
-                       tsrc, shsrc, itemdef, nodedef, sound, &eventmgr);
-       
-       // Client acts as our GameDef
-       IGameDef *gamedef = &client;
        
        {
                wchar_t* text = wgettext("Resolving address...");
@@ -1038,18 +1025,39 @@ void the_game(
        Address connect_address(0,0,0,0, port);
        try{
                if(address == "")
+               {
                        //connect_address.Resolve("localhost");
-                       connect_address.setAddress(127,0,0,1);
+                       if(g_settings->getBool("enable_ipv6") && g_settings->getBool("ipv6_server"))
+                       {
+                               IPv6AddressBytes addr_bytes;
+                               addr_bytes.bytes[15] = 1;
+                               connect_address.setAddress(&addr_bytes);
+                       }
+                       else
+                       {
+                               connect_address.setAddress(127,0,0,1);
+                       }
+               }
                else
                        connect_address.Resolve(address.c_str());
        }
        catch(ResolveError &e)
        {
-               error_message = L"Couldn't resolve address";
+               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;
        }
+       
+       /*
+               Create client
+       */
+       Client client(device, playername.c_str(), password, draw_control,
+               tsrc, shsrc, itemdef, nodedef, sound, &eventmgr,
+               connect_address.isIPv6());
+       
+       // Client acts as our GameDef
+       IGameDef *gamedef = &client;
 
        /*
                Attempt to connect to the server
@@ -1201,26 +1209,28 @@ void the_game(
                        }
                        
                        // Display status
-                       std::ostringstream ss;
                        int progress=0;
                        if (!client.itemdefReceived())
                        {
-                               ss << "Item definitions...";
+                               wchar_t* text = wgettext("Item definitions...");
                                progress = 0;
+                               draw_load_screen(text, device, font, dtime, progress);
+                               delete[] text;
                        }
                        else if (!client.nodedefReceived())
                        {
-                               ss << "Node definitions...";
+                               wchar_t* text = wgettext("Node definitions...");
                                progress = 25;
+                               draw_load_screen(text, device, font, dtime, progress);
+                               delete[] text;
                        }
                        else
                        {
-                               ss << "Media...";
+                               wchar_t* text = wgettext("Media...");
                                progress = 50+client.mediaReceiveProgress()*50+0.5;
+                               draw_load_screen(text, device, font, dtime, progress);
+                               delete[] text;
                        }
-                       wchar_t* text = wgettext(ss.str().c_str());
-                       draw_load_screen(text, device, font, dtime, progress);
-                       delete[] text;
                        
                        // On some computers framerate doesn't seem to be
                        // automatically limited
@@ -1312,7 +1322,7 @@ void the_game(
        */
        int crack_animation_length = 5;
        {
-               video::ITexture *t = tsrc->getTextureRaw("crack_anylength.png");
+               video::ITexture *t = tsrc->getTexture("crack_anylength.png");
                v2u32 size = t->getOriginalSize();
                crack_animation_length = size.Y / size.X;
        }
@@ -1366,6 +1376,7 @@ void the_game(
                        false, false);
        guitext_profiler->setBackgroundColor(video::SColor(120,0,0,0));
        guitext_profiler->setVisible(false);
+       guitext_profiler->setWordWrap(true);
        
        /*
                Some statistics are collected in these
@@ -1684,6 +1695,10 @@ void the_game(
                {
                        input->clear();
                }
+               if (!guienv->hasFocus(gui_chat_console) && gui_chat_console->isOpen())
+               {
+                       gui_chat_console->closeConsoleAtOnce();
+               }
 
                // Input handler step() (used by the random input generator)
                input->step(dtime);
@@ -2198,6 +2213,9 @@ void the_game(
                                        player->hurt_tilt_timer = 1.5;
                                        player->hurt_tilt_strength = event.player_damage.amount/2;
                                        player->hurt_tilt_strength = rangelim(player->hurt_tilt_strength, 2.0, 10.0);
+
+                                       MtEvent *e = new SimpleTriggerEvent("PlayerDamage");
+                                       gamedef->event()->put(e);
                                }
                                else if(event.type == CE_PLAYER_FORCE_MOVE)
                                {
@@ -2271,7 +2289,7 @@ void the_game(
                                else if(event.type == CE_SPAWN_PARTICLE)
                                {
                                        LocalPlayer* player = client.getEnv().getLocalPlayer();
-                                       AtlasPointer ap =
+                                       video::ITexture *texture =
                                                gamedef->tsrc()->getTexture(*(event.spawn_particle.texture));
 
                                        new Particle(gamedef, smgr, player, client.getEnv(),
@@ -2280,12 +2298,15 @@ void the_game(
                                                *event.spawn_particle.acc,
                                                 event.spawn_particle.expirationtime,
                                                 event.spawn_particle.size,
-                                                event.spawn_particle.collisiondetection, ap);
+                                                event.spawn_particle.collisiondetection,
+                                                texture,
+                                                v2f(0.0, 0.0),
+                                                v2f(1.0, 1.0));
                                }
                                else if(event.type == CE_ADD_PARTICLESPAWNER)
                                {
                                        LocalPlayer* player = client.getEnv().getLocalPlayer();
-                                       AtlasPointer ap =
+                                       video::ITexture *texture =
                                                gamedef->tsrc()->getTexture(*(event.add_particlespawner.texture));
 
                                        new ParticleSpawner(gamedef, smgr, player,
@@ -2302,7 +2323,7 @@ void the_game(
                                                 event.add_particlespawner.minsize,
                                                 event.add_particlespawner.maxsize,
                                                 event.add_particlespawner.collisiondetection,
-                                                ap,
+                                                texture,
                                                 event.add_particlespawner.id);
                                }
                                else if(event.type == CE_DELETE_PARTICLESPAWNER)
@@ -2434,6 +2455,7 @@ void the_game(
                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();
@@ -2467,7 +2489,12 @@ void the_game(
                
                //u32 t1 = device->getTimer()->getRealTime();
                
-               f32 d = 4; // max. distance
+               f32 d = playeritem_def.range; // max. distance
+               f32 d_hand = itemdef->get("").range;
+               if(d < 0 && d_hand >= 0)
+                       d = d_hand;
+               else if(d < 0)
+                       d = 4.0;
                core::line3d<f32> shootline(camera_position,
                                camera_position + camera_direction * BS * (d+1));
 
@@ -2567,7 +2594,8 @@ void the_game(
                                Handle digging
                        */
                        
-                       if(nodig_delay_timer <= 0.0 && input->getLeftState())
+                       if(nodig_delay_timer <= 0.0 && input->getLeftState()
+                                       && client.checkPrivilege("interact"))
                        {
                                if(!digging)
                                {
@@ -2590,20 +2618,6 @@ void the_game(
                                        if(tp)
                                                params = getDigParams(nodedef->get(n).groups, tp);
                                }
-                               
-                               SimpleSoundSpec sound_dig = nodedef->get(n).sound_dig;
-                               if(sound_dig.exists()){
-                                       if(sound_dig.name == "__group"){
-                                               if(params.main_group != ""){
-                                                       soundmaker.m_player_leftpunch_sound.gain = 0.5;
-                                                       soundmaker.m_player_leftpunch_sound.name =
-                                                                       std::string("default_dig_") +
-                                                                                       params.main_group;
-                                               }
-                                       } else{
-                                               soundmaker.m_player_leftpunch_sound = sound_dig;
-                                       }
-                               }
 
                                float dig_time_complete = 0.0;
 
@@ -2636,6 +2650,20 @@ void the_game(
                                        dig_index = crack_animation_length;
                                }
 
+                               SimpleSoundSpec sound_dig = nodedef->get(n).sound_dig;
+                               if(sound_dig.exists() && params.diggable){
+                                       if(sound_dig.name == "__group"){
+                                               if(params.main_group != ""){
+                                                       soundmaker.m_player_leftpunch_sound.gain = 0.5;
+                                                       soundmaker.m_player_leftpunch_sound.name =
+                                                                       std::string("default_dig_") +
+                                                                                       params.main_group;
+                                               }
+                                       } else{
+                                               soundmaker.m_player_leftpunch_sound = sound_dig;
+                                       }
+                               }
+
                                // Don't show cracks if not diggable
                                if(dig_time_complete >= 100000.0)
                                {
@@ -2684,13 +2712,20 @@ void the_game(
                                        gamedef->event()->put(e);
                                }
 
-                               dig_time += dtime;
+                               if(dig_time_complete < 100000.0)
+                                       dig_time += dtime;
+                               else {
+                                       dig_time = 0;
+                                       client.setCrack(-1, nodepos);
+                               }
 
                                camera.setDigging(0);  // left click animation
                        }
 
-                       if(input->getRightClicked() ||
-                                       repeat_rightclick_timer >= g_settings->getFloat("repeat_rightclick_time"))
+                       if((input->getRightClicked() ||
+                                       repeat_rightclick_timer >=
+                                               g_settings->getFloat("repeat_rightclick_time")) &&
+                                       client.checkPrivilege("interact"))
                        {
                                repeat_rightclick_timer = 0;
                                infostream<<"Ground right-clicked"<<std::endl;
@@ -2744,13 +2779,17 @@ void the_game(
                                        
                                        // If the wielded item has node placement prediction,
                                        // make that happen
-                                       nodePlacementPrediction(client,
+                                       bool placed = nodePlacementPrediction(client,
                                                        playeritem_def,
                                                        nodepos, neighbourpos);
                                        
                                        // Read the sound
-                                       soundmaker.m_player_rightpunch_sound =
-                                                       playeritem_def.sound_place;
+                                       if(placed)
+                                               soundmaker.m_player_rightpunch_sound =
+                                                               playeritem_def.sound_place;
+                                       else
+                                               soundmaker.m_player_rightpunch_sound =
+                                                               SimpleSoundSpec();
                                }
                        }
                }
@@ -2996,7 +3035,9 @@ void the_game(
                                <<", "<<(player_position.Y/BS)
                                <<", "<<(player_position.Z/BS)
                                <<") (yaw="<<(wrapDegrees_0_360(camera_yaw))
-                               <<") (seed = "<<((unsigned long long)client.getMapSeed())
+                               <<") (t="<<client.getEnv().getClientMap().getHeat(pos_i)
+                               <<"C, h="<<client.getEnv().getClientMap().getHumidity(pos_i)
+                               <<"%) (seed = "<<((unsigned long long)client.getMapSeed())
                                <<")";
                        guitext2->setText(narrow_to_wide(os.str()).c_str());
                        guitext2->setVisible(true);
@@ -3283,7 +3324,7 @@ void the_game(
                if (show_hud)
                {
                        hud.drawHotbar(v2s32(displaycenter.X, screensize.Y),
-                                       client.getHP(), client.getPlayerItem());
+                                       client.getHP(), client.getPlayerItem(), client.getBreath());
                }
 
                /*