The BIG COMMIT(tm)
[supertux.git] / src / badguy.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de>
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; either version 2
11 //  of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 // 
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 //  02111-1307, USA.
22
23 #include <config.h>
24
25 #include <iostream>
26 #include <cmath>
27
28 #include "app/globals.h"
29 #include "defines.h"
30 #include "special/sprite_manager.h"
31 #include "utils/lispwriter.h"
32 #include "badguy.h"
33 #include "tile.h"
34 #include "resources.h"
35 #include "camera.h"
36 #include "level.h"
37 #include "sector.h"
38 #include "tilemap.h"
39 #include "statistics.h"
40 #include "badguy_specs.h"
41
42 #define BADGUY_WALK_SPEED .8f
43 #define WINGLING_FLY_SPEED 1.6f
44
45 BadGuyKind  badguykind_from_string(const std::string& str)
46 {
47   if (str == "jumpy" || str == "money") // was "money" in ancient versions
48     return BAD_JUMPY;
49   else if (str == "mriceblock" || str == "laptop") // was "laptop" in ancient versions
50     return BAD_MRICEBLOCK;
51   else if (str == "mrbomb")
52     return BAD_MRBOMB;
53   else if (str == "stalactite")
54     return BAD_STALACTITE;
55   else if (str == "flame")
56     return BAD_FLAME;
57   else if (str == "fish")
58     return BAD_FISH;
59   else if (str == "flamefish")
60     return BAD_FLAMEFISH;
61   else if (str == "bouncingsnowball")
62     return BAD_BOUNCINGSNOWBALL;
63   else if (str == "flyingsnowball")
64     return BAD_FLYINGSNOWBALL;
65   else if (str == "spiky")
66     return BAD_SPIKY;
67   else if (str == "snowball" || str == "bsod") // was "bsod" in ancient versions
68     return BAD_SNOWBALL;
69   else if (str == "wingling")
70     return BAD_WINGLING;
71   else if (str == "walkingtree")
72     return BAD_WALKINGTREE;
73   else if(str == "bomb")  // not to be used as a real bad guys
74       return BAD_BOMB;
75   else
76     {
77       return BAD_INVALID;
78     }
79 }
80
81 std::string badguykind_to_string(BadGuyKind kind)
82 {
83   switch(kind)
84     {
85     case BAD_JUMPY:
86       return "jumpy";
87       break;
88     case BAD_MRICEBLOCK:
89       return "mriceblock";
90       break;
91     case BAD_MRBOMB:
92       return "mrbomb";
93       break;
94     case BAD_STALACTITE:
95       return "stalactite";
96       break;
97     case BAD_FLAME:
98       return "flame";
99       break;
100     case BAD_FISH:
101       return "fish";
102       break;
103     case BAD_FLAMEFISH:
104       return "flamefish";
105       break;
106     case BAD_BOUNCINGSNOWBALL:
107       return "bouncingsnowball";
108       break;
109     case BAD_FLYINGSNOWBALL:
110       return "flyingsnowball";
111       break;
112     case BAD_SPIKY:
113       return "spiky";
114       break;
115     case BAD_SNOWBALL:
116       return "snowball";
117       break;
118     case BAD_WINGLING:
119       return "wingling";
120       break;
121     case BAD_WALKINGTREE:
122       return "walkingtree";
123     case BAD_BOMB:  // not to be used as a real bad guys
124       return "bomb";
125       break;
126     default:
127       return "snowball";
128     }
129 }
130
131 #if 0
132 BadGuy::BadGuy(BadGuyKind kind_, LispReader& lispreader)
133   : squishcount(0)
134 {
135   lispreader.read_float("x", start_position.x);
136   lispreader.read_float("y", start_position.y);
137
138   kind     = kind_;
139
140   stay_on_platform = false;
141   lispreader.read_bool("stay-on-platform", stay_on_platform);
142
143   init();
144 }
145
146 BadGuy::BadGuy(BadGuyKind kind_, float x, float y)
147   : squishcount(0)
148 {
149   start_position.x = x;
150   start_position.y = y;
151   stay_on_platform = false;
152
153   kind     = kind_;
154   
155   init();
156 }
157
158 BadGuy::~BadGuy()
159 {
160 }
161
162 void
163 BadGuy::init()
164 {
165   bbox.set_pos(start_position);
166   bbox.set_size(32, 32);
167   
168   state    = NORMAL;
169   old_base = base;
170   dir      = LEFT;
171   seen     = false;
172   animation_offset = 0;
173   target.x = target.y = -1;
174   physic.reset();
175   frozen_timer.init(true);
176   timer.init(true);
177
178   specs = badguyspecs_manager->load(badguykind_to_string(kind));
179 }
180
181 void
182 BadGuy::write(LispWriter& writer)
183 {
184   writer.start_list(badguykind_to_string(kind));
185
186   writer.write_float("x", base.x);
187   writer.write_float("y", base.y);
188   writer.write_bool("stay-on-platform", stay_on_platform);  
189
190   writer.end_list(badguykind_to_string(kind));
191 }
192
193 void
194 BadGuy::activate(Direction activation_dir)
195 {
196   state     = NORMAL;
197   animation_offset = 0;
198   target.x = target.y = -1;
199   physic.reset();
200   frozen_timer.start(0);
201   timer.start(0);
202
203   dying = DYING_NOT;
204   seen = true;
205
206   dir = activation_dir;
207   float dirsign = activation_dir == LEFT ? -1 : 1;
208
209   set_action("left", "right");
210   if(kind == BAD_MRBOMB) {
211     physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
212   } else if (kind == BAD_MRICEBLOCK) {
213     physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
214   } else if(kind == BAD_JUMPY) {
215     set_action("left-up", "right-up");
216   } else if(kind == BAD_BOMB) {
217     set_action("ticking-left", "ticking-right");
218     // hack so that the bomb doesn't hurt until it explodes...
219     dying = DYING_SQUISHED;
220   } else if(kind == BAD_FLAME) {
221     angle = 0;
222     physic.enable_gravity(false);
223     set_action("normal", "normal");
224   } else if(kind == BAD_BOUNCINGSNOWBALL) {
225     physic.set_velocity(dirsign * 1.3, 0);
226   } else if(kind == BAD_STALACTITE) {
227     physic.enable_gravity(false);
228     set_action("normal", "normal");
229   } else if(kind == BAD_FISH) {
230     set_action("normal", "normal");
231     physic.enable_gravity(true);
232   } else if(kind == BAD_FLAMEFISH) {
233     set_action("normal", "normal");
234     physic.enable_gravity(true);
235   } else if(kind == BAD_FLYINGSNOWBALL) {
236     physic.enable_gravity(false);
237   } else if(kind == BAD_SPIKY) {
238     physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
239   } else if(kind == BAD_SNOWBALL) {
240     physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
241   } else if(kind == BAD_WINGLING) {
242     physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
243     physic.enable_gravity(false);
244     set_action("left", "left");
245   } else if (kind == BAD_WALKINGTREE) {
246     // TODO: why isn't the height/width being set properly in set_action?
247     physic.set_velocity(dirsign * BADGUY_WALK_SPEED, 0);
248     state = BGM_BIG;
249     set_action("left", "left");
250     bbox.set_size(66, 66);
251   }
252 }
253
254 Surface*
255 BadGuy::get_image()
256 {
257   // Set action as the "default" one.
258   specs->sprite->set_action("left");
259   if(BAD_JUMPY)
260     specs->sprite->set_action("left-up");
261   else if(kind == BAD_BOMB)
262     specs->sprite->set_action("ticking-left");
263   else if(kind == BAD_FLAME)
264     specs->sprite->set_action("normal");
265   else if(kind == BAD_STALACTITE)
266     specs->sprite->set_action("normal");
267   else if(kind == BAD_FISH)
268     specs->sprite->set_action("normal");
269   else if(kind == BAD_FLAMEFISH)
270     specs->sprite->set_action("normal");
271
272   return specs->sprite->get_frame(0);
273 }
274
275 #if 0
276 void
277 BadGuy::action_mriceblock(double elapsed_time)
278 {
279   Player& tux = *Sector::current()->player;
280
281   if(state != HELD)
282     fall();
283   
284   /* Move left/right: */
285   if (state != HELD)
286     {
287       // move
288       physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
289       if (dying != DYING_FALLING)
290         collision_swept_object_map(&old_base,&base);
291     }
292   else if (state == HELD)
293     { /* FIXME: The pbad object shouldn't know about pplayer objects. */
294       /* If we're holding the iceblock */
295       dir = tux.dir;
296       if(tux.size == SMALL)
297         {
298         if(dir == RIGHT)
299           base.x = tux.base.x + 24;
300         else // dir == LEFT
301           base.x = tux.base.x - 12;
302         base.y = tux.base.y + tux.base.height/1.5 - base.height;
303         }
304       else // TUX == BIG
305         {
306         if(dir == RIGHT)
307           base.x = tux.base.x + 24;
308         else // dir == LEFT
309           base.x = tux.base.x - 4;
310         base.y = tux.base.y + tux.base.height/1.5 - base.height;
311         }
312
313       if(collision_object_map(base))
314         {
315           base.x = tux.base.x;
316           base.y = tux.base.y + tux.base.height/1.5 - base.height;
317         }
318
319       if(tux.input.fire != DOWN) /* SHOOT! */
320         {
321           if(dir == LEFT)
322             base.x = tux.base.x - base.width;
323           else
324             base.x = tux.base.x + tux.base.width;
325           old_base = base;
326
327           state=KICK;
328           tux.kick_timer.start(KICKING_TIME);
329           set_action("flat-left", "flat-right");
330           physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
331           SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos());
332         }
333     }
334
335   if (!dying)
336     {
337       int changed = dir;
338       check_horizontal_bump();
339       if(state == KICK && changed != dir)
340         {
341           SoundManager::get()->play_sound(IDToSound(SND_RICOCHET), get_pos(), Sector::current()->player->get_pos());
342         }
343     }
344
345   /* Handle state timer: */
346   if (state == FLAT)
347     {
348       if(!timer.check())
349         {
350           state = NORMAL;
351           set_action("left", "right");
352           physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
353         }
354     }
355 }
356
357 void
358 BadGuy::check_horizontal_bump(bool checkcliff)
359 {
360     float halfheight = base.height / 2;
361     if (dir == LEFT && issolid( base.x, base.y + halfheight))
362     {
363         if (kind == BAD_MRICEBLOCK && state == KICK)
364             {
365             Sector::current()->trybreakbrick(Vector(base.x, base.y + halfheight), false);
366             Sector::current()->tryemptybox(Vector(base.x, base.y + halfheight), dir);
367             }
368             
369         dir = RIGHT;
370         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
371         return;
372     }
373     if (dir == RIGHT && issolid( base.x + base.width, base.y + halfheight))
374     {
375         if (kind == BAD_MRICEBLOCK && state == KICK)
376             {
377             Sector::current()->trybreakbrick(
378                 Vector(base.x + base.width, base.y + halfheight), false);
379             Sector::current()->tryemptybox(
380                 Vector(base.x + base.width, base.y + halfheight), dir);
381             }
382             
383         dir = LEFT;
384         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
385         return;
386     }
387
388     // don't check for cliffs when we're falling
389     if(!checkcliff)
390         return;
391     if(!issolid(base.x + base.width/2, base.y + base.height))
392         return;
393     
394     if(dir == LEFT && !issolid(base.x, (int) base.y + base.height + halfheight))
395     {
396         dir = RIGHT;
397         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
398         return;
399     }
400     if(dir == RIGHT && !issolid(base.x + base.width,
401                 (int) base.y + base.height + halfheight))
402     {
403         dir = LEFT;
404         physic.set_velocity(-physic.get_velocity_x(), physic.get_velocity_y());
405         return;
406     }
407 }
408
409 void
410 BadGuy::fall()
411 {
412   /* Fall if we get off the ground: */
413   if (dying != DYING_FALLING)
414     {
415       if (!issolid(base.x+base.width/2, base.y + base.height))
416         {
417           // not solid below us? enable gravity
418           physic.enable_gravity(true);
419         }
420       else
421         {
422           /* Land: */
423           if (physic.get_velocity_y() < 0)
424             {
425               base.y = int((base.y + base.height)/32) * 32 - base.height;
426               physic.set_velocity_y(0);
427             }
428           // no gravity anymore please
429           physic.enable_gravity(false);
430
431           if (stay_on_platform && state == NORMAL)
432             {
433               if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
434                            base.y + base.height))
435                 {
436                   if (dir == LEFT)
437                   {
438                     dir = RIGHT;
439                     physic.set_velocity_x(fabsf(physic.get_velocity_x()));
440                   } 
441                   else
442                   {
443                     dir = LEFT;
444                     physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
445                   }
446                 }
447             }
448         }
449     }
450   else
451     {
452       physic.enable_gravity(true);
453     }
454 }
455 #endif
456
457 void
458 BadGuy::action_jumpy(double elapsed_time)
459 {
460   if(frozen_timer.check()) {
461     set_action("left-iced", "right-iced");
462     return;
463   }
464
465   const float vy = physic.get_velocity_y();
466
467   // XXX: These tests *should* use location from ground, not velocity
468   if (fabsf(vy) > 560)
469     set_action("left-down", "right-down");
470   else if (fabsf(vy) > 530)
471     set_action("left-middle", "right-middle");
472   else
473     set_action("left-up", "right-up");
474
475   Player& tux = *Sector::current()->player;
476
477   static const float JUMPV = 6;
478     
479   fall();
480   // jump when on ground
481   if(dying == DYING_NOT && issolid(base.x, base.y+32))
482     {
483       physic.set_velocity_y(JUMPV);
484       physic.enable_gravity(true);
485
486       state = JUMPY_JUMP;
487     }
488   else if(state == JUMPY_JUMP)
489     {
490       state = NORMAL;
491     }
492
493   // set direction based on tux
494   if(dying == DYING_NOT) {
495     if(tux.base.x > base.x)
496       dir = RIGHT;
497     else
498       dir = LEFT;
499   }
500
501   // move
502   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
503   if(dying == DYING_NOT)
504     collision_swept_object_map(&old_base, &base);
505 }
506
507 void
508 BadGuy::action_mrbomb(double elapsed_time)
509 {
510   if(frozen_timer.check())
511     {
512     set_action("iced-left", "iced-right");
513     return;
514     }
515
516   if (dying == DYING_NOT)
517     check_horizontal_bump(true);
518
519   fall();
520
521   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
522   if (dying != DYING_FALLING)
523     collision_swept_object_map(&old_base,&base); 
524 }
525
526 void
527 BadGuy::action_bomb(double elapsed_time)
528 {
529   static const int TICKINGTIME = 1000;
530   static const int EXPLODETIME = 1000;
531     
532   fall();
533
534   if(state == NORMAL) {
535     state = BOMB_TICKING;
536     timer.start(TICKINGTIME);
537   } else if(!timer.check()) {
538     if(state == BOMB_TICKING) {
539       state = BOMB_EXPLODE;
540       set_action("explosion", "explosion");
541       dying = DYING_NOT; // now the bomb hurts
542       timer.start(EXPLODETIME);
543
544       SoundManager::get()->play_sound(IDToSound(SND_EXPLODE), this, Sector::current()->player->get_pos());
545     } else if(state == BOMB_EXPLODE) {
546       remove_me();
547       return;
548     }
549   }
550
551   // move
552   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);                 
553   collision_swept_object_map(&old_base,&base);
554 }
555
556 void
557 BadGuy::action_stalactite(double elapsed_time)
558 {
559   Player& tux = *Sector::current()->player;
560
561   static const int SHAKETIME = 800;
562   static const int RANGE = 40;
563     
564   if(state == NORMAL) {
565     // start shaking when tux is below the stalactite and at least 40 pixels
566     // near
567     if(tux.base.x + 32 > base.x - RANGE && tux.base.x < base.x + 32 + RANGE
568             && tux.base.y + tux.base.height > base.y
569             && tux.dying == DYING_NOT) {
570       timer.start(SHAKETIME);
571       state = STALACTITE_SHAKING;
572     }
573   } if(state == STALACTITE_SHAKING) {
574     base.x = old_base.x + (rand() % 6) - 3; // TODO this could be done nicer...
575     if(!timer.check()) {
576       state = STALACTITE_FALL;
577     }
578   } else if(state == STALACTITE_FALL) {
579     fall();
580     /* Destroy if we collides with land */
581     if(issolid(base.x+base.width/2, base.y+base.height))
582     {
583       timer.start(2000);
584       dying = DYING_SQUISHED;
585       state = FLAT;
586       set_action("broken", "broken");
587     }
588   } else if(state == FLAT) {
589     fall();
590   }
591
592   // move
593   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
594
595   if(dying == DYING_SQUISHED && !timer.check())
596     remove_me();
597 }
598
599 void
600 BadGuy::action_flame(double elapsed_time)
601 {
602     static const float radius = 100;
603     static const float speed = 0.02;
604     base.x = old_base.x + cos(angle) * radius;
605     base.y = old_base.y + sin(angle) * radius;
606
607     angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
608 }
609
610 void
611 BadGuy::action_fish(double elapsed_time)
612 {
613   if(frozen_timer.check())
614     {
615     if(physic.get_velocity_y() < 0)
616       set_action("iced-down", "iced-down");
617     else
618       set_action("iced", "iced");
619
620     return;
621     }
622
623   static const float JUMPV = 6;
624   static const int WAITTIME = 1000;
625     
626   // go in wait state when back in water
627   if(dying == DYING_NOT 
628       && gettile(base.x, base.y + base.height)
629       && gettile(base.x, base.y + base.height)->attributes & Tile::WATER
630       && physic.get_velocity_y() <= 0 && state == NORMAL)
631     {
632       state = FISH_WAIT;
633       set_action("hide", "hide");
634       physic.set_velocity(0, 0);
635       physic.enable_gravity(false);
636       timer.start(WAITTIME);
637     }
638   else if(state == FISH_WAIT && !timer.check())
639     {
640       // jump again
641       set_action("normal", "normal");
642       state = NORMAL;
643       physic.set_velocity(0, JUMPV);
644       physic.enable_gravity(true);
645     }
646
647   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
648   if(dying == DYING_NOT)
649     collision_swept_object_map(&old_base, &base);
650
651   if(physic.get_velocity_y() < 0)
652     {
653       set_action("down", "down");
654     }
655 }
656
657 void
658 BadGuy::action_bouncingsnowball(double elapsed_time)
659 {
660   static const float JUMPV = 4.5;
661     
662   fall();
663
664   // jump when on ground
665   if(dying == DYING_NOT && issolid(base.x, base.y+32))
666     {
667       physic.set_velocity_y(JUMPV);
668       physic.enable_gravity(true);
669     }                                                     
670   else
671     {
672       state = NORMAL;
673     }
674
675   // check for right/left collisions
676   check_horizontal_bump();
677
678   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
679   if(dying == DYING_NOT)
680     collision_swept_object_map(&old_base, &base);
681
682   // Handle dying timer:
683   if (dying == DYING_SQUISHED && !timer.check())
684     remove_me();
685 }
686
687 void
688 BadGuy::action_flyingsnowball(double elapsed_time)
689 {
690   static const float FLYINGSPEED = 1;
691   static const int DIRCHANGETIME = 1000;
692     
693   // go into flyup state if none specified yet
694   if(dying == DYING_NOT && state == NORMAL) {
695     state = FLY_UP;
696     physic.set_velocity_y(FLYINGSPEED);
697     timer.start(DIRCHANGETIME/2);
698   }
699
700   if(dying == DYING_NOT && !timer.check()) {
701     if(state == FLY_UP) {
702       state = FLY_DOWN;
703       physic.set_velocity_y(-FLYINGSPEED);
704     } else if(state == FLY_DOWN) {
705       state = FLY_UP;
706       physic.set_velocity_y(FLYINGSPEED);
707     }
708     timer.start(DIRCHANGETIME);
709   }
710
711   if(dying != DYING_NOT)
712     physic.enable_gravity(true);
713
714   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
715   if(dying == DYING_NOT || dying == DYING_SQUISHED)
716     collision_swept_object_map(&old_base, &base);
717
718   if(dying == DYING_NOT)
719     {
720     // set direction based on tux
721     if(Sector::current()->player->base.x > base.x)
722       dir = RIGHT;
723     else
724       dir = LEFT;
725     }
726
727   // Handle dying timer:
728   if (dying == DYING_SQUISHED && !timer.check())
729     remove_me();
730 }
731
732 void
733 BadGuy::action_spiky(double elapsed_time)
734 {
735   if(frozen_timer.check())
736     {
737     set_action("iced-left", "iced-right");
738     return;
739     }
740
741   if (dying == DYING_NOT)
742     check_horizontal_bump();
743
744   fall();
745
746   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
747   if (dying != DYING_FALLING)
748     collision_swept_object_map(&old_base,&base);   
749 }
750
751 void
752 BadGuy::action_snowball(double elapsed_time)
753 {
754   if (dying == DYING_NOT)
755     check_horizontal_bump();
756
757   fall();
758
759   // jump when we're about to fall
760   if (physic.get_velocity_y() == 0 && 
761           !issolid(base.x+base.width/2, base.y + base.height)) {
762     physic.enable_gravity(true);
763     physic.set_velocity_y(2);
764   }
765
766   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
767   if (dying != DYING_FALLING)
768     collision_swept_object_map(&old_base,&base);
769
770   // Handle dying timer:
771   if (dying == DYING_SQUISHED && !timer.check())
772     remove_me();                                  
773 }
774
775 void
776 BadGuy::action_wingling(double elapsed_time)
777 {
778   if (dying != DYING_NOT)
779     physic.enable_gravity(true);
780   else
781   {
782     Player& tux = *Sector::current()->player;
783     int dirsign = physic.get_velocity_x() < 0 ? -1 : 1;
784
785     if (fabsf(tux.base.x - base.x) < 150 && base.y < tux.base.y && tux.dying == DYING_NOT)
786     {
787       if (target.x < 0 && target.y < 0)
788       {
789         target.x = tux.base.x;
790         target.y = tux.base.y;
791         physic.set_velocity(dirsign * 1.5f, -2.25f);
792       }
793     }
794     else if (base.y >= target.y - 16)
795       physic.set_velocity(dirsign * WINGLING_FLY_SPEED, 0);
796   }
797
798   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
799
800
801   // Handle dying timer:
802   if (dying == DYING_SQUISHED && !timer.check())
803     remove_me();
804
805   // TODO: Winglings should be removed after flying off the screen
806 }
807
808 void
809 BadGuy::action_walkingtree(double elapsed_time)
810 {
811   if (dying == DYING_NOT)
812     check_horizontal_bump();
813
814   fall();
815
816   physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
817   if (dying != DYING_FALLING)
818     collision_swept_object_map(&old_base,&base);
819
820   // Handle dying timer:
821   if (dying == DYING_SQUISHED && !timer.check())
822     remove_me();
823 }
824
825 void
826 BadGuy::action(float elapsed_time)
827 {
828   float scroll_x = Sector::current()->camera->get_translation().x;
829   float scroll_y = Sector::current()->camera->get_translation().y;
830   
831   // BadGuy fall below the ground
832   if (base.y > Sector::current()->solids->get_height() * 32) {
833     remove_me();
834     return;
835   }
836
837   // Kill us if we landed on spikes
838   if (dying == DYING_NOT
839       && (kind != BAD_STALACTITE && kind != BAD_FLAME && kind != BAD_BOMB)
840       && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
841       ||  isspike(base.x, base.y + base.height)
842       ||  isspike(base.x + base.width, base.y + base.height)))
843       {
844          physic.set_velocity_y(3);
845          kill_me(0);
846       }
847
848   if(!seen)
849     {
850     /* Activate badguys if they're just around the screen to avoid
851      * the effect of having badguys suddenly popping up from nowhere.
852      */
853     if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
854         start_position.x < scroll_x - base.width &&
855         start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
856         start_position.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
857       activate(RIGHT);
858     else if (start_position.x > scroll_x + screen->w &&
859         start_position.x < scroll_x + screen->w + X_OFFSCREEN_DISTANCE &&
860         start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
861         start_position.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
862       activate(LEFT);
863     else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
864         start_position.x < scroll_x + screen->w + X_OFFSCREEN_DISTANCE &&
865         ((start_position.y > scroll_y + screen->h &&
866         start_position.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE) ||
867         (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
868         start_position.y < scroll_y)))
869         {
870         if(start_position.x < scroll_x - screen->w/2)
871           activate(RIGHT);
872         else
873           activate(LEFT);
874         }
875     /* Special case for badguys on start of the level.
876      * If in the future, it's possible to set Tux start pos, this case
877      * should contemplate that. */
878     else if (start_position.x > 0 && start_position.x < screen->w &&
879              start_position.y > 0 && start_position.y < screen->h)
880       activate(LEFT);
881     }
882   else
883     {
884     if(base.x + base.width < scroll_x - X_OFFSCREEN_DISTANCE*4
885       || base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE*4
886       || base.y + base.height < scroll_y - Y_OFFSCREEN_DISTANCE*4
887       || base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE*4)
888       {
889       seen = false;
890       if(dying != DYING_NOT)
891         remove_me();
892       }
893     }
894
895   if(!seen)
896     return;
897   
898   switch (kind)
899     {
900     case BAD_MRICEBLOCK:
901       action_mriceblock(elapsed_time);
902       break;
903   
904     case BAD_JUMPY:
905       action_jumpy(elapsed_time);
906       break;
907
908     case BAD_MRBOMB:
909       action_mrbomb(elapsed_time);
910       break;
911     
912     case BAD_BOMB:
913       action_bomb(elapsed_time);
914       break;
915
916     case BAD_STALACTITE:
917       action_stalactite(elapsed_time);
918       break;
919
920     case BAD_FLAME:
921       action_flame(elapsed_time);
922       break;
923
924     case BAD_FISH:
925     case BAD_FLAMEFISH:
926       action_fish(elapsed_time);
927       break;
928
929     case BAD_BOUNCINGSNOWBALL:
930       action_bouncingsnowball(elapsed_time);
931       break;
932
933     case BAD_FLYINGSNOWBALL:
934       action_flyingsnowball(elapsed_time);
935       break;
936
937     case BAD_SPIKY:
938       action_spiky(elapsed_time);
939       break;
940
941     case BAD_SNOWBALL:
942       action_snowball(elapsed_time);
943       break;
944
945     case BAD_WINGLING:
946       action_wingling(elapsed_time);
947       break;
948
949     case BAD_WALKINGTREE:
950       action_walkingtree(elapsed_time);
951       break;
952
953     default:
954       break;
955     }
956 }
957
958 void
959 BadGuy::draw(DrawingContext& context)
960 {
961   if(!seen)
962     return;
963
964   if((dir == LEFT && action_left == "hide") ||
965      (dir == RIGHT && action_right == "hide"))
966     return;
967
968   if(dir == LEFT)
969     specs->sprite->set_action(action_left);
970   else  // if(dir == RIGHT)
971     specs->sprite->set_action(action_right);
972
973   if(dying == DYING_FALLING && physic.get_velocity_y() < 0)
974     specs->sprite->draw(context, Vector(base.x, base.y), LAYER_FOREGROUNDTILES+1, VERTICAL_FLIP);
975   else
976     specs->sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
977
978   if(debug_state)
979     context.draw_filled_rect(Vector(base.x, base.y),
980         Vector(base.width, base.height), Color(75,0,75, 150), LAYER_OBJECTS+1);
981 }
982
983 void
984 BadGuy::set_action(std::string left, std::string right)
985 {
986   base.width = 32;
987   base.height = 32;
988
989   action_left = left;
990   action_right = right;
991
992 #if 0
993   if (1)
994     {
995       base.width = 32;
996       base.height = 32;
997     }
998   else
999     {
1000       // FIXME: Using the image size for the physics and collision is
1001       // a bad idea, since images should always overlap their physical
1002       // representation
1003       if(left != 0) {
1004         if(base.width == 0 && base.height == 0) {
1005           base.width  = left->get_width();
1006           base.height = left->get_height();
1007         } else if(base.width != left->get_width() || base.height != left->get_height()) {
1008           base.x -= (left->get_width() - base.width) / 2;
1009           base.y -= left->get_height() - base.height;
1010           base.width = left->get_width();
1011           base.height = left->get_height();
1012           old_base = base;
1013         }
1014       } else {
1015         base.width = base.height = 0;
1016       }
1017     }
1018
1019   animation_offset = 0;
1020   sprite_left  = left;
1021   sprite_right = right;
1022 #endif
1023 }
1024
1025 void
1026 BadGuy::bump()
1027 {
1028   // these can't be bumped
1029   if(kind == BAD_FLAME || kind == BAD_BOMB || kind == BAD_FISH
1030       || kind == BAD_FLAMEFISH || kind == BAD_FLYINGSNOWBALL)
1031     return;
1032
1033   physic.set_velocity_y(3);
1034   kill_me(25);
1035 }
1036
1037 void
1038 BadGuy::squish_me(Player* player)
1039 {
1040   player->bounce(this);
1041     
1042   Sector::current()->add_score(Vector(base.x, base.y),
1043                               25 * player_status.score_multiplier);
1044
1045   SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(), Sector::current()->player->get_pos());
1046   player_status.score_multiplier++;
1047
1048   dying = DYING_SQUISHED;
1049   timer.start(2000);
1050   physic.set_velocity(0, 0);
1051 }
1052
1053 void
1054 BadGuy::squish(Player* player)
1055 {
1056   static const int MAX_ICEBLOCK_SQUICHES = 10;
1057     
1058   if(kind == BAD_MRBOMB) {
1059     // mrbomb transforms into a bomb now
1060     explode(false);
1061     
1062     player->bounce(this);
1063     Sector::current()->add_score(Vector(base.x, base.y),
1064                                 25 * player_status.score_multiplier);
1065     SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(), Sector::current()->player->get_pos());
1066
1067     player_status.score_multiplier++;
1068     return;
1069
1070   } else if (kind == BAD_MRICEBLOCK) {
1071     if (state == NORMAL || state == KICK)
1072       {
1073         /* Flatten! */
1074         SoundManager::get()->play_sound(IDToSound(SND_STOMP), get_pos(), Sector::current()->player->get_pos());
1075         state = FLAT;
1076         set_action("flat-left", "flat-right");
1077         physic.set_velocity_x(0);
1078
1079         timer.start(4000);
1080       } else if (state == FLAT) {
1081         /* Kick! */
1082         SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos());
1083
1084         if (player->base.x < base.x + (base.width/2)) {
1085           physic.set_velocity_x(5);
1086           dir = RIGHT;
1087         } else {
1088           physic.set_velocity_x(-5);
1089           dir = LEFT;
1090         }
1091
1092         state = KICK;
1093         player->kick_timer.start(KICKING_TIME);
1094         set_action("flat-left", "flat-right");
1095       }
1096
1097     player->bounce(this);
1098
1099     player_status.score_multiplier++;
1100
1101     // check for maximum number of squishes
1102     squishcount++;
1103     if(squishcount >= MAX_ICEBLOCK_SQUICHES) {
1104       kill_me(50);
1105       return;
1106     }
1107     
1108     return;
1109   } else if(kind == BAD_FISH || kind == BAD_FLAMEFISH) {
1110     // fish can only be killed when falling down
1111     if(physic.get_velocity_y() >= 0)
1112       return;
1113       
1114     player->bounce(this);
1115               
1116     Sector::current()->add_score(Vector(base.x, base.y),
1117                                 25 * player_status.score_multiplier);
1118
1119     player_status.score_multiplier++;
1120      
1121     // simply remove the fish...
1122     remove_me();
1123     return;
1124   } else if(kind == BAD_BOUNCINGSNOWBALL) {
1125     squish_me(player);
1126     set_action("squished", "squished");
1127     return;
1128   } else if(kind == BAD_FLYINGSNOWBALL) {
1129     squish_me(player);
1130     set_action("squished-left", "squished-right");
1131     return;
1132   } else if(kind == BAD_SNOWBALL) {
1133     squish_me(player);
1134     set_action("squished-left", "squished-right");
1135     return;
1136   } else if(kind == BAD_WINGLING) {
1137     squish_me(player);
1138     set_action("left", "right");
1139   } else if(kind == BAD_WALKINGTREE) {
1140     if (state == BGM_BIG)
1141     {
1142       set_action("left-small", "left-small");
1143       physic.set_velocity_x(physic.get_velocity_x() * 2.0f);
1144
1145       /* Move to the player's direction */
1146       if(dir != Sector::current()->player->dir)
1147         physic.set_velocity_x(-physic.get_velocity_x());
1148       dir = Sector::current()->player->dir;
1149
1150       // XXX magic number: 66 is BGM_BIG height
1151
1152       player->bounce(this);
1153       base.y += 66 - base.height;
1154
1155       Sector::current()->add_score(Vector(base.x, base.y),
1156                                 25 * player_status.score_multiplier);
1157       player_status.score_multiplier++;
1158
1159       state = BGM_SMALL;
1160     }
1161     else
1162       squish_me(player);
1163   }
1164 }
1165
1166 void
1167 BadGuy::kill_me(int score)
1168 {
1169   if(kind == BAD_BOMB)
1170     return;
1171
1172   if(state != HELD)
1173     global_stats.add_points(BADGUYS_KILLED_STAT, 1);
1174
1175   dying = DYING_FALLING;
1176   if(kind == BAD_MRICEBLOCK) {
1177     set_action("falling-left", "falling-right");
1178     if(state == HELD) {
1179       state = NORMAL;
1180       Player& tux = *Sector::current()->player;
1181       tux.holding_something = false;
1182     }
1183   }
1184
1185   physic.enable_gravity(true);
1186
1187   /* Gain some points: */
1188   if (score != 0)
1189     Sector::current()->add_score(Vector(base.x, base.y),
1190                                 score * player_status.score_multiplier);
1191
1192   /* Play death sound: */
1193   SoundManager::get()->play_sound(IDToSound(SND_FALL), this, Sector::current()->player->get_pos());
1194 }
1195
1196 void
1197 BadGuy::explode(bool right_way)
1198 {
1199   BadGuy *badguy = Sector::current()->add_bad_guy(base.x, base.y, BAD_BOMB, true);
1200   if(right_way)
1201     {
1202     badguy->timer.start(0);
1203     badguy->state = BOMB_TICKING;
1204     }
1205   badguy->dir = dir;
1206
1207   remove_me();
1208 }
1209
1210 void
1211 BadGuy::collision(const MovingObject&, int)
1212 {
1213   // later
1214 }
1215
1216 void
1217 BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
1218 {
1219   BadGuy* pbad_c    = NULL;
1220   Bullet* pbullet_c = NULL;
1221
1222   if(type == COLLISION_BUMP) {
1223     bump();
1224     return;
1225   }
1226
1227   if(type == COLLISION_SQUISH) {
1228     Player* player = static_cast<Player*>(p_c_object);
1229     squish(player);
1230     return;
1231   }
1232
1233   /* COLLISION_NORMAL */
1234   switch (c_object)
1235     {
1236     case CO_BULLET:
1237       pbullet_c = (Bullet*) p_c_object;
1238
1239       if(pbullet_c->kind == FIRE_BULLET)
1240         {
1241         if (kind != BAD_BOMB && kind != BAD_STALACTITE && kind != BAD_FLAME
1242             && kind != BAD_FLAMEFISH)
1243           kill_me(10);
1244         }
1245       else if(pbullet_c->kind == ICE_BULLET)
1246         {
1247         if(kind == BAD_FLAME || kind == BAD_FLAMEFISH)
1248           kill_me(10);
1249         else
1250           frozen_timer.start(FROZEN_TIME);
1251         }
1252       break;
1253
1254     case CO_BADGUY:
1255       pbad_c = (BadGuy*) p_c_object;
1256
1257
1258       /* If we're a kicked mriceblock, kill [almost] any badguys we hit */
1259       if(kind == BAD_MRICEBLOCK && state == KICK &&
1260          kind != BAD_FLAME && kind != BAD_BOMB && kind != BAD_STALACTITE)
1261         {
1262           pbad_c->kill_me(25);
1263         }
1264
1265       // a held mriceblock kills the enemy too but falls to ground then
1266       else if(kind == BAD_MRICEBLOCK && state == HELD)
1267         {
1268           pbad_c->kill_me(25);
1269           kill_me(0);
1270         }
1271
1272       /* Kill badguys that run into exploding bomb */
1273       else if (kind == BAD_BOMB && dying == DYING_NOT)
1274       {
1275         if (pbad_c->kind == BAD_MRBOMB)
1276         {
1277           // mrbomb transforms into a bomb now
1278           pbad_c->explode(true);
1279           return;
1280         }
1281         else
1282         {
1283           pbad_c->kill_me(50);
1284         }
1285       }
1286
1287       /* Kill any badguys that get hit by stalactite */
1288       else if (kind == BAD_STALACTITE && dying == DYING_NOT)
1289       {
1290         if (pbad_c->kind == BAD_MRBOMB)
1291         {
1292           // mrbomb transforms into a bomb now
1293           pbad_c->explode(false);
1294           return;
1295         }
1296         else
1297           pbad_c->kill_me(50);
1298       }
1299
1300       /* When enemies run into eachother, make them change directions */
1301       else
1302       {
1303         // Wingling doesn't interact with other badguys
1304         if (pbad_c->kind == BAD_WINGLING || kind == BAD_WINGLING)
1305           break;
1306
1307         // Jumpy, fish, flame, stalactites, wingling are exceptions
1308         if (pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FLAME
1309             || pbad_c->kind == BAD_STALACTITE || pbad_c->kind == BAD_FISH
1310             || pbad_c->kind == BAD_FLAMEFISH)
1311           break;
1312
1313         // Bounce off of other badguy if we land on top of him
1314         if (base.y + base.height < pbad_c->base.y + pbad_c->base.height)
1315         {
1316           if (pbad_c->dir == LEFT)
1317           {
1318             dir = RIGHT;
1319             physic.set_velocity(fabsf(physic.get_velocity_x()), 2);
1320           }
1321           else if (pbad_c->dir == RIGHT)
1322           {
1323             dir = LEFT;
1324             physic.set_velocity(-fabsf(physic.get_velocity_x()), 2);
1325           }
1326
1327           break;
1328         }
1329         else if (base.y + base.height > pbad_c->base.y + pbad_c->base.height)
1330           break;
1331
1332         if (pbad_c->kind != BAD_FLAME)
1333           {
1334             if (dir == LEFT)
1335             {
1336               dir = RIGHT;
1337               physic.set_velocity_x(fabsf(physic.get_velocity_x()));
1338
1339               // Put bad guys a part (or they get jammed)
1340               // only needed to do to one of them
1341               if (physic.get_velocity_x() != 0)
1342                 base.x = pbad_c->base.x + pbad_c->base.width + 1;
1343             }
1344             else if (dir == RIGHT)
1345             {
1346               dir = LEFT;
1347               physic.set_velocity_x(-fabsf(physic.get_velocity_x()));
1348             }
1349
1350           }
1351       }
1352       
1353       break;
1354
1355     case CO_PLAYER:
1356       Player* player = static_cast<Player*>(p_c_object);
1357       /* Get kicked if were flat */
1358       if (state == FLAT && !dying)
1359       {
1360         SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos());
1361
1362         // Hit from left side
1363         if (player->base.x < base.x) {
1364           physic.set_velocity_x(5);
1365           dir = RIGHT;
1366         }
1367         // Hit from right side
1368         else {
1369           physic.set_velocity_x(-5);
1370           dir = LEFT;
1371         }
1372
1373         state = KICK;
1374         player->kick_timer.start(KICKING_TIME);
1375         set_action("flat-left", "flat-right");
1376       }
1377       break;
1378
1379     }
1380 }
1381
1382 #endif