⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 l_bsp_hl.c

📁 quakeIII源码这个不用我多说吧
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.

This file is part of Quake III Arena source code.

Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/
#include "l_cmd.h"
#include "l_math.h"
#include "l_mem.h"
#include "l_log.h"
#include "../botlib/l_script.h"
#include "l_bsp_hl.h"
#include "l_bsp_ent.h"

//=============================================================================

int				hl_nummodels;
hl_dmodel_t		*hl_dmodels;//[HL_MAX_MAP_MODELS];
int				hl_dmodels_checksum;

int				hl_visdatasize;
byte				*hl_dvisdata;//[HL_MAX_MAP_VISIBILITY];
int				hl_dvisdata_checksum;

int				hl_lightdatasize;
byte				*hl_dlightdata;//[HL_MAX_MAP_LIGHTING];
int				hl_dlightdata_checksum;

int				hl_texdatasize;
byte				*hl_dtexdata;//[HL_MAX_MAP_MIPTEX]; // (dmiptexlump_t)
int				hl_dtexdata_checksum;

int				hl_entdatasize;
char				*hl_dentdata;//[HL_MAX_MAP_ENTSTRING];
int				hl_dentdata_checksum;

int				hl_numleafs;
hl_dleaf_t		*hl_dleafs;//[HL_MAX_MAP_LEAFS];
int				hl_dleafs_checksum;

int				hl_numplanes;
hl_dplane_t		*hl_dplanes;//[HL_MAX_MAP_PLANES];
int				hl_dplanes_checksum;

int				hl_numvertexes;
hl_dvertex_t	*hl_dvertexes;//[HL_MAX_MAP_VERTS];
int				hl_dvertexes_checksum;

int				hl_numnodes;
hl_dnode_t		*hl_dnodes;//[HL_MAX_MAP_NODES];
int				hl_dnodes_checksum;

int				hl_numtexinfo;
hl_texinfo_t	*hl_texinfo;//[HL_MAX_MAP_TEXINFO];
int				hl_texinfo_checksum;

int				hl_numfaces;
hl_dface_t		*hl_dfaces;//[HL_MAX_MAP_FACES];
int				hl_dfaces_checksum;

int				hl_numclipnodes;
hl_dclipnode_t	*hl_dclipnodes;//[HL_MAX_MAP_CLIPNODES];
int				hl_dclipnodes_checksum;

int				hl_numedges;
hl_dedge_t		*hl_dedges;//[HL_MAX_MAP_EDGES];
int				hl_dedges_checksum;

int				hl_nummarksurfaces;
unsigned short	*hl_dmarksurfaces;//[HL_MAX_MAP_MARKSURFACES];
int				hl_dmarksurfaces_checksum;

int				hl_numsurfedges;
int				*hl_dsurfedges;//[HL_MAX_MAP_SURFEDGES];
int				hl_dsurfedges_checksum;

//int				num_entities;
//entity_t			entities[HL_MAX_MAP_ENTITIES];


//#ifdef //ME

int hl_bspallocated = false;
int hl_allocatedbspmem = 0;

