* "Outsourced" cheats from GameSession to scripting system.
[supertux.git] / src / sector.cpp
index d0c4a95..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)
@@ -151,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;
@@ -189,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));
   }
@@ -297,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;
       }
     }
   }
@@ -311,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;
       }
     }
   }
@@ -435,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 {
@@ -473,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 != "") {
@@ -621,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;
@@ -637,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;
@@ -675,9 +679,14 @@ Sector::try_unexpose(GameObject* object)
   ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
   if(interface != NULL) {
     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);
   }
 } 
 
@@ -692,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();
 }