]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_rollback.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.git] / src / script / lua_api / l_rollback.cpp
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 #include "lua_api/l_rollback.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_converter.h"
23 #include "server.h"
24 #include "rollback.h"
25
26
27 // rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
28 int ModApiRollback::l_rollback_get_last_node_actor(lua_State *L)
29 {
30         v3s16 p = read_v3s16(L, 1);
31         int range = luaL_checknumber(L, 2);
32         int seconds = luaL_checknumber(L, 3);
33         Server *server = getServer(L);
34         IRollbackManager *rollback = server->getRollbackManager();
35         v3s16 act_p;
36         int act_seconds = 0;
37         std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds);
38         lua_pushstring(L, actor.c_str());
39         push_v3s16(L, act_p);
40         lua_pushnumber(L, act_seconds);
41         return 3;
42 }
43
44 // rollback_revert_actions_by(actor, seconds) -> bool, log messages
45 int ModApiRollback::l_rollback_revert_actions_by(lua_State *L)
46 {
47         std::string actor = luaL_checkstring(L, 1);
48         int seconds = luaL_checknumber(L, 2);
49         Server *server = getServer(L);
50         IRollbackManager *rollback = server->getRollbackManager();
51         std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
52         std::list<std::string> log;
53         bool success = server->rollbackRevertActions(actions, &log);
54         // Push boolean result
55         lua_pushboolean(L, success);
56         // Get the table insert function and push the log table
57         lua_getglobal(L, "table");
58         lua_getfield(L, -1, "insert");
59         int table_insert = lua_gettop(L);
60         lua_newtable(L);
61         int table = lua_gettop(L);
62         for(std::list<std::string>::const_iterator i = log.begin();
63                         i != log.end(); i++)
64         {
65                 lua_pushvalue(L, table_insert);
66                 lua_pushvalue(L, table);
67                 lua_pushstring(L, i->c_str());
68                 if(lua_pcall(L, 2, 0, 0))
69                         script_error(L, "error: %s", lua_tostring(L, -1));
70         }
71         lua_remove(L, -2); // Remove table
72         lua_remove(L, -2); // Remove insert
73         return 2;
74 }
75
76 void ModApiRollback::Initialize(lua_State *L, int top)
77 {
78         API_FCT(rollback_get_last_node_actor);
79         API_FCT(rollback_revert_actions_by);
80 }