]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_brush.c
changed cl_stainmaps to be a loadtime cvar which decides whether to
[xonotic/darkplaces.git] / model_brush.c
index f2804be78581bb2211b5e2b8391d84b65e3ff1be..dbf98b029a3e69ac1d86f05b9333176a4f62708d 100644 (file)
@@ -28,7 +28,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 //cvar_t r_subdivide_size = {CVAR_SAVE, "r_subdivide_size", "128", "how large water polygons should be (smaller values produce more polygons which give better warping effects)"};
 cvar_t halflifebsp = {0, "halflifebsp", "0", "indicates the current map is hlbsp format (useful to know because of different bounding box sizes)"};
-cvar_t mcbsp = {0, "mcbsp", "0", "indicates the current map is mcbsp format (useful to know because of different bounding box sizes)"};
 cvar_t r_novis = {0, "r_novis", "0", "draws whole level, see also sv_cullentities_pvs 0"};
 cvar_t r_picmipworld = {CVAR_SAVE, "r_picmipworld", "1", "whether gl_picmip shall apply to world textures too"};
 cvar_t r_nosurftextures = {0, "r_nosurftextures", "0", "pretends there was no texture lump found in the q1bsp/hlbsp loading (useful for debugging this rare case)"};
@@ -55,7 +54,6 @@ void Mod_BrushInit(void)
 {
 //     Cvar_RegisterVariable(&r_subdivide_size);
        Cvar_RegisterVariable(&halflifebsp);
-       Cvar_RegisterVariable(&mcbsp);
        Cvar_RegisterVariable(&r_novis);
        Cvar_RegisterVariable(&r_picmipworld);
        Cvar_RegisterVariable(&r_nosurftextures);
@@ -783,8 +781,16 @@ loc0:
 //#if COLLISIONPARANOID < 2
 static int Mod_Q1BSP_RecursiveHullCheckPoint(RecursiveHullCheckTraceInfo_t *t, int num)
 {
+       mplane_t *plane;
+       mclipnode_t *nodes = t->hull->clipnodes;
+       mplane_t *planes = t->hull->planes;
+       vec3_t point;
+       VectorCopy(t->start, point);
        while (num >= 0)
-               num = t->hull->clipnodes[num].children[(t->hull->planes[t->hull->clipnodes[num].planenum].type < 3 ? t->start[t->hull->planes[t->hull->clipnodes[num].planenum].type] : DotProduct(t->hull->planes[t->hull->clipnodes[num].planenum].normal, t->start)) < t->hull->planes[t->hull->clipnodes[num].planenum].dist];
+       {
+               plane = planes + nodes[num].planenum;
+               num = nodes[num].children[(plane->type < 3 ? point[plane->type] : DotProduct(plane->normal, point)) < plane->dist];
+       }
        num = Mod_Q1BSP_SuperContentsFromNativeContents(NULL, num);
        t->trace->startsupercontents |= num;
        if (num & SUPERCONTENTS_LIQUIDSMASK)
@@ -820,29 +826,6 @@ static void Mod_Q1BSP_TraceBox(struct model_s *model, int frame, trace_t *trace,
        VectorSubtract(boxmaxs, boxmins, boxsize);
        if (boxsize[0] < 3)
                rhc.hull = &model->brushq1.hulls[0]; // 0x0x0
-       else if (model->brush.ismcbsp)
-       {
-               int i;
-               float vdist, dist;
-               int vdisti = 0;
-
-               vdist = 0;      // shut up compiler warning
-
-       // find the closest hull size (this algorithm probably sucks, a qc field to override it might be in order...)
-               for (i = 1; i < model->brushq1.numhulls; i++)
-               {
-                       dist = fabs(model->brushq1.hulls[i].clip_size[0] - boxsize[0]) +
-                                       fabs(model->brushq1.hulls[i].clip_size[1] - boxsize[1]) +
-                                       fabs(model->brushq1.hulls[i].clip_size[2] - boxsize[2]) * 0.25;
-
-                       if (!vdisti || dist < vdist)
-                       {
-                               vdisti = i;
-                               vdist = dist;
-                       }
-               }
-               rhc.hull = &model->brushq1.hulls[vdisti];
-       }
        else if (model->brush.ishlbsp)
        {
                // LordHavoc: this has to have a minor tolerance (the .1) because of
@@ -900,6 +883,20 @@ static void Mod_Q1BSP_TraceBox(struct model_s *model, int frame, trace_t *trace,
 #endif
 }
 
+static int Mod_Q1BSP_PointSuperContents(struct model_s *model, int frame, const vec3_t point)
+{
+       int num = model->brushq1.hulls[0].firstclipnode;
+       mplane_t *plane;
+       mclipnode_t *nodes = model->brushq1.hulls[0].clipnodes;
+       mplane_t *planes = model->brushq1.hulls[0].planes;
+       while (num >= 0)
+       {
+               plane = planes + nodes[num].planenum;
+               num = nodes[num].children[(plane->type < 3 ? point[plane->type] : DotProduct(plane->normal, point)) < plane->dist];
+       }
+       return Mod_Q1BSP_SuperContentsFromNativeContents(NULL, num);
+}
+
 void Collision_ClipTrace_Box(trace_t *trace, const vec3_t cmins, const vec3_t cmaxs, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontentsmask, int boxsupercontents, int boxq3surfaceflags, texture_t *boxtexture)
 {
 #if 1
@@ -1172,7 +1169,7 @@ loc0:
                                        // bilinear filter each lightmap style, and sum them
                                        for (maps = 0;maps < MAXLIGHTMAPS && surface->lightmapinfo->styles[maps] != 255;maps++)
                                        {
-                                               scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[maps]];
+                                               scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[maps]];
                                                w = w00 * scale;VectorMA(ambientcolor, w, lightmap            , ambientcolor);
                                                w = w01 * scale;VectorMA(ambientcolor, w, lightmap + 3        , ambientcolor);
                                                w = w10 * scale;VectorMA(ambientcolor, w, lightmap + line3    , ambientcolor);
@@ -1320,6 +1317,9 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
        unsigned char *data, *mtdata;
        const char *s;
        char mapname[MAX_QPATH], name[MAX_QPATH];
+       unsigned char zero[4];
+
+       memset(zero, 0, sizeof(zero));
 
        loadmodel->data_textures = NULL;
 
@@ -1358,15 +1358,15 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                        tx->currentskinframe = tx->skinframes[0];
                        tx->basematerialflags = 0;
                }
+               tx->basematerialflags = MATERIALFLAG_WALL;
                if (i == loadmodel->num_textures - 1)
                {
-                       tx->basematerialflags |= MATERIALFLAG_WATER | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_NOSHADOW;
+                       tx->basematerialflags |= MATERIALFLAG_WATERSCROLL | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_NOSHADOW;
                        tx->supercontents = mod_q1bsp_texture_water.supercontents;
                        tx->surfaceflags = mod_q1bsp_texture_water.surfaceflags;
                }
                else
                {
-                       tx->basematerialflags |= MATERIALFLAG_WALL;
                        tx->supercontents = mod_q1bsp_texture_solid.supercontents;
                        tx->surfaceflags = mod_q1bsp_texture_solid.surfaceflags;
                }
@@ -1379,6 +1379,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                Vector4Set(tx->refractcolor4f, 1, 1, 1, 1);
                tx->reflectfactor = 1;
                Vector4Set(tx->reflectcolor4f, 1, 1, 1, 1);
+               tx->r_water_wateralpha = 1;
        }
 
        if (!m)
@@ -1443,7 +1444,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                        if (name[j] >= 'A' && name[j] <= 'Z')
                                name[j] += 'a' - 'A';
 
-               if (dmiptex->name[0] && Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, name, true, false, false))
+               if (dmiptex->name[0] && Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, name, false, false, 0))
                        continue;
 
                tx = loadmodel->data_textures + i;
