Small cleanup of hud add/remove code
authorsapier <Sapier at GMX dot net>
Sun, 25 May 2014 12:34:32 +0000 (14:34 +0200)
committersapier <Sapier at GMX dot net>
Sat, 31 May 2014 20:32:44 +0000 (22:32 +0200)
src/game.cpp
src/hud.cpp
src/player.cpp
src/player.h
src/script/lua_api/l_object.cpp
src/server.cpp

index ef0d3a556f40b2893ae1ef3f8966054566e2d2cf..a3f73465b9d2e96d681acd16bdd6163440b4b0a4 100644 (file)
@@ -2333,16 +2333,13 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
 
                {
                        // Read client events
-                       for(;;)
-                       {
+                       for(;;) {
                                ClientEvent event = client.getClientEvent();
-                               if(event.type == CE_NONE)
-                               {
+                               if(event.type == CE_NONE) {
                                        break;
                                }
                                else if(event.type == CE_PLAYER_DAMAGE &&
-                                               client.getHP() != 0)
-                               {
+                                               client.getHP() != 0) {
                                        //u16 damage = event.player_damage.amount;
                                        //infostream<<"Player damage: "<<damage<<std::endl;
 
@@ -2356,13 +2353,11 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                        MtEvent *e = new SimpleTriggerEvent("PlayerDamage");
                                        gamedef->event()->put(e);
                                }
-                               else if(event.type == CE_PLAYER_FORCE_MOVE)
-                               {
+                               else if(event.type == CE_PLAYER_FORCE_MOVE) {
                                        camera_yaw = event.player_force_move.yaw;
                                        camera_pitch = event.player_force_move.pitch;
                                }
-                               else if(event.type == CE_DEATHSCREEN)
-                               {
+                               else if(event.type == CE_DEATHSCREEN) {
                                        show_deathscreen(&current_formspec, &client, gamedef, tsrc,
                                                        device, &client);
 
@@ -2376,8 +2371,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                        player->hurt_tilt_strength = 0;
 
                                }
-                               else if (event.type == CE_SHOW_FORMSPEC)
-                               {
+                               else if (event.type == CE_SHOW_FORMSPEC) {
                                        FormspecFormSource* fs_src =
                                                        new FormspecFormSource(*(event.show_formspec.formspec));
                                        TextDestPlayerInventory* txt_dst =
@@ -2389,8 +2383,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                        delete(event.show_formspec.formspec);
                                        delete(event.show_formspec.formname);
                                }
-                               else if(event.type == CE_SPAWN_PARTICLE)
-                               {
+                               else if(event.type == CE_SPAWN_PARTICLE) {
                                        LocalPlayer* player = client.getEnv().getLocalPlayer();
                                        video::ITexture *texture =
                                                gamedef->tsrc()->getTexture(*(event.spawn_particle.texture));
@@ -2407,8 +2400,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                                 v2f(0.0, 0.0),
                                                 v2f(1.0, 1.0));
                                }
-                               else if(event.type == CE_ADD_PARTICLESPAWNER)
-                               {
+                               else if(event.type == CE_ADD_PARTICLESPAWNER) {
                                        LocalPlayer* player = client.getEnv().getLocalPlayer();
                                        video::ITexture *texture =
                                                gamedef->tsrc()->getTexture(*(event.add_particlespawner.texture));
@@ -2431,15 +2423,15 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                                 texture,
                                                 event.add_particlespawner.id);
                                }
-                               else if(event.type == CE_DELETE_PARTICLESPAWNER)
-                               {
+                               else if(event.type == CE_DELETE_PARTICLESPAWNER) {
                                        delete_particlespawner (event.delete_particlespawner.id);
                                }
-                               else if (event.type == CE_HUDADD)
-                               {
+                               else if (event.type == CE_HUDADD) {
                                        u32 id = event.hudadd.id;
-                                       size_t nhudelem = player->hud.size();
-                                       if (id > nhudelem || (id < nhudelem && player->hud[id])) {
+
+                                       HudElement *e = player->getHud(id);
+
+                                       if (e != NULL) {
                                                delete event.hudadd.pos;
                                                delete event.hudadd.name;
                                                delete event.hudadd.scale;
@@ -2451,7 +2443,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                                continue;
                                        }
                                        
-                                       HudElement *e = new HudElement;
+                                       e = new HudElement;
                                        e->type   = (HudElementType)event.hudadd.type;
                                        e->pos    = *event.hudadd.pos;
                                        e->name   = *event.hudadd.name;
@@ -2465,10 +2457,9 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                        e->world_pos = *event.hudadd.world_pos;
                                        e->size = *event.hudadd.size;
                                        
-                                       if (id == nhudelem)
-                                               player->hud.push_back(e);
-                                       else
-                                               player->hud[id] = e;
+                                       u32 new_id = player->addHud(e);
+                                       //if this isn't true our huds aren't consistent
+                                       assert(new_id == id);
 
                                        delete event.hudadd.pos;
                                        delete event.hudadd.name;
@@ -2479,18 +2470,17 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                        delete event.hudadd.world_pos;
                                        delete event.hudadd.size;
                                }
-                               else if (event.type == CE_HUDRM)
-                               {
-                                       u32 id = event.hudrm.id;
-                                       if (id < player->hud.size() && player->hud[id]) {
-                                               delete player->hud[id];
-                                               player->hud[id] = NULL;
-                                       }
+                               else if (event.type == CE_HUDRM) {
+                                       HudElement* e = player->removeHud(event.hudrm.id);
+
+                                       if (e != NULL)
+                                               delete (e);
                                }
-                               else if (event.type == CE_HUDCHANGE)
-                               {
+                               else if (event.type == CE_HUDCHANGE) {
                                        u32 id = event.hudchange.id;
-                                       if (id >= player->hud.size() || !player->hud[id]) {
+                                       HudElement* e = player->getHud(id);
+                                       if (e == NULL)
+                                       {
                                                delete event.hudchange.v3fdata;
                                                delete event.hudchange.v2fdata;
                                                delete event.hudchange.sdata;
@@ -2498,7 +2488,6 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                                continue;
                                        }
                                                
-                                       HudElement* e = player->hud[id];
                                        switch (event.hudchange.stat) {
                                                case HUD_STAT_POS:
                                                        e->pos = *event.hudchange.v2fdata;
@@ -2540,19 +2529,18 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                        delete event.hudchange.sdata;
                                        delete event.hudchange.v2s32data;
                                }
-                               else if (event.type == CE_SET_SKY)
-                               {
+                               else if (event.type == CE_SET_SKY) {
                                        sky->setVisible(false);
                                        if(skybox){
                                                skybox->drop();
                                                skybox = NULL;
                                        }
                                        // Handle according to type
-                                       if(*event.set_sky.type == "regular"){
+                                       if(*event.set_sky.type == "regular") {
                                                sky->setVisible(true);
                                        }
                                        else if(*event.set_sky.type == "skybox" &&
-                                                       event.set_sky.params->size() == 6){
+                                                       event.set_sky.params->size() == 6) {
                                                sky->setFallbackBgColor(*event.set_sky.bgcolor);
                                                skybox = smgr->addSkyBoxSceneNode(
                                                                tsrc->getTexture((*event.set_sky.params)[0]),
@@ -2574,8 +2562,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
                                        delete event.set_sky.type;
                                        delete event.set_sky.params;
                                }
-                               else if (event.type == CE_OVERRIDE_DAY_NIGHT_RATIO)
-                               {
+                               else if (event.type == CE_OVERRIDE_DAY_NIGHT_RATIO) {
                                        bool enable = event.override_day_night_ratio.do_override;
                                        u32 value = event.override_day_night_ratio.ratio_f * 1000;
                                        client.getEnv().setDayNightRatioOverride(enable, value);
index 6d0fc8a8998de2bc892e3dab27523b08ceb9cedf..9a11161b483478e8cc4210d62fdd30843e2fa7fe 100644 (file)
@@ -226,8 +226,8 @@ void Hud::drawItems(v2s32 upperleftpos, s32 itemcount, s32 offset,
 
 
 void Hud::drawLuaElements(v3s16 camera_offset) {
-       for (size_t i = 0; i != player->hud.size(); i++) {
-               HudElement *e = player->hud[i];
+       for (size_t i = 0; i != player->maxHudId(); i++) {
+               HudElement *e = player->getHud(i);
                if (!e)
                        continue;
                
index 8ad6ff241ffaa76a7d3c292490009ed7f4efdebc..4dadf26d0b0e2eb2fc986f7830449d339c3a9d0a 100644 (file)
@@ -216,19 +216,18 @@ void Player::deSerialize(std::istream &is, std::string playername)
        setPosition(args.getV3F("position"));
        try{
                hp = args.getS32("hp");
-       }catch(SettingNotFoundException &e){
+       }catch(SettingNotFoundException &e) {
                hp = 20;
        }
        try{
                m_breath = args.getS32("breath");
-       }catch(SettingNotFoundException &e){
+       }catch(SettingNotFoundException &e) {
                m_breath = 11;
        }
 
        inventory.deSerialize(is);
 
-       if(inventory.getList("craftpreview") == NULL)
-       {
+       if(inventory.getList("craftpreview") == NULL) {
                // Convert players without craftpreview
                inventory.addList("craftpreview", 1);
 
@@ -246,14 +245,47 @@ void Player::deSerialize(std::istream &is, std::string playername)
        checkModified();
 }
 
-/*
-       RemotePlayer
-*/
+u32 Player::addHud(HudElement *toadd)
+{
+       u32 id = getFreeHudID();
 
+       if (id < hud.size())
+               hud[id] = toadd;
+       else
+               hud.push_back(toadd);
 
+       return id;
+}
 
+HudElement* Player::getHud(u32 id)
+{
+       if (id < hud.size())
+               return hud[id];
 
+       return NULL;
+}
 
+HudElement* Player::removeHud(u32 id)
+{
+       HudElement* retval = NULL;
+       if (id < hud.size()) {
+               retval = hud[id];
+               hud[id] = NULL;
+       }
+       return retval;
+}
+
+void Player::clearHud()
+{
+       while(!hud.empty()) {
+               delete hud.back();
+               hud.pop_back();
+       }
+}
+
+/*
+       RemotePlayer
+*/
 void RemotePlayer::setPosition(const v3f &position)
 {
        Player::setPosition(position);
index 4c5939d3d66e7b5c1bf8cc306bbd2fed03fd05b8..2b04a310cfc835564a15a6a0834a9bb2546970b4 100644 (file)
@@ -294,10 +294,17 @@ public:
        
        u32 keyPressed;
        
-       std::vector<HudElement *> hud;
+
+       HudElement* getHud(u32 id);
+       u32         addHud(HudElement* hud);
+       HudElement* removeHud(u32 id);
+       void        clearHud();
+       u32         maxHudId() {
+               return hud.size();
+       }
+
        u32 hud_flags;
        s32 hud_hotbar_itemcount;
-
 protected:
        IGameDef *m_gamedef;
 
@@ -314,6 +321,8 @@ protected:
        v3f m_last_pos;
        u16 m_last_hp;
        Inventory m_last_inventory;
+
+       std::vector<HudElement *> hud;
 };
 
 
index 704037437d71401b238141002b002c3ecf547748..a556439dca36c8e2d1e4a035b2f538752d13d848 100644 (file)
@@ -971,7 +971,9 @@ int ObjectRef::l_hud_change(lua_State *L)
                return 0;
 
        u32 id = !lua_isnil(L, 2) ? lua_tonumber(L, 2) : -1;
-       if (id >= player->hud.size())
+
+       HudElement *e = player->getHud(id);
+       if (!e)
                return 0;
 
        HudElementStat stat = HUD_STAT_NUMBER;
@@ -983,10 +985,6 @@ int ObjectRef::l_hud_change(lua_State *L)
        }
 
        void *value = NULL;
-       HudElement *e = player->hud[id];
-       if (!e)
-               return 0;
-
        switch (stat) {
                case HUD_STAT_POS:
                        e->pos = read_v2f(L, 4);
@@ -1049,10 +1047,8 @@ int ObjectRef::l_hud_get(lua_State *L)
                return 0;
 
        u32 id = lua_tonumber(L, -1);
-       if (id >= player->hud.size())
-               return 0;
 
-       HudElement *e = player->hud[id];
+       HudElement *e = player->getHud(id);
        if (!e)
                return 0;
 
index 612192d79ab111d6d0c54ff2444db6b3193dce27..29fb18112004b0533413f4e183f3202f7f670c61 100644 (file)
@@ -4560,24 +4560,25 @@ bool Server::showFormspec(const char *playername, const std::string &formspec, c
 u32 Server::hudAdd(Player *player, HudElement *form) {
        if (!player)
                return -1;
-
-       u32 id = player->getFreeHudID();
-       if (id < player->hud.size())
-               player->hud[id] = form;
-       else
-               player->hud.push_back(form);
        
+       u32 id = player->addHud(form);
+
        SendHUDAdd(player->peer_id, id, form);
+
        return id;
 }
 
 bool Server::hudRemove(Player *player, u32 id) {
-       if (!player || id >= player->hud.size() || !player->hud[id])
+       if (!player)
                return false;
 
-       delete player->hud[id];
-       player->hud[id] = NULL;
+       HudElement* todel = player->removeHud(id);
+
+       if (!todel)
+               return false;
        
+       delete todel;
+
        SendHUDRemove(player->peer_id, id);
        return true;
 }
@@ -5047,7 +5048,7 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id)
                        isSingleplayer());
 
        /* Clean up old HUD elements from previous sessions */
-       player->hud.clear();
+       player->clearHud();
 
        /* Add object to environment */
        m_env->addActiveObject(playersao);