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.
29 #include "lispreader.h"
34 #include "background.h"
35 #include "particlesystem.h"
38 #include "sound_manager.h"
40 #include "resources.h"
41 #include "interactive_object.h"
44 Sector* Sector::_current = 0;
47 : gravity(10), player(0), solids(0), background(0), camera(0),
48 currentmusic(LEVEL_MUSIC)
50 song_title = "Mortimers_chipdisko.mod";
51 player = new Player();
57 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
61 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
70 Sector::parse(LispReader& lispreader)
74 for(lisp_object_t* cur = lispreader.get_lisp(); !lisp_nil_p(cur);
75 cur = lisp_cdr(cur)) {
76 std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
77 // FIXME: doesn't handle empty data
78 lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
79 LispReader reader(lisp_cdr(lisp_car(cur)));
82 name = lisp_string(data);
83 } else if(token == "gravity") {
84 gravity = lisp_real(data);
85 } else if(token == "music") {
86 song_title = lisp_string(data);
88 } else if(token == "camera") {
90 std::cerr << "Warning: More than 1 camera defined in sector.\n";
93 camera = new Camera(this);
96 } else if(token == "background") {
97 background = new Background(reader);
98 add_object(background);
99 } else if(token == "playerspawn") {
100 SpawnPoint* sp = new SpawnPoint;
101 reader.read_string("name", sp->name);
102 reader.read_float("x", sp->pos.x);
103 reader.read_float("y", sp->pos.y);
104 spawnpoints.push_back(sp);
105 } else if(token == "tilemap") {
106 TileMap* tilemap = new TileMap(reader);
109 if(tilemap->is_solid()) {
111 std::cerr << "Warning multiple solid tilemaps in sector.\n";
116 } else if(badguykind_from_string(token) != BAD_INVALID) {
117 add_object(new BadGuy(badguykind_from_string(token), reader));
118 } else if(token == "trampoline") {
119 add_object(new Trampoline(reader));
120 } else if(token == "flying-platform") {
121 add_object(new FlyingPlatform(reader));
122 } else if(token == "particles-snow") {
123 SnowParticleSystem* partsys = new SnowParticleSystem();
124 partsys->parse(reader);
126 } else if(token == "particles-clouds") {
127 CloudParticleSystem* partsys = new CloudParticleSystem();
128 partsys->parse(reader);
130 } else if(token == "door") {
131 add_object(new Door(reader));
133 std::cerr << "Unknown object type '" << token << "'.\n";
138 std::cerr << "sector '" << name << "' does not contain a camera.\n";
139 camera = new Camera(this);
143 throw std::runtime_error("sector does not contain a solid tile layer.");
147 Sector::parse_old_format(LispReader& reader)
152 reader.read_float("gravity", gravity);
154 std::string backgroundimage;
155 reader.read_string("background", backgroundimage);
157 reader.read_float("bkgd_speed", bgspeed);
159 Color bkgd_top, bkgd_bottom;
160 int r = 0, g = 0, b = 128;
161 reader.read_int("bkgd_red_top", r);
162 reader.read_int("bkgd_green_top", g);
163 reader.read_int("bkgd_blue_top", b);
168 reader.read_int("bkgd_red_bottom", r);
169 reader.read_int("bkgd_green_bottom", g);
170 reader.read_int("bkgd_blue_bottom", b);
172 bkgd_bottom.green = g;
173 bkgd_bottom.blue = b;
175 if(backgroundimage != "") {
176 background = new Background;
177 background->set_image(backgroundimage, bgspeed);
178 add_object(background);
180 background = new Background;
181 background->set_gradient(bkgd_top, bkgd_bottom);
182 add_object(background);
185 std::string particlesystem;
186 reader.read_string("particle_system", particlesystem);
187 if(particlesystem == "clouds")
188 add_object(new CloudParticleSystem());
189 else if(particlesystem == "snow")
190 add_object(new SnowParticleSystem());
192 Vector startpos(100, 170);
193 reader.read_float("start_pos_x", startpos.x);
194 reader.read_float("start_pos_y", startpos.y);
196 SpawnPoint* spawn = new SpawnPoint;
197 spawn->pos = startpos;
198 spawn->name = "main";
199 spawnpoints.push_back(spawn);
201 song_title = "Mortimers_chipdisko.mod";
202 reader.read_string("music", song_title);
205 int width, height = 15;
206 reader.read_int("width", width);
207 reader.read_int("height", height);
209 std::vector<unsigned int> tiles;
210 if(reader.read_int_vector("interactive-tm", tiles)
211 || reader.read_int_vector("tilemap", tiles)) {
212 TileMap* tilemap = new TileMap();
213 tilemap->set(width, height, tiles, LAYER_TILES, true);
218 if(reader.read_int_vector("background-tm", tiles)) {
219 TileMap* tilemap = new TileMap();
220 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
224 if(reader.read_int_vector("foreground-tm", tiles)) {
225 TileMap* tilemap = new TileMap();
226 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
230 // TODO read resetpoints
234 lisp_object_t* cur = 0;
235 if(reader.read_lisp("objects", cur)) {
236 while(!lisp_nil_p(cur)) {
237 lisp_object_t* data = lisp_car(cur);
238 std::string object_type = lisp_symbol(lisp_car(data));
240 LispReader reader(lisp_cdr(data));
242 if(object_type == "trampoline") {
243 add_object(new Trampoline(reader));
245 else if(object_type == "flying-platform") {
246 add_object(new FlyingPlatform(reader));
249 BadGuyKind kind = badguykind_from_string(object_type);
250 add_object(new BadGuy(kind, reader));
259 camera = new Camera(this);
264 Sector::write(LispWriter& writer)
266 writer.write_string("name", name);
267 writer.write_float("gravity", gravity);
268 writer.write_string("music", song_title);
270 for(GameObjects::iterator i = gameobjects.begin();
271 i != gameobjects.end(); ++i) {
272 Serializable* serializable = dynamic_cast<Serializable*> (*i);
274 serializable->write(writer);
279 Sector::add_object(GameObject* object)
281 gameobjects_new.push_back(object);
285 Sector::activate(const std::string& spawnpoint)
289 // Apply bonuses from former levels
290 switch (player_status.bonus)
292 case PlayerStatus::NO_BONUS:
295 case PlayerStatus::FLOWER_BONUS:
296 player->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
299 case PlayerStatus::GROWUP_BONUS:
305 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
307 if((*i)->name == spawnpoint) {
313 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
315 player->move(sp->pos);
318 camera->reset(Vector(player->base.x, player->base.y));
322 Sector::action(float elapsed_time)
324 player->check_bounds(camera);
326 /* update objects (don't use iterators here, because the list might change
327 * during the iteration)
329 for(size_t i = 0; i < gameobjects.size(); ++i)
330 if(gameobjects[i]->is_valid())
331 gameobjects[i]->action(elapsed_time);
333 /* Handle all possible collisions. */
336 update_game_objects();
340 Sector::update_game_objects()
342 /** cleanup marked objects */
343 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
344 i != gameobjects.end(); /* nothing */) {
345 if((*i)->is_valid() == false) {
346 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
348 badguys.erase(std::remove(badguys.begin(), badguys.end(), badguy),
351 Bullet* bullet = dynamic_cast<Bullet*> (*i);
354 std::remove(bullets.begin(), bullets.end(), bullet),
357 InteractiveObject* interactive_object =
358 dynamic_cast<InteractiveObject*> (*i);
359 if(interactive_object) {
360 interactive_objects.erase(
361 std::remove(interactive_objects.begin(), interactive_objects.end(),
362 interactive_object), interactive_objects.end());
364 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
367 std::remove(upgrades.begin(), upgrades.end(), upgrade),
370 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
373 std::remove(trampolines.begin(), trampolines.end(), trampoline),
376 FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
377 if(flying_platform) {
378 flying_platforms.erase(
379 std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
380 flying_platforms.end());
384 i = gameobjects.erase(i);
390 /* add newly created objects */
391 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
392 i != gameobjects_new.end(); ++i)
394 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
396 badguys.push_back(badguy);
397 Bullet* bullet = dynamic_cast<Bullet*> (*i);
399 bullets.push_back(bullet);
400 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
402 upgrades.push_back(upgrade);
403 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
405 trampolines.push_back(trampoline);
406 FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (*i);
408 flying_platforms.push_back(flying_platform);
409 InteractiveObject* interactive_object
410 = dynamic_cast<InteractiveObject*> (*i);
411 if(interactive_object)
412 interactive_objects.push_back(interactive_object);
414 gameobjects.push_back(*i);
416 gameobjects_new.clear();
420 Sector::draw(DrawingContext& context)
422 context.push_transform();
423 context.set_translation(camera->get_translation());
425 for(GameObjects::iterator i = gameobjects.begin();
426 i != gameobjects.end(); ++i) {
427 if( (*i)->is_valid() )
431 context.pop_transform();
435 Sector::collision_handler()
437 // CO_BULLET & CO_BADGUY check
438 for(unsigned int i = 0; i < bullets.size(); ++i)
440 for (BadGuys::iterator j = badguys.begin(); j != badguys.end(); ++j)
442 if((*j)->dying != DYING_NOT)
445 if(rectcollision(bullets[i]->base, (*j)->base))
447 // We have detected a collision and now call the
448 // collision functions of the collided objects.
449 (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
450 bullets[i]->collision(CO_BADGUY);
451 break; // bullet is invalid now, so break
456 /* CO_BADGUY & CO_BADGUY check */
457 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
459 if((*i)->dying != DYING_NOT)
462 BadGuys::iterator j = i;
464 for (; j != badguys.end(); ++j)
466 if(j == i || (*j)->dying != DYING_NOT)
469 if(rectcollision((*i)->base, (*j)->base))
471 // We have detected a collision and now call the
472 // collision functions of the collided objects.
473 (*j)->collision(*i, CO_BADGUY);
474 (*i)->collision(*j, CO_BADGUY);
478 if(player->dying != DYING_NOT) return;
480 // CO_BADGUY & CO_PLAYER check
481 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
483 if((*i)->dying != DYING_NOT)
486 if(rectcollision_offset((*i)->base, player->base, 0, 0))
488 // We have detected a collision and now call the collision
489 // functions of the collided objects.
490 if (player->previous_base.y < player->base.y &&
491 player->previous_base.y + player->previous_base.height
492 < (*i)->base.y + (*i)->base.height/2
493 && !player->invincible_timer.started())
495 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
499 player->collision(*i, CO_BADGUY);
500 (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
505 // CO_UPGRADE & CO_PLAYER check
506 for(unsigned int i = 0; i < upgrades.size(); ++i)
508 if(rectcollision(upgrades[i]->base, player->base))
510 // We have detected a collision and now call the collision
511 // functions of the collided objects.
512 upgrades[i]->collision(player, CO_PLAYER, COLLISION_NORMAL);
516 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
517 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
519 if (rectcollision((*i)->base, player->base))
521 if (player->previous_base.y < player->base.y &&
522 player->previous_base.y + player->previous_base.height
523 < (*i)->base.y + (*i)->base.height/2)
525 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
527 else if (player->previous_base.y <= player->base.y)
529 player->collision(*i, CO_TRAMPOLINE);
530 (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
535 // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
536 for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
538 if (rectcollision((*i)->base, player->base))
540 if (player->previous_base.y < player->base.y &&
541 player->previous_base.y + player->previous_base.height
542 < (*i)->base.y + (*i)->base.height/2)
544 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
545 player->collision(*i, CO_FLYING_PLATFORM);
547 /* else if (player->previous_base.y <= player->base.y)
555 Sector::add_score(const Vector& pos, int s)
557 player_status.score += s;
559 add_object(new FloatingScore(pos, s));
563 Sector::add_bouncy_distro(const Vector& pos)
565 add_object(new BouncyDistro(pos));
569 Sector::add_broken_brick(const Vector& pos, Tile* tile)
571 add_broken_brick_piece(pos, Vector(-1, -4), tile);
572 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
574 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
575 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
579 Sector::add_broken_brick_piece(const Vector& pos, const Vector& movement,
582 add_object(new BrokenBrick(tile, pos, movement));
586 Sector::add_bouncy_brick(const Vector& pos)
588 add_object(new BouncyBrick(pos));
592 Sector::add_bad_guy(float x, float y, BadGuyKind kind)
594 BadGuy* badguy = new BadGuy(kind, x, y);
600 Sector::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
602 add_object(new Upgrade(pos, dir, kind));
606 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
608 if(player->got_power == Player::FIRE_POWER)
610 if(bullets.size() > MAX_FIRE_BULLETS-1)
613 else if(player->got_power == Player::ICE_POWER)
615 if(bullets.size() > MAX_ICE_BULLETS-1)
619 Bullet* new_bullet = 0;
620 if(player->got_power == Player::FIRE_POWER)
621 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
622 else if(player->got_power == Player::ICE_POWER)
623 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
625 throw std::runtime_error("wrong bullet type.");
626 add_object(new_bullet);
628 sound_manager->play_sound(sounds[SND_SHOOT]);
635 Sector::trybreakbrick(const Vector& pos, bool small)
637 Tile* tile = solids->get_tile_at(pos);
641 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
642 throw SuperTuxException(errmsg, __FILE__, __LINE__);
645 if (tile->attributes & Tile::BRICK)
649 /* Get a distro from it: */
651 Vector(((int)(pos.x + 1) / 32) * 32, (int)(pos.y / 32) * 32));
653 // TODO: don't handle this in a global way but per-tile...
654 if (!counting_distros)
656 counting_distros = true;
664 if (distro_counter <= 0)
666 counting_distros = false;
667 solids->change_at(pos, tile->next_tile);
670 sound_manager->play_sound(sounds[SND_DISTRO]);
671 player_status.score = player_status.score + SCORE_DISTRO;
672 player_status.distros++;
678 solids->change_at(pos, tile->next_tile);
680 /* Replace it with broken bits: */
681 add_broken_brick(Vector(
682 ((int)(pos.x + 1) / 32) * 32,
683 (int)(pos.y / 32) * 32), tile);
685 /* Get some score: */
686 sound_manager->play_sound(sounds[SND_BRICK]);
687 player_status.score = player_status.score + SCORE_BRICK;
698 Sector::tryemptybox(const Vector& pos, Direction col_side)
700 Tile* tile = solids->get_tile_at(pos);
704 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
705 throw SuperTuxException(errmsg, __FILE__, __LINE__);
709 if (!(tile->attributes & Tile::FULLBOX))
712 // according to the collision side, set the upgrade direction
718 int posx = ((int)(pos.x+1) / 32) * 32;
719 int posy = (int)(pos.y/32) * 32 - 32;
722 case 1: // Box with a distro!
723 add_bouncy_distro(Vector(posx, posy));
724 sound_manager->play_sound(sounds[SND_DISTRO]);
725 player_status.score = player_status.score + SCORE_DISTRO;
726 player_status.distros++;
729 case 2: // Add a fire 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 a fireflower: */
733 add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
734 sound_manager->play_sound(sounds[SND_UPGRADE]);
737 case 5: // Add an ice flower upgrade!
738 if (player->size == SMALL) /* Tux is small, add mints! */
739 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
740 else /* Tux is big, add an iceflower: */
741 add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
742 sound_manager->play_sound(sounds[SND_UPGRADE]);
745 case 3: // Add a golden herring
746 add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
749 case 4: // Add a 1up extra
750 add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
757 solids->change_at(pos, tile->next_tile);
760 /* Try to grab a distro: */
762 Sector::trygrabdistro(const Vector& pos, int bounciness)
764 Tile* tile = solids->get_tile_at(pos);
768 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
769 throw SuperTuxException(errmsg, __FILE__, __LINE__); */
771 //Bad tiles (i.e. tiles that are not defined in supertux.stgt but appear in the map) are changed to ID 0 (blank tile)
772 std::cout << "Warning: Undefined tile at " <<(int)pos.x/32 << "/" << (int)pos.y/32 << " (ID: " << (int)solids->get_tile_id_at(pos).id << ")" << std::endl;
773 solids->change_at(pos,0);
774 tile = solids->get_tile_at(pos);
778 if (!(tile->attributes & Tile::COIN))
781 solids->change_at(pos, tile->next_tile);
782 sound_manager->play_sound(sounds[SND_DISTRO]);
784 if (bounciness == BOUNCE)
786 add_bouncy_distro(Vector(((int)(pos.x + 1) / 32) * 32,
787 (int)(pos.y / 32) * 32));
790 player_status.score = player_status.score + SCORE_DISTRO;
791 player_status.distros++;
795 /* Try to bump a bad guy from below: */
797 Sector::trybumpbadguy(const Vector& pos)
800 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
802 if ((*i)->base.x >= pos.x - 32 && (*i)->base.x <= pos.x + 32 &&
803 (*i)->base.y >= pos.y - 16 && (*i)->base.y <= pos.y + 16)
805 (*i)->collision(player, CO_PLAYER, COLLISION_BUMP);
810 for (unsigned int i = 0; i < upgrades.size(); i++)
812 if (upgrades[i]->base.height == 32 &&
813 upgrades[i]->base.x >= pos.x - 32 && upgrades[i]->base.x <= pos.x + 32 &&
814 upgrades[i]->base.y >= pos.y - 16 && upgrades[i]->base.y <= pos.y + 16)
816 upgrades[i]->collision(player, CO_PLAYER, COLLISION_BUMP);
827 level_song = sound_manager->load_music(datadir + "/music/" + song_title);
829 song_path = (char *) malloc(sizeof(char) * datadir.length() +
830 strlen(song_title.c_str()) + 8 + 5);
831 song_subtitle = strdup(song_title.c_str());
832 strcpy(strstr(song_subtitle, "."), "\0");
833 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
834 song_subtitle, strstr(song_title.c_str(), "."));
835 if(!sound_manager->exists_music(song_path)) {
836 level_song_fast = level_song;
838 level_song_fast = sound_manager->load_music(song_path);
845 Sector::play_music(int type)
848 switch(currentmusic) {
850 sound_manager->play_music(level_song_fast);
853 sound_manager->play_music(level_song);
856 sound_manager->play_music(herring_song);
859 sound_manager->halt_music();
865 Sector::get_music_type()