Omnicleanup

TL;DR: Some guy did some stuff.

This started out as a small project to add a place where (C++) mod api functions can be written once and then used in both the in-game and the mainmenu API. Such as the set of setting_* functions, which were duplicated before, with a slightly different set of functionality.

To add this place (ModApiUtil), I added functions (luaapi_init_game, luaapi_init_mainmenu) where each API initializes the exact modules it needs. This could've been done differently, see this lengthy discussion: http://irc.minetest.ru/minetest-dev/2013-08-12#i_3258617

I also moved the mod API functiions that were in luaapi.cpp to new files (l_mapgen.cpp, l_rollback.cpp, l_server.cpp, l_util.cpp) to reduce luaapi.cpp to a single responsibility: register the required API modules.
Similarly, any stuff in scriptapi.cpp not strictly related to loading lua code was moved to s_server.cpp. Because the mainmenu was now using the ScriptApi engine in script/, it made sense to move guiLuaApi.cpp to script/lua_api/l_mainmenu.cpp and create a script/cpp_api/s_mainmenu.cpp.

It turned out that these changes slowed down compilation because these new source file needed to #include "server.h" (a huge file). Not to mention that any change to almost any header would trigger a rebuild of all the lua API modules. And so it began. I ended up modifying almost half the header and source files of minetest to reduce dependencies. This does reduce initial compliation time a bit, especially on slow computers, but most importantly reduces the amount of recompilation needed when making small changes. While I was doing all that, I took the opportunity to remove some dead code and clean up some small "TODO:" interface changes. I realize this will break some open pull requests, that's why I'm trying to as much of this as is possible at once. Thankfully in most files only the #include section had to be touched, which shouldn't make it too hard to rebase.

Now for the rundown. Feel free to skip this if you plan to read the entire diff... (If you plan to: what is wrong with you?)

Changes visible to players and modders:
- minetest.setting_setbool added to in-game API
- engine.debug, engine.log, engine.setting_save added to mainmenu API
- mainmenu.lua aliases print to engine.debug and calls math.randomseed, similar to builtin.lua
- GUIEngine no longer loads misc_helpers.lua automatically, this is done by a dofile in mainmenu.lua
- Lua errors in main menu callbacks are handled slightly differently, causing an error message dialog to be shown (like when a lua error occurs when starting a server)
- Minor differences in log messages emitted by main menu script loader
- Added setting main_menu_script to minetest.conf.example (and defaultsettings.cpp)

Performance changes:
- narrow_to_wide, wide_to_narrow, serialize{,Wide,Long}String, deSerialize{,Wide,Long}String aren't inline anymore and defined in the .cpp instead of the .h file, to allow util/string.h to not include util/pointer.h which contains some templates that can take long to compile. The same happened to serialize{,Wide,Long}String, deSerialize{,Wide,Long}String and a few other functions. I don't think any of it has a measurable performance impact.

Logistic changes in scriptapi/modapi:
- New files in script/lua_api: l_mainmenu.cpp/h, l_mapgen.cpp/h, l_rollback.cpp/h, l_server.cpp/h, l_util.cpp/h. Aside from l_mainmenu.cpp/h, these contain functions that were formerly in luaapi.cpp (ModApiBasic). l_server.cpp should be the place for utility functions that require a Server, l_util.cpp for ones that don't.
- luaapi.cpp now only contains two functions: luaapi_init_game and luaapi_init_mainmenu, responsible for registering all of the mod API modules available in either API.
- guiLuaApi has been renamed to ModApiMainMenu, and moved from guiLuaApi.cpp to script/lua_api/l_mainmenu.cpp.
- GUIEngine no longer manages a lua stack and loads scripts, it defers this stuff to ScriptApi
- Added the ability to store and load a pointer to the GUIEngine to ScriptApiBase and ModApiBase
- registerFunction, and by extension API_FCT returns void now, not bool.
- Reference classes (InvRef, LuaItemStack, LuaPerlinNoise, LuaPerlinNoiseMap, LuaPseudoRandom, LuaVoxelManip, NodeMetaRef, NodeTimerRef, ObjectRef) now derive from ModApiBase, therefore they can call getScriptApi, getServer, getEnv and getGuiEngine.
- es_BiomeTerrainType was in l_base.cpp for some reason, moved to l_mapgen.cpp
- Split parts of script/common/c_internal.h into script/cpp_api/s_internal.h and script/lua_api/l_internal.h.
- Removed macros PLAYER_TO_SA, ENV_TO_SA, SERVER_TO_SA, STACK_TO_SERVER, REGISTER_LUA_REF, LUA_STACK_AUTOLOCK, renamed LOCK_DEBUG to SCRIPTAPI_LOCK_DEBUG and LOCK_CHECK to SCRIPTAPI_LOCK_CHECK.
For convenience, PLAYER_TO_SA still exists in the implementation of inventorymanager.cpp.
- Removed get_scriptapi(), replaced by getScriptApi() in ModApiBase
- Because it is internal and shouldn't clutter the external header s_base.h (by way of the enum RunCallbacksMode), moved ScriptApiBase::runCallbacks() to c_internal.h. This required changing it back to a C-style function script_run_callbacks().
- Instead of storing the path to minetest.conf in the Server, store it in g_settings_path (main.h). This makes it possible to move l_settings_save to the Server-agnostic l_util.cpp
- Fixed parameter order of push_inventory_list and read_inventory_list, as requested by a // TODO!

