2007-11-04 03:51:54 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "entity.h"
|
|
|
|
#include "entity_entitymodel.h"
|
|
|
|
#include "light.h"
|
|
|
|
|
|
|
|
int g_entityId = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// internal
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void Entity_FreeEpairs( entity_t *e );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void SetKeyValue( epair_t *&e, const char *key, const char *value );
|
|
|
|
static void DeleteKey( epair_t *&e, const char *key );
|
|
|
|
static const char *ValueForKey( epair_t *&e, const char *key );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void Entity_OnKeyValueChanged( entity_t *e, const char* key, const char* value );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
// constructor
|
2012-03-17 20:01:54 +00:00
|
|
|
entity_t *Entity_Alloc(){
|
|
|
|
entity_t *e;
|
|
|
|
e = (entity_t*)malloc( sizeof( *e ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
e->entityId = g_entityId++;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSet( e->origin, 0, 0, 0 );
|
|
|
|
VectorSet( e->color, 1, 1, 1 );
|
|
|
|
e->redoId = 0;
|
|
|
|
e->undoId = 0;
|
|
|
|
e->next = e->prev = NULL;
|
2007-11-04 03:51:54 +00:00
|
|
|
e->brushes.onext = e->brushes.oprev = &e->brushes;
|
2012-03-17 20:01:54 +00:00
|
|
|
e->epairs = NULL;
|
|
|
|
e->eclass = NULL;
|
|
|
|
e->model.pRender = NULL;
|
|
|
|
e->model.pSelect = NULL;
|
|
|
|
e->model.pEdit = NULL;
|
|
|
|
return e;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// destructor
|
2012-03-17 20:01:54 +00:00
|
|
|
void Entity_Free( entity_t *e ){
|
|
|
|
while ( e->brushes.onext != &e->brushes )
|
2007-11-04 03:51:54 +00:00
|
|
|
Brush_Free( e->brushes.onext, true );
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( e->next ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
e->next->prev = e->prev;
|
|
|
|
e->prev->next = e->next;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Entity_FreeEpairs( e );
|
|
|
|
|
|
|
|
if ( e->model.pRender ) {
|
|
|
|
e->model.pRender->DecRef();
|
|
|
|
e->model.pRender = NULL;
|
|
|
|
}
|
|
|
|
if ( e->model.pSelect ) {
|
|
|
|
e->model.pSelect->DecRef();
|
|
|
|
e->model.pSelect = NULL;
|
|
|
|
}
|
|
|
|
if ( e->model.pEdit ) {
|
|
|
|
e->model.pEdit->DecRef();
|
|
|
|
e->model.pEdit = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free( e );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// construct from entity
|
2012-03-17 20:01:54 +00:00
|
|
|
entity_t *Entity_Clone( entity_t *e ){
|
|
|
|
entity_t *n;
|
|
|
|
epair_t *ep;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
n = Entity_Alloc();
|
|
|
|
n->eclass = e->eclass;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( ep = e->epairs ; ep ; ep = ep->next )
|
|
|
|
SetKeyValue( n, ep->key, ep->value );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
// copy some misc stuff as well
|
|
|
|
VectorCopy( e->origin, n->origin );
|
|
|
|
// VectorCopy( e->vRotation, n->vRotation );
|
|
|
|
// VectorCopy( e->vScale, n->vScale );
|
|
|
|
|
|
|
|
// n->bDirty = true;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
const char *ValueForKey( epair_t *&e, const char *key ){
|
|
|
|
epair_t *ep;
|
|
|
|
for ( ep = e ; ep ; ep = ep->next )
|
|
|
|
{
|
|
|
|
if ( !strcmp( ep->key, key ) ) {
|
|
|
|
return ep->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
const char *ValueForKey( entity_t *ent, const char *key ){
|
|
|
|
return ValueForKey( ent->epairs, key );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void SetKeyValue( epair_t *&e, const char *key, const char *value ){
|
|
|
|
epair_t *ep;
|
|
|
|
for ( ep = e ; ep ; ep = ep->next )
|
|
|
|
{
|
|
|
|
if ( !strcmp( ep->key, key ) ) {
|
|
|
|
free( ep->value );
|
|
|
|
ep->value = (char*)malloc( strlen( value ) + 1 );
|
|
|
|
strcpy( ep->value, value );
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
ep = (epair_t*)malloc( sizeof( *ep ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
ep->next = e;
|
|
|
|
e = ep;
|
2012-03-17 20:01:54 +00:00
|
|
|
ep->key = (char*)malloc( strlen( key ) + 1 );
|
|
|
|
strcpy( ep->key, key );
|
|
|
|
ep->value = (char*)malloc( strlen( value ) + 1 );
|
|
|
|
strcpy( ep->value, value );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void SetKeyValue( entity_t *ent, const char *key, const char *value ){
|
|
|
|
if ( ent == NULL ) {
|
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR: SetKeyValue: NULL entity \n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !key || !key[0] ) {
|
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR: SetKeyValue: NULL or zero-length key\n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
SetKeyValue( ent->epairs, key, value );
|
|
|
|
/*!
|
|
|
|
\todo TODO broadcast this through a clean messaging API ;-)
|
|
|
|
*/
|
|
|
|
Entity_OnKeyValueChanged( ent, key, value );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void DeleteKey( epair_t *&e, const char *key ){
|
|
|
|
epair_t **ep, *next;
|
|
|
|
|
2007-11-04 03:51:54 +00:00
|
|
|
ep = &e;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( *ep )
|
2007-11-04 03:51:54 +00:00
|
|
|
{
|
|
|
|
next = *ep;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !strcmp( next->key, key ) ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
*ep = next->next;
|
2012-03-17 20:01:54 +00:00
|
|
|
free( next->key );
|
|
|
|
free( next->value );
|
|
|
|
free( next );
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ep = &next->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void DeleteKey( entity_t *ent, const char *key ){
|
|
|
|
DeleteKey( ent->epairs, key );
|
|
|
|
Entity_OnKeyValueChanged( ent, key, "" );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
float FloatForKey( entity_t *ent, const char *key ){
|
|
|
|
const char *k;
|
|
|
|
|
|
|
|
k = ValueForKey( ent, key );
|
|
|
|
return (float) atof( k );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int IntForKey( entity_t *ent, const char *key ){
|
|
|
|
const char *k;
|
|
|
|
|
|
|
|
k = ValueForKey( ent, key );
|
|
|
|
return atoi( k );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void GetVectorForKey( entity_t *ent, const char *key, vec3_t vec ){
|
|
|
|
const char *k;
|
|
|
|
|
|
|
|
k = ValueForKey( ent, key );
|
|
|
|
sscanf( k, "%f %f %f", &vec[0], &vec[1], &vec[2] );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
Entity_FreeEpairs
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Frees the entity epairs.
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void Entity_FreeEpairs( entity_t *e ){
|
|
|
|
epair_t *ep, *next;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( ep = e->epairs; ep; ep = next )
|
2007-11-04 03:51:54 +00:00
|
|
|
{
|
|
|
|
next = ep->next;
|
2012-03-17 20:01:54 +00:00
|
|
|
free( ep->key );
|
|
|
|
free( ep->value );
|
|
|
|
free( ep );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
e->epairs = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Entity_AddToList( entity_t *e, entity_t *elist ){
|
|
|
|
if ( e->next || e->prev ) {
|
|
|
|
Error( "Entity_AddToList: already linked" );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
//e->next = elist->next;
|
|
|
|
//elist->next->prev = e;
|
|
|
|
//elist->next = e;
|
|
|
|
//e->prev = elist;
|
|
|
|
e->next = elist;
|
|
|
|
e->prev = elist->prev;
|
|
|
|
elist->prev->next = e;
|
|
|
|
elist->prev = e;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Entity_RemoveFromList( entity_t *e ){
|
|
|
|
if ( !e->next || !e->prev ) {
|
|
|
|
Error( "Entity_RemoveFromList: not linked" );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
e->next->prev = e->prev;
|
|
|
|
e->prev->next = e->next;
|
|
|
|
e->next = e->prev = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Entity_LinkBrush( entity_t *e, brush_t *b ){
|
|
|
|
if ( b->oprev || b->onext ) {
|
|
|
|
Error( "Entity_LinkBrush: Already linked" );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
b->owner = e;
|
|
|
|
|
|
|
|
// b->onext = e->brushes.onext;
|
|
|
|
// b->oprev = &e->brushes;
|
|
|
|
// e->brushes.onext->oprev = b;
|
|
|
|
// e->brushes.onext = b;
|
2012-03-17 20:01:54 +00:00
|
|
|
/*
|
|
|
|
SPoG - changed to add brushes to end of list instead of start - so this can be used by map loader.
|
|
|
|
This could concievably cause a problem if someone is traversing e->brushes while calling this function.
|
|
|
|
So don't.
|
|
|
|
*/
|
|
|
|
b->onext = &e->brushes;
|
2007-11-04 03:51:54 +00:00
|
|
|
b->oprev = e->brushes.oprev;
|
|
|
|
e->brushes.oprev->onext = b;
|
|
|
|
e->brushes.oprev = b;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Entity_UnlinkBrush( brush_t *b ){
|
|
|
|
if ( !b->onext || !b->oprev ) {
|
|
|
|
Error( "Entity_UnlinkBrush: Not currently linked" );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
b->onext->oprev = b->oprev;
|
|
|
|
b->oprev->onext = b->onext;
|
|
|
|
b->onext = b->oprev = NULL;
|
|
|
|
b->owner = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for undo
|
2012-03-17 20:01:54 +00:00
|
|
|
int Entity_MemorySize( entity_t *e ){
|
|
|
|
epair_t *ep;
|
2007-11-04 03:51:54 +00:00
|
|
|
int size = 0;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( ep = e->epairs; ep; ep = ep->next )
|
2007-11-04 03:51:54 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
size += strlen( ep->key );
|
|
|
|
size += strlen( ep->value );
|
|
|
|
size += sizeof( epair_t );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
size += sizeof( entity_t );
|
2007-11-04 03:51:54 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
epair_t* Entity_AllocateEpair( const char *key, const char *value ){
|
|
|
|
epair_t *ep = (epair_t*)malloc( sizeof( *ep ) );
|
|
|
|
ep->key = (char*)malloc( strlen( key ) + 1 );
|
|
|
|
strcpy( ep->key, key );
|
|
|
|
ep->value = (char*)malloc( strlen( value ) + 1 );
|
|
|
|
strcpy( ep->value, value );
|
|
|
|
ep->next = NULL;
|
|
|
|
return ep;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
epair_t** Entity_GetKeyValList( entity_t *e ){
|
|
|
|
return &e->epairs;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Entity_SetKeyValList( entity_t *e, epair_t* ep ){
|
|
|
|
if ( e->epairs ) {
|
|
|
|
Sys_Printf( "Warning : pe->epairs != NULL in Entity_SetKeyValList, will not set\n" );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
e->epairs = ep;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( epair_t *pe_ep = e->epairs; pe_ep; pe_ep = pe_ep->next )
|
|
|
|
Entity_OnKeyValueChanged( e, pe_ep->key, pe_ep->value );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
2012-03-17 20:01:54 +00:00
|
|
|
\todo FIXME TTimo
|
|
|
|
this is meant to raise messages instead of calling the IEdit directly
|
|
|
|
*/
|
|
|
|
static void Entity_OnKeyValueChanged( entity_t *e, const char *key, const char* value ){
|
|
|
|
if ( strcmp( key,"classname" ) == 0 ) {
|
|
|
|
e->eclass = Eclass_ForName( value, false );
|
|
|
|
Entity_UpdateClass( e, value );
|
|
|
|
if ( strcmp( value,"light" ) == 0 ) {
|
|
|
|
for ( epair_t* ep = e->epairs; ep != NULL; ep = ep->next )
|
|
|
|
Light_OnKeyValueChanged( e, ep->key, ep->value );
|
|
|
|
}
|
|
|
|
if ( e->model.pEdit ) {
|
|
|
|
for ( epair_t* ep = e->epairs; ep != NULL; ep = ep->next )
|
|
|
|
e->model.pEdit->OnKeyValueChanged( e, ep->key, ep->value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( Entity_IsLight( e ) ) {
|
|
|
|
Light_OnKeyValueChanged( e, key, value );
|
|
|
|
}
|
|
|
|
else if ( e->model.pEdit ) {
|
|
|
|
e->model.pEdit->OnKeyValueChanged( e, key, value );
|
|
|
|
}
|
|
|
|
|
|
|
|
// update brush mins/maxs for legacy culling system
|
|
|
|
if ( e->model.pRender && e->brushes.onext != &e->brushes ) {
|
|
|
|
Brush_Build( e->brushes.onext, true, true, false, true );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|