aboutsummaryrefslogtreecommitdiff
path: root/games
diff options
context:
space:
mode:
Diffstat (limited to 'games')
-rw-r--r--games/devtest/mods/basetools/init.lua105
-rw-r--r--games/devtest/mods/basetools/textures/basetools_bloodsword.pngbin0 -> 225 bytes
-rw-r--r--games/devtest/mods/basetools/textures/basetools_elementalsword.pngbin0 -> 252 bytes
-rw-r--r--games/devtest/mods/basetools/textures/basetools_healdagger.pngbin0 -> 262 bytes
-rw-r--r--games/devtest/mods/basetools/textures/basetools_healsword.pngbin0 -> 229 bytes
-rw-r--r--games/devtest/mods/basetools/textures/basetools_mesesword.pngbin0 -> 225 bytes
-rw-r--r--games/devtest/mods/basetools/textures/basetools_superhealsword.pngbin0 -> 272 bytes
-rw-r--r--games/devtest/mods/basetools/textures/basetools_titaniumsword.pngbin0 -> 225 bytes
-rw-r--r--games/devtest/mods/basetools/textures/basetools_usespick.pngbin0 -> 230 bytes
-rw-r--r--games/devtest/mods/basetools/textures/basetools_usessword.pngbin0 -> 248 bytes
-rw-r--r--games/devtest/mods/basetools/textures/basetools_wooddagger.pngbin0 -> 248 bytes
-rw-r--r--games/devtest/mods/testentities/armor.lua30
-rw-r--r--games/devtest/mods/testentities/textures/testentities_armorball.pngbin561 -> 1301 bytes
-rw-r--r--games/devtest/mods/unittests/itemdescription.lua28
-rw-r--r--games/devtest/mods/unittests/textures/unittests_description_test.pngbin0 -> 268 bytes
15 files changed, 129 insertions, 34 deletions
diff --git a/games/devtest/mods/basetools/init.lua b/games/devtest/mods/basetools/init.lua
index fd83b82eb..3ec69d39f 100644
--- a/games/devtest/mods/basetools/init.lua
+++ b/games/devtest/mods/basetools/init.lua
@@ -279,50 +279,135 @@ minetest.register_tool("basetools:sword_wood", {
})
minetest.register_tool("basetools:sword_stone", {
description = "Stone Sword".."\n"..
- "Damage: fleshy=4",
+ "Damage: fleshy=5",
inventory_image = "basetools_stonesword.png",
tool_capabilities = {
full_punch_interval = 1.0,
max_drop_level=0,
- damage_groups = {fleshy=4},
+ damage_groups = {fleshy=5},
}
})
minetest.register_tool("basetools:sword_steel", {
description = "Steel Sword".."\n"..
- "Damage: fleshy=6",
+ "Damage: fleshy=10",
inventory_image = "basetools_steelsword.png",
tool_capabilities = {
full_punch_interval = 1.0,
max_drop_level=1,
- damage_groups = {fleshy=6},
+ damage_groups = {fleshy=10},
+ }
+})
+minetest.register_tool("basetools:sword_titanium", {
+ description = "Titanium Sword".."\n"..
+ "Damage: fleshy=100",
+ inventory_image = "basetools_titaniumsword.png",
+ tool_capabilities = {
+ full_punch_interval = 1.0,
+ max_drop_level=1,
+ damage_groups = {fleshy=100},
+ }
+})
+minetest.register_tool("basetools:sword_blood", {
+ description = "Blood Sword".."\n"..
+ "Damage: fleshy=1000",
+ inventory_image = "basetools_bloodsword.png",
+ tool_capabilities = {
+ full_punch_interval = 1.0,
+ max_drop_level=1,
+ damage_groups = {fleshy=1000},
+ }
+})
+
+-- Max. damage sword
+minetest.register_tool("basetools:sword_mese", {
+ description = "Mese Sword".."\n"..
+ "Damage: fleshy=32767, fiery=32767, icy=32767".."\n"..
+ "Full Punch Interval: 0.0s",
+ inventory_image = "basetools_mesesword.png",
+ tool_capabilities = {
+ full_punch_interval = 0.0,
+ max_drop_level=1,
+ damage_groups = {fleshy=32767, fiery=32767, icy=32767},
}
})
-- Fire/Ice sword: Deal damage to non-fleshy damage groups
minetest.register_tool("basetools:sword_fire", {
description = "Fire Sword".."\n"..
- "Damage: icy=6",
+ "Damage: icy=10",
inventory_image = "basetools_firesword.png",
tool_capabilities = {
full_punch_interval = 1.0,
max_drop_level=0,
- damage_groups = {icy=6},
+ damage_groups = {icy=10},
}
})
minetest.register_tool("basetools:sword_ice", {
description = "Ice Sword".."\n"..
- "Damage: fiery=6",
+ "Damage: fiery=10",
inventory_image = "basetools_icesword.png",
tool_capabilities = {
full_punch_interval = 1.0,
max_drop_level=0,
- damage_groups = {fiery=6},
+ damage_groups = {fiery=10},
+ }
+})
+minetest.register_tool("basetools:sword_elemental", {
+ description = "Elemental Sword".."\n"..
+ "Damage: fiery=10, icy=10",
+ inventory_image = "basetools_elementalsword.png",
+ tool_capabilities = {
+ full_punch_interval = 1.0,
+ max_drop_level=0,
+ damage_groups = {fiery=10, icy=10},
}
})
+-- Healing weapons: heal HP
+minetest.register_tool("basetools:dagger_heal", {
+ description = "Healing Dagger".."\n"..
+ "Heal: fleshy=1".."\n"..
+ "Full Punch Interval: 0.5s",
+ inventory_image = "basetools_healdagger.png",
+ tool_capabilities = {
+ full_punch_interval = 0.5,
+ damage_groups = {fleshy=-1},
+ }
+})
+minetest.register_tool("basetools:sword_heal", {
+ description = "Healing Sword".."\n"..
+ "Heal: fleshy=10",
+ inventory_image = "basetools_healsword.png",
+ tool_capabilities = {
+ full_punch_interval = 1.0,
+ damage_groups = {fleshy=-10},
+ }
+})
+minetest.register_tool("basetools:sword_heal_super", {
+ description = "Super Healing Sword".."\n"..
+ "Heal: fleshy=32768, fiery=32768, icy=32768",
+ inventory_image = "basetools_superhealsword.png",
+ tool_capabilities = {
+ full_punch_interval = 1.0,
+ damage_groups = {fleshy=-32768, fiery=-32768, icy=-32768},
+ }
+})
+
+
--
-- Dagger: Low damage, fast punch interval
--
+minetest.register_tool("basetools:dagger_wood", {
+ description = "Wooden Dagger".."\n"..
+ "Damage: fleshy=1".."\n"..
+ "Full Punch Interval: 0.5s",
+ inventory_image = "basetools_wooddagger.png",
+ tool_capabilities = {
+ full_punch_interval = 0.5,
+ max_drop_level=0,
+ damage_groups = {fleshy=1},
+ }
+})
minetest.register_tool("basetools:dagger_steel", {
description = "Steel Dagger".."\n"..
"Damage: fleshy=2".."\n"..
@@ -343,7 +428,7 @@ for i=1, #uses do
minetest.register_tool("basetools:pick_uses_"..string.format("%05d", u), {
description = u.."-Uses Pickaxe".."\n"..
"Digs cracky=3",
- inventory_image = "basetools_steelpick.png^[colorize:"..color..":127",
+ inventory_image = "basetools_usespick.png^[colorize:"..color..":127",
tool_capabilities = {
max_drop_level=0,
groupcaps={
@@ -355,7 +440,7 @@ for i=1, #uses do
minetest.register_tool("basetools:sword_uses_"..string.format("%05d", u), {
description = u.."-Uses Sword".."\n"..
"Damage: fleshy=1",
- inventory_image = "basetools_woodsword.png^[colorize:"..color..":127",
+ inventory_image = "basetools_usessword.png^[colorize:"..color..":127",
tool_capabilities = {
damage_groups = {fleshy=1},
punch_attack_uses = u,
diff --git a/games/devtest/mods/basetools/textures/basetools_bloodsword.png b/games/devtest/mods/basetools/textures/basetools_bloodsword.png
new file mode 100644
index 000000000..047adfef7
--- /dev/null
+++ b/games/devtest/mods/basetools/textures/basetools_bloodsword.png
Binary files differ
diff --git a/games/devtest/mods/basetools/textures/basetools_elementalsword.png b/games/devtest/mods/basetools/textures/basetools_elementalsword.png
new file mode 100644
index 000000000..b72821192
--- /dev/null
+++ b/games/devtest/mods/basetools/textures/basetools_elementalsword.png
Binary files differ
diff --git a/games/devtest/mods/basetools/textures/basetools_healdagger.png b/games/devtest/mods/basetools/textures/basetools_healdagger.png
new file mode 100644
index 000000000..f1ceaeb43
--- /dev/null
+++ b/games/devtest/mods/basetools/textures/basetools_healdagger.png
Binary files differ
diff --git a/games/devtest/mods/basetools/textures/basetools_healsword.png b/games/devtest/mods/basetools/textures/basetools_healsword.png
new file mode 100644
index 000000000..e9d6dafee
--- /dev/null
+++ b/games/devtest/mods/basetools/textures/basetools_healsword.png
Binary files differ
diff --git a/games/devtest/mods/basetools/textures/basetools_mesesword.png b/games/devtest/mods/basetools/textures/basetools_mesesword.png
new file mode 100644
index 000000000..2aecc167c
--- /dev/null
+++ b/games/devtest/mods/basetools/textures/basetools_mesesword.png
Binary files differ
diff --git a/games/devtest/mods/basetools/textures/basetools_superhealsword.png b/games/devtest/mods/basetools/textures/basetools_superhealsword.png
new file mode 100644
index 000000000..ca8bd0df8
--- /dev/null
+++ b/games/devtest/mods/basetools/textures/basetools_superhealsword.png
Binary files differ
diff --git a/games/devtest/mods/basetools/textures/basetools_titaniumsword.png b/games/devtest/mods/basetools/textures/basetools_titaniumsword.png
new file mode 100644
index 000000000..f34ab9f4d
--- /dev/null
+++ b/games/devtest/mods/basetools/textures/basetools_titaniumsword.png
Binary files differ
diff --git a/games/devtest/mods/basetools/textures/basetools_usespick.png b/games/devtest/mods/basetools/textures/basetools_usespick.png
new file mode 100644
index 000000000..5aa3a960c
--- /dev/null
+++ b/games/devtest/mods/basetools/textures/basetools_usespick.png
Binary files differ
diff --git a/games/devtest/mods/basetools/textures/basetools_usessword.png b/games/devtest/mods/basetools/textures/basetools_usessword.png
new file mode 100644
index 000000000..9742b9b81
--- /dev/null
+++ b/games/devtest/mods/basetools/textures/basetools_usessword.png
Binary files differ
diff --git a/games/devtest/mods/basetools/textures/basetools_wooddagger.png b/games/devtest/mods/basetools/textures/basetools_wooddagger.png
new file mode 100644
index 000000000..67e37ee71
--- /dev/null
+++ b/games/devtest/mods/basetools/textures/basetools_wooddagger.png
Binary files differ
diff --git a/games/devtest/mods/testentities/armor.lua b/games/devtest/mods/testentities/armor.lua
index 306953d50..3887c75fd 100644
--- a/games/devtest/mods/testentities/armor.lua
+++ b/games/devtest/mods/testentities/armor.lua
@@ -4,10 +4,19 @@
local phasearmor = {
[0]={icy=100},
[1]={fiery=100},
- [2]={fleshy=100},
- [3]={immortal=1},
- [4]={punch_operable=1},
+ [2]={icy=100, fiery=100},
+ [3]={fleshy=-100},
+ [4]={fleshy=1},
+ [5]={fleshy=10},
+ [6]={fleshy=50},
+ [7]={fleshy=100},
+ [8]={fleshy=200},
+ [9]={fleshy=1000},
+ [10]={fleshy=32767},
+ [11]={immortal=1},
+ [12]={punch_operable=1},
}
+local max_phase = 12
minetest.register_entity("testentities:armorball", {
initial_properties = {
@@ -21,7 +30,7 @@ minetest.register_entity("testentities:armorball", {
initial_sprite_basepos = {x=0, y=0},
},
- _phase = 2,
+ _phase = 7,
on_activate = function(self, staticdata)
minetest.log("action", "[testentities] armorball.on_activate")
@@ -32,10 +41,21 @@ minetest.register_entity("testentities:armorball", {
on_rightclick = function(self, clicker)
-- Change armor group and sprite
self._phase = self._phase + 1
- if self._phase >= 5 then
+ if self._phase >= max_phase + 1 then
self._phase = 0
end
self.object:set_sprite({x=0, y=self._phase})
self.object:set_armor_groups(phasearmor[self._phase])
end,
+
+ on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
+ if not puncher then
+ return
+ end
+ local name = puncher:get_player_name()
+ if not name then
+ return
+ end
+ minetest.chat_send_player(name, "time_from_last_punch="..string.format("%.3f", time_from_last_punch).."; damage="..tostring(damage))
+ end,
})
diff --git a/games/devtest/mods/testentities/textures/testentities_armorball.png b/games/devtest/mods/testentities/textures/testentities_armorball.png
index 88147bd1f..ffe33fbad 100644
--- a/games/devtest/mods/testentities/textures/testentities_armorball.png
+++ b/games/devtest/mods/testentities/textures/testentities_armorball.png
Binary files differ
diff --git a/games/devtest/mods/unittests/itemdescription.lua b/games/devtest/mods/unittests/itemdescription.lua
index dc62de7f0..b4c218c98 100644
--- a/games/devtest/mods/unittests/itemdescription.lua
+++ b/games/devtest/mods/unittests/itemdescription.lua
@@ -1,17 +1,7 @@
-local full_description = "Colorful Pickaxe\nThe best pick."
-minetest.register_tool("unittests:colorful_pick", {
+local full_description = "Description Test Item\nFor testing item decription"
+minetest.register_tool("unittests:description_test", {
description = full_description,
- inventory_image = "basetools_mesepick.png",
- tool_capabilities = {
- full_punch_interval = 1.0,
- max_drop_level=3,
- groupcaps={
- cracky={times={[1]=2.0, [2]=1.0, [3]=0.5}, uses=20, maxlevel=3},
- crumbly={times={[1]=2.0, [2]=1.0, [3]=0.5}, uses=20, maxlevel=3},
- snappy={times={[1]=2.0, [2]=1.0, [3]=0.5}, uses=20, maxlevel=3}
- },
- damage_groups = {fleshy=4},
- },
+ inventory_image = "unittests_description_test.png",
})
minetest.register_chatcommand("item_description", {
@@ -30,18 +20,18 @@ local function test_short_desc()
return ItemStack(item):get_short_description()
end
- local stack = ItemStack("unittests:colorful_pick")
- assert(stack:get_short_description() == "Colorful Pickaxe")
- assert(get_short_description("unittests:colorful_pick") == "Colorful Pickaxe")
- assert(minetest.registered_items["unittests:colorful_pick"].short_description == nil)
+ local stack = ItemStack("unittests:description_test")
+ assert(stack:get_short_description() == "Description Test Item")
+ assert(get_short_description("unittests:description_test") == "Description Test Item")
+ assert(minetest.registered_items["unittests:description_test"].short_description == nil)
assert(stack:get_description() == full_description)
- assert(stack:get_description() == minetest.registered_items["unittests:colorful_pick"].description)
+ assert(stack:get_description() == minetest.registered_items["unittests:description_test"].description)
stack:get_meta():set_string("description", "Hello World")
assert(stack:get_short_description() == "Hello World")
assert(stack:get_description() == "Hello World")
assert(get_short_description(stack) == "Hello World")
- assert(get_short_description("unittests:colorful_pick") == "Colorful Pickaxe")
+ assert(get_short_description("unittests:description_test") == "Description Test Item")
stack:get_meta():set_string("short_description", "Foo Bar")
assert(stack:get_short_description() == "Foo Bar")
diff --git a/games/devtest/mods/unittests/textures/unittests_description_test.png b/games/devtest/mods/unittests/textures/unittests_description_test.png
new file mode 100644
index 000000000..a6be43314
--- /dev/null
+++ b/games/devtest/mods/unittests/textures/unittests_description_test.png
Binary files differ
> 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
/*
Minetest
Copyright (C) 2019 EvicenceBKidscode / Pierre-Yves Rollo <dev@pyrollo.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "guiHyperText.h"
#include "guiScrollBar.h"
#include "client/fontengine.h"
#include "client/tile.h"
#include "IVideoDriver.h"
#include "client/client.h"
#include "client/renderingengine.h"
#include "hud.h"
#include "util/string.h"
#include "irrlicht_changes/CGUITTFont.h"

using namespace irr::gui;

static bool check_color(const std::string &str)
{
	irr::video::SColor color;
	return parseColorString(str, color, false);
}

static bool check_integer(const std::string &str)
{
	if (str.empty())
		return false;

	char *endptr = nullptr;
	strtol(str.c_str(), &endptr, 10);

	return *endptr == '\0';
}

// -----------------------------------------------------------------------------
// ParsedText - A text parser

void ParsedText::Element::setStyle(StyleList &style)
{
	this->underline = is_yes(style["underline"]);

	video::SColor color;

	if (parseColorString(style["color"], color, false))
		this->color = color;
	if (parseColorString(style["hovercolor"], color, false))
		this->hovercolor = color;

	unsigned int font_size = std::atoi(style["fontsize"].c_str());
	FontMode font_mode = FM_Standard;
	if (style["fontstyle"] == "mono")
		font_mode = FM_Mono;

	FontSpec spec(font_size, font_mode,
		is_yes(style["bold"]), is_yes(style["italic"]));

	// TODO: find a way to check font validity
	// Build a new fontengine ?
	this->font = g_fontengine->getFont(spec);

	if (!this->font)
		printf("No font found ! Size=%d, mode=%d, bold=%s, italic=%s\n",
				font_size, font_mode, style["bold"].c_str(),
				style["italic"].c_str());
}

void ParsedText::Paragraph::setStyle(StyleList &style)
{
	if (style["halign"] == "center")
		this->halign = HALIGN_CENTER;
	else if (style["halign"] == "right")
		this->halign = HALIGN_RIGHT;
	else if (style["halign"] == "justify")
		this->halign = HALIGN_JUSTIFY;
	else
		this->halign = HALIGN_LEFT;
}

ParsedText::ParsedText(const wchar_t *text)
{
	// Default style
	m_root_tag.name = "root";
	m_root_tag.style["fontsize"] = "16";
	m_root_tag.style["fontstyle"] = "normal";
	m_root_tag.style["bold"] = "false";
	m_root_tag.style["italic"] = "false";
	m_root_tag.style["underline"] = "false";
	m_root_tag.style["halign"] = "left";
	m_root_tag.style["color"] = "#EEEEEE";
	m_root_tag.style["hovercolor"] = "#FF0000";

	m_active_tags.push_front(&m_root_tag);
	m_style = m_root_tag.style;

	// Default simple tags definitions
	StyleList style;

	style["color"] = "#0000FF";
	style["underline"] = "true";
	m_elementtags["action"] = style;
	style.clear();

	style["bold"] = "true";
	m_elementtags["b"] = style;
	style.clear();

	style["italic"] = "true";
	m_elementtags["i"] = style;
	style.clear();

	style["underline"] = "true";
	m_elementtags["u"] = style;
	style.clear();

	style["fontstyle"] = "mono";
	m_elementtags["mono"] = style;
	style.clear();

	style["fontsize"] = m_root_tag.style["fontsize"];
	m_elementtags["normal"] = style;
	style.clear();

	style["fontsize"] = "24";
	m_elementtags["big"] = style;
	style.clear();

	style["fontsize"] = "36";
	m_elementtags["bigger"] = style;
	style.clear();

	style["halign"] = "center";
	m_paragraphtags["center"] = style;
	style.clear();

	style["halign"] = "justify";
	m_paragraphtags["justify"] = style;
	style.clear();

	style["halign"] = "left";
	m_paragraphtags["left"] = style;
	style.clear();

	style["halign"] = "right";
	m_paragraphtags["right"] = style;
	style.clear();

	m_element = NULL;
	m_paragraph = NULL;
	m_end_paragraph_reason = ER_NONE;

	parse(text);
}

ParsedText::~ParsedText()
{
	for (auto &tag : m_not_root_tags)
		delete tag;
}

void ParsedText::parse(const wchar_t *text)
{
	wchar_t c;
	u32 cursor = 0;
	bool escape = false;

	while ((c = text[cursor]) != L'\0') {
		cursor++;

		if (c == L'\r') { // Mac or Windows breaks
			if (text[cursor] == L'\n')
				cursor++;
			// If text has begun, don't skip empty line
			if (m_paragraph) {
				endParagraph(ER_NEWLINE);
				enterElement(ELEMENT_SEPARATOR);
			}
			escape = false;
			continue;
		}

		if (c == L'\n') { // Unix breaks
			// If text has begun, don't skip empty line
			if (m_paragraph) {
				endParagraph(ER_NEWLINE);
				enterElement(ELEMENT_SEPARATOR);
			}
			escape = false;
			continue;
		}

		if (escape) {
			escape = false;
			pushChar(c);
			continue;
		}

		if (c == L'\\') {
			escape = true;
			continue;
		}

		// Tag check
		if (c == L'<') {
			u32 newcursor = parseTag(text, cursor);
			if (newcursor > 0) {
				cursor = newcursor;
				continue;
			}
		}

		// Default behavior
		pushChar(c);
	}

	endParagraph(ER_NONE);
}

void ParsedText::endElement()
{
	m_element = NULL;
}

void ParsedText::endParagraph(EndReason reason)
{
	if (!m_paragraph)
		return;

	EndReason previous = m_end_paragraph_reason;
	m_end_paragraph_reason = reason;
	if (m_empty_paragraph && (reason == ER_TAG ||
			(reason == ER_NEWLINE && previous == ER_TAG))) {
		// Ignore last empty paragraph
		m_paragraph = nullptr;
		m_paragraphs.pop_back();
		return;
	}
	endElement();
	m_paragraph = NULL;
}

void ParsedText::enterParagraph()
{
	if (!m_paragraph) {
		m_paragraphs.emplace_back();
		m_paragraph = &m_paragraphs.back();
		m_paragraph->setStyle(m_style);
		m_empty_paragraph = true;
	}
}

void ParsedText::enterElement(ElementType type)
{
	enterParagraph();

	if (!m_element || m_element->type != type) {
		m_paragraph->elements.emplace_back();
		m_element = &m_paragraph->elements.back();
		m_element->type = type;
		m_element->tags = m_active_tags;
		m_element->setStyle(m_style);
	}
}

void ParsedText::pushChar(wchar_t c)
{
	// New word if needed
	if (c == L' ' || c == L'\t') {
		if (!m_empty_paragraph)
			enterElement(ELEMENT_SEPARATOR);
		else
			return;
	} else {
		m_empty_paragraph = false;
		enterElement(ELEMENT_TEXT);
	}
	m_element->text += c;
}

ParsedText::Tag *ParsedText::newTag(const std::string &name, const AttrsList &attrs)
{
	endElement();
	Tag *newtag = new Tag();
	newtag->name = name;
	newtag->attrs = attrs;
	m_not_root_tags.push_back(newtag);
	return newtag;
}

ParsedText::Tag *ParsedText::openTag(const std::string &name, const AttrsList &attrs)
{
	Tag *newtag = newTag(name, attrs);
	m_active_tags.push_front(newtag);
	return newtag;
}

bool ParsedText::closeTag(const std::string &name)
{
	bool found = false;
	for (auto id = m_active_tags.begin(); id != m_active_tags.end(); ++id)
		if ((*id)->name == name) {
			m_active_tags.erase(id);
			found = true;
			break;
		}
	return found;
}

void ParsedText::parseGenericStyleAttr(
		const std::string &name, const std::string &value, StyleList &style)
{
	// Color styles
	if (name == "color" || name == "hovercolor") {
		if (check_color(value))
			style[name] = value;

		// Boolean styles
	} else if (name == "bold" || name == "italic" || name == "underline") {
		style[name] = is_yes(value);

	} else if (name == "size") {
		if (check_integer(value))
			style["fontsize"] = value;

	} else if (name == "font") {
		if (value == "mono" || value == "normal")
			style["fontstyle"] = value;
	}
}

void ParsedText::parseStyles(const AttrsList &attrs, StyleList &style)
{
	for (auto const &attr : attrs)
		parseGenericStyleAttr(attr.first, attr.second, style);
}

void ParsedText::globalTag(const AttrsList &attrs)
{
	for (const auto &attr : attrs) {
		// Only page level style
		if (attr.first == "margin") {
			if (check_integer(attr.second))
				margin = stoi(attr.second.c_str());

		} else if (attr.first == "valign") {
			if (attr.second == "top")
				valign = ParsedText::VALIGN_TOP;
			else if (attr.second == "bottom")
				valign = ParsedText::VALIGN_BOTTOM;
			else if (attr.second == "middle")
				valign = ParsedText::VALIGN_MIDDLE;
		} else if (attr.first == "background") {
			irr::video::SColor color;
			if (attr.second == "none") {
				background_type = BACKGROUND_NONE;
			} else if (parseColorString(attr.second, color, false)) {
				background_type = BACKGROUND_COLOR;
				background_color = color;
			}

			// Inheriting styles

		} else if (attr.first == "halign") {
			if (attr.second == "left" || attr.second == "center" ||
					attr.second == "right" ||
					attr.second == "justify")
				m_root_tag.style["halign"] = attr.second;

			// Generic default styles

		} else {
			parseGenericStyleAttr(attr.first, attr.second, m_root_tag.style);
		}
	}
}

u32 ParsedText::parseTag(const wchar_t *text, u32 cursor)
{
	// Tag name
	bool end = false;
	std::string name = "";
	wchar_t c = text[cursor];

	if (c == L'/') {
		end = true;
		c = text[++cursor];
		if (c == L'\0')
			return 0;
	}

	while (c != ' ' && c != '>') {
		name += c;
		c = text[++cursor];
		if (c == L'\0')
			return 0;
	}

	// Tag attributes
	AttrsList attrs;
	while (c != L'>') {
		std::string attr_name = "";
		core::stringw attr_val = L"";

		while (c == ' ') {
			c = text[++cursor];
			if (c == L'\0' || c == L'=')
				return 0;
		}

		while (c != L' ' && c != L'=') {
			attr_name += (char)c;
			c = text[++cursor];
			if (c == L'\0' || c == L'>')
				return 0;
		}

		while (c == L' ') {
			c = text[++cursor];
			if (c == L'\0' || c == L'>')
				return 0;
		}

		if (c != L'=')
			return 0;

		c = text[++cursor];

		if (c == L'\0')
			return 0;

		while (c != L'>' && c != L' ') {
			attr_val += c;
			c = text[++cursor];
			if (c == L'\0')
				return 0;
		}

		attrs[attr_name] = stringw_to_utf8(attr_val);
	}

	++cursor; // Last ">"

	// Tag specific processing
	StyleList style;

	if (name == "global") {
		if (end)
			return 0;
		globalTag(attrs);

	} else if (name == "style") {
		if (end) {
			closeTag(name);
		} else {
			parseStyles(attrs, style);
			openTag(name, attrs)->style = style;
		}
		endElement();
	} else if (name == "img" || name == "item") {
		if (end)
			return 0;

		// Name is a required attribute
		if (!attrs.count("name"))
			return 0;

		// Rotate attribute is only for <item>
		if (attrs.count("rotate") && name != "item")
			return 0;

		// Angle attribute is only for <item>
		if (attrs.count("angle") && name != "item")
			return 0;

		// Ok, element can be created
		newTag(name, attrs);

		if (name == "img")
			enterElement(ELEMENT_IMAGE);
		else
			enterElement(ELEMENT_ITEM);

		m_element->text = utf8_to_stringw(attrs["name"]);

		if (attrs.count("float")) {
			if (attrs["float"] == "left")
				m_element->floating = FLOAT_LEFT;
			if (attrs["float"] == "right")
				m_element->floating = FLOAT_RIGHT;
		}

		if (attrs.count("width")) {
			int width = stoi(attrs["width"]);
			if (width > 0)
				m_element->dim.Width = width;
		}

		if (attrs.count("height")) {
			int height = stoi(attrs["height"]);
			if (height > 0)
				m_element->dim.Height = height;
		}

		if (attrs.count("angle")) {
			std::string str = attrs["angle"];
			std::vector<std::string> parts = split(str, ',');
			if (parts.size() == 3) {
				m_element->angle = v3s16(
						rangelim(stoi(parts[0]), -180, 180),
						rangelim(stoi(parts[1]), -180, 180),
						rangelim(stoi(parts[2]), -180, 180));
				m_element->rotation = v3s16(0, 0, 0);
			}
		}

		if (attrs.count("rotate")) {
			if (attrs["rotate"] == "yes") {
				m_element->rotation = v3s16(0, 100, 0);
			} else {
				std::string str = attrs["rotate"];
				std::vector<std::string> parts = split(str, ',');
				if (parts.size() == 3) {
					m_element->rotation = v3s16 (
							rangelim(stoi(parts[0]), -1000, 1000),
							rangelim(stoi(parts[1]), -1000, 1000),
							rangelim(stoi(parts[2]), -1000, 1000));
				}
			}
		}

		endElement();

	} else if (name == "tag") {
		// Required attributes
		if (!attrs.count("name"))
			return 0;

		StyleList tagstyle;
		parseStyles(attrs, tagstyle);

		if (is_yes(attrs["paragraph"]))
			m_paragraphtags[attrs["name"]] = tagstyle;
		else
			m_elementtags[attrs["name"]] = tagstyle;

	} else if (name == "action") {
		if (end) {
			closeTag(name);
		} else {
			if (!attrs.count("name"))
				return 0;
			openTag(name, attrs)->style = m_elementtags["action"];
		}

	} else if (m_elementtags.count(name)) {
		if (end) {
			closeTag(name);
		} else {
			openTag(name, attrs)->style = m_elementtags[name];
		}
		endElement();

	} else if (m_paragraphtags.count(name)) {
		if (end) {
			closeTag(name);
		} else {
			openTag(name, attrs)->style = m_paragraphtags[name];
		}
		endParagraph(ER_TAG);

	} else
		return 0; // Unknown tag

	// Update styles accordingly
	m_style.clear();
	for (auto tag = m_active_tags.crbegin(); tag != m_active_tags.crend(); ++tag)
		for (const auto &prop : (*tag)->style)
			m_style[prop.first] = prop.second;

	return cursor;
}

// -----------------------------------------------------------------------------
// Text Drawer

TextDrawer::TextDrawer(const wchar_t *text, Client *client,
		gui::IGUIEnvironment *environment, ISimpleTextureSource *tsrc) :
		m_text(text),
		m_client(client), m_environment(environment)
{
	// Size all elements
	for (auto &p : m_text.m_paragraphs) {
		for (auto &e : p.elements) {
			switch (e.type) {
			case ParsedText::ELEMENT_SEPARATOR:
			case ParsedText::ELEMENT_TEXT:
				if (e.font) {
					e.dim.Width = e.font->getDimension(e.text.c_str()).Width;
					e.dim.Height = e.font->getDimension(L"Yy").Height;
					if (e.font->getType() == irr::gui::EGFT_CUSTOM) {
						CGUITTFont *tmp = static_cast<CGUITTFont*>(e.font);
						e.baseline = e.dim.Height - 1 - tmp->getAscender() / 64;
					}
				} else {
					e.dim = {0, 0};
				}
				break;

			case ParsedText::ELEMENT_IMAGE:
			case ParsedText::ELEMENT_ITEM:
				// Resize only non sized items
				if (e.dim.Height != 0 && e.dim.Width != 0)
					break;

				// Default image and item size
				core::dimension2d<u32> dim(80, 80);

				if (e.type == ParsedText::ELEMENT_IMAGE) {
					video::ITexture *texture =
						m_client->getTextureSource()->
							getTexture(stringw_to_utf8(e.text));
					if (texture)
						dim = texture->getOriginalSize();
				}

				if (e.dim.Height == 0)
					if (e.dim.Width == 0)
						e.dim = dim;
					else
						e.dim.Height = dim.Height * e.dim.Width /
								dim.Width;
				else
					e.dim.Width = dim.Width * e.dim.Height /
							dim.Height;
				break;
			}
		}
	}
}

// Get element at given coordinates. Coordinates are inner coordinates (starting
// at 0,0).
ParsedText::Element *TextDrawer::getElementAt(core::position2d<s32> pos)
{
	pos.Y -= m_voffset;
	for (auto &p : m_text.m_paragraphs) {
		for (auto &el : p.elements) {
			core::rect<s32> rect(el.pos, el.dim);
			if (rect.isPointInside(pos))
				return &el;
		}
	}
	return 0;
}

/*
   This function places all elements according to given width. Elements have
   been previously sized by constructor and will be later drawed by draw.
   It may be called each time width changes and resulting height can be
   retrieved using getHeight. See GUIHyperText constructor, it uses it once to
   test if text fits in window and eventually another time if width is reduced
   m_floating because of scrollbar added.
*/
void TextDrawer::place(const core::rect<s32> &dest_rect)
{
	m_floating.clear();
	s32 y = 0;
	s32 ymargin = m_text.margin;

	// Iterator used :
	// p - Current paragraph, walked only once
	// el - Current element, walked only once
	// e and f - local element and floating operators

	for (auto &p : m_text.m_paragraphs) {
		// Find and place floating stuff in paragraph
		for (auto e = p.elements.begin(); e != p.elements.end(); ++e) {
			if (e->floating != ParsedText::FLOAT_NONE) {
				if (y)
					e->pos.Y = y + std::max(ymargin, e->margin);
				else
					e->pos.Y = ymargin;

				if (e->floating == ParsedText::FLOAT_LEFT)
					e->pos.X = m_text.margin;
				if (e->floating == ParsedText::FLOAT_RIGHT)
					e->pos.X = dest_rect.getWidth() - e->dim.Width -
							m_text.margin;

				RectWithMargin floating;
				floating.rect = core::rect<s32>(e->pos, e->dim);
				floating.margin = e->margin;

				m_floating.push_back(floating);
			}
		}

		if (y)
			y = y + std::max(ymargin, p.margin);

		ymargin = p.margin;

		// Place non floating stuff
		std::vector<ParsedText::Element>::iterator el = p.elements.begin();

		while (el != p.elements.end()) {
			// Determine line width and y pos
			s32 left, right;
			s32 nexty = y;
			do {
				y = nexty;
				nexty = 0;

				// Inner left & right
				left = m_text.margin;
				right = dest_rect.getWidth() - m_text.margin;

				for (const auto &f : m_floating) {
					// Does floating rect intersect paragraph y line?
					if (f.rect.UpperLeftCorner.Y - f.margin <= y &&
							f.rect.LowerRightCorner.Y + f.margin >= y) {

						// Next Y to try if no room left
						if (!nexty || f.rect.LowerRightCorner.Y +
								std::max(f.margin, p.margin) < nexty) {
							nexty = f.rect.LowerRightCorner.Y +
									std::max(f.margin, p.margin) + 1;
						}

						if (f.rect.UpperLeftCorner.X - f.margin <= left &&
								f.rect.LowerRightCorner.X + f.margin < right) {
							// float on left
							if (f.rect.LowerRightCorner.X +
									std::max(f.margin, p.margin) > left) {
								left = f.rect.LowerRightCorner.X +
										std::max(f.margin, p.margin);
							}
						} else if (f.rect.LowerRightCorner.X + f.margin >= right &&
								f.rect.UpperLeftCorner.X - f.margin > left) {
							// float on right
							if (f.rect.UpperLeftCorner.X -
									std::max(f.margin, p.margin) < right)
								right = f.rect.UpperLeftCorner.X -
										std::max(f.margin, p.margin);

						} else if (f.rect.UpperLeftCorner.X - f.margin <= left &&
							 	f.rect.LowerRightCorner.X + f.margin >= right) {
							// float taking all space
							left = right;
						}
						else
						{ // float in the middle -- should not occure yet, see that later
						}
					}
				}
			} while (nexty && right <= left);

			u32 linewidth = right - left;
			float x = left;

			u32 charsheight = 0;
			u32 charswidth = 0;
			u32 wordcount = 0;

			// Skip begining of line separators but include them in height
			// computation.
			while (el != p.elements.end() &&
					el->type == ParsedText::ELEMENT_SEPARATOR) {
				if (el->floating == ParsedText::FLOAT_NONE) {
					el->drawwidth = 0;
					if (charsheight < el->dim.Height)
						charsheight = el->dim.Height;
				}
				el++;
			}

			std::vector<ParsedText::Element>::iterator linestart = el;
			std::vector<ParsedText::Element>::iterator lineend = p.elements.end();

			// First pass, find elements fitting into line
			// (or at least one element)
			while (el != p.elements.end() && (charswidth == 0 ||
					charswidth + el->dim.Width <= linewidth)) {
				if (el->floating == ParsedText::FLOAT_NONE) {
					if (el->type != ParsedText::ELEMENT_SEPARATOR) {
						lineend = el;
						wordcount++;
					}
					charswidth += el->dim.Width;
					if (charsheight < el->dim.Height)
						charsheight = el->dim.Height;
				}
				el++;
			}

			// Empty line, nothing to place only go down line height
			if (lineend == p.elements.end()) {
				y += charsheight;
				continue;
			}

			// Point to the first position outside line (may be end())
			lineend++;

			// Second pass, compute printable line width and adjustments
			charswidth = 0;
			s32 top = 0;
			s32 bottom = 0;
			for (auto e = linestart; e != lineend; ++e) {
				if (e->floating == ParsedText::FLOAT_NONE) {
					charswidth += e->dim.Width;
					if (top < (s32)e->dim.Height - e->baseline)
						top = e->dim.Height - e->baseline;
					if (bottom < e->baseline)
						bottom = e->baseline;
				}
			}

			float extraspace = 0.f;

			switch (p.halign) {
			case ParsedText::HALIGN_CENTER:
				x += (linewidth - charswidth) / 2.f;
				break;
			case ParsedText::HALIGN_JUSTIFY:
				if (wordcount > 1 && // Justification only if at least two words
					!(lineend == p.elements.end())) // Don't justify last line
					extraspace = ((float)(linewidth - charswidth)) / (wordcount - 1);
				break;
			case ParsedText::HALIGN_RIGHT:
				x += linewidth - charswidth;
				break;
			case ParsedText::HALIGN_LEFT:
				break;
			}

			// Third pass, actually place everything
			for (auto e = linestart; e != lineend; ++e) {
				if (e->floating != ParsedText::FLOAT_NONE)
					continue;

				e->pos.X = x;
				e->pos.Y = y;

				switch (e->type) {
				case ParsedText::ELEMENT_TEXT:
				case ParsedText::ELEMENT_SEPARATOR:
					e->pos.X = x;

					// Align char baselines
					e->pos.Y = y + top + e->baseline - e->dim.Height;

					x += e->dim.Width;
					if (e->type == ParsedText::ELEMENT_SEPARATOR)
						x += extraspace;
					break;

				case ParsedText::ELEMENT_IMAGE:
				case ParsedText::ELEMENT_ITEM:
					x += e->dim.Width;
					break;
				}

				// Draw width for separator can be different than element
				// width. This will be important for char effects like
				// underline.
				e->drawwidth = x - e->pos.X;
			}
			y += charsheight;
		} // Elements (actually lines)
	} // Paragraph

	// Check if float goes under paragraph
	for (const auto &f : m_floating) {
		if (f.rect.LowerRightCorner.Y >= y)
			y = f.rect.LowerRightCorner.Y;
	}

	m_height = y + m_text.margin;
	// Compute vertical offset according to vertical alignment
	if (m_height < dest_rect.getHeight())
		switch (m_text.valign) {
		case ParsedText::VALIGN_BOTTOM:
			m_voffset = dest_rect.getHeight() - m_height;
			break;
		case ParsedText::VALIGN_MIDDLE:
			m_voffset = (dest_rect.getHeight() - m_height) / 2;
			break;
		case ParsedText::VALIGN_TOP:
		default:
			m_voffset = 0;
		}
	else
		m_voffset = 0;
}

// Draw text in a rectangle with a given offset. Items are actually placed in
// relative (to upper left corner) coordinates.
void TextDrawer::draw(const core::rect<s32> &clip_rect,
		const core::position2d<s32> &dest_offset)
{
	irr::video::IVideoDriver *driver = m_environment->getVideoDriver();
	core::position2d<s32> offset = dest_offset;
	offset.Y += m_voffset;

	if (m_text.background_type == ParsedText::BACKGROUND_COLOR)
		driver->draw2DRectangle(m_text.background_color, clip_rect);

	for (auto &p : m_text.m_paragraphs) {
		for (auto &el : p.elements) {
			core::rect<s32> rect(el.pos + offset, el.dim);
			if (!rect.isRectCollided(clip_rect))
				continue;

			switch (el.type) {
			case ParsedText::ELEMENT_SEPARATOR:
			case ParsedText::ELEMENT_TEXT: {
				irr::video::SColor color = el.color;

				for (auto tag : el.tags)
					if (&(*tag) == m_hovertag)
						color = el.hovercolor;

				if (!el.font)
					break;

				if (el.type == ParsedText::ELEMENT_TEXT)
					el.font->draw(el.text, rect, color, false, true,
							&clip_rect);

				if (el.underline &&  el.drawwidth) {
					s32 linepos = el.pos.Y + offset.Y +
							el.dim.Height - (el.baseline >> 1);

					core::rect<s32> linerect(el.pos.X + offset.X,
							linepos - (el.baseline >> 3) - 1,
							el.pos.X + offset.X + el.drawwidth,
							linepos + (el.baseline >> 3));

					driver->draw2DRectangle(color, linerect, &clip_rect);
				}
			} break;

			case ParsedText::ELEMENT_IMAGE: {
				video::ITexture *texture =
						m_client->getTextureSource()->getTexture(
								stringw_to_utf8(el.text));
				if (texture != 0)
					m_environment->getVideoDriver()->draw2DImage(
							texture, rect,
							irr::core::rect<s32>(
									core::position2d<s32>(0, 0),
									texture->getOriginalSize()),
							&clip_rect, 0, true);
			} break;

			case ParsedText::ELEMENT_ITEM: {
				IItemDefManager *idef = m_client->idef();
				ItemStack item;
				item.deSerialize(stringw_to_utf8(el.text), idef);