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 ==============================
29 #error "For a server build, SERVER must be defined globally"
33 #ifdef UNITTEST_DISABLE
35 #pragma message ("Disabling unit tests")
37 #warning "Disabling unit tests"
40 #define ENABLE_TESTS 0
43 #define ENABLE_TESTS 1
47 #pragma comment(lib, "jthread.lib")
48 #pragma comment(lib, "zlibwapi.lib")
54 #include <jmutexautolock.h>
56 #include "common_irrlicht.h"
62 #include "environment.h"
64 #include "serialization.h"
65 #include "constants.h"
68 //#include "irrlichtwrapper.h"
72 These are loaded from the config file.
77 extern void set_default_settings();
84 std::ostream *dout_con_ptr = &dummyout;
85 std::ostream *derr_con_ptr = &dstream_no_stderr;
88 std::ostream *dout_server_ptr = &dstream;
89 std::ostream *derr_server_ptr = &dstream;
92 std::ostream *dout_client_ptr = &dstream;
93 std::ostream *derr_client_ptr = &dstream;
97 gettime.h implementation
103 Use imprecise system calls directly (from porting.h)
105 return porting::getTimeMs();
108 int main(int argc, char *argv[])
111 Low-level initialization
114 bool disable_stderr = false;
116 disable_stderr = true;
119 // Initialize debug streams
120 debugstreams_init(disable_stderr, DEBUGFILE);
121 // Initialize debug stacks
124 DSTACK(__FUNCTION_NAME);
133 // List all allowed options
134 core::map<std::string, ValueSpec> allowed_options;
135 allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG));
136 allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
137 "Load configuration from specified file"));
138 allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
139 allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
140 allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
141 allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
145 bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
147 if(ret == false || cmd_args.getFlag("help"))
149 dstream<<"Allowed options:"<<std::endl;
150 for(core::map<std::string, ValueSpec>::Iterator
151 i = allowed_options.getIterator();
152 i.atEnd() == false; i++)
154 dstream<<" --"<<i.getNode()->getKey();
155 if(i.getNode()->getValue().type == VALUETYPE_FLAG)
164 if(i.getNode()->getValue().help != NULL)
166 dstream<<" "<<i.getNode()->getValue().help
171 return cmd_args.getFlag("help") ? 0 : 1;
179 // Initialize default settings
180 set_default_settings();
182 // Print startup message
183 dstream<<DTIME<<"minetest-c55 server"
184 " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
185 <<", ENABLE_TESTS="<<ENABLE_TESTS
188 // Set locale. This is for forcing '.' as the decimal point.
189 std::locale::global(std::locale("C"));
190 // This enables printing all characters in bitmap font
191 setlocale(LC_CTYPE, "en_US");
193 // Initialize sockets
195 atexit(sockets_cleanup);
205 // Path of configuration file in use
206 std::string configpath = "";
208 if(cmd_args.exists("config"))
210 bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
213 dstream<<"Could not read configuration from \""
214 <<cmd_args.get("config")<<"\""<<std::endl;
217 configpath = cmd_args.get("config");
221 const char *filenames[2] =
224 "../../minetest.conf"
227 for(u32 i=0; i<2; i++)
229 bool r = g_settings.readConfigFile(filenames[i]);
232 configpath = filenames[i];
238 // Initialize random seed
244 if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
245 || cmd_args.getFlag("enable-unittests") == true)
250 // Read map parameters from settings
253 hm_params.blocksize = g_settings.getU16("heightmap_blocksize");
254 hm_params.randmax = g_settings.get("height_randmax");
255 hm_params.randfactor = g_settings.get("height_randfactor");
256 hm_params.base = g_settings.get("height_base");
258 MapParams map_params;
259 map_params.plants_amount = g_settings.getFloat("plants_amount");
260 map_params.ravines_amount = g_settings.getFloat("ravines_amount");
266 std::cout<<std::endl<<std::endl;
269 <<" .__ __ __ "<<std::endl
270 <<" _____ |__| ____ _____/ |_ ____ _______/ |_ "<<std::endl
271 <<" / \\| |/ \\_/ __ \\ __\\/ __ \\ / ___/\\ __\\"<<std::endl
272 <<"| Y Y \\ | | \\ ___/| | \\ ___/ \\___ \\ | | "<<std::endl
273 <<"|__|_| /__|___| /\\___ >__| \\___ >____ > |__| "<<std::endl
274 <<" \\/ \\/ \\/ \\/ \\/ "<<std::endl
277 std::cout<<std::endl;
281 if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
283 port = cmd_args.getU16("port");
285 else if(g_settings.exists("port") && g_settings.getU16("port") != 0)
287 port = g_settings.getU16("port");
291 dstream<<"Please specify port (in config or on command line)"
295 DSTACK("Dedicated server branch");
297 std::cout<<std::endl;
298 std::cout<<"========================"<<std::endl;
299 std::cout<<"Running dedicated server"<<std::endl;
300 std::cout<<"========================"<<std::endl;
301 std::cout<<std::endl;
303 // Figure out path to map
304 std::string map_dir = "../map";
305 if(cmd_args.exists("map-dir"))
306 map_dir = cmd_args.get("map-dir");
308 Server server(map_dir.c_str(), hm_params, map_params);
313 // This is kind of a hack but can be done like this
314 // because server.step() is very light
318 static int counter = 0;
324 core::list<PlayerInfo> list = server.getPlayerInfo();
325 core::list<PlayerInfo>::Iterator i;
326 static u32 sum_old = 0;
327 u32 sum = PIChecksum(list);
330 std::cout<<DTIME<<"Player info:"<<std::endl;
331 for(i=list.begin(); i!=list.end(); i++)
333 i->PrintLine(&std::cout);
341 catch(con::PeerNotFoundException &e)
343 dstream<<DTIME<<"Connection timed out."<<std::endl;
345 #if CATCH_UNHANDLED_EXCEPTIONS
347 This is what has to be done in every thread to get suitable debug info
349 catch(std::exception &e)
351 dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
352 <<e.what()<<std::endl;
357 debugstreams_deinit();