Tweak rollback stuff
[oweals/minetest.git] / src / rollback_interface.h
1 /*
2 Minetest-c55
3 Copyright (C) 2012 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 "exceptions.h"
27
28 class Map;
29 class IGameDef;
30 struct MapNode;
31 class InventoryManager;
32
33 struct RollbackNode
34 {
35         std::string name;
36         int param1;
37         int param2;
38         std::string meta;
39
40         bool operator==(const RollbackNode &other)
41         {
42                 return (name == other.name && param1 == other.param1 &&
43                                 param2 == other.param2 && meta == other.meta);
44         }
45         bool operator!=(const RollbackNode &other)
46         {
47                 return !(*this == other);
48         }
49
50         RollbackNode():
51                 param1(0),
52                 param2(0)
53         {}
54
55         RollbackNode(Map *map, v3s16 p, IGameDef *gamedef);
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         int 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         std::string 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 std::string &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         void fromStream(std::istream &is) throw(SerializationError);
110         
111         // Eg. flowing water level changes are not important
112         bool isImportant(IGameDef *gamedef) const;
113         
114         bool getPosition(v3s16 *dst) const;
115
116         bool applyRevert(Map *map, InventoryManager *imgr, IGameDef *gamedef) const;
117 };
118
119 class IRollbackReportSink
120 {
121 public:
122         virtual ~IRollbackReportSink(){}
123         virtual void reportAction(const RollbackAction &action) = 0;
124         virtual std::string getActor() = 0;
125         virtual bool isActorGuess() = 0;
126         virtual void setActor(const std::string &actor, bool is_guess) = 0;
127         // nearness_shortcut: 100 = same second, same node; 90 = 10s or 10 nodes
128         virtual std::string getSuspect(v3s16 p, int max_time, float nearness_shortcut=98) = 0;
129 };
130
131 class RollbackScopeActor
132 {
133 public:
134         RollbackScopeActor(IRollbackReportSink *sink, const std::string &actor,
135                         bool is_guess=false):
136                 m_sink(sink)
137         {
138                 if(m_sink){
139                         m_actor_was = m_sink->getActor();
140                         m_actor_was_guess = m_sink->isActorGuess();
141                         m_sink->setActor(actor, is_guess);
142                 }
143         }
144         ~RollbackScopeActor()
145         {
146                 if(m_sink){
147                         m_sink->setActor(m_actor_was, m_actor_was_guess);
148                 }
149         }
150 private:
151         IRollbackReportSink *m_sink;
152         std::string m_actor_was;
153         bool m_actor_was_guess;
154 };
155
156 #endif