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