This should work, i guess...
[supertux.git] / src / world.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 Ingo Ruhnke <grumbel@gmx.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 <math.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "globals.h"
28 #include "scene.h"
29 #include "screen.h"
30 #include "defines.h"
31 #include "world.h"
32 #include "level.h"
33 #include "tile.h"
34 #include "resources.h"
35
36 Surface* img_distro[4];
37
38 World* World::current_ = 0;
39
40 World::World(const std::string& filename)
41 {
42   // FIXME: Move this to action and draw and everywhere else where the
43   // world calls child functions
44   current_ = this;
45
46   level = new Level(filename);
47   tux.init();
48
49   set_defaults();
50
51   get_level()->load_gfx();
52   activate_bad_guys();
53   activate_particle_systems();
54   get_level()->load_song();
55
56   apply_bonuses();
57
58   scrolling_timer.init(true);
59 }
60
61 World::World(const std::string& subset, int level_nr)
62 {
63   // FIXME: Move this to action and draw and everywhere else where the
64   // world calls child functions
65   current_ = this;
66
67   level = new Level(subset, level_nr);
68   tux.init();
69
70   set_defaults();
71
72   get_level()->load_gfx();
73   activate_bad_guys();
74   activate_particle_systems();
75   get_level()->load_song();
76
77   apply_bonuses();
78
79   scrolling_timer.init(true);
80 }
81
82 void
83 World::apply_bonuses()
84 {
85   // Apply bonuses from former levels
86   switch (player_status.bonus)
87     {
88     case PlayerStatus::NO_BONUS:
89       break;
90
91     case PlayerStatus::FLOWER_BONUS:
92       tux.got_coffee = true;
93       // fall through
94
95     case PlayerStatus::GROWUP_BONUS:
96       // FIXME: Move this to Player class
97       tux.size = BIG;
98       tux.base.height = 64;
99       tux.base.y -= 32;
100       break;
101     }
102 }
103
104 World::~World()
105 {
106   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
107     delete *i;
108
109   for (ParticleSystems::iterator i = particle_systems.begin();
110           i != particle_systems.end(); ++i)
111     delete *i;
112
113   for (std::vector<BouncyDistro*>::iterator i = bouncy_distros.begin();
114        i != bouncy_distros.end(); ++i)
115     delete *i;
116   
117   for (std::vector<BrokenBrick*>::iterator i = broken_bricks.begin();
118        i != broken_bricks.end(); ++i)
119     delete *i;
120   
121   for (std::vector<BouncyBrick*>::iterator i = bouncy_bricks.begin();
122        i != bouncy_bricks.end(); ++i)
123     delete *i;
124
125   for (std::vector<FloatingScore*>::iterator i = floating_scores.begin();
126        i != floating_scores.end(); ++i)
127     delete *i;
128   
129   delete level;
130 }
131
132 void
133 World::set_defaults()
134 {
135   // Set defaults: 
136   scroll_x = 0;
137
138   player_status.score_multiplier = 1;
139
140   counting_distros = false;
141   distro_counter = 0;
142
143   /* set current song/music */
144   currentmusic = LEVEL_MUSIC;
145 }
146
147 void
148 World::activate_bad_guys()
149 {
150   for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
151        i != level->badguy_data.end();
152        ++i)
153     {
154       add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
155     }
156 }
157
158 void
159 World::activate_particle_systems()
160 {
161   if (level->particle_system == "clouds")
162     {
163       particle_systems.push_back(new CloudParticleSystem);
164     }
165   else if (level->particle_system == "snow")
166     {
167       particle_systems.push_back(new SnowParticleSystem);
168     }
169   else if (level->particle_system != "")
170     {
171       st_abort("unknown particle system specified in level", "");
172     }
173 }
174
175 void
176 World::draw()
177 {
178   int y,x;
179
180   /* Draw the real background */
181   if(get_level()->bkgd_image[0] != '\0')
182     {
183       int s = (int)((float)scroll_x * ((float)level->bkgd_speed/60.)) % screen->w;
184       level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
185       level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
186     }
187   else
188     {
189       drawgradient(level->bkgd_top, level->bkgd_bottom);
190     }
191     
192   /* Draw particle systems (background) */
193   std::vector<ParticleSystem*>::iterator p;
194   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
195     {
196       (*p)->draw(scroll_x, 0, 0);
197     }
198
199   /* Draw background: */
200   for (y = 0; y < 15; ++y)
201     {
202       for (x = 0; x < 21; ++x)
203         {
204           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
205                      level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
206         }
207     }
208
209   /* Draw interactive tiles: */
210   for (y = 0; y < 15; ++y)
211     {
212       for (x = 0; x < 21; ++x)
213         {
214           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
215                      level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
216         }
217     }
218
219   /* (Bouncy bricks): */
220   for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
221     bouncy_bricks[i]->draw();
222
223   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
224     (*i)->draw();
225
226   tux.draw();
227
228   for (unsigned int i = 0; i < bullets.size(); ++i)
229     bullets[i].draw();
230
231   for (unsigned int i = 0; i < floating_scores.size(); ++i)
232     floating_scores[i]->draw();
233
234   for (unsigned int i = 0; i < upgrades.size(); ++i)
235     upgrades[i].draw();
236
237   for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
238     bouncy_distros[i]->draw();
239
240   for (unsigned int i = 0; i < broken_bricks.size(); ++i)
241     broken_bricks[i]->draw();
242
243   /* Draw foreground: */
244   for (y = 0; y < 15; ++y)
245     {
246       for (x = 0; x < 21; ++x)
247         {
248           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
249                      level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
250         }
251     }
252
253   /* Draw particle systems (foreground) */
254   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
255     {
256       (*p)->draw(scroll_x, 0, 1);
257     }
258 }
259
260 void
261 World::action(double frame_ratio)
262 {
263   tux.action(frame_ratio);
264   scrolling(frame_ratio);
265
266   /* Handle bouncy distros: */
267   for (unsigned int i = 0; i < bouncy_distros.size(); i++)
268     bouncy_distros[i]->action(frame_ratio);
269
270   /* Handle broken bricks: */
271   for (unsigned int i = 0; i < broken_bricks.size(); i++)
272     broken_bricks[i]->action(frame_ratio);
273
274   // Handle all kinds of game objects
275   for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
276     bouncy_bricks[i]->action(frame_ratio);
277   
278   for (unsigned int i = 0; i < floating_scores.size(); i++)
279     floating_scores[i]->action(frame_ratio);
280
281   for (unsigned int i = 0; i < bullets.size(); ++i)
282     bullets[i].action(frame_ratio);
283   
284   for (unsigned int i = 0; i < upgrades.size(); i++)
285     upgrades[i].action(frame_ratio);
286
287   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
288     (*i)->action(frame_ratio);
289
290   /* update particle systems */
291   std::vector<ParticleSystem*>::iterator p;
292   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
293     {
294       (*p)->simulate(frame_ratio);
295     }
296
297   /* Handle all possible collisions. */
298   collision_handler();
299   
300   // Cleanup marked badguys
301   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
302       /* ++i handled at end of the loop */) {
303     if ((*i)->is_removable()) {
304       delete *i;
305       i =  bad_guys.erase(i);
306     } else {
307       ++i;
308     }
309   }
310 }
311
312 // the space that it takes for the screen to start scrolling, regarding
313 // screen bounds (in pixels)
314 #define X_SPACE (400-16)
315 // the time it takes to move the camera (in ms)
316 #define CHANGE_DIR_SCROLL_SPEED 2000
317
318 /* This functions takes cares of the scrolling */
319 void World::scrolling(double frame_ratio)
320 {
321   int tux_pos_x = (int)(tux.base.x + (tux.base.width/2));
322
323   if (/*level->back_scrolling || */debug_mode)
324   {
325     if(tux.old_dir != tux.dir)
326       scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
327
328     if(scrolling_timer.check())
329     {
330       float final_scroll_x;
331       if (tux.physic.get_velocity_x() > 0)
332         final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
333       else if (tux.physic.get_velocity_x() < 0)
334         final_scroll_x = tux_pos_x - X_SPACE;
335       else
336       {
337         if (tux.dir == RIGHT)
338           final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
339         else if (tux.dir == LEFT && level->back_scrolling)
340           final_scroll_x = tux_pos_x - X_SPACE;
341       }
342
343 /*      scroll_x +=   (final_scroll_x - scroll_x)
344                   / (frame_ratio * (CHANGE_DIR_SCROLL_SPEED / 100))
345                   + (tux.physic.get_velocity_x() * frame_ratio + tux.physic.get_acceleration_x() * frame_ratio * frame_ratio);
346 */
347
348 scroll_x += ((final_scroll_x - scroll_x) / CHANGE_DIR_SCROLL_SPEED) * frame_ratio;
349
350       // std::cerr << tux_pos_x << " " << final_scroll_x << " " << scroll_x << std::endl;
351
352     }
353     else
354     {
355       if (tux.physic.get_velocity_x() > 0 && scroll_x < tux_pos_x - (screen->w - X_SPACE))
356         scroll_x = tux_pos_x - (screen->w - X_SPACE);
357       else if (tux.physic.get_velocity_x() < 0 && scroll_x > tux_pos_x - X_SPACE && debug_mode)
358         scroll_x = tux_pos_x - X_SPACE;
359       else
360       {
361         if (tux.dir == RIGHT && scroll_x < tux_pos_x - (screen->w - X_SPACE))
362             scroll_x = tux_pos_x - (screen->w - X_SPACE);
363         else if (tux.dir == LEFT && scroll_x > tux_pos_x - X_SPACE && debug_mode)
364             scroll_x = tux_pos_x - X_SPACE;
365       }
366     }
367   }
368
369   else /*no debug*/
370   {
371     if (scroll_x < tux_pos_x - (screen->w - X_SPACE))
372       scroll_x = tux_pos_x - (screen->w - X_SPACE);
373     }
374
375   // this code prevent the screen to scroll before the start or after the level's end
376   if(scroll_x < 0)
377     scroll_x = 0;
378   if(scroll_x > level->width * 32 - screen->w)
379     scroll_x = level->width * 32 - screen->w;
380 }
381
382 void
383 World::collision_handler()
384 {
385   // CO_BULLET & CO_BADGUY check
386   for(unsigned int i = 0; i < bullets.size(); ++i)
387     {
388       for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
389         {
390           if((*j)->dying != DYING_NOT)
391             continue;
392           
393           if(rectcollision(bullets[i].base, (*j)->base))
394             {
395               // We have detected a collision and now call the
396               // collision functions of the collided objects.
397               // collide with bad_guy first, since bullet_collision will
398               // delete the bullet
399               (*j)->collision(0, CO_BULLET);
400               bullets[i].collision(CO_BADGUY);
401               break; // bullet is invalid now, so break
402             }
403         }
404     }
405
406   /* CO_BADGUY & CO_BADGUY check */
407   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
408     {
409       if((*i)->dying != DYING_NOT)
410         continue;
411       
412       BadGuys::iterator j = i;
413       ++j;
414       for (; j != bad_guys.end(); ++j)
415         {
416           if(j == i || (*j)->dying != DYING_NOT)
417             continue;
418
419           if(rectcollision((*i)->base, (*j)->base))
420             {
421               // We have detected a collision and now call the
422               // collision functions of the collided objects.
423               (*j)->collision(*i, CO_BADGUY);
424               (*i)->collision(*j, CO_BADGUY);
425             }
426         }
427     }
428
429   if(tux.dying != DYING_NOT) return;
430     
431   // CO_BADGUY & CO_PLAYER check 
432   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
433     {
434       if((*i)->dying != DYING_NOT)
435         continue;
436       
437       if(rectcollision_offset((*i)->base, tux.base, 0, 0))
438         {
439           // We have detected a collision and now call the collision
440           // functions of the collided objects.
441           if (tux.previous_base.y < tux.base.y &&
442               tux.previous_base.y + tux.previous_base.height 
443               < (*i)->base.y + (*i)->base.height/2
444               && !tux.invincible_timer.started())
445             {
446               (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
447             }
448           else
449             {
450               tux.collision(*i, CO_BADGUY);
451               (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
452             }
453         }
454     }
455
456   // CO_UPGRADE & CO_PLAYER check
457   for(unsigned int i = 0; i < upgrades.size(); ++i)
458     {
459       if(rectcollision(upgrades[i].base, tux.base))
460         {
461           // We have detected a collision and now call the collision
462           // functions of the collided objects.
463           upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
464         }
465     }
466 }
467
468 void
469 World::add_score(float x, float y, int s)
470 {
471   player_status.score += s;
472
473   FloatingScore* new_floating_score = new FloatingScore();
474   new_floating_score->init(x,y,s);
475   floating_scores.push_back(new_floating_score);
476 }
477
478 void
479 World::add_bouncy_distro(float x, float y)
480 {
481   BouncyDistro* new_bouncy_distro = new BouncyDistro();
482   new_bouncy_distro->init(x,y);
483   bouncy_distros.push_back(new_bouncy_distro);
484 }
485
486 void
487 World::add_broken_brick(Tile* tile, float x, float y)
488 {
489   add_broken_brick_piece(tile, x, y, -1, -4);
490   add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
491
492   add_broken_brick_piece(tile, x + 16, y, 1, -4);
493   add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
494 }
495
496 void
497 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
498 {
499   BrokenBrick* new_broken_brick = new BrokenBrick();
500   new_broken_brick->init(tile, x, y, xm, ym);
501   broken_bricks.push_back(new_broken_brick);
502 }
503
504 void
505 World::add_bouncy_brick(float x, float y)
506 {
507   BouncyBrick* new_bouncy_brick = new BouncyBrick();
508   new_bouncy_brick->init(x,y);
509   bouncy_bricks.push_back(new_bouncy_brick);
510 }
511
512 BadGuy*
513 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
514 {
515   BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
516   bad_guys.push_back(badguy);
517   return badguy;
518 }
519
520 void
521 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
522 {
523   Upgrade new_upgrade;
524   new_upgrade.init(x,y,dir,kind);
525   upgrades.push_back(new_upgrade);
526 }
527
528 void 
529 World::add_bullet(float x, float y, float xm, Direction dir)
530 {
531   if(bullets.size() > MAX_BULLETS-1)
532     return;
533
534   Bullet new_bullet;
535   new_bullet.init(x,y,xm,dir);
536   bullets.push_back(new_bullet);
537   
538   play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
539 }
540
541 void
542 World::play_music(int musictype)
543 {
544   currentmusic = musictype;
545   switch(currentmusic) {
546     case HURRYUP_MUSIC:
547       music_manager->play_music(get_level()->get_level_music_fast());
548       break;
549     case LEVEL_MUSIC:
550       music_manager->play_music(get_level()->get_level_music());
551       break;
552     case HERRING_MUSIC:
553       music_manager->play_music(herring_song);
554       break;
555     default:
556       music_manager->halt_music();
557       break;
558   }
559 }
560
561 int
562 World::get_music_type()
563 {
564   return currentmusic;
565 }
566
567 /* Break a brick: */
568 void
569 World::trybreakbrick(float x, float y, bool small)
570 {
571   Level* plevel = get_level();
572   
573   Tile* tile = gettile(x, y);
574   if (tile->brick)
575     {
576       if (tile->data > 0)
577         {
578           /* Get a distro from it: */
579           add_bouncy_distro(((int)(x + 1) / 32) * 32,
580                                   (int)(y / 32) * 32);
581
582           // TODO: don't handle this in a global way but per-tile...
583           if (!counting_distros)
584             {
585               counting_distros = true;
586               distro_counter = 5;
587             }
588           else
589             {
590               distro_counter--;
591             }
592
593           if (distro_counter <= 0)
594             {
595               counting_distros = false;
596               plevel->change(x, y, TM_IA, tile->next_tile);
597             }
598
599           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
600           player_status.score = player_status.score + SCORE_DISTRO;
601           player_status.distros++;
602         }
603       else if (!small)
604         {
605           /* Get rid of it: */
606           plevel->change(x, y, TM_IA, tile->next_tile);
607           
608           /* Replace it with broken bits: */
609           add_broken_brick(tile, 
610                                  ((int)(x + 1) / 32) * 32,
611                                  (int)(y / 32) * 32);
612           
613           /* Get some score: */
614           play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
615           player_status.score = player_status.score + SCORE_BRICK;
616         }
617     }
618 }
619
620 /* Empty a box: */
621 void
622 World::tryemptybox(float x, float y, Direction col_side)
623 {
624   Tile* tile = gettile(x,y);
625   if (!tile->fullbox)
626     return;
627
628   // according to the collision side, set the upgrade direction
629   if(col_side == LEFT)
630     col_side = RIGHT;
631   else
632     col_side = LEFT;
633
634   int posx = ((int)(x+1) / 32) * 32;
635   int posy = (int)(y/32) * 32 - 32;
636   switch(tile->data)
637     {
638     case 1: // Box with a distro!
639       add_bouncy_distro(posx, posy);
640       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
641       player_status.score = player_status.score + SCORE_DISTRO;
642       player_status.distros++;
643       break;
644
645     case 2: // Add an upgrade!
646       if (tux.size == SMALL)     /* Tux is small, add mints! */
647         add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
648       else     /* Tux is big, add an iceflower: */
649         add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
650       play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
651       break;
652
653     case 3: // Add a golden herring
654       add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
655       break;
656
657     case 4: // Add a 1up extra
658       add_upgrade(posx, posy, col_side, UPGRADE_1UP);
659       break;
660     default:
661       break;
662     }
663
664   /* Empty the box: */
665   level->change(x, y, TM_IA, tile->next_tile);
666 }
667
668 /* Try to grab a distro: */
669 void
670 World::trygrabdistro(float x, float y, int bounciness)
671 {
672   Tile* tile = gettile(x, y);
673   if (tile && tile->distro)
674     {
675       level->change(x, y, TM_IA, tile->next_tile);
676       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
677
678       if (bounciness == BOUNCE)
679         {
680           add_bouncy_distro(((int)(x + 1) / 32) * 32,
681                                   (int)(y / 32) * 32);
682         }
683
684       player_status.score = player_status.score + SCORE_DISTRO;
685       player_status.distros++;
686     }
687 }
688
689 /* Try to bump a bad guy from below: */
690 void
691 World::trybumpbadguy(float x, float y)
692 {
693   // Bad guys: 
694   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
695     {
696       if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
697           (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
698         {
699           (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
700         }
701     }
702
703   // Upgrades:
704   for (unsigned int i = 0; i < upgrades.size(); i++)
705     {
706       if (upgrades[i].base.height == 32 &&
707           upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
708           upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
709         {
710           upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
711         }
712     }
713 }
714
715 /* EOF */
716