clientenvironment.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <ISceneManager.h>
  19. #include "clientobject.h"
  20. #include "util/numeric.h"
  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. CEE_PLAYER_BREATH
  38. };
  39. struct ClientEnvEvent
  40. {
  41. ClientEnvEventType type;
  42. union {
  43. //struct{
  44. //} none;
  45. struct{
  46. u8 amount;
  47. bool send_to_server;
  48. } player_damage;
  49. struct{
  50. u16 amount;
  51. } player_breath;
  52. };
  53. };
  54. typedef std::unordered_map<u16, ClientActiveObject*> ClientActiveObjectMap;
  55. class ClientEnvironment : public Environment
  56. {
  57. public:
  58. ClientEnvironment(ClientMap *map, ITextureSource *texturesource, Client *client);
  59. ~ClientEnvironment();
  60. Map & getMap();
  61. ClientMap & getClientMap();
  62. Client *getGameDef() { return m_client; }
  63. void setScript(ClientScripting *script) { m_script = script; }
  64. void step(f32 dtime);
  65. virtual void setLocalPlayer(LocalPlayer *player);
  66. LocalPlayer *getLocalPlayer() const { return m_local_player; }
  67. /*
  68. ClientSimpleObjects
  69. */
  70. void addSimpleObject(ClientSimpleObject *simple);
  71. /*
  72. ActiveObjects
  73. */
  74. GenericCAO* getGenericCAO(u16 id);
  75. ClientActiveObject* getActiveObject(u16 id);
  76. /*
  77. Adds an active object to the environment.
  78. Environment handles deletion of object.
  79. Object may be deleted by environment immediately.
  80. If id of object is 0, assigns a free id to it.
  81. Returns the id of the object.
  82. Returns 0 if not added and thus deleted.
  83. */
  84. u16 addActiveObject(ClientActiveObject *object);
  85. void addActiveObject(u16 id, u8 type, const std::string &init_data);
  86. void removeActiveObject(u16 id);
  87. void processActiveObjectMessage(u16 id, const std::string &data);
  88. /*
  89. Callbacks for activeobjects
  90. */
  91. void damageLocalPlayer(u8 damage, bool handle_hp=true);
  92. void updateLocalPlayerBreath(u16 breath);
  93. /*
  94. Client likes to call these
  95. */
  96. // Get all nearby objects
  97. void getActiveObjects(v3f origin, f32 max_d,
  98. std::vector<DistanceSortedActiveObject> &dest);
  99. bool hasClientEnvEvents() const { return !m_client_event_queue.empty(); }
  100. // Get event from queue. If queue is empty, it triggers an assertion failure.
  101. ClientEnvEvent getClientEnvEvent();
  102. virtual void getSelectedActiveObjects(
  103. const core::line3d<f32> &shootline_on_map,
  104. std::vector<PointedThing> &objects
  105. );
  106. u16 attachement_parent_ids[USHRT_MAX + 1];
  107. const std::list<std::string> &getPlayerNames() { return m_player_names; }
  108. void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
  109. void removePlayerName(const std::string &name) { m_player_names.remove(name); }
  110. void updateCameraOffset(const v3s16 &camera_offset)
  111. { m_camera_offset = camera_offset; }
  112. v3s16 getCameraOffset() const { return m_camera_offset; }
  113. private:
  114. ClientMap *m_map;
  115. LocalPlayer *m_local_player = nullptr;
  116. ITextureSource *m_texturesource;
  117. Client *m_client;
  118. ClientScripting *m_script = nullptr;
  119. ClientActiveObjectMap m_active_objects;
  120. std::vector<ClientSimpleObject*> m_simple_objects;
  121. std::queue<ClientEnvEvent> m_client_event_queue;
  122. IntervalLimiter m_active_object_light_update_interval;
  123. IntervalLimiter m_lava_hurt_interval;
  124. IntervalLimiter m_drowning_interval;
  125. IntervalLimiter m_breathing_interval;
  126. std::list<std::string> m_player_names;
  127. v3s16 m_camera_offset;
  128. };