Emergency 20 Dokumentation  4.2.0
HealthComponent.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2018 Promotion Software GmbH
2 
3 
4 //[-------------------------------------------------------]
5 //[ Header guard ]
6 //[-------------------------------------------------------]
7 #pragma once
8 
9 
10 //[-------------------------------------------------------]
11 //[ Includes ]
12 //[-------------------------------------------------------]
17 
19 
22 #include <qsf/job/JobProxy.h>
23 #include <qsf/math/BitFlagSet.h>
24 #include <qsf/math/EnumBitmask.h>
25 
26 
27 //[-------------------------------------------------------]
28 //[ Forward declarations ]
29 //[-------------------------------------------------------]
30 namespace em5
31 {
32  class FreeplayEvent;
33  namespace multiplayer
34  {
35  class HealthData;
36  }
37 }
38 namespace qsf
39 {
40  class TransformComponent;
41  class MeshComponent;
42  class MessageParameters;
43 }
44 
45 
46 //[-------------------------------------------------------]
47 //[ Namespace ]
48 //[-------------------------------------------------------]
49 namespace em5
50 {
51 
52 
53  //[-------------------------------------------------------]
54  //[ Classes ]
55  //[-------------------------------------------------------]
67  {
68 
69 
70  //[-------------------------------------------------------]
71  //[ Friends ]
72  //[-------------------------------------------------------]
73  // TODO(sw) This is only needed for multiplay and there only on client side
74  // It would be better, when the simulation and pure visually updates are split into separate methods
75  // The pure visually updates could then be called cyclic on the multiplayer client (via an update job) to update the visually aspects
77 
78 
79  //[-------------------------------------------------------]
80  //[ Public definitions ]
81  //[-------------------------------------------------------]
82  public:
83  static const uint32 COMPONENT_ID;
84  static const uint32 HEALTH_ENERGY;
85  static const uint32 MAX_HEALTH_ENERGY;
86  static const uint32 LIFE_ENERGY;
87  static const uint32 MAX_LIFE_ENERGY;
88  static const uint32 DEATH_ENERGY;
89  static const uint32 MAX_DEATH_ENERGY;
90 
92  // TODO(sw) When extending this enum with additional state also multiplayer::HealthData must be updated so that it writes the additional values (currently only 2 bits are written)
93  enum State
94  {
98  STATE_DEAD
99  };
100 
103  {
104  IMMUNITY_INJURY, // Immunity to normal injuries
105  IMMUNITY_ILLNESS, // Immunity to illnesses
106  IMMUNITY_CONTAMINATION_RADIOACTIVE, // Immunity to radioactive contamination
107  IMMUNITY_CONTAMINATION_CHEMICAL, // Immunity to chemical contamination
108  IMMUNITY_CONTAMINATION_PLAGUE, // Immunity to plague contamination
109  IMMUNITY_CONTAMINATION_BIOLOGICAL // Immunity to biological contamination
110  };
112 
113  // Shortcuts
117 
118 
119  //[-------------------------------------------------------]
120  //[ Public methods ]
121  //[-------------------------------------------------------]
122  public:
130  inline explicit HealthComponent(qsf::Prototype* prototype);
131 
136  inline virtual ~HealthComponent();
137 
142  bool injurePersonByEventById(uint32 injuryId, FreeplayEvent* freeplayEvent, bool playCollapseAnimation = true);
143 
144  void killPerson(FreeplayEvent* freeplayEvent, const VisualVitalSigns* visualVitalSigns = nullptr, const qsf::StringHash& InjuryId = injury::DUMMY_INJURY);
145 
146  //[-------------------------------------------------------]
147  //[ Health energy ]
148  //[-------------------------------------------------------]
149  bool isReceivingFireDamage() const;
150 
151  // Decrease the health energy of an person
152  void receiveDamage(float amount, damagetype::DamageType damageType, qsf::Component* sender);
153 
154  // Return the health energy
155  inline float getHealthEnergy() const;
156 
157  // Set the health energy
158  void setHealthEnergy(float healthEnergy);
159 
160  // Return the maximum health energy
161  inline float getMaximumHealthEnergy() const;
162 
163  // Set the maximum health energy
164  void setMaximumHealthEnergy(float MaximumHealthEnergy);
165 
166  //[-------------------------------------------------------]
167  //[ Life energy ]
168  //[-------------------------------------------------------]
169  // Return the life energy, active in case the person is injured
170  inline float getLifeEnergy() const;
171 
172  // Set the life energy, active in case the person is injured
173  void setLifeEnergy(float lifeEnergy);
174 
175  // Return the maximum life energy
176  inline float getMaximumLifeEnergy() const;
177 
178  // Set the maximum life energy
179  void setMaximumLifeEnergy(float maximumLifeEnergy);
180 
181  //[-------------------------------------------------------]
182  //[ Death energy ]
183  //[-------------------------------------------------------]
184  // Return the death energy
185  inline float getDeathEnergy() const;
186 
187  // Set the death energy
188  void setDeathEnergy(float deathEnergy);
189 
190  // Return the maximum death energy
191  inline float getMaximumDeathEnergy() const;
192 
193  // Set the maximum death energy
194  void setMaximumDeathEnergy(float maximumDeathEnergy);
195 
196  //[-------------------------------------------------------]
197  //[ State ]
198  //[-------------------------------------------------------]
199  // Return the current health state
200  inline State getState() const;
201 
202  // Return "true" if the person is healthy (but may be injured)
203  inline bool isHealthy() const;
204 
205  // Return "true" if the person is injured (but may be healthy)
206  inline bool isInjured() const;
207 
208  // Return "true" if the person is dead
209  inline bool isDead() const;
210 
211  // Return "true" if the entity is allowed to move
212  inline bool isMovable() const;
213 
214  // Get the injury bitmask
215  inline Injury::InjuryBitmask getInjuryBitmask() const;
216 
217  inline VisualVitalSigns& getVisualVitalSigns();
218 
223  inline bool getIsDrowning() const;
224 
229  inline bool getIsKneeing() const;
230 
231  // when put on land the person is no longer drowning
232  void setDrowningFollowUpInjury();
233 
234  // Will be close to 0 as long as unit stands near the fire.
235  const qsf::Time& getElapsedTimeAfterLastHeatEnergyReceived() const;
236 
237  //[-------------------------------------------------------]
238  //[ Injury ]
239  //[-------------------------------------------------------]
240  // Get injury
241  inline const Injury* getInjury() const;
242 
243  // Get injury ID
244  inline uint32 getInjuryId() const;
245 
246  // Apply an injury
247  bool applyInjury(const Injury& injury, FreeplayEvent* freeplayEvent, bool playCollapseAnimation = true);
248 
249  // Apply an injury
250  bool applyInjuryById(const qsf::StringHash& id, FreeplayEvent* freeplayEvent, bool playCollapseAnimation = true);
251 
252  // Remove the current injury and set the health state back to healthy
253  void healAllInjuries();
254 
255  void doctorTreatmentFinished();
256 
257  void updateInjuredIcon(bool forceUpdate = false);
258 
259  inline bool getCanDoContaminationQuickTest() const;
260  inline void setCanDoContaminationQuickTest(bool canDoContaminationQuickTest);
261 
262  //[-------------------------------------------------------]
263  //[ Invincibility ]
264  //[-------------------------------------------------------]
265  inline bool isInvincible() const;
266  inline void setInvincible(bool invincible);
267 
268  //[-------------------------------------------------------]
269  //[ Immunities ]
270  //[-------------------------------------------------------]
271  inline const ImmunityBitmask& getImmunities() const;
272 
273  bool isImmune(ImmunityType immunityType) const;
274 
275  // Is the current entity immune to the given injury
276  bool isImmune(const Injury& injury) const;
277 
278  void setImmunityType(ImmunityType immunityType, bool value = true);
279 
280  // Get the immunities as string
281  std::string getImmunitiesAsString() const;
282 
283  // Set the immunities by string
284  void setImmunitiesByString(const std::string& immunities);
285 
286  //[-------------------------------------------------------]
287  //[ Resistances ]
288  //[-------------------------------------------------------]
289  // Get the resistances as string
290  std::string getResistancesAsString() const;
291 
292  // Set the resistances by string
293  void setResistancesByString(const std::string& resistances);
294 
295  // Get the resistances array
296  float* getResistancesArray();
297 
298  //[-------------------------------------------------------]
299  //[ Regeneration ]
300  //[-------------------------------------------------------]
301  // Check if regeneration in healthy state is disabled
302  bool isRegenerationDisabled() const;
303 
304  // Disable or re-enable regeneration in healthy state
305  void setRegenerationDisabled(bool noRegeneration);
306 
307  //[-------------------------------------------------------]
308  //[ Treatment ]
309  //[-------------------------------------------------------]
310  // Return "true" if this patient is currently treated by a doctor
311  inline bool isTreatmentActive() const;
312 
313  // Return "true" if this patient is currently diagnosed by a doctor
314  inline bool isDiagnosisActive() const;
315 
316  // Return "true" if the diagnosis is complete
317  inline bool isDiagnosisComplete() const;
318 
319  // Get the ID of the current injury diagnosis
320  inline uint32 getDiagnosisId() const;
321 
322  // Activate or deactivate treatment
323  inline void setTreatmentActive(bool treatment);
324 
325  // Activate or deactivate diagnosis
326  inline void setDiagnosisActive(bool diagnosis);
327 
328  // Activate or deactivate medical care
329  inline void setMedicalCareActive(bool care);
330 
331  // Activate or deactivate reanimation
332  inline void setReanimationActive(bool reanimantion);
333 
334  // Set status of the examination of the doctor
335  void setDiagnosisComplete(bool examined);
336 
337  // Set all flags to have the target ready for transport
338  // Not part of the original control flow, only for debug
339  inline void setInjuredHealed();
340 
341  //[-------------------------------------------------------]
342  //[ Contamination ]
343  //[-------------------------------------------------------]
344  // Return "true" if the person is contaminated
345  inline bool isContaminated() const;
346 
347  // Get contamination type
348  inline ContaminationType getContaminationType() const;
349 
350  // Contaminate (used for re-contamination, otherwise the injury sets the contamination already)
351  void contaminate(ContaminationType contaminationType);
352 
353  // Decontaminate
354  void decontaminate();
355 
356  //[-------------------------------------------------------]
357  //[ Cleanup ]
358  //[-------------------------------------------------------]
363  inline bool isReadyForCleanup() const;
364 
369  void cleanup();
370 
371  //[-------------------------------------------------------]
372  //[ Debug ]
373  //[-------------------------------------------------------]
374  // Return formatted string for debug display
375  std::string getFormattedDebugText() const;
376 
377 
378  //[-------------------------------------------------------]
379  //[ Public virtual qsf::Component methods ]
380  //[-------------------------------------------------------]
381  public:
382  virtual void serialize(qsf::BinarySerializer& serializer) override;
383 
384 
385  //[-------------------------------------------------------]
386  //[ Protected virtual qsf::Component methods ]
387  //[-------------------------------------------------------]
388  protected:
389  virtual bool onStartup() override;
390  virtual void onShutdown() override;
391  virtual void onSetSimulating(bool simulating) override;
392 
393 
394  //[-------------------------------------------------------]
395  //[ Private static methods ]
396  //[-------------------------------------------------------]
397  private:
398  static void delayedInfection(const qsf::MessageParameters& parameters);
399 
400 
401  //[-------------------------------------------------------]
402  //[ Private methods ]
403  //[-------------------------------------------------------]
404  private:
405  //[-------------------------------------------------------]
406  //[ Lock ]
407  //[-------------------------------------------------------]
408  // Lock the actions of the entity
409  void lockActions(bool playCollapseAnimation=true) const;
410 
411  // Unlock the actions of the entity
412  void unlockActions() const;
413 
414  //[-------------------------------------------------------]
415  //[ Update simulation ]
416  //[-------------------------------------------------------]
417  // Job updates
418  void updateSimulation(const qsf::JobArguments& jobArguments);
419 
420  // Manage the status of a healthy person
421  void manageHealthyState(float secondsPassed);
422 
423  // Manage the status of an injured person
424  void manageInjuredState(float secondsPassed);
425 
426  // Manage the status of a person in the death fight
427  void manageDeathFightState(float secondsPassed);
428 
429  // Apply the fire damage to the health
430  void applyFireDamage();
431 
432  void updateInfectionSpreading(const qsf::JobArguments& jobArguments);
433 
434  // Infect other person in infection radius
435  void processInjurySpreading(const Injury& injury);
436 
437  // Perform a state change
438  void changeState(State state);
439 
440  // Switch to the injury following the currently set injury (for illnesses and contaminations)
441  void switchToFollowingInjury();
442 
443  // Process all visual vital signs that need to be set
444  void processVisualVitalSign(const VisualVitalSigns& visualVitalSigns);
445 
450  void applyHealthValuesFromInjury(bool forceUpdate = false);
451 
452  void testAndTurnTargetFromObjectsBehind();
453 
454  void showHitpointBar();
455 
456  //[-------------------------------------------------------]
457  //[ Hint ]
458  //[-------------------------------------------------------]
459  void showDamageHint(float oldHealthEnergy) const;
460  void showHintReceiveAttackDamage() const;
461  void showMiniMapIconReceiveFireDamage() const;
462 
463  //[-------------------------------------------------------]
464  //[ Debug ]
465  //[-------------------------------------------------------]
466  // Show debug output
467  void showDebug();
468 
469 
470  //[-------------------------------------------------------]
471  //[ Private definitions ]
472  //[-------------------------------------------------------]
473  private:
474  enum InternalFlags
475  {
476  HEALTH_ACTIVE_TREATMENT = 1<<0,
477  HEALTH_ACTIVE_DIAGNOSIS = 1<<1,
478  HEALTH_ACTIVE_MEDICAL_CARE = 1<<2,
479  HEALTH_ACTIVE_REANIMATION = 1<<3,
480  HEALTH_DIAGNOSIS_COMPLETE = 1<<4,
481  HEALTH_TREATMENT_COMPLETE = 1<<5,
482  HEALTH_INJURY_HEALED = 1<<6,
483  HEALTH_NO_REGENERATION = 1<<8
485  };
486  typedef qsf::BitFlagSet<uint16, InternalFlags> InternalFlagSet;
487 
488 
489  //[-------------------------------------------------------]
490  //[ Private static data ]
491  //[-------------------------------------------------------]
492  private:
493  static qsf::MessageProxy mInfectionDelayMessageProxy;
494 
495 
496  //[-------------------------------------------------------]
497  //[ Private data ]
498  //[-------------------------------------------------------]
499  private:
500  // Connected to the CAMP system
501  float mHealthEnergy;
502  float mMaximumHealthEnergy;
503  float mLifeEnergy;
504  float mMaximumLifeEnergy;
505  float mDeathEnergy;
506  float mMaximumDeathEnergy;
507 
508  // Internal
509  State mState;
510  InternalFlagSet mInternalFlags;
511  float mEnergyChangeThisUpdate;
512  qsf::Time mRegenerationBlockedTime;
513  float mFireDamageThisUpdate;
514  qsf::WeakPtr<qsf::Component> mDamageSenderComponent;
515  StatusOverlayComponent::BarType mHealthBarShown;
516 
517  // Injury
518  const Injury* mInjury;
519  ContaminationType mContamination;
520  VisualVitalSigns mVisualVitalSigns;
521  bool mInvincible;
522  bool mCanDoContaminationQuickTest;
523 
524  // Immunity and resistance
525  std::vector<uint32> mIllnessImmunities;
526  ImmunityBitmask mImmunities;
527  float mResistances[damagetype::NUM_DAMAGETYPES];
528 
529  // Proxies
530  qsf::JobProxy mSimulationJobProxy;
531  qsf::JobProxy mSpreadInfectionJobProxy;
532  qsf::DebugDrawProxy mDebugDrawProxy;
533 
534  qsf::Time mElapsedTimeAfterLastHeatEnergyReceived;
535 
536  // Intermediate states
537  bool mCanSendReceivedHeatEnergyMessage;
538 
539  // Hint
540  qsf::Time mAttackHintWaitingTime;
541 
542  // Status icons
544  qsf::Time mTimeShowStatusIconAttack;
545  qsf::Time mTimeShowStatusIconWeapon;
546 
547 
548  //[-------------------------------------------------------]
549  //[ CAMP reflection system ]
550  //[-------------------------------------------------------]
551  QSF_CAMP_RTTI() // Only adds the virtual method "campClassId()", nothing more
552 
553 
554  };
555 
556 
557 //[-------------------------------------------------------]
558 //[ Namespace ]
559 //[-------------------------------------------------------]
560 } // em5
561 
562 
563 //[-------------------------------------------------------]
564 //[ Implementation ]
565 //[-------------------------------------------------------]
567 
568 
569 //[-------------------------------------------------------]
570 //[ CAMP reflection system ]
571 //[-------------------------------------------------------]
573 QSF_CAMP_TYPE_NONCOPYABLE(em5::HealthComponent::ImmunityType)
Definition: ActionPriority.h:13
Job proxy class.
Definition: JobProxy.h:51
Abstract component class.
Definition: Component.h:105
static const uint32 LIFE_ENERGY
Current life energy.
Definition: HealthComponent.h:86
Message parameters class.
Definition: MessageParameters.h:34
unsigned int uint32
Definition: PlatformTypes.h:181
Injury::InjuryBitmask InjuryBitmask
Definition: HealthComponent.h:115
EMERGENCY 5 entity with a health component data cache for a single entity for multiplayer.
Definition: HealthData.h:51
static const uint32 DEATH_ENERGY
Current death energy.
Definition: HealthComponent.h:88
#define EM5_API_EXPORT
Definition: Export.h:28
static const qsf::StringHash DUMMY_INJURY
Definition: Injuries.h:31
Health component base class.
Definition: HealthComponent.h:34
Message listener proxy class.
Definition: MessageProxy.h:47
static const uint32 MAX_LIFE_ENERGY
Maximum life energy.
Definition: HealthComponent.h:87
static const uint32 MAX_HEALTH_ENERGY
Maximum health energy.
Definition: HealthComponent.h:85
Job arguments class: holds data to be passed to the called job method by the job manager.
Definition: JobArguments.h:40
Person is injured, possibly already treated.
Definition: HealthComponent.h:96
EMERGENCY 5 visual vital sign class.
Definition: VisualVitalSigns.h:36
void serialize(BinarySerializer &serializer, BitFlagSet< STORAGE, ENUM > &bitFlagSet)
Definition: BitFlagSet.h:359
EMERGENCY 5 health component class.
Definition: HealthComponent.h:66
Person is near death, but can still be reanimated.
Definition: HealthComponent.h:97
Definition: PickupPersonByParamedicAction.h:21
State
Definition: ProcessState.h:20
Definition: Injuries.h:26
Debug draw proxy class.
Definition: DebugDrawProxy.h:46
Definition: HealthComponent.h:105
#define QSF_CAMP_RTTI()
Only adds the virtual method "campClassId()", nothing more.
Definition: CampClass.h:75
ImmunityType
Definition: HealthComponent.h:102
qsf::EnumBitmask< uint8, ImmunityType > ImmunityBitmask
Definition: HealthComponent.h:111
#define QSF_CAMP_TYPE_NONCOPYABLE(type)
See "CAMP_TYPE_NONCOPYABLE" for details.
Definition: CampClass.h:112
BarType
Definition: StatusOverlayComponent.h:84
static const uint32 MAX_DEATH_ENERGY
Maximum death energy.
Definition: HealthComponent.h:89
StatusIconType
Definition: StatusOverlayComponent.h:96
static const uint32 COMPONENT_ID
"em5::HealthComponent" unique component ID
Definition: HealthComponent.h:83
InjuryFlags
Definition: Injury.h:59
Injury::ContaminationType ContaminationType
Definition: HealthComponent.h:116
Person is not injured.
Definition: HealthComponent.h:95
State
Definition: HealthComponent.h:93
Prototype class.
Definition: Prototype.h:100
ContaminationType
Definition: Injury.h:67
DamageType
Definition: DamageType.h:20
Abstract EMERGENCY 5 freeplay event base class.
Definition: FreeplayEvent.h:66
Compile time string hash class.
Definition: StringHash.h:59
Injury::InjuryFlags InjuryFlags
Definition: HealthComponent.h:114
Injury definition class.
Definition: Injury.h:44
static const uint32 HEALTH_ENERGY
Current health energy.
Definition: HealthComponent.h:84
Binary serializer class.
Definition: BinarySerializer.h:35
Definition: DamageType.h:26
Definition: HealthComponent.h:104
Data type for encapsulating time information. It might represents points in time and time spans...
Definition: Time.h:32