remove dead_script_hint, add scripting capabilities to willowisp, willowisps can...
authorMatthias Braun <matze@braunis.de>
Sun, 27 May 2007 10:01:09 +0000 (10:01 +0000)
committerMatthias Braun <matze@braunis.de>
Sun, 27 May 2007 10:01:09 +0000 (10:01 +0000)
SVN-Revision: 5057

src/badguy/badguy.cpp
src/badguy/badguy.hpp
src/badguy/willowisp.cpp
src/badguy/willowisp.hpp
src/badguy/yeti.cpp
src/lisp/lexer.cpp
src/scripting/willowisp.hpp [new file with mode: 0644]
src/scripting/wrapper.cpp
src/scripting/wrapper.hpp
src/scripting/wrapper.interface.hpp
src/sector.cpp

index 2d1b68e..19e8d8b 100644 (file)
@@ -36,7 +36,9 @@ static const float X_OFFSCREEN_DISTANCE = 1600;
 static const float Y_OFFSCREEN_DISTANCE = 1200;
 
 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer)
-  : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), start_dir(AUTO), frozen(false), ignited(false), draw_dead_script_hint(false), state(STATE_INIT), on_ground_flag(false)
+  : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true),
+    dir(LEFT), start_dir(AUTO), frozen(false), ignited(false),
+    state(STATE_INIT), on_ground_flag(false)
 {
   start_position = bbox.p1;
 
@@ -45,7 +47,9 @@ BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer)
 }
 
 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer)
-  : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(direction), start_dir(direction), frozen(false), ignited(false), draw_dead_script_hint(false), state(STATE_INIT), on_ground_flag(false)
+  : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true),
+    dir(direction), start_dir(direction), frozen(false), ignited(false),
+    state(STATE_INIT), on_ground_flag(false)
 {
   start_position = bbox.p1;
 
@@ -64,7 +68,6 @@ BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int lay
   dir = start_dir;
 
   reader.get("dead-script", dead_script);
-  draw_dead_script_hint = (dead_script != "");
 
   sound_manager->preload("sounds/squish.wav");
   sound_manager->preload("sounds/fall.wav");
@@ -84,11 +87,6 @@ BadGuy::draw(DrawingContext& context)
     context.set_drawing_effect(old_effect);
   } else {
     sprite->draw(context, get_pos(), layer);
-    if (draw_dead_script_hint) {
-      Vector ppos = Vector(systemRandom.randf(bbox.p1.x+8, bbox.p2.x-8), bbox.p2.y);
-      Vector pspeed = Vector(0, -100);
-      Sector::current()->add_object(new Particles(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.6f, .2f, .2f), 3, .1f, LAYER_OBJECTS+1));
-    }
   }
 }
 
