VisualStates.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. int 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. int 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, char* in_targeted_message)
  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. }
  144. void ClearEmotes(){
  145. map<string, EmoteVersionRange*>::iterator map_list;
  146. for(map_list = emoteMap.begin(); map_list != emoteMap.end(); map_list++ )
  147. safe_delete(map_list->second);
  148. emoteMap.clear();
  149. }
  150. void ClearVisualStates(){
  151. map<string, VisualState*>::iterator map_list;
  152. for(map_list = visualStateMap.begin(); map_list != visualStateMap.end(); map_list++ )
  153. safe_delete(map_list->second);
  154. visualStateMap.clear();
  155. }
  156. void InsertVisualState(VisualState* vs){
  157. visualStateMap[vs->GetNameString()] = vs;
  158. }
  159. VisualState* FindVisualState(string var){
  160. if(visualStateMap.count(var) > 0)
  161. return visualStateMap[var];
  162. return 0;
  163. }
  164. void InsertEmoteRange(EmoteVersionRange* emote) {
  165. emoteMap[emote->GetName()] = emote;
  166. }
  167. EmoteVersionRange* FindEmoteRange(string var) {
  168. if (emoteMap.count(var) > 0)
  169. {
  170. return emoteMap[var];
  171. }
  172. return 0;
  173. }
  174. Emote* FindEmote(string var, int32 version){
  175. if (emoteMap.count(var) > 0)
  176. {
  177. map<VersionRange*,Emote*>::iterator itr = emoteMap[var]->FindEmoteVersion(version);
  178. if (itr != emoteMap[var]->GetRangeEnd())
  179. {
  180. Emote* emote = itr->second;
  181. return emote;
  182. }
  183. }
  184. return 0;
  185. }
  186. private:
  187. map<string,VisualState*> visualStateMap;
  188. map<string,EmoteVersionRange*> emoteMap;
  189. };