1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ 2 3 #ifndef EVENT_BATCH_HANDLER_HDR 4 #define EVENT_BATCH_HANDLER_HDR 5 6 #include "EventClient.h" 7 #include "lib/gml/ThreadSafeContainers.h" 8 #include "Sim/Projectiles/ProjectileHandler.h" // for UNSYNCED_PROJ_NOEVENT 9 10 class CUnit; 11 class CFeature; 12 class CProjectile; 13 14 class EventBatchHandler : public CEventClient { 15 public: // EventClient GetFullRead()16 bool GetFullRead() const { return true; } GetReadAllyTeam()17 int GetReadAllyTeam() const { return CEventClient::AllAccessTeam; } 18 19 void UnitMoved(const CUnit* unit); 20 void UnitEnteredRadar(const CUnit* unit, int at); 21 void UnitEnteredLos(const CUnit* unit, int at); 22 void UnitLeftRadar(const CUnit* unit, int at); 23 void UnitLeftLos(const CUnit* unit, int at); 24 void UnitCloaked(const CUnit* unit); 25 void UnitDecloaked(const CUnit* unit); 26 27 void FeatureCreated(const CFeature* feature); 28 void FeatureDestroyed(const CFeature* feature); 29 void FeatureMoved(const CFeature* feature, const float3& oldpos); 30 void ProjectileCreated(const CProjectile* proj); 31 void ProjectileDestroyed(const CProjectile* proj); 32 33 //FIXME check them in EventHandler to see what needs to be fixed 34 //void UnsyncedProjectileCreated(const CProjectile* proj) 35 //void UnsyncedProjectileDestroyed(const CProjectile* proj) 36 //void LoadedModelRequested() 37 38 public: GetInstance()39 static EventBatchHandler* GetInstance() { assert(ebh); return ebh; } 40 static void CreateInstance(); 41 static void DeleteInstance(); 42 43 EventBatchHandler(); ~EventBatchHandler()44 virtual ~EventBatchHandler() {} 45 46 private: 47 static EventBatchHandler* ebh; 48 static boost::int64_t eventSequenceNumber; 49 50 private: 51 struct ProjectileCreatedDestroyedEvent { 52 static void Add(const CProjectile*); 53 static void Remove(const CProjectile*); 54 static void Delete(const CProjectile*); 55 }; 56 57 typedef ThreadListRender< 58 const CProjectile*, 59 std::set<const CProjectile*>, 60 const CProjectile*, 61 ProjectileCreatedDestroyedEvent 62 > ProjectileCreatedDestroyedEventBatch; 63 64 #if UNSYNCED_PROJ_NOEVENT 65 struct UnsyncedProjectileCreatedDestroyedEvent { 66 static void Add(const CProjectile*); 67 static void Remove(const CProjectile*); 68 static void Delete(const CProjectile*); 69 }; 70 71 typedef ThreadListRender< 72 const CProjectile*, 73 std::set<const CProjectile*>, 74 const CProjectile*, 75 UnsyncedProjectileCreatedDestroyedEvent 76 > UnsyncedProjectileCreatedDestroyedEventBatch; 77 #endif 78 79 struct UAD { 80 const CUnit* unit; 81 int data; 82 int status; 83 boost::int64_t seqnum; 84 UADUAD85 UAD(const CUnit* u, int d): unit(u), data(d), status(0), seqnum(eventSequenceNumber++) {} UADUAD86 UAD(const CUnit* u, int d, int s): unit(u), data(d), status(s), seqnum(eventSequenceNumber++) {} 87 bool operator==(const CUnit* u) const { return unit == u; } 88 bool operator==(const UAD& u) const { return unit == u.unit && seqnum == u.seqnum; } 89 bool operator<(const UAD& u) const { return unit < u.unit || (unit == u.unit && seqnum < u.seqnum); } 90 }; 91 public: 92 struct UD { 93 const CUnit* unit; 94 int data; 95 UDUD96 UD(const CUnit* u): unit(u), data(0) {} UDUD97 UD(const CUnit* u, int d): unit(u), data(d) {} 98 bool operator==(const UD& u) const { return unit == u.unit; } 99 bool operator<(const UD& u) const { return unit < u.unit; } 100 }; 101 102 struct UAP { 103 const CUnit* unit; 104 boost::int64_t seqnum; 105 float3 newpos; 106 UAPUAP107 UAP(const CUnit* u, const float3& np): unit(u), seqnum(eventSequenceNumber++), newpos(np) {} 108 bool operator==(const CUnit* u) const { return unit == u; } 109 bool operator==(const UAP& u) const { return unit == u.unit && seqnum == u.seqnum; } 110 bool operator<(const UAP& u) const { return unit < u.unit || (unit == u.unit && seqnum < u.seqnum); } 111 }; 112 113 private: 114 struct UnitCreatedDestroyedEvent { 115 static void Add(const UD&); 116 static void Remove(const UD&); DeleteUnitCreatedDestroyedEvent117 static void Delete(const UD&) { } 118 }; 119 120 struct UnitCloakStateChangedEvent { 121 static void Add(const UAD&); RemoveUnitCloakStateChangedEvent122 static void Remove(const UAD&) { } DeleteUnitCloakStateChangedEvent123 static void Delete(const UAD&) { } 124 }; 125 126 struct UnitLOSStateChangedEvent { 127 static void Add(const UAD&); RemoveUnitLOSStateChangedEvent128 static void Remove(const UAD&) { } DeleteUnitLOSStateChangedEvent129 static void Delete(const UAD&) { } 130 }; 131 132 struct UnitMovedEvent { 133 static void Add(const UAP&); RemoveUnitMovedEvent134 static void Remove(const UAP&) { } DeleteUnitMovedEvent135 static void Delete(const UAP&) { } 136 }; 137 138 typedef ThreadListRender<const CUnit *, std::set<UD>, UD, UnitCreatedDestroyedEvent> UnitCreatedDestroyedEventBatch; 139 typedef ThreadListRender<const CUnit *, std::set<UAD>, UAD, UnitCloakStateChangedEvent> UnitCloakStateChangedEventBatch; 140 typedef ThreadListRender<const CUnit *, std::set<UAD>, UAD, UnitLOSStateChangedEvent> UnitLOSStateChangedEventBatch; 141 typedef ThreadListRender< 142 const CUnit*, 143 std::set<UAP>, 144 UAP, 145 UnitMovedEvent 146 > UnitMovedEventBatch; 147 struct FAP { 148 const CFeature* feat; 149 boost::int64_t seqnum; 150 float3 oldpos; 151 float3 newpos; 152 FAPFAP153 FAP(const CFeature* f, const float3& op, const float3& np): feat(f), seqnum(eventSequenceNumber++), oldpos(op), newpos(np) {} 154 bool operator==(const CFeature* f) const { return feat == f; } 155 bool operator==(const FAP& f) const { return feat == f.feat && seqnum == f.seqnum; } 156 bool operator<(const FAP& f) const { return feat < f.feat || (feat == f.feat && seqnum < f.seqnum); } 157 }; 158 159 struct FeatureCreatedDestroyedEvent { 160 static void Add(const CFeature*); 161 static void Remove(const CFeature*); DeleteFeatureCreatedDestroyedEvent162 static void Delete(const CFeature*) { } 163 }; 164 165 struct FeatureMovedEvent { 166 static void Add(const FAP&); RemoveFeatureMovedEvent167 static void Remove(const FAP&) { } DeleteFeatureMovedEvent168 static void Delete(const FAP&) { } 169 }; 170 171 typedef ThreadListRender< 172 const CFeature*, 173 std::set<const CFeature*>, 174 const CFeature*, 175 FeatureCreatedDestroyedEvent 176 > FeatureCreatedDestroyedEventBatch; 177 typedef ThreadListRender< 178 const CFeature*, 179 std::set<FAP>, 180 FAP, 181 FeatureMovedEvent 182 > FeatureMovedEventBatch; 183 184 //! contains only projectiles that can change simulation state 185 ProjectileCreatedDestroyedEventBatch syncedProjectileCreatedDestroyedEventBatch; 186 187 #if UNSYNCED_PROJ_NOEVENT 188 static UnsyncedProjectileCreatedDestroyedEventBatch 189 #else 190 ProjectileCreatedDestroyedEventBatch 191 #endif 192 //! contains only projectiles that cannot change simulation state 193 unsyncedProjectileCreatedDestroyedEventBatch; 194 195 UnitCreatedDestroyedEventBatch unitCreatedDestroyedEventBatch; 196 UnitCloakStateChangedEventBatch unitCloakStateChangedEventBatch; 197 UnitLOSStateChangedEventBatch unitLOSStateChangedEventBatch; 198 UnitMovedEventBatch unitMovedEventBatch; 199 200 FeatureCreatedDestroyedEventBatch featureCreatedDestroyedEventBatch; 201 FeatureMovedEventBatch featureMovedEventBatch; 202 203 public: 204 void UpdateUnits(); 205 void UpdateDrawUnits(); 206 void DeleteSyncedUnits(); 207 208 void UpdateFeatures(); 209 void UpdateDrawFeatures(); 210 void DeleteSyncedFeatures(); 211 212 void UpdateProjectiles(); 213 void UpdateDrawProjectiles(); 214 void DeleteSyncedProjectiles(); 215 216 void UpdateObjects(); 217 218 void LoadedModelRequested(); 219 EnqueueUnitLOSStateChangeEvent(const CUnit * unit,int allyteam,int newstatus)220 void EnqueueUnitLOSStateChangeEvent(const CUnit* unit, int allyteam, int newstatus) { unitLOSStateChangedEventBatch.enqueue(UAD(unit, allyteam, newstatus)); } EnqueueUnitCloakStateChangeEvent(const CUnit * unit,int cloaked)221 void EnqueueUnitCloakStateChangeEvent(const CUnit* unit, int cloaked) { unitCloakStateChangedEventBatch.enqueue(UAD(unit, cloaked)); } 222 EnqueueUnitMovedEvent(const CUnit * unit,const float3 & newpos)223 void EnqueueUnitMovedEvent(const CUnit* unit, const float3& newpos) { 224 unitMovedEventBatch.enqueue(UAP(unit, newpos)); 225 } EnqueueFeatureMovedEvent(const CFeature * feature,const float3 & oldpos,const float3 & newpos)226 void EnqueueFeatureMovedEvent(const CFeature* feature, const float3& oldpos, const float3& newpos) { 227 featureMovedEventBatch.enqueue(FAP(feature, oldpos, newpos)); 228 } 229 GetSyncedProjectileCreatedDestroyedBatch()230 ProjectileCreatedDestroyedEventBatch& GetSyncedProjectileCreatedDestroyedBatch() { return syncedProjectileCreatedDestroyedEventBatch; } 231 232 #if UNSYNCED_PROJ_NOEVENT 233 static inline UnsyncedProjectileCreatedDestroyedEventBatch& 234 #else 235 ProjectileCreatedDestroyedEventBatch& 236 #endif GetUnsyncedProjectileCreatedDestroyedBatch()237 GetUnsyncedProjectileCreatedDestroyedBatch() { return unsyncedProjectileCreatedDestroyedEventBatch; } 238 239 GetUnitCreatedDestroyedBatch()240 UnitCreatedDestroyedEventBatch& GetUnitCreatedDestroyedBatch() { return unitCreatedDestroyedEventBatch; } GetUnitCloakStateChangedBatch()241 UnitCloakStateChangedEventBatch& GetUnitCloakStateChangedBatch() { return unitCloakStateChangedEventBatch; } GetUnitLOSStateChangedBatch()242 UnitLOSStateChangedEventBatch& GetUnitLOSStateChangedBatch() { return unitLOSStateChangedEventBatch; } 243 GetFeatureCreatedDestroyedEventBatch()244 FeatureCreatedDestroyedEventBatch& GetFeatureCreatedDestroyedEventBatch() { return featureCreatedDestroyedEventBatch; } GetFeatureMovedEventBatch()245 FeatureMovedEventBatch& GetFeatureMovedEventBatch() { return featureMovedEventBatch; } 246 }; 247 248 #define eventBatchHandler (EventBatchHandler::GetInstance()) 249 250 #endif 251