Minetest  5.4.0
rollback_interface.h
Go to the documentation of this file.
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #pragma once
21 
22 #include "irr_v3d.h"
23 #include <string>
24 #include <iostream>
25 #include <list>
26 #include "exceptions.h"
27 #include "inventory.h"
28 
29 class Map;
30 class IGameDef;
31 struct MapNode;
32 class InventoryManager;
33 
35 {
36  std::string name;
37  int param1 = 0;
38  int param2 = 0;
39  std::string meta;
40 
41  bool operator == (const RollbackNode &other)
42  {
43  return (name == other.name && param1 == other.param1 &&
44  param2 == other.param2 && meta == other.meta);
45  }
46  bool operator != (const RollbackNode &other) { return !(*this == other); }
47 
48  RollbackNode() = default;
49 
50  RollbackNode(Map *map, v3s16 p, IGameDef *gamedef);
51 };
52 
53 
55 {
56  enum Type{
61 
62  time_t unix_time = 0;
63  std::string actor;
64  bool actor_is_guess = false;
65 
69 
70  std::string inventory_location;
71  std::string inventory_list;
75 
76  RollbackAction() = default;
77 
78  void setSetNode(v3s16 p_, const RollbackNode &n_old_,
79  const RollbackNode &n_new_)
80  {
82  p = p_;
83  n_old = n_old_;
84  n_new = n_new_;
85  }
86 
87  void setModifyInventoryStack(const std::string &inventory_location_,
88  const std::string &inventory_list_, u32 index_,
89  bool add_, const ItemStack &inventory_stack_)
90  {
92  inventory_location = inventory_location_;
93  inventory_list = inventory_list_;
94  inventory_index = index_;
95  inventory_add = add_;
96  inventory_stack = inventory_stack_;
97  }
98 
99  // String should not contain newlines or nulls
100  std::string toString() const;
101 
102  // Eg. flowing water level changes are not important
103  bool isImportant(IGameDef *gamedef) const;
104 
105  bool getPosition(v3s16 *dst) const;
106 
107  bool applyRevert(Map *map, InventoryManager *imgr, IGameDef *gamedef) const;
108 };
109 
110 
112 {
113 public:
114  virtual void reportAction(const RollbackAction &action) = 0;
115  virtual std::string getActor() = 0;
116  virtual bool isActorGuess() = 0;
117  virtual void setActor(const std::string &actor, bool is_guess) = 0;
118  virtual std::string getSuspect(v3s16 p, float nearness_shortcut,
119  float min_nearness) = 0;
120 
121  virtual ~IRollbackManager() = default;;
122  virtual void flush() = 0;
123  // Get all actors that did something to position p, but not further than
124  // <seconds> in history
125  virtual std::list<RollbackAction> getNodeActors(v3s16 pos, int range,
126  time_t seconds, int limit) = 0;
127  // Get actions to revert <seconds> of history made by <actor>
128  virtual std::list<RollbackAction> getRevertActions(const std::string &actor,
129  time_t seconds) = 0;
130 };
131 
132 
134 {
135 public:
137  const std::string & actor, bool is_guess = false) :
138  rollback(rollback_)
139  {
140  if (rollback) {
143  rollback->setActor(actor, is_guess);
144  }
145  }
147  {
148  if (rollback) {
150  }
151  }
152 
153 private:
155  std::string old_actor;
157 };
Definition: gamedef.h:49
Definition: rollback_interface.h:112
virtual void setActor(const std::string &actor, bool is_guess)=0
virtual std::list< RollbackAction > getRevertActions(const std::string &actor, time_t seconds)=0
virtual std::string getActor()=0
virtual void reportAction(const RollbackAction &action)=0
virtual void flush()=0
virtual std::string getSuspect(v3s16 p, float nearness_shortcut, float min_nearness)=0
virtual ~IRollbackManager()=default
virtual std::list< RollbackAction > getNodeActors(v3s16 pos, int range, time_t seconds, int limit)=0
virtual bool isActorGuess()=0
Definition: inventorymanager.h:106
Definition: map.h:123
Definition: rollback_interface.h:134
RollbackScopeActor(IRollbackManager *rollback_, const std::string &actor, bool is_guess=false)
Definition: rollback_interface.h:136
std::string old_actor
Definition: rollback_interface.h:155
~RollbackScopeActor()
Definition: rollback_interface.h:146
IRollbackManager * rollback
Definition: rollback_interface.h:154
bool old_actor_guess
Definition: rollback_interface.h:156
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
Definition: inventory.h:34
Definition: mapnode.h:118
Definition: rollback_interface.h:55
bool applyRevert(Map *map, InventoryManager *imgr, IGameDef *gamedef) const
Definition: rollback_interface.cpp:124
void setSetNode(v3s16 p_, const RollbackNode &n_old_, const RollbackNode &n_new_)
Definition: rollback_interface.h:78
std::string inventory_location
Definition: rollback_interface.h:70
std::string actor
Definition: rollback_interface.h:63
bool getPosition(v3s16 *dst) const
Definition: rollback_interface.cpp:104
bool inventory_add
Definition: rollback_interface.h:73
RollbackNode n_new
Definition: rollback_interface.h:68
Type
Definition: rollback_interface.h:56
@ TYPE_MODIFY_INVENTORY_STACK
Definition: rollback_interface.h:59
@ TYPE_SET_NODE
Definition: rollback_interface.h:58
@ TYPE_NOTHING
Definition: rollback_interface.h:57
std::string inventory_list
Definition: rollback_interface.h:71
bool isImportant(IGameDef *gamedef) const
Definition: rollback_interface.cpp:83
RollbackNode n_old
Definition: rollback_interface.h:67
u32 inventory_index
Definition: rollback_interface.h:72
RollbackAction()=default
std::string toString() const
Definition: rollback_interface.cpp:53
time_t unix_time
Definition: rollback_interface.h:62
bool actor_is_guess
Definition: rollback_interface.h:64
enum RollbackAction::Type type
v3s16 p
Definition: rollback_interface.h:66
void setModifyInventoryStack(const std::string &inventory_location_, const std::string &inventory_list_, u32 index_, bool add_, const ItemStack &inventory_stack_)
Definition: rollback_interface.h:87
ItemStack inventory_stack
Definition: rollback_interface.h:74
Definition: rollback_interface.h:35
bool operator!=(const RollbackNode &other)
Definition: rollback_interface.h:46
int param2
Definition: rollback_interface.h:38
std::string meta
Definition: rollback_interface.h:39
bool operator==(const RollbackNode &other)
Definition: rollback_interface.h:41
std::string name
Definition: rollback_interface.h:36
RollbackNode()=default
int param1
Definition: rollback_interface.h:37
std::string p(std::string path)
Definition: test_filepath.cpp:59