@@ -356,7 +354,8 @@ BadGuy::kill_fall()
 void
 BadGuy::run_dead_script()
 {
-   if (countMe) Sector::current()->get_level()->stats.badguys++;
+   if (countMe)
+     Sector::current()->get_level()->stats.badguys++;
    
    // start dead-script
   if(dead_script != "") {
index f959286..6c18166 100644 (file)
@@ -229,7 +229,6 @@ protected:
   bool ignited; /**< true if this badguy is currently on fire */
 
   std::string dead_script; /**< script to execute when badguy is killed */
-  bool draw_dead_script_hint; /**< whether to draw a visual indication that this Badguy triggers a script */
 
 private:
   void try_activate();
index a4fecd8..22ec617 100644 (file)
@@ -24,6 +24,7 @@
 #include "game_session.hpp"
 #include "object/lantern.hpp"
 #include "object/player.hpp"
+#include "scripting/squirrel_util.hpp"
 
 static const float FLYSPEED = 64; /**< speed in px per second */
 static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */
@@ -33,27 +34,30 @@ static const std::string SOUNDFILE = "sounds/willowisp.wav";
 WillOWisp::WillOWisp(const lisp::Lisp& reader)
   : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main")
 {
+  flyspeed     = FLYSPEED;
+  track_range  = TRACK_RANGE;
+  vanish_range = VANISH_RANGE;
+
   reader.get("sector", target_sector);
   reader.get("spawnpoint", target_spawnpoint);
+  reader.get("name", name);
+  reader.get("flyspeed", flyspeed);
+  reader.get("track-range", track_range);
+  reader.get("vanish-range", vanish_range);
+  reader.get("hit-script", hit_script);
+
+  const lisp::Lisp* pathLisp = reader.get_lisp("path");
+  if(pathLisp != NULL) {
+    path.reset(new Path());
+       path->read(*pathLisp);
+       walker.reset(new PathWalker(path.get(), false));
+  }
 
   countMe = false;
   sound_manager->preload(SOUNDFILE);
 }
 
 void
-WillOWisp::write(lisp::Writer& writer)
-{
-  writer.start_list("willowisp");
-
-  writer.write_float("x", start_position.x);
-  writer.write_float("y", start_position.y);
-  writer.write_string("sector", target_sector);
-  writer.write_string("spawnpoint", target_spawnpoint);
-
-  writer.end_list("willowisp");
-}
-
-void
 WillOWisp::draw(DrawingContext& context)
 {
   sprite->draw(context, get_pos(), layer);
@@ -75,36 +79,53 @@ WillOWisp::active_update(float elapsed_time)
   Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
   Vector dist = (p2 - p1);
 
-  if (mystate == STATE_IDLE) {
-    if (dist.norm() <= TRACK_RANGE) {
+  switch(mystate) {
+  case STATE_STOPPED:
+    break;
+
+  case STATE_IDLE:
+    if (dist.norm() <= track_range) {
       mystate = STATE_TRACKING;
     }
-  }
+    break;
 
-  if (mystate == STATE_TRACKING) {
-    if (dist.norm() <= VANISH_RANGE) {
+  case STATE_TRACKING:
+    if (dist.norm() <= vanish_range) {
       Vector dir = dist.unit();
-      movement = dir*elapsed_time*FLYSPEED;
+      movement = dir * elapsed_time * flyspeed;
     } else {
       vanish();
     }
     sound_source->set_position(get_pos());
-  }
+    break;
 
-  if (mystate == STATE_WARPING) {
+  case STATE_WARPING:
     if(sprite->animation_done()) {
       remove_me();
     }
-  }
 
-  if (mystate == STATE_VANISHING) {
+  case STATE_VANISHING: {
     Vector dir = dist.unit();
-    movement = dir*elapsed_time*FLYSPEED;
+    movement = dir * elapsed_time * flyspeed;
     if(sprite->animation_done()) {
       remove_me();
     }
+    break;
   }
 
+  case STATE_PATHMOVING:
+  case STATE_PATHMOVING_TRACK:
+    if(walker.get() == NULL)
+      return;
+    movement = walker->advance(elapsed_time) - get_pos();
+    if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
+      mystate = STATE_TRACKING;
+    }
+    break;
+
+  default:
+    assert(false);
+  }
 }
 
 void
@@ -126,7 +147,10 @@ WillOWisp::deactivate()
   sound_source.reset(NULL);
 
   switch (mystate) {
+    case STATE_STOPPED:
     case STATE_IDLE:
+    case STATE_PATHMOVING:
+    case STATE_PATHMOVING_TRACK:
       break;
     case STATE_TRACKING:
       mystate = STATE_IDLE;
@@ -149,24 +173,90 @@ WillOWisp::vanish()
 bool
 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
   Lantern* lantern = dynamic_cast<Lantern*>(&other);
-  if (lantern && lantern->is_open()) return true;
-  if (dynamic_cast<Player*>(&other)) return true;
+
+  if (lantern && lantern->is_open())
+    return true;
+
+  if (dynamic_cast<Player*>(&other))
+    return true;
+
   return false;
 }
 
 HitResponse
 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
-  if(player.is_invincible()) return ABORT_MOVE;
+  if(player.is_invincible())
+    return ABORT_MOVE;
 
-  if (mystate != STATE_TRACKING) return ABORT_MOVE;
+  if (mystate != STATE_TRACKING)
+    return ABORT_MOVE;
 
   mystate = STATE_WARPING;
   sprite->set_action("warping", 1);
 
-  GameSession::current()->respawn(target_sector, target_spawnpoint);
+  if(hit_script != "") {
+    std::istringstream stream(hit_script);
+    Sector::current()->run_script(stream, "hit-script");
+  } else {
+    GameSession::current()->respawn(target_sector, target_spawnpoint);
+  }
   sound_manager->play("sounds/warp.wav");
 
   return CONTINUE;
 }
 
+void
+WillOWisp::goto_node(int node_no)
+{
+  walker->goto_node(node_no);
+  if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
+    mystate = STATE_PATHMOVING;
+    walker->start_moving();
+  }
+}
+
+void
+WillOWisp::set_state(const std::string& new_state)
+{
+  if(new_state == "stopped") {
+    mystate = STATE_STOPPED;
+  } else if(new_state == "idle") {
+    mystate = STATE_IDLE;
+  } else if(new_state == "move_path") {
+    mystate = STATE_PATHMOVING;
+    walker->start_moving();
+  } else if(new_state == "move_path_track") {
+    mystate = STATE_PATHMOVING_TRACK;
+    walker->start_moving();
+  } else if(new_state == "normal") {
+    mystate = STATE_IDLE;
+  } else {
+    std::ostringstream msg;
+    msg << "Can't set unknown willowisp state '" << new_state << "', should "
+               "be stopped, move_path, move_path_track or normal";
+    throw new std::runtime_error(msg.str());
+  }
+}
+
+void
+WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
+{
+  if (name.empty())
+    return;
+
+  std::cout << "Expose me '" << name << "'\n";
+  Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
+  expose_object(vm, table_idx, interface, name);
+}
+  
+void
+WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
+{
+  if (name.empty())
+    return;
+
+  std::cout << "UnExpose me '" << name << "'\n";
+  Scripting::unexpose_object(vm, table_idx, name);
+}
+
 IMPLEMENT_FACTORY(WillOWisp, "willowisp")
index c21933d..d9401c8 100644 (file)
 #define __WILLOWISP_H__
 
 #include "badguy.hpp"
+#include "object/path.hpp"
+#include "object/path_walker.hpp"
+#include "script_interface.hpp"
+#include "scripting/willowisp.hpp"
 
-class WillOWisp : public BadGuy
+class WillOWisp : public BadGuy, public Scripting::WillOWisp,
+                  public ScriptInterface
 {
 public:
   WillOWisp(const lisp::Lisp& reader);
@@ -31,7 +36,6 @@ public:
   void activate();
   void deactivate();
 
-  void write(lisp::Writer& write);
   void active_update(float elapsed_time);
   virtual bool is_flammable() const { return false; }
   virtual bool is_freezable() const { return false; }
@@ -44,20 +48,35 @@ public:
 
   virtual void draw(DrawingContext& context);
 
+  virtual void goto_node(int node_no);
+  virtual void set_state(const std::string& state);
+
+  virtual void expose(HSQUIRRELVM vm, SQInteger table_idx);
+  virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx);
+
 protected:
   virtual bool collides(GameObject& other, const CollisionHit& hit);
   HitResponse collision_player(Player& player, const CollisionHit& hit);
 
 private:
   enum MyState {
-    STATE_IDLE, STATE_TRACKING, STATE_VANISHING, STATE_WARPING
+    STATE_STOPPED, STATE_IDLE, STATE_TRACKING, STATE_VANISHING, STATE_WARPING,
+    STATE_PATHMOVING, STATE_PATHMOVING_TRACK
   };
   MyState mystate;
 
   std::string target_sector;
   std::string target_spawnpoint;
+  std::string hit_script;
 
   std::auto_ptr<SoundSource> sound_source;
+
+  std::auto_ptr<Path>        path;
+  std::auto_ptr<PathWalker>  walker;
+
+  float flyspeed;
+  float track_range;
+  float vanish_range;
 };
 
 #endif
index 5578d0d..7995925 100644 (file)
@@ -60,7 +60,6 @@ Yeti::Yeti(const lisp::Lisp& reader)
   sound_manager->preload("sounds/yeti_gna.wav");
   sound_manager->preload("sounds/yeti_roar.wav");
   hud_head.reset(new Surface("images/creatures/yeti/hudlife.png"));
-  draw_dead_script_hint = false;
 }
 
 Yeti::~Yeti()
