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 lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
76 LispReader reader(lisp_cdr(lisp_car(cur)));
79 name = lisp_string(data);
80 } else if(token == "gravity") {
81 gravity = lisp_integer(data);
82 } else if(token == "music") {
83 song_title = lisp_string(data);
85 } else if(token == "camera") {
87 std::cerr << "Warning: More than 1 camera defined in sector.\n";
90 camera = new Camera(this);
93 } else if(token == "background") {
94 background = new Background(reader);
95 add_object(background);
96 } else if(token == "playerspawn") {
97 SpawnPoint* sp = new SpawnPoint;
98 reader.read_string("name", sp->name);
99 reader.read_float("x", sp->pos.x);
100 reader.read_float("y", sp->pos.y);
101 spawnpoints.push_back(sp);
102 } else if(token == "tilemap") {
103 TileMap* tilemap = new TileMap(reader);
106 if(tilemap->is_solid()) {
108 std::cerr << "Warning multiple solid tilemaps in sector.\n";
113 } else if(badguykind_from_string(token) != BAD_INVALID) {
114 add_object(new BadGuy(badguykind_from_string(token), reader));
115 } else if(token == "trampoline") {
116 add_object(new Trampoline(reader));
117 } else if(token == "flying-platform") {
118 add_object(new FlyingPlatform(reader));
119 } else if(token == "particles-snow") {
120 SnowParticleSystem* partsys = new SnowParticleSystem();
121 partsys->parse(reader);
123 } else if(token == "particles-clouds") {
124 CloudParticleSystem* partsys = new CloudParticleSystem();
125 partsys->parse(reader);
127 } else if(token == "door") {
128 add_object(new Door(reader));
130 std::cerr << "Unknown object type '" << token << "'.\n";
135 std::cerr << "sector '" << name << "' does not contain a camera.\n";
136 camera = new Camera(this);
140 throw std::runtime_error("sector does not contain a solid tile layer.");
144 Sector::parse_old_format(LispReader& reader)
149 reader.read_float("gravity", gravity);
151 std::string backgroundimage;
152 reader.read_string("background", backgroundimage);
154 reader.read_float("bkgd_speed", bgspeed);
156 Color bkgd_top, bkgd_bottom;
157 int r = 0, g = 0, b = 128;
158 reader.read_int("bkgd_red_top", r);
159 reader.read_int("bkgd_green_top", g);
160 reader.read_int("bkgd_blue_top", b);
165 reader.read_int("bkgd_red_bottom", r);
166 reader.read_int("bkgd_green_bottom", g);
167 reader.read_int("bkgd_blue_bottom", b);
169 bkgd_bottom.green = g;
170 bkgd_bottom.blue = b;
172 if(backgroundimage != "") {
173 background = new Background;
174 background->set_image(backgroundimage, bgspeed);
175 add_object(background);
177 background = new Background;
178 background->set_gradient(bkgd_top, bkgd_bottom);
179 add_object(background);
182 std::string particlesystem;
183 reader.read_string("particle_system", particlesystem);
184 if(particlesystem == "clouds")
185 add_object(new CloudParticleSystem());
186 else if(particlesystem == "snow")
187 add_object(new SnowParticleSystem());
189 Vector startpos(100, 170);
190 reader.read_float("start_pos_x", startpos.x);
191 reader.read_float("start_pos_y", startpos.y);
193 SpawnPoint* spawn = new SpawnPoint;
194 spawn->pos = startpos;
195 spawn->name = "main";
196 spawnpoints.push_back(spawn);
198 song_title = "Mortimers_chipdisko.mod";
199 reader.read_string("music", song_title);
202 int width, height = 15;
203 reader.read_int("width", width);
204 reader.read_int("height", height);
206 std::vector<unsigned int> tiles;
207 if(reader.read_int_vector("interactive-tm", tiles)
208 || reader.read_int_vector("tilemap", tiles)) {
209 TileMap* tilemap = new TileMap();
210 tilemap->set(width, height, tiles, LAYER_TILES, true);
215 if(reader.read_int_vector("background-tm", tiles)) {
216 TileMap* tilemap = new TileMap();
217 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
221 if(reader.read_int_vector("foreground-tm", tiles)) {
222 TileMap* tilemap = new TileMap();
223 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
227 // TODO read resetpoints
231 lisp_object_t* cur = 0;
232 if(reader.read_lisp("objects", cur)) {
233 while(!lisp_nil_p(cur)) {
234 lisp_object_t* data = lisp_car(cur);
235 std::string object_type = lisp_symbol(lisp_car(data));
237 LispReader reader(lisp_cdr(data));
239 if(object_type == "trampoline") {
240 add_object(new Trampoline(reader));
242 else if(object_type == "flying-platform") {
243 add_object(new FlyingPlatform(reader));
246 BadGuyKind kind = badguykind_from_string(object_type);
247 add_object(new BadGuy(kind, reader));
256 camera = new Camera(this);
261 Sector::write(LispWriter& writer)
263 writer.write_string("name", name);
264 writer.write_float("gravity", gravity);
266 for(GameObjects::iterator i = gameobjects.begin();
267 i != gameobjects.end(); ++i) {
268 Serializable* serializable = dynamic_cast<Serializable*> (*i);
270 serializable->write(writer);
275 Sector::add_object(GameObject* object)
277 gameobjects_new.push_back(object);
281 Sector::activate(const std::string& spawnpoint)
285 // Apply bonuses from former levels
286 switch (player_status.bonus)
288 case PlayerStatus::NO_BONUS:
291 case PlayerStatus::FLOWER_BONUS:
292 player->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
295 case PlayerStatus::GROWUP_BONUS:
301 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
303 if((*i)->name == spawnpoint) {
309 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
311 player->move(sp->pos);
314 camera->reset(Vector(player->base.x, player->base.y));
318 Sector::action(float elapsed_time)
320 player->check_bounds(camera);
322 /* update objects (don't use iterators here, because the list might change
323 * during the iteration)
325 for(size_t i = 0; i < gameobjects.size(); ++i)
326 if(gameobjects[i]->is_valid())
327 gameobjects[i]->action(elapsed_time);
329 /* Handle all possible collisions. */
332 /** cleanup marked objects */
333 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
334 i != gameobjects.end(); /* nothing */) {
335 if((*i)->is_valid() == false) {
336 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
338 badguys.erase(std::remove(badguys.begin(), badguys.end(), badguy),
341 Bullet* bullet = dynamic_cast<Bullet*> (*i);
344 std::remove(bullets.begin(), bullets.end(), bullet),
347 InteractiveObject* interactive_object =
348 dynamic_cast<InteractiveObject*> (*i);
349 if(interactive_object) {
350 interactive_objects.erase(
351 std::remove(interactive_objects.begin(), interactive_objects.end(),
352 interactive_object), interactive_objects.end());
354 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
357 std::remove(upgrades.begin(), upgrades.end(), upgrade),
360 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
363 std::remove(trampolines.begin(), trampolines.end(), trampoline),
366 FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
367 if(flying_platform) {
368 flying_platforms.erase(
369 std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
370 flying_platforms.end());
374 i = gameobjects.erase(i);
380 /* add newly created objects */
381 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
382 i != gameobjects_new.end(); ++i)
384 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
386 badguys.push_back(badguy);
387 Bullet* bullet = dynamic_cast<Bullet*> (*i);
389 bullets.push_back(bullet);
390 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
392 upgrades.push_back(upgrade);
393 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
395 trampolines.push_back(trampoline);
396 FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (*i);
398 flying_platforms.push_back(flying_platform);
399 InteractiveObject* interactive_object
400 = dynamic_cast<InteractiveObject*> (*i);
401 if(interactive_object)
402 interactive_objects.push_back(interactive_object);
404 gameobjects.push_back(*i);
406 gameobjects_new.clear();
411 Sector::draw(DrawingContext& context)
413 context.push_transform();
414 context.set_translation(camera->get_translation());
416 for(GameObjects::iterator i = gameobjects.begin();
417 i != gameobjects.end(); ++i) {
418 if( (*i)->is_valid() )
422 context.pop_transform();
426 Sector::collision_handler()
428 // CO_BULLET & CO_BADGUY check
429 for(unsigned int i = 0; i < bullets.size(); ++i)
431 for (BadGuys::iterator j = badguys.begin(); j != badguys.end(); ++j)
433 if((*j)->dying != DYING_NOT)
436 if(rectcollision(bullets[i]->base, (*j)->base))
438 // We have detected a collision and now call the
439 // collision functions of the collided objects.
440 (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
441 bullets[i]->collision(CO_BADGUY);
442 break; // bullet is invalid now, so break
447 /* CO_BADGUY & CO_BADGUY check */
448 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
450 if((*i)->dying != DYING_NOT)
453 BadGuys::iterator j = i;
455 for (; j != badguys.end(); ++j)
457 if(j == i || (*j)->dying != DYING_NOT)
460 if(rectcollision((*i)->base, (*j)->base))
462 // We have detected a collision and now call the
463 // collision functions of the collided objects.
464 (*j)->collision(*i, CO_BADGUY);
465 (*i)->collision(*j, CO_BADGUY);
469 if(player->dying != DYING_NOT) return;
471 // CO_BADGUY & CO_PLAYER check
472 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
474 if((*i)->dying != DYING_NOT)
477 if(rectcollision_offset((*i)->base, player->base, 0, 0))
479 // We have detected a collision and now call the collision
480 // functions of the collided objects.
481 if (player->previous_base.y < player->base.y &&
482 player->previous_base.y + player->previous_base.height
483 < (*i)->base.y + (*i)->base.height/2
484 && !player->invincible_timer.started())
486 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
490 player->collision(*i, CO_BADGUY);
491 (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
496 // CO_UPGRADE & CO_PLAYER check
497 for(unsigned int i = 0; i < upgrades.size(); ++i)
499 if(rectcollision(upgrades[i]->base, player->base))
501 // We have detected a collision and now call the collision
502 // functions of the collided objects.
503 upgrades[i]->collision(player, CO_PLAYER, COLLISION_NORMAL);
507 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
508 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
510 if (rectcollision((*i)->base, player->base))
512 if (player->previous_base.y < player->base.y &&
513 player->previous_base.y + player->previous_base.height
514 < (*i)->base.y + (*i)->base.height/2)
516 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
518 else if (player->previous_base.y <= player->base.y)
520 player->collision(*i, CO_TRAMPOLINE);
521 (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
526 // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
527 for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
529 if (rectcollision((*i)->base, player->base))
531 if (player->previous_base.y < player->base.y &&
532 player->previous_base.y + player->previous_base.height
533 < (*i)->base.y + (*i)->base.height/2)
535 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
536 player->collision(*i, CO_FLYING_PLATFORM);
538 /* else if (player->previous_base.y <= player->base.y)
546 Sector::add_score(const Vector& pos, int s)
548 player_status.score += s;
550 add_object(new FloatingScore(pos, s));
554 Sector::add_bouncy_distro(const Vector& pos)
556 add_object(new BouncyDistro(pos));
560 Sector::add_broken_brick(const Vector& pos, Tile* tile)
562 add_broken_brick_piece(pos, Vector(-1, -4), tile);
563 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
565 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
566 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
570 Sector::add_broken_brick_piece(const Vector& pos, const Vector& movement,
573 add_object(new BrokenBrick(tile, pos, movement));
577 Sector::add_bouncy_brick(const Vector& pos)
579 add_object(new BouncyBrick(pos));
583 Sector::add_bad_guy(float x, float y, BadGuyKind kind)
585 BadGuy* badguy = new BadGuy(kind, x, y);
591 Sector::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
593 add_object(new Upgrade(pos, dir, kind));
597 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
599 if(player->got_power == Player::FIRE_POWER)
601 if(bullets.size() > MAX_FIRE_BULLETS-1)
604 else if(player->got_power == Player::ICE_POWER)
606 if(bullets.size() > MAX_ICE_BULLETS-1)
610 Bullet* new_bullet = 0;
611 if(player->got_power == Player::FIRE_POWER)
612 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
613 else if(player->got_power == Player::ICE_POWER)
614 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
616 throw std::runtime_error("wrong bullet type.");
617 add_object(new_bullet);
619 sound_manager->play_sound(sounds[SND_SHOOT]);
626 Sector::trybreakbrick(const Vector& pos, bool small)
628 Tile* tile = solids->get_tile_at(pos);
632 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
633 throw SuperTuxException(errmsg, __FILE__, __LINE__);
636 if (tile->attributes & Tile::BRICK)
640 /* Get a distro from it: */
642 Vector(((int)(pos.x + 1) / 32) * 32, (int)(pos.y / 32) * 32));
644 // TODO: don't handle this in a global way but per-tile...
645 if (!counting_distros)
647 counting_distros = true;
655 if (distro_counter <= 0)
657 counting_distros = false;
658 solids->change_at(pos, tile->next_tile);
661 sound_manager->play_sound(sounds[SND_DISTRO]);
662 player_status.score = player_status.score + SCORE_DISTRO;
663 player_status.distros++;
669 solids->change_at(pos, tile->next_tile);
671 /* Replace it with broken bits: */
672 add_broken_brick(Vector(
673 ((int)(pos.x + 1) / 32) * 32,
674 (int)(pos.y / 32) * 32), tile);
676 /* Get some score: */
677 sound_manager->play_sound(sounds[SND_BRICK]);
678 player_status.score = player_status.score + SCORE_BRICK;
689 Sector::tryemptybox(const Vector& pos, Direction col_side)
691 Tile* tile = solids->get_tile_at(pos);
695 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
696 throw SuperTuxException(errmsg, __FILE__, __LINE__);
700 if (!(tile->attributes & Tile::FULLBOX))
703 // according to the collision side, set the upgrade direction
709 int posx = ((int)(pos.x+1) / 32) * 32;
710 int posy = (int)(pos.y/32) * 32 - 32;
713 case 1: // Box with a distro!
714 add_bouncy_distro(Vector(posx, posy));
715 sound_manager->play_sound(sounds[SND_DISTRO]);
716 player_status.score = player_status.score + SCORE_DISTRO;
717 player_status.distros++;
720 case 2: // Add a fire flower upgrade!
721 if (player->size == SMALL) /* Tux is small, add mints! */
722 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
723 else /* Tux is big, add a fireflower: */
724 add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
725 sound_manager->play_sound(sounds[SND_UPGRADE]);
728 case 5: // Add an ice flower upgrade!
729 if (player->size == SMALL) /* Tux is small, add mints! */
730 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
731 else /* Tux is big, add an iceflower: */
732 add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
733 sound_manager->play_sound(sounds[SND_UPGRADE]);
736 case 3: // Add a golden herring
737 add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
740 case 4: // Add a 1up extra
741 add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
748 solids->change_at(pos, tile->next_tile);
751 /* Try to grab a distro: */
753 Sector::trygrabdistro(const Vector& pos, int bounciness)
755 Tile* tile = solids->get_tile_at(pos);
759 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
760 throw SuperTuxException(errmsg, __FILE__, __LINE__); */
762 //Bad tiles (i.e. tiles that are not defined in supertux.stgt but appear in the map) are changed to ID 0 (blank tile)
763 std::cout << "Warning: Undefined tile at " <<(int)pos.x/32 << "/" << (int)pos.y/32 << " (ID: " << (int)solids->get_tile_id_at(pos) << ")" << std::endl;
764 solids->change_at(pos,0);
765 tile = solids->get_tile_at(pos);
769 if (!(tile->attributes & Tile::COIN))
772 solids->change_at(pos, tile->next_tile);
773 sound_manager->play_sound(sounds[SND_DISTRO]);
775 if (bounciness == BOUNCE)
777 add_bouncy_distro(Vector(((int)(pos.x + 1) / 32) * 32,
778 (int)(pos.y / 32) * 32));
781 player_status.score = player_status.score + SCORE_DISTRO;
782 player_status.distros++;
786 /* Try to bump a bad guy from below: */
788 Sector::trybumpbadguy(const Vector& pos)
791 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
793 if ((*i)->base.x >= pos.x - 32 && (*i)->base.x <= pos.x + 32 &&
794 (*i)->base.y >= pos.y - 16 && (*i)->base.y <= pos.y + 16)
796 (*i)->collision(player, CO_PLAYER, COLLISION_BUMP);
801 for (unsigned int i = 0; i < upgrades.size(); i++)
803 if (upgrades[i]->base.height == 32 &&
804 upgrades[i]->base.x >= pos.x - 32 && upgrades[i]->base.x <= pos.x + 32 &&
805 upgrades[i]->base.y >= pos.y - 16 && upgrades[i]->base.y <= pos.y + 16)
807 upgrades[i]->collision(player, CO_PLAYER, COLLISION_BUMP);
818 level_song = sound_manager->load_music(datadir + "/music/" + song_title);
820 song_path = (char *) malloc(sizeof(char) * datadir.length() +
821 strlen(song_title.c_str()) + 8 + 5);
822 song_subtitle = strdup(song_title.c_str());
823 strcpy(strstr(song_subtitle, "."), "\0");
824 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
825 song_subtitle, strstr(song_title.c_str(), "."));
826 if(!sound_manager->exists_music(song_path)) {
827 level_song_fast = level_song;
829 level_song_fast = sound_manager->load_music(song_path);
836 Sector::play_music(int type)
839 switch(currentmusic) {
841 sound_manager->play_music(level_song_fast);
844 sound_manager->play_music(level_song);
847 sound_manager->play_music(herring_song);
850 sound_manager->halt_music();
856 Sector::get_music_type()