@@ -1531,21 +1532,37 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                                        tx->skinframes[0] = skinframe;
                        }
 
-                       tx->basematerialflags = 0;
+                       tx->basematerialflags = MATERIALFLAG_WALL;
                        if (tx->name[0] == '*')
                        {
                                // LordHavoc: some turbulent textures should not be affected by wateralpha
-                               if (strncmp(tx->name,"*lava",5)
-                                && strncmp(tx->name,"*teleport",9)
-                                && strncmp(tx->name,"*rift",5)) // Scourge of Armagon texture
-                                       tx->basematerialflags |= MATERIALFLAG_WATERALPHA | MATERIALFLAG_NOSHADOW | MATERIALFLAG_WATERSHADER;
-                               tx->basematerialflags |= MATERIALFLAG_WATER | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_NOSHADOW;
+                               if (!strncmp(tx->name, "*glassmirror", 12)) // Tenebrae
+                               {
+                                       // replace the texture with transparent black
+                                       Vector4Set(zero, 128, 128, 128, 128);
+                                       tx->skinframes[0] = R_SkinFrame_LoadInternalBGRA(tx->name, TEXF_MIPMAP | TEXF_PRECACHE | TEXF_ALPHA, zero, 1, 1);
+                                       tx->basematerialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_REFLECTION;
+                               }
+                               else if (!strncmp(tx->name,"*lava",5)
+                                || !strncmp(tx->name,"*teleport",9)
+                                || !strncmp(tx->name,"*rift",5)) // Scourge of Armagon texture
+                                       tx->basematerialflags |= MATERIALFLAG_WATERSCROLL | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_NOSHADOW;
+                               else
+                                       tx->basematerialflags |= MATERIALFLAG_WATERSCROLL | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_NOSHADOW | MATERIALFLAG_WATERALPHA | MATERIALFLAG_WATERSHADER;
+                               if (tx->skinframes[0] && tx->skinframes[0]->fog)
+                                       tx->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
+                       }
+                       else if (!strncmp(tx->name, "mirror", 6)) // Tenebrae
+                       {
+                               // replace the texture with black
+                               tx->skinframes[0] = R_SkinFrame_LoadInternalBGRA(tx->name, TEXF_PRECACHE, zero, 1, 1);
+                               tx->basematerialflags |= MATERIALFLAG_REFLECTION;
                        }
                        else if (!strncmp(tx->name, "sky", 3))
-                               tx->basematerialflags |= MATERIALFLAG_SKY | MATERIALFLAG_NOSHADOW;
-                       else
-                               tx->basematerialflags |= MATERIALFLAG_WALL;
-                       if (tx->skinframes[0] && tx->skinframes[0]->fog)
+                               tx->basematerialflags = MATERIALFLAG_SKY | MATERIALFLAG_NOSHADOW;
+                       else if (!strcmp(tx->name, "caulk"))
+                               tx->basematerialflags = MATERIALFLAG_NODRAW;
+                       else if (tx->skinframes[0] && tx->skinframes[0]->fog)
                                tx->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
 
                        // start out with no animation
