summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/lua_api.txt8
-rw-r--r--src/craftdef.cpp90
-rw-r--r--src/craftdef.h4
-rw-r--r--src/script/lua_api/l_craft.cpp76
-rw-r--r--src/script/lua_api/l_craft.h1
5 files changed, 178 insertions, 1 deletions
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 297566068..a59a9c0f2 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -1847,6 +1847,14 @@ Call these functions only at load time!
* `minetest.register_craftitem(name, item definition)`
* `minetest.register_alias(name, convert_to)`
* `minetest.register_craft(recipe)`
+ * Check recipe table syntax for different types below.
+* `minetest.clear_craft(recipe)`
+ * Will erase existing craft based either on output item or on input recipe.
+ * Specify either output or input only. If you specify both, input will be ignored. For input use the same recipe table
+ syntax as for `minetest.register_craft(recipe)`. For output specify only the item, without a quantity.
+ * If no erase candidate could be found, Lua exception will be thrown.
+ * Warning! The type field ("shaped","cooking" or any other) will be ignored if the recipe
+ contains output. Erasing is then done independently from the crafting method.
* `minetest.register_ore(ore definition)`
* `minetest.register_decoration(decoration definition)`
* `minetest.override_item(name, redefinition)`
diff --git a/src/craftdef.cpp b/src/craftdef.cpp
index d3f1edaf9..45d3018a7 100644
--- a/src/craftdef.cpp
+++ b/src/craftdef.cpp
@@ -960,6 +960,96 @@ public:
return recipes;
}
+
+ virtual bool clearCraftRecipesByOutput(const CraftOutput &output, IGameDef *gamedef)
+ {
+ std::map<std::string, std::vector<CraftDefinition*> >::iterator vec_iter =
+ m_output_craft_definitions.find(output.item);
+
+ if (vec_iter == m_output_craft_definitions.end())
+ return false;
+
+ std::vector<CraftDefinition*> &vec = vec_iter->second;
+ for (std::vector<CraftDefinition*>::iterator i = vec.begin();
+ i != vec.end(); ++i) {
+ CraftDefinition *def = *i;
+ // Recipes are not yet hashed at this point
+ std::vector<CraftDefinition*> &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
+ std::vector<CraftDefinition*> new_vec_by_input;
+ /* We will preallocate necessary memory addresses, so we don't need to reallocate them later.
+ This would save us some performance. */
+ new_vec_by_input.reserve(unhashed_inputs_vec.size());
+ for (std::vector<CraftDefinition*>::iterator i2 = unhashed_inputs_vec.begin();
+ i2 != unhashed_inputs_vec.end(); ++i2) {
+ if (def != *i2) {
+ new_vec_by_input.push_back(*i2);
+ }
+ }
+ m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].swap(new_vec_by_input);
+ }
+ m_output_craft_definitions.erase(output.item);
+ return true;
+ }
+
+ virtual bool clearCraftRecipesByInput(CraftMethod craft_method, unsigned int craft_grid_width,
+ const std::vector<std::string> &recipe, IGameDef *gamedef)
+ {
+ bool all_empty = true;
+ for (std::vector<std::string>::size_type i = 0;
+ i < recipe.size(); i++) {
+ if (!recipe[i].empty()) {
+ all_empty = false;
+ break;
+ }
+ }
+ if (all_empty)
+ return false;
+
+ CraftInput input(craft_method, craft_grid_width, craftGetItems(recipe, gamedef));
+ // Recipes are not yet hashed at this point
+ std::vector<CraftDefinition*> &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0];
+ std::vector<CraftDefinition*> new_vec_by_input;
+ bool got_hit = false;
+ for (std::vector<CraftDefinition*>::size_type
+ i = unhashed_inputs_vec.size(); i > 0; i--) {
+ CraftDefinition *def = unhashed_inputs_vec[i - 1];
+ /* If the input doesn't match the recipe definition, this recipe definition later
+ will be added back in source map. */
+ if (!def->check(input, gamedef)) {
+ new_vec_by_input.push_back(def);
+ continue;
+ }
+ CraftOutput output = def->getOutput(input, gamedef);
+ got_hit = true;
+ std::map<std::string, std::vector<CraftDefinition*> >::iterator
+ vec_iter = m_output_craft_definitions.find(output.item);
+ if (vec_iter == m_output_craft_definitions.end())
+ continue;
+ std::vector<CraftDefinition*> &vec = vec_iter->second;
+ std::vector<CraftDefinition*> new_vec_by_output;
+ /* We will preallocate necessary memory addresses, so we don't need
+ to reallocate them later. This would save us some performance. */
+ new_vec_by_output.reserve(vec.size());
+ for (std::vector<CraftDefinition*>::iterator i = vec.begin();
+ i != vec.end(); ++i) {
+ /* If pointers from map by input and output are not same,
+ we will add 'CraftDefinition*' to a new vector. */
+ if (def != *i) {
+ /* Adding dereferenced iterator value (which are
+ 'CraftDefinition' reference) to a new vector. */
+ new_vec_by_output.push_back(*i);
+ }
+ }
+ // Swaps assigned to current key value with new vector for output map.
+ m_output_craft_definitions[output.item].swap(new_vec_by_output);
+ }
+ if (got_hit)
+ // Swaps value with new vector for input map.
+ m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].swap(new_vec_by_input);
+
+ return got_hit;
+ }
+
virtual std::string dump() const
{
std::ostringstream os(std::ios::binary);
diff --git a/src/craftdef.h b/src/craftdef.h
index cebb2d7ae..695ee0c2c 100644
--- a/src/craftdef.h
+++ b/src/craftdef.h
@@ -426,6 +426,10 @@ public:
virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
IGameDef *gamedef, unsigned limit=0) const=0;
+ virtual bool clearCraftRecipesByOutput(const CraftOutput &output, IGameDef *gamedef) = 0;
+ virtual bool clearCraftRecipesByInput(CraftMethod craft_method,
+ unsigned int craft_grid_width, const std::vector<std::string> &recipe, IGameDef *gamedef) = 0;
+
// Print crafting recipes for debugging
virtual std::string dump() const=0;
diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp
index 391a0133d..d135c689f 100644
--- a/src/script/lua_api/l_craft.cpp
+++ b/src/script/lua_api/l_craft.cpp
@@ -34,7 +34,6 @@ struct EnumString ModApiCraft::es_CraftMethod[] =
{0, NULL},
};
-
// helper for register_craft
bool ModApiCraft::readCraftRecipeShaped(lua_State *L, int index,
int &width, std::vector<std::string> &recipe)
@@ -281,6 +280,80 @@ int ModApiCraft::l_register_craft(lua_State *L)
return 0; /* number of results */
}
+// clear_craft({[output=item], [recipe={{item00,item10},{item01,item11}}])
+int ModApiCraft::l_clear_craft(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ luaL_checktype(L, 1, LUA_TTABLE);
+ int table = 1;
+
+ // Get the writable craft definition manager from the server
+ IWritableCraftDefManager *craftdef =
+ getServer(L)->getWritableCraftDefManager();
+
+ std::string output = getstringfield_default(L, table, "output", "");
+ std::string type = getstringfield_default(L, table, "type", "shaped");
+ CraftOutput c_output(output, 0);
+ if (output != "") {
+ if (craftdef->clearCraftRecipesByOutput(c_output, getServer(L)))
+ return 0;
+ else
+ throw LuaError("No craft recipe known for output"
+ " (output=\"" + output + "\")");
+ }
+ std::vector<std::string> recipe;
+ int width = 0;
+ CraftMethod method = CRAFT_METHOD_NORMAL;
+ /*
+ CraftDefinitionShaped
+ */
+ if (type == "shaped") {
+ lua_getfield(L, table, "recipe");
+ if (lua_isnil(L, -1))
+ throw LuaError("Either output or recipe has to be defined");
+ if (!readCraftRecipeShaped(L, -1, width, recipe))
+ throw LuaError("Invalid crafting recipe");
+ }
+ /*
+ CraftDefinitionShapeless
+ */
+ else if (type == "shapeless") {
+ lua_getfield(L, table, "recipe");
+ if (lua_isnil(L, -1))
+ throw LuaError("Either output or recipe has to be defined");
+ if (!readCraftRecipeShapeless(L, -1, recipe))
+ throw LuaError("Invalid crafting recipe");
+ }
+ /*
+ CraftDefinitionCooking
+ */
+ else if (type == "cooking") {
+ method = CRAFT_METHOD_COOKING;
+ std::string rec = getstringfield_default(L, table, "recipe", "");
+ if (rec == "")
+ throw LuaError("Crafting definition (cooking)"
+ " is missing a recipe");
+ recipe.push_back(rec);
+ }
+ /*
+ CraftDefinitionFuel
+ */
+ else if (type == "fuel") {
+ method = CRAFT_METHOD_FUEL;
+ std::string rec = getstringfield_default(L, table, "recipe", "");
+ if (rec == "")
+ throw LuaError("Crafting definition (fuel)"
+ " is missing a recipe");
+ recipe.push_back(rec);
+ } else {
+ throw LuaError("Unknown crafting definition type: \"" + type + "\"");
+ }
+ if (!craftdef->clearCraftRecipesByInput(method, width, recipe, getServer(L)))
+ throw LuaError("No crafting specified for input");
+ lua_pop(L, 1);
+ return 0;
+}
+
// get_craft_result(input)
int ModApiCraft::l_get_craft_result(lua_State *L)
{
@@ -431,4 +504,5 @@ void ModApiCraft::Initialize(lua_State *L, int top)
API_FCT(get_craft_recipe);
API_FCT(get_craft_result);
API_FCT(register_craft);
+ API_FCT(clear_craft);
}
diff --git a/src/script/lua_api/l_craft.h b/src/script/lua_api/l_craft.h
index 548608776..eb2bce706 100644
--- a/src/script/lua_api/l_craft.h
+++ b/src/script/lua_api/l_craft.h
@@ -33,6 +33,7 @@ private:
static int l_get_craft_recipe(lua_State *L);
static int l_get_all_craft_recipes(lua_State *L);
static int l_get_craft_result(lua_State *L);
+ static int l_clear_craft(lua_State *L);
static bool readCraftReplacements(lua_State *L, int index,
CraftReplacements &replacements);
href='#n592'>592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "rollback.h"
#include <fstream>
#include <list>
#include <sstream>
#include "log.h"
#include "mapnode.h"
#include "gamedef.h"
#include "nodedef.h"
#include "util/serialize.h"
#include "util/string.h"
#include "util/numeric.h"
#include "inventorymanager.h" // deserializing InventoryLocations
#include "sqlite3.h"
#include "filesys.h"

