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