@@ -1669,11 +1686,6 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
                for (i=0; i<l->filelen; i++)
                        loadmodel->brushq1.lightdata[i] = mod_base[l->fileofs+i] >>= 1;
        }
-       else if (loadmodel->brush.ismcbsp)
-       {
-               loadmodel->brushq1.lightdata = (unsigned char *)Mem_Alloc(loadmodel->mempool, l->filelen);
-               memcpy(loadmodel->brushq1.lightdata, mod_base + l->fileofs, l->filelen);
-       }
        else // LordHavoc: bsp version 29 (normal white lighting)
        {
                // LordHavoc: hope is not lost yet, check for a .lit file to load
@@ -1690,7 +1702,8 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
                                i = LittleLong(((int *)data)[1]);
                                if (i == 1)
                                {
-                                       Con_DPrintf("loaded %s\n", litfilename);
+                                       if (developer_loading.integer)
+                                               Con_Printf("loaded %s\n", litfilename);
                                        loadmodel->brushq1.lightdata = (unsigned char *)Mem_Alloc(loadmodel->mempool, filesize - 8);
                                        memcpy(loadmodel->brushq1.lightdata, data + 8, filesize - 8);
                                        Mem_Free(data);
@@ -1702,7 +1715,8 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
                                                        i = LittleLong(((int *)data)[1]);
                                                        if (i == 1)
                                                        {
-                                                               Con_DPrintf("loaded %s\n", dlitfilename);
+                                                               if (developer_loading.integer)
+                                                                       Con_Printf("loaded %s\n", dlitfilename);
                                                                loadmodel->brushq1.nmaplightdata = (unsigned char *)Mem_Alloc(loadmodel->mempool, filesize - 8);
                                                                memcpy(loadmodel->brushq1.nmaplightdata, data + 8, filesize - 8);
                                                                loadmodel->brushq3.deluxemapping_modelspace = false;
@@ -1973,7 +1987,7 @@ static void Mod_Q1BSP_LoadTexinfo(lump_t *l)
                {
                        // if texture chosen is NULL or the shader needs a lightmap,
                        // force to notexture water shader
-                       if (out->texture == NULL || out->texture->basematerialflags & MATERIALFLAG_WALL)
+                       if (out->texture == NULL)
                                out->texture = loadmodel->data_textures + (loadmodel->num_textures - 1);
                }
                else
@@ -2155,7 +2169,7 @@ struct alloc_lm_state
 static void init_alloc_lm_state (struct alloc_lm_state* state)
 {
        int r;
-       
+
        state->currentY = 0;
        for (r = 0; r < MAX_SINGLE_LM_SIZE; r++)
        {
@@ -2332,7 +2346,7 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
                {
                        surface->lightmapinfo->samples = NULL;
                        // give non-lightmapped water a 1x white lightmap
-                       if ((surface->texture->basematerialflags & MATERIALFLAG_WATER) && (surface->lightmapinfo->texinfo->flags & TEX_SPECIAL) && ssize <= 256 && tsize <= 256)
+                       if (surface->texture->name[0] == '*' && (surface->lightmapinfo->texinfo->flags & TEX_SPECIAL) && ssize <= 256 && tsize <= 256)
                        {
                                surface->lightmapinfo->samples = (unsigned char *)Mem_Alloc(loadmodel->mempool, ssize * tsize * 3);
                                surface->lightmapinfo->styles[0] = 0;
@@ -2390,16 +2404,11 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
                {
                        int i, iu, iv, lightmapx, lightmapy;
                        float u, v, ubase, vbase, uscale, vscale;
-                       
+
                        smax = surface->lightmapinfo->extents[0] >> 4;
                        tmax = surface->lightmapinfo->extents[1] >> 4;
                        ssize = (surface->lightmapinfo->extents[0] >> 4) + 1;
                        tsize = (surface->lightmapinfo->extents[1] >> 4) + 1;
-                       
-                       // stainmap for permanent marks on walls
-                       surface->lightmapinfo->stainsamples = (unsigned char *)Mem_Alloc(loadmodel->mempool, ssize * tsize * 3);
-                       // clear to white
-                       memset(surface->lightmapinfo->stainsamples, 255, ssize * tsize * 3);
 
                        if (!lightmaptexture || !Mod_Q1BSP_AllocLightmapBlock(&allocState, lightmapsize, lightmapsize, ssize, tsize, &lightmapx, &lightmapy))
                        {
@@ -2437,7 +2446,36 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
                        }
 
                }
+
+               if (cl_stainmaps.integer)
+               {
+                       // allocate stainmaps for permanent marks on walls
+                       int stainmapsize = 0;
+                       unsigned char *stainsamples = NULL;
+                       for (surfacenum = 0, surface = loadmodel->data_surfaces;surfacenum < count;surfacenum++, surface++)
+                       {
+                               ssize = (surface->lightmapinfo->extents[0] >> 4) + 1;
+                               tsize = (surface->lightmapinfo->extents[1] >> 4) + 1;
+                               stainmapsize += ssize * tsize * 3;
+                       }
+                       // allocate and clear to white
+                       stainsamples = (unsigned char *)Mem_Alloc(loadmodel->mempool, stainmapsize);
+                       memset(stainsamples, 255, stainmapsize);
+                       // assign pointers
+                       for (surfacenum = 0, surface = loadmodel->data_surfaces;surfacenum < count;surfacenum++, surface++)
+                       {
+                               ssize = (surface->lightmapinfo->extents[0] >> 4) + 1;
+                               tsize = (surface->lightmapinfo->extents[1] >> 4) + 1;
+                               surface->lightmapinfo->stainsamples = stainsamples;
+                               stainsamples += ssize * tsize * 3;
+                       }
+               }
        }
+
+       // generate ushort elements array if possible
+       if (loadmodel->surfmesh.data_element3s)
+               for (i = 0;i < loadmodel->surfmesh.num_triangles*3;i++)
+                       loadmodel->surfmesh.data_element3s[i] = loadmodel->surfmesh.data_element3i[i];
 }
 
 static void Mod_Q1BSP_LoadNodes_RecursiveSetParent(mnode_t *node, mnode_t *parent)
@@ -2610,11 +2648,14 @@ qboolean Mod_Q1BSP_CheckWaterAlphaSupport(void)
        int i, j;
        mleaf_t *leaf;
        const unsigned char *pvs;
+       // if there's no vis data, assume supported (because everything is visible all the time)
+       if (!loadmodel->brush.data_pvsclusters)
+               return true;
        // check all liquid leafs to see if they can see into empty leafs, if any
        // can we can assume this map supports r_wateralpha
        for (i = 0, leaf = loadmodel->brush.data_leafs;i < loadmodel->brush.num_leafs;i++, leaf++)
        {
-               if ((leaf->contents == CONTENTS_WATER || leaf->contents == CONTENTS_SLIME) && (leaf->clusterindex >= 0 && loadmodel->brush.data_pvsclusters))
+               if ((leaf->contents == CONTENTS_WATER || leaf->contents == CONTENTS_SLIME) && leaf->clusterindex >= 0)
                {
                        pvs = loadmodel->brush.data_pvsclusters + leaf->clusterindex * loadmodel->brush.num_pvsclusterbytes;
                        for (j = 0;j < loadmodel->brush.num_leafs;j++)
@@ -2641,7 +2682,7 @@ static void Mod_Q1BSP_LoadClipnodes(lump_t *l, hullinfo_t *hullinfo)
        loadmodel->brushq1.clipnodes = out;
        loadmodel->brushq1.numclipnodes = count;
 
-       for (i = 1; i < hullinfo->numhulls; i++)
+       for (i = 1; i < MAX_MAP_HULLS; i++)
        {
                hull = &loadmodel->brushq1.hulls[i];
                hull->clipnodes = out;
@@ -3304,16 +3345,7 @@ static void Mod_Q1BSP_RoundUpToHullSize(model_t *cmodel, const vec3_t inmins, co
        const hull_t *hull;
 
        VectorSubtract(inmaxs, inmins, size);
-       if (cmodel->brush.ismcbsp)
-       {
-               if (size[0] < 3)
-                       hull = &cmodel->brushq1.hulls[0]; // 0x0x0
-               else if (size[2] < 48) // pick the nearest of 40 or 56
-                       hull = &cmodel->brushq1.hulls[2]; // 16x16x40
-               else
-                       hull = &cmodel->brushq1.hulls[1]; // 16x16x56
-       }
-       else if (cmodel->brush.ishlbsp)
+       if (cmodel->brush.ishlbsp)
        {
                if (size[0] < 3)
                        hull = &cmodel->brushq1.hulls[0]; // 0x0x0
@@ -3349,7 +3381,6 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
        float dist, modelyawradius, modelradius, *vec;
        msurface_t *surface;
        int numshadowmeshtriangles;
-       dheader_t _header;
        hullinfo_t hullinfo;
        int totalstylesurfaces, totalstyles, stylecounts[256], remapstyles[256];
        model_brush_lightstyleinfo_t styleinfo[256];
@@ -3359,97 +3390,48 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
 
        mod->type = mod_brushq1;
 
-       if (!memcmp (buffer, "MCBSPpad", 8))
-       {
-               unsigned char   *index;
-
-               mod->brush.ismcbsp = true;
-               mod->brush.ishlbsp = false;
-
-               mod_base = (unsigned char*)buffer;
-
-               index = mod_base;
-               index += 8;
-               i = SB_ReadInt (&index);
-               if (i != MCBSPVERSION)
-                       Host_Error("Mod_Q1BSP_Load: %s has wrong version number(%i should be %i)", mod->name, i, MCBSPVERSION);
-
-       // read hull info
-               hullinfo.numhulls = SB_ReadInt (&index);
-               hullinfo.filehulls = hullinfo.numhulls;
-               mod->brushq1.numhulls = hullinfo.numhulls;
+       header = (dheader_t *)buffer;
 
-               VectorClear (hullinfo.hullsizes[0][0]);
-               VectorClear (hullinfo.hullsizes[0][1]);
-               for (i = 1; i < hullinfo.numhulls; i++)
-               {
-                       hullinfo.hullsizes[i][0][0] = SB_ReadFloat (&index);
-                       hullinfo.hullsizes[i][0][1] = SB_ReadFloat (&index);
-                       hullinfo.hullsizes[i][0][2] = SB_ReadFloat (&index);
-                       hullinfo.hullsizes[i][1][0] = SB_ReadFloat (&index);
-                       hullinfo.hullsizes[i][1][1] = SB_ReadFloat (&index);
-                       hullinfo.hullsizes[i][1][2] = SB_ReadFloat (&index);
-               }
+       i = LittleLong(header->version);
+       if (i != BSPVERSION && i != 30)
+               Host_Error("Mod_Q1BSP_Load: %s has wrong version number(%i should be %i(Quake) or 30(HalfLife)", mod->name, i, BSPVERSION);
+       mod->brush.ishlbsp = i == 30;
 
-       // read lumps
-               _header.version = 0;
-               for (i = 0; i < HEADER_LUMPS; i++)
-               {
-                       _header.lumps[i].fileofs = SB_ReadInt (&index);
-                       _header.lumps[i].filelen = SB_ReadInt (&index);
-               }
+// fill in hull info
+       VectorClear (hullinfo.hullsizes[0][0]);
+       VectorClear (hullinfo.hullsizes[0][1]);
+       if (mod->brush.ishlbsp)
+       {
+               mod->modeldatatypestring = "HLBSP";
 
-               header = &_header;
+               hullinfo.filehulls = 4;
+               VectorSet (hullinfo.hullsizes[1][0], -16, -16, -36);
+               VectorSet (hullinfo.hullsizes[1][1], 16, 16, 36);
+               VectorSet (hullinfo.hullsizes[2][0], -32, -32, -32);
+               VectorSet (hullinfo.hullsizes[2][1], 32, 32, 32);
+               VectorSet (hullinfo.hullsizes[3][0], -16, -16, -18);
+               VectorSet (hullinfo.hullsizes[3][1], 16, 16, 18);
        }
        else
        {
-               header = (dheader_t *)buffer;
-
-               i = LittleLong(header->version);
-               if (i != BSPVERSION && i != 30)
-                       Host_Error("Mod_Q1BSP_Load: %s has wrong version number(%i should be %i(Quake) or 30(HalfLife)", mod->name, i, BSPVERSION);
-               mod->brush.ishlbsp = i == 30;
-               mod->brush.ismcbsp = false;
-
-       // fill in hull info
-               VectorClear (hullinfo.hullsizes[0][0]);
-               VectorClear (hullinfo.hullsizes[0][1]);
-               if (mod->brush.ishlbsp)
-               {
-                       mod->modeldatatypestring = "HLBSP";
-
-                       hullinfo.numhulls = 4;
-                       hullinfo.filehulls = 4;
-                       mod->brushq1.numhulls = 4;
-                       VectorSet (hullinfo.hullsizes[1][0], -16, -16, -36);
-                       VectorSet (hullinfo.hullsizes[1][1], 16, 16, 36);
-                       VectorSet (hullinfo.hullsizes[2][0], -32, -32, -32);
-                       VectorSet (hullinfo.hullsizes[2][1], 32, 32, 32);
-                       VectorSet (hullinfo.hullsizes[3][0], -16, -16, -18);
-                       VectorSet (hullinfo.hullsizes[3][1], 16, 16, 18);
-               }
-               else
-               {
-                       hullinfo.numhulls = 3;
-                       hullinfo.filehulls = 4;
-                       mod->brushq1.numhulls = 3;
-                       VectorSet (hullinfo.hullsizes[1][0], -16, -16, -24);
-                       VectorSet (hullinfo.hullsizes[1][1], 16, 16, 32);
-                       VectorSet (hullinfo.hullsizes[2][0], -32, -32, -24);
-                       VectorSet (hullinfo.hullsizes[2][1], 32, 32, 64);
-               }
+               hullinfo.filehulls = 4;
+               VectorSet (hullinfo.hullsizes[1][0], -16, -16, -24);
+               VectorSet (hullinfo.hullsizes[1][1], 16, 16, 32);
+               VectorSet (hullinfo.hullsizes[2][0], -32, -32, -24);
+               VectorSet (hullinfo.hullsizes[2][1], 32, 32, 64);
+       }
 
-       // read lumps
-               mod_base = (unsigned char*)buffer;
-               for (i = 0; i < HEADER_LUMPS; i++)
-               {
-                       header->lumps[i].fileofs = LittleLong(header->lumps[i].fileofs);
-                       header->lumps[i].filelen = LittleLong(header->lumps[i].filelen);
-               }
+// read lumps
+       mod_base = (unsigned char*)buffer;
+       for (i = 0; i < HEADER_LUMPS; i++)
+       {
+               header->lumps[i].fileofs = LittleLong(header->lumps[i].fileofs);
+               header->lumps[i].filelen = LittleLong(header->lumps[i].filelen);
        }
 
        mod->soundfromcenter = true;
        mod->TraceBox = Mod_Q1BSP_TraceBox;
+       mod->PointSuperContents = Mod_Q1BSP_PointSuperContents;
        mod->brush.TraceLineOfSight = Mod_Q1BSP_TraceLineOfSight;
        mod->brush.SuperContentsFromNativeContents = Mod_Q1BSP_SuperContentsFromNativeContents;
        mod->brush.NativeContentsFromSuperContents = Mod_Q1BSP_NativeContentsFromSuperContents;
@@ -3466,10 +3448,7 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
        mod->brush.PointInLeaf = Mod_Q1BSP_PointInLeaf;
 
        if (loadmodel->isworldmodel)
-       {
                Cvar_SetValue("halflifebsp", mod->brush.ishlbsp);
-               Cvar_SetValue("mcbsp", mod->brush.ismcbsp);
-       }
 
 // load into heap
 
@@ -3534,14 +3513,6 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
        if (loadmodel->brush.numsubmodels)
                loadmodel->brush.submodels = (model_t **)Mem_Alloc(loadmodel->mempool, loadmodel->brush.numsubmodels * sizeof(model_t *));
 
-       if (loadmodel->isworldmodel)
-       {
-               // clear out any stale submodels or worldmodels lying around
-               // if we did this clear before now, an error might abort loading and
-               // leave things in a bad state
-               Mod_RemoveStaleWorldModels(loadmodel);
-       }
-
        // LordHavoc: to clear the fog around the original quake submodel code, I
        // will explain:
        // first of all, some background info on the submodels:
@@ -3644,11 +3615,15 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
                        mod->brush.LightPoint = NULL;
                        mod->brush.AmbientSoundLevelsForPoint = NULL;
                }
+               // copy the submodel bounds, then enlarge the yaw and rotated bounds according to radius
+               VectorCopy(bm->mins, mod->normalmins);
+               VectorCopy(bm->maxs, mod->normalmaxs);
+               VectorCopy(bm->mins, mod->yawmins);
+               VectorCopy(bm->maxs, mod->yawmaxs);
+               VectorCopy(bm->mins, mod->rotatedmins);
+               VectorCopy(bm->maxs, mod->rotatedmaxs);
                if (mod->nummodelsurfaces)
                {
-                       // LordHavoc: calculate bmodel bounding box rather than trusting what it says
-                       mod->normalmins[0] = mod->normalmins[1] = mod->normalmins[2] = 1000000000.0f;
-                       mod->normalmaxs[0] = mod->normalmaxs[1] = mod->normalmaxs[2] = -1000000000.0f;
                        modelyawradius = 0;
                        modelradius = 0;
                        for (j = 0, surface = &mod->data_surfaces[mod->firstmodelsurface];j < mod->nummodelsurfaces;j++, surface++)
@@ -3661,12 +3636,6 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
                                // calculate bounding shapes
                                for (k = 0, vec = (loadmodel->surfmesh.data_vertex3f + 3 * surface->num_firstvertex);k < surface->num_vertices;k++, vec += 3)
                                {
-                                       if (mod->normalmins[0] > vec[0]) mod->normalmins[0] = vec[0];
-                                       if (mod->normalmins[1] > vec[1]) mod->normalmins[1] = vec[1];
-                                       if (mod->normalmins[2] > vec[2]) mod->normalmins[2] = vec[2];
-                                       if (mod->normalmaxs[0] < vec[0]) mod->normalmaxs[0] = vec[0];
-                                       if (mod->normalmaxs[1] < vec[1]) mod->normalmaxs[1] = vec[1];
-                                       if (mod->normalmaxs[2] < vec[2]) mod->normalmaxs[2] = vec[2];
                                        dist = vec[0]*vec[0]+vec[1]*vec[1];
                                        if (modelyawradius < dist)
                                                modelyawradius = dist;
@@ -3677,11 +3646,16 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
                        }
                        modelyawradius = sqrt(modelyawradius);
                        modelradius = sqrt(modelradius);
-                       mod->yawmins[0] = mod->yawmins[1] = - (mod->yawmaxs[0] = mod->yawmaxs[1] = modelyawradius);
-                       mod->yawmins[2] = mod->normalmins[2];
-                       mod->yawmaxs[2] = mod->normalmaxs[2];
-                       mod->rotatedmins[0] = mod->rotatedmins[1] = mod->rotatedmins[2] = -modelradius;
-                       mod->rotatedmaxs[0] = mod->rotatedmaxs[1] = mod->rotatedmaxs[2] = modelradius;
+                       mod->yawmins[0] = min(mod->yawmins[0], -modelyawradius);
+                       mod->yawmaxs[0] = min(mod->yawmaxs[0], -modelyawradius);
+                       mod->yawmins[1] = min(mod->yawmins[1],  modelyawradius);
+                       mod->yawmaxs[1] = min(mod->yawmaxs[1],  modelyawradius);
+                       mod->rotatedmins[0] = min(mod->rotatedmins[0], -modelradius);
+                       mod->rotatedmaxs[0] = min(mod->rotatedmaxs[0],  modelradius);
+                       mod->rotatedmins[1] = min(mod->rotatedmins[1], -modelradius);
+                       mod->rotatedmaxs[1] = min(mod->rotatedmaxs[1],  modelradius);
+                       mod->rotatedmins[2] = min(mod->rotatedmins[2], -modelradius);
+                       mod->rotatedmaxs[2] = min(mod->rotatedmaxs[2],  modelradius);
                        mod->radius = modelradius;
                        mod->radius2 = modelradius * modelradius;
 
@@ -4134,12 +4108,8 @@ void static Mod_Q2BSP_Load(model_t *mod, void *buffer, void *bufferend)
        if (i != Q2BSPVERSION)
                Host_Error("Mod_Q2BSP_Load: %s has wrong version number (%i, should be %i)", mod->name, i, Q2BSPVERSION);
        mod->brush.ishlbsp = false;
-       mod->brush.ismcbsp = false;
        if (loadmodel->isworldmodel)
-       {
                Cvar_SetValue("halflifebsp", mod->brush.ishlbsp);
-               Cvar_SetValue("mcbsp", mod->brush.ismcbsp);
-       }
 
        mod_base = (unsigned char *)header;
 
@@ -4227,7 +4197,7 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
 {
        q3dtexture_t *in;
        texture_t *out;
-       int i, count, c;
+       int i, count;
 
        in = (q3dtexture_t *)(mod_base + l->fileofs);
        if (l->filelen % sizeof(*in))
@@ -4249,12 +4219,8 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
        if (cls.state == ca_dedicated)
                return;
 
-       c = 0;
        for (i = 0;i < count;i++, in++, out++)
-               if (!Mod_LoadTextureFromQ3Shader(out, out->name, false, true, false))
-                       c++;
-       if (c)
-               Con_DPrintf("%s: %i textures missing shaders\n", loadmodel->name, c);
+               Mod_LoadTextureFromQ3Shader(out, out->name, true, true, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE | (r_picmipworld.integer ? TEXF_PICMIP : 0) | TEXF_COMPRESS);
 }
 
 static void Mod_Q3BSP_LoadPlanes(lump_t *l)
@@ -4501,14 +4467,15 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
                        }
                }
        }
-       if (endlightmap < 2)
-               loadmodel->brushq3.deluxemapping = false;
 
        // q3map2 sometimes (or always?) makes a second blank lightmap for no
        // reason when only one lightmap is used, which can throw off the
        // deluxemapping detection method, so check 2-lightmap bsp's specifically
        // to see if the second lightmap is blank, if so it is not deluxemapped.
-       if (endlightmap == 1 && count == 2)
+       //
+       // further research has shown q3map2 sometimes creates a deluxemap and two
+       // blank lightmaps, which must be handled properly as well
+       if (endlightmap == 1 && count > 1)
        {
                c = in[1].rgb;
                for (i = 0;i < 128*128*3;i++)
@@ -4568,7 +4535,8 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
                                        ;
                                for (mergeheight = 1;mergewidth*mergeheight < j && mergeheight < (1 << power);mergeheight *= 2)
                                        ;
-                               Con_DPrintf("lightmap merge texture #%i is %ix%i (%i of %i used)\n", lightmapindex, mergewidth*128, mergeheight*128, min(j, mergewidth*mergeheight), mergewidth*mergeheight);
+                               if (developer_loading.integer)
+                                       Con_Printf("lightmap merge texture #%i is %ix%i (%i of %i used)\n", lightmapindex, mergewidth*128, mergeheight*128, min(j, mergewidth*mergeheight), mergewidth*mergeheight);
                                loadmodel->brushq3.data_lightmaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%04i", lightmapindex), mergewidth * 128, mergeheight * 128, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_PRECACHE | (gl_texturecompression_q3bsplightmaps.integer ? TEXF_COMPRESS : 0), NULL);
                                if (loadmodel->brushq3.data_deluxemaps)
                                        loadmodel->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("deluxemap%04i", lightmapindex), mergewidth * 128, mergeheight * 128, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_PRECACHE | (gl_texturecompression_q3bspdeluxemaps.integer ? TEXF_COMPRESS : 0), NULL);
@@ -4628,7 +4596,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
        {
                // check face type first
                type = LittleLong(in->type);
-               if (type != Q3FACETYPE_POLYGON
+               if (type != Q3FACETYPE_FLAT
                 && type != Q3FACETYPE_PATCH
                 && type != Q3FACETYPE_MESH
                 && type != Q3FACETYPE_FLARE)
@@ -4697,7 +4665,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                }
                switch(type)
                {
-               case Q3FACETYPE_POLYGON:
+               case Q3FACETYPE_FLAT:
                case Q3FACETYPE_MESH:
                        // no processing necessary
                        break;
@@ -4762,7 +4730,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                out->num_firsttriangle = meshtriangles;
                switch(type)
                {
-               case Q3FACETYPE_POLYGON:
+               case Q3FACETYPE_FLAT:
                case Q3FACETYPE_MESH:
                        // no processing necessary, except for lightmap merging
                        for (j = 0;j < out->num_vertices;j++)
@@ -4940,6 +4908,11 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
        // for per pixel lighting
        Mod_BuildTextureVectorsFromNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_texcoordtexture2f, loadmodel->surfmesh.data_normal3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_svector3f, loadmodel->surfmesh.data_tvector3f, true);
 
+       // generate ushort elements array if possible
+       if (loadmodel->surfmesh.data_element3s)
+               for (i = 0;i < loadmodel->surfmesh.num_triangles*3;i++)
+                       loadmodel->surfmesh.data_element3s[i] = loadmodel->surfmesh.data_element3i[i];
+
        // free the no longer needed vertex data
        loadmodel->brushq3.num_vertices = 0;
        if (loadmodel->brushq3.data_vertex3f)
@@ -5219,7 +5192,7 @@ static void Mod_Q3BSP_LightPoint(model_t *model, const vec3_t p, vec3_t ambientc
        q3dlightgrid_t *a, *s;
 
        // scale lighting by lightstyle[0] so that darkmode in dpmod works properly
-       stylescale = r_refdef.rtlightstylevalue[0];
+       stylescale = r_refdef.scene.rtlightstylevalue[0];
 
        if (!model->brushq3.num_lightgrid)
        {
@@ -5284,7 +5257,7 @@ static void Mod_Q3BSP_TracePoint_RecursiveBSPNode(trace_t *trace, model_t *model
        colbrushf_t *brush;
        // find which leaf the point is in
        while (node->plane)
-               node = node->children[DotProduct(point, node->plane->normal) < node->plane->dist];
+               node = node->children[(node->plane->type < 3 ? point[node->plane->type] : DotProduct(point, node->plane->normal)) < node->plane->dist];
        // point trace the brushes
        leaf = (mleaf_t *)node;
        for (i = 0;i < leaf->numleafbrushes;i++)
@@ -5547,6 +5520,38 @@ static void Mod_Q3BSP_TraceBox(model_t *model, int frame, trace_t *trace, const
        }
 }
 
+static int Mod_Q3BSP_PointSuperContents(struct model_s *model, int frame, const vec3_t point)
+{
+       int i;
+       int supercontents = 0;
+       q3mbrush_t *brush;
+       // test if the point is inside each brush
+       if (model->brush.submodel)
+       {
+               // submodels are effectively one leaf
+               for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
+                       if (brush->colbrushf && Collision_PointInsideBrushFloat(point, brush->colbrushf))
+                               supercontents |= brush->colbrushf->supercontents;
+       }
+       else
+       {
+               mnode_t *node = model->brush.data_nodes;
+               mleaf_t *leaf;
+               // find which leaf the point is in
+               while (node->plane)
+                       node = node->children[(node->plane->type < 3 ? point[node->plane->type] : DotProduct(point, node->plane->normal)) < node->plane->dist];
+               leaf = (mleaf_t *)node;
+               // now check the brushes in the leaf
+               for (i = 0;i < leaf->numleafbrushes;i++)
+               {
+                       brush = model->brush.data_brushes + leaf->firstleafbrush[i];
+                       if (brush->colbrushf && Collision_PointInsideBrushFloat(point, brush->colbrushf))
+                               supercontents |= brush->colbrushf->supercontents;
+               }
+       }
+       return supercontents;
+}
+
 static int Mod_Q3BSP_SuperContentsFromNativeContents(model_t *model, int nativecontents)
 {
        int supercontents = 0;
@@ -5631,15 +5636,12 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer, void *bufferend)
        if (i != Q3BSPVERSION)
                Host_Error("Mod_Q3BSP_Load: %s has wrong version number (%i, should be %i)", mod->name, i, Q3BSPVERSION);
        mod->brush.ishlbsp = false;
-       mod->brush.ismcbsp = false;
        if (loadmodel->isworldmodel)
-       {
                Cvar_SetValue("halflifebsp", mod->brush.ishlbsp);
-               Cvar_SetValue("mcbsp", mod->brush.ismcbsp);
-       }
 
        mod->soundfromcenter = true;
        mod->TraceBox = Mod_Q3BSP_TraceBox;
+       mod->PointSuperContents = Mod_Q3BSP_PointSuperContents;
        mod->brush.TraceLineOfSight = Mod_Q1BSP_TraceLineOfSight;
        mod->brush.SuperContentsFromNativeContents = Mod_Q3BSP_SuperContentsFromNativeContents;
        mod->brush.NativeContentsFromSuperContents = Mod_Q3BSP_NativeContentsFromSuperContents;
@@ -5726,14 +5728,6 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer, void *bufferend)
        loadmodel->brush.num_leafs = 0;
        Mod_Q3BSP_RecursiveFindNumLeafs(loadmodel->brush.data_nodes);
 
-       if (loadmodel->isworldmodel)
-       {
-               // clear out any stale submodels or worldmodels lying around
-               // if we did this clear before now, an error might abort loading and
-               // leave things in a bad state
-               Mod_RemoveStaleWorldModels(loadmodel);
-       }
-
        mod = loadmodel;
        for (i = 0;i < loadmodel->brush.numsubmodels;i++)
        {
@@ -5776,6 +5770,30 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer, void *bufferend)
 
                VectorCopy(mod->brushq3.data_models[i].mins, mod->normalmins);
                VectorCopy(mod->brushq3.data_models[i].maxs, mod->normalmaxs);
+               // enlarge the bounding box to enclose all geometry of this model,
+               // because q3map2 sometimes lies (mostly to affect the lightgrid),
+               // which can in turn mess up the farclip (as well as culling when
+               // outside the level - an unimportant concern)
+
+               //printf("Editing model %d... BEFORE re-bounding: %f %f %f - %f %f %f\n", i, mod->normalmins[0], mod->normalmins[1], mod->normalmins[2], mod->normalmaxs[0], mod->normalmaxs[1], mod->normalmaxs[2]);
+               for (j = 0;j < mod->nummodelsurfaces;j++)
+               {
+                       const msurface_t *surface = mod->data_surfaces + j + mod->firstmodelsurface;
+                       const float *v = mod->surfmesh.data_vertex3f + 3 * surface->num_firstvertex;
+                       int k;
+                       if (!surface->num_vertices)
+                               continue;
+                       for (k = 0;k < surface->num_vertices;k++, v += 3)
+                       {
+                               mod->normalmins[0] = min(mod->normalmins[0], v[0]);
+                               mod->normalmins[1] = min(mod->normalmins[1], v[1]);
+                               mod->normalmins[2] = min(mod->normalmins[2], v[2]);
+                               mod->normalmaxs[0] = max(mod->normalmaxs[0], v[0]);
+                               mod->normalmaxs[1] = max(mod->normalmaxs[1], v[1]);
+                               mod->normalmaxs[2] = max(mod->normalmaxs[2], v[2]);
+                       }
+               }
+               //printf("Editing model %d... AFTER re-bounding: %f %f %f - %f %f %f\n", i, mod->normalmins[0], mod->normalmins[1], mod->normalmins[2], mod->normalmaxs[0], mod->normalmaxs[1], mod->normalmaxs[2]);
                corner[0] = max(fabs(mod->normalmins[0]), fabs(mod->normalmaxs[0]));
                corner[1] = max(fabs(mod->normalmins[1]), fabs(mod->normalmaxs[1]));
                corner[2] = max(fabs(mod->normalmins[2]), fabs(mod->normalmaxs[2]));