eclass.cpp

来自「quake3工具源码。包括生成bsp文件」· C++ 代码 · 共 912 行 · 第 1/2 页

CPP
912 行
字号
	
// get the flags
	

// copy to the first /n
	p = parms;
	while (*text && *text != '\n')
		*p++ = *text++;
	*p = 0;
	text++;
	
// any remaining words are parm flags
	p = parms;
	for (i=0 ; i<8 ; i++)
	{
		p = COM_Parse (p);
		if (!p)
			break;
		strcpy (e->flagnames[i], com_token);
	} 

// find the length until close comment
	for (t=text ; t[0] && !(t[0]=='*' && t[1]=='/') ; t++)
	;
	
// copy the comment block out
	len = t-text;
	e->comments = (char*)qmalloc (len+1);
	memcpy (e->comments, text, len);
#if 0
	for (i=0 ; i<len ; i++)
		if (text[i] == '\n')
			e->comments[i] = '\r';
		else
			e->comments[i] = text[i];
#endif
	e->comments[len] = 0;
	
  setSpecialLoad(e, "model=", e->modelpath);
  setSpecialLoad(e, "skin=", e->skinpath);
  char *pFrame = NULL;
  setSpecialLoad(e, "frame=", pFrame);
  if (pFrame != NULL)
  {
    e->nFrame = atoi(pFrame);
  }

  if(!e->skinpath)
	  setSpecialLoad(e, "texture=", e->skinpath);

  // setup show flags
  e->nShowFlags = 0;
  if (strcmpi(e->name, "light") == 0)
  {
    e->nShowFlags |= ECLASS_LIGHT;
  }

  if (  (strnicmp(e->name, "info_player", strlen("info_player")) == 0)
      ||(strnicmp(e->name, "path_corner", strlen("path_corner")) == 0) 
      ||(strnicmp(e->name, "team_ctf", strlen("team_ctf")) == 0) )
  {
    e->nShowFlags |= ECLASS_ANGLE;
  }
  if (strcmpi(e->name, "path") == 0)
  {
    e->nShowFlags |= ECLASS_PATH;
  }
  if (strcmpi(e->name, "misc_model") == 0)
  {
    e->nShowFlags |= ECLASS_MISCMODEL;
  }


  return e;
}

qboolean Eclass_hasModel(eclass_t *e, vec3_t &vMin, vec3_t &vMax)
{
  if (e->modelpath != NULL)
  {
    if (e->model == NULL)
    {
      e->model = reinterpret_cast<entitymodel_t*>(qmalloc(sizeof(entitymodel_t)));
    }
    char *pModelBuff = strdup(e->modelpath);
    char *pSkinBuff = NULL;
    if (e->skinpath)
    {
      pSkinBuff = strdup(e->skinpath);
    }

    CStringList Models;
    CStringList Skins;
    char* pToken = strtok(pModelBuff, ";\0");
    while (pToken != NULL)
    {
      Models.AddTail(pToken);
      pToken = strtok(NULL, ";\0");
    }

    if (pSkinBuff != NULL)
    {
      pToken = strtok(pSkinBuff, ";\0");
      while (pToken != NULL)
      {
        Skins.AddTail(pToken);
        pToken = strtok(NULL, ";\0");
      }
    }

    entitymodel *model = e->model;
    for (int i = 0; i < Models.GetCount(); i++)
    {
      char *pSkin = NULL;
      if (i < Skins.GetCount())
      {
        pSkin = Skins.GetAt(Skins.FindIndex(i)).GetBuffer(0);
      }
      LoadModel(Models.GetAt(Models.FindIndex(i)), e, vMin, vMax, model, pSkin);
      model->pNext = reinterpret_cast<entitymodel_t*>(qmalloc(sizeof(entitymodel_t)));
      model = model->pNext;
    }

    // at this poitn vMin and vMax contain the min max of the model
    // which needs to be centered at origin 0, 0, 0

    VectorSnap(vMin);
    VectorSnap(vMax);

    if (vMax[0] - vMin[0] < 2)
    {
      vMin[0] -= 1;
      vMax[0] += 1;
    }

    if (vMin[1] - vMax[1] < 2)
    {
      vMin[1] -= 1;
      vMax[1] += 1;
    }

    if (vMax[2] - vMin[2] < 2)
    {
      vMax[2] -= 1;
      vMax[2] += 1;
    }

    vec3_t vTemp;
    VectorAdd(vMin, vMax, vTemp);
    VectorScale(vTemp, 0.5, vTemp);
    model = e->model;
    while (model != NULL)
    {
      for (i = 0; i < model->nTriCount; i++)
      {
        for (int j = 0; j < 3; j++)
        {
          ;//VectorSubtract(model->pTriList[i].v[j], vTemp, model->pTriList[i].v[j]);
        }
      }
      model = model->pNext;
    }

    free(pModelBuff);
    free(e->modelpath);
    e->modelpath = NULL;
	  
    if(e->skinpath)
	  {
		  free(e->skinpath);
		  e->skinpath = NULL;
      free(pSkinBuff);
	  }

  }
  return (e->model != NULL && e->model->nTriCount > 0);
}


