00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <cstdlib>
00023 #include <map>
00024
00025 #include "scripting/script.hpp"
00026
00027 #include "game-server/resourcemanager.hpp"
00028 #include "utils/logger.h"
00029
00030 typedef std::map< std::string, Script::Factory > Engines;
00031
00032 static Engines *engines = NULL;
00033
00034 Script::Script():
00035 mMap(NULL),
00036 mEventListener(&scriptEventDispatch)
00037 {}
00038
00039 void Script::registerEngine(const std::string &name, Factory f)
00040 {
00041 if (!engines)
00042 {
00043
00044
00045
00046
00047 engines = new Engines;
00048 }
00049 (*engines)[name] = f;
00050 }
00051
00052 Script *Script::create(const std::string &engine)
00053 {
00054 if (engines)
00055 {
00056 Engines::const_iterator i = engines->find(engine);
00057 if (i != engines->end())
00058 {
00059 return i->second();
00060 }
00061 }
00062 LOG_ERROR("No scripting engine named " << engine);
00063 return NULL;
00064 }
00065
00066 void Script::update()
00067 {
00068 prepare("update");
00069 execute();
00070 }
00071
00072 bool Script::loadFile(const std::string &name)
00073 {
00074 int size;
00075 char *buffer = ResourceManager::loadFile(name, size);
00076 if (buffer)
00077 {
00078 mScriptFile = name;
00079 load(buffer);
00080 free(buffer);
00081 return true;
00082 } else {
00083 return false;
00084 }
00085 }
00086
00087 void Script::loadNPC(const std::string &name, int id, int x, int y,
00088 const char *prog)
00089 {
00090 load(prog);
00091 prepare("create_npc_delayed");
00092 push(name);
00093 push(id);
00094 push(x);
00095 push(y);
00096 execute();
00097 }