aboutsummaryrefslogtreecommitdiff
path: root/src/defaultsettings.cpp
blob: 3315f4aaa7766f18d1963b6b69883cd7d55e3569 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
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 "settings.h"
#include "porting.h"
#include "filesys.h"
#include "config.h"
#include "constants.h"
#include "porting.h"

void set_default_settings(Settings *settings)
{
	// Client and server

	settings->setDefault("name", "");

	// Client stuff
	settings->setDefault("remote_port", "30000");
	settings->setDefault("keymap_forward", "KEY_KEY_W");
	settings->setDefault("keymap_autorun", "");
	settings->setDefault("keymap_backward", "KEY_KEY_S");
	settings->setDefault("keymap_left", "KEY_KEY_A");
	settings->setDefault("keymap_right", "KEY_KEY_D");
	settings->setDefault("keymap_jump", "KEY_SPACE");
	settings->setDefault("keymap_sneak", "KEY_LSHIFT");
	settings->setDefault("keymap_drop", "KEY_KEY_Q");
	settings->setDefault("keymap_inventory", "KEY_KEY_I");
	settings->setDefault("keymap_special1", "KEY_KEY_E");
	settings->setDefault("keymap_chat", "KEY_KEY_T");
	settings->setDefault("keymap_cmd", "/");
	settings->setDefault("keymap_minimap", "KEY_F9");
	settings->setDefault("keymap_console", "KEY_F10");
	settings->setDefault("keymap_rangeselect", "KEY_KEY_R");
	settings->setDefault("keymap_freemove", "KEY_KEY_K");
	settings->setDefault("keymap_fastmove", "KEY_KEY_J");
	settings->setDefault("keymap_noclip", "KEY_KEY_H");
	settings->setDefault("keymap_cinematic", "KEY_F8");
	settings->setDefault("keymap_screenshot", "KEY_F12");
	settings->setDefault("keymap_toggle_hud", "KEY_F1");
	settings->setDefault("keymap_toggle_chat", "KEY_F2");
	settings->setDefault("keymap_toggle_force_fog_off", "KEY_F3");
	settings->setDefault("keymap_toggle_update_camera",
#if DEBUG
			"KEY_F4");
#else
			"");
