htreemgr.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. ** Command & Conquer Generals(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 1 1/22/01 3:36p Greg_h $ */
  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:: 1/08/01 10:04a $*
  30. * *
  31. * $Revision:: 1 $*
  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. for (int treeidx=0; treeidx < MAX_TREES; treeidx++) {
  116. if (TreePtr[treeidx] != NULL) {
  117. delete TreePtr[treeidx];
  118. TreePtr[treeidx] = NULL;
  119. }
  120. }
  121. NumTrees = 0;
  122. }
  123. /***********************************************************************************************
  124. * HTreeManagerClass::Free_All_Trees_With_Exclusion_List -- de-allocates all trees not in list *
  125. * *
  126. * INPUT: *
  127. * *
  128. * OUTPUT: *
  129. * *
  130. * WARNINGS: *
  131. * *
  132. * HISTORY: *
  133. * 12/12/2002 GH : Created. *
  134. *=============================================================================================*/
  135. void HTreeManagerClass::Free_All_Trees_With_Exclusion_List(const W3DExclusionListClass & exclusion_list)
  136. {
  137. // For this system, since it is so simplistic, we simply loop over the array either deleting the tree
  138. // or copying it to the new tail index if it is excluded.
  139. int new_tail = 0;
  140. for (int treeidx=0; treeidx < MAX_TREES; treeidx++) {
  141. if (TreePtr[treeidx] != NULL) {
  142. if (exclusion_list.Is_Excluded(TreePtr[treeidx])) {
  143. //WWDEBUG_SAY(("excluding tree %s\n",TreePtr[treeidx]->Get_Name()));
  144. TreePtr[new_tail] = TreePtr[treeidx];
  145. new_tail++;
  146. } else {
  147. //WWDEBUG_SAY(("deleting tree %s\n",TreePtr[treeidx]->Get_Name()));
  148. delete TreePtr[treeidx];
  149. TreePtr[treeidx] = NULL;
  150. }
  151. }
  152. }
  153. NumTrees = new_tail;
  154. }
  155. /***********************************************************************************************
  156. * HTreeManagerClass::Load_Tree -- load a hierarchy tree from a file *
  157. * *
  158. * INPUT: *
  159. * *
  160. * OUTPUT: *
  161. * *
  162. * WARNINGS: *
  163. * *
  164. * HISTORY: *
  165. * 08/11/1997 GH : Created. *
  166. *=============================================================================================*/
  167. int HTreeManagerClass::Load_Tree(ChunkLoadClass & cload)
  168. {
  169. WWMEMLOG(MEM_ANIMATION);
  170. HTreeClass * newtree = W3DNEW HTreeClass;
  171. if (newtree == NULL) {
  172. goto Error;
  173. }
  174. if (newtree->Load_W3D(cload) != HTreeClass::OK) {
  175. // load failed, delete and return error
  176. delete newtree;
  177. goto Error;
  178. } else if (Get_Tree_ID(newtree->Get_Name()) != -1) {
  179. // tree with this name already exists, reject it!
  180. delete newtree;
  181. goto Error;
  182. } else {
  183. // ok, accept this hierarchy tree!
  184. TreePtr[NumTrees] = newtree;
  185. NumTrees++;
  186. }
  187. return 0;
  188. Error:
  189. return 1;
  190. }
  191. /***********************************************************************************************
  192. * HTreeManagerClass::Get_Tree_ID -- look up the ID of a named hierarchy tree *
  193. * *
  194. * INPUT: *
  195. * *
  196. * OUTPUT: *
  197. * *
  198. * WARNINGS: *
  199. * *
  200. * HISTORY: *
  201. * 08/11/1997 GH : Created. *
  202. *=============================================================================================*/
  203. int HTreeManagerClass::Get_Tree_ID(const char * name)
  204. {
  205. for (int i=0; i<NumTrees; i++) {
  206. if (TreePtr[i] && (stricmp(name,TreePtr[i]->Get_Name()) == 0)) {
  207. return i;
  208. }
  209. }
  210. return -1;
  211. }
  212. /***********************************************************************************************
  213. * HTreeManagerClass::Get_Tree_Name -- look up the name of a id'd hierarchy tree *
  214. * *
  215. * INPUT: *
  216. * *
  217. * OUTPUT: *
  218. * *
  219. * WARNINGS: *
  220. * *
  221. * HISTORY: *
  222. * 08/11/1997 GH : Created. *
  223. *=============================================================================================*/
  224. char *HTreeManagerClass::Get_Tree_Name(const int idx)
  225. {
  226. if ((idx < NumTrees) && TreePtr[idx]) {
  227. if (TreePtr[idx]) {
  228. return (char *)TreePtr[idx]->Get_Name();
  229. }
  230. }
  231. return NULL;
  232. }
  233. /***********************************************************************************************
  234. * HTreeManagerClass::Get_Tree -- get a pointer to the specified hierarchy tree *
  235. * *
  236. * INPUT: *
  237. * *
  238. * OUTPUT: *
  239. * *
  240. * WARNINGS: *
  241. * *
  242. * HISTORY: *
  243. * 08/11/1997 GH : Created. *
  244. *=============================================================================================*/
  245. HTreeClass * HTreeManagerClass::Get_Tree(const char * name)
  246. {
  247. for (int i=0; i<NumTrees; i++) {
  248. if (TreePtr[i] && (stricmp(name,TreePtr[i]->Get_Name()) == 0)) {
  249. return TreePtr[i];
  250. }
  251. }
  252. return NULL;
  253. }
  254. /***********************************************************************************************
  255. * HTreeManagerClass::Get_Tree -- get a pointer to the specified hierarchy tree *
  256. * *
  257. * INPUT: *
  258. * *
  259. * OUTPUT: *
  260. * *
  261. * WARNINGS: *
  262. * *
  263. * HISTORY: *
  264. * 08/11/1997 GH : Created. *
  265. *=============================================================================================*/
  266. HTreeClass * HTreeManagerClass::Get_Tree(int id)
  267. {
  268. if ((id >= 0) && (id < NumTrees)) {
  269. return TreePtr[id];
  270. } else {
  271. return NULL;
  272. }
  273. }