clientenvironment.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Luanti
  2. // SPDX-License-Identifier: LGPL-2.1-or-later
  3. // Copyright (C) 2010-2017 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. #pragma once
  5. #include "environment.h"
  6. #include "util/numeric.h" // IntervalLimiter
  7. #include "activeobjectmgr.h" // client::ActiveObjectMgr
  8. #include "irr_ptr.h"
  9. #include "config.h"
  10. #include <set>
  11. #if !IS_CLIENT_BUILD
  12. #error Do not include in server builds
  13. #endif
  14. class ClientSimpleObject;
  15. class ClientMap;
  16. class ClientScripting;
  17. class ClientActiveObject;
  18. class GenericCAO;
  19. class LocalPlayer;
  20. /*
  21. The client-side environment.
  22. This is not thread-safe.
  23. Must be called from main (irrlicht) thread (uses the SceneManager)
  24. Client uses an environment mutex.
  25. */
  26. enum ClientEnvEventType
  27. {
  28. CEE_NONE,
  29. CEE_PLAYER_DAMAGE
  30. };
  31. struct ClientEnvEvent
  32. {
  33. ClientEnvEventType type;
  34. union {
  35. //struct{
  36. //} none;
  37. struct{
  38. u16 amount;
  39. bool send_to_server;
  40. } player_damage;
  41. };
  42. };
  43. typedef std::unordered_map<u16, ClientActiveObject*> ClientActiveObjectMap;
  44. class ClientEnvironment : public Environment
  45. {
  46. public:
  47. ClientEnvironment(irr_ptr<ClientMap> map, ITextureSource *texturesource, Client *client);
  48. ~ClientEnvironment();
  49. Map & getMap();
  50. ClientMap & getClientMap();
  51. Client *getGameDef() { return m_client; }
  52. void setScript(ClientScripting *script) { m_script = script; }
  53. void step(f32 dtime);
  54. virtual void setLocalPlayer(LocalPlayer *player);
  55. LocalPlayer *getLocalPlayer() const { return m_local_player; }
  56. /*
  57. ClientSimpleObjects
  58. */
  59. void addSimpleObject(ClientSimpleObject *simple);
  60. /*
  61. ActiveObjects
  62. */
  63. GenericCAO* getGenericCAO(u16 id);
  64. ClientActiveObject* getActiveObject(u16 id)
  65. {
  66. return m_ao_manager.getActiveObject(id);
  67. }
  68. /*
  69. Adds an active object to the environment.
  70. Environment handles deletion of object.
  71. Object may be deleted by environment immediately.
  72. If id of object is 0, assigns a free id to it.
  73. Returns the id of the object.
  74. Returns 0 if not added and thus deleted.
  75. */
  76. u16 addActiveObject(std::unique_ptr<ClientActiveObject> object);
  77. void addActiveObject(u16 id, u8 type, const std::string &init_data);
  78. void removeActiveObject(u16 id);
  79. void processActiveObjectMessage(u16 id, const std::string &data);
  80. /*
  81. Callbacks for activeobjects
  82. */
  83. void damageLocalPlayer(u16 damage, bool handle_hp=true);
  84. /*
  85. Client likes to call these
  86. */
  87. // Get all nearby objects
  88. void getActiveObjects(const v3f &origin, f32 max_d,
  89. std::vector<DistanceSortedActiveObject> &dest)
  90. {
  91. return m_ao_manager.getActiveObjects(origin, max_d, dest);
  92. }
  93. bool hasClientEnvEvents() const { return !m_client_event_queue.empty(); }
  94. // Get event from queue. If queue is empty, it triggers an assertion failure.
  95. ClientEnvEvent getClientEnvEvent();
  96. virtual void getSelectedActiveObjects(
  97. const core::line3d<f32> &shootline_on_map,
  98. std::vector<PointedThing> &objects,
  99. const std::optional<Pointabilities> &pointabilities
  100. );
  101. const std::set<std::string> &getPlayerNames() { return m_player_names; }
  102. void addPlayerName(const std::string &name) { m_player_names.insert(name); }
  103. void removePlayerName(const std::string &name) { m_player_names.erase(name); }
  104. void updateCameraOffset(const v3s16 &camera_offset)
  105. { m_camera_offset = camera_offset; }
  106. v3s16 getCameraOffset() const { return m_camera_offset; }
  107. void updateFrameTime(bool is_paused);
  108. u64 getFrameTime() const { return m_frame_time; }
  109. u64 getFrameTimeDelta() const { return m_frame_dtime; }
  110. private:
  111. irr_ptr<ClientMap> m_map;
  112. LocalPlayer *m_local_player = nullptr;
  113. ITextureSource *m_texturesource;
  114. Client *m_client;
  115. ClientScripting *m_script = nullptr;
  116. client::ActiveObjectMgr m_ao_manager;
  117. std::vector<ClientSimpleObject*> m_simple_objects;
  118. std::queue<ClientEnvEvent> m_client_event_queue;
  119. IntervalLimiter m_active_object_light_update_interval;
  120. std::set<std::string> m_player_names;
  121. v3s16 m_camera_offset;
  122. u64 m_frame_time = 0;
  123. u64 m_frame_dtime = 0;
  124. u64 m_frame_time_pause_accumulator = 0;
  125. };