sceneZoneSpace.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "scene/zones/sceneZoneSpace.h"
  24. #include "scene/zones/sceneTraversalState.h"
  25. #include "scene/zones/sceneZoneSpaceManager.h"
  26. #include "scene/sceneRenderState.h"
  27. #include "sim/netConnection.h"
  28. #include "core/stream/bitStream.h"
  29. #include "console/engineAPI.h"
  30. //#define DEBUG_SPEW
  31. ClassChunker< SceneZoneSpace::ZoneSpaceRef > SceneZoneSpace::smZoneSpaceRefChunker;
  32. //-----------------------------------------------------------------------------
  33. SceneZoneSpace::SceneZoneSpace()
  34. : mManager( NULL ),
  35. mZoneRangeStart( SceneZoneSpaceManager::InvalidZoneId ),
  36. mZoneGroup( InvalidZoneGroup ),
  37. mNumZones( 0 ),
  38. mZoneFlags( ZoneFlag_IsClosedOffSpace ),
  39. mConnectedZoneSpaces( NULL )
  40. {
  41. VECTOR_SET_ASSOCIATION( mOccluders );
  42. }
  43. //-----------------------------------------------------------------------------
  44. SceneZoneSpace::~SceneZoneSpace()
  45. {
  46. AssertFatal( mConnectedZoneSpaces == NULL, "SceneZoneSpace::~SceneZoneSpace - Still have connected zone spaces!" );
  47. }
  48. //-----------------------------------------------------------------------------
  49. void SceneZoneSpace::onSceneRemove()
  50. {
  51. _disconnectAllZoneSpaces();
  52. Parent::onSceneRemove();
  53. }
  54. //-----------------------------------------------------------------------------
  55. void SceneZoneSpace::initPersistFields()
  56. {
  57. addGroup( "Zoning" );
  58. addProtectedField( "zoneGroup", TypeS32, Offset( mZoneGroup, SceneZoneSpace ),
  59. &_setZoneGroup, &defaultProtectedGetFn,
  60. "ID of group the zone is part of." );
  61. endGroup( "Zoning" );
  62. Parent::initPersistFields();
  63. }
  64. //-----------------------------------------------------------------------------
  65. bool SceneZoneSpace::writeField( StringTableEntry fieldName, const char* value )
  66. {
  67. // Don't write zoneGroup field if at default.
  68. static StringTableEntry sZoneGroup = StringTable->insert( "zoneGroup" );
  69. if( fieldName == sZoneGroup && getZoneGroup() == InvalidZoneGroup )
  70. return false;
  71. return Parent::writeField( fieldName, value );
  72. }
  73. //-----------------------------------------------------------------------------
  74. void SceneZoneSpace::setZoneGroup( U32 group )
  75. {
  76. if( mZoneGroup == group )
  77. return;
  78. mZoneGroup = group;
  79. setMaskBits( ZoneGroupMask );
  80. // Rezone to establish new connectivity.
  81. if( mManager )
  82. mManager->notifyObjectChanged( this );
  83. }
  84. //-----------------------------------------------------------------------------
  85. U32 SceneZoneSpace::packUpdate( NetConnection* connection, U32 mask, BitStream* stream )
  86. {
  87. U32 retMask = Parent::packUpdate( connection, mask, stream );
  88. if( stream->writeFlag( mask & ZoneGroupMask ) )
  89. stream->write( mZoneGroup );
  90. return retMask;
  91. }
  92. //-----------------------------------------------------------------------------
  93. void SceneZoneSpace::unpackUpdate( NetConnection* connection, BitStream* stream )
  94. {
  95. Parent::unpackUpdate( connection, stream );
  96. if( stream->readFlag() ) // ZoneGroupMask
  97. {
  98. U32 zoneGroup;
  99. stream->read( &zoneGroup );
  100. setZoneGroup( zoneGroup );
  101. }
  102. }
  103. //-----------------------------------------------------------------------------
  104. bool SceneZoneSpace::getOverlappingZones( SceneObject* obj, U32* outZones, U32& outNumZones )
  105. {
  106. return getOverlappingZones( obj->getWorldBox(), outZones, outNumZones );
  107. }
  108. //-----------------------------------------------------------------------------
  109. void SceneZoneSpace::_onZoneAddObject( SceneObject* object, const U32* zoneIDs, U32 numZones )
  110. {
  111. if( object->isVisualOccluder() )
  112. _addOccluder( object );
  113. // If this isn't the root zone and the object is zone space,
  114. // see if we should automatically connect the two.
  115. if( !isRootZone() && object->getTypeMask() & ZoneObjectType )
  116. {
  117. SceneZoneSpace* zoneSpace = dynamic_cast< SceneZoneSpace* >( object );
  118. // Don't connect a zone space that has the same closed off status
  119. // that we have except it is assigned to the same zone group.
  120. if( zoneSpace &&
  121. ( zoneSpace->mZoneFlags.test( ZoneFlag_IsClosedOffSpace ) != mZoneFlags.test( ZoneFlag_IsClosedOffSpace ) ||
  122. ( zoneSpace->getZoneGroup() == getZoneGroup() &&
  123. zoneSpace->getZoneGroup() != InvalidZoneGroup ) ) &&
  124. _automaticallyConnectZoneSpace( zoneSpace ) )
  125. {
  126. connectZoneSpace( zoneSpace );
  127. }
  128. }
  129. }
  130. //-----------------------------------------------------------------------------
  131. void SceneZoneSpace::_onZoneRemoveObject( SceneObject* object )
  132. {
  133. if( object->isVisualOccluder() )
  134. _removeOccluder( object );
  135. if( !isRootZone() && object->getTypeMask() & ZoneObjectType )
  136. {
  137. SceneZoneSpace* zoneSpace = dynamic_cast< SceneZoneSpace* >( object );
  138. if( zoneSpace )
  139. disconnectZoneSpace( zoneSpace );
  140. }
  141. }
  142. //-----------------------------------------------------------------------------
  143. bool SceneZoneSpace::_automaticallyConnectZoneSpace( SceneZoneSpace* zoneSpace ) const
  144. {
  145. //TODO: This is suboptimal. While it prevents the most blatantly wrong automatic connections,
  146. // we need a true polyhedron/polyhedron intersection to accurately determine zone intersection
  147. // when it comes to automatic connections.
  148. U32 numZones = 0;
  149. U32 zones[ SceneObject::MaxObjectZones ];
  150. zoneSpace->getOverlappingZones( getWorldBox(), zones, numZones );
  151. return ( numZones > 0 );
  152. }
  153. //-----------------------------------------------------------------------------
  154. void SceneZoneSpace::connectZoneSpace( SceneZoneSpace* zoneSpace )
  155. {
  156. // If the zone space is already in the list, do nothing.
  157. for( ZoneSpaceRef* ref = mConnectedZoneSpaces; ref != NULL; ref = ref->mNext )
  158. if( ref->mZoneSpace == zoneSpace )
  159. return;
  160. // Link the zone space to the zone space refs.
  161. ZoneSpaceRef* ref = smZoneSpaceRefChunker.alloc();
  162. ref->mZoneSpace = zoneSpace;
  163. ref->mNext = mConnectedZoneSpaces;
  164. mConnectedZoneSpaces = ref;
  165. #ifdef DEBUG_SPEW
  166. Platform::outputDebugString( "[SceneZoneSpace] Connecting %i-%i to %i-%i",
  167. getZoneRangeStart(), getZoneRangeStart() + getZoneRange(),
  168. zoneSpace->getZoneRangeStart(), zoneSpace->getZoneRangeStart() + zoneSpace->getZoneRange()
  169. );
  170. #endif
  171. }
  172. //-----------------------------------------------------------------------------
  173. void SceneZoneSpace::disconnectZoneSpace( SceneZoneSpace* zoneSpace )
  174. {
  175. ZoneSpaceRef* prev = NULL;
  176. for( ZoneSpaceRef* ref = mConnectedZoneSpaces; ref != NULL; prev = ref, ref = ref->mNext )
  177. if( ref->mZoneSpace == zoneSpace )
  178. {
  179. if( prev )
  180. prev->mNext = ref->mNext;
  181. else
  182. mConnectedZoneSpaces = ref->mNext;
  183. #ifdef DEBUG_SPEW
  184. Platform::outputDebugString( "[SceneZoneSpace] Disconnecting %i-%i from %i-%i",
  185. getZoneRangeStart(), getZoneRangeStart() + getZoneRange(),
  186. zoneSpace->getZoneRangeStart(), zoneSpace->getZoneRangeStart() + zoneSpace->getZoneRange()
  187. );
  188. #endif
  189. smZoneSpaceRefChunker.free( ref );
  190. break;
  191. }
  192. }
  193. //-----------------------------------------------------------------------------
  194. void SceneZoneSpace::_disconnectAllZoneSpaces()
  195. {
  196. #ifdef DEBUG_SPEW
  197. if( mConnectedZoneSpaces != NULL )
  198. Platform::outputDebugString( "[SceneZoneSpace] Disconnecting all from %i-%i",
  199. getZoneRangeStart(), getZoneRangeStart() + getZoneRange()
  200. );
  201. #endif
  202. for( ZoneSpaceRef* ref = mConnectedZoneSpaces; ref != NULL; )
  203. {
  204. ZoneSpaceRef* next = ref->mNext;
  205. smZoneSpaceRefChunker.free( ref );
  206. ref = next;
  207. }
  208. mConnectedZoneSpaces = NULL;
  209. }
  210. //-----------------------------------------------------------------------------
  211. void SceneZoneSpace::_addOccluder( SceneObject* object )
  212. {
  213. AssertFatal( !mOccluders.contains( object ), "SceneZoneSpace::_addOccluder - Occluder already added to this zone space!" );
  214. mOccluders.push_back( object );
  215. }
  216. //-----------------------------------------------------------------------------
  217. void SceneZoneSpace::_removeOccluder( SceneObject* object )
  218. {
  219. const U32 numOccluders = mOccluders.size();
  220. for( U32 i = 0; i < numOccluders; ++ i )
  221. if( mOccluders[ i ] == object )
  222. {
  223. mOccluders.erase_fast( i );
  224. break;
  225. }
  226. AssertFatal( !mOccluders.contains( object ), "SceneZoneSpace::_removeOccluder - Occluder still added to this zone space!" );
  227. }
  228. //-----------------------------------------------------------------------------
  229. void SceneZoneSpace::_addOccludersToCullingState( SceneCullingState* state ) const
  230. {
  231. const U32 numOccluders = mOccluders.size();
  232. for( U32 i = 0; i < numOccluders; ++ i )
  233. state->addOccluder( mOccluders[ i ] );
  234. }
  235. //-----------------------------------------------------------------------------
  236. void SceneZoneSpace::_traverseConnectedZoneSpaces( SceneTraversalState* state )
  237. {
  238. // Hand the traversal over to all connected zone spaces.
  239. for( ZoneSpaceRef* ref = mConnectedZoneSpaces; ref != NULL; ref = ref->mNext )
  240. {
  241. SceneZoneSpace* zoneSpace = ref->mZoneSpace;
  242. zoneSpace->traverseZones( state );
  243. }
  244. }
  245. //-----------------------------------------------------------------------------
  246. void SceneZoneSpace::dumpZoneState( bool update )
  247. {
  248. // Nothing to dump if not registered.
  249. if( !mManager )
  250. return;
  251. // If we should update, trigger rezoning for the space
  252. // we occupy.
  253. if( update )
  254. mManager->_rezoneObjects( getWorldBox() );
  255. Con::printf( "====== Zones in: %s =====", describeSelf().c_str() );
  256. // Dump connections.
  257. for( ZoneSpaceRef* ref = mConnectedZoneSpaces; ref != NULL; ref = ref->mNext )
  258. Con::printf( "Connected to: %s", ref->mZoneSpace->describeSelf().c_str() );
  259. // Dump objects.
  260. for( U32 i = 0; i < getZoneRange(); ++ i )
  261. {
  262. U32 zoneId = getZoneRangeStart() + i;
  263. Con::printf( "--- Zone %i", zoneId );
  264. for( SceneZoneSpaceManager::ZoneContentIterator iter( mManager, zoneId, false ); iter.isValid(); ++ iter )
  265. Con::printf( iter->describeSelf() );
  266. }
  267. }
  268. //-----------------------------------------------------------------------------
  269. bool SceneZoneSpace::_setZoneGroup( void* object, const char* index, const char* data )
  270. {
  271. SceneZoneSpace* zone = reinterpret_cast< SceneZoneSpace* >( object );
  272. zone->setZoneGroup( EngineUnmarshallData< S32 >()( data ) );
  273. return false;
  274. }