fixed bug I just introduced
[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 #include "gameobjs.h"
36 #include "camera.h"
37 #include "display_manager.h"
38 #include "background.h"
39 #include "tilemap.h"
40
41 Surface* img_distro[4];
42
43 World* World::current_ = 0;
44
45 World::World(const std::string& filename, int level_nr)
46 {
47   // FIXME: Move this to action and draw and everywhere else where the
48   // world calls child functions
49   current_ = this;
50
51   level = new Level();
52   if(level_nr >= 0) {
53     level->load(filename, level_nr, this);
54   } else {
55     level->load(filename, this);
56   }
57
58   tux = new Player(displaymanager);
59   add_object(tux);
60
61   set_defaults();
62
63   level->load_gfx();
64   // add background
65   activate_particle_systems();
66   background = new Background(displaymanager);
67   if(level->img_bkgd) {
68     background->set_image(level->img_bkgd, level->bkgd_speed);
69   } else {
70     background->set_gradient(level->bkgd_top, level->bkgd_bottom);
71   }
72   add_object(background);
73
74   // add tilemap
75   add_object(new TileMap(displaymanager, level));
76   level->load_song();
77
78   camera = new Camera(tux, level);
79   add_object(camera);               
80
81   apply_bonuses();
82 }
83
84 void
85 World::apply_bonuses()
86 {
87   // Apply bonuses from former levels
88   switch (player_status.bonus)
89     {
90     case PlayerStatus::NO_BONUS:
91       break;
92                                                                                 
93     case PlayerStatus::FLOWER_BONUS:
94       tux->got_power = Player::FIRE_POWER;  // FIXME: add ice power to here
95       // fall through
96                                                                                 
97     case PlayerStatus::GROWUP_BONUS:
98       tux->grow();
99       break;
100     }
101 }
102
103 World::~World()
104 {
105   for (std::vector<GameObject*>::iterator i = gameobjects.begin();
106           i != gameobjects.end(); ++i) {
107     delete *i;
108   }
109
110   delete level;
111 }
112
113 void
114 World::set_defaults()
115 {
116   player_status.score_multiplier = 1;
117
118   counting_distros = false;
119   distro_counter = 0;
120
121   /* set current song/music */
122   currentmusic = LEVEL_MUSIC;
123 }
124
125 void
126 World::add_object(GameObject* object)
127 {
128   // XXX hack for now until new collision code is ready
129   BadGuy* badguy = dynamic_cast<BadGuy*> (object);
130   if(badguy)
131     bad_guys.push_back(badguy);
132   Bullet* bullet = dynamic_cast<Bullet*> (object);
133   if(bullet)
134     bullets.push_back(bullet);
135   Upgrade* upgrade = dynamic_cast<Upgrade*> (object);
136   if(upgrade)
137     upgrades.push_back(upgrade);
138   Trampoline* trampoline = dynamic_cast<Trampoline*> (object);
139   if(trampoline)
140     trampolines.push_back(trampoline);
141   FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (object);
142   if(flying_platform)
143     flying_platforms.push_back(flying_platform);
144
145   gameobjects.push_back(object);
146 }
147
148 void
149 World::parse_objects(lisp_object_t* cur)
150 {
151   while(!lisp_nil_p(cur)) {
152     lisp_object_t* data = lisp_car(cur);
153     std::string object_type = lisp_symbol(lisp_car(data));
154     
155     LispReader reader(lisp_cdr(data));
156
157     if(object_type == "trampoline") {
158       add_object(new Trampoline(displaymanager, reader));
159     }
160     else if(object_type == "flying-platform") {
161       add_object(new FlyingPlatform(displaymanager, reader));
162     }
163     else {
164       BadGuyKind kind = badguykind_from_string(object_type);
165       add_object(new BadGuy(displaymanager, kind, reader));
166     }
167       
168     cur = lisp_cdr(cur);
169   } 
170 }
171
172 void
173 World::activate_particle_systems()
174 {
175   if (level->particle_system == "clouds")
176     {
177       add_object(new CloudParticleSystem(displaymanager));
178     }
179   else if (level->particle_system == "snow")
180     {
181       add_object(new SnowParticleSystem(displaymanager));
182     }
183   else if (level->particle_system != "")
184     {
185       st_abort("unknown particle system specified in level", "");
186     }
187 }
188
189 void
190 World::draw()
191 {
192   /* Draw objects */
193   displaymanager.draw(*camera);
194 }
195
196 void
197 World::action(float elapsed_time)
198 {
199   tux->check_bounds(*camera,
200       level->back_scrolling, (bool)level->hor_autoscroll_speed);
201     
202   /* update objects (don't use iterators here, because the list might change
203    * during the iteration)
204    */
205   for(size_t i = 0; i < gameobjects.size(); ++i)
206     gameobjects[i]->action(elapsed_time);
207
208   /* Handle all possible collisions. */
209   collision_handler();
210  
211   /** cleanup marked objects */
212   for(std::vector<GameObject*>::iterator i = gameobjects.begin();
213       i != gameobjects.end(); /* nothing */) {
214     if((*i)->is_valid() == false) {
215       Drawable* drawable = dynamic_cast<Drawable*> (*i);
216       if(drawable)
217         displaymanager.remove_drawable(drawable);
218       BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
219       if(badguy) {
220         bad_guys.erase(std::remove(bad_guys.begin(), bad_guys.end(), badguy),
221             bad_guys.end());
222       }
223       Bullet* bullet = dynamic_cast<Bullet*> (*i);
224       if(bullet) {
225         bullets.erase(
226             std::remove(bullets.begin(), bullets.end(), bullet),
227             bullets.end());
228       }
229       Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
230       if(upgrade) {
231         upgrades.erase(
232             std::remove(upgrades.begin(), upgrades.end(), upgrade),
233             upgrades.end());
234       }
235       Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
236       if(trampoline) {
237         trampolines.erase(
238             std::remove(trampolines.begin(), trampolines.end(), trampoline),
239             trampolines.end());
240       }
241       FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
242       if(flying_platform) {
243         flying_platforms.erase(
244             std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
245             flying_platforms.end());
246       }
247       
248       delete *i;
249       i = gameobjects.erase(i);
250     } else {
251       ++i;
252     }
253   }
254 }
255
256 void
257 World::collision_handler()
258 {
259   // CO_BULLET & CO_BADGUY check
260   for(unsigned int i = 0; i < bullets.size(); ++i)
261     {
262       for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
263         {
264           if((*j)->dying != DYING_NOT)
265             continue;
266           
267           if(rectcollision(bullets[i]->base, (*j)->base))
268             {
269               // We have detected a collision and now call the
270               // collision functions of the collided objects.
271               (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
272               bullets[i]->collision(CO_BADGUY);
273               break; // bullet is invalid now, so break
274             }
275         }
276     }
277
278   /* CO_BADGUY & CO_BADGUY check */
279   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
280     {
281       if((*i)->dying != DYING_NOT)
282         continue;
283       
284       BadGuys::iterator j = i;
285       ++j;
286       for (; j != bad_guys.end(); ++j)
287         {
288           if(j == i || (*j)->dying != DYING_NOT)
289             continue;
290
291           if(rectcollision((*i)->base, (*j)->base))
292             {
293               // We have detected a collision and now call the
294               // collision functions of the collided objects.
295               (*j)->collision(*i, CO_BADGUY);
296               (*i)->collision(*j, CO_BADGUY);
297             }
298         }
299     }
300
301   if(tux->dying != DYING_NOT) return;
302     
303   // CO_BADGUY & CO_PLAYER check 
304   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
305     {
306       if((*i)->dying != DYING_NOT)
307         continue;
308       
309       if(rectcollision_offset((*i)->base, tux->base, 0, 0))
310         {
311           // We have detected a collision and now call the collision
312           // functions of the collided objects.
313           if (tux->previous_base.y < tux->base.y &&
314               tux->previous_base.y + tux->previous_base.height 
315               < (*i)->base.y + (*i)->base.height/2
316               && !tux->invincible_timer.started())
317             {
318               (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
319             }
320           else
321             {
322               tux->collision(*i, CO_BADGUY);
323               (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
324             }
325         }
326     }
327
328   // CO_UPGRADE & CO_PLAYER check
329   for(unsigned int i = 0; i < upgrades.size(); ++i)
330     {
331       if(rectcollision(upgrades[i]->base, tux->base))
332         {
333           // We have detected a collision and now call the collision
334           // functions of the collided objects.
335           upgrades[i]->collision(tux, CO_PLAYER, COLLISION_NORMAL);
336         }
337     }
338
339   // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
340   for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
341   {
342     if (rectcollision((*i)->base, tux->base))
343     {
344       if (tux->previous_base.y < tux->base.y &&
345           tux->previous_base.y + tux->previous_base.height 
346           < (*i)->base.y + (*i)->base.height/2)
347       {
348         (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
349       }
350       else if (tux->previous_base.y <= tux->base.y)
351       {
352         tux->collision(*i, CO_TRAMPOLINE);
353         (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
354       }
355     }
356   }
357
358   // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
359   for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
360   {
361     if (rectcollision((*i)->base, tux->base))
362     {
363       if (tux->previous_base.y < tux->base.y &&
364           tux->previous_base.y + tux->previous_base.height 
365           < (*i)->base.y + (*i)->base.height/2)
366       {
367         (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
368         tux->collision(*i, CO_FLYING_PLATFORM);
369       }
370 /*      else if (tux->previous_base.y <= tux->base.y)
371       {
372       }*/
373     }
374   }
375 }
376
377 void
378 World::add_score(const Vector& pos, int s)
379 {
380   player_status.score += s;
381
382   add_object(new FloatingScore(displaymanager, pos, s));
383 }
384
385 void
386 World::add_bouncy_distro(const Vector& pos)
387 {
388   add_object(new BouncyDistro(displaymanager, pos));
389 }
390
391 void
392 World::add_broken_brick(const Vector& pos, Tile* tile)
393 {
394   add_broken_brick_piece(pos, Vector(-1, -4), tile);
395   add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
396
397   add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
398   add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
399 }
400
401 void
402 World::add_broken_brick_piece(const Vector& pos, const Vector& movement,
403     Tile* tile)
404 {
405   add_object(new BrokenBrick(displaymanager, tile, pos, movement));
406 }
407
408 void
409 World::add_bouncy_brick(const Vector& pos)
410 {
411   add_object(new BouncyBrick(displaymanager, pos));
412 }
413
414 BadGuy*
415 World::add_bad_guy(float x, float y, BadGuyKind kind)
416 {
417   BadGuy* badguy = new BadGuy(displaymanager, kind, x, y);
418   add_object(badguy);
419   return badguy;
420 }
421
422 void
423 World::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
424 {
425   add_object(new Upgrade(displaymanager, pos, dir, kind));
426 }
427
428 bool
429 World::add_bullet(const Vector& pos, float xm, Direction dir)
430 {
431   if(tux->got_power == Player::FIRE_POWER)
432     {
433     if(bullets.size() > MAX_FIRE_BULLETS-1)
434       return false;
435     }
436   else if(tux->got_power == Player::ICE_POWER)
437     {
438     if(bullets.size() > MAX_ICE_BULLETS-1)
439       return false;
440     }
441
442   Bullet* new_bullet = 0;
443   if(tux->got_power == Player::FIRE_POWER)
444     new_bullet = new Bullet(displaymanager, pos, xm, dir, FIRE_BULLET);
445   else if(tux->got_power == Player::ICE_POWER)
446     new_bullet = new Bullet(displaymanager, pos, xm, dir, ICE_BULLET);
447   else
448     st_abort("wrong bullet type.", "");
449   add_object(new_bullet);
450   
451   play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
452
453   return true;
454 }
455
456 void
457 World::play_music(int musictype)
458 {
459   currentmusic = musictype;
460   switch(currentmusic) {
461     case HURRYUP_MUSIC:
462       music_manager->play_music(get_level()->get_level_music_fast());
463       break;
464     case LEVEL_MUSIC:
465       music_manager->play_music(get_level()->get_level_music());
466       break;
467     case HERRING_MUSIC:
468       music_manager->play_music(herring_song);
469       break;
470     default:
471       music_manager->halt_music();
472       break;
473   }
474 }
475
476 int
477 World::get_music_type()
478 {
479   return currentmusic;
480 }
481
482 /* Break a brick: */
483 bool
484 World::trybreakbrick(float x, float y, bool small)
485 {
486   Level* plevel = get_level();
487   
488   Tile* tile = gettile(x, y);
489   if (tile->brick)
490     {
491       if (tile->data > 0)
492         {
493           /* Get a distro from it: */
494           add_bouncy_distro(
495               Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32));
496
497           // TODO: don't handle this in a global way but per-tile...
498           if (!counting_distros)
499             {
500               counting_distros = true;
501               distro_counter = 5;
502             }
503           else
504             {
505               distro_counter--;
506             }
507
508           if (distro_counter <= 0)
509             {
510               counting_distros = false;
511               plevel->change(x, y, TM_IA, tile->next_tile);
512             }
513
514           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
515           player_status.score = player_status.score + SCORE_DISTRO;
516           player_status.distros++;
517           return true;
518         }
519       else if (!small)
520         {
521           /* Get rid of it: */
522           plevel->change(x, y, TM_IA, tile->next_tile);
523           
524           /* Replace it with broken bits: */
525           add_broken_brick(Vector(
526                                  ((int)(x + 1) / 32) * 32,
527                                  (int)(y / 32) * 32), tile);
528           
529           /* Get some score: */
530           play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
531           player_status.score = player_status.score + SCORE_BRICK;
532           
533           return true;
534         }
535     }
536
537   return false;
538 }
539
540 /* Empty a box: */
541 void
542 World::tryemptybox(float x, float y, Direction col_side)
543 {
544   Tile* tile = gettile(x,y);
545   if (!tile->fullbox)
546     return;
547
548   // according to the collision side, set the upgrade direction
549   if(col_side == LEFT)
550     col_side = RIGHT;
551   else
552     col_side = LEFT;
553
554   int posx = ((int)(x+1) / 32) * 32;
555   int posy = (int)(y/32) * 32 - 32;
556   switch(tile->data)
557     {
558     case 1: // Box with a distro!
559       add_bouncy_distro(Vector(posx, posy));
560       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
561       player_status.score = player_status.score + SCORE_DISTRO;
562       player_status.distros++;
563       break;
564
565     case 2: // Add a fire flower upgrade!
566       if (tux->size == SMALL)     /* Tux is small, add mints! */
567         add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
568       else     /* Tux is big, add a fireflower: */
569         add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
570       play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
571       break;
572     
573     case 5: // Add an ice flower upgrade!
574       if (tux->size == SMALL)     /* Tux is small, add mints! */
575         add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
576       else     /* Tux is big, add an iceflower: */
577         add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
578       play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
579       break;
580
581     case 3: // Add a golden herring
582       add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
583       break;
584
585     case 4: // Add a 1up extra
586       add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
587       break;
588     default:
589       break;
590     }
591
592   /* Empty the box: */
593   level->change(x, y, TM_IA, tile->next_tile);
594 }
595
596 /* Try to grab a distro: */
597 void
598 World::trygrabdistro(float x, float y, int bounciness)
599 {
600   Tile* tile = gettile(x, y);
601   if (tile && tile->distro)
602     {
603       level->change(x, y, TM_IA, tile->next_tile);
604       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
605
606       if (bounciness == BOUNCE)
607         {
608           add_bouncy_distro(Vector(((int)(x + 1) / 32) * 32,
609                                   (int)(y / 32) * 32));
610         }
611
612       player_status.score = player_status.score + SCORE_DISTRO;
613       player_status.distros++;
614     }
615 }
616
617 /* Try to bump a bad guy from below: */
618 void
619 World::trybumpbadguy(float x, float y)
620 {
621   // Bad guys: 
622   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
623     {
624       if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
625           (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
626         {
627           (*i)->collision(tux, CO_PLAYER, COLLISION_BUMP);
628         }
629     }
630
631   // Upgrades:
632   for (unsigned int i = 0; i < upgrades.size(); i++)
633     {
634       if (upgrades[i]->base.height == 32 &&
635           upgrades[i]->base.x >= x - 32 && upgrades[i]->base.x <= x + 32 &&
636           upgrades[i]->base.y >= y - 16 && upgrades[i]->base.y <= y + 16)
637         {
638           upgrades[i]->collision(tux, CO_PLAYER, COLLISION_BUMP);
639         }
640     }
641 }
642
643 /* EOF */
644