#endif
	settings->setDefault("keymap_toggle_debug", "KEY_F5");
	settings->setDefault("keymap_toggle_profiler", "KEY_F6");
	settings->setDefault("keymap_camera_mode", "KEY_F7");
	settings->setDefault("keymap_increase_viewing_range_min", "+");
	settings->setDefault("keymap_decrease_viewing_range_min", "-");
	settings->setDefault("enable_build_where_you_stand", "false" );
	settings->setDefault("3d_mode", "none");
	settings->setDefault("3d_paralax_strength", "0.025");
	settings->setDefault("aux1_descends", "false");
	settings->setDefault("doubletap_jump", "false");
	settings->setDefault("always_fly_fast", "true");
	settings->setDefault("directional_colored_fog", "true");
	settings->setDefault("tooltip_show_delay", "400");

	// Some (temporary) keys for debugging
	settings->setDefault("keymap_print_debug_stacks", "KEY_KEY_P");
	settings->setDefault("keymap_quicktune_prev", "KEY_HOME");
	settings->setDefault("keymap_quicktune_next", "KEY_END");
	settings->setDefault("keymap_quicktune_dec", "KEY_NEXT");
	settings->setDefault("keymap_quicktune_inc", "KEY_PRIOR");

	// Show debug info by default?
	#ifdef NDEBUG
	settings->setDefault("show_debug", "false");
	#else
	settings->setDefault("show_debug", "true");
	#endif

	settings->setDefault("wanted_fps", "30");
	settings->setDefault("fps_max", "60");
	settings->setDefault("pause_fps_max", "20");
	// A bit more than the server will send around the player, to make fog blend well
	settings->setDefault("viewing_range_nodes_max", "240");
	settings->setDefault("viewing_range_nodes_min", "35");
	settings->setDefault("map_generation_limit", "31000");
	settings->setDefault("screenW", "800");
	settings->setDefault("screenH", "600");
	settings->setDefault("fullscreen", "false");
	settings->setDefault("fullscreen_bpp", "24");
	settings->setDefault("fsaa", "0");
	settings->setDefault("vsync", "false");
	settings->setDefault("address", "");
	settings->setDefault("random_input", "false");
	settings->setDefault("client_unload_unused_data_timeout", "600");
	settings->setDefault("client_mapblock_limit", "5000");
	settings->setDefault("enable_fog", "true");
	settings->setDefault("fov", "72");
	settings->setDefault("view_bobbing", "true");
	settings->setDefault("new_style_water", "false");
	settings->setDefault("leaves_style", "fancy");
	settings->setDefault("connected_glass", "false");
	settings->setDefault("smooth_lighting", "true");
	settings->setDefault("display_gamma", "1.8");
	settings->setDefault("texture_path", "");
	settings->setDefault("shader_path", "");
	settings->setDefault("video_driver", "opengl");
	settings->setDefault("free_move", "false");
	settings->setDefault("noclip", "false");
	settings->setDefault("continuous_forward", "false");
	settings->setDefault("cinematic", "false");
	settings->setDefault("camera_smoothing", "0");
	settings->setDefault("cinematic_camera_smoothing", "0.7");
	settings->setDefault("fast_move", "false");
	settings->setDefault("invert_mouse", "false");
	settings->setDefault("enable_clouds", "true");
	settings->setDefault("screenshot_path", ".");
	settings->setDefault("view_bobbing_amount", "1.0");
	settings->setDefault("fall_bobbing_amount", "0.0");
	settings->setDefault("enable_3d_clouds", "true");
	settings->setDefault("cloud_height", "120");
	settings->setDefault("cloud_radius", "12");
	settings->setDefault("menu_clouds", "true");
	settings->setDefault("opaque_water", "false");
	settings->setDefault("console_color", "(0,0,0)");
	settings->setDefault("console_alpha", "200");
	settings->setDefault("selectionbox_color", "(0,0,0)");
	settings->setDefault("enable_node_highlighting", "false");
	settings->setDefault("crosshair_color", "(255,255,255)");
	settings->setDefault("crosshair_alpha", "255");
	settings->setDefault("hud_scaling", "1.0");
	settings->setDefault("gui_scaling", "1.0");
	settings->setDefault("gui_scaling_filter", "false");
	settings->setDefault("gui_scaling_filter_txr2img", "true");
	settings->setDefault("mouse_sensitivity", "0.2");
	settings->setDefault("enable_sound", "true");
	settings->setDefault("sound_volume", "0.8");
	settings->setDefault("desynchronize_mapblock_texture_animation", "true");
	settings->setDefault("selectionbox_width","2");
	settings->setDefault("hud_hotbar_max_width","1.0");
	settings->setDefault("enable_local_map_saving", "false");

	settings->setDefault("mip_map", "false");
	settings->setDefault("anisotropic_filter", "false");
	settings->setDefault("bilinear_filter", "false");
	settings->setDefault("trilinear_filter", "false");
	settings->setDefault("texture_clean_transparent", "false");
	settings->setDefault("texture_min_size", "64");
	settings->setDefault("preload_item_visuals", "false");
	settings->setDefault("enable_bumpmapping", "false");
	settings->setDefault("enable_parallax_occlusion", "false");
	settings->setDefault("generate_normalmaps", "false");
	settings->setDefault("normalmaps_strength", "0.6");
	settings->setDefault("normalmaps_smooth", "1");
	settings->setDefault("parallax_occlusion_mode", "1");
	settings->setDefault("parallax_occlusion_iterations", "4");
	settings->setDefault("parallax_occlusion_scale", "0.08");
	settings->setDefault("parallax_occlusion_bias", "0.04");
	settings->setDefault("enable_waving_water", "false");
	settings->setDefault("water_wave_height", "1.0");
	settings->setDefault("water_wave_length", "20.0");
	settings->setDefault("water_wave_speed", "5.0");
	settings->setDefault("enable_waving_leaves", "false");
	settings->setDefault("enable_waving_plants", "false");
	settings->setDefault("ambient_occlusion_gamma", "2.2");
	settings->setDefault("enable_shaders", "true");
	settings->setDefault("repeat_rightclick_time", "0.25");
	settings->setDefault("enable_particles", "true");
	settings->setDefault("enable_mesh_cache", "false");

	settings->setDefault("enable_minimap", "true");
	settings->setDefault("minimap_shape_round", "true");
	settings->setDefault("minimap_double_scan_height", "true");

	settings->setDefault("curl_timeout", "5000");
	settings->setDefault("curl_parallel_limit", "8");
	settings->setDefault("curl_file_download_timeout", "300000");
	settings->setDefault("curl_verify_cert", "true");

	settings->setDefault("enable_remote_media_server", "true");

	settings->setDefault("serverlist_url", "servers.minetest.net");
	settings->setDefault("serverlist_file", "favoriteservers.txt");
	settings->setDefault("server_announce", "false");
	settings->setDefault("server_url", "");
	settings->setDefault("server_address", "");
	settings->setDefault("server_name", "");
	settings->setDefault("server_description", "");

