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