Cafu Engine
CompHumanPlayer.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_GAMESYS_COMPONENT_HUMAN_PLAYER_HPP_INCLUDED
8 #define CAFU_GAMESYS_COMPONENT_HUMAN_PLAYER_HPP_INCLUDED
9 
10 #include "CompBase.hpp"
11 #include "../../Games/PlayerCommand.hpp" // TODO: This file must be moved (and/or its contents completely redesigned).
12 #include "GuiSys/GuiImpl.hpp"
13 
15 
16 
17 namespace cf
18 {
19  namespace GameSys
20  {
21  class CarriedWeaponT;
22  class ComponentCarriedWeaponT;
23 
24 
25  /// Entities with this component are associated with a client connection
26  /// at whose ends is a human player who provides input to control the entity.
28  {
29  public:
30 
31  /// The constructor.
33 
34  /// The copy constructor.
35  /// @param Comp The component to create a copy of.
37 
38  /// The destructor.
40 
41  /// A temporary method for compatibility with old code.
42  ArrayT<PlayerCommandT>& GetPlayerCommands() { return m_PlayerCommands; }
43 
44  /// Returns the player's current velocity (as learned from its PlayerPhysics component).
46 
47  /// Returns the origin of the entity's camera, in world-space.
49 
50  /// Returns the view (forward) vector of the entity's camera, in world space, optionally deviated by some random amount.
51  Vector3dT GetCameraViewDirWS(double Random = 0.0) const;
52 
53  /// Traces a ray that originates at the player camera's origin in the given direction through the world.
54  bool TraceCameraRay(const Vector3dT& Dir, Vector3dT& HitPoint, ComponentBaseT*& HitComp) const;
55 
56  /// A helper function for Think().
57  void CheckGUIs(bool ThinkingOnServerSide, bool HaveButtonClick) const;
58 
59  /// A helper method (that does the actual work) for DoServerFrame() *and* the (re-)prediction in the client.
60  void Think(const PlayerCommandT& PlayerCommand, bool ThinkingOnServerSide);
61 
62  /// Returns the ComponentCarriedWeaponT component of the currently active weapon,
63  /// or NULL if currently no weapon is active.
65 
66  /// This method initiates the holstering of the currently active weapon and the subsequent drawing
67  /// of the given weapon.
68  ///
69  /// If the current weapon is none, unknown or not available to the player (e.g. because it has never been picked up),
70  /// or if it turns out that the weapon does not support holstering (e.g. because there is no holstering
71  /// sequence available), the holstering is skipped and the next weapon is drawn immediately.
72  /// If the current weapon is fine but is not idle at the time that this method is called (e.g. reloading
73  /// or firing), the call is *ignored*, that is, the weapon is *not* changed.
74  ///
75  /// @param NextWeaponNr The index number into the CarriedWeapon components of this entity, starting at 1.
76  /// Use 0 to select "no" weapon.
77  /// @param Force If `true`, forces the drawing of the next weapon immediately, ignoring the idle
78  /// state and holstering sequence of the current weapon. This is normally only used
79  /// if, for example, the last hand grenade has been thrown and bare-handed animation
80  /// sequences for idle and holster are not available.
81  void SelectWeapon(uint8_t NextWeaponNr, bool Force = false);
82 
83  /// This method draws the next weapon as previously prepared by SelectWeapon().
84  ///
85  /// It is intended to be called at the end of the holstering sequence of the previous weapon, either
86  /// directly from SelectWeapon() when it found that holstering can entirely be skipped, or indirectly
87  /// when SelectWeapon() calls the previous weapon's `Holster()` callback, the end of the holster
88  /// sequence is detected in the `OnSequenceWrap_Sv()` script callback, and its implementation in turn
89  /// calls this method.
90  void SelectNextWeapon();
91 
92  /// Returns a pseudo-random integer in range `0 ... n-1`.
93  ///
94  /// The important aspect of this method is that it returns pseudo-random numbers that are reproducible
95  /// in the context of the "client prediction" feature of the Cafu Engine. All random numbers that are
96  /// used in human player code must be obtained from one of the GetRandom() methods in this class.
97  unsigned int GetRandom(unsigned int n);
98 
99  /// Returns a pseudo-random double in range `[0.0, 1.0]` (inclusive).
100  ///
101  /// The important aspect of this method is that it returns pseudo-random numbers that are reproducible
102  /// in the context of the "client prediction" feature of the Cafu Engine. All random numbers that are
103  /// used in human player code must be obtained from one of the GetRandom() methods in this class.
104  double GetRandom();
105 
106 
107  // Base class overrides.
108  ComponentHumanPlayerT* Clone() const override;
109  const char* GetName() const override { return "HumanPlayer"; }
110  BoundingBox3fT GetCullingBB() const override;
111  void PostRender(bool FirstPersonView) override;
112  void DoServerFrame(float t) override;
113  void DoClientFrame(float t) override;
114 
115 
116  // The TypeSys related declarations for this class.
117  const cf::TypeSys::TypeInfoT* GetType() const override { return &TypeInfo; }
118  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
119  static const cf::TypeSys::TypeInfoT TypeInfo;
120 
121 
122  protected:
123 
124  // The Lua API methods of this class.
125  static int GetActiveWeapon(lua_State* LuaState);
126  static int SelectWeapon(lua_State* LuaState);
127  static int SelectNextWeapon(lua_State* LuaState);
128  static int FireRay(lua_State* LuaState);
129  static int GetRandom(lua_State* LuaState);
130  static int SpawnWeaponChild(lua_State* LuaState);
131  static int RegisterParticle(lua_State* LuaState);
132  static int toString(lua_State* LuaState);
133 
134  static const luaL_Reg MethodsList[]; ///< The list of Lua methods for this class.
135  static const char* DocClass;
136  static const cf::TypeSys::MethsDocT DocMethods[];
137  static const cf::TypeSys::VarsDocT DocVars[];
138 
139 
140  private:
141 
142  void FillMemberVars(); ///< A helper method for the constructors.
143  bool IsPlayerPrototype() const; ///< Is this component in a player prototype entity, or in a concrete player instance?
144  IntrusivePtrT<cf::GuiSys::GuiImplT> GetGuiHUD(); ///< Returns the GUI instance for the player's Head-Up Display.
145 
146  TypeSys::VarT<std::string> m_PlayerName; ///< The name that the player chose for himself.
147  TypeSys::VarT<uint16_t> m_RandomCount; ///< Keeps track of the next random number that is returned by the GetRandom() methods.
148  TypeSys::VarT<uint8_t> m_StateOfExistence; ///< For the player's main state machine, e.g. "spectator, dead, alive, ...".
149  TypeSys::VarT<uint8_t> m_Health; ///< Health.
150  TypeSys::VarT<uint8_t> m_Armor; ///< Armor.
151  TypeSys::VarT<uint8_t> m_Frags; ///< Frags.
152  TypeSys::VarT<uint8_t> m_ActiveWeaponNr; ///< The index number into the CarriedWeapon components of this entity, starting at 1, indicating the currently active weapon. The weapon must also be available (have been picked up) before the player can use it. A value of 0 means that "no" weapon is currently active.
153  TypeSys::VarT<uint8_t> m_NextWeaponNr; ///< The next weapon to be drawn by SelectNextWeapon(). Like m_ActiveWeaponNr, this is an index number into the CarriedWeapon components of this entity, starting at 1. A value of 0 means "none".
154  TypeSys::VarT<float> m_HeadSway; ///< The progress of one "head swaying" cycle in state FrozenSpectator.
155 
156  ArrayT<PlayerCommandT> m_PlayerCommands; ///< The commands to be processed in the next Think() step.
157  IntrusivePtrT<GuiSys::GuiImplT> m_GuiHUD; ///< The GUI instance for the player's Head-Up Display.
158  ParticleMaterialSetT* m_GenericMatSet; ///< Resources needed to implement the temporary RegisterParticle() method.
159  ParticleMaterialSetT* m_WhiteSmokeMatSet; ///< Resources needed to implement the temporary RegisterParticle() method.
160  };
161  }
162 }
163 
164 #endif
Vector3dT GetCameraOriginWS() const
Returns the origin of the entity's camera, in world-space.
Definition: CompHumanPlayer.cpp:257
void DoServerFrame(float t) override
Derived classes override this method in order to implement the real work proposed by OnServerFrame()...
Definition: CompHumanPlayer.cpp:963
This class implements smart (reference-counted) pointers.
Definition: Pointer.hpp:43
ArrayT< PlayerCommandT > & GetPlayerCommands()
A temporary method for compatibility with old code.
Definition: CompHumanPlayer.hpp:42
Vector3dT GetCameraViewDirWS(double Random=0.0) const
Returns the view (forward) vector of the entity's camera, in world space, optionally deviated by some...
Definition: CompHumanPlayer.cpp:270
void DoClientFrame(float t) override
Derived classes override this method in order to implement the real work proposed by OnClientFrame()...
Definition: CompHumanPlayer.cpp:1032
bool TraceCameraRay(const Vector3dT &Dir, Vector3dT &HitPoint, ComponentBaseT *&HitComp) const
Traces a ray that originates at the player camera's origin in the given direction through the world...
Definition: CompHumanPlayer.cpp:298
This struct represents per-frame player inputs for controlling human player entities.
Definition: PlayerCommand.hpp:32
This class represents a set of (render-)materials.
Definition: ParticleEngineMS.hpp:60
Vector3dT GetPlayerVelocity() const
Returns the player's current velocity (as learned from its PlayerPhysics component).
Definition: CompHumanPlayer.cpp:242
void Think(const PlayerCommandT &PlayerCommand, bool ThinkingOnServerSide)
A helper method (that does the actual work) for DoServerFrame() and the (re-)prediction in the client...
Definition: CompHumanPlayer.cpp:472
void SelectWeapon(uint8_t NextWeaponNr, bool Force=false)
This method initiates the holstering of the currently active weapon and the subsequent drawing of the...
Definition: CompHumanPlayer.cpp:812
double GetRandom()
Returns a pseudo-random double in range [0.0, 1.0] (inclusive).
Definition: CompHumanPlayer.cpp:917
const char * GetName() const override
Returns the name of this component.
Definition: CompHumanPlayer.hpp:109
void SelectNextWeapon()
This method draws the next weapon as previously prepared by SelectWeapon().
Definition: CompHumanPlayer.cpp:860
void CheckGUIs(bool ThinkingOnServerSide, bool HaveButtonClick) const
A helper function for Think().
Definition: CompHumanPlayer.cpp:338
static const luaL_Reg MethodsList[]
The list of Lua methods for this class.
Definition: CompHumanPlayer.hpp:134
ComponentHumanPlayerT()
The constructor.
Definition: CompHumanPlayer.cpp:73
IntrusivePtrT< ComponentCarriedWeaponT > GetActiveWeapon() const
Returns the ComponentCarriedWeaponT component of the currently active weapon, or NULL if currently no...
Definition: CompHumanPlayer.cpp:798
void PostRender(bool FirstPersonView) override
This method provides an opportunity for another render pass.
Definition: CompHumanPlayer.cpp:940
ComponentHumanPlayerT * Clone() const override
The virtual copy constructor.
Definition: CompHumanPlayer.cpp:332
BoundingBox3fT GetCullingBB() const override
This method returns a bounding-box that encloses the visual representation of this component...
Definition: CompHumanPlayer.cpp:929
Definition: TypeSys.hpp:52
Definition: TypeSys.hpp:57
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
~ComponentHumanPlayerT()
The destructor.
Definition: CompHumanPlayer.cpp:131
Entities with this component are associated with a client connection at whose ends is a human player ...
Definition: CompHumanPlayer.hpp:27
Definition: TypeSys.hpp:68
This is the base class for the components that an entity is composed/aggregated of.
Definition: CompBase.hpp:54