2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // DEntity.cpp: implementation of the DEntity class.
22 //////////////////////////////////////////////////////////////////////
27 #pragma warning(disable : 4786)
39 #include "dialogs/dialogs-gtk.h"
47 #include "generic/referencecounted.h"
57 const char* brushEntityList[] = {
77 //////////////////////////////////////////////////////////////////////
78 // Construction/Destruction
79 //////////////////////////////////////////////////////////////////////
81 DEntity::DEntity(const char *classname, int ID)
83 SetClassname(classname);
95 //////////////////////////////////////////////////////////////////////
97 //////////////////////////////////////////////////////////////////////
99 void DEntity::ClearBrushes()
101 for(std::list<DBrush *>::const_iterator deadBrush=brushList.begin(); deadBrush!=brushList.end(); deadBrush++)
108 void DEntity::ClearPatches()
110 for(std::list<DPatch *>::const_iterator deadPatch=patchList.begin(); deadPatch!=patchList.end(); deadPatch++)
117 DPatch* DEntity::NewPatch()
119 DPatch* newPatch = new DPatch;
121 patchList.push_back(newPatch);
126 DBrush* DEntity::NewBrush(int ID)
128 DBrush* newBrush = new DBrush(ID);
130 brushList.push_back(newBrush);
135 char* getNextBracket(char* s)
148 bool DEntity::LoadFromPrt(char *filename)
151 strcpy(portals.fn, filename);
154 if(portals.node_count == 0)
161 for(unsigned int i = 0; i < portals.node_count; i++)
164 DBrush* brush = NewBrush();
166 for(unsigned int j = 0; j < portals.node[i].portal_count; j++)
168 for(unsigned int k = 0; k < portals.node[i].portal[j].point_count-2; k++)
170 vec3_t v1, v2, normal, n;
171 VectorSubtract(portals.node[i].portal[j].point[k+2].p, portals.node[i].portal[j].point[k+1].p, v1);
172 VectorSubtract(portals.node[i].portal[j].point[k].p, portals.node[i].portal[j].point[k+1].p, v2);
173 CrossProduct(v1, v2, n);
174 VectorNormalize(n, v2);
178 VectorCopy(v2, normal);
182 VectorSubtract(v2, normal, v1);
183 if(VectorLength(v1) > 0.01)
192 brush->AddFace(portals.node[i].portal[j].point[2].p, portals.node[i].portal[j].point[1].p, portals.node[i].portal[j].point[0].p, "textures/common/caulk", false);
194 brush->AddFace(portals.node[i].portal[j].point[0].p, portals.node[i].portal[j].point[1].p, portals.node[i].portal[j].point[2].p, "textures/common/caulk", false);
197 brush->BuildInRadiant(false, NULL);
203 DPlane* DEntity::AddFaceToBrush(vec3_t va, vec3_t vb, vec3_t vc, _QERFaceData* faceData, int ID)
205 DBrush* buildBrush = GetBrushForID(ID);
206 return buildBrush->AddFace(va, vb, vc, faceData);
207 // slow, dont use much
210 DBrush* DEntity::GetBrushForID(int ID)
212 DBrush* buildBrush = NULL;
214 for(std::list<DBrush *>::const_iterator chkBrush=brushList.begin(); chkBrush!=brushList.end(); chkBrush++)
216 if((*chkBrush)->m_nBrushID == ID)
218 buildBrush = (*chkBrush);
224 buildBrush = NewBrush(ID);
229 template<typename Functor>
230 class BrushSelectedVisitor : public SelectionSystem::Visitor
232 const Functor& m_functor;
234 BrushSelectedVisitor(const Functor& functor) : m_functor(functor)
237 void visit(scene::Instance& instance) const
239 if(Node_isBrush(instance.path().top()))
241 m_functor(*instance.path().top());
246 template<typename Functor>
247 inline const Functor& Scene_forEachSelectedBrush(const Functor& functor)
249 GlobalSelectionSystem().foreachSelected(BrushSelectedVisitor<Functor>(functor));
253 void DEntity_loadBrush(DEntity& entity, scene::Node& brush)
255 DBrush* loadBrush = entity.NewBrush(entity.brushList.size());
256 loadBrush->LoadFromBrush(brush, true);
258 typedef ReferenceCaller1<DEntity, scene::Node&, DEntity_loadBrush> DEntityLoadBrushCaller;
260 void DEntity::LoadSelectedBrushes()
265 Scene_forEachSelectedBrush(DEntityLoadBrushCaller(*this));
268 template<typename Functor>
269 class PatchSelectedVisitor : public SelectionSystem::Visitor
271 const Functor& m_functor;
273 PatchSelectedVisitor(const Functor& functor) : m_functor(functor)
276 void visit(scene::Instance& instance) const
278 if(Node_isPatch(instance.path().top()))
280 m_functor(*instance.path().top());
285 template<typename Functor>
286 inline const Functor& Scene_forEachSelectedPatch(const Functor& functor)
288 GlobalSelectionSystem().foreachSelected(PatchSelectedVisitor<Functor>(functor));
292 void DEntity_loadPatch(DEntity& entity, scene::Node& patch)
294 DPatch* loadPatch = entity.NewPatch();
295 loadPatch->LoadFromBrush(patch);
297 typedef ReferenceCaller1<DEntity, scene::Node&, DEntity_loadPatch> DEntityLoadPatchCaller;
299 void DEntity::LoadSelectedPatches()
304 Scene_forEachSelectedPatch(DEntityLoadPatchCaller(*this));
307 bool* DEntity::BuildIntersectList()
309 int max = GetIDMax();
313 bool* pbIntList = new bool[max];
314 memset(pbIntList, 0, sizeof(bool)*(max));
316 for(std::list<DBrush *>::const_iterator pB1=brushList.begin(); pB1!=brushList.end(); pB1++)
318 std::list<DBrush *>::const_iterator pB2=pB1;
319 for(pB2++; pB2!=brushList.end(); pB2++)
321 if((*pB1)->IntersectsWith((*pB2)))
323 pbIntList[(*pB1)->m_nBrushID] = true;
324 pbIntList[(*pB2)->m_nBrushID] = true;
332 bool* DEntity::BuildDuplicateList()
334 int max = GetIDMax();
338 bool* pbDupList = new bool[max];
339 memset(pbDupList, 0, sizeof(bool)*(max));
341 for(std::list<DBrush *>::const_iterator pB1=brushList.begin(); pB1!=brushList.end(); pB1++)
343 std::list<DBrush *>::const_iterator pB2=pB1;
344 for(pB2++; pB2!=brushList.end(); pB2++)
348 pbDupList[(*pB1)->m_nBrushID] = true;
349 pbDupList[(*pB2)->m_nBrushID] = true;
357 void DEntity::SelectBrushes(bool *selectList)
359 if(selectList == NULL)
362 GlobalSelectionSystem().setSelectedAll(false);
364 scene::Path path(NodeReference(GlobalSceneGraph().root()));
365 path.push(NodeReference(*QER_Entity));
367 for(std::list<DBrush *>::const_iterator pBrush=brushList.begin(); pBrush!=brushList.end(); pBrush++)
369 if(selectList[(*pBrush)->m_nBrushID])
371 path.push(NodeReference(*(*pBrush)->QER_brush));
372 Instance_getSelectable(*GlobalSceneGraph().find(path))->setSelected(true);
378 bool DEntity::LoadFromEntity(scene::Node& ent, bool bLoadPatches) {
385 LoadEPairList(Node_getEntity(ent));
389 for(i = 0; brushEntityList[i]; i++)
391 if(string_equal_nocase(brushEntityList[i], m_Classname))
401 if(Node_getTraversable(ent))
403 class load_brushes_t : public scene::Traversable::Walker
408 load_brushes_t(DEntity* entity)
409 : m_entity(entity), m_count(0)
412 bool pre(scene::Node& node) const
414 if(Node_isPatch(node))
416 DPatch* loadPatch = m_entity->NewPatch();
417 loadPatch->LoadFromBrush(node);
419 else if(Node_isBrush(node))
421 DBrush* loadBrush = m_entity->NewBrush(m_count++);
422 loadBrush->LoadFromBrush(node, true);
426 } load_brushes(this);
428 Node_getTraversable(ent)->traverse(load_brushes);
434 void DEntity::RemoveNonCheckBrushes(std::list<Str>* exclusionList, bool useDetail)
436 std::list<DBrush *>::iterator chkBrush=brushList.begin();
438 while( chkBrush!=brushList.end() )
442 if((*chkBrush)->IsDetail())
445 chkBrush = brushList.erase(chkBrush);
450 std::list<Str>::iterator eTexture;
452 for( eTexture=exclusionList->begin(); eTexture!=exclusionList->end(); eTexture++ )
454 if((*chkBrush)->HasTexture((*eTexture).GetBuffer()))
457 chkBrush = brushList.erase(chkBrush);
462 if( eTexture == exclusionList->end() )
467 void DEntity::ResetChecks(std::list<Str>* exclusionList)
469 for(std::list<DBrush *>::const_iterator resetBrush=brushList.begin(); resetBrush!=brushList.end(); resetBrush++)
471 (*resetBrush)->ResetChecks(exclusionList);
475 int DEntity::FixBrushes()
479 for(std::list<DBrush *>::const_iterator fixBrush=brushList.begin(); fixBrush!=brushList.end(); fixBrush++)
481 count += (*fixBrush)->RemoveRedundantPlanes();
487 void DEntity::BuildInRadiant(bool allowDestruction)
489 bool makeEntity = strcmp(m_Classname, "worldspawn") ? true : false;
493 NodeReference node(GlobalEntityCreator().createEntity(GlobalEntityClassManager().findOrInsert(m_Classname.GetBuffer(), !brushList.empty() || !patchList.empty())));
495 for(std::list<DEPair* >::const_iterator buildEPair=epairList.begin(); buildEPair!=epairList.end(); buildEPair++)
497 Node_getEntity(node)->setKeyValue((*buildEPair)->key, (*buildEPair)->value);
500 Node_getTraversable(GlobalSceneGraph().root())->insert(node);
502 for(std::list<DBrush *>::const_iterator buildBrush=brushList.begin(); buildBrush!=brushList.end(); buildBrush++)
503 (*buildBrush)->BuildInRadiant(allowDestruction, NULL, node.get_pointer());
505 for(std::list<DPatch *>::const_iterator buildPatch=patchList.begin(); buildPatch!=patchList.end(); buildPatch++)
506 (*buildPatch)->BuildInRadiant(node.get_pointer());
508 QER_Entity = node.get_pointer();
512 for(std::list<DBrush *>::const_iterator buildBrush=brushList.begin(); buildBrush!=brushList.end(); buildBrush++)
513 (*buildBrush)->BuildInRadiant(allowDestruction, NULL);
515 for(std::list<DPatch *>::const_iterator buildPatch=patchList.begin(); buildPatch!=patchList.end(); buildPatch++)
516 (*buildPatch)->BuildInRadiant();
522 int DEntity::GetIDMax( void ) {
524 for(std::list<DBrush *>::const_iterator cntBrush=brushList.begin(); cntBrush!=brushList.end(); cntBrush++) {
525 if((*cntBrush)->m_nBrushID > max)
526 max = (*cntBrush)->m_nBrushID;
531 void DEntity::SetClassname( const char *classname ) {
532 m_Classname = classname;
535 void DEntity::SaveToFile(FILE *pFile)
537 fprintf(pFile, "{\n");
539 fprintf(pFile, "\"classname\" \"%s\"\n", (const char *)m_Classname);
541 for(std::list<DEPair *>::const_iterator ep=epairList.begin(); ep!=epairList.end(); ep++)
543 fprintf(pFile, "\"%s\" \"%s\"\n", (const char *)(*ep)->key, (const char *)(*ep)->value);
546 for(std::list<DBrush *>::const_iterator bp=brushList.begin(); bp!=brushList.end(); bp++)
548 (*bp)->SaveToFile(pFile);
551 fprintf(pFile, "}\n");
554 void DEntity::ClearEPairs()
556 for(std::list<DEPair *>::const_iterator deadEPair=epairList.begin(); deadEPair!=epairList.end(); deadEPair++)
563 void DEntity::AddEPair(const char *key, const char *value) {
565 newEPair = FindEPairByKey( key );
567 newEPair = new DEPair;
568 newEPair->Build(key, value);
569 epairList.push_back(newEPair);
571 newEPair->Build(key, value);
575 void DEntity::LoadEPairList(Entity *epl)
577 class load_epairs_t : public Entity::Visitor
581 load_epairs_t(DEntity* entity)
585 void visit(const char* key, const char* value)
587 if(strcmp(key, "classname") == 0)
588 m_entity->SetClassname(value);
590 m_entity->AddEPair(key, value);
595 epl->forEachKeyValue(load_epairs);
598 bool DEntity::ResetTextures(const char* textureName, float fScale[2], float fShift[2], int rotation, const char* newTextureName,
599 int bResetTextureName, int bResetScale[2], int bResetShift[2], int bResetRotation, bool rebuild)
603 for(std::list<DBrush *>::const_iterator resetBrush=brushList.begin(); resetBrush!=brushList.end(); resetBrush++)
605 bool tmp = (*resetBrush)->ResetTextures(textureName, fScale, fShift, rotation, newTextureName,
606 bResetTextureName, bResetScale, bResetShift, bResetRotation);
614 if(bResetTextureName)
616 for(std::list<DPatch *>::const_iterator resetPatch=patchList.begin(); resetPatch!=patchList.end(); resetPatch++)
618 bool tmp = (*resetPatch)->ResetTextures(textureName, newTextureName);
630 DEPair* DEntity::FindEPairByKey(const char* keyname)
632 for(std::list<DEPair *>::const_iterator ep=epairList.begin(); ep!=epairList.end(); ep++)
634 char* c = (*ep)->key;
635 if(!strcmp(c, keyname))
641 void DEntity::RemoveFromRadiant()
643 Node_getTraversable(GlobalSceneGraph().root())->erase(*QER_Entity);
648 void DEntity::SpawnString(const char* key, const char* defaultstring, const char** out)
650 DEPair* pEP = FindEPairByKey(key);
654 *out = defaultstring;
658 void DEntity::SpawnInt(const char* key, const char* defaultstring, int* out)
660 DEPair* pEP = FindEPairByKey(key);
662 *out = atoi(pEP->value);
664 *out = atoi(defaultstring);
668 void DEntity::SpawnFloat(const char* key, const char* defaultstring, float* out)
670 DEPair* pEP = FindEPairByKey(key);
672 *out = static_cast<float>(atof(pEP->value));
674 *out = static_cast<float>(atof(defaultstring));
678 void DEntity::SpawnVector(const char* key, const char* defaultstring, vec_t* out)
680 DEPair* pEP = FindEPairByKey(key);
682 sscanf(pEP->value, "%f %f %f", &out[0], &out[1], &out[2]);
684 sscanf(defaultstring, "%f %f %f", &out[0], &out[1], &out[2]);
688 int DEntity::GetBrushCount( void ) {
689 return brushList.size();
692 DBrush* DEntity::FindBrushByPointer( scene::Node& brush ) {
693 for(std::list<DBrush *>::const_iterator listBrush = brushList.begin(); listBrush != brushList.end(); listBrush++) {
694 DBrush* pBrush = (*listBrush);
695 if(pBrush->QER_brush == &brush) {