clientenvironment.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2017 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "environment.h"
  18. #include "util/numeric.h" // IntervalLimiter
  19. #include "activeobjectmgr.h" // client::ActiveObjectMgr
  20. #include <set>
  21. class ClientSimpleObject;
  22. class ClientMap;
  23. class ClientScripting;
  24. class ClientActiveObject;
  25. class GenericCAO;
  26. class LocalPlayer;
  27. /*
  28. The client-side environment.
  29. This is not thread-safe.
  30. Must be called from main (irrlicht) thread (uses the SceneManager)
  31. Client uses an environment mutex.
  32. */
  33. enum ClientEnvEventType
  34. {
  35. CEE_NONE,
  36. CEE_PLAYER_DAMAGE
  37. };
  38. struct ClientEnvEvent
  39. {
  40. ClientEnvEventType type;
  41. union {
  42. //struct{
  43. //} none;
  44. struct{
  45. u16 amount;
  46. bool send_to_server;
  47. } player_damage;
  48. };
  49. };
  50. typedef std::unordered_map<u16, ClientActiveObject*> ClientActiveObjectMap;
  51. class ClientEnvironment : public Environment
  52. {
  53. public:
  54. ClientEnvironment(ClientMap *map, ITextureSource *texturesource, Client *client);
  55. ~ClientEnvironment();
  56. Map & getMap();
  57. ClientMap & getClientMap();
  58. Client *getGameDef() { return m_client; }
  59. void setScript(ClientScripting *script) { m_script = script; }
  60. void step(f32 dtime);
  61. virtual void setLocalPlayer(LocalPlayer *player);
  62. LocalPlayer *getLocalPlayer() const { return m_local_player; }
  63. /*
  64. ClientSimpleObjects
  65. */
  66. void addSimpleObject(ClientSimpleObject *simple);
  67. /*
  68. ActiveObjects
  69. */
  70. GenericCAO* getGenericCAO(u16 id);
  71. ClientActiveObject* getActiveObject(u16 id)
  72. {
  73. return m_ao_manager.getActiveObject(id);
  74. }
  75. /*
  76. Adds an active object to the environment.
  77. Environment handles deletion of object.
  78. Object may be deleted by environment immediately.
  79. If id of object is 0, assigns a free id to it.
  80. Returns the id of the object.
  81. Returns 0 if not added and thus deleted.
  82. */
  83. u16 addActiveObject(std::unique_ptr<ClientActiveObject> object);
  84. void addActiveObject(u16 id, u8 type, const std::string &init_data);
  85. void removeActiveObject(u16 id);
  86. void processActiveObjectMessage(u16 id, const std::string &data);
  87. /*
  88. Callbacks for activeobjects
  89. */
  90. void damageLocalPlayer(u16 damage, bool handle_hp=true);
  91. /*
  92. Client likes to call these
  93. */
  94. // Get all nearby objects
  95. void getActiveObjects(const v3f &origin, f32 max_d,
  96. std::vector<DistanceSortedActiveObject> &dest)
  97. {
  98. return m_ao_manager.getActiveObjects(origin, max_d, dest);
  99. }
  100. bool hasClientEnvEvents() const { return !m_client_event_queue.empty(); }
  101. // Get event from queue. If queue is empty, it triggers an assertion failure.
  102. ClientEnvEvent getClientEnvEvent();
  103. virtual void getSelectedActiveObjects(
  104. const core::line3d<f32> &shootline_on_map,
  105. std::vector<PointedThing> &objects
  106. );
  107. const std::set<std::string> &getPlayerNames() { return m_player_names; }
  108. void addPlayerName(const std::string &name) { m_player_names.insert(name); }
  109. void removePlayerName(const std::string &name) { m_player_names.erase(name); }
  110. void updateCameraOffset(const v3s16 &camera_offset)
  111. { m_camera_offset = camera_offset; }
  112. v3s16 getCameraOffset() const { return m_camera_offset; }
  113. void updateFrameTime(bool is_paused);
  114. u64 getFrameTime() const { return m_frame_time; }
  115. u64 getFrameTimeDelta() const { return m_frame_dtime; }
  116. private:
  117. ClientMap *m_map;
  118. LocalPlayer *m_local_player = nullptr;
  119. ITextureSource *m_texturesource;
  120. Client *m_client;
  121. ClientScripting *m_script = nullptr;
  122. client::ActiveObjectMgr m_ao_manager;
  123. std::vector<ClientSimpleObject*> m_simple_objects;
  124. std::queue<ClientEnvEvent> m_client_event_queue;
  125. IntervalLimiter m_active_object_light_update_interval;
  126. std::set<std::string> m_player_names;
  127. v3s16 m_camera_offset;
  128. u64 m_frame_time = 0;
  129. u64 m_frame_dtime = 0;
  130. u64 m_frame_time_pause_accumulator = 0;
  131. };