Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / server / src / ai_service / profile_in_state.h
blobaaa8e46c909b2b8d72a7275b511cdb2c56ed451f
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef PROFILE_OWNER_H
18 #define PROFILE_OWNER_H
20 #include <algorithm>
21 #include "alias_tree_owner.h"
22 #include "profile.h"
24 //////////////////////////////////////////////////////////////////////////////
25 // CProfileParameters //
26 //////////////////////////////////////////////////////////////////////////////
28 class CProfileParameters
30 public:
31 struct TProfileParameter
33 std::string Name;
34 std::string ValueStr;
35 float ValueFloat;
37 TProfileParameter(std::string name, std::string valueStr, float valueFloat)
38 : Name(name)
39 , ValueStr(valueStr)
40 , ValueFloat(valueFloat)
45 typedef std::vector<TProfileParameter> TProfileParameters;
47 private:
48 struct TFindParameter
49 : public std::unary_function<TProfileParameter, bool>
51 std::string _Value;
52 TFindParameter(std::string const& value)
53 : _Value(value)
56 bool operator ()(TProfileParameter const& param) const
58 return param.Name==_Value;
62 public:
63 void setProfileParameters(std::vector<std::string> const& params);
65 void setProfileParameters(TProfileParameters const& parameters) { _profileParameters = parameters; }
67 TProfileParameters const& profileParameters() const { return _profileParameters; }
69 /** Ask for a profile parameter.
70 * Return true if the parameter is found and fill 'value' with parameter value.
72 bool getProfileParameter(std::string const& paramName, std::string& value) const;
74 /** Ask for a profile parameter.
75 * Return true if the parameter is found and fill 'value' with parameter value.
77 bool getProfileParameter(std::string const& paramName, float& value) const;
79 /** Ask for a profile parameter.
80 * Return true if the parameter is found.
82 bool checkProfileParameter(std::string const& paramName) const;
84 void addProfileParameter(std::string name, std::string valueStr, float valueFloat);
86 void removeProfileParameter(std::string name);
88 void mergeProfileParameter(const TProfileParameter &parameter);
90 void mergeProfileParameters(const TProfileParameters &parameters);
92 void parseParameters(std::vector<std::string> const& params, TProfileParameters& parsed);
93 TProfileParameters _profileParameters;
96 //////////////////////////////////////////////////////////////////////////////
97 // CProfileInState //
98 //////////////////////////////////////////////////////////////////////////////
100 class CProfileInState
101 : public CProfileParameters
102 #ifdef NL_DEBUG
103 , public NLMISC::IDbgPtrData
104 #endif
106 public:
107 #ifdef NL_DEBUG
108 CProfileInState()
110 _MoveProfile.setData( this );
111 _ActivityProfile.setData( this );
113 #endif
114 void setMoveProfile(IAIProfileFactory* profile) { _MoveProfile = profile; }
115 IAIProfileFactory* moveProfile() const { return _MoveProfile; }
116 void setActivityProfile(IAIProfileFactory* profile) { _ActivityProfile = profile; }
117 IAIProfileFactory* activityProfile() const { return _ActivityProfile; }
119 private:
120 NLMISC::CDbgPtr<IAIProfileFactory> _MoveProfile;
121 NLMISC::CDbgPtr<IAIProfileFactory> _ActivityProfile;
124 /****************************************************************************/
125 /* Inlined methods */
126 /****************************************************************************/
128 //////////////////////////////////////////////////////////////////////////////
129 // CProfileParameters //
130 //////////////////////////////////////////////////////////////////////////////
132 inline
133 void CProfileParameters::setProfileParameters(std::vector<std::string> const& params)
135 parseParameters(params, _profileParameters);
138 inline
139 bool CProfileParameters::getProfileParameter(std::string const& paramName, std::string& value) const
141 TProfileParameters::const_iterator it(std::find_if(_profileParameters.begin(), _profileParameters.end(), TFindParameter(paramName)));
142 if (it != _profileParameters.end())
144 value = it->ValueStr;
145 return true;
147 else
148 return false;
151 inline
152 bool CProfileParameters::getProfileParameter(std::string const& paramName, float& value) const
154 TProfileParameters::const_iterator it(std::find_if(_profileParameters.begin(), _profileParameters.end(), TFindParameter(paramName)));
155 if (it == _profileParameters.end())
156 return false;
158 value = it->ValueFloat;
159 return true;
162 inline
163 bool CProfileParameters::checkProfileParameter(std::string const& paramName) const
165 return std::find_if(_profileParameters.begin(), _profileParameters.end(), TFindParameter(paramName)) != _profileParameters.end();
168 inline
169 void CProfileParameters::addProfileParameter(std::string name, std::string valueStr, float valueFloat)
171 mergeProfileParameter(TProfileParameter(name, valueStr, valueFloat));
174 inline
175 void CProfileParameters::removeProfileParameter(std::string name)
177 TProfileParameters::iterator it(std::find_if(_profileParameters.begin(), _profileParameters.end(), TFindParameter(name)));
178 if (it != _profileParameters.end())
179 _profileParameters.erase(it);
182 inline
183 void CProfileParameters::mergeProfileParameter(const TProfileParameter &parameter)
185 // remove duplicate
186 TProfileParameters::iterator it = std::find_if(_profileParameters.begin(), _profileParameters.end(), TFindParameter(parameter.Name));
187 if (it != _profileParameters.end())
188 _profileParameters.erase(it);
189 // merge value
190 _profileParameters.push_back(parameter);
193 inline
194 void CProfileParameters::mergeProfileParameters(const TProfileParameters &parameters)
196 for (uint i=0; i<parameters.size(); ++i)
198 // remove duplicate
199 _profileParameters.erase(std::remove_if(_profileParameters.begin(), _profileParameters.end(), TFindParameter(parameters[i].Name)), _profileParameters.end());
201 // merge value
202 _profileParameters.insert(_profileParameters.end(), parameters.begin(), parameters.end());
205 inline
206 void CProfileParameters::parseParameters(std::vector<std::string> const& params, TProfileParameters& parsed)
208 for (uint i=0; i<params.size(); ++i)
210 std::string key, tail;
211 if (AI_SHARE::stringToKeywordAndTail(params[i], key, tail))
213 parsed.push_back(TProfileParameter(key, tail, float(atof(tail.c_str()))));
214 continue;
217 if (AI_SHARE::stringToWordAndTail(params[i], key, tail))
219 parsed.push_back(TProfileParameter(key, "", 0.0f));
220 continue;
225 #endif // PROFILE_OWNER_H