Other logistic changes, mainly to optimize compiling speed:
- Changed irrlichttypes_extrabloated.h to irrlichttypes_bloated.h, irrlichttypes_bloated.h to irr_v3d.h etc., irrlichttypes_bloated.h to irrlichttypes.h where possible
- Removed lots of includes of stuff that was not needed, or could be solved with forward declarations. I made sure, using a custom script, that no struct was forward declared as class or vice versa, to please MSVC.
- Added explicit includes of stuff that was actually needed
- Moved several classes and globals from the .h to the .cpp (when they were only used in the latter), which in turn allowed removing more includes from the .h. Examples of this are in debug.cpp/h, emerge.cpp/h, server.cpp/h, ...
- Some things are pointers now instead of, er, not pointers, such as Server::m_banmanager. This allows using forward declarations.
- A few "Queue"s changed to std::list, reducing need to include util/container.h
- Moved EmergeThread from emerge.h to emerge.cpp, added a function EmergeManager::triggerAllThreads() to allow doing the former.
- Moved the single function makePacket_TOCLIENT_TIME_OF_DAY from clientserver.cpp to Server::SendTimeOfDay. Less cpp files, lower compile times.
- Removed some mistaken (by me) includes of <cassert> and replaced them with "debug.h".
- Moved PacketCounter from debug.h to client.h, where its only use is.
- Moved __NORETURN and __FUNCTION_NAME, which were only used in debug.h, from threads.h to debug.h
- Moved PEER_ID_INEXISTENT and PEER_ID_SERVER from connection.h to constants.h, so that some places can avoid include the relatively heavy connection.h.
- Removed unused WATER_LEVEL constant
- Removed unused Environment::printPlayers(std::ostream&)
- The IBackgroundBlockEmerger is now the EmergeManager, not the server, which made it possible to not include environment.h in server.h: good because server.h is heavily included, while environment.h includes lots of stuff.
- Moved drawItemStack from guiFormSpecMenu.cpp to hud.cpp
- hud_get_free_id(player) -> player.getFreeHudID(), allowing hud.h to be included without including player.h
- trim() moved from strfnd.h to util/string.h and optimized a bit, added tests for it
- Fixed doc/menu_lua_api.txt not ending in a newline
- Removed various stuff that had been commented out for a long time and is unlikely to return
- Fixed comment mentioning common mods above ModConfiguration::m_name_conflicts
- I may have missed some others

~~Stuff I don't like:~~
- ~~ScriptApi now subclasses _all_ of the script api modules, including modules that require the server and modules that require the GUIEngine, at all times. Sounds dirty, and it is. Suggestions welcome.~~



Update 2013-08-14:
I fixed the "Stuff I don't like". The unsquashed commit showing the changes: https://github.com/kahrl/minetest/commit/492a73eba05f70de3ea9d21f670cbe5d74817066

Summary:
- Instead of ScriptApi, there exist two classes GameScripting (in script/scripting_game.cpp) and MainMenuScripting (in script/scripting_mainmenu.cpp). These only subclass the appropriate ScriptApi classes.
- The functions luaapi_init_game and luaapi_init_mainmenu, which initialized the modapi modules, are now called GameScripting::InitializeModApi and MainMenuScripting::InitializeModApi, respectively.
- Mod execution and creation of the lua stack is now handled by ScriptApiBase, to avoid code duplication between GameScripting and MainMenuScripting
- ModApiBase::getScriptApi(L) is now ModApiBase::getScriptApiBase(L). To get the full scriptapi either do something like getServer(L)->getScriptIface() or call getScriptApi&lt;ScriptApiWhatever&gt;(L), which calls getScriptApiBase(L) and then dynamic_casts the result to ScriptApiWhatever*, throwing a LuaError if the cast fails.
- Fixed several CMakeLists.txt so that mainmenu-related source files aren't compiled for minetestserver anymore (note: they were compiled before, but had no effect because they had an #ifndef SERVER around them)