]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/hud.cpp
(Re)spawn players within 'mapgen_limit'
[dragonfireclient.git] / src / hud.cpp
index d72141899afd63b391d7e124eb0228fdaad00684..a2f031b4c4a7ca0974f820c674587495c7a05912 100644 (file)
@@ -23,8 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "settings.h"
 #include "util/numeric.h"
 #include "log.h"
-#include "gamedef.h"
-#include "itemdef.h"
+#include "client.h"
 #include "inventory.h"
 #include "client/tile.h"
 #include "localplayer.h"
@@ -33,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "fontengine.h"
 #include "guiscalingfilter.h"
 #include "mesh.h"
+#include "wieldmesh.h"
 #include <IGUIStaticText.h>
 
 #ifdef HAVE_TOUCHSCREENGUI
@@ -40,13 +40,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #endif
 
 Hud::Hud(video::IVideoDriver *driver, scene::ISceneManager* smgr,
-               gui::IGUIEnvironment* guienv, IGameDef *gamedef, LocalPlayer *player,
+               gui::IGUIEnvironment* guienv, Client *client, LocalPlayer *player,
                Inventory *inventory)
 {
        this->driver      = driver;
        this->smgr        = smgr;
        this->guienv      = guienv;
-       this->gamedef     = gamedef;
+       this->client      = client;
        this->player      = player;
        this->inventory   = inventory;
 
@@ -60,7 +60,7 @@ Hud::Hud(video::IVideoDriver *driver, scene::ISceneManager* smgr,
        for (unsigned int i = 0; i < 4; i++)
                hbar_colors[i] = video::SColor(255, 255, 255, 255);
 
-       tsrc = gamedef->getTextureSource();
+       tsrc = client->getTextureSource();
 
        v3f crosshair_color = g_settings->getV3F("crosshair_color");
        u32 cross_r = rangelim(myround(crosshair_color.X), 0, 255);
@@ -87,24 +87,31 @@ Hud::Hud(video::IVideoDriver *driver, scene::ISceneManager* smgr,
        m_halo_boxes.clear();
 
        m_selection_pos = v3f(0.0, 0.0, 0.0);
-       std::string mode = g_settings->get("node_highlighting");
+       std::string mode_setting = g_settings->get("node_highlighting");
+
+       if (mode_setting == "halo") {
+               m_mode = HIGHLIGHT_HALO;
+       } else if (mode_setting == "none") {
+               m_mode = HIGHLIGHT_NONE;
+       } else {
+               m_mode = HIGHLIGHT_BOX;
+       }
+
        m_selection_material.Lighting = false;
 
        if (g_settings->getBool("enable_shaders")) {
-               IShaderSource *shdrsrc = gamedef->getShaderSource();
+               IShaderSource *shdrsrc = client->getShaderSource();
                u16 shader_id = shdrsrc->getShader(
-                       mode == "halo" ? "selection_shader" : "default_shader", 1, 1);
+                       m_mode == HIGHLIGHT_HALO ? "selection_shader" : "default_shader", 1, 1);
                m_selection_material.MaterialType = shdrsrc->getShaderInfo(shader_id).material;
        } else {
                m_selection_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
        }
 
-       if (mode == "box") {
-               m_use_selection_mesh = false;
+       if (m_mode == HIGHLIGHT_BOX) {
                m_selection_material.Thickness =
                        rangelim(g_settings->getS16("selectionbox_width"), 1, 5);
-       } else if (mode == "halo") {
-               m_use_selection_mesh = true;
+       } else if (m_mode == HIGHLIGHT_HALO) {
                m_selection_material.setTexture(0, tsrc->getTextureForMesh("halo.png"));
                m_selection_material.setFlag(video::EMF_BACK_FACE_CULLING, true);
        } else {
@@ -192,7 +199,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect,
                if (!use_hotbar_image)
                        driver->draw2DRectangle(bgcolor2, rect, NULL);
                drawItemStack(driver, g_fontengine->getFont(), item, rect, NULL,
-                       gamedef, selected ? IT_ROT_SELECTED : IT_ROT_NONE);
+                       client, selected ? IT_ROT_SELECTED : IT_ROT_NONE);
        }
 
 //NOTE: selectitem = 0 -> no selected; selectitem 1-based
@@ -208,14 +215,18 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
        s32 width   = (itemcount - inv_offset) * (m_hotbar_imagesize + m_padding * 2);
 
        if (direction == HUD_DIR_TOP_BOTTOM || direction == HUD_DIR_BOTTOM_TOP) {
-               width  = m_hotbar_imagesize + m_padding * 2;
-               height = (itemcount - inv_offset) * (m_hotbar_imagesize + m_padding * 2);
+               s32 tmp = height;
+               height = width;
+               width = tmp;
        }
 
        // Position of upper left corner of bar
-       v2s32 pos = upperleftpos + screen_offset;
-       pos *= m_hud_scaling * porting::getDisplayDensity();
+       v2s32 pos = screen_offset;
+       pos.X *= m_hud_scaling * porting::getDisplayDensity();
+       pos.Y *= m_hud_scaling * porting::getDisplayDensity();
+       pos += upperleftpos;
 
+       // Store hotbar_image in member variable, used by drawItem()
        if (hotbar_image != player->hotbar_image) {
                hotbar_image = player->hotbar_image;
                if (hotbar_image != "")
@@ -224,6 +235,7 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
                        use_hotbar_image = false;
        }
 
+       // Store hotbar_selected_image in member variable, used by drawItem()
        if (hotbar_selected_image != player->hotbar_selected_image) {
                hotbar_selected_image = player->hotbar_selected_image;
                if (hotbar_selected_image != "")
@@ -232,7 +244,7 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
                        use_hotbar_selected_image = false;
        }
 
-       /* draw customized item background */
+       // draw customized item background
        if (use_hotbar_image) {
                core::rect<s32> imgrect2(-m_padding/2, -m_padding/2,
                        width+m_padding/2, height+m_padding/2);
@@ -244,12 +256,12 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
                        NULL, hbar_colors, true);
        }
 
+       // Draw items
+       core::rect<s32> imgrect(0, 0, m_hotbar_imagesize, m_hotbar_imagesize);
        for (s32 i = inv_offset; i < itemcount && (size_t)i < mainlist->getSize(); i++) {
-               v2s32 steppos;
                s32 fullimglen = m_hotbar_imagesize + m_padding * 2;
 
-               core::rect<s32> imgrect(0, 0, m_hotbar_imagesize, m_hotbar_imagesize);
-
+               v2s32 steppos;
                switch (direction) {
                case HUD_DIR_RIGHT_LEFT:
                        steppos = v2s32(-(m_padding + (i - inv_offset) * fullimglen), m_padding);
@@ -314,7 +326,7 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
                                                                                 (e->number >> 8)  & 0xFF,
                                                                                 (e->number >> 0)  & 0xFF);
                                core::rect<s32> size(0, 0, e->scale.X, text_height * e->scale.Y);
-                               std::wstring text = utf8_to_wide(e->text);
+                               std::wstring text = unescape_enriched(utf8_to_wide(e->text));
                                core::dimension2d<u32> textsize = font->getDimension(text.c_str());
                                v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2),
                                             (e->align.Y - 1.0) * (textsize.Height / 2));
