9
3

VisualStates.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator 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 General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "../common/Log.h"
  17. #include "../../common/MiscFunctions.h"
  18. #include <map>
  19. using namespace std;
  20. // Visual States must use a hash table because of the large amount that exists and the large spacing
  21. // between their ID's. String and character arrays could not be used for the first iterator because
  22. // it would require the same pointer to access it from the hash table, which is obviously not possible
  23. // since the text is from the client.
  24. // maximum amount of iterations it will attempt to find a entree
  25. #define HASH_SEARCH_MAX 20
  26. class VisualState
  27. {
  28. public:
  29. VisualState(int inID, char* inName){
  30. if(!inName)
  31. return;
  32. name = string(inName);
  33. id = inID;
  34. }
  35. int GetID() { return id; }
  36. const char* GetName() { return name.c_str(); }
  37. string GetNameString() { return name; }
  38. private:
  39. int id;
  40. string name;
  41. };
  42. class Emote{
  43. public:
  44. Emote(char* in_name, int in_visual_state, char* in_message, char* in_targeted_message){
  45. if(!in_name)
  46. return;
  47. name = string(in_name);
  48. visual_state = in_visual_state;
  49. if(in_message)
  50. message = string(in_message);
  51. if(in_targeted_message)
  52. targeted_message = string(in_targeted_message);
  53. }
  54. int32 GetVisualState() { return visual_state; }
  55. const char* GetName() { return name.c_str(); }
  56. const char* GetMessage() { return message.c_str(); }
  57. const char* GetTargetedMessage() { return targeted_message.c_str(); }
  58. string GetNameString() { return name; }
  59. string GetMessageString() { return message; }
  60. string GetTargetedMessageString() { return targeted_message; }
  61. private:
  62. int32 visual_state;
  63. string name;
  64. string message;
  65. string targeted_message;
  66. };
  67. class EmoteVersionRange {
  68. public:
  69. EmoteVersionRange(char* in_name)
  70. {
  71. name = string(in_name);
  72. }
  73. ~EmoteVersionRange()
  74. {
  75. map<VersionRange*, Emote*>::iterator itr;
  76. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  77. {
  78. VersionRange* range = itr->first;
  79. Emote* emote = itr->second;
  80. delete range;
  81. delete emote;
  82. }
  83. version_map.clear();
  84. }
  85. void AddVersionRange(int32 min_version, int32 max_version,
  86. char* in_name, int in_visual_state, char* in_message = nullptr, char* in_targeted_message = nullptr)
  87. {
  88. map<VersionRange*, Emote*>::iterator itr = FindVersionRange(min_version, max_version);
  89. if (itr != version_map.end())
  90. {
  91. VersionRange* range = itr->first;
  92. LogWrite(WORLD__ERROR, 0, "Emotes Table Error: Duplicate emote mapping of %s with range min %u max %u, Existing found with range min %u max %u\n", name.c_str(), min_version, max_version, range->GetMinVersion(), range->GetMaxVersion());
  93. return;
  94. }
  95. version_map.insert(make_pair(new VersionRange(min_version, max_version), new Emote(in_name, in_visual_state, in_message, in_targeted_message)));
  96. }
  97. map<VersionRange*, Emote*>::iterator FindVersionRange(int32 min_version, int32 max_version)
  98. {
  99. map<VersionRange*, Emote*>::iterator itr;
  100. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  101. {
  102. VersionRange* range = itr->first;
  103. // if min and max version are both in range
  104. if (range->GetMinVersion() <= min_version && max_version <= range->GetMaxVersion())
  105. return itr;
  106. // if the min version is in range, but max range is 0
  107. else if (range->GetMinVersion() <= min_version && range->GetMaxVersion() == 0)
  108. return itr;
  109. // if min version is 0 and max_version has a cap
  110. else if (range->GetMinVersion() == 0 && max_version <= range->GetMaxVersion())
  111. return itr;
  112. }
  113. return version_map.end();
  114. }
  115. map<VersionRange*, Emote*>::iterator FindEmoteVersion(int32 version)
  116. {
  117. map<VersionRange*, Emote*>::iterator itr;
  118. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  119. {
  120. VersionRange* range = itr->first;
  121. // if min and max version are both in range
  122. if (version >= range->GetMinVersion() && (range->GetMaxVersion() == 0 || version <= range->GetMaxVersion()))
  123. return itr;
  124. }
  125. return version_map.end();
  126. }
  127. const char* GetName() { return name.c_str(); }
  128. string GetNameString() { return name; }
  129. map<VersionRange*, Emote*>::iterator GetRangeEnd() { return version_map.end(); }
  130. private:
  131. map<VersionRange*, Emote*> version_map;
  132. string name;
  133. };
  134. class VisualStates
  135. {
  136. public:
  137. ~VisualStates(){
  138. Reset();
  139. }
  140. void Reset(){
  141. ClearVisualStates();
  142. ClearEmotes();
  143. ClearSpellVisuals();
  144. }
  145. void ClearEmotes(){
  146. map<string, EmoteVersionRange*>::iterator map_list;
  147. for(map_list = emoteMap.begin(); map_list != emoteMap.end(); map_list++ )
  148. safe_delete(map_list->second);
  149. emoteMap.clear();
  150. }
  151. void ClearVisualStates(){
  152. map<string, VisualState*>::iterator map_list;
  153. for(map_list = visualStateMap.begin(); map_list != visualStateMap.end(); map_list++ )
  154. safe_delete(map_list->second);
  155. visualStateMap.clear();
  156. }
  157. void InsertVisualState(VisualState* vs){
  158. visualStateMap[vs->GetNameString()] = vs;
  159. }
  160. VisualState* FindVisualState(string var){
  161. if(visualStateMap.count(var) > 0)
  162. return visualStateMap[var];
  163. return 0;
  164. }
  165. void InsertEmoteRange(EmoteVersionRange* emote) {
  166. emoteMap[emote->GetName()] = emote;
  167. }
  168. EmoteVersionRange* FindEmoteRange(string var) {
  169. if (emoteMap.count(var) > 0)
  170. {
  171. return emoteMap[var];
  172. }
  173. return 0;
  174. }
  175. Emote* FindEmote(string var, int32 version){
  176. if (emoteMap.count(var) > 0)
  177. {
  178. map<VersionRange*,Emote*>::iterator itr = emoteMap[var]->FindEmoteVersion(version);
  179. if (itr != emoteMap[var]->GetRangeEnd())
  180. {
  181. Emote* emote = itr->second;
  182. return emote;
  183. }
  184. }
  185. return 0;
  186. }
  187. void InsertSpellVisualRange(EmoteVersionRange* emote, int32 spell_visual_id) {
  188. spellMap[emote->GetName()] = emote;
  189. spellMapID[spell_visual_id] = emote;
  190. }
  191. EmoteVersionRange* FindSpellVisualRange(string var) {
  192. if (spellMap.count(var) > 0)
  193. {
  194. return spellMap[var];
  195. }
  196. return 0;
  197. }
  198. EmoteVersionRange* FindSpellVisualRangeByID(int32 id) {
  199. if (spellMapID.count(id) > 0)
  200. {
  201. return spellMapID[id];
  202. }
  203. return 0;
  204. }
  205. Emote* FindSpellVisual(string var, int32 version){
  206. if (spellMap.count(var) > 0)
  207. {
  208. map<VersionRange*,Emote*>::iterator itr = spellMap[var]->FindEmoteVersion(version);
  209. if (itr != spellMap[var]->GetRangeEnd())
  210. {
  211. Emote* emote = itr->second;
  212. return emote;
  213. }
  214. }
  215. return 0;
  216. }
  217. Emote* FindSpellVisualByID(int32 visual_id, int32 version){
  218. if (spellMapID.count(visual_id) > 0)
  219. {
  220. map<VersionRange*,Emote*>::iterator itr = spellMapID[visual_id]->FindEmoteVersion(version);
  221. if (itr != spellMapID[visual_id]->GetRangeEnd())
  222. {
  223. Emote* emote = itr->second;
  224. return emote;
  225. }
  226. }
  227. return 0;
  228. }
  229. void ClearSpellVisuals(){
  230. map<string, EmoteVersionRange*>::iterator map_list;
  231. for(map_list = spellMap.begin(); map_list != spellMap.end(); map_list++ )
  232. safe_delete(map_list->second);
  233. spellMap.clear();
  234. spellMapID.clear();
  235. }
  236. private:
  237. map<string,VisualState*> visualStateMap;
  238. map<string,EmoteVersionRange*> emoteMap;
  239. map<string,EmoteVersionRange*> spellMap;
  240. map<int32,EmoteVersionRange*> spellMapID;
  241. };