Changeset 7482 for code/trunk
- Timestamp:
- Sep 23, 2010, 11:12:22 AM (14 years ago)
- Location:
- code/trunk/src/modules/objects
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/modules/objects/Script.cc
r7474 r7482 1 1 2 /* 2 3 * ORXONOX - the hottest 3D action shooter ever to exist … … 47 48 CreateFactory(Script); 48 49 49 register MemberNetworkFunction(Script, execute);50 registerStaticNetworkFunction(Script::executeHelper); 50 51 51 52 // Initializing constants. … … 54 55 /*static*/ const int Script::INF = -1; 55 56 57 /*static*/ LuaState* Script::LUA_STATE = NULL; 58 56 59 /** 57 60 @brief … … 65 68 66 69 // Initialize variables. 67 this->luaState_ = NULL;68 70 this->remainingExecutions_ = Script::INF; 69 71 this->mode_ = ScriptMode::normal; … … 71 73 this->times_ = Script::INF; 72 74 this->needsGraphics_ = false; 73 74 this->counter_ = 0.0f;75 76 this->registerVariables();77 75 } 78 76 … … 83 81 Script::~Script() 84 82 { 85 if(this->isInitialized() && this->luaState_ != NULL)86 delete this->luaState_;83 //if(this->isInitialized() && this->luaState_ != NULL) 84 // delete this->luaState_; 87 85 } 88 86 … … 108 106 XMLPortEventSink(Script, BaseObject, "trigger", trigger, xmlelement, mode); 109 107 110 if(this->isOnLoad()) // If the object is onLoad the code is executed at once for all clients.108 if(this->isOnLoad()) // If the object is onLoad the code is executed at once for the server. 111 109 this->execute(0); 112 110 } … … 125 123 126 124 XMLPortEventState(Script, BaseObject, "trigger", trigger, xmlelement, mode); 127 }128 129 /**130 @brief131 Register the variables that need to be synchronized.132 */133 void Script::registerVariables(void)134 {135 registerVariable(code_, VariableDirection::ToClient);136 registerVariable(needsGraphics_, VariableDirection::ToClient);137 registerVariable(modeStr_, VariableDirection::ToClient, new NetworkCallback<Script>(this, &Script::modeChanged));138 }139 140 void Script::modeChanged(void)141 {142 this->setMode(this->modeStr_);143 }144 145 void Script::tick(float dt)146 {147 SUPER(Script, tick, dt);148 149 if(!this->clientCallbacks_.empty())150 {151 if(this->counter_ < 2.0f)152 {153 this->counter_ += dt;154 }155 else156 {157 for(std::vector<unsigned int>::iterator it = this->clientCallbacks_.begin(); it != this->clientCallbacks_.end(); it++)158 {159 this->execute(*it, true);160 }161 this->clientCallbacks_.clear();162 this->counter_ = 0.0f;163 }164 }165 125 } 166 126 … … 225 185 void Script::execute(unsigned int clientId, bool fromCallback) 226 186 { 227 if(GameMode::isServer()) 187 COUT(0) << "EXECUTE: " << Host::getPlayerID() << " | " << clientId << std::endl; //TODO: Remove. 188 // If this is the server or we're in standalone mode we check whether we still want to execute the code and if so decrease the number of remaining executions. 189 if(GameMode::isServer() || GameMode::isStandalone()) 228 190 { 229 191 // If the number of executions have been used up. … … 232 194 233 195 // Decrement the number of remaining executions. 196 //TODO: Mode such that this is consistent in any case. 234 197 if(this->times_ != Script::INF) 235 198 this->remainingExecutions_--; 236 199 } 237 200 201 // If this is either the standalone mode or we're on the client we want to be. 238 202 if(GameMode::isStandalone() || Host::getPlayerID() == clientId) 239 203 { 240 // If the code needs graphics to be executed but the GameMode doesn't show graphics the code isn't executed. 241 if(this->needsGraphics_ && !GameMode::showsGraphics()) 242 return; 243 244 if(this->mode_ == ScriptMode::normal) // If the mode is 'normal'. 245 CommandExecutor::execute(this->code_); 246 else if(this->mode_ == ScriptMode::lua) // If it's 'lua'. 247 { 248 if(this->luaState_ == NULL) 249 this->luaState_ = new LuaState(); 250 this->luaState_->doString(this->code_); 251 } 252 } 204 this->executeHelper(this->getCode(), this->getMode(), this->getNeedsGraphics()); 205 } 206 207 // If this is the server and we're not on the client we want to be. 253 208 if(!GameMode::isStandalone() && GameMode::isServer() && Host::getPlayerID() != clientId) 254 209 { 210 COUT(0) << "1" << std::endl; //TODO: Remove. 211 // If this is not the result of a clientConnected callback and we want to execute the code for all clients. 212 //TODO: In this case does the server get executed as well? 255 213 if(!fromCallback && this->isForAll()) 256 214 { … … 258 216 for(std::map<unsigned int, PlayerInfo*>::const_iterator it = clients.begin(); it != clients.end(); it++) 259 217 { 260 call MemberNetworkFunction(Script, execute, this->getObjectID(), it->first, it->first, false);218 callStaticNetworkFunction(Script::executeHelper, it->first, this->getCode(), this->getMode(), this->getNeedsGraphics()); 261 219 } 262 220 } 221 // Else we execute the code just for the specified client. 263 222 else 264 223 { 265 callMemberNetworkFunction(Script, execute, this->getObjectID(), clientId, clientId, false); 224 COUT(0) << "2" << std::endl; //TODO: Remove. 225 callStaticNetworkFunction(Script::executeHelper, clientId, this->getCode(), this->getMode(), this->getNeedsGraphics()); 266 226 } 267 227 } 268 228 } 269 229 230 /** 231 @brief 232 Helper method that is used to reach this Script object on other clients. 233 */ 234 /*static*/ void Script::executeHelper(const std::string& code, const std::string& mode, bool needsGraphics) 235 { 236 COUT(0) << "HELPER: " << code << " | " << mode << " | " << needsGraphics << std::endl; //TODO: Remove. 237 238 // If the code needs graphics to be executed but the GameMode doesn't show graphics the code isn't executed. 239 if(needsGraphics && !GameMode::showsGraphics()) 240 return; 241 242 if(mode == Script::NORMAL) // If the mode is 'normal'. 243 CommandExecutor::execute(code); 244 else if(mode == Script::LUA) // If it's 'lua'. 245 { 246 if(Script::LUA_STATE == NULL) 247 Script::LUA_STATE = new LuaState(); 248 Script::LUA_STATE->doString(code); 249 } 250 } 251 252 /** 253 @brief 254 Callback that is called when a new client has connected. 255 @param clientId 256 The clientId of the new client that has connected. 257 */ 270 258 void Script::clientConnected(unsigned int clientId) 271 259 { 260 // If this is the server and the Script is specified as being 'onLoad'. 272 261 if(!GameMode::isStandalone() && GameMode::isServer() && this->isOnLoad()) 273 262 { 274 if(clientId != 0) 275 //TODO: Do better. This is only a temporary fix. 276 this->clientCallbacks_.push_back(clientId); 263 if(clientId != 0) //TODO: Remove if not needed. 264 this->execute(clientId, true); 277 265 } 278 266 } … … 295 283 this->setMode(ScriptMode::lua); 296 284 this->modeStr_ = Script::LUA; 297 // Creates a new LuaState.298 if(this->luaState_ == NULL)299 this->luaState_ = new LuaState();300 285 } 301 286 else … … 305 290 this->modeStr_ = Script::NORMAL; 306 291 } 292 } 293 294 /** 295 @brief 296 Sets the mode to the mode specified in this->modeStr_. 297 This is used internally for networking purposes. 298 */ 299 void Script::modeChanged(void) 300 { 301 this->setMode(this->modeStr_); 307 302 } 308 303 -
code/trunk/src/modules/objects/Script.h
r7474 r7482 1 1 2 /* 2 3 * ORXONOX - the hottest 3D action shooter ever to exist … … 36 37 37 38 #include "core/BaseObject.h" 38 #include "tools/interfaces/Tickable.h"39 39 #include "network/synchronisable/Synchronisable.h" 40 40 #include "network/ClientConnectionListener.h" … … 83 83 Damian 'Mozork' Frick 84 84 */ 85 class _ObjectsExport Script : public BaseObject, public Synchronisable, public ClientConnectionListener , public Tickable85 class _ObjectsExport Script : public BaseObject, public Synchronisable, public ClientConnectionListener 86 86 { 87 87 public: … … 92 92 virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode); //!< Creates a port that can be used to channel events and react to them. 93 93 94 virtual void tick(float dt);95 96 94 bool trigger(bool triggered, BaseObject* trigger); //!< Is called when an event comes in trough the event port. 97 95 void execute(unsigned int clientId, bool fromCallback = false); //!< Executes the Scripts code for the input client, depending on the mode. 96 static void executeHelper(const std::string& code, const std::string& mode, bool needsGraphics); //!< Helper method that is used to reach this Script object on other clients. 98 97 99 98 /** … … 160 159 { return this->forAll_; } 161 160 162 virtual void clientConnected(unsigned int clientId); 161 virtual void clientConnected(unsigned int clientId); //!< Callback that is called when a new client has connected. 163 162 virtual void clientDisconnected(unsigned int clientid) {} 164 163 … … 171 170 std::string code_; //!< The code that is executed by this Script. 172 171 ScriptMode::Value mode_; //!< The mode the Script is in. Determines whether the code is executed the normal way or in lua. 172 std::string modeStr_; //!< The mode the Script is in, as a string. Is used for networking purposes. 173 173 bool onLoad_; //!< Whether the Scripts code is executed upon loading (creation) of this Script. 174 174 int times_; //!< The number of times the Scripts code is executed at the most. -1 denotes infinity. … … 176 176 bool forAll_; //!< Whether the code is executed for all players (in a multiplayer setup) or just for the one triggering the Script. 177 177 178 std::string modeStr_; 179 180 std::vector<unsigned int> clientCallbacks_; 181 float counter_; 182 183 LuaState* luaState_; //!< The LuaState to execute the code in lua. 178 static LuaState* LUA_STATE; //!< The LuaState to execute the code in lua. 184 179 int remainingExecutions_; //!< The number of remainign executions. -1 denotes infinity. 185 180 186 void registerVariables(void); 187 void modeChanged(); 181 void modeChanged(); //!< Sets the mode to the mode specified in this->modeStr_. 188 182 189 183 /**
Note: See TracChangeset
for help on using the changeset viewer.