]> git.lizzy.rs Git - dragonfireclient.git/blob - src/servermain.cpp
e8d7c471d6820a6e2c7b3001fdd0b96df3de0032
[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 TODO: Move the default settings into some separate file
24
25 */
26
27 #ifndef SERVER
28         #ifdef _WIN32
29         #else
30                 #error "For a server build, SERVER must be defined globally"
31         #endif
32 #endif
33
34 #ifdef UNITTEST_DISABLE
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 #ifdef _WIN32
53         #define WIN32_LEAN_AND_MEAN
54         #include <windows.h>
55         #define sleep_ms(x) Sleep(x)
56 #else
57         #include <unistd.h>
58         #define sleep_ms(x) usleep(x*1000)
59 #endif
60
61 #include <iostream>
62 #include <fstream>
63 #include <time.h>
64 #include <jmutexautolock.h>
65 #include <locale.h>
66 #include "common_irrlicht.h"
67 #include "debug.h"
68 #include "map.h"
69 #include "player.h"
70 #include "main.h"
71 #include "test.h"
72 #include "environment.h"
73 #include "server.h"
74 #include "serialization.h"
75 #include "constants.h"
76 #include "strfnd.h"
77 #include "porting.h"
78
79 // Dummy variable
80 IrrlichtDevice *g_device = NULL;
81
82 /*
83         Settings.
84         These are loaded from the config file.
85 */
86
87 Settings g_settings;
88
89 // Sets default settings
90 void set_default_settings()
91 {
92         // Client stuff
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");
106
107         // Server stuff
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");
123 }
124
125 /*
126         Debug streams
127 */
128
129 // Connection
130 std::ostream *dout_con_ptr = &dummyout;
131 std::ostream *derr_con_ptr = &dstream_no_stderr;
132
133 // Server
134 std::ostream *dout_server_ptr = &dstream;
135 std::ostream *derr_server_ptr = &dstream;
136
137 // Client
138 std::ostream *dout_client_ptr = &dstream;
139 std::ostream *derr_client_ptr = &dstream;
140
141
142 /*
143         Timestamp stuff
144 */
145
146 JMutex g_timestamp_mutex;
147
148 std::string getTimestamp()
149 {
150         if(g_timestamp_mutex.IsInitialized()==false)
151                 return "";
152         JMutexAutoLock lock(g_timestamp_mutex);
153         time_t t = time(NULL);
154         struct tm *tm = localtime(&t);
155         char cs[20];
156         strftime(cs, 20, "%H:%M:%S", tm);
157         return cs;
158 }
159
160 int main(int argc, char *argv[])
161 {
162         /*
163                 Low-level initialization
164         */
165
166         bool disable_stderr = false;
167 #ifdef _WIN32
168         disable_stderr = true;
169 #endif
170
171         // Initialize debug streams
172         debugstreams_init(disable_stderr, DEBUGFILE);
173         // Initialize debug stacks
174         debug_stacks_init();
175
176         DSTACK(__FUNCTION_NAME);
177
178         try
179         {
180         
181         /*
182                 Parse command line
183         */
184         
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));
193
194         Settings cmd_args;
195         
196         bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
197
198         if(ret == false || cmd_args.getFlag("help"))
199         {
200                 dstream<<"Allowed options:"<<std::endl;
201                 for(core::map<std::string, ValueSpec>::Iterator
202                                 i = allowed_options.getIterator();
203                                 i.atEnd() == false; i++)
204                 {
205                         dstream<<"  --"<<i.getNode()->getKey();
206                         if(i.getNode()->getValue().type == VALUETYPE_FLAG)
207                         {
208                         }
209                         else
210                         {
211                                 dstream<<" <value>";
212                         }
213                         dstream<<std::endl;
214
215                         if(i.getNode()->getValue().help != NULL)
216                         {
217                                 dstream<<"      "<<i.getNode()->getValue().help
218                                                 <<std::endl;
219                         }
220                 }
221
222                 return cmd_args.getFlag("help") ? 0 : 1;
223         }
224
225
226         /*
227                 Basic initialization
228         */
229
230         // Initialize default settings
231         set_default_settings();
232         
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
237                         <<std::endl;
238         
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");
243
244         // Initialize sockets
245         sockets_init();
246         atexit(sockets_cleanup);
247         
248         // Initialize timestamp mutex
249         g_timestamp_mutex.Init();
250
251         /*
252                 Initialization
253         */
254
255         /*
256                 Read config file
257         */
258         
259         // Path of configuration file in use
260         std::string configpath = "";
261         
262         if(cmd_args.exists("config"))
263         {
264                 bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
265                 if(r == false)
266                 {
267                         dstream<<"Could not read configuration from \""
268                                         <<cmd_args.get("config")<<"\""<<std::endl;
269                         return 1;
270                 }
271                 configpath = cmd_args.get("config");
272         }
273         else
274         {
275                 const char *filenames[2] =
276                 {
277                         "../minetest.conf",
278                         "../../minetest.conf"
279                 };
280
281                 for(u32 i=0; i<2; i++)
282                 {
283                         bool r = g_settings.readConfigFile(filenames[i]);
284                         if(r)
285                         {
286                                 configpath = filenames[i];
287                                 break;
288                         }
289                 }
290         }
291
292         // Initialize random seed
293         srand(time(0));
294
295         /*
296                 Run unit tests
297         */
298         if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
299                         || cmd_args.getFlag("enable-unittests") == true)
300         {
301                 run_tests();
302         }
303         
304         // Read map parameters from settings
305
306         HMParams hm_params;
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");
311
312         MapParams map_params;
313         map_params.plants_amount = g_settings.getFloat("plants_amount");
314         map_params.ravines_amount = g_settings.getFloat("ravines_amount");
315
316         /*
317                 Check parameters
318         */
319
320         std::cout<<std::endl<<std::endl;
321         
322         std::cout
323         <<"        .__               __                   __   "<<std::endl
324         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
325         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
326         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
327         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
328         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
329         <<std::endl
330         <<"Now with more waterish water!"
331         <<std::endl;
332
333         std::cout<<std::endl;
334         
335         // Port?
336         u16 port = 30000;
337         if(cmd_args.exists("port"))
338         {
339                 port = cmd_args.getU16("port");
340         }
341         else if(g_settings.exists("port"))
342         {
343                 port = g_settings.getU16("port");
344         }
345         else
346         {
347                 dstream<<"Please specify port (in config or on command line)"
348                                 <<std::endl;
349         }
350         
351         DSTACK("Dedicated server branch");
352         
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;
358
359         Server server("../map", hm_params, map_params);
360         server.start(port);
361
362         for(;;)
363         {
364                 // This is kind of a hack but can be done like this
365                 // because server.step() is very light
366                 sleep_ms(30);
367                 server.step(0.030);
368
369                 static int counter = 0;
370                 counter--;
371                 if(counter <= 0)
372                 {
373                         counter = 10;
374
375                         core::list<PlayerInfo> list = server.getPlayerInfo();
376                         core::list<PlayerInfo>::Iterator i;
377                         static u32 sum_old = 0;
378                         u32 sum = PIChecksum(list);
379                         if(sum != sum_old)
380                         {
381                                 std::cout<<DTIME<<"Player info:"<<std::endl;
382                                 for(i=list.begin(); i!=list.end(); i++)
383                                 {
384                                         i->PrintLine(&std::cout);
385                                 }
386                         }
387                         sum_old = sum;
388                 }
389         }
390
391         /*
392                 Update configuration file
393         */
394         if(configpath != "")
395         {
396                 g_settings.updateConfigFile(configpath.c_str());
397         }
398
399         } //try
400         catch(con::PeerNotFoundException &e)
401         {
402                 dstream<<DTIME<<"Connection timed out."<<std::endl;
403         }
404 #if CATCH_UNHANDLED_EXCEPTIONS
405         /*
406                 This is what has to be done in every thread to get suitable debug info
407         */
408         catch(std::exception &e)
409         {
410                 dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
411                                 <<e.what()<<std::endl;
412                 assert(0);
413         }
414 #endif
415
416         debugstreams_deinit();
417         
418         return 0;
419 }
420
421 //END