Ticket #4046: EntityMapForComponents.patch
File EntityMapForComponents.patch, 14.0 KB (added by , 8 years ago) |
---|
-
ComponentManager.cpp
305 305 { 306 306 // For every script component with this cid, we need to switch its 307 307 // prototype from the old constructor's prototype property to the new one's 308 const std::map<entity_id_t,IComponent*>& comps = componentManager->m_ComponentsByTypeId[cid];309 std::map<entity_id_t,IComponent*>::const_iterator eit = comps.begin();308 const EntityMap<IComponent*>& comps = componentManager->m_ComponentsByTypeId[cid]; 309 EntityMap<IComponent*>::const_iterator eit = comps.begin(); 310 310 for (; eit != comps.end(); ++eit) 311 311 { 312 312 JS::RootedValue instance(cx, eit->second->GetJSInstance()); … … 315 315 componentManager->m_ScriptInterface.SetPrototype(instance, protoVal); 316 316 } 317 317 } 318 const EntityMap<IComponent*>& localComps = componentManager->m_LocalComponentsByTypeId[cid]; 319 eit = localComps.begin(); 320 for (; eit != localComps.end(); ++eit) 321 { 322 JS::RootedValue instance(cx, eit->second->GetJSInstance()); 323 if (!instance.isNull()) 324 { 325 componentManager->m_ScriptInterface.SetPrototype(instance, protoVal); 326 } 327 } 318 328 } 319 329 } 320 330 … … 509 519 m_DynamicMessageSubscriptionsNonsyncByComponent.clear(); 510 520 511 521 // Delete all IComponents 512 std::map<ComponentTypeId, std::map<entity_id_t,IComponent*> >::iterator iit = m_ComponentsByTypeId.begin();522 std::map<ComponentTypeId, EntityMap<IComponent*> >::iterator iit = m_ComponentsByTypeId.begin(); 513 523 for (; iit != m_ComponentsByTypeId.end(); ++iit) 514 524 { 515 std::map<entity_id_t,IComponent*>::iterator eit = iit->second.begin();525 EntityMap<IComponent*>::iterator eit = iit->second.begin(); 516 526 for (; eit != iit->second.end(); ++eit) 517 527 { 518 528 eit->second->Deinit(); … … 519 529 m_ComponentTypesById[iit->first].dealloc(eit->second); 520 530 } 521 531 } 532 std::map<ComponentTypeId, EntityMap<IComponent*> >::iterator localiit = m_LocalComponentsByTypeId.begin(); 533 for (; localiit != m_LocalComponentsByTypeId.end(); ++localiit) 534 { 522 535 536 EntityMap<IComponent*>::iterator eit = localiit->second.begin(); 537 for (; eit != localiit->second.end(); ++eit) 538 { 539 eit->second->Deinit(); 540 m_ComponentTypesById[localiit->first].dealloc(eit->second); 541 } 542 } 523 543 std::vector<boost::unordered_map<entity_id_t, IComponent*> >::iterator ifcit = m_ComponentsByInterface.begin(); 524 544 for (; ifcit != m_ComponentsByInterface.end(); ++ifcit) 525 545 ifcit->clear(); 526 546 527 547 m_ComponentsByTypeId.clear(); 548 m_LocalComponentsByTypeId.clear(); 528 549 529 550 // Delete all SEntityComponentCaches 530 551 std::map<entity_id_t, SEntityComponentCache*>::iterator ccit = m_ComponentCaches.begin(); … … 757 778 return NULL; 758 779 } 759 780 760 std::map<entity_id_t, IComponent*>& emap2 = m_ComponentsByTypeId[cid];761 762 781 // If this is a scripted component, construct the appropriate JS object first 763 782 JS::RootedValue obj(cx); 764 783 if (ct.type == CT_Script) … … 780 799 781 800 // Store a reference to the new component 782 801 emap1.insert(std::make_pair(ent.GetId(), component)); 783 emap2.insert(std::make_pair(ent.GetId(), component)); 802 803 if (ENTITY_IS_LOCAL(ent.GetId())) 804 m_LocalComponentsByTypeId[cid].insert(ent.GetId() & ~ENTITY_TAGMASK, component); 805 else 806 m_ComponentsByTypeId[cid].insert(ent.GetId(), component); 784 807 // TODO: We need to more careful about this - if an entity is constructed by a component 785 808 // while we're iterating over all components, this will invalidate the iterators and everything 786 809 // will break. … … 917 940 CMessageDestroy msg(ent); 918 941 PostMessage(ent, msg); 919 942 920 // Destroy the components, and remove from m_ComponentsByTypeId :921 std::map<ComponentTypeId, std::map<entity_id_t,IComponent*> >::iterator iit = m_ComponentsByTypeId.begin();943 // Destroy the components, and remove from m_ComponentsByTypeId and m_LocalComponentsByTypeId: 944 std::map<ComponentTypeId, EntityMap<IComponent*> >::iterator iit = m_ComponentsByTypeId.begin(); 922 945 for (; iit != m_ComponentsByTypeId.end(); ++iit) 923 946 { 924 std::map<entity_id_t,IComponent*>::iterator eit = iit->second.find(ent);947 EntityMap<IComponent*>::iterator eit = iit->second.find(ent); 925 948 if (eit != iit->second.end()) 926 949 { 927 950 eit->second->Deinit(); … … 931 954 handle.GetComponentCache()->interfaces[m_ComponentTypesById[iit->first].iid] = NULL; 932 955 } 933 956 } 957 iit = m_LocalComponentsByTypeId.begin(); 958 for (; iit != m_LocalComponentsByTypeId.end(); ++iit) 959 { 960 EntityMap<IComponent*>::iterator eit = iit->second.find(ent); 961 if (eit != iit->second.end()) 962 { 963 eit->second->Deinit(); 964 RemoveComponentDynamicSubscriptions(eit->second); 965 m_ComponentTypesById[iit->first].dealloc(eit->second); 966 iit->second.erase(ent); 967 handle.GetComponentCache()->interfaces[m_ComponentTypesById[iit->first].iid] = NULL; 968 } 969 } 934 970 935 971 free(handle.GetComponentCache()); 936 972 m_ComponentCaches.erase(ent); … … 1006 1042 std::vector<ComponentTypeId>::const_iterator ctit = it->second.begin(); 1007 1043 for (; ctit != it->second.end(); ++ctit) 1008 1044 { 1045 std::map<ComponentTypeId, EntityMap<IComponent*> >::const_iterator emap; 1009 1046 // Find the component instances of this type (if any) 1010 std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator emap = m_ComponentsByTypeId.find(*ctit); 1011 if (emap == m_ComponentsByTypeId.end()) 1012 continue; 1013 1047 if (ENTITY_IS_LOCAL(ent)) 1048 { 1049 emap = m_LocalComponentsByTypeId.find(*ctit); 1050 if (emap == m_LocalComponentsByTypeId.end()) 1051 continue; 1052 } 1053 else 1054 { 1055 emap = m_ComponentsByTypeId.find(*ctit); 1056 if (emap == m_ComponentsByTypeId.end()) 1057 continue; 1058 } 1014 1059 // Send the message to all of them 1015 std::map<entity_id_t,IComponent*>::const_iterator eit = emap->second.find(ent);1060 EntityMap<IComponent*>::const_iterator eit = emap->second.find(ent); 1016 1061 if (eit != emap->second.end()) 1017 1062 eit->second->HandleMessage(msg, false); 1018 1063 } … … 1032 1077 for (; ctit != it->second.end(); ++ctit) 1033 1078 { 1034 1079 // Find the component instances of this type (if any) 1035 std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator emap = m_ComponentsByTypeId.find(*ctit); 1036 if (emap == m_ComponentsByTypeId.end()) 1037 continue; 1038 1039 // Send the message to all of them 1040 std::map<entity_id_t, IComponent*>::const_iterator eit = emap->second.begin(); 1041 for (; eit != emap->second.end(); ++eit) 1042 eit->second->HandleMessage(msg, false); 1080 std::map<ComponentTypeId, EntityMap<IComponent*> >::const_iterator emap = m_ComponentsByTypeId.find(*ctit); 1081 if (emap != m_ComponentsByTypeId.end()) 1082 { 1083 // Send the message to all of them 1084 EntityMap<IComponent*>::const_iterator eit = emap->second.begin(); 1085 for (; eit != emap->second.end(); ++eit) 1086 eit->second->HandleMessage(msg, false); 1087 } 1088 emap = m_LocalComponentsByTypeId.find((*ctit) & !ENTITY_TAGMASK); 1089 if (emap != m_LocalComponentsByTypeId.end()) 1090 { 1091 // Send the message to all of them 1092 EntityMap<IComponent*>::const_iterator eit = emap->second.begin(); 1093 for (; eit != emap->second.end(); ++eit) 1094 eit->second->HandleMessage(msg, false); 1095 } 1043 1096 } 1044 1097 } 1045 1098 … … 1069 1122 } 1070 1123 1071 1124 // Find the component instances of this type (if any) 1072 std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator emap = m_ComponentsByTypeId.find(*ctit); 1073 if (emap == m_ComponentsByTypeId.end()) 1074 continue; 1075 1076 // Send the message to all of them 1077 std::map<entity_id_t, IComponent*>::const_iterator eit = emap->second.begin(); 1078 for (; eit != emap->second.end(); ++eit) 1079 eit->second->HandleMessage(msg, true); 1125 std::map<ComponentTypeId, EntityMap<IComponent*> >::const_iterator emap = m_ComponentsByTypeId.find(*ctit); 1126 if (emap != m_ComponentsByTypeId.end()) 1127 { 1128 // Send the message to all of them 1129 EntityMap<IComponent*>::const_iterator eit = emap->second.begin(); 1130 for (; eit != emap->second.end(); ++eit) 1131 eit->second->HandleMessage(msg, true); 1132 } 1133 emap = m_LocalComponentsByTypeId.find((*ctit) & !ENTITY_TAGMASK); 1134 if (emap != m_LocalComponentsByTypeId.end()) 1135 { 1136 // Send the message to all of them 1137 EntityMap<IComponent*>::const_iterator eit = emap->second.begin(); 1138 for (; eit != emap->second.end(); ++eit) 1139 eit->second->HandleMessage(msg, true); 1140 } 1080 1141 } 1081 1142 } 1082 1143 -
ComponentManager.h
27 27 #include <boost/random/linear_congruential.hpp> 28 28 #include <boost/unordered_map.hpp> 29 29 30 #include "EntityMap.h" 30 31 #include <map> 31 32 32 33 class IComponent; … … 355 356 std::map<ComponentTypeId, ComponentType> m_ComponentTypesById; 356 357 std::vector<CComponentManager::ComponentTypeId> m_ScriptedSystemComponents; 357 358 std::vector<boost::unordered_map<entity_id_t, IComponent*> > m_ComponentsByInterface; // indexed by InterfaceId 358 std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> > m_ComponentsByTypeId; 359 std::map<ComponentTypeId, EntityMap<IComponent*> > m_ComponentsByTypeId; 360 std::map<ComponentTypeId, EntityMap<IComponent*> > m_LocalComponentsByTypeId; 359 361 std::map<MessageTypeId, std::vector<ComponentTypeId> > m_LocalMessageSubscriptions; 360 362 std::map<MessageTypeId, std::vector<ComponentTypeId> > m_GlobalMessageSubscriptions; 361 363 std::map<std::string, ComponentTypeId> m_ComponentTypeIdsByName; -
ComponentManagerSerialization.cpp
58 58 std::map<entity_id_t, std::map<ComponentTypeId, IComponent*> > components; 59 59 //std::map<ComponentTypeId, std::string> names; 60 60 61 std::map<ComponentTypeId, std::map<entity_id_t,IComponent*> >::const_iterator ctit = m_ComponentsByTypeId.begin();61 std::map<ComponentTypeId, EntityMap<IComponent*> >::const_iterator ctit = m_ComponentsByTypeId.begin(); 62 62 for (; ctit != m_ComponentsByTypeId.end(); ++ctit) 63 63 { 64 std::map<entity_id_t,IComponent*>::const_iterator eit = ctit->second.begin();64 EntityMap<IComponent*>::const_iterator eit = ctit->second.begin(); 65 65 for (; eit != ctit->second.end(); ++eit) 66 66 { 67 67 components[eit->first][ctit->first] = eit->second; … … 109 109 serializer.StringASCII("rng", SerializeRNG(m_RNG), 0, 32); 110 110 serializer.NumberU32_Unbounded("next entity id", m_NextEntityId); 111 111 112 std::map<ComponentTypeId, std::map<entity_id_t,IComponent*> >::const_iterator cit = m_ComponentsByTypeId.begin();112 std::map<ComponentTypeId, EntityMap<IComponent*> >::const_iterator cit = m_ComponentsByTypeId.begin(); 113 113 for (; cit != m_ComponentsByTypeId.end(); ++cit) 114 114 { 115 115 // In quick mode, only check unit positions … … 118 118 119 119 // Only emit component types if they have a component that will be serialized 120 120 bool needsSerialization = false; 121 for ( std::map<entity_id_t,IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit)121 for (EntityMap<IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit) 122 122 { 123 123 // Don't serialize local entities 124 124 if (ENTITY_IS_LOCAL(eit->first)) … … 133 133 134 134 serializer.NumberI32_Unbounded("component type id", cit->first); 135 135 136 for ( std::map<entity_id_t,IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit)136 for (EntityMap<IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit) 137 137 { 138 138 // Don't serialize local entities 139 139 if (ENTITY_IS_LOCAL(eit->first)) … … 188 188 serializer.StringASCII("rng", SerializeRNG(m_RNG), 0, 32); 189 189 serializer.NumberU32_Unbounded("next entity id", m_NextEntityId); 190 190 191 std::map<ComponentTypeId, std::map<entity_id_t,IComponent*> >::const_iterator cit;191 std::map<ComponentTypeId, EntityMap<IComponent*> >::const_iterator cit; 192 192 193 193 uint32_t numSystemComponentTypes = 0; 194 194 uint32_t numComponentTypes = 0; … … 199 199 { 200 200 // Only emit component types if they have a component that will be serialized 201 201 bool needsSerialization = false; 202 for ( std::map<entity_id_t,IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit)202 for (EntityMap<IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit) 203 203 { 204 204 // Don't serialize local entities, and handle SYSTEM_ENTITY separately 205 205 if (ENTITY_IS_LOCAL(eit->first) || eit->first == SYSTEM_ENTITY) … … 238 238 239 239 serializer.StringASCII("name", ctit->second.name, 0, 255); 240 240 241 std::map<entity_id_t,IComponent*>::const_iterator eit = cit->second.find(SYSTEM_ENTITY);241 EntityMap<IComponent*>::const_iterator eit = cit->second.find(SYSTEM_ENTITY); 242 242 if (eit == cit->second.end()) 243 243 { 244 244 debug_warn(L"Invalid eit"); // this should never happen … … 265 265 266 266 // Count the components before serializing any of them 267 267 uint32_t numComponents = 0; 268 for ( std::map<entity_id_t,IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit)268 for (EntityMap<IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit) 269 269 { 270 270 // Don't serialize local entities or SYSTEM_ENTITY 271 271 if (ENTITY_IS_LOCAL(eit->first) || eit->first == SYSTEM_ENTITY) … … 278 278 serializer.NumberU32_Unbounded("num components", numComponents); 279 279 280 280 // Serialize the components now 281 for ( std::map<entity_id_t,IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit)281 for (EntityMap<IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit) 282 282 { 283 283 // Don't serialize local entities or SYSTEM_ENTITY 284 284 if (ENTITY_IS_LOCAL(eit->first) || eit->first == SYSTEM_ENTITY) -
EntityMap.h
18 18 #define INCLUDED_ENTITYMAP 19 19 20 20 #include "Entity.h" 21 #include "simulation2/serialization/ISerializer.h" 22 #include "simulation2/serialization/IDeserializer.h" 21 23 22 24 /** 23 25 * A fast replacement for map<entity_id_t, T>.