rollback_interface.h 3.3 KB

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