minecraft-like crafting
[oweals/minetest.git] / src / servermain.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 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.
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 General Public License for more details.
14
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.
18 */
19
20 /*
21 =============================== NOTES ==============================
22
23
24 */
25
26 #ifndef SERVER
27         #ifdef _WIN32
28         #else
29                 #error "For a server build, SERVER must be defined globally"
30         #endif
31 #endif
32
33 #ifdef UNITTEST_DISABLE
34         #ifdef _WIN32
35                 #pragma message ("Disabling unit tests")
36         #else
37                 #warning "Disabling unit tests"
38         #endif
39         // Disable unit tests
40         #define ENABLE_TESTS 0
41 #else
42         // Enable unit tests
43         #define ENABLE_TESTS 1
44 #endif
45
46 #ifdef _MSC_VER
47 #pragma comment(lib, "jthread.lib")
48 #pragma comment(lib, "zlibwapi.lib")
49 #endif
50
51 #include <iostream>
52 #include <fstream>
53 #include <time.h>
54 #include <jmutexautolock.h>
55 #include <locale.h>
56 #include "common_irrlicht.h"
57 #include "debug.h"
58 #include "map.h"
59 #include "player.h"
60 #include "main.h"
61 #include "test.h"
62 #include "environment.h"
63 #include "server.h"
64 #include "serialization.h"
65 #include "constants.h"
66 #include "strfnd.h"
67 #include "porting.h"
68 //#include "irrlichtwrapper.h"
69
70 /*
71         Settings.
72         These are loaded from the config file.
73 */
74
75 Settings g_settings;
76
77 extern void set_default_settings();
78
79 /*
80         Debug streams
81 */
82
83 // Connection
84 std::ostream *dout_con_ptr = &dummyout;
85 std::ostream *derr_con_ptr = &dstream_no_stderr;
86
87 // Server
88 std::ostream *dout_server_ptr = &dstream;
89 std::ostream *derr_server_ptr = &dstream;
90
91 // Client
92 std::ostream *dout_client_ptr = &dstream;
93 std::ostream *derr_client_ptr = &dstream;
94
95
96 /*
97         gettime.h implementation
98 */
99
100 u32 getTimeMs()
101 {
102         /*
103                 Use imprecise system calls directly (from porting.h)
104         */
105         return porting::getTimeMs();
106 }
107
108 int main(int argc, char *argv[])
109 {
110         /*
111                 Low-level initialization
112         */
113
114         bool disable_stderr = false;
115 #ifdef _WIN32
116         disable_stderr = true;
117 #endif
118
119         // Initialize debug streams
120         debugstreams_init(disable_stderr, DEBUGFILE);
121         // Initialize debug stacks
122         debug_stacks_init();
123
124         DSTACK(__FUNCTION_NAME);
125
126         initializeMaterialProperties();
127
128         try
129         {
130         
131         /*
132                 Parse command line
133         */
134         
135         // List all allowed options
136         core::map<std::string, ValueSpec> allowed_options;
137         allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG));
138         allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
139                         "Load configuration from specified file"));
140         allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
141         allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
142         allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
143         allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
144
145         Settings cmd_args;
146         
147         bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
148
149         if(ret == false || cmd_args.getFlag("help"))
150         {
151                 dstream<<"Allowed options:"<<std::endl;
152                 for(core::map<std::string, ValueSpec>::Iterator
153                                 i = allowed_options.getIterator();
154                                 i.atEnd() == false; i++)
155                 {
156                         dstream<<"  --"<<i.getNode()->getKey();
157                         if(i.getNode()->getValue().type == VALUETYPE_FLAG)
158                         {
159                         }
160                         else
161                         {
162                                 dstream<<" <value>";
163                         }
164                         dstream<<std::endl;
165
166                         if(i.getNode()->getValue().help != NULL)
167                         {
168                                 dstream<<"      "<<i.getNode()->getValue().help
169                                                 <<std::endl;
170                         }
171                 }
172
173                 return cmd_args.getFlag("help") ? 0 : 1;
174         }
175
176
177         /*
178                 Basic initialization
179         */
180
181         // Initialize default settings
182         set_default_settings();
183         
184         // Print startup message
185         dstream<<DTIME<<"minetest-c55 server"
186                         " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
187                         <<", ENABLE_TESTS="<<ENABLE_TESTS
188                         <<std::endl;
189         
190         // Set locale. This is for forcing '.' as the decimal point.
191         std::locale::global(std::locale("C"));
192         // This enables printing all characters in bitmap font
193         setlocale(LC_CTYPE, "en_US");
194
195         // Initialize sockets
196         sockets_init();
197         atexit(sockets_cleanup);
198         
199         /*
200                 Initialization
201         */
202
203         /*
204                 Read config file
205         */
206         
207         // Path of configuration file in use
208         std::string configpath = "";
209         
210         if(cmd_args.exists("config"))
211         {
212                 bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
213                 if(r == false)
214                 {
215                         dstream<<"Could not read configuration from \""
216                                         <<cmd_args.get("config")<<"\""<<std::endl;
217                         return 1;
218                 }
219                 configpath = cmd_args.get("config");
220         }
221         else
222         {
223                 const char *filenames[2] =
224                 {
225                         "../minetest.conf",
226                         "../../minetest.conf"
227                 };
228
229                 for(u32 i=0; i<2; i++)
230                 {
231                         bool r = g_settings.readConfigFile(filenames[i]);
232                         if(r)
233                         {
234                                 configpath = filenames[i];
235                                 break;
236                         }
237                 }
238         }
239
240         // Initialize random seed
241         srand(time(0));
242
243         /*
244                 Run unit tests
245         */
246         if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
247                         || cmd_args.getFlag("enable-unittests") == true)
248         {
249                 run_tests();
250         }
251         
252         // Read map parameters from settings
253
254         HMParams hm_params;
255         hm_params.blocksize = g_settings.getU16("heightmap_blocksize");
256         hm_params.randmax = g_settings.get("height_randmax");
257         hm_params.randfactor = g_settings.get("height_randfactor");
258         hm_params.base = g_settings.get("height_base");
259
260         MapParams map_params;
261         map_params.plants_amount = g_settings.getFloat("plants_amount");
262         map_params.ravines_amount = g_settings.getFloat("ravines_amount");
263
264         /*
265                 Check parameters
266         */
267
268         std::cout<<std::endl<<std::endl;
269         
270         std::cout
271         <<"        .__               __                   __   "<<std::endl
272         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
273         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
274         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
275         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
276         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
277         <<std::endl;
278
279         std::cout<<std::endl;
280         
281         // Port?
282         u16 port = 30000;
283         if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
284         {
285                 port = cmd_args.getU16("port");
286         }
287         else if(g_settings.exists("port") && g_settings.getU16("port") != 0)
288         {
289                 port = g_settings.getU16("port");
290         }
291         else
292         {
293                 dstream<<"Please specify port (in config or on command line)"
294                                 <<std::endl;
295         }
296         
297         DSTACK("Dedicated server branch");
298         
299         std::cout<<std::endl;
300         std::cout<<"========================"<<std::endl;
301         std::cout<<"Running dedicated server"<<std::endl;
302         std::cout<<"========================"<<std::endl;
303         std::cout<<std::endl;
304         
305         // Figure out path to map
306         std::string map_dir = "../map";
307         if(cmd_args.exists("map-dir"))
308                 map_dir = cmd_args.get("map-dir");
309         
310         Server server(map_dir.c_str(), hm_params, map_params);
311         server.start(port);
312
313         for(;;)
314         {
315                 // This is kind of a hack but can be done like this
316                 // because server.step() is very light
317                 sleep_ms(30);
318                 server.step(0.030);
319
320                 static int counter = 0;
321                 counter--;
322                 if(counter <= 0)
323                 {
324                         counter = 10;
325
326                         core::list<PlayerInfo> list = server.getPlayerInfo();
327                         core::list<PlayerInfo>::Iterator i;
328                         static u32 sum_old = 0;
329                         u32 sum = PIChecksum(list);
330                         if(sum != sum_old)
331                         {
332                                 std::cout<<DTIME<<"Player info:"<<std::endl;
333                                 for(i=list.begin(); i!=list.end(); i++)
334                                 {
335                                         i->PrintLine(&std::cout);
336                                 }
337                         }
338                         sum_old = sum;
339                 }
340         }
341
342         } //try
343         catch(con::PeerNotFoundException &e)
344         {
345                 dstream<<DTIME<<"Connection timed out."<<std::endl;
346         }
347 #if CATCH_UNHANDLED_EXCEPTIONS
348         /*
349                 This is what has to be done in every thread to get suitable debug info
350         */
351         catch(std::exception &e)
352         {
353                 dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
354                                 <<e.what()<<std::endl;
355                 assert(0);
356         }
357 #endif
358
359         debugstreams_deinit();
360         
361         return 0;
362 }
363
364 //END