util.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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/max2w3d/util.cpp 28 10/27/00 4:12p Greg_h $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando Tools - W3D export *
  24. * *
  25. * $Archive:: /Commando/Code/Tools/max2w3d/util.cpp $*
  26. * *
  27. * $Author:: Greg_h $*
  28. * *
  29. * $Modtime:: 10/27/00 1:13p $*
  30. * *
  31. * $Revision:: 28 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Cleanup_Orthogonal_Matrix -- removes very small numbers from the matrix *
  36. * Set_W3D_Name -- set a W3D name *
  37. * Split_Node_Name -- break a node name into the base and extension *
  38. * Is_Max_Tri_Mesh -- Is this node a triangle mesh? *
  39. * -- checks if the node is the origin of a model *
  40. * -- Checks if the node is the origin for the base obect (non-LOD'd). *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #include "util.h"
  43. #include "w3dutil.h"
  44. #include "skin.h"
  45. #include "skindata.h"
  46. #include "modstack.h"
  47. #define MAX_NODE_NAME_LEN 256 // max name size we can handle
  48. const float EPSILON = 0.00001f;
  49. static char _string[256];
  50. static int get_geometry_type(INode * node)
  51. {
  52. assert(node != NULL);
  53. return W3DAppData2Struct::Get_App_Data(node)->Get_Geometry_Type();
  54. //return (get_w3d_bits(node) & GEO_TYPE_MASK);
  55. }
  56. /***********************************************************************************************
  57. * Cleanup_Orthogonal_Matrix -- removes very small numbers from the matrix *
  58. * *
  59. * INPUT: *
  60. * *
  61. * OUTPUT: *
  62. * *
  63. * WARNINGS: *
  64. * *
  65. * HISTORY: *
  66. * 10/26/1997 GH : Created. *
  67. *=============================================================================================*/
  68. Matrix3 Cleanup_Orthogonal_Matrix(Matrix3 & mat)
  69. {
  70. Matrix3 newmat = mat;
  71. for (int j=0; j<3; j++) {
  72. Point3 row = newmat.GetRow(j);
  73. if (fabs(row.x) < EPSILON) row.x = 0.0f;
  74. if (fabs(row.y) < EPSILON) row.y = 0.0f;
  75. if (fabs(row.z) < EPSILON) row.z = 0.0f;
  76. row = Normalize(row);
  77. newmat.SetRow(j,row);
  78. }
  79. return newmat;
  80. }
  81. /***********************************************************************************************
  82. * Set_W3D_Name -- set a W3D name *
  83. * *
  84. * INPUT: *
  85. * *
  86. * OUTPUT: *
  87. * *
  88. * WARNINGS: *
  89. * *
  90. * HISTORY: *
  91. * 10/26/1997 GH : Created. *
  92. * 9/13/1999 AJA : Strip off the trailing ".digits" since this is a convention we've set in *
  93. * MAX to help artists manage LODs. *
  94. *=============================================================================================*/
  95. void Set_W3D_Name(char * set_name,const char * src)
  96. {
  97. memset(set_name,0,W3D_NAME_LEN);
  98. strncpy(set_name,src,W3D_NAME_LEN-1);
  99. char *dot = strrchr(set_name, '.');
  100. if (dot)
  101. {
  102. // If a number comes after the dot, strip it off
  103. int value;
  104. if (sscanf(dot+1, "%d", &value) == 1)
  105. *dot = 0;
  106. // If nothing comes after the dot, strip it off
  107. else if (*(dot+1) == 0)
  108. *dot = 0;
  109. }
  110. strupr(set_name);
  111. }
  112. /***********************************************************************************************
  113. * Split_Node_Name -- break a node name into the base and extension *
  114. * *
  115. * INPUT: *
  116. * *
  117. * OUTPUT: *
  118. * *
  119. * WARNINGS: *
  120. * *
  121. * HISTORY: *
  122. * 10/26/1997 GH : Created. *
  123. *=============================================================================================*/
  124. void Split_Node_Name(const char * name,char * set_base,char * set_exten,int * set_exten_index)
  125. {
  126. // Nodes are assumed to be named in the following way:
  127. // <name>.<exten character><exten digits>
  128. // for example: mesh.d1
  129. char buf[MAX_NODE_NAME_LEN];
  130. char * ptr;
  131. assert(strlen(name) < MAX_NODE_NAME_LEN);
  132. // Initialize
  133. if (set_base != NULL) set_base[0] = 0;
  134. if (set_exten != NULL) set_exten[0] = 0;
  135. if (set_exten_index != NULL) *set_exten_index = 0;
  136. // Get the base name
  137. strncpy(buf,name,MAX_NODE_NAME_LEN);
  138. ptr = buf;
  139. while ((*ptr != 0) && (*ptr != '.')) {
  140. ptr++;
  141. }
  142. if (*ptr == '.') {
  143. // copy what we have so far into set_base
  144. *ptr = 0;
  145. if (set_base != NULL) strncpy(set_base,buf,MAX_NODE_NAME_LEN);
  146. // copy the rest back into the extension
  147. ptr++;
  148. if (set_exten != NULL) strncpy(set_exten,ptr,MAX_NODE_NAME_LEN);
  149. // now get the extension index
  150. ptr++;
  151. if (set_exten_index != NULL) *set_exten_index = atoi(ptr);
  152. } else {
  153. // no extension, just copy the base name
  154. if (set_base != NULL) strncpy(set_base,buf,MAX_NODE_NAME_LEN);
  155. return;
  156. }
  157. }
  158. bool Append_Lod_Character (char *meshname, int lod_level, INodeListClass *origin_list)
  159. {
  160. if (meshname == NULL || lod_level < 0)
  161. return false;
  162. if (!origin_list)
  163. return false;
  164. int num_lods = origin_list->Num_Nodes();
  165. /*
  166. ** Search the other LODs to see if there is a mesh with the same name.
  167. ** If there is, we will append the current LOD level digit to the name.
  168. ** If there is not, the name will not be modified.
  169. */
  170. INode *conflict = NULL, *cur_origin = NULL;
  171. int i, lod;
  172. for (i = 0; i < num_lods; i++)
  173. {
  174. // Don't bother searching the current LOD.
  175. lod = Get_Lod_Level((*origin_list)[i]);
  176. if (lod == lod_level)
  177. continue;
  178. // Search this lod for a node of the same name.
  179. conflict = Find_Named_Node(meshname, (*origin_list)[i]);
  180. if (conflict)
  181. {
  182. // Name length is a worry here, because the name plus the number
  183. // must be less than W3D_NAME_LEN (which is fairly small!).
  184. int length = strlen(meshname);
  185. if ( (lod_level < 10) && (length < W3D_NAME_LEN - 1) )
  186. {
  187. // Append a number corresponding to the LOD level to the mesh name.
  188. // Highest-detail LOD is '0' (convention).
  189. char *insert = meshname + length;
  190. *insert++ = '0' + lod_level;
  191. *insert = '\0';
  192. }
  193. else if ( (lod_level < 100) && (length < W3D_NAME_LEN - 2) )
  194. {
  195. // Append a number corresponding to the LOD level to the mesh name.
  196. // Highest-detail LOD is '0' (convention).
  197. char buf[3];
  198. sprintf(buf, "%d", lod_level);
  199. strcat(meshname, buf);
  200. }
  201. else
  202. {
  203. // Replace the last character of the mesh name with the lod character (as above).
  204. meshname[W3D_NAME_LEN-2] = '0' + lod_level;
  205. }
  206. // Name mangling finished (conflict with other LODs is solved on their pass).
  207. break;
  208. }
  209. }
  210. return true;
  211. }
  212. void Create_Full_Path(char *full_path, const char *curr, const char *rel_path)
  213. {
  214. // Copy current dir to full path. If it doesn't end with a slash, add one.
  215. strcpy(full_path, curr);
  216. int curr_len = strlen(curr);
  217. char *full_p = full_path + curr_len; // Point at the terminating NULL
  218. if (curr_len == 0 ||(*(full_p - 1) != '/' && *(full_p - 1) != '\\')) {
  219. *full_p = '\\';
  220. *(++full_p) = '\000'; // Point at the terminating NULL
  221. }
  222. // Scan "..\"s at the beginning of the rel path, scan backwards on the
  223. // full path (current dir):
  224. const char *rel_p;
  225. for ( rel_p = rel_path;
  226. *rel_p == '.' && *(rel_p+1) == '.' && ( *(rel_p+2) == '/' || *(rel_p+2) == '\\' );
  227. rel_p += 3)
  228. {
  229. full_p--;
  230. for (; full_p > full_path && *(full_p-1) != '/' && *(full_p-1) != '\\'; full_p--);
  231. *full_p = '\000';
  232. }
  233. // Copy the remainder of the relative path to the full path:
  234. strcpy(full_p, rel_p);
  235. }
  236. // This enum is used inside Create_Relative_Path:
  237. enum PathCharType {
  238. NULL_CHAR,
  239. SLASH_CHAR,
  240. PLAIN_CHAR
  241. };
  242. void Create_Relative_Path(char *rel_path, const char *curr, const char *full_path)
  243. {
  244. // Copy both constant strings and convert them to uppercase:
  245. int curr_len = strlen(curr);
  246. char *up_curr = (char *)malloc(curr_len + 1);
  247. strcpy(up_curr, curr);
  248. _strupr(up_curr);
  249. int full_len = strlen(full_path);
  250. char *up_full = (char *)malloc(full_len + 1);
  251. strcpy(up_full, full_path);
  252. _strupr(up_full);
  253. char *rel_p = rel_path;
  254. // Find shared prefix of curr and full path
  255. const char *full_p = up_full;
  256. const char *curr_p = up_curr;
  257. for ( ;
  258. *full_p && *full_p == *curr_p || (*full_p == '/' && *curr_p == '\\') || (*full_p == '\\' && *curr_p == '/');
  259. full_p++, curr_p++
  260. );
  261. // If no shared prefix at this point set the relative path to 0
  262. // This will force the code to use the absolute path.
  263. if (full_p == up_full) {
  264. rel_path[0] = 0;
  265. goto end;
  266. }
  267. // The first different character for each string can be: a NULL, a slash,
  268. // or an ordinary character.
  269. PathCharType full_type, curr_type;
  270. if (*full_p == '\000') {
  271. full_type = NULL_CHAR;
  272. } else {
  273. if (*full_p == '/' || *full_p == '\\') {
  274. full_type = SLASH_CHAR;
  275. } else {
  276. full_type = PLAIN_CHAR;
  277. }
  278. }
  279. if (*curr_p == '\000') {
  280. curr_type = NULL_CHAR;
  281. } else {
  282. if (*curr_p == '/' || *curr_p == '\\') {
  283. curr_type = SLASH_CHAR;
  284. } else {
  285. curr_type = PLAIN_CHAR;
  286. }
  287. }
  288. // If the last fullpath char is a NULL or both are slashes, we have an
  289. // error - return full path
  290. if (full_type == NULL_CHAR || (full_type == SLASH_CHAR && curr_type == SLASH_CHAR)) {
  291. strcpy(rel_path, up_full);
  292. goto end;
  293. }
  294. // If the current path has ended (last char is a NULL) and the full path's
  295. // last char is a slash, then just copy the remainder of the full path
  296. // (w/o the slash) to the relative path, and exit.
  297. if (curr_type == NULL_CHAR && full_type == SLASH_CHAR) {
  298. full_p++; // skip slash
  299. strcpy(rel_path, full_p);
  300. goto end;
  301. }
  302. // If one of following holds:
  303. // 1) One of the last chars is a slash and the other is a plain char
  304. // 2) The current path has ended (last char is NULL) and the last char
  305. // of the full path is a plain char
  306. // 3) The last char of both are plain chars and the previous char is not a
  307. // slash
  308. // Then we must backtrack both pointers until the characters before them
  309. // are slashes. If there are no previous slashes, we have an error.
  310. if ( (full_type == SLASH_CHAR && curr_type == PLAIN_CHAR) ||
  311. (curr_type == SLASH_CHAR && full_type == PLAIN_CHAR) ||
  312. (curr_type == NULL_CHAR && full_type == PLAIN_CHAR) ||
  313. (curr_type == PLAIN_CHAR && full_type == PLAIN_CHAR &&
  314. *(full_p-1) != '/' && *(full_p-1) != '\\') )
  315. {
  316. for (;
  317. full_p > up_full &&
  318. ( (*(full_p - 1) != '/' && *(full_p - 1) != '\\') ||
  319. (*(curr_p - 1) != '/' && *(curr_p - 1) != '\\') );
  320. full_p--, curr_p--);
  321. }
  322. // If no shared prefix at this point (not even a drive letter) return the
  323. // full path
  324. if (full_p == up_full) {
  325. strcpy(rel_path, up_full);
  326. goto end;
  327. }
  328. // Scan all directories levels in current path from shared point to end -
  329. // for each one add a "../" to the relative path. Note that at this point
  330. // we know we have to add at least one.
  331. *rel_p++ = '.';
  332. *rel_p++ = '.';
  333. *rel_p++ = '\\';
  334. // Go over remaining current path, for each slash we find add one "../" to
  335. // the relative path
  336. for (; *curr_p; curr_p++) {
  337. if (*curr_p == '/' || *curr_p == '\\') {
  338. *rel_p++ = '.';
  339. *rel_p++ = '.';
  340. *rel_p++ = '\\';
  341. }
  342. }
  343. // If the last char of the current path is a slash remove a "../" from the
  344. // relative path.
  345. if (*(curr_p - 1) == '/' || *(curr_p - 1) == '\\') {
  346. rel_p -= 3;
  347. }
  348. // Copy remaining full path (from shared point to end) to relative path
  349. strcpy(rel_p, full_p);
  350. end:
  351. free(up_curr);
  352. free(up_full);
  353. }
  354. bool Is_Full_Path(char * path)
  355. {
  356. // first scan for a drive letter (scan for a colon)
  357. if (strchr(path,':') != NULL) {
  358. return true;
  359. }
  360. // now scan for a "network" path (starts with "//")
  361. if ((path[0] == '/') && (path[1] == '/')) {
  362. return true;
  363. }
  364. if ((path[0] == '\\') && (path[1] == '\\')) {
  365. return true;
  366. }
  367. return false;
  368. }
  369. /***********************************************************************************************
  370. * Is_Max_Tri_Mesh -- Is this node a triangle mesh? *
  371. * *
  372. * INPUT: *
  373. * *
  374. * OUTPUT: *
  375. * *
  376. * WARNINGS: *
  377. * *
  378. * HISTORY: *
  379. * 2/2/98 GTH : Created. *
  380. *=============================================================================================*/
  381. bool Is_Max_Tri_Mesh(INode * node)
  382. {
  383. Object *obj = node->EvalWorldState(0).obj;
  384. if (obj && obj->CanConvertToType(Class_ID(TRIOBJ_CLASS_ID, 0))) {
  385. return true;
  386. }
  387. return false;
  388. }
  389. bool Is_Damage_Root(INode *node)
  390. {
  391. if (node == NULL)
  392. return false;
  393. // Is the node's parent the scene root?
  394. INode *parent = node->GetParentNode();
  395. if (!parent || !parent->IsRootNode())
  396. return false;
  397. // Is the node's name in the form "damage.*"?
  398. char *name = node->GetName();
  399. if (strnicmp(name, "damage.", strlen("damage.")) != 0)
  400. return false;
  401. /* This won't pick up references to a dummy object for some reason.
  402. // Does the node point to a dummy object?
  403. Object *obj = node->GetObjectRef();
  404. if (!obj || !obj->CanConvertToType(Class_ID(DUMMY_CLASS_ID, 0)))
  405. return false;
  406. */
  407. return true;
  408. }
  409. /***********************************************************************************************
  410. * Is_Origin -- checks if the node is the origin of a model *
  411. * *
  412. * A node is an origin if its parent is the scene root, it is a dummy object, and its name *
  413. * is of the form "origin.*" (case insensitive). All descendants of the origin will be *
  414. * expressed in coordinates relative to the origin object. *
  415. * *
  416. * INPUT: *
  417. * *
  418. * OUTPUT: *
  419. * *
  420. * WARNINGS: *
  421. * *
  422. * HISTORY: *
  423. * 9/13/99 AJA : Created.
  424. *=============================================================================================*/
  425. bool Is_Origin(INode * node)
  426. {
  427. if (!node) return false;
  428. if (node->IsRootNode()) return true;
  429. if (node->IsHidden()) return false;
  430. // Is the node's parent the scene root?
  431. INode *parent = node->GetParentNode();
  432. if (!parent || !parent->IsRootNode())
  433. return false;
  434. // Is the node's name in the form "origin.*"?
  435. char *name = node->GetName();
  436. if (strnicmp(name, "origin.", strlen("origin.")) != 0)
  437. return false;
  438. /* This won't pick up references to a dummy object for some reason.
  439. // Does the node point to a dummy object?
  440. Object *obj = node->GetObjectRef();
  441. if (!obj || !obj->CanConvertToType(Class_ID(DUMMY_CLASS_ID, 0)))
  442. return false;
  443. */
  444. // This is an origin.
  445. return true;
  446. }
  447. /***********************************************************************************************
  448. * Is_Base_Origin -- Checks if the node is the origin for the base obect (non-LOD'd). *
  449. * *
  450. * INPUT: *
  451. * *
  452. * OUTPUT: *
  453. * *
  454. * WARNINGS: *
  455. * *
  456. * HISTORY: *
  457. * 9/13/1999 AJA : Created. *
  458. *=============================================================================================*/
  459. bool Is_Base_Origin(INode * node)
  460. {
  461. if (!node) return false;
  462. if (node->IsRootNode()) return true;
  463. if (!Is_Origin(node)) return false;
  464. // An origin is the base object origin if it's name is "origin." or
  465. // "origin.0" (a numeric value evaluating to zero as scanned by sscanf
  466. // which would include "origin.00" "origin.000", etc.).
  467. bool is_base_origin = false;
  468. char *name = node->GetName();
  469. if (stricmp(name, "origin.") == 0)
  470. is_base_origin = true;
  471. else if (strlen(name) > strlen("origin."))
  472. {
  473. // We know the first 7 characters are "origin." because that
  474. // was tested in Is_Origin(). Is it "origin.0"?
  475. int idx;
  476. if ((sscanf(name+strlen("origin."), "%d", &idx) == 1) && (idx == 0))
  477. is_base_origin = true;
  478. }
  479. return is_base_origin;
  480. }
  481. int Get_Lod_Level(INode *node)
  482. {
  483. if (node->IsRootNode()) return 0;
  484. if (!Is_Origin(node)) return -1;
  485. char *name = node->GetName();
  486. char *dot = strrchr(name, '.');
  487. assert(dot);
  488. return atoi(dot+1);
  489. }
  490. int Get_Damage_State(INode *node)
  491. {
  492. if (!Is_Damage_Root(node)) return -1;
  493. char *name = node->GetName();
  494. char *dot = strrchr(name, '.');
  495. assert(dot);
  496. return atoi(dot+1);
  497. }
  498. INode *Find_Named_Node(char *nodename, INode *root)
  499. {
  500. if (!root || !nodename)
  501. return NULL;
  502. // Perform a breadth-first search of the tree for a node
  503. // of the given name.
  504. INode *child = NULL;
  505. int i;
  506. char cur_name[W3D_NAME_LEN];
  507. // Is this the node we're looking for?
  508. Set_W3D_Name(cur_name, root->GetName());
  509. if (strcmp(cur_name, nodename) == 0)
  510. return root;
  511. // Check the children against the given name.
  512. for (i = 0; i < root->NumChildren(); i++)
  513. {
  514. // Is it this child?
  515. child = root->GetChildNode(i);
  516. Set_W3D_Name(cur_name, child->GetName());
  517. if (strcmp(nodename, cur_name) == 0)
  518. return child;
  519. }
  520. // Wasn't any children. Check each child's descendants.
  521. for (i = 0; i < root->NumChildren(); i++)
  522. {
  523. child = root->GetChildNode(i);
  524. INode *found = Find_Named_Node(nodename, child);
  525. if (found)
  526. return found;
  527. }
  528. // Didn't find the node anywhere.
  529. return NULL;
  530. }