/* Minetest Copyright (C) 2010-2017 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #ifndef SERVER_ENVIRONMENT_HEADER #define SERVER_ENVIRONMENT_HEADER #include "environment.h" #include "mapnode.h" #include "mapblock.h" #include class IGameDef; class ServerMap; struct GameParams; class RemotePlayer; class PlayerDatabase; class PlayerSAO; class ServerEnvironment; class ActiveBlockModifier; class ServerActiveObject; class Server; class ServerScripting; /* {Active, Loading} block modifier interface. These are fed into ServerEnvironment at initialization time; ServerEnvironment handles deleting them. */ class ActiveBlockModifier { public: ActiveBlockModifier(){}; virtual ~ActiveBlockModifier(){}; // Set of contents to trigger on virtual const std::set &getTriggerContents() const = 0; // Set of required neighbors (trigger doesn't happen if none are found) // Empty = do not check neighbors virtual const std::set &getRequiredNeighbors() const = 0; // Trigger interval in seconds virtual float getTriggerInterval() = 0; // Random chance of (1 / return value), 0 is disallowed virtual u32 getTriggerChance() = 0; // Whether to modify chance to simulate time lost by an unnattended block virtual bool getSimpleCatchUp() = 0; // This is called usually at interval for 1/chance of the nodes virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){}; virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n, u32 active_object_count, u32 active_object_count_wider){}; }; struct ABMWithState { ActiveBlockModifier *abm; float timer; ABMWithState(ActiveBlockModifier *abm_); }; struct LoadingBlockModifierDef { // Set of contents to trigger on std::set trigger_contents; std::string name; bool run_at_every_load; virtual ~LoadingBlockModifierDef() {} virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){}; }; struct LBMContentMapping { typedef std::map > container_map; container_map map; std::vector lbm_list; // Needs to be separate method (not inside destructor), // because the LBMContentMapping may be copied and destructed // many times during operation in the lbm_lookup_map. void deleteContents(); void addLBM(LoadingBlockModifierDef *lbm_def, IGameDef *gamedef); const std::vector *lookup(content_t c) const; }; class LBMManager { public: LBMManager(): m_query_mode(false) {} ~LBMManager(); // Don't call this after loadIntroductionTimes() ran. void addLBMDef(LoadingBlockModifierDef *lbm_def); void loadIntroductionTimes(const std::string ×, IGameDef *gamedef, u32 now); // Don't call this before loadIntroductionTimes() ran. std::string createIntroductionTimesString(); // Don't call this before loadIntroductionTimes() ran. void applyLBMs(ServerEnvironment *env, MapBlock *block, u32 stamp); // Warning: do not make this std::unordered_map, order is relevant here typedef std::map lbm_lookup_map; private: // Once we set this to true, we can only query, // not modify bool m_query_mode; // For m_query_mode == false: // The key of the map is the LBM def's name. // TODO make this std::unordered_map std::map m_lbm_defs; // For m_query_mode == true: // The key of the map is the LBM def's first introduction time. lbm_lookup_map m_lbm_lookup; // Returns an iterator to the LBMs that were introduced // after the given time. This is guaranteed to return // valid values for everything lbm_lookup_map::const_iterator getLBMsIntroducedAfter(u32 time) { return m_lbm_lookup.lower_bound(time); } }; /* List of active blocks, used by ServerEnvironment */ class ActiveBlockList { public: void update(std::vector &active_positions, s16 radius, std::set &blocks_removed, std::set &blocks_added); bool contains(v3s16 p){ return (m_list.find(p) != m_list.end()); } void clear(){ m_list.clear(); } std::set m_list; std::set m_forceloaded_list; private: }; /* Operation mode for ServerEnvironment::clearObjects() */ enum ClearObjectsMode { // Load and go through every mapblock, clearing objects CLEAR_OBJECTS_MODE_FULL, // Clear objects immediately in loaded mapblocks; // clear objects in unloaded mapblocks only when the mapblocks are next activated. CLEAR_OBJECTS_MODE_QUICK, }; /* The server-side environment. This is not thread-safe. Server uses an environment mutex. */ typedef UNORDERED_MAP ActiveObjectMap; class ServerEnvironment : public Environment { public: ServerEnvironment(ServerMap *map, ServerScripting *scriptIface, Server *server, const std::string &path_world); ~ServerEnvironment(); Map & getMap(); ServerMap & getServerMap(); //TODO find way to remove this fct! ServerScripting* getScriptIface() { return m_script; } Server *getGameDef() { return m_server; } float getSendRecommendedInterval() { return m_recommended_send_interval; } void kickAllPlayers(AccessDeniedCode reason, const std::string &str_reason, bool reconnect); // Save players void saveLoadedPlayers(); void savePlayer(RemotePlayer *player); PlayerSAO *loadPlayer(RemotePlayer *player, bool *new_player, u16 peer_id, bool is_singleplayer); void addPlayer(RemotePlayer *player); void removePlayer(RemotePlayer *player); bool removePlayerFromDatabase(const std::string &name); /* Save and load time of day and game timer */ void saveMeta(); void loadMeta(); // to be called instead of loadMeta if // env_meta.txt doesn't exist (e.g. new world) void loadDefaultMeta(); u32 addParticleSpawner(float exptime); u32 addParticleSpawner(float exptime, u16 attached_id); void deleteParticleSpawner(u32 id, bool remove_from_object = true); /* External ActiveObject interface ------------------------------------------- */ ServerActiveObject* getActiveObject(u16 id); /* Add an active object to the environment. Environment handles deletion of object. Object may be deleted by environment immediately. If id of object is 0, assigns a free id to it. Returns the id of the object. GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "cpp_api/s_mainmenu.h" #include "cpp_api/s_internal.h" #include "common/c_converter.h" void ScriptApiMainMenu::setMainMenuData(MainMenuDataForScript *data) { SCRIPTAPI_PRECHECKHEADER lua_getglobal(L, "gamedata"); int gamedata_idx = lua_gettop(L); lua_pushstring(L, "errormessage"); if (!data->errormessage.empty()) { lua_pushstring(L, data->errormessage.c_str()); } else { lua_pushnil(L); } lua_settable(L, gamedata_idx); setboolfield(L, gamedata_idx, "reconnect_requested", data->reconnect_requested); lua_pop(L, 1); } void ScriptApiMainMenu::handleMainMenuEvent(std::string text) { SCRIPTAPI_PRECHECKHEADER int error_handler = PUSH_ERROR_HANDLER(L); // Get handler function lua_getglobal(L, "core"); lua_getfield(L, -1, "event_handler"); lua_remove(L, -2); // Remove core if (lua_isnil(L, -1)) { lua_pop(L, 1); // Pop event_handler return; } luaL_checktype(L, -1, LUA_TFUNCTION); // Call it lua_pushstring(L, text.c_str()); PCALL_RES(lua_pcall(L, 1, 0, error_handler)); lua_pop(L, 1