clientobject.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 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 "irrlichttypes_extrabloated.h"
  18. #include "activeobject.h"
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. class ClientEnvironment;
  22. class ITextureSource;
  23. class Client;
  24. class IGameDef;
  25. class LocalPlayer;
  26. struct ItemStack;
  27. class WieldMeshSceneNode;
  28. class ClientActiveObject : public ActiveObject
  29. {
  30. public:
  31. ClientActiveObject(u16 id, Client *client, ClientEnvironment *env);
  32. virtual ~ClientActiveObject();
  33. virtual void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) = 0;
  34. virtual void removeFromScene(bool permanent) {}
  35. virtual void updateLight(u32 day_night_ratio) {}
  36. virtual bool getCollisionBox(aabb3f *toset) const { return false; }
  37. virtual bool getSelectionBox(aabb3f *toset) const { return false; }
  38. virtual bool collideWithObjects() const { return false; }
  39. virtual const v3f getPosition() const { return v3f(0.0f); }
  40. virtual scene::ISceneNode *getSceneNode() const
  41. { return NULL; }
  42. virtual scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() const
  43. { return NULL; }
  44. virtual bool isLocalPlayer() const { return false; }
  45. virtual ClientActiveObject *getParent() const { return nullptr; };
  46. virtual const std::unordered_set<int> &getAttachmentChildIds() const
  47. { static std::unordered_set<int> rv; return rv; }
  48. virtual void updateAttachments() {};
  49. virtual bool doShowSelectionBox() { return true; }
  50. // Step object in time
  51. virtual void step(float dtime, ClientEnvironment *env) {}
  52. // Process a message sent by the server side object
  53. virtual void processMessage(const std::string &data) {}
  54. virtual std::string infoText() { return ""; }
  55. virtual std::string debugInfoText() { return ""; }
  56. /*
  57. This takes the return value of
  58. ServerActiveObject::getClientInitializationData
  59. */
  60. virtual void initialize(const std::string &data) {}
  61. // Create a certain type of ClientActiveObject
  62. static ClientActiveObject *create(ActiveObjectType type, Client *client,
  63. ClientEnvironment *env);
  64. // If returns true, punch will not be sent to the server
  65. virtual bool directReportPunch(v3f dir, const ItemStack *punchitem = nullptr,
  66. float time_from_last_punch = 1000000) { return false; }
  67. protected:
  68. // Used for creating objects based on type
  69. typedef ClientActiveObject *(*Factory)(Client *client, ClientEnvironment *env);
  70. static void registerType(u16 type, Factory f);
  71. Client *m_client;
  72. ClientEnvironment *m_env;
  73. private:
  74. // Used for creating objects based on type
  75. static std::unordered_map<u16, Factory> m_types;
  76. };
  77. class DistanceSortedActiveObject
  78. {
  79. public:
  80. ClientActiveObject *obj;
  81. DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
  82. {
  83. obj = a_obj;
  84. d = a_d;
  85. }
  86. bool operator < (const DistanceSortedActiveObject &other) const
  87. {
  88. return d < other.d;
  89. }
  90. private:
  91. f32 d;
  92. };