Patch by Matt McCutchen to prevent division by zero when Tux spawns exactly at a...
[supertux.git] / src / badguy / willowisp.cpp
1 //  $Id$
2 //
3 //  SuperTux - "Will-O-Wisp" Badguy
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "willowisp.hpp"
23
24 #include "log.hpp"
25 #include "game_session.hpp"
26 #include "object/lantern.hpp"
27 #include "object/player.hpp"
28 #include "scripting/squirrel_util.hpp"
29 #include "object/path.hpp"
30 #include "object/path_walker.hpp"
31 #include "audio/sound_source.hpp"
32 #include "lisp/writer.hpp"
33 #include "object_factory.hpp"
34 #include "audio/sound_manager.hpp"
35 #include "sector.hpp"
36 #include "sprite/sprite.hpp"
37
38 static const float FLYSPEED = 64; /**< speed in px per second */
39 static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */
40 static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */
41 static const std::string SOUNDFILE = "sounds/willowisp.wav";
42
43 WillOWisp::WillOWisp(const lisp::Lisp& reader)
44   : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main")
45 {
46   bool running = false;
47   flyspeed     = FLYSPEED;
48   track_range  = TRACK_RANGE;
49   vanish_range = VANISH_RANGE;
50
51   reader.get("sector", target_sector);
52   reader.get("spawnpoint", target_spawnpoint);
53   reader.get("name", name);
54   reader.get("flyspeed", flyspeed);
55   reader.get("track-range", track_range);
56   reader.get("vanish-range", vanish_range);
57   reader.get("hit-script", hit_script);
58   reader.get("running", running);
59
60   const lisp::Lisp* pathLisp = reader.get_lisp("path");
61   if(pathLisp != NULL) {
62     path.reset(new Path());
63     path->read(*pathLisp);
64     walker.reset(new PathWalker(path.get(), running));
65     if(running)
66       mystate = STATE_PATHMOVING_TRACK;
67   }
68
69   countMe = false;
70   sound_manager->preload(SOUNDFILE);
71   sound_manager->preload("sounds/warp.wav");
72
73   sprite->set_action("idle");
74 }
75
76 void
77 WillOWisp::draw(DrawingContext& context)
78 {
79   sprite->draw(context, get_pos(), layer);
80
81   context.push_target();
82   context.set_target(DrawingContext::LIGHTMAP);
83
84   sprite->draw(context, get_pos(), layer);
85
86   context.pop_target();
87 }
88
89 void
90 WillOWisp::active_update(float elapsed_time)
91 {
92   Player* player = get_nearest_player();
93   if (!player) return;
94   Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
95   Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
96   Vector dist = (p2 - p1);
97
98   switch(mystate) {
99   case STATE_STOPPED:
100     break;
101
102   case STATE_IDLE:
103     if (dist.norm() <= track_range) {
104       mystate = STATE_TRACKING;
105     }
106     break;
107
108   case STATE_TRACKING:
109     if (dist.norm() > vanish_range) {
110       vanish();
111     } else if (dist.norm() >= 1) {
112       Vector dir = dist.unit();
113       movement = dir * elapsed_time * flyspeed;
114     } else {
115       /* We somehow landed right on top of the player without colliding.
116        * Sit tight and avoid a division by zero. */
117     }
118     sound_source->set_position(get_pos());
119     break;
120
121   case STATE_WARPING:
122     if(sprite->animation_done()) {
123       remove_me();
124     }
125
126   case STATE_VANISHING: {
127     Vector dir = dist.unit();
128     movement = dir * elapsed_time * flyspeed;
129     if(sprite->animation_done()) {
130       remove_me();
131     }
132     break;
133   }
134
135   case STATE_PATHMOVING:
136   case STATE_PATHMOVING_TRACK:
137     if(walker.get() == NULL)
138       return;
139     movement = walker->advance(elapsed_time) - get_pos();
140     if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
141       mystate = STATE_TRACKING;
142     }
143     break;
144
145   default:
146     assert(false);
147   }
148 }
149
150 void
151 WillOWisp::activate()
152 {
153   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
154   sound_source->set_position(get_pos());
155   sound_source->set_looping(true);
156   sound_source->set_gain(2.0);
157   sound_source->set_reference_distance(32);
158   sound_source->play();
159 }
160
161 void
162 WillOWisp::deactivate()
163 {
164   sound_source.reset(NULL);
165
166   switch (mystate) {
167     case STATE_STOPPED:
168     case STATE_IDLE:
169     case STATE_PATHMOVING:
170     case STATE_PATHMOVING_TRACK:
171       break;
172     case STATE_TRACKING:
173       mystate = STATE_IDLE;
174       break;
175     case STATE_WARPING:
176     case STATE_VANISHING:
177       remove_me();
178       break;
179   }
180 }
181
182 void
183 WillOWisp::vanish()
184 {
185   mystate = STATE_VANISHING;
186   sprite->set_action("vanishing", 1);
187   set_colgroup_active(COLGROUP_DISABLED);
188 }
189
190 bool
191 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
192   Lantern* lantern = dynamic_cast<Lantern*>(&other);
193
194   if (lantern && lantern->is_open())
195     return true;
196
197   if (dynamic_cast<Player*>(&other))
198     return true;
199
200   return false;
201 }
202
203 HitResponse
204 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
205   if(player.is_invincible())
206     return ABORT_MOVE;
207
208   if (mystate != STATE_TRACKING)
209     return ABORT_MOVE;
210
211   mystate = STATE_WARPING;
212   sprite->set_action("warping", 1);
213
214   if(hit_script != "") {
215     std::istringstream stream(hit_script);
216     Sector::current()->run_script(stream, "hit-script");
217   } else {
218     GameSession::current()->respawn(target_sector, target_spawnpoint);
219   }
220   sound_manager->play("sounds/warp.wav");
221
222   return CONTINUE;
223 }
224
225 void
226 WillOWisp::goto_node(int node_no)
227 {
228   walker->goto_node(node_no);
229   if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
230     mystate = STATE_PATHMOVING;
231   }
232 }
233
234 void
235 WillOWisp::start_moving()
236 {
237   walker->start_moving();
238 }
239
240 void
241 WillOWisp::stop_moving()
242 {
243   walker->stop_moving();
244 }
245
246 void
247 WillOWisp::set_state(const std::string& new_state)
248 {
249   if(new_state == "stopped") {
250     mystate = STATE_STOPPED;
251   } else if(new_state == "idle") {
252     mystate = STATE_IDLE;
253   } else if(new_state == "move_path") {
254     mystate = STATE_PATHMOVING;
255     walker->start_moving();
256   } else if(new_state == "move_path_track") {
257     mystate = STATE_PATHMOVING_TRACK;
258     walker->start_moving();
259   } else if(new_state == "normal") {
260     mystate = STATE_IDLE;
261   } else if(new_state == "vanish") {
262     vanish();
263   } else {
264     std::ostringstream msg;
265     msg << "Can't set unknown willowisp state '" << new_state << "', should "
266                 "be stopped, move_path, move_path_track or normal";
267     throw new std::runtime_error(msg.str());
268   }
269 }
270
271 void
272 WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
273 {
274   if (name.empty())
275     return;
276
277   std::cout << "Expose me '" << name << "'\n";
278   Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
279   expose_object(vm, table_idx, interface, name);
280 }
281   
282 void
283 WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
284 {
285   if (name.empty())
286     return;
287
288   std::cout << "UnExpose me '" << name << "'\n";
289   Scripting::unexpose_object(vm, table_idx, name);
290 }
291
292 IMPLEMENT_FACTORY(WillOWisp, "willowisp")