]> git.lizzy.rs Git - dragonfireclient.git/blob - src/servermain.cpp
added sand to map generator
[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 #include "mineral.h"
72
73 /*
74         Settings.
75         These are loaded from the config file.
76 */
77
78 Settings g_settings;
79
80 extern void set_default_settings();
81
82 /*
83         Debug streams
84 */
85
86 // Connection
87 std::ostream *dout_con_ptr = &dummyout;
88 std::ostream *derr_con_ptr = &dstream_no_stderr;
89
90 // Server
91 std::ostream *dout_server_ptr = &dstream;
92 std::ostream *derr_server_ptr = &dstream;
93
94 // Client
95 std::ostream *dout_client_ptr = &dstream;
96 std::ostream *derr_client_ptr = &dstream;
97
98
99 /*
100         gettime.h implementation
101 */
102
103 u32 getTimeMs()
104 {
105         /*
106                 Use imprecise system calls directly (from porting.h)
107         */
108         return porting::getTimeMs();
109 }
110
111 int main(int argc, char *argv[])
112 {
113         /*
114                 Low-level initialization
115         */
116
117         bool disable_stderr = false;
118 #ifdef _WIN32
119         disable_stderr = true;
120 #endif
121
122         // Initialize debug streams
123         debugstreams_init(disable_stderr, DEBUGFILE);
124         // Initialize debug stacks
125         debug_stacks_init();
126
127         DSTACK(__FUNCTION_NAME);
128
129         porting::initializePaths();
130
131         initializeMaterialProperties();
132
133         BEGIN_DEBUG_EXCEPTION_HANDLER
134
135         // Print startup message
136         dstream<<DTIME<<"minetest-c55"
137                         " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
138                         <<", "<<BUILD_INFO
139                         <<std::endl;
140         
141         try
142         {
143         
144         /*
145                 Parse command line
146         */
147         
148         // List all allowed options
149         core::map<std::string, ValueSpec> allowed_options;
150         allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG));
151         allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
152                         "Load configuration from specified file"));
153         allowed_options.insert("port", ValueSpec(VALUETYPE_STRING));
154         allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG));
155         allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG));
156         allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING));
157
158         Settings cmd_args;
159         
160         bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
161
162         if(ret == false || cmd_args.getFlag("help"))
163         {
164                 dstream<<"Allowed options:"<<std::endl;
165                 for(core::map<std::string, ValueSpec>::Iterator
166                                 i = allowed_options.getIterator();
167                                 i.atEnd() == false; i++)
168                 {
169                         dstream<<"  --"<<i.getNode()->getKey();
170                         if(i.getNode()->getValue().type == VALUETYPE_FLAG)
171                         {
172                         }
173                         else
174                         {
175                                 dstream<<" <value>";
176                         }
177                         dstream<<std::endl;
178
179                         if(i.getNode()->getValue().help != NULL)
180                         {
181                                 dstream<<"      "<<i.getNode()->getValue().help
182                                                 <<std::endl;
183                         }
184                 }
185
186                 return cmd_args.getFlag("help") ? 0 : 1;
187         }
188
189
190         /*
191                 Basic initialization
192         */
193
194         // Initialize default settings
195         set_default_settings();
196         
197         // Set locale. This is for forcing '.' as the decimal point.
198         std::locale::global(std::locale("C"));
199         // This enables printing all characters in bitmap font
200         setlocale(LC_CTYPE, "en_US");
201
202         // Initialize sockets
203         sockets_init();
204         atexit(sockets_cleanup);
205         
206         /*
207                 Initialization
208         */
209
210         /*
211                 Read config file
212         */
213         
214         // Path of configuration file in use
215         std::string configpath = "";
216         
217         if(cmd_args.exists("config"))
218         {
219                 bool r = g_settings.readConfigFile(cmd_args.get("config").c_str());
220                 if(r == false)
221                 {
222                         dstream<<"Could not read configuration from \""
223                                         <<cmd_args.get("config")<<"\""<<std::endl;
224                         return 1;
225                 }
226                 configpath = cmd_args.get("config");
227         }
228         else
229         {
230                 core::array<std::string> filenames;
231                 filenames.push_back(porting::path_userdata + "/minetest.conf");
232 #ifdef RUN_IN_PLACE
233                 filenames.push_back(porting::path_userdata + "/../minetest.conf");
234 #endif
235
236                 for(u32 i=0; i<filenames.size(); i++)
237                 {
238                         bool r = g_settings.readConfigFile(filenames[i].c_str());
239                         if(r)
240                         {
241                                 configpath = filenames[i];
242                                 break;
243                         }
244                 }
245         }
246
247         // Initialize random seed
248         srand(time(0));
249         mysrand(time(0));
250
251         /*
252                 Run unit tests
253         */
254         if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
255                         || cmd_args.getFlag("enable-unittests") == true)
256         {
257                 run_tests();
258         }
259
260         // Initialize stuff
261         
262         IIrrlichtWrapper irrlicht; // Dummy
263         init_mapnode(&irrlicht);
264         init_mineral(&irrlicht);
265
266         // Read map parameters from settings
267
268         HMParams hm_params;
269         /*hm_params.blocksize = g_settings.getU16("heightmap_blocksize");
270         hm_params.randmax = g_settings.get("height_randmax");
271         hm_params.randfactor = g_settings.get("height_randfactor");
272         hm_params.base = g_settings.get("height_base");*/
273
274         MapParams map_params;
275         map_params.plants_amount = g_settings.getFloat("plants_amount");
276         map_params.ravines_amount = g_settings.getFloat("ravines_amount");
277
278         /*
279                 Check parameters
280         */
281
282         std::cout<<std::endl<<std::endl;
283         
284         std::cout
285         <<"        .__               __                   __   "<<std::endl
286         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
287         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
288         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
289         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
290         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl
291         <<std::endl;
292
293         std::cout<<std::endl;
294         
295         // Port?
296         u16 port = 30000;
297         if(cmd_args.exists("port") && cmd_args.getU16("port") != 0)
298         {
299                 port = cmd_args.getU16("port");
300         }
301         else if(g_settings.exists("port") && g_settings.getU16("port") != 0)
302         {
303                 port = g_settings.getU16("port");
304         }
305         else
306         {
307                 dstream<<"Please specify port (in config or on command line)"
308                                 <<std::endl;
309         }
310         
311         // Figure out path to map
312         std::string map_dir = porting::path_userdata+"/map";
313         if(cmd_args.exists("map-dir"))
314                 map_dir = cmd_args.get("map-dir");
315         else if(g_settings.exists("map-dir"))
316                 map_dir = g_settings.get("map-dir");
317         
318         // Create server
319         Server server(map_dir.c_str(), hm_params, map_params);
320         server.start(port);
321         
322         // Run server
323         dedicated_server_loop(server);
324         
325         } //try
326         catch(con::PeerNotFoundException &e)
327         {
328                 dstream<<DTIME<<"Connection timed out."<<std::endl;
329         }
330
331         END_DEBUG_EXCEPTION_HANDLER
332
333         debugstreams_deinit();
334         
335         return 0;
336 }
337
338 //END