#if USE_FREETYPE
	settings->setDefault("freetype", "true");
	settings->setDefault("font_path", porting::getDataPath("fonts" DIR_DELIM "liberationsans.ttf"));
	settings->setDefault("font_shadow", "1");
	settings->setDefault("font_shadow_alpha", "128");
	settings->setDefault("mono_font_path", porting::getDataPath("fonts" DIR_DELIM "liberationmono.ttf"));
	settings->setDefault("fallback_font_path", porting::getDataPath("fonts" DIR_DELIM "DroidSansFallbackFull.ttf"));

	settings->setDefault("fallback_font_shadow", "1");
	settings->setDefault("fallback_font_shadow_alpha", "128");

	std::stringstream fontsize;
	fontsize << TTF_DEFAULT_FONT_SIZE;

	settings->setDefault("font_size", fontsize.str());
	settings->setDefault("mono_font_size", fontsize.str());
	settings->setDefault("fallback_font_size", fontsize.str());
#else
	settings->setDefault("freetype", "false");
	settings->setDefault("font_path", porting::getDataPath("fonts" DIR_DELIM "lucida_sans"));
	settings->setDefault("mono_font_path", porting::getDataPath("fonts" DIR_DELIM "mono_dejavu_sans"));

	std::stringstream fontsize;
	fontsize << DEFAULT_FONT_SIZE;

	settings->setDefault("font_size", fontsize.str());
	settings->setDefault("mono_font_size", fontsize.str());
#endif

	// Server stuff
	// "map-dir" doesn't exist by default.
	settings->setDefault("workaround_window_size","5");
	settings->setDefault("max_packets_per_iteration","1024");
	settings->setDefault("port", "30000");
	settings->setDefault("bind_address", "");
	settings->setDefault("default_game", "minetest");
	settings->setDefault("motd", "");
	settings->setDefault("max_users", "15");
	settings->setDefault("strict_protocol_version_checking", "false");
	settings->setDefault("creative_mode", "false");
	settings->setDefault("enable_damage", "true");
	settings->setDefault("fixed_map_seed", "");
	settings->setDefault("give_initial_stuff", "false");
	settings->setDefault("default_password", "");
	settings->setDefault("default_privs", "interact, shout");
	settings->setDefault("player_transfer_distance", "0");
	settings->setDefault("enable_pvp", "true");
	settings->setDefault("vertical_spawn_range", "16");
	settings->setDefault("disallow_empty_password", "false");
	settings->setDefault("disable_anticheat", "false");
	settings->setDefault("enable_rollback_recording", "false");
#ifdef NDEBUG
	settings->setDefault("deprecated_lua_api_handling", "legacy");
#else
	settings->setDefault("deprecated_lua_api_handling", "log");