#define POINTS_PER_NODE (16.0)

#define SQLRES(f, good) \
	if ((f) != (good)) {\
		throw FileNotGoodException(std::string("RollbackManager: " \
			"SQLite3 error (" __FILE__ ":" TOSTRING(__LINE__) \
			"): ") + sqlite3_errmsg(db)); \
	}
#define SQLOK(f) SQLRES(f, SQLITE_OK)

#define SQLOK_ERRSTREAM(s, m)                             \
	if ((s) != SQLITE_OK) {                               \
		errorstream << "RollbackManager: " << (m) << ": " \
			<< sqlite3_errmsg(db) << std::endl;           \
	}

#define FINALIZE_STATEMENT(statement) \
	SQLOK_ERRSTREAM(sqlite3_finalize(statement), "Failed to finalize " #statement)

class ItemStackRow : public ItemStack {
public:
	ItemStackRow & operator = (const ItemStack & other)
	{
		*static_cast<ItemStack *>(this) = other;
		return *this;
	}

	int id;
};

struct ActionRow {
	int          id;
	int          actor;
	time_t       timestamp;
	int          type;
	std::string  location, list;
	int          index, add;
	ItemStackRow stack;
	int          nodeMeta;
	int          x, y, z;
	int          oldNode;
	int          oldParam1, oldParam2;
	std::string  oldMeta;
	int          newNode;
	int          newParam1, newParam2;
	std::string  newMeta;
	int          guessed;
};


struct Entity {
	int         id;
	std::string name;
};



RollbackManager::RollbackManager(const std::string & world_path,
		IGameDef * gamedef_) :
	gamedef(gamedef_)
{
	verbosestream << "RollbackManager::RollbackManager(" << world_path
		<< ")" << std::endl;

	std::string txt_filename = world_path + DIR_DELIM "rollback.txt";
	std::string migrating_flag = txt_filename + ".migrating";
	database_path = world_path + DIR_DELIM "rollback.sqlite";

	bool created = initDatabase();

	if (fs::PathExists(txt_filename) && (created ||
			fs::PathExists(migrating_flag))) {
		std::ofstream of(migrating_flag.c_str());
		of.close();
		migrate(txt_filename);
		fs::DeleteSingleFileOrEmptyDirectory(migrating_flag);
	}
}


RollbackManager::~RollbackManager()
{
	flush();

	FINALIZE_STATEMENT(stmt_insert);
	FINALIZE_STATEMENT(stmt_replace);
	FINALIZE_STATEMENT(stmt_select);
	FINALIZE_STATEMENT(stmt_select_range);
	FINALIZE_STATEMENT(stmt_select_withActor);
	FINALIZE_STATEMENT(stmt_knownActor_select);
	FINALIZE_STATEMENT(stmt_knownActor_insert);
	FINALIZE_STATEMENT(stmt_knownNode_select);
	FINALIZE_STATEMENT(stmt_knownNode_insert);

	SQLOK_ERRSTREAM(sqlite3_close(db), "Could not close db");
}


void RollbackManager::registerNewActor(const int id, const std::string &name)
{
	Entity actor = {id, name};
	knownActors.push_back(actor);
}


void RollbackManager::registerNewNode(const int id, const std::string &name)
{
	Entity node = {id, name};
	knownNodes.push_back(node);
}


int RollbackManager::getActorId(const std::string &name)
{
	for (std::vector<Entity>::const_iterator iter = knownActors.begin();
			iter != knownActors.end(); ++iter) {
		if (iter->name == name) {
			return iter->id;
		}
	}

	SQLOK(sqlite3_bind_text(stmt_knownActor_insert, 1, name.c_str(), name.size(), NULL));
	SQLRES(sqlite3_step(stmt_knownActor_insert), SQLITE_DONE);
	SQLOK(sqlite3_reset(stmt_knownActor_insert));

	int id = sqlite3_last_insert_rowid(db);
	registerNewActor(id, name);

	return id;
}


int RollbackManager::getNodeId(const std::string &name)
{
	for (std::vector<Entity>::const_iterator iter = knownNodes.begin();
			iter != knownNodes.end(); ++iter) {
		if (iter->name == name) {
			return iter->id;
		}
	}

	SQLOK(sqlite3_bind_text(stmt_knownNode_insert, 1, name.c_str(), name.size(), NULL));
	SQLRES(sqlite3_step(stmt_knownNode_insert), SQLITE_DONE);
	SQLOK(sqlite3_reset(stmt_knownNode_insert));

	int id = sqlite3_last_insert_rowid(db);
	registerNewNode(id, name);

	return id;
}


const char * RollbackManager::getActorName(const int id)
{
	for (std::vector<Entity>::const_iterator iter = knownActors.begin();
			iter != knownActors.end(); ++iter) {
		if (iter->id == id) {
			return iter->name.c_str();
		}
	}

	return "";
}


const char * RollbackManager::getNodeName(const int id)
{
	for (std::vector<Entity>::const_iterator iter = knownNodes.begin();
			iter != knownNodes.end(); ++iter) {
		if (iter->id == id) {
			return iter->name.c_str();
		}
	}

	return "";
}


bool RollbackManager::createTables()
{
	SQLOK(sqlite3_exec(db,
		"CREATE TABLE IF NOT EXISTS `actor` (\n"
		"	`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
		"	`name` TEXT NOT NULL\n"
		");\n"
		"CREATE TABLE IF NOT EXISTS `node` (\n"
		"	`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
		"	`name` TEXT NOT NULL\n"
		");\n"
		"CREATE TABLE IF NOT EXISTS `action` (\n"
		"	`id` INTEGER PRIMARY KEY AUTOINCREMENT,\n"
		"	`actor` INTEGER NOT NULL,\n"
		"	`timestamp` TIMESTAMP NOT NULL,\n"
		"	`type` INTEGER NOT NULL,\n"
		"	`list` TEXT,\n"
		"	`index` INTEGER,\n"
		"	`add` INTEGER,\n"
		"	`stackNode` INTEGER,\n"
		"	`stackQuantity` INTEGER,\n"
		"	`nodeMeta` INTEGER,\n"
		"	`x` INT,\n"
		"	`y` INT,\n"
		"	`z` INT,\n"
		"	`oldNode` INTEGER,\n"
		"	`oldParam1` INTEGER,\n"
		"	`oldParam2` INTEGER,\n"
		"	`oldMeta` TEXT,\n"
		"	`newNode` INTEGER,\n"
		"	`newParam1` INTEGER,\n"
		"	`newParam2` INTEGER,\n"
		"	`newMeta` TEXT,\n"
		"	`guessedActor` INTEGER,\n"
		"	FOREIGN KEY (`actor`) REFERENCES `actor`(`id`),\n"
		"	FOREIGN KEY (`stackNode`) REFERENCES `node`(`id`),\n"
		"	FOREIGN KEY (`oldNode`)   REFERENCES `node`(`id`),\n"
		"	FOREIGN KEY (`newNode`)   REFERENCES `node`(`id`)\n"
		");\n"
		"CREATE INDEX IF NOT EXISTS `actionIndex` ON `action`(`x`,`y`,`z`,`timestamp`,`actor`);\n",
		NULL, NULL, NULL));
	verbosestream << "SQL Rollback: SQLite3 database structure was created" << std::endl;

	return true;
}


bool RollbackManager::initDatabase()
{
	verbosestream << "RollbackManager: Database connection setup" << std::endl;

	bool needs_create = !fs::PathExists(database_path);
	SQLOK(sqlite3_open_v2(database_path.c_str(), &db,
			SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL));

	if (needs_create) {
		createTables();
	}

	SQLOK(sqlite3_prepare_v2(db,
		"INSERT INTO `action` (\n"
		"	`actor`, `timestamp`, `type`,\n"
		"	`list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
		"	`x`, `y`, `z`,\n"
		"	`oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
		"	`newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
		"	`guessedActor`\n"
		") VALUES (\n"
		"	?, ?, ?,\n"
		"	?, ?, ?, ?, ?, ?,\n"
		"	?, ?, ?,\n"
		"	?, ?, ?, ?,\n"
		"	?, ?, ?, ?,\n"
		"	?"
		");",
		-1, &stmt_insert, NULL));

	SQLOK(sqlite3_prepare_v2(db,
		"REPLACE INTO `action` (\n"
		"	`actor`, `timestamp`, `type`,\n"
		"	`list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
		"	`x`, `y`, `z`,\n"
		"	`oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
		"	`newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
		"	`guessedActor`, `id`\n"
		") VALUES (\n"
		"	?, ?, ?,\n"
		"	?, ?, ?, ?, ?, ?,\n"
		"	?, ?, ?,\n"
		"	?, ?, ?, ?,\n"
		"	?, ?, ?, ?,\n"
		"	?, ?\n"
		");",
		-1, &stmt_replace, NULL));

	SQLOK(sqlite3_prepare_v2(db,
		"SELECT\n"
		"	`actor`, `timestamp`, `type`,\n"
		"	`list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
		"	`x`, `y`, `z`,\n"
		"	`oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
		"	`newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
		"	`guessedActor`\n"
		" FROM `action`\n"
		" WHERE `timestamp` >= ?\n"
		" ORDER BY `timestamp` DESC, `id` DESC",
		-1, &stmt_select, NULL));

	SQLOK(sqlite3_prepare_v2(db,
		"SELECT\n"
		"	`actor`, `timestamp`, `type`,\n"
		"	`list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
		"	`x`, `y`, `z`,\n"
		"	`oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
		"	`newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
		"	`guessedActor`\n"
		"FROM `action`\n"
		"WHERE `timestamp` >= ?\n"
		"	AND `x` IS NOT NULL\n"
		"	AND `y` IS NOT NULL\n"
		"	AND `z` IS NOT NULL\n"
		"	AND `x` BETWEEN ? AND ?\n"
		"	AND `y` BETWEEN ? AND ?\n"
		"	AND `z` BETWEEN ? AND ?\n"
		"ORDER BY `timestamp` DESC, `id` DESC\n"
		"LIMIT 0,?",
		-1, &stmt_select_range, NULL));

	SQLOK(sqlite3_prepare_v2(db,
		"SELECT\n"
		"	`actor`, `timestamp`, `type`,\n"
		"	`list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
		"	`x`, `y`, `z`,\n"
		"	`oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
		"	`newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
		"	`guessedActor`\n"
		"FROM `action`\n"
		"WHERE `timestamp` >= ?\n"
		"	AND `actor` = ?\n"
		"ORDER BY `timestamp` DESC, `id` DESC\n",
		-1, &stmt_select_withActor, NULL));

	SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `actor`",
			-1, &stmt_knownActor_select, NULL));

	SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `actor` (`name`) VALUES (?)",
			-1, &stmt_knownActor_insert, NULL));

	SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `node`",
			-1, &stmt_knownNode_select, NULL));

	SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `node` (`name`) VALUES (?)",
			-1, &stmt_knownNode_insert, NULL));

	verbosestream << "SQL prepared statements setup correctly" << std::endl;

	while (sqlite3_step(stmt_knownActor_select) == SQLITE_ROW) {
		registerNewActor(
		        sqlite3_column_int(stmt_knownActor_select, 0),
		        reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownActor_select, 1))
		);
	}
	SQLOK(sqlite3_reset(stmt_knownActor_select));

	while (sqlite3_step(stmt_knownNode_select) == SQLITE_ROW) {
		registerNewNode(
		        sqlite3_column_int(stmt_knownNode_select, 0),
		        reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownNode_select, 1))
		);
	}
	SQLOK(sqlite3_reset(stmt_knownNode_select));

	return needs_create;
}


