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