Handle certain errors properly when using --go
[oweals/minetest.git] / src / main.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 #ifdef NDEBUG
21         /*#ifdef _WIN32
22                 #pragma message ("Disabling unit tests")
23         #else
24                 #warning "Disabling unit tests"
25         #endif*/
26         // Disable unit tests
27         #define ENABLE_TESTS 0
28 #else
29         // Enable unit tests
30         #define ENABLE_TESTS 1
31 #endif
32
33 #ifdef _MSC_VER
34 #ifndef SERVER // Dedicated server isn't linked with Irrlicht
35         #pragma comment(lib, "Irrlicht.lib")
36         // This would get rid of the console window
37         //#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
38 #endif
39         #pragma comment(lib, "zlibwapi.lib")
40         #pragma comment(lib, "Shell32.lib")
41 #endif
42
43 #include "irrlicht.h" // createDevice
44
45 #include "main.h"
46 #include "mainmenumanager.h"
47 #include <iostream>
48 #include <fstream>
49 #include <locale.h>
50 #include "common_irrlicht.h"
51 #include "debug.h"
52 #include "test.h"
53 #include "server.h"
54 #include "constants.h"
55 #include "porting.h"
56 #include "gettime.h"
57 #include "guiMessageMenu.h"
58 #include "filesys.h"
59 #include "config.h"
60 #include "guiMainMenu.h"
61 #include "game.h"
62 #include "keycode.h"
63 #include "tile.h"
64 #include "chat.h"
65 #include "defaultsettings.h"
66 #include "gettext.h"
67 #include "settings.h"
68 #include "profiler.h"
69 #include "log.h"
70 #include "mods.h"
71 #include "utility_string.h"
72 #include "subgame.h"
73
74 /*
75         Settings.
76         These are loaded from the config file.
77 */
78 Settings main_settings;
79 Settings *g_settings = &main_settings;
80
81 // Global profiler
82 Profiler main_profiler;
83 Profiler *g_profiler = &main_profiler;
84
85 /*
86         Debug streams
87 */
88
89 // Connection
90 std::ostream *dout_con_ptr = &dummyout;
91 std::ostream *derr_con_ptr = &verbosestream;
92 //std::ostream *dout_con_ptr = &infostream;
93 //std::ostream *derr_con_ptr = &errorstream;
94
95 // Server
96 std::ostream *dout_server_ptr = &infostream;
97 std::ostream *derr_server_ptr = &errorstream;
98
99 // Client
100 std::ostream *dout_client_ptr = &infostream;
101 std::ostream *derr_client_ptr = &errorstream;
102
103 #ifndef SERVER
104 /*
105         Random stuff
106 */
107
108 /* mainmenumanager.h */
109
110 gui::IGUIEnvironment* guienv = NULL;
111 gui::IGUIStaticText *guiroot = NULL;
112 MainMenuManager g_menumgr;
113
114 bool noMenuActive()
115 {
116         return (g_menumgr.menuCount() == 0);
117 }
118
119 // Passed to menus to allow disconnecting and exiting
120 MainGameCallback *g_gamecallback = NULL;
121 #endif
122
123 /*
124         gettime.h implementation
125 */
126
127 #ifdef SERVER
128
129 u32 getTimeMs()
130 {
131         /* Use imprecise system calls directly (from porting.h) */
132         return porting::getTimeMs();
133 }
134
135 #else
136
137 // A small helper class
138 class TimeGetter
139 {
140 public:
141         virtual u32 getTime() = 0;
142 };
143
144 // A precise irrlicht one
145 class IrrlichtTimeGetter: public TimeGetter
146 {
147 public:
148         IrrlichtTimeGetter(IrrlichtDevice *device):
149                 m_device(device)
150         {}
151         u32 getTime()
152         {
153                 if(m_device == NULL)
154                         return 0;
155                 return m_device->getTimer()->getRealTime();
156         }
157 private:
158         IrrlichtDevice *m_device;
159 };
160 // Not so precise one which works without irrlicht
161 class SimpleTimeGetter: public TimeGetter
162 {
163 public:
164         u32 getTime()
165         {
166                 return porting::getTimeMs();
167         }
168 };
169
170 // A pointer to a global instance of the time getter
171 // TODO: why?
172 TimeGetter *g_timegetter = NULL;
173
174 u32 getTimeMs()
175 {
176         if(g_timegetter == NULL)
177                 return 0;
178         return g_timegetter->getTime();
179 }
180
181 #endif
182
183 class StderrLogOutput: public ILogOutput
184 {
185 public:
186         /* line: Full line with timestamp, level and thread */
187         void printLog(const std::string &line)
188         {
189                 std::cerr<<line<<std::endl;
190         }
191 } main_stderr_log_out;
192
193 class DstreamNoStderrLogOutput: public ILogOutput
194 {
195 public:
196         /* line: Full line with timestamp, level and thread */
197         void printLog(const std::string &line)
198         {
199                 dstream_no_stderr<<line<<std::endl;
200         }
201 } main_dstream_no_stderr_log_out;
202
203 #ifndef SERVER
204
205 /*
206         Event handler for Irrlicht
207
208         NOTE: Everything possible should be moved out from here,
209               probably to InputHandler and the_game
210 */
211
212 class MyEventReceiver : public IEventReceiver
213 {
214 public:
215         // This is the one method that we have to implement
216         virtual bool OnEvent(const SEvent& event)
217         {
218                 /*
219                         React to nothing here if a menu is active
220                 */
221                 if(noMenuActive() == false)
222                 {
223                         return false;
224                 }
225
226                 // Remember whether each key is down or up
227                 if(event.EventType == irr::EET_KEY_INPUT_EVENT)
228                 {
229                         if(event.KeyInput.PressedDown) {
230                                 keyIsDown.set(event.KeyInput);
231                                 keyWasDown.set(event.KeyInput);
232                         } else {
233                                 keyIsDown.unset(event.KeyInput);
234                         }
235                 }
236
237                 if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
238                 {
239                         if(noMenuActive() == false)
240                         {
241                                 left_active = false;
242                                 middle_active = false;
243                                 right_active = false;
244                         }
245                         else
246                         {
247                                 left_active = event.MouseInput.isLeftPressed();
248                                 middle_active = event.MouseInput.isMiddlePressed();
249                                 right_active = event.MouseInput.isRightPressed();
250
251                                 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
252                                 {
253                                         leftclicked = true;
254                                 }
255                                 if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
256                                 {
257                                         rightclicked = true;
258                                 }
259                                 if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
260                                 {
261                                         leftreleased = true;
262                                 }
263                                 if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
264                                 {
265                                         rightreleased = true;
266                                 }
267                                 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
268                                 {
269                                         mouse_wheel += event.MouseInput.Wheel;
270                                 }
271                         }
272                 }
273
274                 return false;
275         }
276
277         bool IsKeyDown(const KeyPress &keyCode) const
278         {
279                 return keyIsDown[keyCode];
280         }
281         
282         // Checks whether a key was down and resets the state
283         bool WasKeyDown(const KeyPress &keyCode)
284         {
285                 bool b = keyWasDown[keyCode];
286                 if (b)
287                         keyWasDown.unset(keyCode);
288                 return b;
289         }
290
291         s32 getMouseWheel()
292         {
293                 s32 a = mouse_wheel;
294                 mouse_wheel = 0;
295                 return a;
296         }
297
298         void clearInput()
299         {
300                 keyIsDown.clear();
301                 keyWasDown.clear();
302
303                 leftclicked = false;
304                 rightclicked = false;
305                 leftreleased = false;
306                 rightreleased = false;
307
308                 left_active = false;
309                 middle_active = false;
310                 right_active = false;
311
312                 mouse_wheel = 0;
313         }
314
315         MyEventReceiver()
316         {
317                 clearInput();
318         }
319
320         bool leftclicked;
321         bool rightclicked;
322         bool leftreleased;
323         bool rightreleased;
324
325         bool left_active;
326         bool middle_active;
327         bool right_active;
328
329         s32 mouse_wheel;
330
331 private:
332         IrrlichtDevice *m_device;
333         
334         // The current state of keys
335         KeyList keyIsDown;
336         // Whether a key has been pressed or not
337         KeyList keyWasDown;
338 };
339
340 /*
341         Separated input handler
342 */
343
344 class RealInputHandler : public InputHandler
345 {
346 public:
347         RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver):
348                 m_device(device),
349                 m_receiver(receiver)
350         {
351         }
352         virtual bool isKeyDown(const KeyPress &keyCode)
353         {
354                 return m_receiver->IsKeyDown(keyCode);
355         }
356         virtual bool wasKeyDown(const KeyPress &keyCode)
357         {
358                 return m_receiver->WasKeyDown(keyCode);
359         }
360         virtual v2s32 getMousePos()
361         {
362                 return m_device->getCursorControl()->getPosition();
363         }
364         virtual void setMousePos(s32 x, s32 y)
365         {
366                 m_device->getCursorControl()->setPosition(x, y);
367         }
368
369         virtual bool getLeftState()
370         {
371                 return m_receiver->left_active;
372         }
373         virtual bool getRightState()
374         {
375                 return m_receiver->right_active;
376         }
377         
378         virtual bool getLeftClicked()
379         {
380                 return m_receiver->leftclicked;
381         }
382         virtual bool getRightClicked()
383         {
384                 return m_receiver->rightclicked;
385         }
386         virtual void resetLeftClicked()
387         {
388                 m_receiver->leftclicked = false;
389         }
390         virtual void resetRightClicked()
391         {
392                 m_receiver->rightclicked = false;
393         }
394
395         virtual bool getLeftReleased()
396         {
397                 return m_receiver->leftreleased;
398         }
399         virtual bool getRightReleased()
400         {
401                 return m_receiver->rightreleased;
402         }
403         virtual void resetLeftReleased()
404         {
405                 m_receiver->leftreleased = false;
406         }
407         virtual void resetRightReleased()
408         {
409                 m_receiver->rightreleased = false;
410         }
411
412         virtual s32 getMouseWheel()
413         {
414                 return m_receiver->getMouseWheel();
415         }
416
417         void clear()
418         {
419                 m_receiver->clearInput();
420         }
421 private:
422         IrrlichtDevice *m_device;
423         MyEventReceiver *m_receiver;
424 };
425
426 class RandomInputHandler : public InputHandler
427 {
428 public:
429         RandomInputHandler()
430         {
431                 leftdown = false;
432                 rightdown = false;
433                 leftclicked = false;
434                 rightclicked = false;
435                 leftreleased = false;
436                 rightreleased = false;
437                 keydown.clear();
438         }
439         virtual bool isKeyDown(const KeyPress &keyCode)
440         {
441                 return keydown[keyCode];
442         }
443         virtual bool wasKeyDown(const KeyPress &keyCode)
444         {
445                 return false;
446         }
447         virtual v2s32 getMousePos()
448         {
449                 return mousepos;
450         }
451         virtual void setMousePos(s32 x, s32 y)
452         {
453                 mousepos = v2s32(x,y);
454         }
455
456         virtual bool getLeftState()
457         {
458                 return leftdown;
459         }
460         virtual bool getRightState()
461         {
462                 return rightdown;
463         }
464
465         virtual bool getLeftClicked()
466         {
467                 return leftclicked;
468         }
469         virtual bool getRightClicked()
470         {
471                 return rightclicked;
472         }
473         virtual void resetLeftClicked()
474         {
475                 leftclicked = false;
476         }
477         virtual void resetRightClicked()
478         {
479                 rightclicked = false;
480         }
481
482         virtual bool getLeftReleased()
483         {
484                 return leftreleased;
485         }
486         virtual bool getRightReleased()
487         {
488                 return rightreleased;
489         }
490         virtual void resetLeftReleased()
491         {
492                 leftreleased = false;
493         }
494         virtual void resetRightReleased()
495         {
496                 rightreleased = false;
497         }
498
499         virtual s32 getMouseWheel()
500         {
501                 return 0;
502         }
503
504         virtual void step(float dtime)
505         {
506                 {
507                         static float counter1 = 0;
508                         counter1 -= dtime;
509                         if(counter1 < 0.0)
510                         {
511                                 counter1 = 0.1*Rand(1, 40);
512                                 keydown.toggle(getKeySetting("keymap_jump"));
513                         }
514                 }
515                 {
516                         static float counter1 = 0;
517                         counter1 -= dtime;
518                         if(counter1 < 0.0)
519                         {
520                                 counter1 = 0.1*Rand(1, 40);
521                                 keydown.toggle(getKeySetting("keymap_special1"));
522                         }
523                 }
524                 {
525                         static float counter1 = 0;
526                         counter1 -= dtime;
527                         if(counter1 < 0.0)
528                         {
529                                 counter1 = 0.1*Rand(1, 40);
530                                 keydown.toggle(getKeySetting("keymap_forward"));
531                         }
532                 }
533                 {
534                         static float counter1 = 0;
535                         counter1 -= dtime;
536                         if(counter1 < 0.0)
537                         {
538                                 counter1 = 0.1*Rand(1, 40);
539                                 keydown.toggle(getKeySetting("keymap_left"));
540                         }
541                 }
542                 {
543                         static float counter1 = 0;
544                         counter1 -= dtime;
545                         if(counter1 < 0.0)
546                         {
547                                 counter1 = 0.1*Rand(1, 20);
548                                 mousespeed = v2s32(Rand(-20,20), Rand(-15,20));
549                         }
550                 }
551                 {
552                         static float counter1 = 0;
553                         counter1 -= dtime;
554                         if(counter1 < 0.0)
555                         {
556                                 counter1 = 0.1*Rand(1, 30);
557                                 leftdown = !leftdown;
558                                 if(leftdown)
559                                         leftclicked = true;
560                                 if(!leftdown)
561                                         leftreleased = true;
562                         }
563                 }
564                 {
565                         static float counter1 = 0;
566                         counter1 -= dtime;
567                         if(counter1 < 0.0)
568                         {
569                                 counter1 = 0.1*Rand(1, 15);
570                                 rightdown = !rightdown;
571                                 if(rightdown)
572                                         rightclicked = true;
573                                 if(!rightdown)
574                                         rightreleased = true;
575                         }
576                 }
577                 mousepos += mousespeed;
578         }
579
580         s32 Rand(s32 min, s32 max)
581         {
582                 return (myrand()%(max-min+1))+min;
583         }
584 private:
585         KeyList keydown;
586         v2s32 mousepos;
587         v2s32 mousespeed;
588         bool leftdown;
589         bool rightdown;
590         bool leftclicked;
591         bool rightclicked;
592         bool leftreleased;
593         bool rightreleased;
594 };
595
596 void drawMenuBackground(video::IVideoDriver* driver)
597 {
598         core::dimension2d<u32> screensize = driver->getScreenSize();
599                 
600         video::ITexture *bgtexture =
601                         driver->getTexture(getTexturePath("menubg.png").c_str());
602         if(bgtexture)
603         {
604                 s32 scaledsize = 128;
605                 
606                 // The important difference between destsize and screensize is
607                 // that destsize is rounded to whole scaled pixels.
608                 // These formulas use component-wise multiplication and division of v2u32.
609                 v2u32 texturesize = bgtexture->getSize();
610                 v2u32 sourcesize = texturesize * screensize / scaledsize + v2u32(1,1);
611                 v2u32 destsize = scaledsize * sourcesize / texturesize;
612                 
613                 // Default texture wrapping mode in Irrlicht is ETC_REPEAT.
614                 driver->draw2DImage(bgtexture,
615                         core::rect<s32>(0, 0, destsize.X, destsize.Y),
616                         core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
617                         NULL, NULL, true);
618         }
619         
620         video::ITexture *logotexture =
621                         driver->getTexture(getTexturePath("menulogo.png").c_str());
622         if(logotexture)
623         {
624                 v2s32 logosize(logotexture->getOriginalSize().Width,
625                                 logotexture->getOriginalSize().Height);
626                 logosize *= 4;
627
628                 video::SColor bgcolor(255,50,50,50);
629                 core::rect<s32> bgrect(0, screensize.Height-logosize.Y-20,
630                                 screensize.Width, screensize.Height);
631                 driver->draw2DRectangle(bgcolor, bgrect, NULL);
632
633                 core::rect<s32> rect(0,0,logosize.X,logosize.Y);
634                 rect += v2s32(screensize.Width/2,screensize.Height-10-logosize.Y);
635                 rect -= v2s32(logosize.X/2, 0);
636                 driver->draw2DImage(logotexture, rect,
637                         core::rect<s32>(core::position2d<s32>(0,0),
638                         core::dimension2di(logotexture->getSize())),
639                         NULL, NULL, true);
640         }
641 }
642
643 #endif
644
645 // These are defined global so that they're not optimized too much.
646 // Can't change them to volatile.
647 s16 temp16;
648 f32 tempf;
649 v3f tempv3f1;
650 v3f tempv3f2;
651 std::string tempstring;
652 std::string tempstring2;
653
654 void SpeedTests()
655 {
656         {
657                 infostream<<"The following test should take around 20ms."<<std::endl;
658                 TimeTaker timer("Testing std::string speed");
659                 const u32 jj = 10000;
660                 for(u32 j=0; j<jj; j++)
661                 {
662                         tempstring = "";
663                         tempstring2 = "";
664                         const u32 ii = 10;
665                         for(u32 i=0; i<ii; i++){
666                                 tempstring2 += "asd";
667                         }
668                         for(u32 i=0; i<ii+1; i++){
669                                 tempstring += "asd";
670                                 if(tempstring == tempstring2)
671                                         break;
672                         }
673                 }
674         }
675         
676         infostream<<"All of the following tests should take around 100ms each."
677                         <<std::endl;
678
679         {
680                 TimeTaker timer("Testing floating-point conversion speed");
681                 tempf = 0.001;
682                 for(u32 i=0; i<4000000; i++){
683                         temp16 += tempf;
684                         tempf += 0.001;
685                 }
686         }
687         
688         {
689                 TimeTaker timer("Testing floating-point vector speed");
690
691                 tempv3f1 = v3f(1,2,3);
692                 tempv3f2 = v3f(4,5,6);
693                 for(u32 i=0; i<10000000; i++){
694                         tempf += tempv3f1.dotProduct(tempv3f2);
695                         tempv3f2 += v3f(7,8,9);
696                 }
697         }
698
699         {
700                 TimeTaker timer("Testing core::map speed");
701                 
702                 core::map<v2s16, f32> map1;
703                 tempf = -324;
704                 const s16 ii=300;
705                 for(s16 y=0; y<ii; y++){
706                         for(s16 x=0; x<ii; x++){
707                                 map1.insert(v2s16(x,y), tempf);
708                                 tempf += 1;
709                         }
710                 }
711                 for(s16 y=ii-1; y>=0; y--){
712                         for(s16 x=0; x<ii; x++){
713                                 tempf = map1[v2s16(x,y)];
714                         }
715                 }
716         }
717
718         {
719                 infostream<<"Around 5000/ms should do well here."<<std::endl;
720                 TimeTaker timer("Testing mutex speed");
721                 
722                 JMutex m;
723                 m.Init();
724                 u32 n = 0;
725                 u32 i = 0;
726                 do{
727                         n += 10000;
728                         for(; i<n; i++){
729                                 m.Lock();
730                                 m.Unlock();
731                         }
732                 }
733                 // Do at least 10ms
734                 while(timer.getTime() < 10);
735
736                 u32 dtime = timer.stop();
737                 u32 per_ms = n / dtime;
738                 infostream<<"Done. "<<dtime<<"ms, "
739                                 <<per_ms<<"/ms"<<std::endl;
740         }
741 }
742
743 int main(int argc, char *argv[])
744 {
745         int retval = 0;
746
747         /*
748                 Initialization
749         */
750
751         log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION);
752         log_add_output_all_levs(&main_dstream_no_stderr_log_out);
753
754         log_register_thread("main");
755
756         // Set locale. This is for forcing '.' as the decimal point.
757         std::locale::global(std::locale("C"));
758         // This enables printing all characters in bitmap font
759         setlocale(LC_CTYPE, "en_US");
760
761         /*
762                 Parse command line
763         */
764         
765         // List all allowed options
766         core::map<std::string, ValueSpec> allowed_options;
767         allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG,
768                         "Show allowed options"));
769         allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
770                         "Load configuration from specified file"));
771         allowed_options.insert("port", ValueSpec(VALUETYPE_STRING,
772                         "Set network port (UDP)"));
773         allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG,
774                         "Disable unit tests"));
775         allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG,
776                         "Enable unit tests"));
777         allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING,
778                         "Same as --world (deprecated)"));
779         allowed_options.insert("world", ValueSpec(VALUETYPE_STRING,
780                         "Set world path (implies local game)"));
781         allowed_options.insert("verbose", ValueSpec(VALUETYPE_FLAG,
782                         "Print more information to console"));
783         allowed_options.insert("logfile", ValueSpec(VALUETYPE_STRING,
784                         "Set logfile path ('' = no logging)"));
785         allowed_options.insert("gameid", ValueSpec(VALUETYPE_STRING,
786                         "Set gameid (\"--gameid list\" prints available ones)"));
787 #ifndef SERVER
788         allowed_options.insert("speedtests", ValueSpec(VALUETYPE_FLAG,
789                         "Run speed tests"));
790         allowed_options.insert("address", ValueSpec(VALUETYPE_STRING,
791                         "Address to connect to. ('' = local game)"));
792         allowed_options.insert("random-input", ValueSpec(VALUETYPE_FLAG,
793                         "Enable random user input, for testing"));
794         allowed_options.insert("server", ValueSpec(VALUETYPE_FLAG,
795                         "Run dedicated server"));
796         allowed_options.insert("name", ValueSpec(VALUETYPE_STRING,
797                         "Set player name"));
798         allowed_options.insert("password", ValueSpec(VALUETYPE_STRING,
799                         "Set password"));
800         allowed_options.insert("go", ValueSpec(VALUETYPE_FLAG,
801                         "Disable main menu"));
802 #endif
803
804         Settings cmd_args;
805         
806         bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
807
808         if(ret == false || cmd_args.getFlag("help"))
809         {
810                 dstream<<"Allowed options:"<<std::endl;
811                 for(core::map<std::string, ValueSpec>::Iterator
812                                 i = allowed_options.getIterator();
813                                 i.atEnd() == false; i++)
814                 {
815                         std::ostringstream os1(std::ios::binary);
816                         os1<<"  --"<<i.getNode()->getKey();
817                         if(i.getNode()->getValue().type == VALUETYPE_FLAG)
818                                 {}
819                         else
820                                 os1<<" <value>";
821                         dstream<<padStringRight(os1.str(), 24);
822
823                         if(i.getNode()->getValue().help != NULL)
824                                 dstream<<i.getNode()->getValue().help;
825                         dstream<<std::endl;
826                 }
827
828                 return cmd_args.getFlag("help") ? 0 : 1;
829         }
830         
831         /*
832                 Low-level initialization
833         */
834         
835         // In certain cases, output info level on stderr
836         if(cmd_args.getFlag("verbose") || cmd_args.getFlag("speedtests"))
837                 log_add_output(&main_stderr_log_out, LMT_INFO);
838
839         porting::signal_handler_init();
840         bool &kill = *porting::signal_handler_killstatus();
841         
842         porting::initializePaths();
843
844         // Create user data directory
845         fs::CreateDir(porting::path_user);
846
847         init_gettext((porting::path_share+DIR_DELIM+".."+DIR_DELIM+"locale").c_str());
848         
849         // Initialize debug streams
850 #ifdef RUN_IN_PLACE
851         std::string logfile = DEBUGFILE;
852 #else
853         std::string logfile = porting::path_user+DIR_DELIM+DEBUGFILE;
854 #endif
855         if(cmd_args.exists("logfile"))
856                 logfile = cmd_args.get("logfile");
857         if(logfile != "")
858                 debugstreams_init(false, logfile.c_str());
859         else
860                 debugstreams_init(false, NULL);
861
862         infostream<<"logfile    = "<<logfile<<std::endl;
863         infostream<<"path_share = "<<porting::path_share<<std::endl;
864         infostream<<"path_user  = "<<porting::path_user<<std::endl;
865
866         // Initialize debug stacks
867         debug_stacks_init();
868         DSTACK(__FUNCTION_NAME);
869
870         // Debug handler
871         BEGIN_DEBUG_EXCEPTION_HANDLER
872         
873         // List gameids if requested
874         if(cmd_args.exists("gameid") && cmd_args.get("gameid") == "list")
875         {
876                 std::set<std::string> gameids = getAvailableGameIds();
877                 for(std::set<std::string>::const_iterator i = gameids.begin();
878                                 i != gameids.end(); i++)
879                         dstream<<(*i)<<std::endl;
880                 return 0;
881         }
882         
883         // Print startup message
884         actionstream<<PROJECT_NAME<<
885                         " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
886                         <<", "<<BUILD_INFO
887                         <<std::endl;
888         
889         /*
890                 Basic initialization
891         */
892
893         // Initialize default settings
894         set_default_settings(g_settings);
895         
896         // Initialize sockets
897         sockets_init();
898         atexit(sockets_cleanup);
899         
900         /*
901                 Read config file
902         */
903         
904         // Path of configuration file in use
905         std::string configpath = "";
906         
907         if(cmd_args.exists("config"))
908         {
909                 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
910                 if(r == false)
911                 {
912                         errorstream<<"Could not read configuration from \""
913                                         <<cmd_args.get("config")<<"\""<<std::endl;
914                         return 1;
915                 }
916                 configpath = cmd_args.get("config");
917         }
918         else
919         {
920                 core::array<std::string> filenames;
921                 filenames.push_back(porting::path_user +
922                                 DIR_DELIM + "minetest.conf");
923                 // Legacy configuration file location
924                 filenames.push_back(porting::path_user +
925                                 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
926 #ifdef RUN_IN_PLACE
927                 // Try also from a lower level (to aid having the same configuration
928                 // for many RUN_IN_PLACE installs)
929                 filenames.push_back(porting::path_user +
930                                 DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
931 #endif
932
933                 for(u32 i=0; i<filenames.size(); i++)
934                 {
935                         bool r = g_settings->readConfigFile(filenames[i].c_str());
936                         if(r)
937                         {
938                                 configpath = filenames[i];
939                                 break;
940                         }
941                 }
942                 
943                 // If no path found, use the first one (menu creates the file)
944                 if(configpath == "")
945                         configpath = filenames[0];
946         }
947
948         // Initialize random seed
949         srand(time(0));
950         mysrand(time(0));
951
952         /*
953                 Run unit tests
954         */
955
956         if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
957                         || cmd_args.getFlag("enable-unittests") == true)
958         {
959                 run_tests();
960         }
961         
962         /*
963                 Game parameters
964         */
965
966         // Port
967         u16 port = 30000;
968         if(cmd_args.exists("port"))
969                 port = cmd_args.getU16("port");
970         else if(g_settings->exists("port"))
971                 port = g_settings->getU16("port");
972         if(port == 0)
973                 port = 30000;
974         
975         // World directory
976         std::string commanded_world = "";
977         if(cmd_args.exists("world"))
978                 commanded_world = cmd_args.get("world");
979         else if(cmd_args.exists("map-dir"))
980                 commanded_world = cmd_args.get("map-dir");
981         else if(g_settings->exists("map-dir"))
982                 commanded_world = g_settings->get("map-dir");
983         
984         // Gamespec
985         SubgameSpec commanded_gamespec;
986         if(cmd_args.exists("gameid")){
987                 std::string gameid = cmd_args.get("gameid");
988                 commanded_gamespec = findSubgame(gameid);
989                 if(!commanded_gamespec.isValid()){
990                         errorstream<<"Game \""<<gameid<<"\" not found"<<std::endl;
991                         return 1;
992                 }
993         }
994
995         /*
996                 Run dedicated server if asked to or no other option
997         */
998 #ifdef SERVER
999         bool run_dedicated_server = true;
1000 #else
1001         bool run_dedicated_server = cmd_args.getFlag("server");
1002 #endif
1003         if(run_dedicated_server)
1004         {
1005                 DSTACK("Dedicated server branch");
1006                 // Create time getter if built with Irrlicht
1007 #ifndef SERVER
1008                 g_timegetter = new SimpleTimeGetter();
1009 #endif
1010
1011                 // World directory
1012                 std::string world_path;
1013                 bool is_legacy_world = false;
1014                 if(commanded_world != ""){
1015                         world_path = commanded_world;
1016                 }
1017                 else{
1018                         // No specific world was commanded
1019                         // Check if the world is found from the default directory, and if
1020                         // not, see if the legacy world directory exists.
1021                         world_path = porting::path_user + DIR_DELIM + "server" + DIR_DELIM + "worlds" + DIR_DELIM + "world";
1022                         std::string legacy_world_path = porting::path_user+DIR_DELIM+".."+DIR_DELIM+"world";
1023                         if(!fs::PathExists(world_path) && fs::PathExists(legacy_world_path)){
1024                                 errorstream<<"Warning: Using legacy world directory \""
1025                                                 <<legacy_world_path<<"\""<<std::endl;
1026                                 world_path = legacy_world_path;
1027                                 is_legacy_world = true;
1028                         }
1029                 }
1030
1031                 // Gamespec
1032                 std::string world_gameid = getWorldGameId(world_path, is_legacy_world);
1033                 SubgameSpec gamespec = findSubgame(world_gameid);
1034                 if(commanded_gamespec.isValid() &&
1035                                 commanded_gamespec.id != world_gameid){
1036                         errorstream<<"WARNING: Overriding gameid from \""
1037                                         <<world_gameid<<"\" to \""
1038                                         <<commanded_gamespec.id<<"\""<<std::endl;
1039                         gamespec = commanded_gamespec;
1040                 }
1041
1042                 if(!gamespec.isValid()){
1043                         errorstream<<"Invalid gamespec. (world_gameid="
1044                                         <<world_gameid<<")"<<std::endl;
1045                         return 1;
1046                 }
1047                 
1048                 infostream<<"Using gamespec \""<<gamespec.id<<"\""<<std::endl;
1049
1050                 // Create server
1051                 Server server(world_path, configpath, gamespec);
1052                 server.start(port);
1053                 
1054                 // Run server
1055                 dedicated_server_loop(server, kill);
1056
1057                 return 0;
1058         }
1059
1060 #ifndef SERVER // Exclude from dedicated server build
1061
1062         /*
1063                 More parameters
1064         */
1065         
1066         std::string address = g_settings->get("address");
1067         if(cmd_args.exists("address"))
1068                 address = cmd_args.get("address");
1069         else if(cmd_args.exists("world"))
1070                 address = "";
1071         
1072         std::string playername = g_settings->get("name");
1073         if(cmd_args.exists("name"))
1074                 playername = cmd_args.get("name");
1075         
1076         bool skip_main_menu = cmd_args.getFlag("go");
1077
1078         /*
1079                 Device initialization
1080         */
1081
1082         // Resolution selection
1083         
1084         bool fullscreen = false;
1085         u16 screenW = g_settings->getU16("screenW");
1086         u16 screenH = g_settings->getU16("screenH");
1087
1088         // Determine driver
1089
1090         video::E_DRIVER_TYPE driverType;
1091         
1092         std::string driverstring = g_settings->get("video_driver");
1093
1094         if(driverstring == "null")
1095                 driverType = video::EDT_NULL;
1096         else if(driverstring == "software")
1097                 driverType = video::EDT_SOFTWARE;
1098         else if(driverstring == "burningsvideo")
1099                 driverType = video::EDT_BURNINGSVIDEO;
1100         else if(driverstring == "direct3d8")
1101                 driverType = video::EDT_DIRECT3D8;
1102         else if(driverstring == "direct3d9")
1103                 driverType = video::EDT_DIRECT3D9;
1104         else if(driverstring == "opengl")
1105                 driverType = video::EDT_OPENGL;
1106         else
1107         {
1108                 errorstream<<"WARNING: Invalid video_driver specified; defaulting "
1109                                 "to opengl"<<std::endl;
1110                 driverType = video::EDT_OPENGL;
1111         }
1112
1113         /*
1114                 Create device and exit if creation failed
1115         */
1116
1117         MyEventReceiver receiver;
1118
1119         IrrlichtDevice *device;
1120         device = createDevice(driverType,
1121                         core::dimension2d<u32>(screenW, screenH),
1122                         16, fullscreen, false, false, &receiver);
1123
1124         if (device == 0)
1125                 return 1; // could not create selected driver.
1126         
1127         /*
1128                 Continue initialization
1129         */
1130
1131         video::IVideoDriver* driver = device->getVideoDriver();
1132
1133         // Disable mipmaps (because some of them look ugly)
1134         driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
1135
1136         /*
1137                 This changes the minimum allowed number of vertices in a VBO.
1138                 Default is 500.
1139         */
1140         //driver->setMinHardwareBufferVertexCount(50);
1141
1142         // Create time getter
1143         g_timegetter = new IrrlichtTimeGetter(device);
1144         
1145         // Create game callback for menus
1146         g_gamecallback = new MainGameCallback(device);
1147         
1148         /*
1149                 Speed tests (done after irrlicht is loaded to get timer)
1150         */
1151         if(cmd_args.getFlag("speedtests"))
1152         {
1153                 dstream<<"Running speed tests"<<std::endl;
1154                 SpeedTests();
1155                 return 0;
1156         }
1157         
1158         device->setResizable(true);
1159
1160         bool random_input = g_settings->getBool("random_input")
1161                         || cmd_args.getFlag("random-input");
1162         InputHandler *input = NULL;
1163         if(random_input)
1164                 input = new RandomInputHandler();
1165         else
1166                 input = new RealInputHandler(device, &receiver);
1167         
1168         scene::ISceneManager* smgr = device->getSceneManager();
1169
1170         guienv = device->getGUIEnvironment();
1171         gui::IGUISkin* skin = guienv->getSkin();
1172         gui::IGUIFont* font = guienv->getFont(getTexturePath("fontlucida.png").c_str());
1173         if(font)
1174                 skin->setFont(font);
1175         else
1176                 errorstream<<"WARNING: Font file was not found."
1177                                 " Using default font."<<std::endl;
1178         // If font was not found, this will get us one
1179         font = skin->getFont();
1180         assert(font);
1181         
1182         u32 text_height = font->getDimension(L"Hello, world!").Height;
1183         infostream<<"text_height="<<text_height<<std::endl;
1184
1185         //skin->setColor(gui::EGDC_BUTTON_TEXT, video::SColor(255,0,0,0));
1186         skin->setColor(gui::EGDC_BUTTON_TEXT, video::SColor(255,255,255,255));
1187         //skin->setColor(gui::EGDC_3D_HIGH_LIGHT, video::SColor(0,0,0,0));
1188         //skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(0,0,0,0));
1189         skin->setColor(gui::EGDC_3D_HIGH_LIGHT, video::SColor(255,0,0,0));
1190         skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(255,0,0,0));
1191         skin->setColor(gui::EGDC_HIGH_LIGHT, video::SColor(255,70,100,50));
1192         skin->setColor(gui::EGDC_HIGH_LIGHT_TEXT, video::SColor(255,255,255,255));
1193         
1194         /*
1195                 GUI stuff
1196         */
1197
1198         ChatBackend chat_backend;
1199
1200         /*
1201                 If an error occurs, this is set to something and the
1202                 menu-game loop is restarted. It is then displayed before
1203                 the menu.
1204         */
1205         std::wstring error_message = L"";
1206
1207         // The password entered during the menu screen,
1208         std::string password;
1209
1210         bool first_loop = true;
1211
1212         /*
1213                 Menu-game loop
1214         */
1215         while(device->run() && kill == false)
1216         {
1217                 // Set the window caption
1218                 device->setWindowCaption(L"Minetest [Main Menu]");
1219
1220                 // This is used for catching disconnects
1221                 try
1222                 {
1223
1224                         /*
1225                                 Clear everything from the GUIEnvironment
1226                         */
1227                         guienv->clear();
1228                         
1229                         /*
1230                                 We need some kind of a root node to be able to add
1231                                 custom gui elements directly on the screen.
1232                                 Otherwise they won't be automatically drawn.
1233                         */
1234                         guiroot = guienv->addStaticText(L"",
1235                                         core::rect<s32>(0, 0, 10000, 10000));
1236                         
1237                         SubgameSpec gamespec;
1238                         WorldSpec worldspec;
1239
1240                         /*
1241                                 Out-of-game menu loop.
1242
1243                                 Loop quits when menu returns proper parameters.
1244                         */
1245                         while(kill == false)
1246                         {
1247                                 // If skip_main_menu, only go through here once
1248                                 if(skip_main_menu && !first_loop){
1249                                         kill = true;
1250                                         break;
1251                                 }
1252                                 first_loop = false;
1253                                 
1254                                 // Cursor can be non-visible when coming from the game
1255                                 device->getCursorControl()->setVisible(true);
1256                                 // Some stuff are left to scene manager when coming from the game
1257                                 // (map at least?)
1258                                 smgr->clear();
1259                                 
1260                                 // Initialize menu data
1261                                 MainMenuData menudata;
1262                                 menudata.address = narrow_to_wide(address);
1263                                 menudata.name = narrow_to_wide(playername);
1264                                 menudata.port = narrow_to_wide(itos(port));
1265                                 if(cmd_args.exists("password"))
1266                                         menudata.password = narrow_to_wide(cmd_args.get("password"));
1267                                 menudata.fancy_trees = g_settings->getBool("new_style_leaves");
1268                                 menudata.smooth_lighting = g_settings->getBool("smooth_lighting");
1269                                 menudata.clouds_3d = g_settings->getBool("enable_3d_clouds");
1270                                 menudata.opaque_water = g_settings->getBool("opaque_water");
1271                                 menudata.creative_mode = g_settings->getBool("creative_mode");
1272                                 menudata.enable_damage = g_settings->getBool("enable_damage");
1273                                 // Get world listing for the menu
1274                                 std::vector<WorldSpec> worldspecs = getAvailableWorlds();
1275                                 for(std::vector<WorldSpec>::const_iterator i = worldspecs.begin();
1276                                                 i != worldspecs.end(); i++)
1277                                         menudata.worlds.push_back(narrow_to_wide(
1278                                                         i->name + " [" + i->gameid + "]"));
1279                                 // Select if there is only one
1280                                 if(worldspecs.size() == 1)
1281                                         menudata.selected_world = 0;
1282                                 else
1283                                         menudata.selected_world = -1;
1284                                 // If a world was commanded, append and select it
1285                                 if(commanded_world != ""){
1286                                         std::string gameid = getWorldGameId(commanded_world, true);
1287                                         if(gameid == "")
1288                                                 gameid = g_settings->get("default_game");
1289                                         WorldSpec spec(commanded_world, "[commanded world]", gameid);
1290                                         worldspecs.push_back(spec);
1291                                         menudata.worlds.push_back(narrow_to_wide(spec.name)
1292                                                         +L" ["+narrow_to_wide(spec.gameid)+L"]");
1293                                         menudata.selected_world = menudata.worlds.size()-1;
1294                                 }
1295
1296                                 if(skip_main_menu == false)
1297                                 {
1298                                         GUIMainMenu *menu =
1299                                                         new GUIMainMenu(guienv, guiroot, -1, 
1300                                                                 &g_menumgr, &menudata, g_gamecallback);
1301                                         menu->allowFocusRemoval(true);
1302
1303                                         if(error_message != L"")
1304                                         {
1305                                                 verbosestream<<"error_message = "
1306                                                                 <<wide_to_narrow(error_message)<<std::endl;
1307
1308                                                 GUIMessageMenu *menu2 =
1309                                                                 new GUIMessageMenu(guienv, guiroot, -1, 
1310                                                                         &g_menumgr, error_message.c_str());
1311                                                 menu2->drop();
1312                                                 error_message = L"";
1313                                         }
1314
1315                                         video::IVideoDriver* driver = device->getVideoDriver();
1316                                         
1317                                         infostream<<"Created main menu"<<std::endl;
1318
1319                                         while(device->run() && kill == false)
1320                                         {
1321                                                 if(menu->getStatus() == true)
1322                                                         break;
1323
1324                                                 //driver->beginScene(true, true, video::SColor(255,0,0,0));
1325                                                 driver->beginScene(true, true, video::SColor(255,128,128,128));
1326
1327                                                 drawMenuBackground(driver);
1328
1329                                                 guienv->drawAll();
1330                                                 
1331                                                 driver->endScene();
1332                                                 
1333                                                 // On some computers framerate doesn't seem to be
1334                                                 // automatically limited
1335                                                 sleep_ms(25);
1336                                         }
1337                                         
1338                                         // Break out of menu-game loop to shut down cleanly
1339                                         if(device->run() == false || kill == true)
1340                                                 break;
1341                                         
1342                                         infostream<<"Dropping main menu"<<std::endl;
1343
1344                                         menu->drop();
1345                                 }
1346
1347                                 // Set world path to selected one
1348                                 if(menudata.selected_world != -1){
1349                                         worldspec = worldspecs[menudata.selected_world];
1350                                         infostream<<"Selected world: "<<worldspec.name
1351                                                         <<" ["<<worldspec.path<<"]"<<std::endl;
1352                                 }
1353                                 
1354                                 // Delete map if requested
1355                                 if(menudata.delete_world)
1356                                 {
1357                                         if(menudata.selected_world == -1){
1358                                                 error_message = L"Cannot delete world: "
1359                                                                 L"no world selected";
1360                                                 errorstream<<wide_to_narrow(error_message)<<std::endl;
1361                                                 continue;
1362                                         }
1363                                         /*bool r = fs::RecursiveDeleteContent(worldspec.path);
1364                                         if(r == false){
1365                                                 error_message = L"World delete failed";
1366                                                 errorstream<<wide_to_narrow(error_message)<<std::endl;
1367                                         }*/
1368                                         // TODO: Some kind of a yes/no dialog is needed.
1369                                         error_message = L"This doesn't do anything currently.";
1370                                         errorstream<<wide_to_narrow(error_message)<<std::endl;
1371                                         continue;
1372                                 }
1373
1374                                 playername = wide_to_narrow(menudata.name);
1375                                 password = translatePassword(playername, menudata.password);
1376                                 //infostream<<"Main: password hash: '"<<password<<"'"<<std::endl;
1377
1378                                 address = wide_to_narrow(menudata.address);
1379                                 int newport = stoi(wide_to_narrow(menudata.port));
1380                                 if(newport != 0)
1381                                         port = newport;
1382                                 // Save settings
1383                                 g_settings->set("new_style_leaves", itos(menudata.fancy_trees));
1384                                 g_settings->set("smooth_lighting", itos(menudata.smooth_lighting));
1385                                 g_settings->set("enable_3d_clouds", itos(menudata.clouds_3d));
1386                                 g_settings->set("opaque_water", itos(menudata.opaque_water));
1387                                 g_settings->set("creative_mode", itos(menudata.creative_mode));
1388                                 g_settings->set("enable_damage", itos(menudata.enable_damage));
1389                                 g_settings->set("name", playername);
1390                                 g_settings->set("address", address);
1391                                 g_settings->set("port", itos(port));
1392                                 // Update configuration file
1393                                 if(configpath != "")
1394                                         g_settings->updateConfigFile(configpath.c_str());
1395                                 
1396                                 // If local game
1397                                 if(address == "")
1398                                 {
1399                                         if(menudata.selected_world == -1){
1400                                                 error_message = L"No world selected and no address "
1401                                                                 L"provided. Nothing to do.";
1402                                                 errorstream<<wide_to_narrow(error_message)<<std::endl;
1403                                                 continue;
1404                                         }
1405                                         // Load gamespec for required game
1406                                         gamespec = findSubgame(worldspec.gameid);
1407                                         if(!gamespec.isValid() && !commanded_gamespec.isValid()){
1408                                                 error_message = L"Could not find or load game \""
1409                                                                 + narrow_to_wide(worldspec.gameid) + L"\"";
1410                                                 errorstream<<wide_to_narrow(error_message)<<std::endl;
1411                                                 continue;
1412                                         }
1413                                         if(commanded_gamespec.isValid() &&
1414                                                         commanded_gamespec.id != worldspec.gameid){
1415                                                 errorstream<<"WARNING: Overriding gamespec from \""
1416                                                                 <<worldspec.gameid<<"\" to \""
1417                                                                 <<commanded_gamespec.id<<"\""<<std::endl;
1418                                                 gamespec = commanded_gamespec;
1419                                         }
1420
1421                                         if(!gamespec.isValid()){
1422                                                 error_message = L"Invalid gamespec. (world_gameid="
1423                                                                 +narrow_to_wide(worldspec.gameid)+L")";
1424                                                 errorstream<<wide_to_narrow(error_message)<<std::endl;
1425                                                 continue;
1426                                         }
1427                                 }
1428
1429                                 // Continue to game
1430                                 break;
1431                         }
1432                         
1433                         // Break out of menu-game loop to shut down cleanly
1434                         if(device->run() == false || kill == true)
1435                                 break;
1436                         
1437                         /*
1438                                 Run game
1439                         */
1440                         the_game(
1441                                 kill,
1442                                 random_input,
1443                                 input,
1444                                 device,
1445                                 font,
1446                                 worldspec.path,
1447                                 playername,
1448                                 password,
1449                                 address,
1450                                 port,
1451                                 error_message,
1452                                 configpath,
1453                                 chat_backend,
1454                                 gamespec
1455                         );
1456
1457                 } //try
1458                 catch(con::PeerNotFoundException &e)
1459                 {
1460                         error_message = L"Connection error (timed out?)";
1461                         errorstream<<wide_to_narrow(error_message)<<std::endl;
1462                 }
1463                 catch(ServerError &e)
1464                 {
1465                         error_message = narrow_to_wide(e.what());
1466                         errorstream<<wide_to_narrow(error_message)<<std::endl;
1467                 }
1468                 catch(ModError &e)
1469                 {
1470                         errorstream<<e.what()<<std::endl;
1471                         error_message = narrow_to_wide(e.what()) + L"\nCheck debug.txt for details.";
1472                 }
1473 #ifdef NDEBUG
1474                 catch(std::exception &e)
1475                 {
1476                         std::string narrow_message = "Some exception, what()=\"";
1477                         narrow_message += e.what();
1478                         narrow_message += "\"";
1479                         errorstream<<narrow_message<<std::endl;
1480                         error_message = narrow_to_wide(narrow_message);
1481                 }
1482 #endif
1483
1484                 // If no main menu, show error and exit
1485                 if(skip_main_menu)
1486                 {
1487                         if(error_message != L""){
1488                                 verbosestream<<"error_message = "
1489                                                 <<wide_to_narrow(error_message)<<std::endl;
1490                                 retval = 1;
1491                         }
1492                         break;
1493                 }
1494         } // Menu-game loop
1495         
1496         delete input;
1497
1498         /*
1499                 In the end, delete the Irrlicht device.
1500         */
1501         device->drop();
1502
1503 #endif // !SERVER
1504         
1505         // Update configuration file
1506         if(configpath != "")
1507                 g_settings->updateConfigFile(configpath.c_str());
1508
1509         END_DEBUG_EXCEPTION_HANDLER(errorstream)
1510         
1511         debugstreams_deinit();
1512         
1513         return retval;
1514 }
1515
1516 //END
1517