htreemgr.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program 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. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /Commando/Code/ww3d2/htreemgr.cpp 2 9/19/01 6:17p Jani_p $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando / G 3D Library *
  24. * *
  25. * $Archive:: /Commando/Code/ww3d2/htreemgr.cpp $*
  26. * *
  27. * Author:: Byon_g *
  28. * *
  29. * $Modtime:: 9/14/01 12:01p $*
  30. * *
  31. * $Revision:: 2 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * HTreeManagerClass::HTreeManagerClass -- constructor *
  36. * HTreeManagerClass::~HTreeManagerClass -- destructor *
  37. * HTreeManagerClass::Free -- de-allocate all memory in use *
  38. * HTreeManagerClass::Free_All_Trees -- de-allocates all hierarchy trees currently loaded *
  39. * HTreeManagerClass::Load_Tree -- load a hierarchy tree from a file *
  40. * HTreeManagerClass::Get_Tree_ID -- look up the ID of a named hierarchy tree *
  41. * HTreeManagerClass::Get_Tree -- get a pointer to the specified hierarchy tree *
  42. * HTreeManagerClass::Get_Tree -- get a pointer to the specified hierarchy tree *
  43. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  44. #include "htreemgr.h"
  45. #include <string.h>
  46. #include "htree.h"
  47. #include "chunkio.h"
  48. #include "wwmemlog.h"
  49. #include "w3dexclusionlist.h"
  50. /***********************************************************************************************
  51. * HTreeManagerClass::HTreeManagerClass -- constructor *
  52. * *
  53. * INPUT: *
  54. * *
  55. * OUTPUT: *
  56. * *
  57. * WARNINGS: *
  58. * *
  59. * HISTORY: *
  60. * 08/11/1997 GH : Created. *
  61. *=============================================================================================*/
  62. HTreeManagerClass::HTreeManagerClass(void) :
  63. NumTrees(0)
  64. {
  65. for (int treeidx=0; treeidx < MAX_TREES; treeidx++) {
  66. TreePtr[treeidx] = NULL;
  67. }
  68. }
  69. /***********************************************************************************************
  70. * HTreeManagerClass::~HTreeManagerClass -- destructor *
  71. * *
  72. * INPUT: *
  73. * *
  74. * OUTPUT: *
  75. * *
  76. * WARNINGS: *
  77. * *
  78. * HISTORY: *
  79. * 08/11/1997 GH : Created. *
  80. *=============================================================================================*/
  81. HTreeManagerClass::~HTreeManagerClass(void)
  82. {
  83. Free();
  84. }
  85. /***********************************************************************************************
  86. * HTreeManagerClass::Free -- de-allocate all memory in use *
  87. * *
  88. * INPUT: *
  89. * *
  90. * OUTPUT: *
  91. * *
  92. * WARNINGS: *
  93. * *
  94. * HISTORY: *
  95. * 08/11/1997 GH : Created. *
  96. *=============================================================================================*/
  97. void HTreeManagerClass::Free(void)
  98. {
  99. Free_All_Trees();
  100. }
  101. /***********************************************************************************************
  102. * HTreeManagerClass::Free_All_Trees -- de-allocates all hierarchy trees currently loaded *
  103. * *
  104. * INPUT: *
  105. * *
  106. * OUTPUT: *
  107. * *
  108. * WARNINGS: *
  109. * *
  110. * HISTORY: *
  111. * 08/11/1997 GH : Created. *
  112. *=============================================================================================*/
  113. void HTreeManagerClass::Free_All_Trees(void)
  114. {
  115. // Clear the hash table
  116. TreeHash.Remove_All();
  117. for (int treeidx=0; treeidx < MAX_TREES; treeidx++) {
  118. if (TreePtr[treeidx] != NULL) {
  119. delete TreePtr[treeidx];
  120. TreePtr[treeidx] = NULL;
  121. }
  122. }
  123. NumTrees = 0;
  124. }
  125. /***********************************************************************************************
  126. * HTreeManagerClass::Free_All_Trees_With_Exclusion_List -- de-allocates all trees not in list *
  127. * *
  128. * INPUT: *
  129. * *
  130. * OUTPUT: *
  131. * *
  132. * WARNINGS: *
  133. * *
  134. * HISTORY: *
  135. * 12/12/2002 GH : Created. *
  136. *=============================================================================================*/
  137. void HTreeManagerClass::Free_All_Trees_With_Exclusion_List(const W3DExclusionListClass & exclusion_list)
  138. {
  139. // For this system, since it is so simplistic, we simply loop over the array either deleting the tree
  140. // or copying it to the new tail index if it is excluded.
  141. int new_tail = 0;
  142. for (int treeidx=0; treeidx < MAX_TREES; treeidx++) {
  143. if (TreePtr[treeidx] != NULL) {
  144. if (exclusion_list.Is_Excluded(TreePtr[treeidx])) {
  145. //WWDEBUG_SAY(("excluding tree %s\n",TreePtr[treeidx]->Get_Name()));
  146. TreePtr[new_tail] = TreePtr[treeidx];
  147. new_tail++;
  148. } else {
  149. //WWDEBUG_SAY(("deleting tree %s\n",TreePtr[treeidx]->Get_Name()));
  150. delete TreePtr[treeidx];
  151. TreePtr[treeidx] = NULL;
  152. }
  153. }
  154. }
  155. NumTrees = new_tail;
  156. // Clear the hash table
  157. TreeHash.Remove_All();
  158. // Add back any trees that were not deleted
  159. for (treeidx=0; treeidx < new_tail; treeidx++)
  160. {
  161. // Insert to hash table for fast name based search
  162. StringClass lower_case_name(TreePtr[treeidx]->Get_Name(),true);
  163. _strlwr(lower_case_name.Peek_Buffer());
  164. TreeHash.Insert(lower_case_name,TreePtr[treeidx]);
  165. }
  166. }
  167. /***********************************************************************************************
  168. * HTreeManagerClass::Load_Tree -- load a hierarchy tree from a file *
  169. * *
  170. * INPUT: *
  171. * *
  172. * OUTPUT: *
  173. * *
  174. * WARNINGS: *
  175. * *
  176. * HISTORY: *
  177. * 08/11/1997 GH : Created. *
  178. *=============================================================================================*/
  179. int HTreeManagerClass::Load_Tree(ChunkLoadClass & cload)
  180. {
  181. WWMEMLOG(MEM_ANIMATION);
  182. HTreeClass * newtree = W3DNEW HTreeClass;
  183. if (newtree == NULL) {
  184. goto Error;
  185. }
  186. if (newtree->Load_W3D(cload) != HTreeClass::OK) {
  187. // load failed, delete and return error
  188. delete newtree;
  189. goto Error;
  190. } else if (Get_Tree_ID(newtree->Get_Name()) != -1) {
  191. // tree with this name already exists, reject it!
  192. delete newtree;
  193. goto Error;
  194. } else {
  195. // ok, accept this hierarchy tree!
  196. TreePtr[NumTrees] = newtree;
  197. NumTrees++;
  198. // Insert to hash table for fast name based search
  199. StringClass lower_case_name(newtree->Get_Name(),true);
  200. _strlwr(lower_case_name.Peek_Buffer());
  201. TreeHash.Insert(lower_case_name,newtree);
  202. }
  203. return 0;
  204. Error:
  205. return 1;
  206. }
  207. /***********************************************************************************************
  208. * HTreeManagerClass::Get_Tree_ID -- look up the ID of a named hierarchy tree *
  209. * *
  210. * INPUT: *
  211. * *
  212. * OUTPUT: *
  213. * *
  214. * WARNINGS: *
  215. * *
  216. * HISTORY: *
  217. * 08/11/1997 GH : Created. *
  218. *=============================================================================================*/
  219. int HTreeManagerClass::Get_Tree_ID(const char * name)
  220. {
  221. for (int i=0; i<NumTrees; i++) {
  222. if (TreePtr[i] && (stricmp(name,TreePtr[i]->Get_Name()) == 0)) {
  223. return i;
  224. }
  225. }
  226. return -1;
  227. }
  228. /***********************************************************************************************
  229. * HTreeManagerClass::Get_Tree_Name -- look up the name of a id'd hierarchy tree *
  230. * *
  231. * INPUT: *
  232. * *
  233. * OUTPUT: *
  234. * *
  235. * WARNINGS: *
  236. * *
  237. * HISTORY: *
  238. * 08/11/1997 GH : Created. *
  239. *=============================================================================================*/
  240. char *HTreeManagerClass::Get_Tree_Name(const int idx)
  241. {
  242. if ((idx < NumTrees) && TreePtr[idx]) {
  243. if (TreePtr[idx]) {
  244. return (char *)TreePtr[idx]->Get_Name();
  245. }
  246. }
  247. return NULL;
  248. }
  249. /***********************************************************************************************
  250. * HTreeManagerClass::Get_Tree -- get a pointer to the specified hierarchy tree *
  251. * *
  252. * INPUT: *
  253. * *
  254. * OUTPUT: *
  255. * *
  256. * WARNINGS: *
  257. * *
  258. * HISTORY: *
  259. * 08/11/1997 GH : Created. *
  260. *=============================================================================================*/
  261. HTreeClass * HTreeManagerClass::Get_Tree(const char * name)
  262. {
  263. StringClass lower_case_name(name,true);
  264. _strlwr(lower_case_name.Peek_Buffer());
  265. return TreeHash.Get(lower_case_name);
  266. // for (int i=0; i<NumTrees; i++) {
  267. // if (TreePtr[i] && (stricmp(name,TreePtr[i]->Get_Name()) == 0)) {
  268. //
  269. // return TreePtr[i];
  270. // }
  271. // }
  272. // return NULL;
  273. }
  274. /***********************************************************************************************
  275. * HTreeManagerClass::Get_Tree -- get a pointer to the specified hierarchy tree *
  276. * *
  277. * INPUT: *
  278. * *
  279. * OUTPUT: *
  280. * *
  281. * WARNINGS: *
  282. * *
  283. * HISTORY: *
  284. * 08/11/1997 GH : Created. *
  285. *=============================================================================================*/
  286. HTreeClass * HTreeManagerClass::Get_Tree(int id)
  287. {
  288. if ((id >= 0) && (id < NumTrees)) {
  289. return TreePtr[id];
  290. } else {
  291. return NULL;
  292. }
  293. }