@@ -350,11 +362,11 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
                                                                                 (e->number >> 8)  & 0xFF,
                                                                                 (e->number >> 0)  & 0xFF);
                                core::rect<s32> size(0, 0, 200, 2 * text_height);
-                               std::wstring text = utf8_to_wide(e->name);
+                               std::wstring text = unescape_enriched(utf8_to_wide(e->name));
                                font->draw(text.c_str(), size + pos, color);
                                std::ostringstream os;
                                os << distance << e->text;
-                               text = utf8_to_wide(os.str());
+                               text = unescape_enriched(utf8_to_wide(os.str()));
                                pos.Y += text_height;
                                font->draw(text.c_str(), size + pos, color);
                                break; }
@@ -513,7 +525,7 @@ void Hud::setSelectionPos(const v3f &pos, const v3s16 &camera_offset)
 
 void Hud::drawSelectionMesh()
 {
-       if (!m_use_selection_mesh) {
+       if (m_mode == HIGHLIGHT_BOX) {
                // Draw 3D selection boxes
                video::SMaterial oldmaterial = driver->getMaterial2D();
                driver->setMaterial(m_selection_material);
@@ -533,11 +545,17 @@ void Hud::drawSelectionMesh()
                        driver->draw3DBox(box, video::SColor(255, r, g, b));
                }
                driver->setMaterial(oldmaterial);
-       } else if (m_selection_mesh) {
+       } else if (m_mode == HIGHLIGHT_HALO && m_selection_mesh) {
                // Draw selection mesh
                video::SMaterial oldmaterial = driver->getMaterial2D();
                driver->setMaterial(m_selection_material);
                setMeshColor(m_selection_mesh, m_selection_mesh_color);
+               video::SColor face_color(0,
+                       MYMIN(255, m_selection_mesh_color.getRed() * 1.5),
+                       MYMIN(255, m_selection_mesh_color.getGreen() * 1.5),
+                       MYMIN(255, m_selection_mesh_color.getBlue() * 1.5));
+               setMeshColorByNormal(m_selection_mesh, m_selected_face_normal,
+                       face_color);
                scene::IMesh* mesh = cloneMesh(m_selection_mesh);
                translateMesh(mesh, m_selection_pos_with_offset);
                u32 mc = m_selection_mesh->getMeshBufferCount();
@@ -553,7 +571,7 @@ void Hud::drawSelectionMesh()
 void Hud::updateSelectionMesh(const v3s16 &camera_offset)
 {
        m_camera_offset = camera_offset;
-       if (!m_use_selection_mesh)
+       if (m_mode != HIGHLIGHT_HALO)
                return;
 
        if (m_selection_mesh) {
@@ -608,7 +626,7 @@ void Hud::resizeHotbar() {
 }
 
 struct MeshTimeInfo {
-       s32 time;
+       u64 time;
        scene::IMesh *mesh;
 };
 
@@ -617,7 +635,7 @@ void drawItemStack(video::IVideoDriver *driver,
                const ItemStack &item,
                const core::rect<s32> &rect,
                const core::rect<s32> *clip,
-               IGameDef *gamedef,
+               Client *client,
                ItemRotationKind rotation_kind)
 {
        static MeshTimeInfo rotation_time_infos[IT_ROT_NONE];
@@ -631,19 +649,20 @@ void drawItemStack(video::IVideoDriver *driver,
                return;
        }
 
-       const ItemDefinition &def = item.getDefinition(gamedef->idef());
-       scene::IMesh* mesh = gamedef->idef()->getWieldMesh(def.name, gamedef);
+       const ItemDefinition &def = item.getDefinition(client->idef());
+       ItemMesh *imesh = client->idef()->getWieldMesh(def.name, client);
 
-       if (mesh) {
+       if (imesh && imesh->mesh) {
+               scene::IMesh *mesh = imesh->mesh;
                driver->clearZBuffer();
                s32 delta = 0;
                if (rotation_kind < IT_ROT_NONE) {
                        MeshTimeInfo &ti = rotation_time_infos[rotation_kind];
                        if (mesh != ti.mesh) {
                                ti.mesh = mesh;
-                               ti.time = getTimeMs();
+                               ti.time = porting::getTimeMs();
                        } else {
-                               delta = porting::getDeltaMs(ti.time, getTimeMs()) % 100000;
+                               delta = porting::getDeltaMs(ti.time, porting::getTimeMs()) % 100000;
                        }
                }
                core::rect<s32> oldViewPort = driver->getViewPort();
@@ -657,16 +676,32 @@ void drawItemStack(video::IVideoDriver *driver,
                matrix.makeIdentity();
 
                if (enable_animations) {
-                       float timer_f = (float)delta / 5000.0;
+                       float timer_f = (float) delta / 5000.0;
                        matrix.setRotationDegrees(core::vector3df(0, 360 * timer_f, 0));
                }
 
                driver->setTransform(video::ETS_WORLD, matrix);
                driver->setViewPort(rect);
 
+               video::SColor basecolor =
+                       client->idef()->getItemstackColor(item, client);
+
                u32 mc = mesh->getMeshBufferCount();
                for (u32 j = 0; j < mc; ++j) {
                        scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
+                       // we can modify vertices relatively fast,
+                       // because these meshes are not buffered.
+                       assert(buf->getHardwareMappingHint_Vertex() == scene::EHM_NEVER);
+                       video::SColor c = basecolor;
+                       if (imesh->buffer_colors.size() > j) {
+                               ItemPartColor *p = &imesh->buffer_colors[j];
+                               if (p->override_base)
+                                       c = p->color;
+                       }
+                       if (imesh->needs_shading)
+                               colorizeMeshBuffer(buf, &c);
+                       else
+                               setMeshBufferColor(buf, c);
                        video::SMaterial &material = buf->getMaterial();
                        material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
                        material.Lighting = false;