rollback_interface.h 3.9 KB

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