Linux multi-monitor fullscreen support
[ryzomcore.git] / ryzom / client / src / interface_v3 / item_special_effect.cpp
blobdd4485f9d865d80d74dd1d9d5e39044619aaaf48
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "stdpch.h"
21 #include "item_special_effect.h"
22 #include "nel/misc/algo.h"
23 #include "nel/misc/i18n.h"
24 #include "../sheet_manager.h"
26 using namespace std;
27 using namespace NLMISC;
29 CItemSpecialEffectHelper::CItemSpecialEffectHelper()
31 // Register special effects
32 registerItemSpecialEffect("ISE_FIGHT_ADD_CRITICAL");
33 registerItemSpecialEffect("ISE_FIGHT_VAMPIRISM");
34 registerItemSpecialEffect("ISE_MAGIC_DIVINE_INTERVENTION");
35 registerItemSpecialEffect("ISE_MAGIC_SHOOT_AGAIN");
36 registerItemSpecialEffect("ISE_CRAFT_ADD_STAT_BONUS");
37 registerItemSpecialEffect("ISE_CRAFT_ADD_LIMIT");
38 registerItemSpecialEffect("ISE_FORAGE_ADD_RM");
39 registerItemSpecialEffect("ISE_FORAGE_NO_RISK");
42 CItemSpecialEffectHelper* CItemSpecialEffectHelper::getInstance()
44 // Singleton
45 static CItemSpecialEffectHelper* instance = NULL;
46 if (instance == NULL)
47 instance = new CItemSpecialEffectHelper;
48 return instance;
51 void CItemSpecialEffectHelper::registerItemSpecialEffect(const string &name)
53 // store parameters to be replaced in UIstring
54 vector<string> params;
56 // get ui string
57 string ucs = CI18N::get("uiItemFX_" + name);
58 CSString p, s = ucs;
60 // locate and store parameters
61 // %p : percent
62 // %n : integer
63 // %r : real
64 // %s : string
65 p = s.splitTo('%', true);
66 while (!p.empty() && !s.empty())
68 if (s[0] == 'p' || s[0] == 'n' || s[0] == 'r' || s[0] == 's')
70 string tmp = "%";
71 tmp += s[0];
72 if (s.size() >=2 && (uint8)s[1] < (uint8)'\x80' && isdigit(s[1]))
73 tmp += s[1];
74 params.push_back(tmp);
76 p = s.splitTo('%', true);
79 effectMap.insert(make_pair(name, params));
82 void CItemSpecialEffectHelper::getItemSpecialEffectText(const CItemSheet *pIS, string &itemText)
84 // check if some effects are present on this item
85 bool firstEffect = false;
86 string effects;
87 effects += getEffect(pIS->getEffect1(), firstEffect);
88 effects += getEffect(pIS->getEffect2(), firstEffect);
89 effects += getEffect(pIS->getEffect3(), firstEffect);
90 effects += getEffect(pIS->getEffect4(), firstEffect);
92 if(!effects.empty()) effects += "\n";
94 // Display special effects info.
95 strFindReplace(itemText, "%special_effects", effects);
98 string CItemSpecialEffectHelper::getEffect(const std::string &effect, bool &first)
100 string result;
101 CSString eff = effect;
103 if (eff.empty())
104 return result;
106 // Get name id of effect
107 CSString name = toUpperAscii(eff.splitTo(':', true));
109 // Extract parameters from sheet
110 vector<CSString> params;
111 CSString param = eff.splitTo(':', true);
112 while (!param.empty())
114 params.push_back(param);
115 param = eff.splitTo(':', true);
118 // Check number of arguments
119 uint n = (uint)(effectMap[name]).size();
120 if (params.size() != n)
122 nlinfo("Bad arguments for : %s", effect.c_str());
123 return result;
126 // Get translated ui string
127 result = CI18N::get("uiItemFX_" + name);
129 // Add endline if it's not the first
130 if (!first)
131 first = true;
132 else
133 result = "\n" + result;
135 // Replace each parameters with value from sheet
136 //nlinfo("name = %s", name.c_str());
137 for (uint i=0 ; i<n ; i++)
139 string replace, p = effectMap[name][i];
140 //nlinfo("param = %s : %s", p.c_str(), params[i].c_str());
142 // %p : percent format
143 if (p[1] == 'p')
145 float f;
146 fromString(params[i], f);
147 replace = toString("%.1f", f*100);
149 // %n : integer format
150 else if (p[1] == 'n')
152 int a;
153 fromString(params[i], a);
154 replace = toString("%d", a);
156 // %r : real format
157 else if (p[1] == 'r')
159 float f;
160 fromString(params[i], f);
161 replace = toString("%.1f", f);
163 // %s : string format
164 else if (p[1] == 's')
166 replace = params[i];
168 else
170 continue;
173 strFindReplace(result, p.c_str(), replace);
176 return result;