nodelist.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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/Tools/pluglib/nodelist.cpp 8 1/02/01 6:31p Greg_h $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando / G *
  24. * *
  25. * File Name : NODELIST.CPP *
  26. * *
  27. * Programmer : Greg Hjelstrom *
  28. * *
  29. * Start Date : 06/09/97 *
  30. * *
  31. * Last Update : June 9, 1997 [GH] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * INodeListClass::INodeListClass -- Create an INodeList *
  36. * INodeListClass::~INodeListClass -- Delete the INode List *
  37. * INode * INodeListClass::operator[] -- Array-like access to the list members *
  38. * INodeListClass::callback -- callback function for MAX *
  39. * INodeListClass::INodeListClass -- A "copy" contstructor with filtering... *
  40. * INodeListClass::INodeListClass -- constructor *
  41. * INodeListClass::INodeListClass -- Constructor *
  42. * INodeListClass::Insert -- insert a list of nodes into this list *
  43. * INodeListClass::Insert -- Inserts an INode into the list *
  44. * INodeListClass::Add_Tree -- Add a tree of INodes to the list *
  45. * INodeListClass::Remove -- Remove the i'th element of the list *
  46. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  47. #include "nodelist.h"
  48. static AnyINodeFilter _AnyFilter;
  49. /*******************************************************************************
  50. * ListEntryClass
  51. *
  52. * Used to implement a linked list of INodes.
  53. *
  54. *******************************************************************************/
  55. class INodeListEntryClass
  56. {
  57. public:
  58. INodeListEntryClass(INode * n,TimeValue /*time*/) { Node = n; }
  59. ~INodeListEntryClass(void) {}
  60. INode * Node;
  61. INodeListEntryClass * Next;
  62. };
  63. /***********************************************************************************************
  64. * INodeListClass::INodeListClass -- Constructor *
  65. * *
  66. * INPUT: *
  67. * *
  68. * OUTPUT: *
  69. * *
  70. * WARNINGS: *
  71. * *
  72. * HISTORY: *
  73. * 07/02/1997 GH : Created. *
  74. *=============================================================================================*/
  75. INodeListClass::INodeListClass(TimeValue time,INodeFilterClass * inodefilter) :
  76. NumNodes(0),
  77. Time(time),
  78. ListHead(NULL),
  79. INodeFilter(inodefilter)
  80. {
  81. if (INodeFilter == NULL) {
  82. INodeFilter = &_AnyFilter;
  83. }
  84. }
  85. /***********************************************************************************************
  86. * INodeListClass::INodeListClass -- Create an INodeList *
  87. * *
  88. * INPUT: *
  89. * scene - 3dsMAX scene to enumerate *
  90. * time - time at which to create the list of INodes *
  91. * inodefilter - object which will accept or reject each INode in the scene *
  92. * *
  93. * OUTPUT: *
  94. * *
  95. * WARNINGS: *
  96. * *
  97. * HISTORY: *
  98. * 06/09/1997 GH : Created. *
  99. *=============================================================================================*/
  100. INodeListClass::INodeListClass(IScene * scene,TimeValue time,INodeFilterClass * inodefilter) :
  101. NumNodes(0),
  102. Time(time),
  103. ListHead(NULL),
  104. INodeFilter(inodefilter)
  105. {
  106. if (INodeFilter == NULL) {
  107. INodeFilter = &_AnyFilter;
  108. }
  109. scene->EnumTree(this);
  110. }
  111. /***********************************************************************************************
  112. * INodeListClass::INodeListClass -- Constructor *
  113. * *
  114. * INPUT: *
  115. * *
  116. * OUTPUT: *
  117. * *
  118. * WARNINGS: *
  119. * *
  120. * HISTORY: *
  121. * 1/13/98 GTH : Created. *
  122. *=============================================================================================*/
  123. INodeListClass::INodeListClass(INode * root,TimeValue time,INodeFilterClass * nodefilter) :
  124. NumNodes(0),
  125. Time(time),
  126. ListHead(NULL),
  127. INodeFilter(nodefilter)
  128. {
  129. if (INodeFilter == NULL) {
  130. INodeFilter = &_AnyFilter;
  131. }
  132. Add_Tree(root);
  133. }
  134. /***********************************************************************************************
  135. * INodeListClass::INodeListClass -- A "copy" contstructor with filtering... *
  136. * *
  137. * INPUT: *
  138. * *
  139. * OUTPUT: *
  140. * *
  141. * WARNINGS: *
  142. * *
  143. * HISTORY: *
  144. * 07/02/1997 GH : Created. *
  145. *=============================================================================================*/
  146. INodeListClass::INodeListClass(INodeListClass & copyfrom,TimeValue time,INodeFilterClass * inodefilter) :
  147. NumNodes(0),
  148. Time(time),
  149. ListHead(NULL),
  150. INodeFilter(inodefilter)
  151. {
  152. if (INodeFilter == NULL) {
  153. INodeFilter = &_AnyFilter;
  154. }
  155. for (unsigned i=0; i<copyfrom.Num_Nodes(); i++) {
  156. Insert(copyfrom[i]);
  157. }
  158. }
  159. /***********************************************************************************************
  160. * INodeListClass::~INodeListClass -- Delete the INode List *
  161. * *
  162. * INPUT: *
  163. * *
  164. * OUTPUT: *
  165. * *
  166. * WARNINGS: *
  167. * *
  168. * HISTORY: *
  169. * 06/09/1997 GH : Created. *
  170. *=============================================================================================*/
  171. INodeListClass::~INodeListClass(void)
  172. {
  173. while (ListHead)
  174. {
  175. INodeListEntryClass * next = ListHead->Next;
  176. delete ListHead;
  177. ListHead = next;
  178. }
  179. NumNodes = 0;
  180. ListHead = NULL;
  181. }
  182. /***********************************************************************************************
  183. * INode * INodeListClass::operator[] -- Array-like access to the list members *
  184. * *
  185. * INPUT: *
  186. * index - index of the list entry *
  187. * *
  188. * OUTPUT: *
  189. * pointer to an INode *
  190. * *
  191. * WARNINGS: *
  192. * *
  193. * HISTORY: *
  194. * 06/09/1997 GH : Created. *
  195. *=============================================================================================*/
  196. INode * INodeListClass::operator[] ( int index ) const
  197. {
  198. INodeListEntryClass * entry = ListHead;
  199. while (index > 0 && entry != NULL )
  200. {
  201. entry = entry->Next;
  202. index--;
  203. }
  204. return entry->Node;
  205. }
  206. /***********************************************************************************************
  207. * INodeListClass::Insert -- insert a list of nodes into this list *
  208. * *
  209. * INPUT: *
  210. * *
  211. * OUTPUT: *
  212. * *
  213. * WARNINGS: *
  214. * *
  215. * HISTORY: *
  216. * 1/14/98 GTH : Created. *
  217. *=============================================================================================*/
  218. void INodeListClass::Insert(INodeListClass & insertlist)
  219. {
  220. for (unsigned int i=0; i<insertlist.Num_Nodes(); i++) {
  221. Insert(insertlist[i]);
  222. }
  223. }
  224. /***********************************************************************************************
  225. * INodeListClass::Insert -- Inserts an INode into the list *
  226. * *
  227. * INPUT: *
  228. * *
  229. * OUTPUT: *
  230. * *
  231. * WARNINGS: *
  232. * *
  233. * HISTORY: *
  234. * 07/02/1997 GH : Created. *
  235. *=============================================================================================*/
  236. void INodeListClass::Insert(INode * node)
  237. {
  238. if (INodeFilter->Accept_Node(node,Time))
  239. {
  240. INodeListEntryClass * newentry = new INodeListEntryClass(node, Time);
  241. newentry->Next = ListHead;
  242. ListHead = newentry;
  243. NumNodes++;
  244. }
  245. }
  246. /***********************************************************************************************
  247. * INodeListClass::Remove -- Remove the i'th element of the list *
  248. * *
  249. * INPUT: *
  250. * *
  251. * OUTPUT: *
  252. * *
  253. * WARNINGS: *
  254. * *
  255. * HISTORY: *
  256. * 10/27/2000 gth : Created. *
  257. *=============================================================================================*/
  258. void INodeListClass::Remove(int i)
  259. {
  260. if ((i < 0) || (i > Num_Nodes())) {
  261. return;
  262. }
  263. INodeListEntryClass * prev = ListHead;
  264. while (i > 1) {
  265. prev = prev->Next;
  266. }
  267. INodeListEntryClass * deleteme = prev->Next;
  268. if (deleteme != NULL) {
  269. prev->Next = prev->Next->Next;
  270. delete deleteme;
  271. }
  272. }
  273. /***********************************************************************************************
  274. * INodeListClass::Add_Tree -- Add a tree of INodes to the list *
  275. * *
  276. * INPUT: *
  277. * *
  278. * OUTPUT: *
  279. * *
  280. * WARNINGS: *
  281. * *
  282. * HISTORY: *
  283. * 1/13/98 GTH : Created. *
  284. *=============================================================================================*/
  285. void INodeListClass::Add_Tree(INode * root)
  286. {
  287. if (root == NULL) return;
  288. Insert(root);
  289. for (int i=0; i<root->NumberOfChildren(); i++) {
  290. Add_Tree(root->GetChildNode(i));
  291. }
  292. }
  293. /***********************************************************************************************
  294. * INodeListClass::callback -- callback function for MAX *
  295. * *
  296. * 3dsMAX calls this function with a pointer to each INode in the scene. We keep a pointer *
  297. * to the ones we like. *
  298. * *
  299. * INPUT: *
  300. * *
  301. * OUTPUT: *
  302. * *
  303. * WARNINGS: *
  304. * *
  305. * HISTORY: *
  306. * 06/09/1997 GH : Created. *
  307. *=============================================================================================*/
  308. int INodeListClass::callback(INode * node)
  309. {
  310. Insert(node);
  311. return TREE_CONTINUE; // Keep on enumerating....
  312. }
  313. void INodeListClass::Sort(const INodeCompareClass & node_compare)
  314. {
  315. for (unsigned int i=0; i<Num_Nodes(); i++) {
  316. for (unsigned int j=0; j<Num_Nodes(); j++) {
  317. INodeListEntryClass * ni = get_nth_item(i);
  318. INodeListEntryClass * nj = get_nth_item(j);
  319. if (node_compare(ni->Node,nj->Node) > 0) {
  320. INode * tmp = ni->Node;
  321. ni->Node = nj->Node;
  322. nj->Node = tmp;
  323. }
  324. }
  325. }
  326. }
  327. INodeListEntryClass * INodeListClass::get_nth_item(int index)
  328. {
  329. INodeListEntryClass * entry = ListHead;
  330. while (index > 0 && entry != NULL )
  331. {
  332. entry = entry->Next;
  333. index--;
  334. }
  335. return entry;
  336. }