index 4e4f2fb..c9daf4b 100644 (file)
@@ -130,7 +130,7 @@ Lexer::getNextToken()
         } catch(EOFException& ) {
           std::stringstream msg;
           msg << "Parse error in line " << startline << ": "
-            << "EOF while parsing string.";
+              << "EOF while parsing string.";
           throw std::runtime_error(msg.str());
         }
         nextChar();
diff --git a/src/scripting/willowisp.hpp b/src/scripting/willowisp.hpp
new file mode 100644 (file)
index 0000000..fb1be12
--- /dev/null
@@ -0,0 +1,48 @@
+//  $Id$
+//
+//  SuperTux
+//  Copyright (C) 2007 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
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+#ifndef __SCRIPTING_WILLOWISP_H__
+#define __SCRIPTING_WILLOWISP_H__
+
+namespace Scripting
+{
+
+class WillOWisp
+{
+public:
+#ifndef SCRIPTING_API
+  virtual ~WillOWisp()
+  {}
+#endif
+
+  /** Move willowisp to given node */
+  virtual void goto_node(int node_no) = 0;
+
+  /** set willowisp state can be:
+   * -stopped          willowisp doesn't move
+   * -move_path        willowisp moves along the path (call goto_node)
+   * -move_path_track  willowisp moves along path but catchs tux when he is near
+   * -normal           "normal" mode starts tracking tux when he is near enough
+   */
+  virtual void set_state(const std::string& state) = 0;
+};
+
+}
+
+#endif
index 91c0cda..a47c84e 100644 (file)
@@ -2895,6 +2895,71 @@ static SQInteger LevelTime_set_time_wrapper(HSQUIRRELVM vm)
 
 }
 
