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