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.
26 #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 // XXX a bit hackish, at least try to keep the number of these things down...
278 BadGuy* badguy = dynamic_cast<BadGuy*> (object);
280 badguys.push_back(badguy);
281 Bullet* bullet = dynamic_cast<Bullet*> (object);
283 bullets.push_back(bullet);
284 Upgrade* upgrade = dynamic_cast<Upgrade*> (object);
286 upgrades.push_back(upgrade);
287 Trampoline* trampoline = dynamic_cast<Trampoline*> (object);
289 trampolines.push_back(trampoline);
290 FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (object);
292 flying_platforms.push_back(flying_platform);
293 InteractiveObject* interactive_object
294 = dynamic_cast<InteractiveObject*> (object);
295 if(interactive_object)
296 interactive_objects.push_back(interactive_object);
298 gameobjects.push_back(object);
302 Sector::activate(const std::string& spawnpoint)
306 // Apply bonuses from former levels
307 switch (player_status.bonus)
309 case PlayerStatus::NO_BONUS:
312 case PlayerStatus::FLOWER_BONUS:
313 player->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
316 case PlayerStatus::GROWUP_BONUS:
322 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
324 if((*i)->name == spawnpoint) {
330 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
332 player->move(sp->pos);
335 camera->reset(Vector(player->base.x, player->base.y));
339 Sector::action(float elapsed_time)
341 player->check_bounds(camera);
343 /* update objects (don't use iterators here, because the list might change
344 * during the iteration)
346 for(size_t i = 0; i < gameobjects.size(); ++i)
347 if(gameobjects[i]->is_valid())
348 gameobjects[i]->action(elapsed_time);
350 /* Handle all possible collisions. */
353 /** cleanup marked objects */
354 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
355 i != gameobjects.end(); /* nothing */) {
356 if((*i)->is_valid() == false) {
357 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
359 badguys.erase(std::remove(badguys.begin(), badguys.end(), badguy),
362 Bullet* bullet = dynamic_cast<Bullet*> (*i);
365 std::remove(bullets.begin(), bullets.end(), bullet),
368 InteractiveObject* interactive_object =
369 dynamic_cast<InteractiveObject*> (*i);
370 if(interactive_object) {
371 interactive_objects.erase(
372 std::remove(interactive_objects.begin(), interactive_objects.end(),
373 interactive_object), interactive_objects.end());
375 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
378 std::remove(upgrades.begin(), upgrades.end(), upgrade),
381 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
384 std::remove(trampolines.begin(), trampolines.end(), trampoline),
387 FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
388 if(flying_platform) {
389 flying_platforms.erase(
390 std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
391 flying_platforms.end());
395 i = gameobjects.erase(i);
403 Sector::draw(DrawingContext& context)
405 context.push_transform();
406 context.set_translation(camera->get_translation());
408 for(GameObjects::iterator i = gameobjects.begin();
409 i != gameobjects.end(); ++i) {
410 if( (*i)->is_valid() )
414 context.pop_transform();
418 Sector::collision_handler()
420 // CO_BULLET & CO_BADGUY check
421 for(unsigned int i = 0; i < bullets.size(); ++i)
423 for (BadGuys::iterator j = badguys.begin(); j != badguys.end(); ++j)
425 if((*j)->dying != DYING_NOT)
428 if(rectcollision(bullets[i]->base, (*j)->base))
430 // We have detected a collision and now call the
431 // collision functions of the collided objects.
432 (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
433 bullets[i]->collision(CO_BADGUY);
434 break; // bullet is invalid now, so break
439 /* CO_BADGUY & CO_BADGUY check */
440 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
442 if((*i)->dying != DYING_NOT)
445 BadGuys::iterator j = i;
447 for (; j != badguys.end(); ++j)
449 if(j == i || (*j)->dying != DYING_NOT)
452 if(rectcollision((*i)->base, (*j)->base))
454 // We have detected a collision and now call the
455 // collision functions of the collided objects.
456 (*j)->collision(*i, CO_BADGUY);
457 (*i)->collision(*j, CO_BADGUY);
461 if(player->dying != DYING_NOT) return;
463 // CO_BADGUY & CO_PLAYER check
464 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
466 if((*i)->dying != DYING_NOT)
469 if(rectcollision_offset((*i)->base, player->base, 0, 0))
471 // We have detected a collision and now call the collision
472 // functions of the collided objects.
473 if (player->previous_base.y < player->base.y &&
474 player->previous_base.y + player->previous_base.height
475 < (*i)->base.y + (*i)->base.height/2
476 && !player->invincible_timer.started())
478 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
482 player->collision(*i, CO_BADGUY);
483 (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
488 // CO_UPGRADE & CO_PLAYER check
489 for(unsigned int i = 0; i < upgrades.size(); ++i)
491 if(rectcollision(upgrades[i]->base, player->base))
493 // We have detected a collision and now call the collision
494 // functions of the collided objects.
495 upgrades[i]->collision(player, CO_PLAYER, COLLISION_NORMAL);
499 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
500 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
502 if (rectcollision((*i)->base, player->base))
504 if (player->previous_base.y < player->base.y &&
505 player->previous_base.y + player->previous_base.height
506 < (*i)->base.y + (*i)->base.height/2)
508 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
510 else if (player->previous_base.y <= player->base.y)
512 player->collision(*i, CO_TRAMPOLINE);
513 (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL);
518 // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
519 for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
521 if (rectcollision((*i)->base, player->base))
523 if (player->previous_base.y < player->base.y &&
524 player->previous_base.y + player->previous_base.height
525 < (*i)->base.y + (*i)->base.height/2)
527 (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH);
528 player->collision(*i, CO_FLYING_PLATFORM);
530 /* else if (player->previous_base.y <= player->base.y)
538 Sector::add_score(const Vector& pos, int s)
540 player_status.score += s;
542 add_object(new FloatingScore(pos, s));
546 Sector::add_bouncy_distro(const Vector& pos)
548 add_object(new BouncyDistro(pos));
552 Sector::add_broken_brick(const Vector& pos, Tile* tile)
554 add_broken_brick_piece(pos, Vector(-1, -4), tile);
555 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
557 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
558 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
562 Sector::add_broken_brick_piece(const Vector& pos, const Vector& movement,
565 add_object(new BrokenBrick(tile, pos, movement));
569 Sector::add_bouncy_brick(const Vector& pos)
571 add_object(new BouncyBrick(pos));
575 Sector::add_bad_guy(float x, float y, BadGuyKind kind)
577 BadGuy* badguy = new BadGuy(kind, x, y);
583 Sector::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
585 add_object(new Upgrade(pos, dir, kind));
589 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
591 if(player->got_power == Player::FIRE_POWER)
593 if(bullets.size() > MAX_FIRE_BULLETS-1)
596 else if(player->got_power == Player::ICE_POWER)
598 if(bullets.size() > MAX_ICE_BULLETS-1)
602 Bullet* new_bullet = 0;
603 if(player->got_power == Player::FIRE_POWER)
604 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
605 else if(player->got_power == Player::ICE_POWER)
606 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
608 throw std::runtime_error("wrong bullet type.");
609 add_object(new_bullet);
611 sound_manager->play_sound(sounds[SND_SHOOT]);
618 Sector::trybreakbrick(const Vector& pos, bool small)
620 Tile* tile = solids->get_tile_at(pos);
624 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
625 throw SuperTuxException(errmsg, __FILE__, __LINE__);
628 if (tile->attributes & Tile::BRICK)
632 /* Get a distro from it: */
634 Vector(((int)(pos.x + 1) / 32) * 32, (int)(pos.y / 32) * 32));
636 // TODO: don't handle this in a global way but per-tile...
637 if (!counting_distros)
639 counting_distros = true;
647 if (distro_counter <= 0)
649 counting_distros = false;
650 solids->change_at(pos, tile->next_tile);
653 sound_manager->play_sound(sounds[SND_DISTRO]);
654 player_status.score = player_status.score + SCORE_DISTRO;
655 player_status.distros++;
661 solids->change_at(pos, tile->next_tile);
663 /* Replace it with broken bits: */
664 add_broken_brick(Vector(
665 ((int)(pos.x + 1) / 32) * 32,
666 (int)(pos.y / 32) * 32), tile);
668 /* Get some score: */
669 sound_manager->play_sound(sounds[SND_BRICK]);
670 player_status.score = player_status.score + SCORE_BRICK;
681 Sector::tryemptybox(const Vector& pos, Direction col_side)
683 Tile* tile = solids->get_tile_at(pos);
687 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
688 throw SuperTuxException(errmsg, __FILE__, __LINE__);
692 if (!(tile->attributes & Tile::FULLBOX))
695 // according to the collision side, set the upgrade direction
701 int posx = ((int)(pos.x+1) / 32) * 32;
702 int posy = (int)(pos.y/32) * 32 - 32;
705 case 1: // Box with a distro!
706 add_bouncy_distro(Vector(posx, posy));
707 sound_manager->play_sound(sounds[SND_DISTRO]);
708 player_status.score = player_status.score + SCORE_DISTRO;
709 player_status.distros++;
712 case 2: // Add a fire flower upgrade!
713 if (player->size == SMALL) /* Tux is small, add mints! */
714 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
715 else /* Tux is big, add a fireflower: */
716 add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
717 sound_manager->play_sound(sounds[SND_UPGRADE]);
720 case 5: // Add an ice 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 an iceflower: */
724 add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
725 sound_manager->play_sound(sounds[SND_UPGRADE]);
728 case 3: // Add a golden herring
729 add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
732 case 4: // Add a 1up extra
733 add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
740 solids->change_at(pos, tile->next_tile);
743 /* Try to grab a distro: */
745 Sector::trygrabdistro(const Vector& pos, int bounciness)
747 Tile* tile = solids->get_tile_at(pos);
751 sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
752 throw SuperTuxException(errmsg, __FILE__, __LINE__);
756 if (!(tile->attributes & Tile::COIN))
759 solids->change_at(pos, tile->next_tile);
760 sound_manager->play_sound(sounds[SND_DISTRO]);
762 if (bounciness == BOUNCE)
764 add_bouncy_distro(Vector(((int)(pos.x + 1) / 32) * 32,
765 (int)(pos.y / 32) * 32));
768 player_status.score = player_status.score + SCORE_DISTRO;
769 player_status.distros++;
772 /* Try to bump a bad guy from below: */
774 Sector::trybumpbadguy(const Vector& pos)
777 for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i)
779 if ((*i)->base.x >= pos.x - 32 && (*i)->base.x <= pos.x + 32 &&
780 (*i)->base.y >= pos.y - 16 && (*i)->base.y <= pos.y + 16)
782 (*i)->collision(player, CO_PLAYER, COLLISION_BUMP);
787 for (unsigned int i = 0; i < upgrades.size(); i++)
789 if (upgrades[i]->base.height == 32 &&
790 upgrades[i]->base.x >= pos.x - 32 && upgrades[i]->base.x <= pos.x + 32 &&
791 upgrades[i]->base.y >= pos.y - 16 && upgrades[i]->base.y <= pos.y + 16)
793 upgrades[i]->collision(player, CO_PLAYER, COLLISION_BUMP);
804 level_song = sound_manager->load_music(datadir + "/music/" + song_title);
806 song_path = (char *) malloc(sizeof(char) * datadir.length() +
807 strlen(song_title.c_str()) + 8 + 5);
808 song_subtitle = strdup(song_title.c_str());
809 strcpy(strstr(song_subtitle, "."), "\0");
810 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
811 song_subtitle, strstr(song_title.c_str(), "."));
812 if(!sound_manager->exists_music(song_path)) {
813 level_song_fast = level_song;
815 level_song_fast = sound_manager->load_music(song_path);
822 Sector::play_music(int type)
825 switch(currentmusic) {
827 sound_manager->play_music(level_song_fast);
830 sound_manager->play_music(level_song);
833 sound_manager->play_music(herring_song);
836 sound_manager->halt_music();
842 Sector::get_music_type()