00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #if defined(WIN32) || defined(_WIN32)
00025 #include <io.h>
00026 #endif
00027 #include <malloc.h>
00028 #include "l_cmd.h"
00029 #include "l_math.h"
00030 #include "l_poly.h"
00031 #include "l_threads.h"
00032 #include "../botlib/l_script.h"
00033 #include "l_bsp_ent.h"
00034 #include "q2files.h"
00035 #include "l_mem.h"
00036 #include "l_utils.h"
00037 #include "l_log.h"
00038 #include "l_qfiles.h"
00039
00040 #define BSPC_VERSION "2.1h"
00041
00042 #define ME
00043 #define DEBUG
00044 #define NODELIST
00045 #define SIN
00046
00047 #define MAX_BRUSH_SIDES 128 //maximum number of sides per brush
00048 #define CLIP_EPSILON 0.1
00049 #define MAX_MAP_BOUNDS 65535
00050 #define BOGUS_RANGE (MAX_MAP_BOUNDS+128) //somewhere outside the map
00051 #define TEXINFO_NODE -1 //side is allready on a node
00052 #define PLANENUM_LEAF -1 //used for leaf nodes
00053 #define MAXEDGES 20 //maximum number of face edges
00054 #define MAX_NODE_BRUSHES 8 //maximum brushes in a node
00055
00056 #define SFL_TESTED 1
00057 #define SFL_VISIBLE 2
00058 #define SFL_BEVEL 4
00059 #define SFL_TEXTURED 8
00060 #define SFL_CURVE 16
00061
00062
00063 typedef struct plane_s
00064 {
00065 vec3_t normal;
00066 vec_t dist;
00067 int type;
00068 int signbits;
00069 struct plane_s *hash_chain;
00070 } plane_t;
00071
00072 typedef struct
00073 {
00074 vec_t shift[2];
00075 vec_t rotate;
00076 vec_t scale[2];
00077 char name[32];
00078 int flags;
00079 int value;
00080 } brush_texture_t;
00081
00082 typedef struct side_s
00083 {
00084 int planenum;
00085 int texinfo;
00086 winding_t *winding;
00087 struct side_s *original;
00088 int lightinfo;
00089 int contents;
00090 int surf;
00091 unsigned short flags;
00092 } side_t;
00093
00094 typedef struct mapbrush_s
00095 {
00096 int entitynum;
00097 int brushnum;
00098
00099 int contents;
00100 #ifdef ME
00101 int expansionbbox;
00102 int leafnum;
00103 int modelnum;
00104 #endif
00105
00106 vec3_t mins, maxs;
00107
00108 int numsides;
00109 side_t *original_sides;
00110 } mapbrush_t;
00111
00112 typedef struct face_s
00113 {
00114 struct face_s *next;
00115
00116
00117
00118
00119 struct face_s *merged;
00120 struct face_s *split[2];
00121
00122 struct portal_s *portal;
00123 int texinfo;
00124 #ifdef SIN
00125 int lightinfo;
00126 #endif
00127 int planenum;
00128 int contents;
00129 int outputnumber;
00130 winding_t *w;
00131 int numpoints;
00132 qboolean badstartvert;
00133 int vertexnums[MAXEDGES];
00134 } face_t;
00135
00136 typedef struct bspbrush_s
00137 {
00138 struct bspbrush_s *next;
00139 vec3_t mins, maxs;
00140 int side, testside;
00141 mapbrush_t *original;
00142 int numsides;
00143 side_t sides[6];
00144 } bspbrush_t;
00145
00146 typedef struct node_s
00147 {
00148
00149 int planenum;
00150 struct node_s *parent;
00151 vec3_t mins, maxs;
00152 bspbrush_t *volume;
00153
00154
00155 qboolean detail_seperator;
00156 side_t *side;
00157 struct node_s *children[2];
00158 face_t *faces;
00159
00160
00161 bspbrush_t *brushlist;
00162 int contents;
00163 int occupied;
00164 entity_t *occupant;
00165 int cluster;
00166 int area;
00167 struct portal_s *portals;
00168 #ifdef NODELIST
00169 struct node_s *next;
00170 #endif
00171 #ifdef ME
00172 int expansionbboxes;
00173 int modelnum;
00174 #endif
00175 } node_t;
00176
00177 typedef struct portal_s
00178 {
00179 plane_t plane;
00180 node_t *onnode;
00181 node_t *nodes[2];
00182 struct portal_s *next[2];
00183 winding_t *winding;
00184
00185 qboolean sidefound;
00186 side_t *side;
00187 face_t *face[2];
00188 #ifdef ME
00189 struct tmp_face_s *tmpface;
00190 int planenum;
00191 #endif
00192 } portal_t;
00193
00194 typedef struct
00195 {
00196 node_t *headnode;
00197 node_t outside_node;
00198 vec3_t mins, maxs;
00199 } tree_t;
00200
00201
00202
00203
00204
00205 extern qboolean noprune;
00206 extern qboolean nodetail;
00207 extern qboolean fulldetail;
00208 extern qboolean nomerge;
00209 extern qboolean nosubdiv;
00210 extern qboolean nowater;
00211 extern qboolean noweld;
00212 extern qboolean noshare;
00213 extern qboolean notjunc;
00214 extern qboolean onlyents;
00215 #ifdef ME
00216 extern qboolean nocsg;
00217 extern qboolean create_aas;
00218 extern qboolean freetree;
00219 extern qboolean lessbrushes;
00220 extern qboolean nobrushmerge;
00221 extern qboolean cancelconversion;
00222 extern qboolean noliquids;
00223 extern qboolean capsule_collision;
00224 #endif //ME
00225
00226 extern float subdivide_size;
00227 extern vec_t microvolume;
00228
00229 extern char outbase[32];
00230 extern char source[1024];
00231
00232
00233
00234
00235
00236 #define MAX_MAPFILE_PLANES 256000
00237 #define MAX_MAPFILE_BRUSHES 65535
00238 #define MAX_MAPFILE_BRUSHSIDES (MAX_MAPFILE_BRUSHES*8)
00239 #define MAX_MAPFILE_TEXINFO 8192
00240
00241 extern int entity_num;
00242
00243 extern plane_t mapplanes[MAX_MAPFILE_PLANES];
00244 extern int nummapplanes;
00245 extern int mapplaneusers[MAX_MAPFILE_PLANES];
00246
00247 extern int nummapbrushes;
00248 extern mapbrush_t mapbrushes[MAX_MAPFILE_BRUSHES];
00249
00250 extern vec3_t map_mins, map_maxs;
00251
00252 extern int nummapbrushsides;
00253 extern side_t brushsides[MAX_MAPFILE_BRUSHSIDES];
00254 extern brush_texture_t side_brushtextures[MAX_MAPFILE_BRUSHSIDES];
00255
00256 #ifdef ME
00257
00258 typedef struct
00259 {
00260 float vecs[2][4];
00261 int flags;
00262 int value;
00263 char texture[64];
00264 int nexttexinfo;
00265 } map_texinfo_t;
00266
00267 extern map_texinfo_t map_texinfo[MAX_MAPFILE_TEXINFO];
00268 extern int map_numtexinfo;
00269 #define NODESTACKSIZE 1024
00270
00271 #define MAPTYPE_QUAKE1 1
00272 #define MAPTYPE_QUAKE2 2
00273 #define MAPTYPE_QUAKE3 3
00274 #define MAPTYPE_HALFLIFE 4
00275 #define MAPTYPE_SIN 5
00276
00277 extern int nodestack[NODESTACKSIZE];
00278 extern int *nodestackptr;
00279 extern int nodestacksize;
00280 extern int brushmodelnumbers[MAX_MAPFILE_BRUSHES];
00281 extern int dbrushleafnums[MAX_MAPFILE_BRUSHES];
00282 extern int dplanes2mapplanes[MAX_MAPFILE_PLANES];
00283
00284 extern int loadedmaptype;
00285 #endif //ME
00286
00287 extern int c_boxbevels;
00288 extern int c_edgebevels;
00289 extern int c_areaportals;
00290 extern int c_clipbrushes;
00291 extern int c_squattbrushes;
00292
00293
00294 int FindFloatPlane(vec3_t normal, vec_t dist);
00295
00296 int PlaneTypeForNormal(vec3_t normal);
00297
00298 int PlaneFromPoints(int *p0, int *p1, int *p2);
00299
00300 void AddBrushBevels(mapbrush_t *b);
00301
00302 qboolean MakeBrushWindings(mapbrush_t *ob);
00303
00304 void MarkBrushBevels(mapbrush_t *brush);
00305
00306 int BrushExists(mapbrush_t *brush);
00307
00308 int LoadMapFromBSP(struct quakefile_s *qf);
00309
00310 void ResetMapLoading(void);
00311
00312 void PrintMapInfo(void);
00313
00314 void WriteMapFile(char *filename);
00315
00316
00317
00318
00319
00320 void Q2_ResetMapLoading(void);
00321
00322 void Q2_LoadMapFile(char *filename);
00323
00324 void Q2_LoadMapFromBSP(char *filename, int offset, int length);
00325
00326
00327
00328
00329
00330 void Q1_ResetMapLoading(void);
00331
00332 void Q1_LoadMapFile(char *filename);
00333
00334 void Q1_LoadMapFromBSP(char *filename, int offset, int length);
00335
00336
00337
00338
00339 void Q3_ResetMapLoading(void);
00340
00341 void Q3_LoadMapFromBSP(struct quakefile_s *qf);
00342
00343
00344
00345
00346
00347 void Sin_ResetMapLoading(void);
00348
00349 void Sin_LoadMapFile(char *filename);
00350
00351 void Sin_LoadMapFromBSP(char *filename, int offset, int length);
00352
00353
00354
00355
00356
00357 void HL_ResetMapLoading(void);
00358
00359 void HL_LoadMapFile(char *filename);
00360
00361 void HL_LoadMapFromBSP(char *filename, int offset, int length);
00362
00363
00364
00365
00366
00367 typedef struct
00368 {
00369 char name[64];
00370 int flags;
00371 int value;
00372 int contents;
00373 char animname[64];
00374 } textureref_t;
00375
00376 #define MAX_MAP_TEXTURES 1024
00377
00378 extern textureref_t textureref[MAX_MAP_TEXTURES];
00379
00380 int FindMiptex(char *name);
00381 int TexinfoForBrushTexture(plane_t *plane, brush_texture_t *bt, vec3_t origin);
00382 void TextureAxisFromPlane(plane_t *pln, vec3_t xv, vec3_t yv);
00383
00384
00385
00386
00387
00388 bspbrush_t *MakeBspBrushList(int startbrush, int endbrush, vec3_t clipmins, vec3_t clipmaxs);
00389 bspbrush_t *ChopBrushes(bspbrush_t *head);
00390 bspbrush_t *InitialBrushList(bspbrush_t *list);
00391 bspbrush_t *OptimizedBrushList(bspbrush_t *list);
00392 void WriteBrushMap(char *name, bspbrush_t *list);
00393 void CheckBSPBrush(bspbrush_t *brush);
00394 void BSPBrushWindings(bspbrush_t *brush);
00395 bspbrush_t *TryMergeBrushes(bspbrush_t *brush1, bspbrush_t *brush2);
00396 tree_t *ProcessWorldBrushes(int brush_start, int brush_end);
00397
00398
00399
00400
00401
00402 #define PSIDE_FRONT 1
00403 #define PSIDE_BACK 2
00404 #define PSIDE_BOTH (PSIDE_FRONT|PSIDE_BACK)
00405 #define PSIDE_FACING 4
00406
00407 void WriteBrushList(char *name, bspbrush_t *brush, qboolean onlyvis);
00408 bspbrush_t *CopyBrush(bspbrush_t *brush);
00409 void SplitBrush(bspbrush_t *brush, int planenum, bspbrush_t **front, bspbrush_t **back);
00410 node_t *AllocNode(void);
00411 bspbrush_t *AllocBrush(int numsides);
00412 int CountBrushList(bspbrush_t *brushes);
00413 void FreeBrush(bspbrush_t *brushes);
00414 vec_t BrushVolume(bspbrush_t *brush);
00415 void BoundBrush(bspbrush_t *brush);
00416 void FreeBrushList(bspbrush_t *brushes);
00417 tree_t *BrushBSP(bspbrush_t *brushlist, vec3_t mins, vec3_t maxs);
00418 bspbrush_t *BrushFromBounds(vec3_t mins, vec3_t maxs);
00419 int BrushMostlyOnSide(bspbrush_t *brush, plane_t *plane);
00420 qboolean WindingIsHuge(winding_t *w);
00421 qboolean WindingIsTiny(winding_t *w);
00422 void ResetBrushBSP(void);
00423
00424
00425
00426
00427
00428 int VisibleContents (int contents);
00429 void MakeHeadnodePortals (tree_t *tree);
00430 void MakeNodePortal (node_t *node);
00431 void SplitNodePortals (node_t *node);
00432 qboolean Portal_VisFlood (portal_t *p);
00433 qboolean FloodEntities (tree_t *tree);
00434 void FillOutside (node_t *headnode);
00435 void FloodAreas (tree_t *tree);
00436 void MarkVisibleSides (tree_t *tree, int start, int end);
00437 void FreePortal (portal_t *p);
00438 void EmitAreaPortals (node_t *headnode);
00439 void MakeTreePortals (tree_t *tree);
00440
00441
00442
00443
00444
00445 void OutputWinding(winding_t *w, FILE *glview);
00446 void WriteGLView(tree_t *tree, char *source);
00447
00448
00449
00450
00451
00452 extern vec3_t draw_mins, draw_maxs;
00453 extern qboolean drawflag;
00454
00455 void Draw_ClearWindow (void);
00456 void DrawWinding (winding_t *w);
00457 void GLS_BeginScene (void);
00458 void GLS_Winding (winding_t *w, int code);
00459 void GLS_EndScene (void);
00460
00461
00462
00463
00464
00465 void LeakFile (tree_t *tree);
00466
00467
00468
00469
00470
00471 tree_t *Tree_Alloc(void);
00472 void Tree_Free(tree_t *tree);
00473 void Tree_Free_r(node_t *node);
00474 void Tree_Print_r(node_t *node, int depth);
00475 void Tree_FreePortals_r(node_t *node);
00476 void Tree_PruneNodes_r(node_t *node);
00477 void Tree_PruneNodes(node_t *node);