+static SQInteger WillOWisp_release_hook(SQUserPointer ptr, SQInteger )
+{
+  Scripting::WillOWisp* _this = reinterpret_cast<Scripting::WillOWisp*> (ptr);
+  delete _this;
+  return 0;
+}
+
+static SQInteger WillOWisp_goto_node_wrapper(HSQUIRRELVM vm)
+{
+  SQUserPointer data;
+  if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {
+    sq_throwerror(vm, _SC("'goto_node' called without instance"));
+    return SQ_ERROR;
+  }
+  Scripting::WillOWisp* _this = reinterpret_cast<Scripting::WillOWisp*> (data);
+  SQInteger arg0;
+  if(SQ_FAILED(sq_getinteger(vm, 2, &arg0))) {
+    sq_throwerror(vm, _SC("Argument 1 not an integer"));
+    return SQ_ERROR;
+  }
+
+  try {
+    _this->goto_node(static_cast<int> (arg0));
+
+    return 0;
+
+  } catch(std::exception& e) {
+    sq_throwerror(vm, e.what());
+    return SQ_ERROR;
+  } catch(...) {
+    sq_throwerror(vm, _SC("Unexpected exception while executing function 'goto_node'"));
+    return SQ_ERROR;
+  }
+
+}
+
+static SQInteger WillOWisp_set_state_wrapper(HSQUIRRELVM vm)
+{
+  SQUserPointer data;
+  if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {
+    sq_throwerror(vm, _SC("'set_state' called without instance"));
+    return SQ_ERROR;
+  }
+  Scripting::WillOWisp* _this = reinterpret_cast<Scripting::WillOWisp*> (data);
+  const SQChar* arg0;
+  if(SQ_FAILED(sq_getstring(vm, 2, &arg0))) {
+    sq_throwerror(vm, _SC("Argument 1 not a string"));
+    return SQ_ERROR;
+  }
+
+  try {
+    _this->set_state(arg0);
+
+    return 0;
+
+  } catch(std::exception& e) {
+    sq_throwerror(vm, e.what());
+    return SQ_ERROR;
+  } catch(...) {
+    sq_throwerror(vm, _SC("Unexpected exception while executing function 'set_state'"));
+    return SQ_ERROR;
+  }
+
+}
+
 static SQInteger display_wrapper(HSQUIRRELVM vm)
 {
   return Scripting::display(vm);
@@ -3912,6 +3977,32 @@ void create_squirrel_instance(HSQUIRRELVM v, Scripting::LevelTime* object, bool
   sq_remove(v, -2); // remove root table
 }
 
+void create_squirrel_instance(HSQUIRRELVM v, Scripting::WillOWisp* object, bool setup_releasehook)
+{
+  using namespace Wrapper;
+
+  sq_pushroottable(v);
+  sq_pushstring(v, "WillOWisp", -1);
+  if(SQ_FAILED(sq_get(v, -2))) {
+    std::ostringstream msg;
+    msg << "Couldn't resolved squirrel type 'WillOWisp'";
+    throw SquirrelError(v, msg.str());
+  }
+
+  if(SQ_FAILED(sq_createinstance(v, -1)) || SQ_FAILED(sq_setinstanceup(v, -1, object))) {
+    std::ostringstream msg;
+    msg << "Couldn't setup squirrel instance for object of type 'WillOWisp'";
+    throw SquirrelError(v, msg.str());
+  }
+  sq_remove(v, -2); // remove object name
+
+  if(setup_releasehook) {
+    sq_setreleasehook(v, -1, WillOWisp_release_hook);
+  }
+
+  sq_remove(v, -2); // remove root table
+}
+
 void register_supertux_wrapper(HSQUIRRELVM v)
 {
   using namespace Wrapper;
@@ -4927,6 +5018,29 @@ void register_supertux_wrapper(HSQUIRRELVM v)
     throw SquirrelError(v, "Couldn't register class 'LevelTime'");
   }
 
+  // Register class WillOWisp
+  sq_pushstring(v, "WillOWisp", -1);
+  if(sq_newclass(v, SQFalse) < 0) {
+    std::ostringstream msg;
+    msg << "Couldn't create new class 'WillOWisp'";
+    throw SquirrelError(v, msg.str());
+  }
+  sq_pushstring(v, "goto_node", -1);
+  sq_newclosure(v, &WillOWisp_goto_node_wrapper, 0);
+  if(SQ_FAILED(sq_createslot(v, -3))) {
+    throw SquirrelError(v, "Couldn't register function 'goto_node'");
+  }
+
+  sq_pushstring(v, "set_state", -1);
+  sq_newclosure(v, &WillOWisp_set_state_wrapper, 0);
+  if(SQ_FAILED(sq_createslot(v, -3))) {
+    throw SquirrelError(v, "Couldn't register function 'set_state'");
+  }
+
+  if(SQ_FAILED(sq_createslot(v, -3))) {
+    throw SquirrelError(v, "Couldn't register class 'WillOWisp'");
+  }
+
 }
 
 } // end of namespace Scripting
