rollback_interface.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "exceptions.h"
  22. class Map;
  23. class IGameDef;
  24. struct MapNode;
  25. class InventoryManager;
  26. struct RollbackNode
  27. {
  28. std::string name;
  29. int param1;
  30. int param2;
  31. std::string meta;
  32. bool operator==(const RollbackNode &other)
  33. {
  34. return (name == other.name && param1 == other.param1 &&
  35. param2 == other.param2 && meta == other.meta);
  36. }
  37. bool operator!=(const RollbackNode &other)
  38. {
  39. return !(*this == other);
  40. }
  41. RollbackNode():
  42. param1(0),
  43. param2(0)
  44. {}
  45. RollbackNode(Map *map, v3s16 p, IGameDef *gamedef);
  46. };
  47. struct RollbackAction
  48. {
  49. enum Type{
  50. TYPE_NOTHING,
  51. TYPE_SET_NODE,
  52. TYPE_MODIFY_INVENTORY_STACK,
  53. } type;
  54. time_t unix_time;
  55. std::string actor;
  56. bool actor_is_guess;
  57. v3s16 p;
  58. RollbackNode n_old;
  59. RollbackNode n_new;
  60. std::string inventory_location;
  61. std::string inventory_list;
  62. u32 inventory_index;
  63. bool inventory_add;
  64. std::string inventory_stack;
  65. RollbackAction():
  66. type(TYPE_NOTHING),
  67. unix_time(0),
  68. actor_is_guess(false)
  69. {}
  70. void setSetNode(v3s16 p_, const RollbackNode &n_old_,
  71. const RollbackNode &n_new_)
  72. {
  73. type = TYPE_SET_NODE;
  74. p = p_;
  75. n_old = n_old_;
  76. n_new = n_new_;
  77. }
  78. void setModifyInventoryStack(const std::string &inventory_location_,
  79. const std::string &inventory_list_, int index_,
  80. bool add_, const std::string &inventory_stack_)
  81. {
  82. type = TYPE_MODIFY_INVENTORY_STACK;
  83. inventory_location = inventory_location_;
  84. inventory_list = inventory_list_;
  85. inventory_index = index_;
  86. inventory_add = add_;
  87. inventory_stack = inventory_stack_;
  88. }
  89. // String should not contain newlines or nulls
  90. std::string toString() const;
  91. void fromStream(std::istream &is) throw(SerializationError);
  92. // Eg. flowing water level changes are not important
  93. bool isImportant(IGameDef *gamedef) const;
  94. bool getPosition(v3s16 *dst) const;
  95. bool applyRevert(Map *map, InventoryManager *imgr, IGameDef *gamedef) const;
  96. };
  97. class IRollbackReportSink
  98. {
  99. public:
  100. virtual ~IRollbackReportSink(){}
  101. virtual void reportAction(const RollbackAction &action) = 0;
  102. virtual std::string getActor() = 0;
  103. virtual bool isActorGuess() = 0;
  104. virtual void setActor(const std::string &actor, bool is_guess) = 0;
  105. virtual std::string getSuspect(v3s16 p, float nearness_shortcut,
  106. float min_nearness) = 0;
  107. };
  108. class RollbackScopeActor
  109. {
  110. public:
  111. RollbackScopeActor(IRollbackReportSink *sink, const std::string &actor,
  112. bool is_guess=false):
  113. m_sink(sink)
  114. {
  115. if(m_sink){
  116. m_actor_was = m_sink->getActor();
  117. m_actor_was_guess = m_sink->isActorGuess();
  118. m_sink->setActor(actor, is_guess);
  119. }
  120. }
  121. ~RollbackScopeActor()
  122. {
  123. if(m_sink){
  124. m_sink->setActor(m_actor_was, m_actor_was_guess);
  125. }
  126. }
  127. private:
  128. IRollbackReportSink *m_sink;
  129. std::string m_actor_was;
  130. bool m_actor_was_guess;
  131. };
  132. #endif