Final Minetest - Please use Linux & Windows build kits at: https://downloads.minetest.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rollback_interface.h 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Minetest
  3. Copyright (C) 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. #ifndef ROLLBACK_INTERFACE_HEADER
  17. #define ROLLBACK_INTERFACE_HEADER
  18. #include "irr_v3d.h"
  19. #include <string>
  20. #include <iostream>
  21. #include <list>
  22. #include "exceptions.h"
  23. #include "inventory.h"
  24. class Map;
  25. class IGameDef;
  26. struct MapNode;
  27. class InventoryManager;
  28. struct RollbackNode
  29. {
  30. std::string name;
  31. int param1 = 0;
  32. int param2 = 0;
  33. std::string meta;
  34. bool operator == (const RollbackNode &other)
  35. {
  36. return (name == other.name && param1 == other.param1 &&
  37. param2 == other.param2 && meta == other.meta);
  38. }
  39. bool operator != (const RollbackNode &other) { return !(*this == other); }
  40. RollbackNode() = default;
  41. RollbackNode(Map *map, v3s16 p, IGameDef *gamedef);
  42. };
  43. struct RollbackAction
  44. {
  45. enum Type{
  46. TYPE_NOTHING,
  47. TYPE_SET_NODE,
  48. TYPE_MODIFY_INVENTORY_STACK,
  49. } type = TYPE_NOTHING;
  50. time_t unix_time = 0;
  51. std::string actor;
  52. bool actor_is_guess = false;
  53. v3s16 p;
  54. RollbackNode n_old;
  55. RollbackNode n_new;
  56. std::string inventory_location;
  57. std::string inventory_list;
  58. u32 inventory_index;
  59. bool inventory_add;
  60. ItemStack inventory_stack;
  61. RollbackAction() = default;
  62. void setSetNode(v3s16 p_, const RollbackNode &n_old_,
  63. const RollbackNode &n_new_)
  64. {
  65. type = TYPE_SET_NODE;
  66. p = p_;
  67. n_old = n_old_;
  68. n_new = n_new_;
  69. }
  70. void setModifyInventoryStack(const std::string &inventory_location_,
  71. const std::string &inventory_list_, u32 index_,
  72. bool add_, const ItemStack &inventory_stack_)
  73. {
  74. type = TYPE_MODIFY_INVENTORY_STACK;
  75. inventory_location = inventory_location_;
  76. inventory_list = inventory_list_;
  77. inventory_index = index_;
  78. inventory_add = add_;
  79. inventory_stack = inventory_stack_;
  80. }
  81. // String should not contain newlines or nulls
  82. std::string toString() const;
  83. // Eg. flowing water level changes are not important
  84. bool isImportant(IGameDef *gamedef) const;
  85. bool getPosition(v3s16 *dst) const;
  86. bool applyRevert(Map *map, InventoryManager *imgr, IGameDef *gamedef) const;
  87. };
  88. class IRollbackManager
  89. {
  90. public:
  91. virtual void reportAction(const RollbackAction &action) = 0;
  92. virtual std::string getActor() = 0;
  93. virtual bool isActorGuess() = 0;
  94. virtual void setActor(const std::string &actor, bool is_guess) = 0;
  95. virtual std::string getSuspect(v3s16 p, float nearness_shortcut,
  96. float min_nearness) = 0;
  97. virtual ~IRollbackManager() = default;;
  98. virtual void flush() = 0;
  99. // Get all actors that did something to position p, but not further than
  100. // <seconds> in history
  101. virtual std::list<RollbackAction> getNodeActors(v3s16 pos, int range,
  102. time_t seconds, int limit) = 0;
  103. // Get actions to revert <seconds> of history made by <actor>
  104. virtual std::list<RollbackAction> getRevertActions(const std::string &actor,
  105. time_t seconds) = 0;
  106. };
  107. class RollbackScopeActor
  108. {
  109. public:
  110. RollbackScopeActor(IRollbackManager * rollback_,
  111. const std::string & actor, bool is_guess = false) :
  112. rollback(rollback_)
  113. {
  114. if (rollback) {
  115. old_actor = rollback->getActor();
  116. old_actor_guess = rollback->isActorGuess();
  117. rollback->setActor(actor, is_guess);
  118. }
  119. }
  120. ~RollbackScopeActor()
  121. {
  122. if (rollback) {
  123. rollback->setActor(old_actor, old_actor_guess);
  124. }
  125. }
  126. private:
  127. IRollbackManager * rollback;
  128. std::string old_actor;
  129. bool old_actor_guess;
  130. };
  131. #endif