void HL_AllocMaxBSP(void)
{
	//models
	hl_nummodels = 0;
	hl_dmodels = (hl_dmodel_t *) GetMemory(HL_MAX_MAP_MODELS * sizeof(hl_dmodel_t));
	hl_allocatedbspmem = HL_MAX_MAP_MODELS * sizeof(hl_dmodel_t);
	//visibility
	hl_visdatasize = 0;
	hl_dvisdata = (byte *) GetMemory(HL_MAX_MAP_VISIBILITY * sizeof(byte));
	hl_allocatedbspmem += HL_MAX_MAP_VISIBILITY * sizeof(byte);
	//light data
	hl_lightdatasize = 0;
	hl_dlightdata = (byte *) GetMemory(HL_MAX_MAP_LIGHTING * sizeof(byte));
	hl_allocatedbspmem += HL_MAX_MAP_LIGHTING * sizeof(byte);
	//texture data
	hl_texdatasize = 0;
	hl_dtexdata = (byte *) GetMemory(HL_MAX_MAP_MIPTEX * sizeof(byte)); // (dmiptexlump_t)
	hl_allocatedbspmem += HL_MAX_MAP_MIPTEX * sizeof(byte);
	//entities
	hl_entdatasize = 0;
	hl_dentdata = (char *) GetMemory(HL_MAX_MAP_ENTSTRING * sizeof(char));
	hl_allocatedbspmem += HL_MAX_MAP_ENTSTRING * sizeof(char);
	//leaves
	hl_numleafs = 0;
	hl_dleafs = (hl_dleaf_t *) GetMemory(HL_MAX_MAP_LEAFS * sizeof(hl_dleaf_t));
	hl_allocatedbspmem += HL_MAX_MAP_LEAFS * sizeof(hl_dleaf_t);
	//planes
	hl_numplanes = 0;
	hl_dplanes = (hl_dplane_t *) GetMemory(HL_MAX_MAP_PLANES * sizeof(hl_dplane_t));
	hl_allocatedbspmem += HL_MAX_MAP_PLANES * sizeof(hl_dplane_t);
	//vertexes
	hl_numvertexes = 0;
	hl_dvertexes = (hl_dvertex_t *) GetMemory(HL_MAX_MAP_VERTS * sizeof(hl_dvertex_t));
	hl_allocatedbspmem += HL_MAX_MAP_VERTS * sizeof(hl_dvertex_t);
	//nodes
	hl_numnodes = 0;
	hl_dnodes = (hl_dnode_t *) GetMemory(HL_MAX_MAP_NODES * sizeof(hl_dnode_t));
	hl_allocatedbspmem += HL_MAX_MAP_NODES * sizeof(hl_dnode_t);
	//texture info
	hl_numtexinfo = 0;
	hl_texinfo = (hl_texinfo_t *) GetMemory(HL_MAX_MAP_TEXINFO * sizeof(hl_texinfo_t));
	hl_allocatedbspmem += HL_MAX_MAP_TEXINFO * sizeof(hl_texinfo_t);
	//faces
	hl_numfaces = 0;
	hl_dfaces = (hl_dface_t *) GetMemory(HL_MAX_MAP_FACES * sizeof(hl_dface_t));
	hl_allocatedbspmem += HL_MAX_MAP_FACES * sizeof(hl_dface_t);
	//clip nodes
	hl_numclipnodes = 0;
	hl_dclipnodes = (hl_dclipnode_t *) GetMemory(HL_MAX_MAP_CLIPNODES * sizeof(hl_dclipnode_t));
	hl_allocatedbspmem += HL_MAX_MAP_CLIPNODES * sizeof(hl_dclipnode_t);
	//edges
	hl_numedges = 0;
	hl_dedges = (hl_dedge_t *) GetMemory(HL_MAX_MAP_EDGES * sizeof(hl_dedge_t));
	hl_allocatedbspmem += HL_MAX_MAP_EDGES, sizeof(hl_dedge_t);
	//mark surfaces
	hl_nummarksurfaces = 0;
	hl_dmarksurfaces = (unsigned short *) GetMemory(HL_MAX_MAP_MARKSURFACES * sizeof(unsigned short));
	hl_allocatedbspmem += HL_MAX_MAP_MARKSURFACES * sizeof(unsigned short);
	//surface edges
	hl_numsurfedges = 0;
	hl_dsurfedges = (int *) GetMemory(HL_MAX_MAP_SURFEDGES * sizeof(int));
	hl_allocatedbspmem += HL_MAX_MAP_SURFEDGES * sizeof(int);
	//print allocated memory
	Log_Print("allocated ");
	PrintMemorySize(hl_allocatedbspmem);
	Log_Print(" of BSP memory\n");
} //end of the function HL_AllocMaxBSP

