lots of cool scripting stuff
[supertux.git] / src / scripting / script_interpreter.cpp
index 91e3263..af17653 100644 (file)
 #include "object/text_object.hpp"
 #include "object/scripted_object.hpp"
 #include "object/display_effect.hpp"
+#include "object/player.hpp"
 #include "scripting/sound.hpp"
 #include "scripting/scripted_object.hpp"
 #include "scripting/display_effect.hpp"
+#include "scripting/squirrel_error.hpp"
 
 static void printfunc(HSQUIRRELVM, const char* str, ...)
 {
@@ -49,21 +51,21 @@ ScriptInterpreter::ScriptInterpreter(const std::string& new_working_directory)
   // register squirrel libs
   sq_pushroottable(v);
   if(sqstd_register_bloblib(v) < 0)
-    throw SquirrelError(v, "Couldn't register blob lib");
+    throw Scripting::SquirrelError(v, "Couldn't register blob lib");
   if(sqstd_register_iolib(v) < 0)
-    throw SquirrelError(v, "Couldn't register io lib");
+    throw Scripting::SquirrelError(v, "Couldn't register io lib");
   if(sqstd_register_systemlib(v) < 0)
-    throw SquirrelError(v, "Couldn't register system lib");
+    throw Scripting::SquirrelError(v, "Couldn't register system lib");
   if(sqstd_register_mathlib(v) < 0)
-    throw SquirrelError(v, "Couldn't register math lib");
+    throw Scripting::SquirrelError(v, "Couldn't register math lib");
   if(sqstd_register_stringlib(v) < 0)
-    throw SquirrelError(v, "Couldn't register string lib");
+    throw Scripting::SquirrelError(v, "Couldn't register string lib");
 
   // register print function
   sq_setprintfunc(v, printfunc);
   
   // register supertux API
-  SquirrelWrapper::register_supertux_wrapper(v);
+  Scripting::register_supertux_wrapper(v);
 
   // expose some "global" objects
   sound = new Scripting::Sound();
@@ -88,6 +90,7 @@ ScriptInterpreter::register_sector(Sector* sector)
     expose_object(scripted_object, scripted_object->get_name());
   }
   
+  expose_object(static_cast<Scripting::Player*> (sector->player), "Tux");
   TextObject* text_object = new TextObject();
   sector->add_object(text_object);
   Scripting::Text* text = static_cast<Scripting::Text*> (text_object);
@@ -123,12 +126,12 @@ ScriptInterpreter::run_script(std::istream& in, const std::string& sourcename,
   printf("Stackbefore:\n");
   print_squirrel_stack(v);
   if(sq_compile(v, squirrel_read_char, &in, sourcename.c_str(), true) < 0)
-    throw SquirrelError(v, "Couldn't parse script");
+    throw Scripting::SquirrelError(v, "Couldn't parse script");
  
   _current = this;
   sq_push(v, -2);
   if(sq_call(v, 1, false) < 0)
-    throw SquirrelError(v, "Couldn't start script");
+    throw Scripting::SquirrelError(v, "Couldn't start script");
   _current = 0;
   if(sq_getvmstate(v) != SQ_VMSTATE_SUSPENDED) {
     if(remove_when_terminated) {
@@ -156,7 +159,7 @@ ScriptInterpreter::update(float )
   
   _current = this;
   if(sq_wakeupvm(v, false, false) < 0)
-    throw SquirrelError(v, "Couldn't resume script");
+    throw Scripting::SquirrelError(v, "Couldn't resume script");
   _current = 0;
   if(sq_getvmstate(v) != SQ_VMSTATE_SUSPENDED) {
     printf("script ended...\n");
@@ -178,8 +181,18 @@ ScriptInterpreter::add_script_object(Sector* sector, const std::string& name,
     std::auto_ptr<ScriptInterpreter> interpreter(
                new ScriptInterpreter(workdir));
     interpreter->register_sector(sector);
+    
+    // load global default.nut file if it exists
+    //TODO: Load all .nut files from that directory
+    try {
+      std::string filename = "script/default.nut";
+      IFileStream in(filename);
+      interpreter->run_script(in, filename, false);
+    } catch(std::exception& e) {
+      // nothing
+    }
 
-    // load default.nut file if it exists
+    // load world-specific default.nut file if it exists
     try {
       std::string filename = workdir + "/default.nut";
       IFileStream in(filename);