/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * 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 General Public License for more details. * * You should have received a copy of the GNU 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. * * Author: * Benjamin Knecht * Co-authors: * Damian 'Mozork' Frick * */ #include "Script.h" #include "core/command/CommandExecutor.h" #include "core/CoreIncludes.h" #include "core/EventIncludes.h" #include "core/GameMode.h" #include "core/LuaState.h" #include "core/XMLPort.h" #include "network/Host.h" #include "network/NetworkFunction.h" #include "PlayerManager.h" #include "infos/PlayerInfo.h" #include "interfaces/PlayerTrigger.h" #include "worldentities/pawns/Pawn.h" namespace orxonox { CreateFactory(Script); registerMemberNetworkFunction(Script, execute); // Initializing constants. /*static*/ const std::string Script::NORMAL = "normal"; /*static*/ const std::string Script::LUA = "lua"; /*static*/ const int Script::INF = -1; /** @brief Constructor. Registers and initializes the object. @param creator The creator of this object. */ Script::Script(BaseObject* creator) : BaseObject(creator), Synchronisable(creator) { RegisterObject(Script); // Initialize variables. this->luaState_ = NULL; this->remainingExecutions_ = Script::INF; this->mode_ = ScriptMode::normal; this->onLoad_ = true; this->times_ = Script::INF; this->needsGraphics_ = false; this->counter_ = 0.0f; this->registerVariables(); } /** @brief Destructor. Cleans up. */ Script::~Script() { if(this->isInitialized() && this->luaState_ != NULL) delete this->luaState_; } /** @brief Method for creating a Script object through XML. @param xmlelement The element. @param mode The mode. */ void Script::XMLPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(Script, XMLPort, xmlelement, mode); XMLPortParam(Script, "code", setCode, getCode, xmlelement, mode); XMLPortParamTemplate(Script, "mode", setMode, getMode, xmlelement, mode, const std::string&).defaultValues(Script::NORMAL); XMLPortParam(Script, "onLoad", setOnLoad, isOnLoad, xmlelement, mode).defaultValues(true); XMLPortParam(Script, "times", setTimes, getTimes, xmlelement, mode).defaultValues(Script::INF); XMLPortParam(Script, "needsGraphics", setNeedsGraphics, getNeedsGraphics, xmlelement, mode).defaultValues(false); XMLPortParam(Script, "forAll", setForAll, isForAll, xmlelement, mode).defaultValues(false); XMLPortEventSink(Script, BaseObject, "trigger", trigger, xmlelement, mode); if(this->isOnLoad()) // If the object is onLoad the code is executed at once for all clients. this->execute(0); } /** @brief Creates a port that can be used to channel events and react to them. @param xmlelement The element. @param mode The mode. */ void Script::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(Script, XMLEventPort, xmlelement, mode); XMLPortEventState(Script, BaseObject, "trigger", trigger, xmlelement, mode); } /** @brief Register the variables that need to be synchronized. */ void Script::registerVariables(void) { registerVariable(code_, VariableDirection::ToClient); registerVariable(needsGraphics_, VariableDirection::ToClient); registerVariable(modeStr_, VariableDirection::ToClient, new NetworkCallback