index cc6e43e..9deb6ef 100644 (file)
@@ -29,6 +29,7 @@ void create_squirrel_instance(HSQUIRRELVM v, Scripting::Thunderstorm* object, bo
 void create_squirrel_instance(HSQUIRRELVM v, Scripting::TileMap* object, bool setup_releasehook = false);
 void create_squirrel_instance(HSQUIRRELVM v, Scripting::SSector* object, bool setup_releasehook = false);
 void create_squirrel_instance(HSQUIRRELVM v, Scripting::LevelTime* object, bool setup_releasehook = false);
+void create_squirrel_instance(HSQUIRRELVM v, Scripting::WillOWisp* object, bool setup_releasehook = false);
 
 }
 
index c28ce59..d95dd67 100644 (file)
@@ -16,3 +16,4 @@
 #include "tilemap.hpp"
 #include "ssector.hpp"
 #include "level_time.hpp"
+#include "willowisp.hpp"
index aa95d7c..57418fd 100644 (file)
@@ -897,10 +897,10 @@ void check_collisions(collision::Constraints* constraints,
     return;
 
   // calculate intersection
-  float itop = r1.get_bottom() - r2.get_top();
+  float itop    = r1.get_bottom() - r2.get_top();
   float ibottom = r2.get_bottom() - r1.get_top();
-  float ileft = r1.get_right() - r2.get_left();
-  float iright = r2.get_right() - r1.get_left();
+  float ileft   = r1.get_right() - r2.get_left();
+  float iright  = r2.get_right() - r1.get_left();
 
   if(fabsf(movement.y) > fabsf(movement.x)) {
     if(ileft < SHIFT_DELTA) {