void EClass_InsertSortedList(eclass_t *&pList, eclass_t *e)
{
	eclass_t	*s;
	
	if (!pList)
	{
		pList = e;
		return;
	}


	s = pList;
	if (stricmp (e->name, s->name) < 0)
	{
		e->next = s;
		pList = e;
		return;
	}

	do
	{
		if (!s->next || stricmp (e->name, s->next->name) < 0)
		{
			e->next = s->next;
			s->next = e;
			return;
		}
		s=s->next;
	} while (1);
}

/*
=================
Eclass_InsertAlphabetized
=================
*/
void Eclass_InsertAlphabetized (eclass_t *e)
{
#if 1
  EClass_InsertSortedList(eclass, e);
#else
	eclass_t	*s;
	
	if (!eclass)
	{
		eclass = e;
		return;
	}


	s = eclass;
	if (stricmp (e->name, s->name) < 0)
	{
		e->next = s;
		eclass = e;
		return;
	}

	do
	{
		if (!s->next || stricmp (e->name, s->next->name) < 0)
		{
			e->next = s->next;
			s->next = e;
			return;
		}
		s=s->next;
	} while (1);
#endif
}


/*
=================
Eclass_ScanFile
=================
*/

qboolean parsing_single = false;
qboolean eclass_found;
eclass_t *eclass_e;
//#ifdef BUILD_LIST
extern bool g_bBuildList;
CString strDefFile;
//#endif
void Eclass_ScanFile (char *filename)
{
	int		size;
	char	*data;
	eclass_t	*e;
	int		i;
	char    temp[1024];
	
	QE_ConvertDOSToUnixName( temp, filename );
	
	Sys_Printf ("ScanFile: %s\n", temp);
	
	// BUG
	size = LoadFile (filename, (void**)&data);
	eclass_found = false;
	for (i=0 ; i<size ; i++)
		if (!strncmp(data+i, "/*QUAKED",8))
		{
			
			//#ifdef BUILD_LIST
			if (g_bBuildList)
			{
				CString strDef = "";
				int j = i;
				while (1)
				{
					strDef += *(data+j);
					if (*(data+j) == '/' && *(data+j-1) == '*')
						break;
					j++;
				}
				strDef += "\r\n\r\n\r\n";
				strDefFile += strDef;
			}
			//#endif
			e = Eclass_InitFromText (data+i);
			if (e)
				Eclass_InsertAlphabetized (e);
			else
				printf ("Error parsing: %s in %s\n",debugname, filename);

			// single ?
			eclass_e = e;
			eclass_found = true;
			if ( parsing_single )
				break;
		}
		
		free (data);		
}



