htreemgr.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. ** Command & Conquer Renegade(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. /***********************************************************************************************
  50. * HTreeManagerClass::HTreeManagerClass -- constructor *
  51. * *
  52. * INPUT: *
  53. * *
  54. * OUTPUT: *
  55. * *
  56. * WARNINGS: *
  57. * *
  58. * HISTORY: *
  59. * 08/11/1997 GH : Created. *
  60. *=============================================================================================*/
  61. HTreeManagerClass::HTreeManagerClass(void) :
  62. NumTrees(0)
  63. {
  64. for (int treeidx=0; treeidx < MAX_TREES; treeidx++) {
  65. TreePtr[treeidx] = NULL;
  66. }
  67. }
  68. /***********************************************************************************************
  69. * HTreeManagerClass::~HTreeManagerClass -- destructor *
  70. * *
  71. * INPUT: *
  72. * *
  73. * OUTPUT: *
  74. * *
  75. * WARNINGS: *
  76. * *
  77. * HISTORY: *
  78. * 08/11/1997 GH : Created. *
  79. *=============================================================================================*/
  80. HTreeManagerClass::~HTreeManagerClass(void)
  81. {
  82. Free();
  83. }
  84. /***********************************************************************************************
  85. * HTreeManagerClass::Free -- de-allocate all memory in use *
  86. * *
  87. * INPUT: *
  88. * *
  89. * OUTPUT: *
  90. * *
  91. * WARNINGS: *
  92. * *
  93. * HISTORY: *
  94. * 08/11/1997 GH : Created. *
  95. *=============================================================================================*/
  96. void HTreeManagerClass::Free(void)
  97. {
  98. Free_All_Trees();
  99. }
  100. /***********************************************************************************************
  101. * HTreeManagerClass::Free_All_Trees -- de-allocates all hierarchy trees currently loaded *
  102. * *
  103. * INPUT: *
  104. * *
  105. * OUTPUT: *
  106. * *
  107. * WARNINGS: *
  108. * *
  109. * HISTORY: *
  110. * 08/11/1997 GH : Created. *
  111. *=============================================================================================*/
  112. void HTreeManagerClass::Free_All_Trees(void)
  113. {
  114. // Clear the hash table
  115. TreeHash.Remove_All();
  116. for (int treeidx=0; treeidx < MAX_TREES; treeidx++) {
  117. if (TreePtr[treeidx] != NULL) {
  118. delete TreePtr[treeidx];
  119. TreePtr[treeidx] = NULL;
  120. }
  121. }
  122. NumTrees = 0;
  123. }
  124. /***********************************************************************************************
  125. * HTreeManagerClass::Load_Tree -- load a hierarchy tree from a file *
  126. * *
  127. * INPUT: *
  128. * *
  129. * OUTPUT: *
  130. * *
  131. * WARNINGS: *
  132. * *
  133. * HISTORY: *
  134. * 08/11/1997 GH : Created. *
  135. *=============================================================================================*/
  136. int HTreeManagerClass::Load_Tree(ChunkLoadClass & cload)
  137. {
  138. WWMEMLOG(MEM_ANIMATION);
  139. HTreeClass * newtree = new HTreeClass;
  140. if (newtree == NULL) {
  141. goto Error;
  142. }
  143. if (newtree->Load_W3D(cload) != HTreeClass::OK) {
  144. // load failed, delete and return error
  145. delete newtree;
  146. goto Error;
  147. } else if (Get_Tree_ID(newtree->Get_Name()) != -1) {
  148. // tree with this name already exists, reject it!
  149. delete newtree;
  150. goto Error;
  151. } else {
  152. // ok, accept this hierarchy tree!
  153. TreePtr[NumTrees] = newtree;
  154. NumTrees++;
  155. // Insert to hash table for fast name based search
  156. StringClass lower_case_name(newtree->Get_Name(),true);
  157. _strlwr(lower_case_name.Peek_Buffer());
  158. TreeHash.Insert(lower_case_name,newtree);
  159. }
  160. return 0;
  161. Error:
  162. return 1;
  163. }
  164. /***********************************************************************************************
  165. * HTreeManagerClass::Get_Tree_ID -- look up the ID of a named hierarchy tree *
  166. * *
  167. * INPUT: *
  168. * *
  169. * OUTPUT: *
  170. * *
  171. * WARNINGS: *
  172. * *
  173. * HISTORY: *
  174. * 08/11/1997 GH : Created. *
  175. *=============================================================================================*/
  176. int HTreeManagerClass::Get_Tree_ID(const char * name)
  177. {
  178. for (int i=0; i<NumTrees; i++) {
  179. if (TreePtr[i] && (stricmp(name,TreePtr[i]->Get_Name()) == 0)) {
  180. return i;
  181. }
  182. }
  183. return -1;
  184. }
  185. /***********************************************************************************************
  186. * HTreeManagerClass::Get_Tree_Name -- look up the name of a id'd hierarchy tree *
  187. * *
  188. * INPUT: *
  189. * *
  190. * OUTPUT: *
  191. * *
  192. * WARNINGS: *
  193. * *
  194. * HISTORY: *
  195. * 08/11/1997 GH : Created. *
  196. *=============================================================================================*/
  197. char *HTreeManagerClass::Get_Tree_Name(const int idx)
  198. {
  199. if ((idx < NumTrees) && TreePtr[idx]) {
  200. if (TreePtr[idx]) {
  201. return (char *)TreePtr[idx]->Get_Name();
  202. }
  203. }
  204. return NULL;
  205. }
  206. /***********************************************************************************************
  207. * HTreeManagerClass::Get_Tree -- get a pointer to the specified hierarchy tree *
  208. * *
  209. * INPUT: *
  210. * *
  211. * OUTPUT: *
  212. * *
  213. * WARNINGS: *
  214. * *
  215. * HISTORY: *
  216. * 08/11/1997 GH : Created. *
  217. *=============================================================================================*/
  218. HTreeClass * HTreeManagerClass::Get_Tree(const char * name)
  219. {
  220. StringClass lower_case_name(name,true);
  221. _strlwr(lower_case_name.Peek_Buffer());
  222. return TreeHash.Get(lower_case_name);
  223. // for (int i=0; i<NumTrees; i++) {
  224. // if (TreePtr[i] && (stricmp(name,TreePtr[i]->Get_Name()) == 0)) {
  225. //
  226. // return TreePtr[i];
  227. // }
  228. // }
  229. // return NULL;
  230. }
  231. /***********************************************************************************************
  232. * HTreeManagerClass::Get_Tree -- get a pointer to the specified hierarchy tree *
  233. * *
  234. * INPUT: *
  235. * *
  236. * OUTPUT: *
  237. * *
  238. * WARNINGS: *
  239. * *
  240. * HISTORY: *
  241. * 08/11/1997 GH : Created. *
  242. *=============================================================================================*/
  243. HTreeClass * HTreeManagerClass::Get_Tree(int id)
  244. {
  245. if ((id >= 0) && (id < NumTrees)) {
  246. return TreePtr[id];
  247. } else {
  248. return NULL;
  249. }
  250. }