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 "app/globals.h"
31 #include "object/gameobjs.h"
32 #include "object/camera.h"
33 #include "object/background.h"
34 #include "object/particlesystem.h"
35 #include "object/tilemap.h"
36 #include "lisp/parser.h"
37 #include "lisp/lisp.h"
38 #include "lisp/writer.h"
39 #include "lisp/list_iterator.h"
41 #include "audio/sound_manager.h"
43 #include "resources.h"
44 #include "statistics.h"
45 #include "special/collision.h"
46 #include "math/rectangle.h"
47 #include "math/aatriangle.h"
48 #include "object/coin.h"
49 #include "object/block.h"
50 #include "object/invisible_block.h"
51 #include "object/invisible_tile.h"
52 #include "object/platform.h"
53 #include "object/bullet.h"
54 #include "badguy/jumpy.h"
55 #include "badguy/snowball.h"
56 #include "badguy/bouncing_snowball.h"
57 #include "badguy/flame.h"
58 #include "badguy/mriceblock.h"
59 #include "badguy/mrbomb.h"
60 #include "badguy/dispenser.h"
61 #include "badguy/spike.h"
62 #include "badguy/spiky.h"
63 #include "badguy/nolok_01.h"
64 #include "trigger/door.h"
65 #include "trigger/sequence_trigger.h"
66 #include "trigger/secretarea_trigger.h"
68 Sector* Sector::_current = 0;
71 : gravity(10), player(0), solids(0), camera(0),
72 currentmusic(LEVEL_MUSIC)
74 song_title = "Mortimers_chipdisko.mod";
75 player = new Player();
81 update_game_objects();
82 assert(gameobjects_new.size() == 0);
84 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
89 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
98 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
100 if(name == "background") {
101 return new Background(reader);
102 } else if(name == "camera") {
103 Camera* camera = new Camera(this);
104 camera->parse(reader);
106 } else if(name == "tilemap") {
107 return new TileMap(reader);
108 } else if(name == "particles-snow") {
109 SnowParticleSystem* partsys = new SnowParticleSystem();
110 partsys->parse(reader);
112 } else if(name == "particles-clouds") {
113 CloudParticleSystem* partsys = new CloudParticleSystem();
114 partsys->parse(reader);
116 } else if(name == "door") {
117 return new Door(reader);
118 } else if(name == "secretarea") {
119 return new SecretAreaTrigger(reader);
120 } else if(name == "platform") {
121 return new Platform(reader);
122 } else if(name == "jumpy" || name == "money") {
123 return new Jumpy(reader);
124 } else if(name == "snowball") {
125 return new SnowBall(reader);
126 } else if(name == "bouncingsnowball") {
127 return new BouncingSnowball(reader);
128 } else if(name == "flame") {
129 return new Flame(reader);
130 } else if(name == "mriceblock") {
131 return new MrIceBlock(reader);
132 } else if(name == "mrbomb") {
133 return new MrBomb(reader);
134 } else if(name == "dispenser") {
135 return new Dispenser(reader);
136 } else if(name == "spike") {
137 return new Spike(reader);
138 } else if(name == "spiky") {
139 return new Spiky(reader);
140 } else if(name == "nolok_01") {
141 return new Nolok_01(reader);
144 std::cerr << "Unknown object type '" << name << "'.\n";
149 Sector::parse(const lisp::Lisp& sector)
153 lisp::ListIterator iter(§or);
155 const std::string& token = iter.item();
156 if(token == "name") {
157 iter.value()->get(name);
158 } else if(token == "gravity") {
159 iter.value()->get(gravity);
160 } else if(token == "music") {
161 iter.value()->get(song_title);
163 } else if(token == "spawnpoint") {
164 const lisp::Lisp* spawnpoint_lisp = iter.lisp();
166 SpawnPoint* sp = new SpawnPoint;
167 spawnpoint_lisp->get("name", sp->name);
168 spawnpoint_lisp->get("x", sp->pos.x);
169 spawnpoint_lisp->get("y", sp->pos.y);
170 spawnpoints.push_back(sp);
172 GameObject* object = parse_object(token, *(iter.lisp()));
179 update_game_objects();
181 update_game_objects();
183 std::cerr << "sector '" << name << "' does not contain a camera.\n";
184 camera = new Camera(this);
188 throw std::runtime_error("sector does not contain a solid tile layer.");
192 Sector::parse_old_format(const lisp::Lisp& reader)
197 reader.get("gravity", gravity);
199 std::string backgroundimage;
200 reader.get("background", backgroundimage);
202 reader.get("bkgd_speed", bgspeed);
205 Color bkgd_top, bkgd_bottom;
206 int r = 0, g = 0, b = 128;
207 reader.get("bkgd_red_top", r);
208 reader.get("bkgd_green_top", g);
209 reader.get("bkgd_blue_top", b);
214 reader.get("bkgd_red_bottom", r);
215 reader.get("bkgd_green_bottom", g);
216 reader.get("bkgd_blue_bottom", b);
218 bkgd_bottom.green = g;
219 bkgd_bottom.blue = b;
221 if(backgroundimage != "") {
222 Background* background = new Background;
223 background->set_image(backgroundimage, bgspeed);
224 add_object(background);
226 Background* background = new Background;
227 background->set_gradient(bkgd_top, bkgd_bottom);
228 add_object(background);
231 std::string particlesystem;
232 reader.get("particle_system", particlesystem);
233 if(particlesystem == "clouds")
234 add_object(new CloudParticleSystem());
235 else if(particlesystem == "snow")
236 add_object(new SnowParticleSystem());
238 Vector startpos(100, 170);
239 reader.get("start_pos_x", startpos.x);
240 reader.get("start_pos_y", startpos.y);
242 SpawnPoint* spawn = new SpawnPoint;
243 spawn->pos = startpos;
244 spawn->name = "main";
245 spawnpoints.push_back(spawn);
247 song_title = "Mortimers_chipdisko.mod";
248 reader.get("music", song_title);
251 int width, height = 15;
252 reader.get("width", width);
253 reader.get("height", height);
255 std::vector<unsigned int> tiles;
256 if(reader.get_vector("interactive-tm", tiles)
257 || reader.get_vector("tilemap", tiles)) {
258 TileMap* tilemap = new TileMap();
259 tilemap->set(width, height, tiles, LAYER_TILES, true);
263 if(reader.get_vector("background-tm", tiles)) {
264 TileMap* tilemap = new TileMap();
265 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
269 if(reader.get_vector("foreground-tm", tiles)) {
270 TileMap* tilemap = new TileMap();
271 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
275 // read reset-points (now spawn-points)
276 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
278 lisp::ListIterator iter(resetpoints);
280 if(iter.item() == "point") {
282 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
284 SpawnPoint* sp = new SpawnPoint;
287 spawnpoints.push_back(sp);
290 std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
296 const lisp::Lisp* objects = reader.get_lisp("objects");
298 lisp::ListIterator iter(objects);
300 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
304 std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
310 Camera* camera = new Camera(this);
313 update_game_objects();
315 update_game_objects();
317 throw std::runtime_error("sector does not contain a solid tile layer.");
321 Sector::fix_old_tiles()
324 for(size_t x=0; x < solids->get_width(); ++x) {
325 for(size_t y=0; y < solids->get_height(); ++y) {
326 const Tile* tile = solids->get_tile(x, y);
327 Vector pos(x*32, y*32);
329 if(tile->getID() == 112) {
330 add_object(new InvisibleBlock(pos));
331 solids->change(x, y, 0);
332 } else if(tile->getID() == 1311) {
333 add_object(new InvisibleTile(pos));
334 solids->change(x, y, 0);
335 } else if(tile->getID() == 295) {
336 add_object(new Spike(pos, Spike::NORTH));
337 solids->change(x, y, 0);
338 } else if(tile->getID() == 296) {
339 add_object(new Spike(pos, Spike::EAST));
340 solids->change(x, y, 0);
341 } else if(tile->getID() == 297) {
342 add_object(new Spike(pos, Spike::SOUTH));
343 solids->change(x, y, 0);
344 } else if(tile->getID() == 298) {
345 add_object(new Spike(pos, Spike::WEST));
346 solids->change(x, y, 0);
347 } else if(tile->getAttributes() & Tile::COIN) {
348 add_object(new Coin(pos));
349 solids->change(x, y, 0);
350 } else if(tile->getAttributes() & Tile::FULLBOX) {
351 add_object(new BonusBlock(pos, tile->getData()));
352 solids->change(x, y, 0);
353 } else if(tile->getAttributes() & Tile::BRICK) {
354 add_object(new Brick(pos, tile->getData()));
355 solids->change(x, y, 0);
356 } else if(tile->getAttributes() & Tile::GOAL) {
357 add_object(new SequenceTrigger(pos, "endsequence"));
358 solids->change(x, y, 0);
365 Sector::write(lisp::Writer& writer)
367 writer.write_string("name", name);
368 writer.write_float("gravity", gravity);
369 writer.write_string("music", song_title);
372 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
374 SpawnPoint* spawn = *i;
375 writer.start_list("spawn-points");
376 writer.write_string("name", spawn->name);
377 writer.write_float("x", spawn->pos.x);
378 writer.write_float("y", spawn->pos.y);
379 writer.end_list("spawn-points");
383 for(GameObjects::iterator i = gameobjects.begin();
384 i != gameobjects.end(); ++i) {
385 Serializable* serializable = dynamic_cast<Serializable*> (*i);
387 serializable->write(writer);
392 Sector::add_object(GameObject* object)
394 // make sure the object isn't already in the list
396 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
399 assert("object already added to sector" == 0);
402 for(GameObjects::iterator i = gameobjects_new.begin();
403 i != gameobjects_new.end(); ++i) {
405 assert("object already added to sector" == 0);
410 gameobjects_new.push_back(object);
414 Sector::activate(const std::string& spawnpoint)
418 // Apply bonuses from former levels
419 switch (player_status.bonus)
421 case PlayerStatus::NO_BONUS:
424 case PlayerStatus::FLOWER_BONUS:
425 player->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
428 case PlayerStatus::GROWUP_BONUS:
434 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
436 if((*i)->name == spawnpoint) {
442 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
444 player->move(sp->pos);
447 camera->reset(player->get_pos());
451 Sector::get_best_spawn_point(Vector pos)
453 Vector best_reset_point = Vector(-1,-1);
455 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
457 if((*i)->name != "main")
459 if((*i)->pos.x > best_reset_point.x && (*i)->pos.x < pos.x)
460 best_reset_point = (*i)->pos;
463 return best_reset_point;
467 Sector::action(float elapsed_time)
469 player->check_bounds(camera);
472 for(GameObjects::iterator i = gameobjects.begin();
473 i != gameobjects.end(); ++i) {
474 GameObject* object = *i;
475 if(!object->is_valid())
478 object->action(elapsed_time);
481 /* Handle all possible collisions. */
483 update_game_objects();
487 Sector::update_game_objects()
489 /** cleanup marked objects */
490 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
491 i != gameobjects.end(); /* nothing */) {
492 if((*i)->is_valid() == false) {
493 Bullet* bullet = dynamic_cast<Bullet*> (*i);
496 std::remove(bullets.begin(), bullets.end(), bullet),
500 i = gameobjects.erase(i);
506 /* add newly created objects */
507 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
508 i != gameobjects_new.end(); ++i)
510 Bullet* bullet = dynamic_cast<Bullet*> (*i);
512 bullets.push_back(bullet);
514 TileMap* tilemap = dynamic_cast<TileMap*> (*i);
515 if(tilemap && tilemap->is_solid()) {
519 std::cerr << "Another solid tilemaps added. Ignoring.";
523 Camera* camera = dynamic_cast<Camera*> (*i);
525 if(this->camera != 0) {
526 std::cerr << "Warning: Multiple cameras added. Ignoring.";
529 this->camera = camera;
532 gameobjects.push_back(*i);
534 gameobjects_new.clear();
538 Sector::draw(DrawingContext& context)
540 context.push_transform();
541 context.set_translation(camera->get_translation());
543 for(GameObjects::iterator i = gameobjects.begin();
544 i != gameobjects.end(); ++i) {
545 GameObject* object = *i;
546 if(!object->is_valid())
549 object->draw(context);
552 context.pop_transform();
555 static const float DELTA = .001;
558 Sector::collision_tilemap(MovingObject* object, int depth)
562 std::cout << "Max collision depth reached.\n";
564 object->movement = Vector(0, 0);
568 // calculate rectangle where the object will move
570 if(object->get_movement().x >= 0) {
571 x1 = object->get_pos().x;
572 x2 = object->get_bbox().p2.x + object->get_movement().x;
574 x1 = object->get_pos().x + object->get_movement().x;
575 x2 = object->get_bbox().p2.x;
578 if(object->get_movement().y >= 0) {
579 y1 = object->get_pos().y;
580 y2 = object->get_bbox().p2.y + object->get_movement().y;
582 y1 = object->get_pos().y + object->get_movement().y;
583 y2 = object->get_bbox().p2.y;
586 // test with all tiles in this rectangle
587 int starttilex = int(x1-1) / 32;
588 int starttiley = int(y1-1) / 32;
589 int max_x = int(x2+1);
590 int max_y = int(y2+1);
592 CollisionHit temphit, hit;
593 Rectangle dest = object->get_bbox();
594 dest.move(object->movement);
595 hit.time = -1; // represents an invalid value
596 for(int x = starttilex; x*32 < max_x; ++x) {
597 for(int y = starttiley; y*32 < max_y; ++y) {
598 const Tile* tile = solids->get_tile(x, y);
601 if(!(tile->getAttributes() & Tile::SOLID))
603 if((tile->getAttributes() & Tile::UNISOLID) && object->movement.y < 0)
606 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
608 Vector p1(x*32, y*32);
609 Vector p2((x+1)*32, (y+1)*32);
610 triangle = AATriangle(p1, p2, tile->getData());
612 if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
614 if(temphit.time > hit.time)
617 } else { // normal rectangular tile
618 Rectangle rect(x*32, y*32, (x+1)*32, (y+1)*32);
619 if(Collision::rectangle_rectangle(temphit, dest,
620 object->movement, rect)) {
621 if(temphit.time > hit.time)
628 // did we collide at all?
632 // call collision function
633 HitResponse response = object->collision(*solids, hit);
634 if(response == ABORT_MOVE) {
635 object->movement = Vector(0, 0);
638 if(response == FORCE_MOVE) {
641 // move out of collision and try again
642 object->movement += hit.normal * (hit.depth + DELTA);
643 collision_tilemap(object, depth+1);
647 Sector::collision_object(MovingObject* object1, MovingObject* object2)
650 Rectangle dest1 = object1->get_bbox();
651 dest1.move(object1->get_movement());
652 Rectangle dest2 = object2->get_bbox();
653 dest2.move(object2->get_movement());
655 Vector movement = object1->get_movement() - object2->get_movement();
656 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
657 HitResponse response1 = object1->collision(*object2, hit);
659 HitResponse response2 = object2->collision(*object1, hit);
661 if(response1 != CONTINUE) {
662 if(response1 == ABORT_MOVE)
663 object1->movement = Vector(0, 0);
664 if(response2 == CONTINUE)
665 object2->movement += hit.normal * (hit.depth + DELTA);
666 } else if(response2 != CONTINUE) {
667 if(response2 == ABORT_MOVE)
668 object2->movement = Vector(0, 0);
669 if(response1 == CONTINUE)
670 object1->movement += -hit.normal * (hit.depth + DELTA);
672 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
673 object2->movement += hit.normal * (hit.depth/2 + DELTA);
679 Sector::collision_handler()
681 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
682 i != gameobjects.end(); ++i) {
683 GameObject* gameobject = *i;
684 if(!gameobject->is_valid())
686 MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
689 if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
690 movingobject->bbox.move(movingobject->movement);
691 movingobject->movement = Vector(0, 0);
695 // collision with tilemap
696 if(! (movingobject->movement == Vector(0, 0)))
697 collision_tilemap(movingobject, 0);
699 // collision with other objects
700 for(std::vector<GameObject*>::iterator i2 = i+1;
701 i2 != gameobjects.end(); ++i2) {
702 GameObject* other_object = *i2;
703 if(!other_object->is_valid()
704 || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
706 MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
710 collision_object(movingobject, movingobject2);
713 movingobject->bbox.move(movingobject->get_movement());
714 movingobject->movement = Vector(0, 0);
719 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
721 if(player->got_power == Player::FIRE_POWER) {
722 if(bullets.size() > MAX_FIRE_BULLETS-1)
724 } else if(player->got_power == Player::ICE_POWER) {
725 if(bullets.size() > MAX_ICE_BULLETS-1)
729 Bullet* new_bullet = 0;
730 if(player->got_power == Player::FIRE_POWER)
731 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
732 else if(player->got_power == Player::ICE_POWER)
733 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
735 throw std::runtime_error("wrong bullet type.");
736 add_object(new_bullet);
738 SoundManager::get()->play_sound(IDToSound(SND_SHOOT));
744 Sector::add_smoke_cloud(const Vector& pos)
746 add_object(new SmokeCloud(pos));
751 Sector::add_floating_text(const Vector& pos, const std::string& text)
753 add_object(new FloatingText(pos, text));
762 level_song = SoundManager::get()->load_music(datadir + "/music/" + song_title);
764 song_path = (char *) malloc(sizeof(char) * datadir.length() +
765 strlen(song_title.c_str()) + 8 + 5);
766 song_subtitle = strdup(song_title.c_str());
767 strcpy(strstr(song_subtitle, "."), "\0");
768 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
769 song_subtitle, strstr(song_title.c_str(), "."));
770 if(!SoundManager::get()->exists_music(song_path)) {
771 level_song_fast = level_song;
773 level_song_fast = SoundManager::get()->load_music(song_path);
780 Sector::play_music(int type)
783 switch(currentmusic) {
785 SoundManager::get()->play_music(level_song_fast);
788 SoundManager::get()->play_music(level_song);
791 SoundManager::get()->play_music(herring_song);
794 SoundManager::get()->halt_music();
800 Sector::get_music_type()
806 Sector::get_total_badguys()
808 int total_badguys = 0;
810 for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i)
812 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
817 return total_badguys;
821 Sector::inside(const Rectangle& rect) const
823 if(rect.p1.x > solids->get_width() * 32
824 || rect.p1.y > solids->get_height() * 32
825 || rect.p2.x < 0 || rect.p2.y < 0)