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