* "Outsourced" cheats from GameSession to scripting system.
[supertux.git] / src / sector.cpp
index a710145..93a0858 100644 (file)
@@ -1,7 +1,7 @@
 //  $Id$
 //
 //  SuperTux -  A Jump'n Run
-//  Copyright (C) 2004 Matthias Braun <matze@braunis.de
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
 //  This program is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU General Public License
 #include "script_manager.hpp"
 #include "scripting/wrapper_util.hpp"
 #include "script_interface.hpp"
-#include "msg.hpp"
+#include "log.hpp"
 
 Sector* Sector::_current = 0;
 
+bool Sector::show_collrects = false;
+bool Sector::draw_solids_only = false;
+
 Sector::Sector()
   : currentmusic(LEVEL_MUSIC), gravity(10),
     player(0), solids(0), camera(0)
@@ -76,13 +79,13 @@ Sector::Sector()
   add_object(new TextObject());
 
 #ifdef USE_GRID
-  grid = new CollisionGrid(32000, 32000);
-#else
-  grid = 0;
+  grid.reset(new CollisionGrid(32000, 32000));
 #endif
 
+  script_manager.reset(new ScriptManager(ScriptManager::instance));
+
   // create a new squirrel table for the sector
-  HSQUIRRELVM vm = script_manager->get_global_vm();
+  HSQUIRRELVM vm = ScriptManager::instance->get_vm();
   
   sq_newtable(vm);
   sq_pushroottable(vm);
@@ -98,13 +101,14 @@ Sector::Sector()
 
 Sector::~Sector()
 {
+  deactivate();
+  
+  script_manager.reset(NULL);
+  sq_release(ScriptManager::instance->get_vm(), &sector_table);
   update_game_objects();
   assert(gameobjects_new.size() == 0);
 
-  deactivate();
-
-  delete grid;
-
   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
       ++i) {
     before_object_remove(*i);
@@ -114,8 +118,6 @@ Sector::~Sector()
   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
       ++i)
     delete *i;
-
-  sq_release(script_manager->get_global_vm(), &sector_table);
 }
 
 GameObject*
@@ -152,7 +154,7 @@ Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
   try {
     return create_object(name, reader);
   } catch(std::exception& e) {
-    msg_warning << e.what() << "" << std::endl;
+    log_warning << e.what() << "" << std::endl;
   }
   
   return 0;
@@ -190,7 +192,7 @@ Sector::parse(const lisp::Lisp& sector)
 
   fix_old_tiles();
   if(!camera) {
-    msg_warning << "sector '" << name << "' does not contain a camera." << std::endl;
+    log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
     update_game_objects();
     add_object(new Camera(this));
   }
@@ -298,7 +300,7 @@ Sector::parse_old_format(const lisp::Lisp& reader)
           spawnpoints.push_back(sp);
           }
       } else {
-        msg_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
+        log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
       }
     }
   }
@@ -312,7 +314,7 @@ Sector::parse_old_format(const lisp::Lisp& reader)
       if(object) {
         add_object(object);
       } else {
-        msg_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
+        log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
       }
     }
   }
