Fix coverity issues (uninitialized members)
[supertux.git] / src / badguy / badguy.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/badguy.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "object/bullet.hpp"
21 #include "object/player.hpp"
22 #include "supertux/level.hpp"
23 #include "supertux/sector.hpp"
24 #include "supertux/tile.hpp"
25 #include "util/reader.hpp"
26
27 #include <math.h>
28 #include <sstream>
29
30 static const float SQUISH_TIME = 2;
31
32 static const float X_OFFSCREEN_DISTANCE = 1280;
33 static const float Y_OFFSCREEN_DISTANCE = 800;
34
35 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name_, int layer_) :
36   MovingSprite(pos, sprite_name_, layer_, COLGROUP_DISABLED),
37   physic(),
38   countMe(true),
39   is_initialized(false),
40   start_position(),
41   dir(LEFT),
42   start_dir(AUTO),
43   frozen(false),
44   ignited(false),
45   in_water(false),
46   dead_script(),
47   state(STATE_INIT),
48   is_active_flag(),
49   state_timer(),
50   on_ground_flag(false),
51   floor_normal(),
52   colgroup_active(COLGROUP_MOVING)
53 {
54   start_position = bbox.p1;
55
56   SoundManager::current()->preload("sounds/squish.wav");
57   SoundManager::current()->preload("sounds/fall.wav");
58   SoundManager::current()->preload("sounds/splash.ogg");
59
60   dir = (start_dir == AUTO) ? LEFT : start_dir;
61 }
62
63 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name_, int layer_) :
64   MovingSprite(pos, sprite_name_, layer_, COLGROUP_DISABLED),
65   physic(),
66   countMe(true),
67   is_initialized(false),
68   start_position(),
69   dir(direction),
70   start_dir(direction),
71   frozen(false),
72   ignited(false),
73   in_water(false),
74   dead_script(),
75   state(STATE_INIT),
76   is_active_flag(),
77   state_timer(),
78   on_ground_flag(false),
79   floor_normal(),
80   colgroup_active(COLGROUP_MOVING)
81 {
82   start_position = bbox.p1;
83
84   SoundManager::current()->preload("sounds/squish.wav");
85   SoundManager::current()->preload("sounds/fall.wav");
86   SoundManager::current()->preload("sounds/splash.ogg");
87
88   dir = (start_dir == AUTO) ? LEFT : start_dir;
89 }
90
91 BadGuy::BadGuy(const Reader& reader, const std::string& sprite_name_, int layer_) :
92   MovingSprite(reader, sprite_name_, layer_, COLGROUP_DISABLED),
93   physic(),
94   countMe(true),
95   is_initialized(false),
96   start_position(),
97   dir(LEFT),
98   start_dir(AUTO),
99   frozen(false),
100   ignited(false),
101   in_water(false),
102   dead_script(),
103   state(STATE_INIT),
104   is_active_flag(),
105   state_timer(),
106   on_ground_flag(false),
107   floor_normal(),
108   colgroup_active(COLGROUP_MOVING)
109 {
110   start_position = bbox.p1;
111
112   std::string dir_str = "auto";
113   reader.get("direction", dir_str);
114   start_dir = str2dir( dir_str );
115   dir = start_dir;
116
117   reader.get("dead-script", dead_script);
118
119   SoundManager::current()->preload("sounds/squish.wav");
120   SoundManager::current()->preload("sounds/fall.wav");
121   SoundManager::current()->preload("sounds/splash.ogg");
122
123   dir = (start_dir == AUTO) ? LEFT : start_dir;
124 }
125
126 void
127 BadGuy::draw(DrawingContext& context)
128 {
129   if(!sprite.get())
130     return;
131   if(state == STATE_INIT || state == STATE_INACTIVE)
132     return;
133   if(state == STATE_FALLING) {
134     DrawingEffect old_effect = context.get_drawing_effect();
135     context.set_drawing_effect(old_effect | VERTICAL_FLIP);
136     sprite->draw(context, get_pos(), layer);
137     context.set_drawing_effect(old_effect);
138   } else {
139     sprite->draw(context, get_pos(), layer);
140   }
141 }
142
143 void
144 BadGuy::update(float elapsed_time)
145 {
146   if(!Sector::current()->inside(bbox)) {
147     is_active_flag = false;
148     remove_me();
149     if(countMe) {
150       // get badguy name from sprite_name ignoring path and extension
151       std::string badguy = sprite_name.substr(0, sprite_name.length() - 7);
152       int path_chars = badguy.rfind("/",badguy.length());
153       badguy = badguy.substr(path_chars + 1, badguy.length() - path_chars);
154       // log warning since badguys_killed can no longer reach total_badguys
155       std::string current_level = "[" + Sector::current()->get_level()->filename + "] ";
156       log_warning << current_level << "Counted badguy " << badguy << " starting at " << start_position << " has left the sector" <<std::endl;;
157     }
158     return;
159   }
160   if ((state != STATE_INACTIVE) && is_offscreen()) {
161     if (state == STATE_ACTIVE) deactivate();
162     set_state(STATE_INACTIVE);
163   }
164
165   switch(state) {
166     case STATE_ACTIVE:
167       is_active_flag = true;
168       active_update(elapsed_time);
169       break;
170     case STATE_INIT:
171     case STATE_INACTIVE:
172       is_active_flag = false;
173       inactive_update(elapsed_time);
174       try_activate();
175       break;
176     case STATE_SQUISHED:
177       is_active_flag = false;
178       if(state_timer.check()) {
179         remove_me();
180         break;
181       }
182       movement = physic.get_movement(elapsed_time);
183       break;
184     case STATE_FALLING:
185       is_active_flag = false;
186       movement = physic.get_movement(elapsed_time);
187       break;
188   }
189
190   on_ground_flag = false;
191 }
192
193 Direction
194 BadGuy::str2dir( std::string dir_str )
195 {
196   if( dir_str == "auto" )
197     return AUTO;
198   if( dir_str == "left" )
199     return LEFT;
200   if( dir_str == "right" )
201     return RIGHT;
202
203   //default to "auto"
204   log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
205   return AUTO;
206 }
207
208 void
209 BadGuy::initialize()
210 {
211 }
212
213 void
214 BadGuy::activate()
215 {
216 }
217
218 void
219 BadGuy::deactivate()
220 {
221 }
222
223 void
224 BadGuy::active_update(float elapsed_time)
225 {
226   movement = physic.get_movement(elapsed_time);
227   if(frozen)
228     sprite->stop_animation();
229 }
230
231 void
232 BadGuy::inactive_update(float )
233 {
234 }
235
236 void
237 BadGuy::collision_tile(uint32_t tile_attributes)
238 {
239   // Don't kill badguys that have already been killed
240   if (!is_active()) return;
241
242   if(tile_attributes & Tile::WATER && !is_in_water())
243   {
244     in_water = true;
245     SoundManager::current()->play("sounds/splash.ogg", get_pos());
246   }
247   if(!(tile_attributes & Tile::WATER) && is_in_water())
248   {
249     in_water = false;
250   }
251
252   if(tile_attributes & Tile::HURTS) {
253     if (tile_attributes & Tile::FIRE) {
254       if (is_flammable()) ignite();
255     }
256     else if (tile_attributes & Tile::ICE) {
257       if (is_freezable()) freeze();
258     }
259     else {
260       kill_fall();
261     }
262   }
263 }
264
265 HitResponse
266 BadGuy::collision(GameObject& other, const CollisionHit& hit)
267 {
268   if (!is_active()) return ABORT_MOVE;
269
270   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
271   if(badguy && badguy->is_active() && badguy->get_group() == COLGROUP_MOVING) {
272
273     /* Badguys don't let badguys squish other badguys. It's bad. */
274 #if 0
275     // hit from above?
276     if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
277       if(collision_squished(*badguy)) {
278         return ABORT_MOVE;
279       }
280     }
281 #endif
282
283     return collision_badguy(*badguy, hit);
284   }
285
286   Player* player = dynamic_cast<Player*> (&other);
287   if(player) {
288
289     // hit from above?
290     if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
291       if(player->is_stone()) {
292         kill_fall();
293         return FORCE_MOVE;
294       }
295       if(collision_squished(*player)) {
296         return FORCE_MOVE;
297       }
298     }
299
300     if(player->is_stone()) {
301       collision_solid(hit);
302       return FORCE_MOVE;
303     }
304
305     return collision_player(*player, hit);
306   }
307
308   Bullet* bullet = dynamic_cast<Bullet*> (&other);
309   if(bullet)
310     return collision_bullet(*bullet, hit);
311
312   return FORCE_MOVE;
313 }
314
315 void
316 BadGuy::collision_solid(const CollisionHit& hit)
317 {
318   physic.set_velocity_x(0);
319   physic.set_velocity_y(0);
320   update_on_ground_flag(hit);
321 }
322
323 HitResponse
324 BadGuy::collision_player(Player& player, const CollisionHit& )
325 {
326   if(player.is_invincible()) {
327     kill_fall();
328     return ABORT_MOVE;
329   }
330
331   //TODO: unfreeze timer
332   if(frozen)
333     //unfreeze();
334     return FORCE_MOVE;
335
336   player.kill(false);
337   return FORCE_MOVE;
338 }
339
340 HitResponse
341 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
342 {
343   return FORCE_MOVE;
344 }
345
346 bool
347 BadGuy::collision_squished(GameObject& object)
348 {
349   // frozen badguys can be killed with butt-jump
350   if(frozen)
351   {
352     Player* player = dynamic_cast<Player*>(&object);
353     if(player && (player->does_buttjump)) {
354       player->bounce(*this);
355       kill_fall();//TODO: shatter frozen badguys
356       return true;
357     }
358   }
359     return false;
360 }
361
362 HitResponse
363 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
364 {
365   if (is_frozen()) {
366     if(bullet.get_type() == FIRE_BONUS) {
367       // fire bullet thaws frozen badguys
368       unfreeze();
369       bullet.remove_me();
370       return ABORT_MOVE;
371     } else {
372       // other bullets ricochet
373       bullet.ricochet(*this, hit);
374       return FORCE_MOVE;
375     }
376   }
377   else if (is_ignited()) {
378     if(bullet.get_type() == ICE_BONUS) {
379       // ice bullets extinguish ignited badguys
380       extinguish();
381       bullet.remove_me();
382       return ABORT_MOVE;
383     } else {
384       // other bullets are absorbed by ignited badguys
385       bullet.remove_me();
386       return FORCE_MOVE;
387     }
388   }
389   else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
390     // fire bullets ignite flammable badguys
391     ignite();
392     bullet.remove_me();
393     return ABORT_MOVE;
394   }
395   else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
396     // ice bullets freeze freezable badguys
397     freeze();
398     bullet.remove_me();
399     return ABORT_MOVE;
400   }
401   else {
402     // in all other cases, bullets ricochet
403     bullet.ricochet(*this, hit);
404     return FORCE_MOVE;
405   }
406 }
407
408 void
409 BadGuy::kill_squished(GameObject& object)
410 {
411   if (!is_active()) return;
412
413   SoundManager::current()->play("sounds/squish.wav", get_pos());
414   physic.enable_gravity(true);
415   physic.set_velocity_x(0);
416   physic.set_velocity_y(0);
417   set_state(STATE_SQUISHED);
418   set_group(COLGROUP_MOVING_ONLY_STATIC);
419   Player* player = dynamic_cast<Player*>(&object);
420   if (player) {
421     player->bounce(*this);
422   }
423
424   // start dead-script
425   run_dead_script();
426 }
427
428 void
429 BadGuy::kill_fall()
430 {
431   if (!is_active()) return;
432
433   SoundManager::current()->play("sounds/fall.wav", get_pos());
434   physic.set_velocity_y(0);
435   physic.set_acceleration_y(0);
436   physic.enable_gravity(true);
437   set_state(STATE_FALLING);
438
439   // Set the badguy layer to be the foremost, so that
440   // this does not reveal secret tilemaps:
441   layer = Sector::current()->get_foremost_layer() + 1;
442
443   // start dead-script
444   run_dead_script();
445 }
446
447 void
448 BadGuy::run_dead_script()
449 {
450   if (countMe)
451     Sector::current()->get_level()->stats.badguys++;
452
453   countMe = false;
454
455   // start dead-script
456   if(dead_script != "") {
457     std::istringstream stream(dead_script);
458     Sector::current()->run_script(stream, "dead-script");
459   }
460 }
461
462 void
463 BadGuy::set_state(State state_)
464 {
465   if(this->state == state_)
466     return;
467
468   State laststate = this->state;
469   this->state = state_;
470   switch(state_) {
471     case STATE_SQUISHED:
472       state_timer.start(SQUISH_TIME);
473       break;
474     case STATE_ACTIVE:
475       set_group(colgroup_active);
476       //bbox.set_pos(start_position);
477       break;
478     case STATE_INACTIVE:
479       // was the badguy dead anyway?
480       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
481         remove_me();
482       }
483       set_group(COLGROUP_DISABLED);
484       break;
485     case STATE_FALLING:
486       set_group(COLGROUP_DISABLED);
487       break;
488     default:
489       break;
490   }
491 }
492
493 bool
494 BadGuy::is_offscreen()
495 {
496   Player* player = get_nearest_player();
497   if (!player) return false;
498   Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
499   // In SuperTux 0.1.x, Badguys were activated when Tux<->Badguy center distance was approx. <= ~668px
500   // This doesn't work for wide-screen monitors which give us a virt. res. of approx. 1066px x 600px
501   if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
502     return false;
503   }
504   return true;
505 }
506
507 void
508 BadGuy::try_activate()
509 {
510   // Don't activate if player is dying
511   Player* player = get_nearest_player();
512   if (!player) return;
513
514   if (!is_offscreen()) {
515     set_state(STATE_ACTIVE);
516     if (!is_initialized) {
517
518       // if starting direction was set to AUTO, this is our chance to re-orient the badguy
519       if (start_dir == AUTO) {
520         Player* player_ = get_nearest_player();
521         if (player_ && (player_->get_bbox().p1.x > get_bbox().p2.x)) {
522           dir = RIGHT;
523         } else {
524           dir = LEFT;
525         }
526       }
527
528       initialize();
529       is_initialized = true;
530     }
531     activate();
532   }
533 }
534
535 bool
536 BadGuy::might_fall(int height)
537 {
538   // make sure we check for at least a 1-pixel fall
539   assert(height > 0);
540
541   float x1;
542   float x2;
543   float y1 = bbox.p2.y + 1;
544   float y2 = bbox.p2.y + 1 + height;
545   if (dir == LEFT) {
546     x1 = bbox.p1.x - 1;
547     x2 = bbox.p1.x;
548   } else {
549     x1 = bbox.p2.x;
550     x2 = bbox.p2.x + 1;
551   }
552   return Sector::current()->is_free_of_statics(Rectf(x1, y1, x2, y2));
553 }
554
555 Player*
556 BadGuy::get_nearest_player()
557 {
558   return Sector::current()->get_nearest_player (this->get_bbox ());
559 }
560
561 void
562 BadGuy::update_on_ground_flag(const CollisionHit& hit)
563 {
564   if (hit.bottom) {
565     on_ground_flag = true;
566     floor_normal = hit.slope_normal;
567   }
568 }
569
570 bool
571 BadGuy::on_ground()
572 {
573   return on_ground_flag;
574 }
575
576 bool
577 BadGuy::is_active()
578 {
579   return is_active_flag;
580 }
581
582 Vector
583 BadGuy::get_floor_normal()
584 {
585   return floor_normal;
586 }
587
588 void
589 BadGuy::freeze()
590 {
591   set_group(COLGROUP_MOVING_STATIC);
592   frozen = true;
593
594   if(sprite->has_action("iced-left"))
595     sprite->set_action(dir == LEFT ? "iced-left" : "iced-right", 1);
596   // when no iced action exists, default to shading badguy blue
597   else
598   {
599     sprite->set_color(Color(0.60, 0.72, 0.88f));
600     sprite->stop_animation();
601   }
602 }
603
604 void
605 BadGuy::unfreeze()
606 {
607   set_group(colgroup_active);
608   frozen = false;
609
610   // restore original color if needed
611   if(!sprite->has_action("iced-left"))
612   {
613     sprite->set_color(Color(1.00, 1.00, 1.00f));
614     sprite->set_animation_loops();
615   }
616 }
617
618 bool
619 BadGuy::is_freezable() const
620 {
621   return false;
622 }
623
624 bool
625 BadGuy::is_frozen() const
626 {
627   return frozen;
628 }
629
630 bool
631 BadGuy::is_in_water() const
632 {
633   return in_water;
634 }
635
636 void
637 BadGuy::ignite()
638 {
639   kill_fall();
640 }
641
642 void
643 BadGuy::extinguish()
644 {
645 }
646
647 bool
648 BadGuy::is_flammable() const
649 {
650   return true;
651 }
652
653 bool
654 BadGuy::is_ignited() const
655 {
656   return ignited;
657 }
658
659 void
660 BadGuy::set_colgroup_active(CollisionGroup group_)
661 {
662   this->colgroup_active = group_;
663   if (state == STATE_ACTIVE) set_group(group_);
664 }
665
666 /* EOF */