void HL_FreeMaxBSP(void)
{
	//models
	hl_nummodels = 0;
	FreeMemory(hl_dmodels);
	hl_dmodels = NULL;
	//visibility
	hl_visdatasize = 0;
	FreeMemory(hl_dvisdata);
	hl_dvisdata = NULL;
	//light data
	hl_lightdatasize = 0;
	FreeMemory(hl_dlightdata);
	hl_dlightdata = NULL;
	//texture data
	hl_texdatasize = 0;
	FreeMemory(hl_dtexdata);
	hl_dtexdata = NULL;
	//entities
	hl_entdatasize = 0;
	FreeMemory(hl_dentdata);
	hl_dentdata = NULL;
	//leaves
	hl_numleafs = 0;
	FreeMemory(hl_dleafs);
	hl_dleafs = NULL;
	//planes
	hl_numplanes = 0;
	FreeMemory(hl_dplanes);
	hl_dplanes = NULL;
	//vertexes
	hl_numvertexes = 0;
	FreeMemory(hl_dvertexes);
	hl_dvertexes = NULL;
	//nodes
	hl_numnodes = 0;
	FreeMemory(hl_dnodes);
	hl_dnodes = NULL;
	//texture info
	hl_numtexinfo = 0;
	FreeMemory(hl_texinfo);
	hl_texinfo = NULL;
	//faces
	hl_numfaces = 0;
	FreeMemory(hl_dfaces);
	hl_dfaces = NULL;
	//clip nodes
	hl_numclipnodes = 0;
	FreeMemory(hl_dclipnodes);
	hl_dclipnodes = NULL;
	//edges
	hl_numedges = 0;
	FreeMemory(hl_dedges);
	hl_dedges = NULL;
	//mark surfaces
	hl_nummarksurfaces = 0;
	FreeMemory(hl_dmarksurfaces);
	hl_dmarksurfaces = NULL;
	//surface edges
	hl_numsurfedges = 0;
	FreeMemory(hl_dsurfedges);
	hl_dsurfedges = NULL;
	//
	Log_Print("freed ");
	PrintMemorySize(hl_allocatedbspmem);
	Log_Print(" of BSP memory\n");
	hl_allocatedbspmem = 0;
} //end of the function HL_FreeMaxBSP
//#endif //ME

/*
===============
FastChecksum
===============
*/

int FastChecksum(void *buffer, int bytes)
{
	int	checksum = 0;

	while( bytes-- )  
		checksum = (checksum << 4) ^ *((char *)buffer)++;

	return checksum;
}

/*
===============
HL_CompressVis
===============
*/
int HL_CompressVis(byte *vis, byte *dest)
{
	int		j;
	int		rep;
	int		visrow;
	byte	*dest_p;
	
	dest_p = dest;
	visrow = (hl_numleafs + 7)>>3;
	
	for (j=0 ; j<visrow ; j++)
	{
		*dest_p++ = vis[j];
		if (vis[j])
			continue;

		rep = 1;
		for ( j++; j<visrow ; j++)
			if (vis[j] || rep == 255)
				break;
			else
				rep++;
		*dest_p++ = rep;
		j--;
	}
	
	return dest_p - dest;
}


/*
===================
HL_DecompressVis
===================
*/
void HL_DecompressVis (byte *in, byte *decompressed)
{
	int		c;
	byte	*out;
	int		row;

	row = (hl_numleafs+7)>>3;	
	out = decompressed;

	do
	{
		if (*in)
		{
			*out++ = *in++;
			continue;
		}
	
		c = in[1];
		in += 2;
		while (c)
		{
			*out++ = 0;
			c--;
		}
	} while (out - decompressed < row);
}

//=============================================================================

