C++ modernize: Pragma once (#6264)
[oweals/minetest.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 #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
34 struct RollbackNode
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() {}
49         RollbackNode(Map *map, v3s16 p, IGameDef *gamedef);
50 };
51
52
53 struct RollbackAction
54 {
55         enum Type{
56                 TYPE_NOTHING,
57                 TYPE_SET_NODE,
58                 TYPE_MODIFY_INVENTORY_STACK,
59         } type;
60
61         time_t unix_time;
62         std::string actor;
63         bool actor_is_guess;
64
65         v3s16 p;
66         RollbackNode n_old;
67         RollbackNode n_new;
68
69         std::string inventory_location;
70         std::string inventory_list;
71         u32 inventory_index;
72         bool inventory_add;
73         ItemStack inventory_stack;
74
75         RollbackAction():
76                 type(TYPE_NOTHING),
77                 unix_time(0),
78                 actor_is_guess(false)
79         {}
80
81         void setSetNode(v3s16 p_, const RollbackNode &n_old_,
82                         const RollbackNode &n_new_)
83         {
84                 type = TYPE_SET_NODE;
85                 p = p_;
86                 n_old = n_old_;
87                 n_new = n_new_;
88         }
89
90         void setModifyInventoryStack(const std::string &inventory_location_,
91                         const std::string &inventory_list_, int index_,
92                         bool add_, const ItemStack &inventory_stack_)
93         {
94                 type = TYPE_MODIFY_INVENTORY_STACK;
95                 inventory_location = inventory_location_;
96                 inventory_list = inventory_list_;
97                 inventory_index = index_;
98                 inventory_add = add_;
99                 inventory_stack = inventory_stack_;
100         }
101
102         // String should not contain newlines or nulls
103         std::string toString() const;
104
105         // Eg. flowing water level changes are not important
106         bool isImportant(IGameDef *gamedef) const;
107
108         bool getPosition(v3s16 *dst) const;
109
110         bool applyRevert(Map *map, InventoryManager *imgr, IGameDef *gamedef) const;
111 };
112
113
114 class IRollbackManager
115 {
116 public:
117         virtual void reportAction(const RollbackAction &action) = 0;
118         virtual std::string getActor() = 0;
119         virtual bool isActorGuess() = 0;
120         virtual void setActor(const std::string &actor, bool is_guess) = 0;
121         virtual std::string getSuspect(v3s16 p, float nearness_shortcut,
122                                        float min_nearness) = 0;
123
124         virtual ~IRollbackManager() {};
125         virtual void flush() = 0;
126         // Get all actors that did something to position p, but not further than
127         // <seconds> in history
128         virtual std::list<RollbackAction> getNodeActors(v3s16 pos, int range,
129                         time_t seconds, int limit) = 0;
130         // Get actions to revert <seconds> of history made by <actor>
131         virtual std::list<RollbackAction> getRevertActions(const std::string &actor,
132                         time_t seconds) = 0;
133 };
134
135
136 class RollbackScopeActor
137 {
138 public:
139         RollbackScopeActor(IRollbackManager * rollback_,
140                         const std::string & actor, bool is_guess = false) :
141                 rollback(rollback_)
142         {
143                 if (rollback) {
144                         old_actor = rollback->getActor();
145                         old_actor_guess = rollback->isActorGuess();
146                         rollback->setActor(actor, is_guess);
147                 }
148         }
149         ~RollbackScopeActor()
150         {
151                 if (rollback) {
152                         rollback->setActor(old_actor, old_actor_guess);
153                 }
154         }
155
156 private:
157         IRollbackManager * rollback;
158         std::string old_actor;
159         bool old_actor_guess;
160 };