genericobject.h

00001 /*--License:
00002         Lilith 3D Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2002-2007
00004         www.grinninglizard.com/lilith
00005 
00006         This program is free software; you can redistribute it and/or
00007         modify it under the terms of the GNU General Public License
00008         as published by the Free Software Foundation; either version 2
00009         of the License, or (at your option) any later version.
00010 
00011         This program is distributed in the hope that it will be useful,
00012         but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License
00017         along with this program; if not, write to the Free Software
00018         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020         The full text of the license can be found in license.txt
00021 */
00022 
00023 #ifndef LILITH_OBJECT_INCLUDED
00024 #define LILITH_OBJECT_INCLUDED
00025 
00026 #include <string>
00027 #include <list>
00028 
00029 #include "../grinliz/glvector.h"
00030 
00031 namespace lilith3d
00032 {
00033 class TerrainMesh;
00034 class Mesh;
00035 struct PathSurfaceInstance;
00036 class BuildingMesh;
00037 class WalkingMob;
00038 
00039 enum
00040 {
00041         TEST_TERRAIN    = 0x01,
00042         TEST_PSI                = 0x02,                 
00043         TEST_MESH               = 0x04,                 // test for mesh (that are not trees)
00044         TEST_TREE               = 0x08,                 // test for trees
00045         TEST_ALL = TEST_TERRAIN | TEST_PSI | TEST_MESH | TEST_TREE
00046 };
00047 
00053 class LilithObject
00054 {
00055   public:
00057         LilithObject();
00059         LilithObject( TerrainMesh* tmesh, const grinliz::Vector3F& intersect, float distance );
00061         LilithObject( Mesh* mesh, const grinliz::Vector3F& intersect, float distance );
00063         LilithObject( PathSurfaceInstance* psi, const grinliz::Vector3F& intersect, float distance );
00064 
00066         grinliz::Vector3F Intersection() const  { return intersect; }
00068         float X() const { return intersect.x; }
00070         float Y() const { return intersect.y; }
00072         float Z() const { return intersect.z; }
00073 
00075         const std::string& Description() const  { return desc; }
00076         
00078         TerrainMesh* ToTerrain() const                          { return tmesh; }
00082         Mesh* ToMesh() const                                            { return mesh; }
00085         PathSurfaceInstance* ToPSI() const      { return psi; }
00086 
00088         float Distance() const                                          { return distance; }
00089 
00090         friend bool operator<( const LilithObject& lhs, const LilithObject& rhs )       { return lhs.distance < rhs.distance; }
00091 
00092   private:
00093         void Clear();
00094 
00095         float distance;
00096         grinliz::Vector3F intersect;
00097 
00098         TerrainMesh* tmesh;
00099         Mesh* mesh;
00100         PathSurfaceInstance* psi;
00101         std::string desc;
00102 };
00103 
00104 class LilithObjectFar : public std::unary_function< LilithObject, bool> 
00105 {
00106   public:
00107         const float d;
00108         LilithObjectFar( float _d )     : d( _d ) {}
00109         bool operator()(const LilithObject& a) const { return a.Distance() >= d; }
00110 
00111 private:
00112         void operator=(const LilithObjectFar& );        // private and undefined
00113 };
00114 
00115 typedef std::list< LilithObject > LilithObjectList;
00116 typedef LilithObjectList::iterator LilithObjectListIterator;
00117 
00118 struct PSIMobRing
00119 {
00120         PSIMobRing() {
00121                 next = this; 
00122                 prev = this;
00123                 psi = 0; 
00124                 mob = 0; 
00125         }
00126         ~PSIMobRing() { 
00127                 Unlink(); 
00128                 #if defined( _MSC_VER ) && defined( DEBUG )
00129                 GLASSERT( _CrtCheckMemory() );
00130                 #endif
00131         }
00132 
00133         PathSurfaceInstance* psi;
00134         WalkingMob* mob;
00135 
00136         PSIMobRing* Next() { return next; }
00137 
00138         bool IsLinked() {
00139                 if ( next == this ) {
00140                         GLASSERT( prev == this );
00141                         return false;
00142                 }
00143                 return true;
00144         }
00145         /*
00146         void Validate() {
00147                 #ifdef DEBUG
00148                 int count = 0;
00149                 for( PSIMobRing* node = this->next; node != this; node=node->next ) {
00150                         GLASSERT( psi || mob );
00151                         GLASSERT( !(psi && mob) );
00152                         ++count;
00153                 }
00154                 for( PSIMobRing* node = this->prev; node != this; node=node->prev ) {
00155                         GLASSERT( psi || mob );
00156                         GLASSERT( !(psi && mob) );
00157                         --count;
00158                 }
00159                 GLASSERT( count == 0 );
00160                 #endif
00161         }
00162         */
00163 
00164         void Unlink() {
00165                 /*
00166                 #ifdef DEBUG
00167                 Validate();
00168                 GLASSERT( !(this->psi && this->mob ) );
00169                 GLOUTPUT(( "unlink %x : ", this ));
00170                 PSIMobRing* start = next;
00171                 #endif
00172                 */
00173                 
00174                 next->prev = prev;
00175                 prev->next = next;
00176                 next = prev = this;
00177 
00178                 /*
00179                 #ifdef DEBUG
00180                 PSIMobRing* node = start; 
00181                 do {
00182                         GLOUTPUT(( "%x ", node ));
00183                         node = node->next;
00184                 } while ( node != start );
00185                 GLOUTPUT(( "\n" ));
00186                 #endif
00187                 */
00188         }
00189 
00190         void Link( PSIMobRing* addMe )
00191         {
00192                 GLASSERT( !addMe->psi && addMe->mob );
00193                 GLASSERT( this->psi && !this->mob );
00194 
00195                 addMe->next = next;
00196                 addMe->prev = this;
00197 
00198                 next->prev = addMe;
00199                 next = addMe;
00200                 /*
00201                 #ifdef DEBUG
00202                 GLOUTPUT(( "link %x add=%x : %x ", this, addMe, this ));
00203                 for( PSIMobRing* node = this->next; node != this; node=node->next ) {
00204                         GLOUTPUT(( "%x ", node ));
00205                 }
00206                 GLOUTPUT(( "\n" ));
00207 
00208                 this->Validate();
00209                 addMe->Validate();
00210                 #endif
00211                 */
00212         }
00213 
00214         static PathSurfaceInstance* FindPSI( PSIMobRing* ring )
00215         {
00216                 //ring->Validate();
00217                 GLASSERT( !(ring->psi && ring->mob ) );
00218                 if ( ring->psi )
00219                         return ring->psi;
00220 
00221                 for( PSIMobRing* node = ring->next; node != ring; node=node->next )
00222                 {
00223                         GLASSERT( !(node->psi && node->mob ) );
00224                         if ( node->psi )
00225                                 return node->psi;
00226                 }
00227                 return 0;
00228         }
00229 
00230 private:
00231         PSIMobRing* next;
00232         PSIMobRing* prev;
00233 };
00234 
00235 };      // namespace lilith3d
00236 #endif

Generated on Fri Mar 23 19:36:21 2007 for Lilith3D by  doxygen 1.5.1-p1