9
3

VisualStates.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <map>
  18. using namespace std;
  19. // Visual States must use a hash table because of the large amount that exists and the large spacing
  20. // between their ID's. String and character arrays could not be used for the first iterator because
  21. // it would require the same pointer to access it from the hash table, which is obviously not possible
  22. // since the text is from the client.
  23. // maximum amount of iterations it will attempt to find a entree
  24. #define HASH_SEARCH_MAX 20
  25. class VisualState
  26. {
  27. public:
  28. VisualState(int inID, char* inName){
  29. if(!inName)
  30. return;
  31. name = string(inName);
  32. id = inID;
  33. }
  34. int GetID() { return id; }
  35. const char* GetName() { return name.c_str(); }
  36. string GetNameString() { return name; }
  37. private:
  38. int id;
  39. string name;
  40. };
  41. class Emote{
  42. public:
  43. Emote(char* in_name, int in_visual_state, char* in_message, char* in_targeted_message){
  44. if(!in_name)
  45. return;
  46. name = string(in_name);
  47. visual_state = in_visual_state;
  48. if(in_message)
  49. message = string(in_message);
  50. if(in_targeted_message)
  51. targeted_message = string(in_targeted_message);
  52. }
  53. int GetVisualState() { return visual_state; }
  54. const char* GetName() { return name.c_str(); }
  55. const char* GetMessage() { return message.c_str(); }
  56. const char* GetTargetedMessage() { return targeted_message.c_str(); }
  57. string GetNameString() { return name; }
  58. string GetMessageString() { return message; }
  59. string GetTargetedMessageString() { return targeted_message; }
  60. private:
  61. int visual_state;
  62. string name;
  63. string message;
  64. string targeted_message;
  65. };
  66. class VersionRange {
  67. public:
  68. VersionRange(int32 in_min_version, int32 in_max_version)
  69. {
  70. min_version = in_min_version;
  71. max_version = in_max_version;
  72. }
  73. int32 GetMinVersion() { return min_version; }
  74. int32 GetMaxVersion() { return max_version; }
  75. private:
  76. int32 min_version;
  77. int32 max_version;
  78. };
  79. class EmoteVersionRange {
  80. public:
  81. EmoteVersionRange(char* in_name)
  82. {
  83. name = string(in_name);
  84. }
  85. ~EmoteVersionRange()
  86. {
  87. map<VersionRange*, Emote*>::iterator itr;
  88. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  89. {
  90. VersionRange* range = itr->first;
  91. Emote* emote = itr->second;
  92. delete range;
  93. delete emote;
  94. }
  95. version_map.clear();
  96. }
  97. void AddVersionRange(int32 min_version, int32 max_version,
  98. char* in_name, int in_visual_state, char* in_message, char* in_targeted_message)
  99. {
  100. map<VersionRange*, Emote*>::iterator itr = FindVersionRange(min_version, max_version);
  101. if (itr != version_map.end())
  102. {
  103. VersionRange* range = itr->first;
  104. 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());
  105. return;
  106. }
  107. version_map.insert(make_pair(new VersionRange(min_version, max_version), new Emote(in_name, in_visual_state, in_message, in_targeted_message)));
  108. }
  109. map<VersionRange*, Emote*>::iterator FindVersionRange(int32 min_version, int32 max_version)
  110. {
  111. map<VersionRange*, Emote*>::iterator itr;
  112. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  113. {
  114. VersionRange* range = itr->first;
  115. // if min and max version are both in range
  116. if (range->GetMinVersion() <= min_version && max_version <= range->GetMaxVersion())
  117. return itr;
  118. // if the min version is in range, but max range is 0
  119. else if (range->GetMinVersion() <= min_version && range->GetMaxVersion() == 0)
  120. return itr;
  121. // if min version is 0 and max_version has a cap
  122. else if (range->GetMinVersion() == 0 && max_version <= range->GetMaxVersion())
  123. return itr;
  124. }
  125. return version_map.end();
  126. }
  127. map<VersionRange*, Emote*>::iterator FindEmoteVersion(int32 version)
  128. {
  129. map<VersionRange*, Emote*>::iterator itr;
  130. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  131. {
  132. VersionRange* range = itr->first;
  133. // if min and max version are both in range
  134. if (version >= range->GetMinVersion() && (range->GetMaxVersion() == 0 || version <= range->GetMaxVersion()))
  135. return itr;
  136. }
  137. return version_map.end();
  138. }
  139. const char* GetName() { return name.c_str(); }
  140. string GetNameString() { return name; }
  141. map<VersionRange*, Emote*>::iterator GetRangeEnd() { return version_map.end(); }
  142. private:
  143. map<VersionRange*, Emote*> version_map;
  144. string name;
  145. };
  146. class VisualStates
  147. {
  148. public:
  149. ~VisualStates(){
  150. Reset();
  151. }
  152. void Reset(){
  153. ClearVisualStates();
  154. ClearEmotes();
  155. }
  156. void ClearEmotes(){
  157. map<string, EmoteVersionRange*>::iterator map_list;
  158. for(map_list = emoteMap.begin(); map_list != emoteMap.end(); map_list++ )
  159. safe_delete(map_list->second);
  160. emoteMap.clear();
  161. }
  162. void ClearVisualStates(){
  163. map<string, VisualState*>::iterator map_list;
  164. for(map_list = visualStateMap.begin(); map_list != visualStateMap.end(); map_list++ )
  165. safe_delete(map_list->second);
  166. visualStateMap.clear();
  167. }
  168. void InsertVisualState(VisualState* vs){
  169. visualStateMap[vs->GetNameString()] = vs;
  170. }
  171. VisualState* FindVisualState(string var){
  172. if(visualStateMap.count(var) > 0)
  173. return visualStateMap[var];
  174. return 0;
  175. }
  176. void InsertEmoteRange(EmoteVersionRange* emote) {
  177. emoteMap[emote->GetName()] = emote;
  178. }
  179. EmoteVersionRange* FindEmoteRange(string var) {
  180. if (emoteMap.count(var) > 0)
  181. {
  182. return emoteMap[var];
  183. }
  184. return 0;
  185. }
  186. Emote* FindEmote(string var, int32 version){
  187. if (emoteMap.count(var) > 0)
  188. {
  189. map<VersionRange*,Emote*>::iterator itr = emoteMap[var]->FindEmoteVersion(version);
  190. if (itr != emoteMap[var]->GetRangeEnd())
  191. {
  192. Emote* emote = itr->second;
  193. return emote;
  194. }
  195. }
  196. return 0;
  197. }
  198. private:
  199. map<string,VisualState*> visualStateMap;
  200. map<string,EmoteVersionRange*> emoteMap;
  201. };