void Eclass_InitForSourceDirectory (char *path)
{
	struct _finddata_t fileinfo;
	int		handle;
	char	filename[1024];
	char	filebase[1024];
	char    temp[1024];
	char	*s;

	QE_ConvertDOSToUnixName( temp, path );

	Sys_Printf ("Eclass_InitForSourceDirectory: %s\n", temp );

	strcpy (filebase, path);
	s = filebase + strlen(filebase)-1;
	while (*s != '\\' && *s != '/' && s!=filebase)
		s--;
	*s = 0;

  CleanUpEntities();
	eclass = NULL;
//#ifdef BUILD_LIST
  if (g_bBuildList)
    strDefFile = "";
//#endif
	handle = _findfirst (path, &fileinfo);
	if (handle != -1)
	{
		do
		{
			sprintf (filename, "%s\\%s", filebase, fileinfo.name);
			Eclass_ScanFile (filename);
		} while (_findnext( handle, &fileinfo ) != -1);

		_findclose (handle);
	}

//#ifdef BUILD_LIST
  if (g_bBuildList)
  {
    CFile file;
    if (file.Open("c:\\entities.def", CFile::modeCreate | CFile::modeWrite)) 
    {
      file.Write(strDefFile.GetBuffer(0), strDefFile.GetLength());
      file.Close();
    }
  }
//#endif

	eclass_bad = Eclass_InitFromText ("/*QUAKED UNKNOWN_CLASS (0 0.5 0) ?");
}

eclass_t *Eclass_ForName (char *name, qboolean has_brushes)
{
	eclass_t	*e;
	char		init[1024];

#ifdef _DEBUG
  // grouping stuff, not an eclass
  if (strcmp(name, "group_info")==0)
    Sys_Printf("WARNING: unexpected group_info entity in Eclass_ForName\n");
#endif

	if (!name)
		return eclass_bad;

	for (e=eclass ; e ; e=e->next)
		if (!strcmp (name, e->name))
			return e;

	// create a new class for it
	if (has_brushes)
	{
		sprintf (init, "/*QUAKED %s (0 0.5 0) ?\nNot found in source.\n", name);
		e = Eclass_InitFromText (init);
	}
	else
	{
		sprintf (init, "/*QUAKED %s (0 0.5 0) (-8 -8 -8) (8 8 8)\nNot found in source.\n", name);
		e = Eclass_InitFromText (init);
	}

	Eclass_InsertAlphabetized (e);

	return e;
}


eclass_t* GetCachedModel(entity_t *pEntity, const char *pName, vec3_t &vMin, vec3_t &vMax)
{

	eclass_t *e = NULL;
  if (pName == NULL || strlen(pName) == 0)
  {
    return NULL;
  }

	for (e = g_md3Cache; e ; e = e->next)
  {
		if (!strcmp (pName, e->name))
    {
      pEntity->md3Class = e;
      VectorCopy(e->mins, vMin);
      VectorCopy(e->maxs, vMax);
			return e;
    }
  }

	e = (eclass_t*)qmalloc(sizeof(*e));
	memset (e, 0, sizeof(*e));
  e->name = strdup(pName);
  e->modelpath = strdup(pName);
  e->skinpath = strdup(pName);
  char *p = strstr(e->skinpath, ".md3");
  if (p != NULL)
  {
    p++;
    strncpy(p, "tga", 3);
  }
  else
  {
    free(e->skinpath);
    e->skinpath = NULL;
  }

  e->color[0] = e->color[2] = 0.85;

  if (Eclass_hasModel(e, vMin, vMax))
  {
    EClass_InsertSortedList(g_md3Cache, e);
    VectorCopy(vMin, e->mins);
    VectorCopy(vMax, e->maxs);
    pEntity->md3Class = e;
    return e;
  }

  return NULL;
}

⌨️ 快捷键说明

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