/*
=============
HL_SwapBSPFile

Byte swaps all data in a bsp file.
=============
*/
void HL_SwapBSPFile (qboolean todisk)
{
	int i, j, c;
	hl_dmodel_t *d;
	hl_dmiptexlump_t *mtl;

	
// models	
	for (i = 0; i < hl_nummodels; i++)
	{
		d = &hl_dmodels[i];

		for (j = 0; j < HL_MAX_MAP_HULLS; j++)
			d->headnode[j] = LittleLong(d->headnode[j]);

		d->visleafs = LittleLong(d->visleafs);
		d->firstface = LittleLong(d->firstface);
		d->numfaces = LittleLong(d->numfaces);
		
		for (j = 0; j < 3; j++)
		{
			d->mins[j] = LittleFloat(d->mins[j]);
			d->maxs[j] = LittleFloat(d->maxs[j]);
			d->origin[j] = LittleFloat(d->origin[j]);
		}
	}

//
// vertexes
//
	for (i = 0; i < hl_numvertexes; i++)
	{
		for (j = 0; j < 3; j++)
			hl_dvertexes[i].point[j] = LittleFloat (hl_dvertexes[i].point[j]);
	}
		
//
// planes
//	
	for (i=0 ; i<hl_numplanes ; i++)
	{
		for (j=0 ; j<3 ; j++)
			hl_dplanes[i].normal[j] = LittleFloat (hl_dplanes[i].normal[j]);
		hl_dplanes[i].dist = LittleFloat (hl_dplanes[i].dist);
		hl_dplanes[i].type = LittleLong (hl_dplanes[i].type);
	}
	
//
// texinfos
//	
	for (i=0 ; i<hl_numtexinfo ; i++)
	{
		for (j=0 ; j<8 ; j++)
			hl_texinfo[i].vecs[0][j] = LittleFloat (hl_texinfo[i].vecs[0][j]);
		hl_texinfo[i].miptex = LittleLong (hl_texinfo[i].miptex);
		hl_texinfo[i].flags = LittleLong (hl_texinfo[i].flags);
	}
	
//
// faces
//
	for (i=0 ; i<hl_numfaces ; i++)
	{
		hl_dfaces[i].texinfo = LittleShort (hl_dfaces[i].texinfo);
		hl_dfaces[i].planenum = LittleShort (hl_dfaces[i].planenum);
		hl_dfaces[i].side = LittleShort (hl_dfaces[i].side);
		hl_dfaces[i].lightofs = LittleLong (hl_dfaces[i].lightofs);
		hl_dfaces[i].firstedge = LittleLong (hl_dfaces[i].firstedge);
		hl_dfaces[i].numedges = LittleShort (hl_dfaces[i].numedges);
	}

//
// nodes
//
	for (i=0 ; i<hl_numnodes ; i++)
	{
		hl_dnodes[i].planenum = LittleLong (hl_dnodes[i].planenum);
		for (j=0 ; j<3 ; j++)
		{
			hl_dnodes[i].mins[j] = LittleShort (hl_dnodes[i].mins[j]);
			hl_dnodes[i].maxs[j] = LittleShort (hl_dnodes[i].maxs[j]);
		}
		hl_dnodes[i].children[0] = LittleShort (hl_dnodes[i].children[0]);
		hl_dnodes[i].children[1] = LittleShort (hl_dnodes[i].children[1]);
		hl_dnodes[i].firstface = LittleShort (hl_dnodes[i].firstface);
		hl_dnodes[i].numfaces = LittleShort (hl_dnodes[i].numfaces);
	}

//
// leafs
//
	for (i=0 ; i<hl_numleafs ; i++)
	{
		hl_dleafs[i].contents = LittleLong (hl_dleafs[i].contents);
		for (j=0 ; j<3 ; j++)
		{
			hl_dleafs[i].mins[j] = LittleShort (hl_dleafs[i].mins[j]);
			hl_dleafs[i].maxs[j] = LittleShort (hl_dleafs[i].maxs[j]);
		}

		hl_dleafs[i].firstmarksurface = LittleShort (hl_dleafs[i].firstmarksurface);
		hl_dleafs[i].nummarksurfaces = LittleShort (hl_dleafs[i].nummarksurfaces);
		hl_dleafs[i].visofs = LittleLong (hl_dleafs[i].visofs);
	}

//
// clipnodes
//
	for (i=0 ; i<hl_numclipnodes ; i++)
	{
		hl_dclipnodes[i].planenum = LittleLong (hl_dclipnodes[i].planenum);
		hl_dclipnodes[i].children[0] = LittleShort (hl_dclipnodes[i].children[0]);
		hl_dclipnodes[i].children[1] = LittleShort (hl_dclipnodes[i].children[1]);
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -