2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "entity_entitymodel.h"
33 static void Entity_FreeEpairs(entity_t *e);
35 static void SetKeyValue (epair_t *&e, const char *key, const char *value);
36 static void DeleteKey (epair_t *&e, const char *key);
37 static const char *ValueForKey ( epair_t *&e, const char *key);
39 static void Entity_OnKeyValueChanged(entity_t *e, const char* key, const char* value);
42 entity_t *Entity_Alloc()
45 e = (entity_t*)malloc (sizeof(*e));
46 e->entityId = g_entityId++;
47 VectorSet(e->origin, 0, 0, 0);
48 VectorSet(e->color, 1, 1, 1);
51 e->next = e->prev = NULL;
52 e->brushes.onext = e->brushes.oprev = &e->brushes;
55 e->model.pRender = NULL;
56 e->model.pSelect = NULL;
57 e->model.pEdit = NULL;
62 void Entity_Free (entity_t *e)
64 while (e->brushes.onext != &e->brushes)
65 Brush_Free( e->brushes.onext, true );
69 e->next->prev = e->prev;
70 e->prev->next = e->next;
77 e->model.pRender->DecRef();
78 e->model.pRender = NULL;
82 e->model.pSelect->DecRef();
83 e->model.pSelect = NULL;
87 e->model.pEdit->DecRef();
88 e->model.pEdit = NULL;
94 // construct from entity
95 entity_t *Entity_Clone (entity_t *e)
101 n->eclass = e->eclass;
103 for (ep = e->epairs ; ep ; ep=ep->next)
104 SetKeyValue(n, ep->key, ep->value);
106 // copy some misc stuff as well
107 VectorCopy( e->origin, n->origin );
108 // VectorCopy( e->vRotation, n->vRotation );
109 // VectorCopy( e->vScale, n->vScale );
120 const char *ValueForKey ( epair_t *&e, const char *key)
123 for (ep=e ; ep ; ep=ep->next)
125 if (!strcmp (ep->key, key) )
133 const char *ValueForKey (entity_t *ent, const char *key)
135 return ValueForKey(ent->epairs, key);
138 void SetKeyValue (epair_t *&e, const char *key, const char *value)
141 for (ep=e ; ep ; ep=ep->next)
143 if (!strcmp (ep->key, key) )
146 ep->value = (char*)malloc(strlen(value)+1);
147 strcpy (ep->value, value);
151 ep = (epair_t*)malloc (sizeof(*ep));
154 ep->key = (char*)malloc(strlen(key)+1);
155 strcpy (ep->key, key);
156 ep->value = (char*)malloc(strlen(value)+1);
157 strcpy (ep->value, value);
161 void SetKeyValue (entity_t *ent, const char *key, const char *value)
165 Sys_FPrintf(SYS_ERR, "ERROR: SetKeyValue: NULL entity \n");
171 Sys_FPrintf(SYS_ERR, "ERROR: SetKeyValue: NULL or zero-length key\n");
175 SetKeyValue(ent->epairs, key, value);
177 \todo TODO broadcast this through a clean messaging API ;-)
179 Entity_OnKeyValueChanged(ent, key, value);
182 void DeleteKey (epair_t *&e, const char *key)
190 if ( !strcmp (next->key, key) )
202 void DeleteKey (entity_t *ent, const char *key)
204 DeleteKey(ent->epairs, key);
205 Entity_OnKeyValueChanged(ent, key, "");
208 float FloatForKey (entity_t *ent, const char *key)
212 k = ValueForKey (ent, key);
213 return (float) atof(k);
216 int IntForKey (entity_t *ent, const char *key)
220 k = ValueForKey (ent, key);
224 void GetVectorForKey (entity_t *ent, const char *key, vec3_t vec)
228 k = ValueForKey (ent, key);
229 sscanf (k, "%f %f %f", &vec[0], &vec[1], &vec[2]);
236 Frees the entity epairs.
239 void Entity_FreeEpairs(entity_t *e)
243 for (ep = e->epairs; ep; ep = next)
253 void Entity_AddToList(entity_t *e, entity_t *elist)
255 if (e->next || e->prev)
256 Error ("Entity_AddToList: already linked");
257 //e->next = elist->next;
258 //elist->next->prev = e;
262 e->prev = elist->prev;
263 elist->prev->next = e;
267 void Entity_RemoveFromList (entity_t *e)
269 if (!e->next || !e->prev)
270 Error ("Entity_RemoveFromList: not linked");
271 e->next->prev = e->prev;
272 e->prev->next = e->next;
273 e->next = e->prev = NULL;
276 void Entity_LinkBrush (entity_t *e, brush_t *b)
278 if (b->oprev || b->onext)
279 Error ("Entity_LinkBrush: Already linked");
282 // b->onext = e->brushes.onext;
283 // b->oprev = &e->brushes;
284 // e->brushes.onext->oprev = b;
285 // e->brushes.onext = b;
287 SPoG - changed to add brushes to end of list instead of start - so this can be used by map loader.
288 This could concievably cause a problem if someone is traversing e->brushes while calling this function.
291 b->onext = &e->brushes;
292 b->oprev = e->brushes.oprev;
293 e->brushes.oprev->onext = b;
294 e->brushes.oprev = b;
297 void Entity_UnlinkBrush (brush_t *b)
299 if (!b->onext || !b->oprev)
300 Error ("Entity_UnlinkBrush: Not currently linked");
301 b->onext->oprev = b->oprev;
302 b->oprev->onext = b->onext;
303 b->onext = b->oprev = NULL;
308 int Entity_MemorySize(entity_t *e)
313 for (ep = e->epairs; ep; ep = ep->next)
315 size += strlen(ep->key);
316 size += strlen(ep->value);
317 size += sizeof(epair_t);
319 size += sizeof(entity_t);
323 epair_t* Entity_AllocateEpair(const char *key, const char *value)
325 epair_t *ep = (epair_t*)malloc (sizeof(*ep));
326 ep->key = (char*)malloc(strlen(key)+1);
327 strcpy (ep->key, key);
328 ep->value = (char*)malloc(strlen(value)+1);
329 strcpy (ep->value, value);
334 epair_t** Entity_GetKeyValList(entity_t *e)
339 void Entity_SetKeyValList(entity_t *e, epair_t* ep)
342 Sys_Printf( "Warning : pe->epairs != NULL in Entity_SetKeyValList, will not set\n" );
346 for (epair_t *pe_ep = e->epairs; pe_ep; pe_ep = pe_ep->next)
347 Entity_OnKeyValueChanged(e, pe_ep->key, pe_ep->value);
354 this is meant to raise messages instead of calling the IEdit directly
356 static void Entity_OnKeyValueChanged(entity_t *e, const char *key, const char* value)
358 if(strcmp(key,"classname") == 0)
360 e->eclass = Eclass_ForName(value, false);
361 Entity_UpdateClass(e, value);
362 if(strcmp(value,"light") == 0)
363 for(epair_t* ep = e->epairs; ep != NULL; ep=ep->next)
364 Light_OnKeyValueChanged(e, ep->key, ep->value);
366 for(epair_t* ep = e->epairs; ep != NULL; ep=ep->next)
367 e->model.pEdit->OnKeyValueChanged(e, ep->key, ep->value);
369 else if(Entity_IsLight(e))
370 Light_OnKeyValueChanged(e, key, value);
371 else if(e->model.pEdit)
372 e->model.pEdit->OnKeyValueChanged(e, key, value);
374 // update brush mins/maxs for legacy culling system
375 if(e->model.pRender && e->brushes.onext != &e->brushes)
376 Brush_Build( e->brushes.onext, true, true, false, true );