@@ -390,9 +392,13 @@ Sector::write(lisp::Writer& writer)
 HSQUIRRELVM
 Sector::run_script(std::istream& in, const std::string& sourcename)
 {
+  // create new thread and keep a weakref
   HSQUIRRELVM vm = script_manager->create_thread();
+
+  // set sector_table as roottable for the thread
   sq_pushobject(vm, sector_table);
   sq_setroottable(vm);
+
   Scripting::compile_and_run(vm, in, sourcename);
 
   return vm;
@@ -432,7 +438,7 @@ Sector::activate(const std::string& spawnpoint)
     }
   }                                                                           
   if(!sp) {
-    msg_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
+    log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
     if(spawnpoint != "main") {
       activate("main");
     } else {
@@ -452,12 +458,12 @@ Sector::activate(const Vector& player_pos)
     _current = this;
 
     // register sectortable as current_sector in scripting
-    HSQUIRRELVM vm = script_manager->get_global_vm();
+    HSQUIRRELVM vm = ScriptManager::instance->get_vm();
     sq_pushroottable(vm);
-    sq_pushstring(vm, "current_sector", -1);
+    sq_pushstring(vm, "sector", -1);
     sq_pushobject(vm, sector_table);
     if(SQ_FAILED(sq_createslot(vm, -3)))
-      throw Scripting::SquirrelError(vm, "Couldn't set current_sector in roottable");
+      throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
     sq_pop(vm, 1);
 
     for(GameObjects::iterator i = gameobjects.begin();
@@ -470,6 +476,7 @@ Sector::activate(const Vector& player_pos)
 
   player->move(player_pos);
   camera->reset(player->get_pos());
+  update_game_objects();
 
   // Run init script
   if(init_script != "") {
@@ -484,11 +491,12 @@ Sector::deactivate()
   if(_current != this)
     return;
 
-  HSQUIRRELVM vm = script_manager->get_global_vm();
+  // remove sector entry from global vm
+  HSQUIRRELVM vm = ScriptManager::instance->get_vm();
   sq_pushroottable(vm);
-  sq_pushstring(vm, "current_sector", -1);
+  sq_pushstring(vm, "sector", -1);
   if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
-    throw Scripting::SquirrelError(vm, "Couldn't unset current_sector in roottable");
+    throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
   sq_pop(vm, 1);
   
   for(GameObjects::iterator i = gameobjects.begin();
@@ -512,6 +520,8 @@ Sector::get_active_region()
 void
 Sector::update(float elapsed_time)
 {
+  script_manager->update();
+
   player->check_bounds(camera);
 
 #if 0
@@ -615,14 +625,14 @@ Sector::before_object_add(GameObject* object)
     if(solids == 0) {
       solids = tilemap;
     } else {
-      msg_warning << "Another solid tilemaps added. Ignoring" << std::endl;
+      log_warning << "Another solid tilemaps added. Ignoring" << std::endl;
     }
   }
 
   Camera* camera = dynamic_cast<Camera*> (object);
   if(camera) {
     if(this->camera != 0) {
-      msg_warning << "Multiple cameras added. Ignoring" << std::endl;
+      log_warning << "Multiple cameras added. Ignoring" << std::endl;
       return false;
     }
     this->camera = camera;
@@ -631,7 +641,7 @@ Sector::before_object_add(GameObject* object)
   Player* player = dynamic_cast<Player*> (object);
   if(player) {
     if(this->player != 0) {
-      msg_warning << "Multiple players added. Ignoring" << std::endl;
+      log_warning << "Multiple players added. Ignoring" << std::endl;
       return false;
     }
     this->player = player;
@@ -649,7 +659,7 @@ Sector::try_expose(GameObject* object)
 {
   ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
   if(interface != NULL) {
-    HSQUIRRELVM vm = script_manager->get_global_vm();
+    HSQUIRRELVM vm = script_manager->get_vm();
     sq_pushobject(vm, sector_table);
     interface->expose(vm, -1);
     sq_pop(vm, 1);
@@ -668,10 +678,15 @@ Sector::try_unexpose(GameObject* object)
 {
   ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
   if(interface != NULL) {
-    HSQUIRRELVM vm = script_manager->get_global_vm();
+    HSQUIRRELVM vm = script_manager->get_vm();
+    int oldtop = sq_gettop(vm);
     sq_pushobject(vm, sector_table);
-    interface->unexpose(vm, -1);
-    sq_pop(vm, 1);
+    try {
+      interface->unexpose(vm, -1);
+    } catch(std::exception& e) {
+      log_warning << "Couldn't unregister object: " << e.what() << std::endl;
+    }
+    sq_settop(vm, oldtop);
   }
 } 
 
@@ -686,10 +701,28 @@ Sector::draw(DrawingContext& context)
     GameObject* object = *i; 
     if(!object->is_valid())
       continue;
-    
+
+    if (draw_solids_only)
+    {
+      TileMap* tm = dynamic_cast<TileMap*>(object);
+      if (tm && !tm->is_solid())
+        continue;
+    }
+
     object->draw(context);
   }
 
+  if(show_collrects) {
+    Color col(0.2, 0.2, 0.2, 0.7);
+    for(MovingObjects::iterator i = moving_objects.begin();
+            i != moving_objects.end(); ++i) {
+      MovingObject* object = *i;
+      const Rect& rect = object->get_bbox();
+
+      context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
+    }
+  }
+
   context.pop_transform();
 }
 
@@ -1100,6 +1133,9 @@ Sector::play_music(MusicType type)
     case HERRING_MUSIC:
       sound_manager->play_music("music/salcon.ogg");
       break;
+    case HERRING_WARNING_MUSIC:
+      sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
+      break;
     default:
       sound_manager->play_music("");
       break;