l_bsp_q2.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "l_cmd.h"
  19. #include "l_math.h"
  20. #include "l_mem.h"
  21. #include "l_log.h"
  22. #include "l_poly.h"
  23. #include "../botlib/l_script.h"
  24. #include "q2files.h"
  25. #include "l_bsp_q2.h"
  26. #include "l_bsp_ent.h"
  27. #define q2_dmodel_t dmodel_t
  28. #define q2_lump_t lump_t
  29. #define q2_dheader_t dheader_t
  30. #define q2_dmodel_t dmodel_t
  31. #define q2_dvertex_t dvertex_t
  32. #define q2_dplane_t dplane_t
  33. #define q2_dnode_t dnode_t
  34. #define q2_texinfo_t texinfo_t
  35. #define q2_dedge_t dedge_t
  36. #define q2_dface_t dface_t
  37. #define q2_dleaf_t dleaf_t
  38. #define q2_dbrushside_t dbrushside_t
  39. #define q2_dbrush_t dbrush_t
  40. #define q2_dvis_t dvis_t
  41. #define q2_dareaportal_t dareaportal_t
  42. #define q2_darea_t darea_t
  43. #define q2_nummodels nummodels
  44. #define q2_dmodels dmodels
  45. #define q2_numleafs numleafs
  46. #define q2_dleafs dleafs
  47. #define q2_numplanes numplanes
  48. #define q2_dplanes dplanes
  49. #define q2_numvertexes numvertexes
  50. #define q2_dvertexes dvertexes
  51. #define q2_numnodes numnodes
  52. #define q2_dnodes dnodes
  53. #define q2_numtexinfo numtexinfo
  54. #define q2_texinfo texinfo
  55. #define q2_numfaces numfaces
  56. #define q2_dfaces dfaces
  57. #define q2_numedges numedges
  58. #define q2_dedges dedges
  59. #define q2_numleaffaces numleaffaces
  60. #define q2_dleaffaces dleaffaces
  61. #define q2_numleafbrushes numleafbrushes
  62. #define q2_dleafbrushes dleafbrushes
  63. #define q2_dsurfedges dsurfedges
  64. #define q2_numbrushes numbrushes
  65. #define q2_dbrushes dbrushes
  66. #define q2_numbrushsides numbrushsides
  67. #define q2_dbrushsides dbrushsides
  68. #define q2_numareas numareas
  69. #define q2_dareas dareas
  70. #define q2_numareaportals numareaportals
  71. #define q2_dareaportals dareaportals
  72. void GetLeafNums (void);
  73. //=============================================================================
  74. int nummodels;
  75. dmodel_t *dmodels;//[MAX_MAP_MODELS];
  76. int visdatasize;
  77. byte *dvisdata;//[MAX_MAP_VISIBILITY];
  78. dvis_t *dvis;// = (dvis_t *)dvisdata;
  79. int lightdatasize;
  80. byte *dlightdata;//[MAX_MAP_LIGHTING];
  81. int entdatasize;
  82. char *dentdata;//[MAX_MAP_ENTSTRING];
  83. int numleafs;
  84. dleaf_t *dleafs;//[MAX_MAP_LEAFS];
  85. int numplanes;
  86. dplane_t *dplanes;//[MAX_MAP_PLANES];
  87. int numvertexes;
  88. dvertex_t *dvertexes;//[MAX_MAP_VERTS];
  89. int numnodes;
  90. dnode_t *dnodes;//[MAX_MAP_NODES];
  91. //NOTE: must be static for q2 .map to q2 .bsp
  92. int numtexinfo;
  93. texinfo_t texinfo[MAX_MAP_TEXINFO];
  94. int numfaces;
  95. dface_t *dfaces;//[MAX_MAP_FACES];
  96. int numedges;
  97. dedge_t *dedges;//[MAX_MAP_EDGES];
  98. int numleaffaces;
  99. unsigned short *dleaffaces;//[MAX_MAP_LEAFFACES];
  100. int numleafbrushes;
  101. unsigned short *dleafbrushes;//[MAX_MAP_LEAFBRUSHES];
  102. int numsurfedges;
  103. int *dsurfedges;//[MAX_MAP_SURFEDGES];
  104. int numbrushes;
  105. dbrush_t *dbrushes;//[MAX_MAP_BRUSHES];
  106. int numbrushsides;
  107. dbrushside_t *dbrushsides;//[MAX_MAP_BRUSHSIDES];
  108. int numareas;
  109. darea_t *dareas;//[MAX_MAP_AREAS];
  110. int numareaportals;
  111. dareaportal_t *dareaportals;//[MAX_MAP_AREAPORTALS];
  112. #define MAX_MAP_DPOP 256
  113. byte dpop[MAX_MAP_DPOP];
  114. //
  115. char brushsidetextured[MAX_MAP_BRUSHSIDES];
  116. //#ifdef ME
  117. int bspallocated = false;
  118. int allocatedbspmem = 0;
  119. void Q2_AllocMaxBSP(void)
  120. {
  121. //models
  122. nummodels = 0;
  123. dmodels = (dmodel_t *) GetClearedMemory(MAX_MAP_MODELS * sizeof(dmodel_t));
  124. allocatedbspmem += MAX_MAP_MODELS * sizeof(dmodel_t);
  125. //vis data
  126. visdatasize = 0;
  127. dvisdata = (byte *) GetClearedMemory(MAX_MAP_VISIBILITY * sizeof(byte));
  128. dvis = (dvis_t *) dvisdata;
  129. allocatedbspmem += MAX_MAP_VISIBILITY * sizeof(byte);
  130. //light data
  131. lightdatasize = 0;
  132. dlightdata = (byte *) GetClearedMemory(MAX_MAP_LIGHTING * sizeof(byte));
  133. allocatedbspmem += MAX_MAP_LIGHTING * sizeof(byte);
  134. //entity data
  135. entdatasize = 0;
  136. dentdata = (char *) GetClearedMemory(MAX_MAP_ENTSTRING * sizeof(char));
  137. allocatedbspmem += MAX_MAP_ENTSTRING * sizeof(char);
  138. //leafs
  139. numleafs = 0;
  140. dleafs = (dleaf_t *) GetClearedMemory(MAX_MAP_LEAFS * sizeof(dleaf_t));
  141. allocatedbspmem += MAX_MAP_LEAFS * sizeof(dleaf_t);
  142. //planes
  143. numplanes = 0;
  144. dplanes = (dplane_t *) GetClearedMemory(MAX_MAP_PLANES * sizeof(dplane_t));
  145. allocatedbspmem += MAX_MAP_PLANES * sizeof(dplane_t);
  146. //vertexes
  147. numvertexes = 0;
  148. dvertexes = (dvertex_t *) GetClearedMemory(MAX_MAP_VERTS * sizeof(dvertex_t));
  149. allocatedbspmem += MAX_MAP_VERTS * sizeof(dvertex_t);
  150. //nodes
  151. numnodes = 0;
  152. dnodes = (dnode_t *) GetClearedMemory(MAX_MAP_NODES * sizeof(dnode_t));
  153. allocatedbspmem += MAX_MAP_NODES * sizeof(dnode_t);
  154. /*
  155. //texture info
  156. numtexinfo = 0;
  157. texinfo = (texinfo_t *) GetClearedMemory(MAX_MAP_TEXINFO * sizeof(texinfo_t));
  158. allocatedbspmem += MAX_MAP_TEXINFO * sizeof(texinfo_t);
  159. //*/
  160. //faces
  161. numfaces = 0;
  162. dfaces = (dface_t *) GetClearedMemory(MAX_MAP_FACES * sizeof(dface_t));
  163. allocatedbspmem += MAX_MAP_FACES * sizeof(dface_t);
  164. //edges
  165. numedges = 0;
  166. dedges = (dedge_t *) GetClearedMemory(MAX_MAP_EDGES * sizeof(dedge_t));
  167. allocatedbspmem += MAX_MAP_EDGES * sizeof(dedge_t);
  168. //leaf faces
  169. numleaffaces = 0;
  170. dleaffaces = (unsigned short *) GetClearedMemory(MAX_MAP_LEAFFACES * sizeof(unsigned short));
  171. allocatedbspmem += MAX_MAP_LEAFFACES * sizeof(unsigned short);
  172. //leaf brushes
  173. numleafbrushes = 0;
  174. dleafbrushes = (unsigned short *) GetClearedMemory(MAX_MAP_LEAFBRUSHES * sizeof(unsigned short));
  175. allocatedbspmem += MAX_MAP_LEAFBRUSHES * sizeof(unsigned short);
  176. //surface edges
  177. numsurfedges = 0;
  178. dsurfedges = (int *) GetClearedMemory(MAX_MAP_SURFEDGES * sizeof(int));
  179. allocatedbspmem += MAX_MAP_SURFEDGES * sizeof(int);
  180. //brushes
  181. numbrushes = 0;
  182. dbrushes = (dbrush_t *) GetClearedMemory(MAX_MAP_BRUSHES * sizeof(dbrush_t));
  183. allocatedbspmem += MAX_MAP_BRUSHES * sizeof(dbrush_t);
  184. //brushsides
  185. numbrushsides = 0;
  186. dbrushsides = (dbrushside_t *) GetClearedMemory(MAX_MAP_BRUSHSIDES * sizeof(dbrushside_t));
  187. allocatedbspmem += MAX_MAP_BRUSHSIDES * sizeof(dbrushside_t);
  188. //areas
  189. numareas = 0;
  190. dareas = (darea_t *) GetClearedMemory(MAX_MAP_AREAS * sizeof(darea_t));
  191. allocatedbspmem += MAX_MAP_AREAS * sizeof(darea_t);
  192. //area portals
  193. numareaportals = 0;
  194. dareaportals = (dareaportal_t *) GetClearedMemory(MAX_MAP_AREAPORTALS * sizeof(dareaportal_t));
  195. allocatedbspmem += MAX_MAP_AREAPORTALS * sizeof(dareaportal_t);
  196. //print allocated memory
  197. Log_Print("allocated ");
  198. PrintMemorySize(allocatedbspmem);
  199. Log_Print(" of BSP memory\n");
  200. } //end of the function Q2_AllocMaxBSP
  201. void Q2_FreeMaxBSP(void)
  202. {
  203. //models
  204. nummodels = 0;
  205. FreeMemory(dmodels);
  206. dmodels = NULL;
  207. //vis data
  208. visdatasize = 0;
  209. FreeMemory(dvisdata);
  210. dvisdata = NULL;
  211. dvis = NULL;
  212. //light data
  213. lightdatasize = 0;
  214. FreeMemory(dlightdata);
  215. dlightdata = NULL;
  216. //entity data
  217. entdatasize = 0;
  218. FreeMemory(dentdata);
  219. dentdata = NULL;
  220. //leafs
  221. numleafs = 0;
  222. FreeMemory(dleafs);
  223. dleafs = NULL;
  224. //planes
  225. numplanes = 0;
  226. FreeMemory(dplanes);
  227. dplanes = NULL;
  228. //vertexes
  229. numvertexes = 0;
  230. FreeMemory(dvertexes);
  231. dvertexes = NULL;
  232. //nodes
  233. numnodes = 0;
  234. FreeMemory(dnodes);
  235. dnodes = NULL;
  236. /*
  237. //texture info
  238. numtexinfo = 0;
  239. FreeMemory(texinfo);
  240. texinfo = NULL;
  241. //*/
  242. //faces
  243. numfaces = 0;
  244. FreeMemory(dfaces);
  245. dfaces = NULL;
  246. //edges
  247. numedges = 0;
  248. FreeMemory(dedges);
  249. dedges = NULL;
  250. //leaf faces
  251. numleaffaces = 0;
  252. FreeMemory(dleaffaces);
  253. dleaffaces = NULL;
  254. //leaf brushes
  255. numleafbrushes = 0;
  256. FreeMemory(dleafbrushes);
  257. dleafbrushes = NULL;
  258. //surface edges
  259. numsurfedges = 0;
  260. FreeMemory(dsurfedges);
  261. dsurfedges = NULL;
  262. //brushes
  263. numbrushes = 0;
  264. FreeMemory(dbrushes);
  265. dbrushes = NULL;
  266. //brushsides
  267. numbrushsides = 0;
  268. FreeMemory(dbrushsides);
  269. dbrushsides = NULL;
  270. //areas
  271. numareas = 0;
  272. FreeMemory(dareas);
  273. dareas = NULL;
  274. //area portals
  275. numareaportals = 0;
  276. FreeMemory(dareaportals);
  277. dareaportals = NULL;
  278. //
  279. Log_Print("freed ");
  280. PrintMemorySize(allocatedbspmem);
  281. Log_Print(" of BSP memory\n");
  282. allocatedbspmem = 0;
  283. } //end of the function Q2_FreeMaxBSP
  284. #define WCONVEX_EPSILON 0.5
  285. int InsideWinding(winding_t *w, vec3_t point, int planenum)
  286. {
  287. int i;
  288. float dist;
  289. vec_t *v1, *v2;
  290. vec3_t normal, edgevec;
  291. dplane_t *plane;
  292. for (i = 1; i <= w->numpoints; i++)
  293. {
  294. v1 = w->p[i % w->numpoints];
  295. v2 = w->p[(i + 1) % w->numpoints];
  296. VectorSubtract(v2, v1, edgevec);
  297. plane = &dplanes[planenum];
  298. CrossProduct(plane->normal, edgevec, normal);
  299. VectorNormalize(normal);
  300. dist = DotProduct(normal, v1);
  301. //
  302. if (DotProduct(normal, point) - dist > WCONVEX_EPSILON) return false;
  303. } //end for
  304. return true;
  305. } //end of the function InsideWinding
  306. int InsideFace(dface_t *face, vec3_t point)
  307. {
  308. int i, edgenum, side;
  309. float dist;
  310. vec_t *v1, *v2;
  311. vec3_t normal, edgevec;
  312. dplane_t *plane;
  313. for (i = 0; i < face->numedges; i++)
  314. {
  315. //get the first and second vertex of the edge
  316. edgenum = dsurfedges[face->firstedge + i];
  317. side = edgenum < 0;
  318. v1 = dvertexes[dedges[abs(edgenum)].v[side]].point;
  319. v2 = dvertexes[dedges[abs(edgenum)].v[!side]].point;
  320. //create a plane through the edge vector, orthogonal to the face plane
  321. //and with the normal vector pointing out of the face
  322. VectorSubtract(v1, v2, edgevec);
  323. plane = &dplanes[face->planenum];
  324. CrossProduct(plane->normal, edgevec, normal);
  325. VectorNormalize(normal);
  326. dist = DotProduct(normal, v1);
  327. //
  328. if (DotProduct(normal, point) - dist > WCONVEX_EPSILON) return false;
  329. } //end for
  330. return true;
  331. } //end of the function InsideFace
  332. //===========================================================================
  333. // returns the amount the face and the winding overlap
  334. //
  335. // Parameter: -
  336. // Returns: -
  337. // Changes Globals: -
  338. //===========================================================================
  339. float Q2_FaceOnWinding(q2_dface_t *face, winding_t *winding)
  340. {
  341. int i, edgenum, side;
  342. float dist, area;
  343. q2_dplane_t plane;
  344. vec_t *v1, *v2;
  345. vec3_t normal, edgevec;
  346. winding_t *w;
  347. //
  348. w = CopyWinding(winding);
  349. memcpy(&plane, &q2_dplanes[face->planenum], sizeof(q2_dplane_t));
  350. //check on which side of the plane the face is
  351. if (face->side)
  352. {
  353. VectorNegate(plane.normal, plane.normal);
  354. plane.dist = -plane.dist;
  355. } //end if
  356. for (i = 0; i < face->numedges && w; i++)
  357. {
  358. //get the first and second vertex of the edge
  359. edgenum = q2_dsurfedges[face->firstedge + i];
  360. side = edgenum > 0;
  361. //if the face plane is flipped
  362. v1 = q2_dvertexes[q2_dedges[abs(edgenum)].v[side]].point;
  363. v2 = q2_dvertexes[q2_dedges[abs(edgenum)].v[!side]].point;
  364. //create a plane through the edge vector, orthogonal to the face plane
  365. //and with the normal vector pointing inward
  366. VectorSubtract(v1, v2, edgevec);
  367. CrossProduct(edgevec, plane.normal, normal);
  368. VectorNormalize(normal);
  369. dist = DotProduct(normal, v1);
  370. //
  371. ChopWindingInPlace(&w, normal, dist, -0.1); //CLIP_EPSILON
  372. } //end for
  373. if (w)
  374. {
  375. area = WindingArea(w);
  376. FreeWinding(w);
  377. return area;
  378. } //end if
  379. return 0;
  380. } //end of the function Q2_FaceOnWinding
  381. //===========================================================================
  382. // creates a winding for the given brush side on the given brush
  383. //
  384. // Parameter: -
  385. // Returns: -
  386. // Changes Globals: -
  387. //===========================================================================
  388. winding_t *Q2_BrushSideWinding(dbrush_t *brush, dbrushside_t *baseside)
  389. {
  390. int i;
  391. dplane_t *baseplane, *plane;
  392. winding_t *w;
  393. dbrushside_t *side;
  394. //create a winding for the brush side with the given planenumber
  395. baseplane = &dplanes[baseside->planenum];
  396. w = BaseWindingForPlane(baseplane->normal, baseplane->dist);
  397. for (i = 0; i < brush->numsides && w; i++)
  398. {
  399. side = &dbrushsides[brush->firstside + i];
  400. //don't chop with the base plane
  401. if (side->planenum == baseside->planenum) continue;
  402. //also don't use planes that are almost equal
  403. plane = &dplanes[side->planenum];
  404. if (DotProduct(baseplane->normal, plane->normal) > 0.999
  405. && fabs(baseplane->dist - plane->dist) < 0.01) continue;
  406. //
  407. plane = &dplanes[side->planenum^1];
  408. ChopWindingInPlace(&w, plane->normal, plane->dist, -0.1); //CLIP_EPSILON);
  409. } //end for
  410. return w;
  411. } //end of the function Q2_BrushSideWinding
  412. //===========================================================================
  413. //
  414. // Parameter: -
  415. // Returns: -
  416. // Changes Globals: -
  417. //===========================================================================
  418. int Q2_HintSkipBrush(dbrush_t *brush)
  419. {
  420. int j;
  421. dbrushside_t *brushside;
  422. for (j = 0; j < brush->numsides; j++)
  423. {
  424. brushside = &dbrushsides[brush->firstside + j];
  425. if (brushside->texinfo > 0)
  426. {
  427. if (texinfo[brushside->texinfo].flags & (SURF_SKIP|SURF_HINT))
  428. {
  429. return true;
  430. } //end if
  431. } //end if
  432. } //end for
  433. return false;
  434. } //end of the function Q2_HintSkipBrush
  435. //===========================================================================
  436. // fix screwed brush texture references
  437. //
  438. // Parameter: -
  439. // Returns: -
  440. // Changes Globals: -
  441. //===========================================================================
  442. qboolean WindingIsTiny(winding_t *w);
  443. void Q2_FixTextureReferences(void)
  444. {
  445. int i, j, k, we;
  446. dbrushside_t *brushside;
  447. dbrush_t *brush;
  448. dface_t *face;
  449. winding_t *w;
  450. memset(brushsidetextured, false, MAX_MAP_BRUSHSIDES);
  451. //go over all the brushes
  452. for (i = 0; i < numbrushes; i++)
  453. {
  454. brush = &dbrushes[i];
  455. //hint brushes are not textured
  456. if (Q2_HintSkipBrush(brush)) continue;
  457. //go over all the sides of the brush
  458. for (j = 0; j < brush->numsides; j++)
  459. {
  460. brushside = &dbrushsides[brush->firstside + j];
  461. //
  462. w = Q2_BrushSideWinding(brush, brushside);
  463. if (!w)
  464. {
  465. brushsidetextured[brush->firstside + j] = true;
  466. continue;
  467. } //end if
  468. else
  469. {
  470. //RemoveEqualPoints(w, 0.2);
  471. if (WindingIsTiny(w))
  472. {
  473. FreeWinding(w);
  474. brushsidetextured[brush->firstside + j] = true;
  475. continue;
  476. } //end if
  477. else
  478. {
  479. we = WindingError(w);
  480. if (we == WE_NOTENOUGHPOINTS
  481. || we == WE_SMALLAREA
  482. || we == WE_POINTBOGUSRANGE
  483. // || we == WE_NONCONVEX
  484. )
  485. {
  486. FreeWinding(w);
  487. brushsidetextured[brush->firstside + j] = true;
  488. continue;
  489. } //end if
  490. } //end else
  491. } //end else
  492. if (WindingArea(w) < 20)
  493. {
  494. brushsidetextured[brush->firstside + j] = true;
  495. } //end if
  496. //find a face for texturing this brush
  497. for (k = 0; k < numfaces; k++)
  498. {
  499. face = &dfaces[k];
  500. //if the face is in the same plane as the brush side
  501. if ((face->planenum&~1) != (brushside->planenum&~1)) continue;
  502. //if the face is partly or totally on the brush side
  503. if (Q2_FaceOnWinding(face, w))
  504. {
  505. brushside->texinfo = face->texinfo;
  506. brushsidetextured[brush->firstside + j] = true;
  507. break;
  508. } //end if
  509. } //end for
  510. FreeWinding(w);
  511. } //end for
  512. } //end for
  513. } //end of the function Q2_FixTextureReferences*/
  514. //#endif //ME
  515. /*
  516. ===============
  517. CompressVis
  518. ===============
  519. */
  520. int Q2_CompressVis (byte *vis, byte *dest)
  521. {
  522. int j;
  523. int rep;
  524. int visrow;
  525. byte *dest_p;
  526. dest_p = dest;
  527. // visrow = (r_numvisleafs + 7)>>3;
  528. visrow = (dvis->numclusters + 7)>>3;
  529. for (j=0 ; j<visrow ; j++)
  530. {
  531. *dest_p++ = vis[j];
  532. if (vis[j])
  533. continue;
  534. rep = 1;
  535. for ( j++; j<visrow ; j++)
  536. if (vis[j] || rep == 255)
  537. break;
  538. else
  539. rep++;
  540. *dest_p++ = rep;
  541. j--;
  542. }
  543. return dest_p - dest;
  544. }
  545. /*
  546. ===================
  547. DecompressVis
  548. ===================
  549. */
  550. void Q2_DecompressVis (byte *in, byte *decompressed)
  551. {
  552. int c;
  553. byte *out;
  554. int row;
  555. // row = (r_numvisleafs+7)>>3;
  556. row = (dvis->numclusters+7)>>3;
  557. out = decompressed;
  558. do
  559. {
  560. if (*in)
  561. {
  562. *out++ = *in++;
  563. continue;
  564. }
  565. c = in[1];
  566. if (!c)
  567. Error ("DecompressVis: 0 repeat");
  568. in += 2;
  569. while (c)
  570. {
  571. *out++ = 0;
  572. c--;
  573. }
  574. } while (out - decompressed < row);
  575. }
  576. //=============================================================================
  577. /*
  578. =============
  579. SwapBSPFile
  580. Byte swaps all data in a bsp file.
  581. =============
  582. */
  583. void Q2_SwapBSPFile (qboolean todisk)
  584. {
  585. int i, j;
  586. dmodel_t *d;
  587. // models
  588. for (i=0 ; i<nummodels ; i++)
  589. {
  590. d = &dmodels[i];
  591. d->firstface = LittleLong (d->firstface);
  592. d->numfaces = LittleLong (d->numfaces);
  593. d->headnode = LittleLong (d->headnode);
  594. for (j=0 ; j<3 ; j++)
  595. {
  596. d->mins[j] = LittleFloat(d->mins[j]);
  597. d->maxs[j] = LittleFloat(d->maxs[j]);
  598. d->origin[j] = LittleFloat(d->origin[j]);
  599. }
  600. }
  601. //
  602. // vertexes
  603. //
  604. for (i=0 ; i<numvertexes ; i++)
  605. {
  606. for (j=0 ; j<3 ; j++)
  607. dvertexes[i].point[j] = LittleFloat (dvertexes[i].point[j]);
  608. }
  609. //
  610. // planes
  611. //
  612. for (i=0 ; i<numplanes ; i++)
  613. {
  614. for (j=0 ; j<3 ; j++)
  615. dplanes[i].normal[j] = LittleFloat (dplanes[i].normal[j]);
  616. dplanes[i].dist = LittleFloat (dplanes[i].dist);
  617. dplanes[i].type = LittleLong (dplanes[i].type);
  618. }
  619. //
  620. // texinfos
  621. //
  622. for (i=0 ; i<numtexinfo ; i++)
  623. {
  624. for (j=0 ; j<8 ; j++)
  625. texinfo[i].vecs[0][j] = LittleFloat (texinfo[i].vecs[0][j]);
  626. texinfo[i].flags = LittleLong (texinfo[i].flags);
  627. texinfo[i].value = LittleLong (texinfo[i].value);
  628. texinfo[i].nexttexinfo = LittleLong (texinfo[i].nexttexinfo);
  629. }
  630. //
  631. // faces
  632. //
  633. for (i=0 ; i<numfaces ; i++)
  634. {
  635. dfaces[i].texinfo = LittleShort (dfaces[i].texinfo);
  636. dfaces[i].planenum = LittleShort (dfaces[i].planenum);
  637. dfaces[i].side = LittleShort (dfaces[i].side);
  638. dfaces[i].lightofs = LittleLong (dfaces[i].lightofs);
  639. dfaces[i].firstedge = LittleLong (dfaces[i].firstedge);
  640. dfaces[i].numedges = LittleShort (dfaces[i].numedges);
  641. }
  642. //
  643. // nodes
  644. //
  645. for (i=0 ; i<numnodes ; i++)
  646. {
  647. dnodes[i].planenum = LittleLong (dnodes[i].planenum);
  648. for (j=0 ; j<3 ; j++)
  649. {
  650. dnodes[i].mins[j] = LittleShort (dnodes[i].mins[j]);
  651. dnodes[i].maxs[j] = LittleShort (dnodes[i].maxs[j]);
  652. }
  653. dnodes[i].children[0] = LittleLong (dnodes[i].children[0]);
  654. dnodes[i].children[1] = LittleLong (dnodes[i].children[1]);
  655. dnodes[i].firstface = LittleShort (dnodes[i].firstface);
  656. dnodes[i].numfaces = LittleShort (dnodes[i].numfaces);
  657. }
  658. //
  659. // leafs
  660. //
  661. for (i=0 ; i<numleafs ; i++)
  662. {
  663. dleafs[i].contents = LittleLong (dleafs[i].contents);
  664. dleafs[i].cluster = LittleShort (dleafs[i].cluster);
  665. dleafs[i].area = LittleShort (dleafs[i].area);
  666. for (j=0 ; j<3 ; j++)
  667. {
  668. dleafs[i].mins[j] = LittleShort (dleafs[i].mins[j]);
  669. dleafs[i].maxs[j] = LittleShort (dleafs[i].maxs[j]);
  670. }
  671. dleafs[i].firstleafface = LittleShort (dleafs[i].firstleafface);
  672. dleafs[i].numleaffaces = LittleShort (dleafs[i].numleaffaces);
  673. dleafs[i].firstleafbrush = LittleShort (dleafs[i].firstleafbrush);
  674. dleafs[i].numleafbrushes = LittleShort (dleafs[i].numleafbrushes);
  675. }
  676. //
  677. // leaffaces
  678. //
  679. for (i=0 ; i<numleaffaces ; i++)
  680. dleaffaces[i] = LittleShort (dleaffaces[i]);
  681. //
  682. // leafbrushes
  683. //
  684. for (i=0 ; i<numleafbrushes ; i++)
  685. dleafbrushes[i] = LittleShort (dleafbrushes[i]);
  686. //
  687. // surfedges
  688. //
  689. for (i=0 ; i<numsurfedges ; i++)
  690. dsurfedges[i] = LittleLong (dsurfedges[i]);
  691. //
  692. // edges
  693. //
  694. for (i=0 ; i<numedges ; i++)
  695. {
  696. dedges[i].v[0] = LittleShort (dedges[i].v[0]);
  697. dedges[i].v[1] = LittleShort (dedges[i].v[1]);
  698. }
  699. //
  700. // brushes
  701. //
  702. for (i=0 ; i<numbrushes ; i++)
  703. {
  704. dbrushes[i].firstside = LittleLong (dbrushes[i].firstside);
  705. dbrushes[i].numsides = LittleLong (dbrushes[i].numsides);
  706. dbrushes[i].contents = LittleLong (dbrushes[i].contents);
  707. }
  708. //
  709. // areas
  710. //
  711. for (i=0 ; i<numareas ; i++)
  712. {
  713. dareas[i].numareaportals = LittleLong (dareas[i].numareaportals);
  714. dareas[i].firstareaportal = LittleLong (dareas[i].firstareaportal);
  715. }
  716. //
  717. // areasportals
  718. //
  719. for (i=0 ; i<numareaportals ; i++)
  720. {
  721. dareaportals[i].portalnum = LittleLong (dareaportals[i].portalnum);
  722. dareaportals[i].otherarea = LittleLong (dareaportals[i].otherarea);
  723. }
  724. //
  725. // brushsides
  726. //
  727. for (i=0 ; i<numbrushsides ; i++)
  728. {
  729. dbrushsides[i].planenum = LittleShort (dbrushsides[i].planenum);
  730. dbrushsides[i].texinfo = LittleShort (dbrushsides[i].texinfo);
  731. }
  732. //
  733. // visibility
  734. //
  735. if (todisk)
  736. j = dvis->numclusters;
  737. else
  738. j = LittleLong(dvis->numclusters);
  739. dvis->numclusters = LittleLong (dvis->numclusters);
  740. for (i=0 ; i<j ; i++)
  741. {
  742. dvis->bitofs[i][0] = LittleLong (dvis->bitofs[i][0]);
  743. dvis->bitofs[i][1] = LittleLong (dvis->bitofs[i][1]);
  744. }
  745. } //end of the function Q2_SwapBSPFile
  746. dheader_t *header;
  747. int Q2_CopyLump (int lump, void *dest, int size, int maxsize)
  748. {
  749. int length, ofs;
  750. length = header->lumps[lump].filelen;
  751. ofs = header->lumps[lump].fileofs;
  752. if (length % size)
  753. Error ("LoadBSPFile: odd lump size");
  754. if ((length/size) > maxsize)
  755. Error ("Q2_LoadBSPFile: exceeded max size for lump %d size %d > maxsize %d\n", lump, (length/size), maxsize);
  756. memcpy (dest, (byte *)header + ofs, length);
  757. return length / size;
  758. } //end of the function Q2_CopyLump
  759. /*
  760. =============
  761. LoadBSPFile
  762. =============
  763. */
  764. void Q2_LoadBSPFile(char *filename, int offset, int length)
  765. {
  766. int i;
  767. //
  768. // load the file header
  769. //
  770. LoadFile (filename, (void **)&header, offset, length);
  771. // swap the header
  772. for (i=0 ; i< sizeof(dheader_t)/4 ; i++)
  773. ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
  774. if (header->ident != IDBSPHEADER)
  775. Error ("%s is not a IBSP file", filename);
  776. if (header->version != BSPVERSION)
  777. Error ("%s is version %i, not %i", filename, header->version, BSPVERSION);
  778. nummodels = Q2_CopyLump (LUMP_MODELS, dmodels, sizeof(dmodel_t), MAX_MAP_MODELS);
  779. numvertexes = Q2_CopyLump (LUMP_VERTEXES, dvertexes, sizeof(dvertex_t), MAX_MAP_VERTS);
  780. numplanes = Q2_CopyLump (LUMP_PLANES, dplanes, sizeof(dplane_t), MAX_MAP_PLANES);
  781. numleafs = Q2_CopyLump (LUMP_LEAFS, dleafs, sizeof(dleaf_t), MAX_MAP_LEAFS);
  782. numnodes = Q2_CopyLump (LUMP_NODES, dnodes, sizeof(dnode_t), MAX_MAP_NODES);
  783. numtexinfo = Q2_CopyLump (LUMP_TEXINFO, texinfo, sizeof(texinfo_t), MAX_MAP_TEXINFO);
  784. numfaces = Q2_CopyLump (LUMP_FACES, dfaces, sizeof(dface_t), MAX_MAP_FACES);
  785. numleaffaces = Q2_CopyLump (LUMP_LEAFFACES, dleaffaces, sizeof(dleaffaces[0]), MAX_MAP_LEAFFACES);
  786. numleafbrushes = Q2_CopyLump (LUMP_LEAFBRUSHES, dleafbrushes, sizeof(dleafbrushes[0]), MAX_MAP_LEAFBRUSHES);
  787. numsurfedges = Q2_CopyLump (LUMP_SURFEDGES, dsurfedges, sizeof(dsurfedges[0]), MAX_MAP_SURFEDGES);
  788. numedges = Q2_CopyLump (LUMP_EDGES, dedges, sizeof(dedge_t), MAX_MAP_EDGES);
  789. numbrushes = Q2_CopyLump (LUMP_BRUSHES, dbrushes, sizeof(dbrush_t), MAX_MAP_BRUSHES);
  790. numbrushsides = Q2_CopyLump (LUMP_BRUSHSIDES, dbrushsides, sizeof(dbrushside_t), MAX_MAP_BRUSHSIDES);
  791. numareas = Q2_CopyLump (LUMP_AREAS, dareas, sizeof(darea_t), MAX_MAP_AREAS);
  792. numareaportals = Q2_CopyLump (LUMP_AREAPORTALS, dareaportals, sizeof(dareaportal_t), MAX_MAP_AREAPORTALS);
  793. visdatasize = Q2_CopyLump (LUMP_VISIBILITY, dvisdata, 1, MAX_MAP_VISIBILITY);
  794. lightdatasize = Q2_CopyLump (LUMP_LIGHTING, dlightdata, 1, MAX_MAP_LIGHTING);
  795. entdatasize = Q2_CopyLump (LUMP_ENTITIES, dentdata, 1, MAX_MAP_ENTSTRING);
  796. Q2_CopyLump (LUMP_POP, dpop, 1, MAX_MAP_DPOP);
  797. FreeMemory(header); // everything has been copied out
  798. //
  799. // swap everything
  800. //
  801. Q2_SwapBSPFile (false);
  802. Q2_FixTextureReferences();
  803. } //end of the function Q2_LoadBSPFile
  804. /*
  805. =============
  806. LoadBSPFileTexinfo
  807. Only loads the texinfo lump, so qdata can scan for textures
  808. =============
  809. */
  810. void Q2_LoadBSPFileTexinfo (char *filename)
  811. {
  812. int i;
  813. FILE *f;
  814. int length, ofs;
  815. header = GetMemory(sizeof(dheader_t));
  816. f = fopen (filename, "rb");
  817. fread (header, sizeof(dheader_t), 1, f);
  818. // swap the header
  819. for (i=0 ; i< sizeof(dheader_t)/4 ; i++)
  820. ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
  821. if (header->ident != IDBSPHEADER)
  822. Error ("%s is not a IBSP file", filename);
  823. if (header->version != BSPVERSION)
  824. Error ("%s is version %i, not %i", filename, header->version, BSPVERSION);
  825. length = header->lumps[LUMP_TEXINFO].filelen;
  826. ofs = header->lumps[LUMP_TEXINFO].fileofs;
  827. fseek (f, ofs, SEEK_SET);
  828. fread (texinfo, length, 1, f);
  829. fclose (f);
  830. numtexinfo = length / sizeof(texinfo_t);
  831. FreeMemory(header); // everything has been copied out
  832. Q2_SwapBSPFile (false);
  833. } //end of the function Q2_LoadBSPFileTexinfo
  834. //============================================================================
  835. FILE *wadfile;
  836. dheader_t outheader;
  837. void Q2_AddLump (int lumpnum, void *data, int len)
  838. {
  839. lump_t *lump;
  840. lump = &header->lumps[lumpnum];
  841. lump->fileofs = LittleLong( ftell(wadfile) );
  842. lump->filelen = LittleLong(len);
  843. SafeWrite (wadfile, data, (len+3)&~3);
  844. } //end of the function Q2_AddLump
  845. /*
  846. =============
  847. WriteBSPFile
  848. Swaps the bsp file in place, so it should not be referenced again
  849. =============
  850. */
  851. void Q2_WriteBSPFile (char *filename)
  852. {
  853. header = &outheader;
  854. memset (header, 0, sizeof(dheader_t));
  855. Q2_SwapBSPFile (true);
  856. header->ident = LittleLong (IDBSPHEADER);
  857. header->version = LittleLong (BSPVERSION);
  858. wadfile = SafeOpenWrite (filename);
  859. SafeWrite (wadfile, header, sizeof(dheader_t)); // overwritten later
  860. Q2_AddLump (LUMP_PLANES, dplanes, numplanes*sizeof(dplane_t));
  861. Q2_AddLump (LUMP_LEAFS, dleafs, numleafs*sizeof(dleaf_t));
  862. Q2_AddLump (LUMP_VERTEXES, dvertexes, numvertexes*sizeof(dvertex_t));
  863. Q2_AddLump (LUMP_NODES, dnodes, numnodes*sizeof(dnode_t));
  864. Q2_AddLump (LUMP_TEXINFO, texinfo, numtexinfo*sizeof(texinfo_t));
  865. Q2_AddLump (LUMP_FACES, dfaces, numfaces*sizeof(dface_t));
  866. Q2_AddLump (LUMP_BRUSHES, dbrushes, numbrushes*sizeof(dbrush_t));
  867. Q2_AddLump (LUMP_BRUSHSIDES, dbrushsides, numbrushsides*sizeof(dbrushside_t));
  868. Q2_AddLump (LUMP_LEAFFACES, dleaffaces, numleaffaces*sizeof(dleaffaces[0]));
  869. Q2_AddLump (LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes*sizeof(dleafbrushes[0]));
  870. Q2_AddLump (LUMP_SURFEDGES, dsurfedges, numsurfedges*sizeof(dsurfedges[0]));
  871. Q2_AddLump (LUMP_EDGES, dedges, numedges*sizeof(dedge_t));
  872. Q2_AddLump (LUMP_MODELS, dmodels, nummodels*sizeof(dmodel_t));
  873. Q2_AddLump (LUMP_AREAS, dareas, numareas*sizeof(darea_t));
  874. Q2_AddLump (LUMP_AREAPORTALS, dareaportals, numareaportals*sizeof(dareaportal_t));
  875. Q2_AddLump (LUMP_LIGHTING, dlightdata, lightdatasize);
  876. Q2_AddLump (LUMP_VISIBILITY, dvisdata, visdatasize);
  877. Q2_AddLump (LUMP_ENTITIES, dentdata, entdatasize);
  878. Q2_AddLump (LUMP_POP, dpop, sizeof(dpop));
  879. fseek (wadfile, 0, SEEK_SET);
  880. SafeWrite (wadfile, header, sizeof(dheader_t));
  881. fclose (wadfile);
  882. } //end of the function Q2_WriteBSPFile
  883. //============================================================================
  884. /*
  885. =============
  886. PrintBSPFileSizes
  887. Dumps info about current file
  888. =============
  889. */
  890. void Q2_PrintBSPFileSizes (void)
  891. {
  892. if (!num_entities)
  893. Q2_ParseEntities();
  894. printf ("%6i models %7i\n"
  895. ,nummodels, (int)(nummodels*sizeof(dmodel_t)));
  896. printf ("%6i brushes %7i\n"
  897. ,numbrushes, (int)(numbrushes*sizeof(dbrush_t)));
  898. printf ("%6i brushsides %7i\n"
  899. ,numbrushsides, (int)(numbrushsides*sizeof(dbrushside_t)));
  900. printf ("%6i planes %7i\n"
  901. ,numplanes, (int)(numplanes*sizeof(dplane_t)));
  902. printf ("%6i texinfo %7i\n"
  903. ,numtexinfo, (int)(numtexinfo*sizeof(texinfo_t)));
  904. printf ("%6i entdata %7i\n", num_entities, entdatasize);
  905. printf ("\n");
  906. printf ("%6i vertexes %7i\n"
  907. ,numvertexes, (int)(numvertexes*sizeof(dvertex_t)));
  908. printf ("%6i nodes %7i\n"
  909. ,numnodes, (int)(numnodes*sizeof(dnode_t)));
  910. printf ("%6i faces %7i\n"
  911. ,numfaces, (int)(numfaces*sizeof(dface_t)));
  912. printf ("%6i leafs %7i\n"
  913. ,numleafs, (int)(numleafs*sizeof(dleaf_t)));
  914. printf ("%6i leaffaces %7i\n"
  915. ,numleaffaces, (int)(numleaffaces*sizeof(dleaffaces[0])));
  916. printf ("%6i leafbrushes %7i\n"
  917. ,numleafbrushes, (int)(numleafbrushes*sizeof(dleafbrushes[0])));
  918. printf ("%6i surfedges %7i\n"
  919. ,numsurfedges, (int)(numsurfedges*sizeof(dsurfedges[0])));
  920. printf ("%6i edges %7i\n"
  921. ,numedges, (int)(numedges*sizeof(dedge_t)));
  922. //NEW
  923. printf ("%6i areas %7i\n"
  924. ,numareas, (int)(numareas*sizeof(darea_t)));
  925. printf ("%6i areaportals %7i\n"
  926. ,numareaportals, (int)(numareaportals*sizeof(dareaportal_t)));
  927. //ENDNEW
  928. printf (" lightdata %7i\n", lightdatasize);
  929. printf (" visdata %7i\n", visdatasize);
  930. } //end of the function Q2_PrintBSPFileSizes
  931. /*
  932. ================
  933. ParseEntities
  934. Parses the dentdata string into entities
  935. ================
  936. */
  937. void Q2_ParseEntities (void)
  938. {
  939. script_t *script;
  940. num_entities = 0;
  941. script = LoadScriptMemory(dentdata, entdatasize, "*Quake2 bsp file");
  942. SetScriptFlags(script, SCFL_NOSTRINGWHITESPACES |
  943. SCFL_NOSTRINGESCAPECHARS);
  944. while(ParseEntity(script))
  945. {
  946. } //end while
  947. FreeScript(script);
  948. } //end of the function Q2_ParseEntities
  949. /*
  950. ================
  951. UnparseEntities
  952. Generates the dentdata string from all the entities
  953. ================
  954. */
  955. void Q2_UnparseEntities (void)
  956. {
  957. char *buf, *end;
  958. epair_t *ep;
  959. char line[2048];
  960. int i;
  961. char key[1024], value[1024];
  962. buf = dentdata;
  963. end = buf;
  964. *end = 0;
  965. for (i=0 ; i<num_entities ; i++)
  966. {
  967. ep = entities[i].epairs;
  968. if (!ep)
  969. continue; // ent got removed
  970. strcat (end,"{\n");
  971. end += 2;
  972. for (ep = entities[i].epairs ; ep ; ep=ep->next)
  973. {
  974. strcpy (key, ep->key);
  975. StripTrailing (key);
  976. strcpy (value, ep->value);
  977. StripTrailing (value);
  978. sprintf (line, "\"%s\" \"%s\"\n", key, value);
  979. strcat (end, line);
  980. end += strlen(line);
  981. }
  982. strcat (end,"}\n");
  983. end += 2;
  984. if (end > buf + MAX_MAP_ENTSTRING)
  985. Error ("Entity text too long");
  986. }
  987. entdatasize = end - buf + 1;
  988. } //end of the function Q2_UnparseEntities