#endif

	settings->setDefault("kick_msg_shutdown", "Server shutting down.");
	settings->setDefault("kick_msg_crash", "This server has experienced an internal error. You will now be disconnected.");
	settings->setDefault("ask_reconnect_on_crash", "false");

	settings->setDefault("profiler_print_interval", "0");
	settings->setDefault("enable_mapgen_debug_info", "false");
	settings->setDefault("active_object_send_range_blocks", "3");
	settings->setDefault("active_block_range", "2");
	//settings->setDefault("max_simultaneous_block_sends_per_client", "1");
	// This causes frametime jitter on client side, or does it?
	settings->setDefault("max_simultaneous_block_sends_per_client", "10");
	settings->setDefault("max_simultaneous_block_sends_server_total", "40");
	settings->setDefault("max_block_send_distance", "9");
	settings->setDefault("max_block_generate_distance", "7");
	settings->setDefault("max_clearobjects_extra_loaded_blocks", "4096");
	settings->setDefault("time_send_interval", "5");
	settings->setDefault("time_speed", "72");
	settings->setDefault("server_unload_unused_data_timeout", "29");
	settings->setDefault("max_objects_per_block", "49");
	settings->setDefault("server_map_save_interval", "5.3");
	settings->setDefault("sqlite_synchronous", "2");
	settings->setDefault("full_block_send_enable_min_time_from_building", "2.0");
	settings->setDefault("dedicated_server_step", "0.1");
	settings->setDefault("ignore_world_load_errors", "false");
	settings->setDefault("remote_media", "");
	settings->setDefault("debug_log_level", "action");
	settings->setDefault("emergequeue_limit_total", "256");
	settings->setDefault("emergequeue_limit_diskonly", "32");
	settings->setDefault("emergequeue_limit_generate", "32");
	settings->setDefault("num_emerge_threads", "1");
	settings->setDefault("secure.enable_security", "false");
	settings->setDefault("secure.trusted_mods", "");

	// physics stuff
	settings->setDefault("movement_acceleration_default", "3");
	settings->setDefault("movement_acceleration_air", "2");
	settings->setDefault("movement_acceleration_fast", "10");
	settings->setDefault("movement_speed_walk", "4");
	settings->setDefault("movement_speed_crouch", "1.35");
	settings->setDefault("movement_speed_fast", "20");
	settings->setDefault("movement_speed_climb", "2");
	settings->setDefault("movement_speed_jump", "6.5");
	settings->setDefault("movement_liquid_fluidity", "1");
	settings->setDefault("movement_liquid_fluidity_smooth", "0.5");
	settings->setDefault("movement_liquid_sink", "10");
	settings->setDefault("movement_gravity", "9.81");

	//liquid stuff
	settings->setDefault("liquid_loop_max", "100000");
	settings->setDefault("liquid_queue_purge_time", "0");
	settings->setDefault("liquid_update", "1.0");

	//mapgen stuff
	settings->setDefault("mg_name", "v6");
	settings->setDefault("water_level", "1");
	settings->setDefault("chunksize", "5");
	settings->setDefault("mg_flags", "dungeons");
	settings->setDefault("mgv6_spflags", "jungles, snowbiomes");

	// IPv6
	settings->setDefault("enable_ipv6", "true");
	settings->setDefault("ipv6_server", "false");

	settings->setDefault("main_menu_path", "");
	settings->setDefault("main_menu_mod_mgr", "1");
	settings->setDefault("main_menu_game_mgr", "0");
	settings->setDefault("modstore_download_url", "https://forum.minetest.net/media/");
	settings->setDefault("modstore_listmods_url", "https://forum.minetest.net/mmdb/mods/");
	settings->setDefault("modstore_details_url", "https://forum.minetest.net/mmdb/mod/*/");

	settings->setDefault("high_precision_fpu", "true");

	settings->setDefault("language", "");

#ifdef __ANDROID__
	settings->setDefault("screenW", "0");
	settings->setDefault("screenH", "0");
	settings->setDefault("enable_shaders", "false");
	settings->setDefault("fullscreen", "true");
	settings->setDefault("enable_particles", "false");
	settings->setDefault("video_driver", "ogles1");
	settings->setDefault("touchtarget", "true");
	settings->setDefault("TMPFolder","/sdcard/" PROJECT_NAME_C "/tmp/");
	settings->setDefault("touchscreen_threshold","20");
	settings->setDefault("smooth_lighting", "false");
	settings->setDefault("max_simultaneous_block_sends_per_client", "3");
	settings->setDefault("emergequeue_limit_diskonly", "8");
	settings->setDefault("emergequeue_limit_generate", "8");
	settings->setDefault("preload_item_visuals", "false");

	settings->setDefault("viewing_range_nodes_max", "50");
	settings->setDefault("viewing_range_nodes_min", "20");
	settings->setDefault("inventory_image_hack", "false");

	//check for device with small screen
	float x_inches = ((double) porting::getDisplaySize().X /
			(160 * porting::getDisplayDensity()));
	if (x_inches  < 3.5) {
		settings->setDefault("hud_scaling", "0.6");
	}
	else if (x_inches < 4.5) {
		settings->setDefault("hud_scaling", "0.7");
	}
	settings->setDefault("curl_verify_cert","false");
