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