3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Matthias Braun <matze@braunis.de
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "lispreader.h"
32 #include "background.h"
33 #include "particlesystem.h"
36 #include "sound_manager.h"
38 #include "resources.h"
39 #include "interactive_object.h"
42 Sector* Sector::_current = 0;
45 : gravity(10), player(0), solids(0), background(0), camera(0),
46 currentmusic(LEVEL_MUSIC)
48 song_title = "Mortimers_chipdisko.mod";
49 player = new Player();
55 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
59 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
68 Sector::parse(LispReader& lispreader)
72 for(lisp_object_t* cur = lispreader.get_lisp(); !lisp_nil_p(cur);
73 cur = lisp_cdr(cur)) {
74 std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
75 // FIXME: doesn't handle empty data
76 lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
77 LispReader reader(lisp_cdr(lisp_car(cur)));
80 name = lisp_string(data);
81 } else if(token == "gravity") {
82 gravity = lisp_real(data);
83 } else if(token == "music") {
84 song_title = lisp_string(data);
86 } else if(token == "camera") {
88 std::cerr << "Warning: More than 1 camera defined in sector.\n";
91 camera = new Camera(this);
94 } else if(token == "background") {
95 background = new Background(reader);
96 add_object(background);
97 } else if(token == "playerspawn") {
98 SpawnPoint* sp = new SpawnPoint;
99 reader.read_string("name", sp->name);
100 reader.read_float("x", sp->pos.x);
101 reader.read_float("y", sp->pos.y);
102 spawnpoints.push_back(sp);
103 } else if(token == "tilemap") {
104 TileMap* tilemap = new TileMap(reader);
107 if(tilemap->is_solid()) {
109 std::cerr << "Warning multiple solid tilemaps in sector.\n";
114 } else if(badguykind_from_string(token) != BAD_INVALID) {
115 add_object(new BadGuy(badguykind_from_string(token), reader));
116 } else if(token == "trampoline") {
117 add_object(new Trampoline(reader));
118 } else if(token == "flying-platform") {
119 add_object(new FlyingPlatform(reader));
120 } else if(token == "particles-snow") {
121 SnowParticleSystem* partsys = new SnowParticleSystem();
122 partsys->parse(reader);
124 } else if(token == "particles-clouds") {
125 CloudParticleSystem* partsys = new CloudParticleSystem();
126 partsys->parse(reader);
128 } else if(token == "door") {
129 add_object(new Door(reader));
131 std::cerr << "Unknown object type '" << token << "'.\n";
136 std::cerr << "sector '" << name << "' does not contain a camera.\n";
137 camera = new Camera(this);
141 throw std::runtime_error("sector does not contain a solid tile layer.");
145 Sector::parse_old_format(LispReader& reader)
150 reader.read_float("gravity", gravity);
152 std::string backgroundimage;
153 reader.read_string("background", backgroundimage);
155 reader.read_float("bkgd_speed", bgspeed);
157 Color bkgd_top, bkgd_bottom;
158 int r = 0, g = 0, b = 128;
159 reader.read_int("bkgd_red_top", r);
160 reader.read_int("bkgd_green_top", g);
161 reader.read_int("bkgd_blue_top", b);
166 reader.read_int("bkgd_red_bottom", r);
167 reader.read_int("bkgd_green_bottom", g);
168 reader.read_int("bkgd_blue_bottom", b);
170 bkgd_bottom.green = g;
171 bkgd_bottom.blue = b;
173 if(backgroundimage != "") {
174 background = new Background;
175 background->set_image(backgroundimage, bgspeed);
176 add_object(background);
178 background = new Background;
179 background->set_gradient(bkgd_top, bkgd_bottom);
180 add_object(background);
183 std::string particlesystem;
184 reader.read_string("particle_system", particlesystem);
185 if(particlesystem == "clouds")
186 add_object(new CloudParticleSystem());
187 else if(particlesystem == "snow")
188 add_object(new SnowParticleSystem());
190 Vector startpos(100, 170);
191 reader.read_float("start_pos_x", startpos.x);
192 reader.read_float("start_pos_y", startpos.y);
194 SpawnPoint* spawn = new SpawnPoint;
195 spawn->pos = startpos;
196 spawn->name = "main";
197 spawnpoints.push_back(spawn);
199 song_title = "Mortimers_chipdisko.mod";
200 reader.read_string("music", song_title);
203 int width, height = 15;
204 reader.read_int("width", width);
205 reader.read_int("height", height);
207 std::vector<unsigned int> tiles;
208 if(reader.read_int_vector("interactive-tm", tiles)
209 || reader.read_int_vector("tilemap", tiles)) {
210 TileMap* tilemap = new TileMap();
211 tilemap->set(width, height, tiles, LAYER_TILES, true);
216 if(reader.read_int_vector("background-tm", tiles)) {
217 TileMap* tilemap = new TileMap();
218 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
222 if(reader.read_int_vector("foreground-tm", tiles)) {
223 TileMap* tilemap = new TileMap();
224 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
228 // TODO read resetpoints
232 lisp_object_t* cur = 0;
233 if(reader.read_lisp("objects", cur)) {
234 while(!lisp_nil_p(cur)) {
235 lisp_object_t* data = lisp_car(cur);
236 std::string object_type = lisp_symbol(lisp_car(data));
238 LispReader reader(lisp_cdr(data));
240 if(object_type == "trampoline") {
241 add_object(new Trampoline(reader));
243 else if(object_type == "flying-platform") {
244 add_object(new FlyingPlatform(reader));
247 BadGuyKind kind = badguykind_from_string(object_type);
248 add_object(new BadGuy(kind, reader));
257 camera = new Camera(this);
262 Sector::write(LispWriter& writer)
264 writer.write_string("name", name);
265 writer.write_float("gravity", gravity);
267 for(GameObjects::iterator i = gameobjects.begin();
268 i != gameobjects.end(); ++i) {
269 Serializable* serializable = dynamic_cast<Serializable*> (*i);
271 serializable->write(writer);
276 Sector::add_object(GameObject* object)
278 gameobjects_new.push_back(object);
282 Sector::activate(const std::string& spawnpoint)
286 // Apply bonuses from former levels
287 switch (player_status.bonus)
289 case PlayerStatus::NO_BONUS:
292 case PlayerStatus::FLOWER_BONUS:
293 player->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
296 case PlayerStatus::GROWUP_BONUS:
302 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
304 if((*i)->name == spawnpoint) {
310 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
312 player->move(sp->pos);
315 camera->reset(Vector(player->base.x, player->base.y));
319 Sector::action(float elapsed_time)
321 player->check_bounds(camera);
323 /* update objects (don't use iterators here, because the list might change
324 * during the iteration)
326 for(size_t i = 0; i < gameobjects.size(); ++i)
327 if(gameobjects[i]->is_valid())
328 gameobjects[i]->action(elapsed_time);
330 /* Handle all possible collisions. */
333 /** cleanup marked objects */
334 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
335 i != gameobjects.end(); /* nothing */) {
336 if((*i)->is_valid() == false) {
337 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
339 badguys.erase(std::remove(badguys.begin(), badguys.end(), badguy),
342 Bullet* bullet = dynamic_cast<Bullet*> (*i);
345 std::remove(bullets.begin(), bullets.end(), bullet),
348 InteractiveObject* interactive_object =
349 dynamic_cast<InteractiveObject*> (*i);
350 if(interactive_object) {
351 interactive_objects.erase(
352 std::remove(interactive_objects.begin(), interactive_objects.end(),
353 interactive_object), interactive_objects.end());
355 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
358 std::remove(upgrades.begin(), upgrades.end(), upgrade),
361 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
364 std::remove(trampolines.begin(), trampolines.end(), trampoline),
367 FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
368 if(flying_platform) {
369 flying_platforms.erase(
370 std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
371 flying_platforms.end());
375 i = gameobjects.erase(i);
381 /* add newly created objects */
382 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
383 i != gameobjects_new.end(); ++i)
385 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
387 badguys.push_back(badguy);
388 Bullet* bullet = dynamic_cast<Bullet*> (*i);
390 bullets.push_back(bullet);
391 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
393 upgrades.push_back(upgrade);
394 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
396 trampolines.push_back(trampoline);
397 FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (*i);
399 flying_platforms.push_back(flying_platform);
400 InteractiveObject* interactive_object
401 = dynamic_cast<InteractiveObject*> (*i);
402 if(interactive_object)
403 interactive_objects.push_back(interactive_object);
405 gameobjects.push_back(*i);
407 gameobjects_new.clear();
412 Sector::draw(DrawingContext& context)
414 context.push_transform();
415 context.set_translation(camera->get_translation());
417 for(GameObjects::iterator i = gameobjects.begin();
418 i != gameobjects.end(); ++i) {
419 if( (*i)->is_valid() )
423 context.pop_transform();
427 Sector::collision_handler()
429 // CO_BULLET & CO_BADGUY check
430 for(unsigned int i = 0; i < bullets.size(); ++i)
432 for (BadGuys::iterator j = badguys.begin(); j != badguys.end(); ++j)
434 if((*j)->dying != DYING_NOT)
437 if(rectcollision(bullets[i]->base, (*j)->base))
439 // We have detected a collision and now call the
440 // collision functions of the collided objects.
441 (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
442 bullets[i]->collision(CO_BADGUY);
443 break; // bullet is invalid now, so break
448 /* CO_BADGUY & CO_BADGUY check */
449 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
451 if((*i)->dying != DYING_NOT)
454 BadGuys::iterator j = i;
456 for (; j != badguys.end(); ++j)
458 if(j == i || (*j)->dying != DYING_NOT)
461 if(rectcollision((*i)->base, (*j)->base))
463 // We have detected a collision and now call the
464 // collision functions of the collided objects.
465 (*j)->collision(*i, CO_BADGUY);
466 (*i)->collision(*j, CO_BADGUY);
470 if(player->dying != DYING_NOT) return;
472 // CO_BADGUY & CO_PLAYER check
473 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
475 if((*i)->dying != DYING_NOT)
478 if(rectcollision_offset((*i)->base, player->base, 0, 0))
480 // We have detected a collision and now call the collision
481 // functions of the collided objects.
482 if (player->previous_base.y < player->base.y &&
483 player->previous_base.y + player->previous_base.height
484 < (*i)->base.y + (*i)->base.height/2
485 && !player->invincible_timer.started())
487 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
491 player->collision(*i, CO_BADGUY);
492 (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
497 // CO_UPGRADE & CO_PLAYER check
498 for(unsigned int i = 0; i < upgrades.size(); ++i)
500 if(rectcollision(upgrades[i]->base, player->base))
502 // We have detected a collision and now call the collision
503 // functions of the collided objects.
504 upgrades[i]->collision(player, CO_PLAYER, COLLISION_NORMAL);
508 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
509 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
511 if (rectcollision((*i)->base, player->base))
513 if (player->previous_base.y < player->base.y &&
514 player->previous_base.y + player->previous_base.height
515 < (*i)->base.y + (*i)->base.height/2)
517 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
519 else if (player->previous_base.y <= player->base.y)
521 player->collision(*i, CO_TRAMPOLINE);
522 (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
527 // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
528 for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
530 if (rectcollision((*i)->base, player->base))
532 if (player->previous_base.y < player->base.y &&
533 player->previous_base.y + player->previous_base.height
534 < (*i)->base.y + (*i)->base.height/2)
536 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
537 player->collision(*i, CO_FLYING_PLATFORM);
539 /* else if (player->previous_base.y <= player->base.y)
547 Sector::add_score(const Vector& pos, int s)
549 player_status.score += s;
551 add_object(new FloatingScore(pos, s));
555 Sector::add_bouncy_distro(const Vector& pos)
557 add_object(new BouncyDistro(pos));
561 Sector::add_broken_brick(const Vector& pos, Tile* tile)
563 add_broken_brick_piece(pos, Vector(-1, -4), tile);
564 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
566 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
567 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
571 Sector::add_broken_brick_piece(const Vector& pos, const Vector& movement,
574 add_object(new BrokenBrick(tile, pos, movement));
578 Sector::add_bouncy_brick(const Vector& pos)
580 add_object(new BouncyBrick(pos));
584 Sector::add_bad_guy(float x, float y, BadGuyKind kind)
586 BadGuy* badguy = new BadGuy(kind, x, y);
592 Sector::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
594 add_object(new Upgrade(pos, dir, kind));
598 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
600 if(player->got_power == Player::FIRE_POWER)
602 if(bullets.size() > MAX_FIRE_BULLETS-1)
605 else if(player->got_power == Player::ICE_POWER)
607 if(bullets.size() > MAX_ICE_BULLETS-1)
611 Bullet* new_bullet = 0;
612 if(player->got_power == Player::FIRE_POWER)
613 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
614 else if(player->got_power == Player::ICE_POWER)
615 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
617 throw std::runtime_error("wrong bullet type.");
618 add_object(new_bullet);
620 sound_manager->play_sound(sounds[SND_SHOOT]);
627 Sector::trybreakbrick(const Vector& pos, bool small)
629 Tile* tile = solids->get_tile_at(pos);
633 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
634 throw SuperTuxException(errmsg, __FILE__, __LINE__);
637 if (tile->attributes & Tile::BRICK)
641 /* Get a distro from it: */
643 Vector(((int)(pos.x + 1) / 32) * 32, (int)(pos.y / 32) * 32));
645 // TODO: don't handle this in a global way but per-tile...
646 if (!counting_distros)
648 counting_distros = true;
656 if (distro_counter <= 0)
658 counting_distros = false;
659 solids->change_at(pos, tile->next_tile);
662 sound_manager->play_sound(sounds[SND_DISTRO]);
663 player_status.score = player_status.score + SCORE_DISTRO;
664 player_status.distros++;
670 solids->change_at(pos, tile->next_tile);
672 /* Replace it with broken bits: */
673 add_broken_brick(Vector(
674 ((int)(pos.x + 1) / 32) * 32,
675 (int)(pos.y / 32) * 32), tile);
677 /* Get some score: */
678 sound_manager->play_sound(sounds[SND_BRICK]);
679 player_status.score = player_status.score + SCORE_BRICK;
690 Sector::tryemptybox(const Vector& pos, Direction col_side)
692 Tile* tile = solids->get_tile_at(pos);
696 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
697 throw SuperTuxException(errmsg, __FILE__, __LINE__);
701 if (!(tile->attributes & Tile::FULLBOX))
704 // according to the collision side, set the upgrade direction
710 int posx = ((int)(pos.x+1) / 32) * 32;
711 int posy = (int)(pos.y/32) * 32 - 32;
714 case 1: // Box with a distro!
715 add_bouncy_distro(Vector(posx, posy));
716 sound_manager->play_sound(sounds[SND_DISTRO]);
717 player_status.score = player_status.score + SCORE_DISTRO;
718 player_status.distros++;
721 case 2: // Add a fire flower upgrade!
722 if (player->size == SMALL) /* Tux is small, add mints! */
723 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
724 else /* Tux is big, add a fireflower: */
725 add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
726 sound_manager->play_sound(sounds[SND_UPGRADE]);
729 case 5: // Add an ice flower upgrade!
730 if (player->size == SMALL) /* Tux is small, add mints! */
731 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
732 else /* Tux is big, add an iceflower: */
733 add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
734 sound_manager->play_sound(sounds[SND_UPGRADE]);
737 case 3: // Add a golden herring
738 add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
741 case 4: // Add a 1up extra
742 add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
749 solids->change_at(pos, tile->next_tile);
752 /* Try to grab a distro: */
754 Sector::trygrabdistro(const Vector& pos, int bounciness)
756 Tile* tile = solids->get_tile_at(pos);
760 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
761 throw SuperTuxException(errmsg, __FILE__, __LINE__); */
763 //Bad tiles (i.e. tiles that are not defined in supertux.stgt but appear in the map) are changed to ID 0 (blank tile)
764 std::cout << "Warning: Undefined tile at " <<(int)pos.x/32 << "/" << (int)pos.y/32 << " (ID: " << (int)solids->get_tile_id_at(pos) << ")" << std::endl;
765 solids->change_at(pos,0);
766 tile = solids->get_tile_at(pos);
770 if (!(tile->attributes & Tile::COIN))
773 solids->change_at(pos, tile->next_tile);
774 sound_manager->play_sound(sounds[SND_DISTRO]);
776 if (bounciness == BOUNCE)
778 add_bouncy_distro(Vector(((int)(pos.x + 1) / 32) * 32,
779 (int)(pos.y / 32) * 32));
782 player_status.score = player_status.score + SCORE_DISTRO;
783 player_status.distros++;
787 /* Try to bump a bad guy from below: */
789 Sector::trybumpbadguy(const Vector& pos)
792 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
794 if ((*i)->base.x >= pos.x - 32 && (*i)->base.x <= pos.x + 32 &&
795 (*i)->base.y >= pos.y - 16 && (*i)->base.y <= pos.y + 16)
797 (*i)->collision(player, CO_PLAYER, COLLISION_BUMP);
802 for (unsigned int i = 0; i < upgrades.size(); i++)
804 if (upgrades[i]->base.height == 32 &&
805 upgrades[i]->base.x >= pos.x - 32 && upgrades[i]->base.x <= pos.x + 32 &&
806 upgrades[i]->base.y >= pos.y - 16 && upgrades[i]->base.y <= pos.y + 16)
808 upgrades[i]->collision(player, CO_PLAYER, COLLISION_BUMP);
819 level_song = sound_manager->load_music(datadir + "/music/" + song_title);
821 song_path = (char *) malloc(sizeof(char) * datadir.length() +
822 strlen(song_title.c_str()) + 8 + 5);
823 song_subtitle = strdup(song_title.c_str());
824 strcpy(strstr(song_subtitle, "."), "\0");
825 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
826 song_subtitle, strstr(song_title.c_str(), "."));
827 if(!sound_manager->exists_music(song_path)) {
828 level_song_fast = level_song;
830 level_song_fast = sound_manager->load_music(song_path);
837 Sector::play_music(int type)
840 switch(currentmusic) {
842 sound_manager->play_music(level_song_fast);
845 sound_manager->play_music(level_song);
848 sound_manager->play_music(herring_song);
851 sound_manager->halt_music();
857 Sector::get_music_type()