#else
	settings->setDefault("screen_dpi", "72");
#endif
}

void override_default_settings(Settings *settings, Settings *from)
{
	std::vector<std::string> names = from->getNames();
	for(size_t i=0; i<names.size(); i++){
		const std::string &name = names[i];
		settings->setDefault(name, from->get(name));
	}
}
an class="hl str">", replacements="<<replacements.dump()<<")"; return os.str(); } void CraftDefinitionShaped::serializeBody(std::ostream &os) const { os<<serializeString(output); writeU16(os, width); writeU16(os, recipe.size()); for(u32 i=0; i<recipe.size(); i++) os<<serializeString(recipe[i]); replacements.serialize(os); } void CraftDefinitionShaped::deSerializeBody(std::istream &is, int version) { if(version != 1) throw SerializationError( "unsupported CraftDefinitionShaped version"); output = deSerializeString(is); width = readU16(is); recipe.clear(); u32 count = readU16(is); for(u32 i=0; i<count; i++) recipe.push_back(deSerializeString(is)); replacements.deSerialize(is); } /* CraftDefinitionShapeless */ std::string CraftDefinitionShapeless::getName() const { return "shapeless"; } bool CraftDefinitionShapeless::check(const CraftInput &input, IGameDef *gamedef) const { if(input.method != CRAFT_METHOD_NORMAL) return false; // Filter empty items out of input std::vector<std::string> input_filtered; for(std::vector<ItemStack>::const_iterator i = input.items.begin(); i != input.items.end(); i++) { if(i->name != "") input_filtered.push_back(i->name); } // If there is a wrong number of items in input, no match if(input_filtered.size() != recipe.size()){ /*dstream<<"Number of input items ("<<input_filtered.size() <<") does not match recipe size ("<<recipe.size()<<") " <<"of recipe with output="<<output<<std::endl;*/ return false; } // Try with all permutations of the recipe std::vector<std::string> recipe_copy = craftGetItemNames(recipe, gamedef); // Start from the lexicographically first permutation (=sorted) std::sort(recipe_copy.begin(), recipe_copy.end()); //while(std::prev_permutation(recipe_copy.begin(), recipe_copy.end())){} do{ // If all items match, the recipe matches bool all_match = true; //dstream<<"Testing recipe (output="<<output<<"):"; for(size_t i=0; i<recipe.size(); i++){ //dstream<<" ("<<input_filtered[i]<<" == "<<recipe_copy[i]<<")"; if(!inputItemMatchesRecipe(input_filtered[i], recipe_copy[i], gamedef->idef())){ all_match = false; break; } } //dstream<<" -> match="<<all_match<<std::endl; if(all_match) return true; }while(std::next_permutation(recipe_copy.begin(), recipe_copy.end())); return false; } CraftOutput CraftDefinitionShapeless::getOutput(const CraftInput &input, IGameDef *gamedef) const { return CraftOutput(output, 0); } CraftInput CraftDefinitionShapeless::getInput(const CraftOutput &output, IGameDef *gamedef) const { return CraftInput(CRAFT_METHOD_NORMAL,0,craftGetItems(recipe,gamedef)); } void CraftDefinitionShapeless::decrementInput(CraftInput &input, IGameDef *gamedef) const { craftDecrementOrReplaceInput(input, replacements, gamedef); } std::string CraftDefinitionShapeless::dump() const { std::ostringstream os(std::ios::binary); os<<"(shapeless, output=\""<<output <<"\", recipe="<<craftDumpMatrix(recipe, recipe.size()) <<", replacements="<<replacements.dump()<<")"; return os.str(); } void CraftDefinitionShapeless::serializeBody(std::ostream &os) const { os<<serializeString(output); writeU16(os, recipe.size()); for(u32 i=0; i<recipe.size(); i++) os<<serializeString(recipe[i]); replacements.serialize(os); } void CraftDefinitionShapeless::deSerializeBody(std::istream &is, int version) { if(version != 1) throw SerializationError( "unsupported CraftDefinitionShapeless version"); output = deSerializeString(is); recipe.clear(); u32 count = readU16(is); for(u32 i=0; i<count; i++) recipe.push_back(deSerializeString(is)); replacements.deSerialize(is); } /* CraftDefinitionToolRepair */ static ItemStack craftToolRepair( const ItemStack &item1, const ItemStack &item2, float additional_wear, IGameDef *gamedef) { IItemDefManager *idef = gamedef->idef(); if(item1.count != 1 || item2.count != 1 || item1.name != item2.name || idef->get(item1.name).type != ITEM_TOOL || idef->get(item2.name).type != ITEM_TOOL) { // Failure return ItemStack(); } s32 item1_uses = 65536 - (u32) item1.wear; s32 item2_uses = 65536 - (u32) item2.wear; s32 new_uses = item1_uses + item2_uses; s32 new_wear = 65536 - new_uses + floor(additional_wear * 65536 + 0.5); if(new_wear >= 65536) return ItemStack(); if(new_wear < 0) new_wear = 0; ItemStack repaired = item1; repaired.wear = new_wear; return repaired; } std::string CraftDefinitionToolRepair::getName() const { return "toolrepair"; } bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef) const { if(input.method != CRAFT_METHOD_NORMAL) return false; ItemStack item1; ItemStack item2; for(std::vector<ItemStack>::const_iterator i = input.items.begin(); i != input.items.end(); i++) { if(!i->empty()) { if(item1.empty()) item1 = *i; else if(item2.empty()) item2 = *i; else return false; } } ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef); return !repaired.empty(); } CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameDef *gamedef) const { ItemStack item1; ItemStack item2; for(std::vector<ItemStack>::const_iterator i = input.items.begin(); i != input.items.end(); i++) { if(!i->empty()) { if(item1.empty()) item1 = *i; else if(item2.empty()) item2 = *i; } } ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef); return CraftOutput(repaired.getItemString(), 0); } CraftInput CraftDefinitionToolRepair::getInput(const CraftOutput &output, IGameDef *gamedef) const { std::vector<ItemStack> stack; stack.push_back(ItemStack()); return CraftInput(CRAFT_METHOD_COOKING,additional_wear,stack); } void CraftDefinitionToolRepair::decrementInput(CraftInput &input, IGameDef *gamedef) const { craftDecrementInput(input, gamedef); } std::string CraftDefinitionToolRepair::dump() const { std::ostringstream os(std::ios::binary); os<<"(toolrepair, additional_wear="<<additional_wear<<")"; return os.str(); } void CraftDefinitionToolRepair::serializeBody(std::ostream &os) const { writeF1000(os, additional_wear); } void CraftDefinitionToolRepair::deSerializeBody(std::istream &is, int version) { if(version != 1) throw SerializationError( "unsupported CraftDefinitionToolRepair version"); additional_wear = readF1000(is); } /* CraftDefinitionCooking */ std::string CraftDefinitionCooking::getName() const { return "cooking"; } bool CraftDefinitionCooking::check(const CraftInput &input, IGameDef *gamedef) const { if(input.method != CRAFT_METHOD_COOKING) return false; // Filter empty items out of input std::vector<std::string> input_filtered; for(std::vector<ItemStack>::const_iterator i = input.items.begin(); i != input.items.end(); i++) { if(i->name != "") input_filtered.push_back(i->name); } // If there is a wrong number of items in input, no match if(input_filtered.size() != 1){ /*dstream<<"Number of input items ("<<input_filtered.size() <<") does not match recipe size (1) " <<"of cooking recipe with output="<<output<<std::endl;*/ return false; } // Check the single input item return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef()); } CraftOutput CraftDefinitionCooking::getOutput(const CraftInput &input, IGameDef *gamedef) const { return CraftOutput(output, cooktime); } CraftInput CraftDefinitionCooking::getInput(const CraftOutput &output, IGameDef *gamedef) const { std::vector<std::string> rec; rec.push_back(recipe); return CraftInput(CRAFT_METHOD_COOKING,cooktime,craftGetItems(rec,gamedef)); } void CraftDefinitionCooking::decrementInput(CraftInput &input, IGameDef *gamedef) const { craftDecrementOrReplaceInput(input, replacements, gamedef); } std::string CraftDefinitionCooking::dump() const { std::ostringstream os(std::ios::binary); os<<"(cooking, output=\""<<output <<"\", recipe=\""<<recipe <<"\", cooktime="<<cooktime<<")" <<", replacements="<<replacements.dump()<<")"; return os.str(); } void CraftDefinitionCooking::serializeBody(std::ostream &os) const { os<<serializeString(output); os<<serializeString(recipe); writeF1000(os, cooktime); replacements.serialize(os); } void CraftDefinitionCooking::deSerializeBody(std::istream &is, int version) { if(version != 1) throw SerializationError( "unsupported CraftDefinitionCooking version"); output = deSerializeString(is); recipe = deSerializeString(is); cooktime = readF1000(is); replacements.deSerialize(is); } /* CraftDefinitionFuel */ std::string CraftDefinitionFuel::getName() const { return "fuel"; } bool CraftDefinitionFuel::check(const CraftInput &input, IGameDef *gamedef) const { if(input.method != CRAFT_METHOD_FUEL) return false; // Filter empty items out of input std::vector<std::string> input_filtered; for(std::vector<ItemStack>::const_iterator i = input.items.begin(); i != input.items.end(); i++) { if(i->name != "") input_filtered.push_back(i->name); } // If there is a wrong number of items in input, no match if(input_filtered.size() != 1){ /*dstream<<"Number of input items ("<<input_filtered.size() <<") does not match recipe size (1) " <<"of fuel recipe with burntime="<<burntime<<std::endl;*/ return false; } // Check the single input item return inputItemMatchesRecipe(input_filtered[0], recipe, gamedef->idef()); } CraftOutput CraftDefinitionFuel::getOutput(const CraftInput &input, IGameDef *gamedef) const { return CraftOutput("", burntime); } CraftInput CraftDefinitionFuel::getInput(const CraftOutput &output, IGameDef *gamedef) const { std::vector<std::string> rec; rec.push_back(recipe); return CraftInput(CRAFT_METHOD_COOKING,(int)burntime,craftGetItems(rec,gamedef)); } void CraftDefinitionFuel::decrementInput(CraftInput &input, IGameDef *gamedef) const { craftDecrementOrReplaceInput(input, replacements, gamedef); } std::string CraftDefinitionFuel::dump() const { std::ostringstream os(std::ios::binary); os<<"(fuel, recipe=\""<<recipe <<"\", burntime="<<burntime<<")" <<", replacements="<<replacements.dump()<<")"; return os.str(); } void CraftDefinitionFuel::serializeBody(std::ostream &os) const { os<<serializeString(recipe); writeF1000(os, burntime); replacements.serialize(os); } void CraftDefinitionFuel::deSerializeBody(std::istream &is, int version) { if(version != 1) throw SerializationError( "unsupported CraftDefinitionFuel version"); recipe = deSerializeString(is); burntime = readF1000(is); replacements.deSerialize(is); } /* Craft definition manager */ class CCraftDefManager: public IWritableCraftDefManager { public: virtual ~CCraftDefManager() { clear(); } virtual bool getCraftResult(CraftInput &input, CraftOutput &output, bool decrementInput, IGameDef *gamedef) const { output.item = ""; output.time = 0; // If all input items are empty, abort. bool all_empty = true; for(std::vector<ItemStack>::const_iterator i = input.items.begin(); i != input.items.end(); i++) { if(!i->empty()) { all_empty = false; break; } } if(all_empty) return false; // Walk crafting definitions from back to front, so that later // definitions can override earlier ones. for(std::vector<CraftDefinition*>::const_reverse_iterator i = m_craft_definitions.rbegin(); i != m_craft_definitions.rend(); i++) { CraftDefinition *def = *i; /*infostream<<"Checking "<<input.dump()<<std::endl <<" against "<<def->dump()<<std::endl;*/ try { if(def->check(input, gamedef)) { // Get output, then decrement input (if requested) output = def->getOutput(input, gamedef); if(decrementInput) def->decrementInput(input, gamedef); return true; } } catch(SerializationError &e) { errorstream<<"getCraftResult: ERROR: " <<"Serialization error in recipe " <<def->dump()<<std::endl; // then go on with the next craft definition } } return false; } virtual bool getCraftRecipe(CraftInput &input, CraftOutput &output, IGameDef *gamedef) const { CraftOutput tmpout; tmpout.item = ""; tmpout.time = 0; // If output item is empty, abort. if(output.item.empty()) return false; // Walk crafting definitions from back to front, so that later // definitions can override earlier ones. for(std::vector<CraftDefinition*>::const_reverse_iterator i = m_craft_definitions.rbegin(); i != m_craft_definitions.rend(); i++) { CraftDefinition *def = *i; /*infostream<<"Checking "<<input.dump()<<std::endl <<" against "<<def->dump()<<std::endl;*/ try { tmpout = def->getOutput(input, gamedef); if((tmpout.item.substr(0,output.item.length()) == output.item) && ((tmpout.item[output.item.length()] == 0) || (tmpout.item[output.item.length()] == ' '))) { // Get output, then decrement input (if requested) input = def->getInput(output, gamedef); return true; } } catch(SerializationError &e) { errorstream<<"getCraftResult: ERROR: " <<"Serialization error in recipe " <<def->dump()<<std::endl; // then go on with the next craft definition } } return false; } virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output, IGameDef *gamedef) const { std::vector<CraftDefinition*> recipes_list; CraftInput input; CraftOutput tmpout; tmpout.item = ""; tmpout.time = 0; for(std::vector<CraftDefinition*>::const_reverse_iterator i = m_craft_definitions.rbegin(); i != m_craft_definitions.rend(); i++) { CraftDefinition *def = *i; /*infostream<<"Checking "<<input.dump()<<std::endl <<" against "<<def->dump()<<std::endl;*/ try { tmpout = def->getOutput(input, gamedef); if(tmpout.item.substr(0,output.item.length()) == output.item) { // Get output, then decrement input (if requested) input = def->getInput(output, gamedef); recipes_list.push_back(*i); } } catch(SerializationError &e) { errorstream<<"getCraftResult: ERROR: " <<"Serialization error in recipe " <<def->dump()<<std::endl; // then go on with the next craft definition } } return recipes_list; } virtual std::string dump() const { std::ostringstream os(std::ios::binary); os<<"Crafting definitions:\n"; for(std::vector<CraftDefinition*>::const_iterator i = m_craft_definitions.begin(); i != m_craft_definitions.end(); i++) { os<<(*i)->dump()<<"\n"; } return os.str(); } virtual void registerCraft(CraftDefinition *def) { verbosestream<<"registerCraft: registering craft definition: " <<def->dump()<<std::endl; m_craft_definitions.push_back(def); } virtual void clear() { for(std::vector<CraftDefinition*>::iterator i = m_craft_definitions.begin(); i != m_craft_definitions.end(); i++){ delete *i; } m_craft_definitions.clear(); } virtual void serialize(std::ostream &os) const { writeU8(os, 0); // version u16 count = m_craft_definitions.size(); writeU16(os, count); for(std::vector<CraftDefinition*>::const_iterator i = m_craft_definitions.begin(); i != m_craft_definitions.end(); i++){ CraftDefinition *def = *i; // Serialize wrapped in a string std::ostringstream tmp_os(std::ios::binary); def->serialize(tmp_os); os<<serializeString(tmp_os.str()); } } virtual void deSerialize(std::istream &is) { // Clear everything clear(); // Deserialize int version = readU8(is); if(version != 0) throw SerializationError( "unsupported CraftDefManager version"); u16 count = readU16(is); for(u16 i=0; i<count; i++){ // Deserialize a string and grab a CraftDefinition from it std::istringstream tmp_is(deSerializeString(is), std::ios::binary); CraftDefinition *def = CraftDefinition::deSerialize(tmp_is); // Register registerCraft(def); } } private: std::vector<CraftDefinition*> m_craft_definitions; }; IWritableCraftDefManager* createCraftDefManager() { return new CCraftDefManager(); }