3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU 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.
21 =============================== NOTES ==============================
23 TODO: Move the default settings into some separate file
30 #error "For a server build, SERVER must be defined globally"
34 #ifdef UNITTEST_DISABLE
36 #pragma message ("Disabling unit tests")
38 #warning "Disabling unit tests"
41 #define ENABLE_TESTS 0
44 #define ENABLE_TESTS 1
48 #pragma comment(lib, "jthread.lib")
49 #pragma comment(lib, "zlibwapi.lib")
53 #define WIN32_LEAN_AND_MEAN
55 #define sleep_ms(x) Sleep(x)
58 #define sleep_ms(x) usleep(x*1000)
64 #include <jmutexautolock.h>
66 #include "common_irrlicht.h"
72 #include "environment.h"
74 #include "serialization.h"
75 #include "constants.h"
80 IrrlichtDevice *g_device = NULL;
84 These are loaded from the config file.
89 // Sets default settings
90 void set_default_settings()
93 g_settings.setDefault("wanted_fps", "30");
94 g_settings.setDefault("fps_max", "60");
95 g_settings.setDefault("viewing_range_nodes_max", "300");
96 g_settings.setDefault("viewing_range_nodes_min", "35");
97 g_settings.setDefault("screenW", "");
98 g_settings.setDefault("screenH", "");
99 g_settings.setDefault("host_game", "");
100 g_settings.setDefault("port", "");
101 g_settings.setDefault("address", "");
102 g_settings.setDefault("name", "");
103 g_settings.setDefault("random_input", "false");
104 g_settings.setDefault("client_delete_unused_sectors_timeout", "1200");
105 g_settings.setDefault("enable_fog", "true");
108 g_settings.setDefault("creative_mode", "false");
109 g_settings.setDefault("heightmap_blocksize", "32");
110 g_settings.setDefault("height_randmax", "constant 50.0");
111 g_settings.setDefault("height_randfactor", "constant 0.6");
112 g_settings.setDefault("height_base", "linear 0 0 0");
113 g_settings.setDefault("plants_amount", "1.0");
114 g_settings.setDefault("ravines_amount", "1.0");
115 g_settings.setDefault("objectdata_interval", "0.2");
116 g_settings.setDefault("active_object_range", "2");
117 g_settings.setDefault("max_simultaneous_block_sends_per_client", "1");
118 g_settings.setDefault("max_simultaneous_block_sends_server_total", "4");
119 g_settings.setDefault("disable_water_climb", "true");
120 g_settings.setDefault("endless_water", "true");
121 g_settings.setDefault("max_block_send_distance", "5");
122 g_settings.setDefault("max_block_generate_distance", "4");
130 std::ostream *dout_con_ptr = &dummyout;
131 std::ostream *derr_con_ptr = &dstream_no_stderr;
134 std::ostream *dout_server_ptr = &dstream;
135 std::ostream *derr_server_ptr = &dstream;
138 std::ostream *dout_client_ptr = &dstream;
139 std::ostream *derr_client_ptr = &dstream;
146 JMutex g_timestamp_mutex;
148 std::string getTimestamp()
150 if(g_timestamp_mutex.IsInitialized()==false)
152 JMutexAutoLock lock(g_timestamp_mutex);
153 time_t t = time(NULL);
154 struct tm *tm = localtime(&t);
156 strftime(cs, 20, "%H:%M:%S", tm);
160 int main(int argc, char *argv[])
163 Low-level initialization
166 bool disable_stderr = false;
168 disable_stderr = true;
171 // Initialize debug streams
172 debugstreams_init(disable_stderr, DEBUGFILE);
173 // Initialize debug stacks
176 DSTACK(__FUNCTION_NAME);
185 // List all allowed options
186 core::map<std::string, ValueSpec> allowed_options;
187 allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG));
188 allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
189 "Load configuration from specified file"));
190 allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
191 allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
192 allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
196 bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
198 if(ret == false || cmd_args.getFlag("help"))
200 dstream<<"Allowed options:"<<std::endl;
201 for(core::map<std::string, ValueSpec>::Iterator
202 i = allowed_options.getIterator();
203 i.atEnd() == false; i++)
205 dstream<<" --"<<i.getNode()->getKey();
206 if(i.getNode()->getValue().type == VALUETYPE_FLAG)
215 if(i.getNode()->getValue().help != NULL)
217 dstream<<" "<<i.getNode()->getValue().help
222 return cmd_args.getFlag("help") ? 0 : 1;
230 // Initialize default settings
231 set_default_settings();
233 // Print startup message
234 dstream<<DTIME<<"minetest-c55 server"
235 " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
236 <<", ENABLE_TESTS="<<ENABLE_TESTS
239 // Set locale. This is for forcing '.' as the decimal point.
240 std::locale::global(std::locale("C"));
241 // This enables printing all characters in bitmap font
242 setlocale(LC_CTYPE, "en_US");
244 // Initialize sockets
246 atexit(sockets_cleanup);
248 // Initialize timestamp mutex
249 g_timestamp_mutex.Init();
259 // Path of configuration file in use
260 std::string configpath = "";
262 if(cmd_args.exists("config"))
264 bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
267 dstream<<"Could not read configuration from \""
268 <<cmd_args.get("config")<<"\""<<std::endl;
271 configpath = cmd_args.get("config");
275 const char *filenames[2] =
278 "../../minetest.conf"
281 for(u32 i=0; i<2; i++)
283 bool r = g_settings.readConfigFile(filenames[i]);
286 configpath = filenames[i];
292 // Initialize random seed
298 if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
299 || cmd_args.getFlag("enable-unittests") == true)
304 // Read map parameters from settings
307 hm_params.blocksize = g_settings.getU16("heightmap_blocksize");
308 hm_params.randmax = g_settings.get("height_randmax");
309 hm_params.randfactor = g_settings.get("height_randfactor");
310 hm_params.base = g_settings.get("height_base");
312 MapParams map_params;
313 map_params.plants_amount = g_settings.getFloat("plants_amount");
314 map_params.ravines_amount = g_settings.getFloat("ravines_amount");
320 std::cout<<std::endl<<std::endl;
323 <<" .__ __ __ "<<std::endl
324 <<" _____ |__| ____ _____/ |_ ____ _______/ |_ "<<std::endl
325 <<" / \\| |/ \\_/ __ \\ __\\/ __ \\ / ___/\\ __\\"<<std::endl
326 <<"| Y Y \\ | | \\ ___/| | \\ ___/ \\___ \\ | | "<<std::endl
327 <<"|__|_| /__|___| /\\___ >__| \\___ >____ > |__| "<<std::endl
328 <<" \\/ \\/ \\/ \\/ \\/ "<<std::endl
330 <<"Now with more waterish water!"
333 std::cout<<std::endl;
337 if(cmd_args.exists("port"))
339 port = cmd_args.getU16("port");
341 else if(g_settings.exists("port"))
343 port = g_settings.getU16("port");
347 dstream<<"Please specify port (in config or on command line)"
351 DSTACK("Dedicated server branch");
353 std::cout<<std::endl;
354 std::cout<<"========================"<<std::endl;
355 std::cout<<"Running dedicated server"<<std::endl;
356 std::cout<<"========================"<<std::endl;
357 std::cout<<std::endl;
359 Server server("../map", hm_params, map_params);
364 // This is kind of a hack but can be done like this
365 // because server.step() is very light
369 static int counter = 0;
375 core::list<PlayerInfo> list = server.getPlayerInfo();
376 core::list<PlayerInfo>::Iterator i;
377 static u32 sum_old = 0;
378 u32 sum = PIChecksum(list);
381 std::cout<<DTIME<<"Player info:"<<std::endl;
382 for(i=list.begin(); i!=list.end(); i++)
384 i->PrintLine(&std::cout);
392 Update configuration file
396 g_settings.updateConfigFile(configpath.c_str());
400 catch(con::PeerNotFoundException &e)
402 dstream<<DTIME<<"Connection timed out."<<std::endl;
404 #if CATCH_UNHANDLED_EXCEPTIONS
406 This is what has to be done in every thread to get suitable debug info
408 catch(std::exception &e)
410 dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
411 <<e.what()<<std::endl;
416 debugstreams_deinit();