bool RollbackManager::registerRow(const ActionRow & row)
{
	sqlite3_stmt * stmt_do = (row.id) ? stmt_replace : stmt_insert;

	bool nodeMeta = false;

	SQLOK(sqlite3_bind_int  (stmt_do, 1, row.actor));
	SQLOK(sqlite3_bind_int64(stmt_do, 2, row.timestamp));
	SQLOK(sqlite3_bind_int  (stmt_do, 3, row.type));

	if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
		const std::string & loc = row.location;
		nodeMeta = (loc.substr(0, 9) == "nodemeta:");

		SQLOK(sqlite3_bind_text(stmt_do, 4, row.list.c_str(), row.list.size(), NULL));
		SQLOK(sqlite3_bind_int (stmt_do, 5, row.index));
		SQLOK(sqlite3_bind_int (stmt_do, 6, row.add));
		SQLOK(sqlite3_bind_int (stmt_do, 7, row.stack.id));
		SQLOK(sqlite3_bind_int (stmt_do, 8, row.stack.count));
		SQLOK(sqlite3_bind_int (stmt_do, 9, (int) nodeMeta));

		if (nodeMeta) {
			std::string::size_type p1, p2;
			p1 = loc.find(':') + 1;
			p2 = loc.find(',');
			std::string x = loc.substr(p1, p2 - p1);
			p1 = p2 + 1;
			p2 = loc.find(',', p1);
			std::string y = loc.substr(p1, p2 - p1);
			std::string z = loc.substr(p2 + 1);
			SQLOK(sqlite3_bind_int(stmt_do, 10, atoi(x.c_str())));
			SQLOK(sqlite3_bind_int(stmt_do, 11, atoi(y.c_str())));
			SQLOK(sqlite3_bind_int(stmt_do, 12, atoi(z.c_str())));
		}
	} else {
		SQLOK(sqlite3_bind_null(stmt_do, 4));
		SQLOK(sqlite3_bind_null(stmt_do, 5));
		SQLOK(sqlite3_bind_null(stmt_do, 6));
		SQLOK(sqlite3_bind_null(stmt_do, 7));
		SQLOK(sqlite3_bind_null(stmt_do, 8));
		SQLOK(sqlite3_bind_null(stmt_do, 9));
	}

	if (row.type == RollbackAction::TYPE_SET_NODE) {
		SQLOK(sqlite3_bind_int (stmt_do, 10, row.x));
		SQLOK(sqlite3_bind_int (stmt_do, 11, row.y));