- fix a bunch of msvc warnings (mostly assigning double constants to float variables)
[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::set_state(State state)
358 {
359   if(this->state == state)
360     return;
361
362   State laststate = this->state;
363   this->state = state;
364   switch(state) {
365     case STATE_SQUISHED:
366       state_timer.start(SQUISH_TIME);
367       break;
368     case STATE_ACTIVE:
369       set_group(COLGROUP_MOVING);
370       bbox.set_pos(start_position);
371       break;
372     case STATE_INACTIVE:
373       // was the badguy dead anyway?
374       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
375         remove_me();
376       }
377       set_group(COLGROUP_DISABLED);
378       break;
379     case STATE_FALLING:
380       set_group(COLGROUP_DISABLED);
381       break;
382     default:
383       break;
384   }
385 }
386
387 bool
388 BadGuy::is_offscreen()
389 {
390   float scroll_x = Sector::current()->camera->get_translation().x;
391   float scroll_y = Sector::current()->camera->get_translation().y;
392
393   if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
394       || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE + SCREEN_WIDTH
395       || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
396       || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE + SCREEN_HEIGHT)
397     return true;
398
399   return false;
400 }
401
402 void
403 BadGuy::try_activate()
404 {
405   float scroll_x = Sector::current()->camera->get_translation().x;
406   float scroll_y = Sector::current()->camera->get_translation().y;
407
408   /* Activate badguys if they're just around the screen to avoid
409    * the effect of having badguys suddenly popping up from nowhere.
410    */
411   //Badguy left of screen
412   if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
413       start_position.x < scroll_x - bbox.get_width() &&
414       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
415       start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) {
416     if (start_dir != AUTO) dir = start_dir; else dir = RIGHT;
417     set_state(STATE_ACTIVE);
418     activate();
419   //Badguy right of screen
420   } else if (start_position.x > scroll_x +  SCREEN_WIDTH &&
421       start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
422       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
423       start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) {
424     if (start_dir != AUTO) dir = start_dir; else dir = LEFT;
425     set_state(STATE_ACTIVE);
426     activate();
427   //Badguy over or under screen
428   } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
429        start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
430        ((start_position.y > scroll_y + SCREEN_HEIGHT &&
431          start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) ||
432         (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
433          start_position.y < scroll_y - bbox.get_height()  ))) {
434      if (start_dir != AUTO) dir = start_dir;
435      else{
436          // if nearest player is to our right, start facing right
437          Player* player = get_nearest_player();
438          if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
439              dir = RIGHT;
440          } else {
441                  dir = LEFT;
442          }
443      }
444      set_state(STATE_ACTIVE);
445      activate();
446   } else if(state == STATE_INIT
447       && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
448       && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE + SCREEN_WIDTH
449       && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
450       && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE + SCREEN_HEIGHT ) {
451     if (start_dir != AUTO) {
452       dir = start_dir;
453     } else {
454       // if nearest player is to our right, start facing right
455       Player* player = get_nearest_player();
456       if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
457         dir = RIGHT;
458       } else {
459         dir = LEFT;
460       }
461     }
462     set_state(STATE_ACTIVE);
463     activate();
464   }
465 }
466
467 bool
468 BadGuy::might_fall(int height)
469 {
470   // make sure we check for at least a 1-pixel fall
471   assert(height > 0);
472
473   float x1;
474   float x2;
475   float y1 = bbox.p2.y + 1;
476   float y2 = bbox.p2.y + 1 + height;
477   if (dir == LEFT) {
478     x1 = bbox.p1.x - 1;
479     x2 = bbox.p1.x - 1;
480   } else {
481     x1 = bbox.p2.x + 1;
482     x2 = bbox.p2.x + 1;
483   }
484   return Sector::current()->is_free_of_statics(Rect(x1, y1, x2, y2));
485 }
486
487 Player*
488 BadGuy::get_nearest_player()
489 {
490   // FIXME: does not really return nearest player
491
492   std::vector<Player*> players = Sector::current()->get_players();
493   for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
494     Player* player = *playerIter;
495     return player;
496   }
497
498   return 0;
499 }
500
501 void
502 BadGuy::update_on_ground_flag(const CollisionHit& hit)
503 {
504   if (hit.bottom) {
505     on_ground_flag = true;
506     floor_normal = hit.slope_normal;
507   }
508 }
509
510 bool
511 BadGuy::on_ground()
512 {
513   return on_ground_flag;
514 }
515
516 Vector
517 BadGuy::get_floor_normal()
518 {
519   return floor_normal;
520 }
521
522 void
523 BadGuy::freeze()
524 {
525   set_group(COLGROUP_MOVING_STATIC);
526   frozen = true;
527 }
528
529 void
530 BadGuy::unfreeze()
531 {
532   set_group(COLGROUP_MOVING);
533   frozen = false;
534 }
535
536 bool
537 BadGuy::is_freezable() const
538 {
539   return false;
540 }
541
542 bool
543 BadGuy::is_frozen() const
544 {
545   return frozen;
546 }
547
548 void 
549 BadGuy::ignite() 
550 {
551   kill_fall();
552 }
553
554 void 
555 BadGuy::extinguish() 
556 {
557 }
558
559 bool 
560 BadGuy::is_flammable() const 
561 {
562   return true;
563 }
564
565 bool 
566 BadGuy::is_ignited() const 
567 {
568   return ignited;
569 }
570