remove dead_script_hint, add scripting capabilities to willowisp, willowisps can...
[supertux.git] / src / badguy / badguy.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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  02111-1307, USA.
19 #include <config.h>
20
21 #include "badguy.hpp"
22 #include "object/camera.hpp"
23 #include "object/tilemap.hpp"
24 #include "tile.hpp"
25 #include "statistics.hpp"
26 #include "game_session.hpp"
27 #include "log.hpp"
28 #include "level.hpp"
29 #include "object/bullet.hpp"
30 #include "main.hpp"
31 #include "object/particles.hpp"
32 #include "random_generator.hpp"
33
34 static const float SQUISH_TIME = 2;
35 static const float X_OFFSCREEN_DISTANCE = 1600;
36 static const float Y_OFFSCREEN_DISTANCE = 1200;
37
38 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer)
39   : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true),
40     dir(LEFT), start_dir(AUTO), frozen(false), ignited(false),
41     state(STATE_INIT), on_ground_flag(false)
42 {
43   start_position = bbox.p1;
44
45   sound_manager->preload("sounds/squish.wav");
46   sound_manager->preload("sounds/fall.wav");
47 }
48
49 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer)
50   : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true),
51     dir(direction), start_dir(direction), frozen(false), ignited(false),
52     state(STATE_INIT), on_ground_flag(false)
53 {
54   start_position = bbox.p1;
55
56   sound_manager->preload("sounds/squish.wav");
57   sound_manager->preload("sounds/fall.wav");
58 }
59
60 BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer)
61   : MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), start_dir(AUTO), frozen(false), ignited(false), state(STATE_INIT), on_ground_flag(false)
62 {
63   start_position = bbox.p1;
64
65   std::string dir_str = "auto";
66   reader.get("direction", dir_str);
67   start_dir = str2dir( dir_str );
68   dir = start_dir;
69
70   reader.get("dead-script", dead_script);
71
72   sound_manager->preload("sounds/squish.wav");
73   sound_manager->preload("sounds/fall.wav");
74 }
75
76 void
77 BadGuy::draw(DrawingContext& context)
78 {
79   if(!sprite)
80     return;
81   if(state == STATE_INIT || state == STATE_INACTIVE)
82     return;
83   if(state == STATE_FALLING) {
84     DrawingEffect old_effect = context.get_drawing_effect();
85     context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
86     sprite->draw(context, get_pos(), layer);
87     context.set_drawing_effect(old_effect);
88   } else {
89     sprite->draw(context, get_pos(), layer);
90   }
91 }
92
93 void
94 BadGuy::update(float elapsed_time)
95 {
96   if(!Sector::current()->inside(bbox)) {
97     remove_me();
98     return;
99   }
100   if(is_offscreen()) {
101     if (state == STATE_ACTIVE) deactivate();
102     set_state(STATE_INACTIVE);
103   }
104
105   switch(state) {
106     case STATE_ACTIVE:
107       active_update(elapsed_time);
108       break;
109     case STATE_INIT:
110     case STATE_INACTIVE:
111       inactive_update(elapsed_time);
112       try_activate();
113       break;
114     case STATE_SQUISHED:
115       if(state_timer.check()) {
116         remove_me();
117         break;
118       }
119       movement = physic.get_movement(elapsed_time);
120       break;
121     case STATE_FALLING:
122       movement = physic.get_movement(elapsed_time);
123       break;
124   }
125
126   on_ground_flag = false;
127 }
128
129 Direction
130 BadGuy::str2dir( std::string dir_str )
131 {
132   if( dir_str == "auto" )
133     return AUTO;
134   if( dir_str == "left" )
135     return LEFT;
136   if( dir_str == "right" )
137     return RIGHT;
138
139   //default to "auto"
140   log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
141   return AUTO;
142 }
143
144 void
145 BadGuy::activate()
146 {
147 }
148
149 void
150 BadGuy::deactivate()
151 {
152 }
153
154 void
155 BadGuy::write(lisp::Writer& )
156 {
157   log_warning << "tried to write out a generic badguy" << std::endl;
158 }
159
160 void
161 BadGuy::active_update(float elapsed_time)
162 {
163   movement = physic.get_movement(elapsed_time);
164 }
165
166 void
167 BadGuy::inactive_update(float )
168 {
169 }
170
171 void
172 BadGuy::collision_tile(uint32_t tile_attributes)
173 {
174   if(tile_attributes & Tile::HURTS) {
175     if (tile_attributes & Tile::FIRE) {
176       if (is_flammable()) ignite();
177     }
178     else if (tile_attributes & Tile::ICE) {
179       if (is_freezable()) freeze();
180     }
181     else {
182       kill_fall();
183     }
184   }
185 }
186
187 HitResponse
188 BadGuy::collision(GameObject& other, const CollisionHit& hit)
189 {
190   switch(state) {
191     case STATE_INIT:
192     case STATE_INACTIVE:
193       return ABORT_MOVE;
194     case STATE_ACTIVE: {
195       BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
196       if(badguy && badguy->state == STATE_ACTIVE && badguy->get_group() == COLGROUP_MOVING) {
197
198         // hit from above?
199         if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
200           if(collision_squished(*badguy)) {
201             return ABORT_MOVE;
202           }
203         }
204
205         return collision_badguy(*badguy, hit);
206       }
207
208       Player* player = dynamic_cast<Player*> (&other);
209       if(player) {
210
211         // hit from above?
212         if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
213           if(collision_squished(*player)) {
214             return ABORT_MOVE;
215           }
216         }
217
218         return collision_player(*player, hit);
219       }
220
221       Bullet* bullet = dynamic_cast<Bullet*> (&other);
222       if(bullet)
223         return collision_bullet(*bullet, hit);
224
225       return FORCE_MOVE;
226     }
227     case STATE_SQUISHED:
228       return FORCE_MOVE;
229     case STATE_FALLING:
230       return FORCE_MOVE;
231   }
232
233   return ABORT_MOVE;
234 }
235
236 void
237 BadGuy::collision_solid(const CollisionHit& hit)
238 {
239   physic.set_velocity_x(0);
240   physic.set_velocity_y(0);
241   update_on_ground_flag(hit);
242 }
243
244 HitResponse
245 BadGuy::collision_player(Player& player, const CollisionHit& )
246 {
247   if(player.is_invincible()) {
248     kill_fall();
249     return ABORT_MOVE;
250   }
251
252   if(frozen)
253     unfreeze();
254   player.kill(false);
255   return FORCE_MOVE;
256 }
257
258 HitResponse
259 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
260 {
261   return FORCE_MOVE;
262 }
263
264 bool
265 BadGuy::collision_squished(GameObject& )
266 {
267   return false;
268 }
269
270 HitResponse
271 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
272 {
273   if (is_frozen()) {
274     if(bullet.get_type() == FIRE_BONUS) {
275       // fire bullet thaws frozen badguys
276       unfreeze();
277       bullet.remove_me();
278       return ABORT_MOVE;
279     } else {
280       // other bullets ricochet
281       bullet.ricochet(*this, hit);
282       return FORCE_MOVE;
283     }
284   }
285   else if (is_ignited()) {
286     if(bullet.get_type() == ICE_BONUS) {
287       // ice bullets extinguish ignited badguys
288       extinguish();
289       bullet.remove_me();
290       return ABORT_MOVE;
291     } else {
292       // other bullets are absorbed by ignited badguys
293       bullet.remove_me();
294       return FORCE_MOVE;
295     }
296   }
297   else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
298     // fire bullets ignite flammable badguys
299     ignite();
300     bullet.remove_me();
301     return ABORT_MOVE;
302   }
303   else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
304     // ice bullets freeze freezable badguys
305     freeze();
306     bullet.remove_me();
307     return ABORT_MOVE;
308   }
309   else {
310     // in all other cases, bullets ricochet
311     bullet.ricochet(*this, hit);
312     return FORCE_MOVE;
313   }
314 }
315
316 void
317 BadGuy::kill_squished(GameObject& object)
318 {
319   sound_manager->play("sounds/squish.wav", get_pos());
320   physic.enable_gravity(true);
321   physic.set_velocity_x(0);
322   physic.set_velocity_y(0);
323   set_state(STATE_SQUISHED);
324   set_group(COLGROUP_MOVING_ONLY_STATIC);
325   Player* player = dynamic_cast<Player*>(&object);
326   if (player) {
327     if (countMe) Sector::current()->get_level()->stats.badguys++;
328     player->bounce(*this);
329   }
330
331   // start dead-script
332   if(dead_script != "") {
333     std::istringstream stream(dead_script);
334     Sector::current()->run_script(stream, "dead-script");
335   }
336 }
337
338 void
339 BadGuy::kill_fall()
340 {
341   sound_manager->play("sounds/fall.wav", get_pos());
342   if (countMe) Sector::current()->get_level()->stats.badguys++;
343   physic.set_velocity_y(0);
344   physic.enable_gravity(true);
345   set_state(STATE_FALLING);
346
347   // start dead-script
348   if(dead_script != "") {
349     std::istringstream stream(dead_script);
350     Sector::current()->run_script(stream, "dead-script");
351   }
352 }
353
354 void
355 BadGuy::run_dead_script()
356 {
357    if (countMe)
358      Sector::current()->get_level()->stats.badguys++;
359    
360    // start dead-script
361   if(dead_script != "") {
362     std::istringstream stream(dead_script);
363     Sector::current()->run_script(stream, "dead-script");
364   }
365 }
366
367 void
368 BadGuy::set_state(State state)
369 {
370   if(this->state == state)
371     return;
372
373   State laststate = this->state;
374   this->state = state;
375   switch(state) {
376     case STATE_SQUISHED:
377       state_timer.start(SQUISH_TIME);
378       break;
379     case STATE_ACTIVE:
380       set_group(COLGROUP_MOVING);
381       bbox.set_pos(start_position);
382       break;
383     case STATE_INACTIVE:
384       // was the badguy dead anyway?
385       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
386         remove_me();
387       }
388       set_group(COLGROUP_DISABLED);
389       break;
390     case STATE_FALLING:
391       set_group(COLGROUP_DISABLED);
392       break;
393     default:
394       break;
395   }
396 }
397
398 bool
399 BadGuy::is_offscreen()
400 {
401   float scroll_x = Sector::current()->camera->get_translation().x;
402   float scroll_y = Sector::current()->camera->get_translation().y;
403
404   if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
405       || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE + SCREEN_WIDTH
406       || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
407       || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE + SCREEN_HEIGHT)
408     return true;
409
410   return false;
411 }
412
413 void
414 BadGuy::try_activate()
415 {
416   float scroll_x = Sector::current()->camera->get_translation().x;
417   float scroll_y = Sector::current()->camera->get_translation().y;
418
419   /* Activate badguys if they're just around the screen to avoid
420    * the effect of having badguys suddenly popping up from nowhere.
421    */
422   //Badguy left of screen
423   if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
424       start_position.x < scroll_x - bbox.get_width() &&
425       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
426       start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) {
427     if (start_dir != AUTO) dir = start_dir; else dir = RIGHT;
428     set_state(STATE_ACTIVE);
429     activate();
430   //Badguy right of screen
431   } else if (start_position.x > scroll_x +  SCREEN_WIDTH &&
432       start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
433       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
434       start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) {
435     if (start_dir != AUTO) dir = start_dir; else dir = LEFT;
436     set_state(STATE_ACTIVE);
437     activate();
438   //Badguy over or under screen
439   } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
440        start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
441        ((start_position.y > scroll_y + SCREEN_HEIGHT &&
442          start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) ||
443         (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
444          start_position.y < scroll_y - bbox.get_height()  ))) {
445      if (start_dir != AUTO) dir = start_dir;
446      else{
447          // if nearest player is to our right, start facing right
448          Player* player = get_nearest_player();
449          if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
450              dir = RIGHT;
451          } else {
452                  dir = LEFT;
453          }
454      }
455      set_state(STATE_ACTIVE);
456      activate();
457   } else if(state == STATE_INIT
458       && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
459       && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE + SCREEN_WIDTH
460       && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
461       && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE + SCREEN_HEIGHT ) {
462     if (start_dir != AUTO) {
463       dir = start_dir;
464     } else {
465       // if nearest player is to our right, start facing right
466       Player* player = get_nearest_player();
467       if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
468         dir = RIGHT;
469       } else {
470         dir = LEFT;
471       }
472     }
473     set_state(STATE_ACTIVE);
474     activate();
475   }
476 }
477
478 bool
479 BadGuy::might_fall(int height)
480 {
481   // make sure we check for at least a 1-pixel fall
482   assert(height > 0);
483
484   float x1;
485   float x2;
486   float y1 = bbox.p2.y + 1;
487   float y2 = bbox.p2.y + 1 + height;
488   if (dir == LEFT) {
489     x1 = bbox.p1.x - 1;
490     x2 = bbox.p1.x - 1;
491   } else {
492     x1 = bbox.p2.x + 1;
493     x2 = bbox.p2.x + 1;
494   }
495   return Sector::current()->is_free_of_statics(Rect(x1, y1, x2, y2));
496 }
497
498 Player*
499 BadGuy::get_nearest_player()
500 {
501   // FIXME: does not really return nearest player
502
503   std::vector<Player*> players = Sector::current()->get_players();
504   for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
505     Player* player = *playerIter;
506     return player;
507   }
508
509   return 0;
510 }
511
512 void
513 BadGuy::update_on_ground_flag(const CollisionHit& hit)
514 {
515   if (hit.bottom) {
516     on_ground_flag = true;
517     floor_normal = hit.slope_normal;
518   }
519 }
520
521 bool
522 BadGuy::on_ground()
523 {
524   return on_ground_flag;
525 }
526
527 Vector
528 BadGuy::get_floor_normal()
529 {
530   return floor_normal;
531 }
532
533 void
534 BadGuy::freeze()
535 {
536   set_group(COLGROUP_MOVING_STATIC);
537   frozen = true;
538 }
539
540 void
541 BadGuy::unfreeze()
542 {
543   set_group(COLGROUP_MOVING);
544   frozen = false;
545 }
546
547 bool
548 BadGuy::is_freezable() const
549 {
550   return false;
551 }
552
553 bool
554 BadGuy::is_frozen() const
555 {
556   return frozen;
557 }
558
559 void
560 BadGuy::ignite()
561 {
562   kill_fall();
563 }
564
565 void
566 BadGuy::extinguish()
567 {
568 }
569
570 bool
571 BadGuy::is_flammable() const
572 {
573   return true;
574 }
575
576 bool
577 BadGuy::is_ignited() const
578 {
579   return ignited;
580 }