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 "collision_grid.h"
46 #include "special/collision.h"
47 #include "math/rectangle.h"
48 #include "math/aatriangle.h"
49 #include "object/coin.h"
50 #include "object/block.h"
51 #include "object/invisible_block.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();
78 grid = new CollisionGrid(32000, 32000);
83 update_game_objects();
84 assert(gameobjects_new.size() == 0);
88 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
93 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
102 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
104 if(name == "background") {
105 return new Background(reader);
106 } else if(name == "camera") {
107 Camera* camera = new Camera(this);
108 camera->parse(reader);
110 } else if(name == "tilemap") {
111 return new TileMap(reader);
112 } else if(name == "particles-snow") {
113 SnowParticleSystem* partsys = new SnowParticleSystem();
114 partsys->parse(reader);
116 } else if(name == "particles-clouds") {
117 CloudParticleSystem* partsys = new CloudParticleSystem();
118 partsys->parse(reader);
120 } else if(name == "door") {
121 return new Door(reader);
122 } else if(name == "secretarea") {
123 return new SecretAreaTrigger(reader);
124 } else if(name == "platform") {
125 return new Platform(reader);
126 } else if(name == "jumpy" || name == "money") {
127 return new Jumpy(reader);
128 } else if(name == "snowball") {
129 return new SnowBall(reader);
130 } else if(name == "bouncingsnowball") {
131 return new BouncingSnowball(reader);
132 } else if(name == "flame") {
133 return new Flame(reader);
134 } else if(name == "mriceblock") {
135 return new MrIceBlock(reader);
136 } else if(name == "mrbomb") {
137 return new MrBomb(reader);
138 } else if(name == "dispenser") {
139 return new Dispenser(reader);
140 } else if(name == "spike") {
141 return new Spike(reader);
142 } else if(name == "spiky") {
143 return new Spiky(reader);
144 } else if(name == "nolok_01") {
145 return new Nolok_01(reader);
148 std::cerr << "Unknown object type '" << name << "'.\n";
153 Sector::parse(const lisp::Lisp& sector)
157 lisp::ListIterator iter(§or);
159 const std::string& token = iter.item();
160 if(token == "name") {
161 iter.value()->get(name);
162 } else if(token == "gravity") {
163 iter.value()->get(gravity);
164 } else if(token == "music") {
165 iter.value()->get(song_title);
167 } else if(token == "spawnpoint") {
168 const lisp::Lisp* spawnpoint_lisp = iter.lisp();
170 SpawnPoint* sp = new SpawnPoint;
171 spawnpoint_lisp->get("name", sp->name);
172 spawnpoint_lisp->get("x", sp->pos.x);
173 spawnpoint_lisp->get("y", sp->pos.y);
174 spawnpoints.push_back(sp);
176 GameObject* object = parse_object(token, *(iter.lisp()));
183 update_game_objects();
185 update_game_objects();
187 std::cerr << "sector '" << name << "' does not contain a camera.\n";
188 camera = new Camera(this);
192 throw std::runtime_error("sector does not contain a solid tile layer.");
196 Sector::parse_old_format(const lisp::Lisp& reader)
201 reader.get("gravity", gravity);
203 std::string backgroundimage;
204 reader.get("background", backgroundimage);
206 reader.get("bkgd_speed", bgspeed);
209 Color bkgd_top, bkgd_bottom;
210 int r = 0, g = 0, b = 128;
211 reader.get("bkgd_red_top", r);
212 reader.get("bkgd_green_top", g);
213 reader.get("bkgd_blue_top", b);
218 reader.get("bkgd_red_bottom", r);
219 reader.get("bkgd_green_bottom", g);
220 reader.get("bkgd_blue_bottom", b);
222 bkgd_bottom.green = g;
223 bkgd_bottom.blue = b;
225 if(backgroundimage != "") {
226 Background* background = new Background;
227 background->set_image(backgroundimage, bgspeed);
228 add_object(background);
230 Background* background = new Background;
231 background->set_gradient(bkgd_top, bkgd_bottom);
232 add_object(background);
235 std::string particlesystem;
236 reader.get("particle_system", particlesystem);
237 if(particlesystem == "clouds")
238 add_object(new CloudParticleSystem());
239 else if(particlesystem == "snow")
240 add_object(new SnowParticleSystem());
242 Vector startpos(100, 170);
243 reader.get("start_pos_x", startpos.x);
244 reader.get("start_pos_y", startpos.y);
246 SpawnPoint* spawn = new SpawnPoint;
247 spawn->pos = startpos;
248 spawn->name = "main";
249 spawnpoints.push_back(spawn);
251 song_title = "Mortimers_chipdisko.mod";
252 reader.get("music", song_title);
255 int width, height = 15;
256 reader.get("width", width);
257 reader.get("height", height);
259 std::vector<unsigned int> tiles;
260 if(reader.get_vector("interactive-tm", tiles)
261 || reader.get_vector("tilemap", tiles)) {
262 TileMap* tilemap = new TileMap();
263 tilemap->set(width, height, tiles, LAYER_TILES, true);
267 if(reader.get_vector("background-tm", tiles)) {
268 TileMap* tilemap = new TileMap();
269 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
273 if(reader.get_vector("foreground-tm", tiles)) {
274 TileMap* tilemap = new TileMap();
275 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
279 // read reset-points (now spawn-points)
280 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
282 lisp::ListIterator iter(resetpoints);
284 if(iter.item() == "point") {
286 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
288 SpawnPoint* sp = new SpawnPoint;
291 spawnpoints.push_back(sp);
294 std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
300 const lisp::Lisp* objects = reader.get_lisp("objects");
302 lisp::ListIterator iter(objects);
304 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
308 std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
314 Camera* camera = new Camera(this);
317 update_game_objects();
319 update_game_objects();
321 throw std::runtime_error("sector does not contain a solid tile layer.");
325 Sector::fix_old_tiles()
328 for(size_t x=0; x < solids->get_width(); ++x) {
329 for(size_t y=0; y < solids->get_height(); ++y) {
330 const Tile* tile = solids->get_tile(x, y);
331 Vector pos(x*32, y*32);
333 if(tile->getID() == 112) {
334 add_object(new InvisibleBlock(pos));
335 solids->change(x, y, 0);
336 } else if(tile->getID() == 295) {
337 add_object(new Spike(pos, Spike::NORTH));
338 solids->change(x, y, 0);
339 } else if(tile->getID() == 296) {
340 add_object(new Spike(pos, Spike::EAST));
341 solids->change(x, y, 0);
342 } else if(tile->getID() == 297) {
343 add_object(new Spike(pos, Spike::SOUTH));
344 solids->change(x, y, 0);
345 } else if(tile->getID() == 298) {
346 add_object(new Spike(pos, Spike::WEST));
347 solids->change(x, y, 0);
348 } else if(tile->getAttributes() & Tile::COIN) {
349 add_object(new Coin(pos));
350 solids->change(x, y, 0);
351 } else if(tile->getAttributes() & Tile::FULLBOX) {
352 add_object(new BonusBlock(pos, tile->getData()));
353 solids->change(x, y, 0);
354 } else if(tile->getAttributes() & Tile::BRICK) {
355 add_object(new Brick(pos, tile->getData()));
356 solids->change(x, y, 0);
357 } else if(tile->getAttributes() & Tile::GOAL) {
358 add_object(new SequenceTrigger(pos, "endsequence"));
359 solids->change(x, y, 0);
366 Sector::write(lisp::Writer& writer)
368 writer.write_string("name", name);
369 writer.write_float("gravity", gravity);
370 writer.write_string("music", song_title);
373 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
375 SpawnPoint* spawn = *i;
376 writer.start_list("spawn-points");
377 writer.write_string("name", spawn->name);
378 writer.write_float("x", spawn->pos.x);
379 writer.write_float("y", spawn->pos.y);
380 writer.end_list("spawn-points");
384 for(GameObjects::iterator i = gameobjects.begin();
385 i != gameobjects.end(); ++i) {
386 Serializable* serializable = dynamic_cast<Serializable*> (*i);
388 serializable->write(writer);
393 Sector::add_object(GameObject* object)
395 // make sure the object isn't already in the list
397 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
400 assert("object already added to sector" == 0);
403 for(GameObjects::iterator i = gameobjects_new.begin();
404 i != gameobjects_new.end(); ++i) {
406 assert("object already added to sector" == 0);
411 gameobjects_new.push_back(object);
415 Sector::activate(const std::string& spawnpoint)
419 // Apply bonuses from former levels
420 switch (player_status.bonus)
422 case PlayerStatus::NO_BONUS:
425 case PlayerStatus::FLOWER_BONUS:
426 player->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
429 case PlayerStatus::GROWUP_BONUS:
435 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
437 if((*i)->name == spawnpoint) {
443 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
445 player->move(sp->pos);
448 camera->reset(player->get_pos());
452 Sector::get_best_spawn_point(Vector pos)
454 Vector best_reset_point = Vector(-1,-1);
456 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
458 if((*i)->name != "main")
460 if((*i)->pos.x > best_reset_point.x && (*i)->pos.x < pos.x)
461 best_reset_point = (*i)->pos;
464 return best_reset_point;
468 Sector::action(float elapsed_time)
470 player->check_bounds(camera);
473 for(GameObjects::iterator i = gameobjects.begin();
474 i != gameobjects.end(); ++i) {
475 GameObject* object = *i;
476 if(!object->is_valid())
479 object->action(elapsed_time);
482 /* Handle all possible collisions. */
484 update_game_objects();
488 Sector::update_game_objects()
490 /** cleanup marked objects */
491 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
492 i != gameobjects.end(); /* nothing */) {
493 GameObject* object = *i;
495 if(object->is_valid()) {
500 Bullet* bullet = dynamic_cast<Bullet*> (object);
503 std::remove(bullets.begin(), bullets.end(), bullet),
506 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
508 grid->remove_object(movingobject);
511 i = gameobjects.erase(i);
514 /* add newly created objects */
515 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
516 i != gameobjects_new.end(); ++i)
518 GameObject* object = *i;
520 Bullet* bullet = dynamic_cast<Bullet*> (object);
522 bullets.push_back(bullet);
524 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
526 grid->add_object(movingobject);
528 TileMap* tilemap = dynamic_cast<TileMap*> (object);
529 if(tilemap && tilemap->is_solid()) {
533 std::cerr << "Another solid tilemaps added. Ignoring.";
537 Camera* camera = dynamic_cast<Camera*> (object);
539 if(this->camera != 0) {
540 std::cerr << "Warning: Multiple cameras added. Ignoring.";
543 this->camera = camera;
546 gameobjects.push_back(object);
548 gameobjects_new.clear();
552 Sector::draw(DrawingContext& context)
554 context.push_transform();
555 context.set_translation(camera->get_translation());
557 for(GameObjects::iterator i = gameobjects.begin();
558 i != gameobjects.end(); ++i) {
559 GameObject* object = *i;
560 if(!object->is_valid())
563 object->draw(context);
566 context.pop_transform();
569 static const float DELTA = .001;
572 Sector::collision_tilemap(MovingObject* object, int depth)
576 std::cout << "Max collision depth reached.\n";
578 object->movement = Vector(0, 0);
582 // calculate rectangle where the object will move
584 if(object->get_movement().x >= 0) {
585 x1 = object->get_pos().x;
586 x2 = object->get_bbox().p2.x + object->get_movement().x;
588 x1 = object->get_pos().x + object->get_movement().x;
589 x2 = object->get_bbox().p2.x;
592 if(object->get_movement().y >= 0) {
593 y1 = object->get_pos().y;
594 y2 = object->get_bbox().p2.y + object->get_movement().y;
596 y1 = object->get_pos().y + object->get_movement().y;
597 y2 = object->get_bbox().p2.y;
600 // test with all tiles in this rectangle
601 int starttilex = int(x1-1) / 32;
602 int starttiley = int(y1-1) / 32;
603 int max_x = int(x2+1);
604 int max_y = int(y2+1);
606 CollisionHit temphit, hit;
607 Rectangle dest = object->get_bbox();
608 dest.move(object->movement);
609 hit.time = -1; // represents an invalid value
610 for(int x = starttilex; x*32 < max_x; ++x) {
611 for(int y = starttiley; y*32 < max_y; ++y) {
612 const Tile* tile = solids->get_tile(x, y);
615 if(!(tile->getAttributes() & Tile::SOLID))
617 if((tile->getAttributes() & Tile::UNISOLID) && object->movement.y < 0)
620 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
622 Vector p1(x*32, y*32);
623 Vector p2((x+1)*32, (y+1)*32);
624 triangle = AATriangle(p1, p2, tile->getData());
626 if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
628 if(temphit.time > hit.time)
631 } else { // normal rectangular tile
632 Rectangle rect(x*32, y*32, (x+1)*32, (y+1)*32);
633 if(Collision::rectangle_rectangle(temphit, dest,
634 object->movement, rect)) {
635 if(temphit.time > hit.time)
642 // did we collide at all?
646 // call collision function
647 HitResponse response = object->collision(*solids, hit);
648 if(response == ABORT_MOVE) {
649 object->movement = Vector(0, 0);
652 if(response == FORCE_MOVE) {
655 // move out of collision and try again
656 object->movement += hit.normal * (hit.depth + DELTA);
657 collision_tilemap(object, depth+1);
661 Sector::collision_object(MovingObject* object1, MovingObject* object2)
664 Rectangle dest1 = object1->get_bbox();
665 dest1.move(object1->get_movement());
666 Rectangle dest2 = object2->get_bbox();
667 dest2.move(object2->get_movement());
669 Vector movement = object1->get_movement() - object2->get_movement();
670 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
671 HitResponse response1 = object1->collision(*object2, hit);
673 HitResponse response2 = object2->collision(*object1, hit);
675 if(response1 != CONTINUE) {
676 if(response1 == ABORT_MOVE)
677 object1->movement = Vector(0, 0);
678 if(response2 == CONTINUE)
679 object2->movement += hit.normal * (hit.depth + DELTA);
680 } else if(response2 != CONTINUE) {
681 if(response2 == ABORT_MOVE)
682 object2->movement = Vector(0, 0);
683 if(response1 == CONTINUE)
684 object1->movement += -hit.normal * (hit.depth + DELTA);
686 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
687 object2->movement += hit.normal * (hit.depth/2 + DELTA);
693 Sector::collision_handler()
696 grid->check_collisions();
698 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
699 i != gameobjects.end(); ++i) {
700 GameObject* gameobject = *i;
701 if(!gameobject->is_valid())
703 MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
706 if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
707 movingobject->bbox.move(movingobject->movement);
708 movingobject->movement = Vector(0, 0);
712 // collision with tilemap
713 if(! (movingobject->movement == Vector(0, 0)))
714 collision_tilemap(movingobject, 0);
716 // collision with other objects
717 for(std::vector<GameObject*>::iterator i2 = i+1;
718 i2 != gameobjects.end(); ++i2) {
719 GameObject* other_object = *i2;
720 if(!other_object->is_valid()
721 || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
723 MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
727 collision_object(movingobject, movingobject2);
730 movingobject->bbox.move(movingobject->get_movement());
731 movingobject->movement = Vector(0, 0);
737 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
739 if(player->got_power == Player::FIRE_POWER) {
740 if(bullets.size() > MAX_FIRE_BULLETS-1)
742 } else if(player->got_power == Player::ICE_POWER) {
743 if(bullets.size() > MAX_ICE_BULLETS-1)
747 Bullet* new_bullet = 0;
748 if(player->got_power == Player::FIRE_POWER)
749 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
750 else if(player->got_power == Player::ICE_POWER)
751 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
753 throw std::runtime_error("wrong bullet type.");
754 add_object(new_bullet);
756 SoundManager::get()->play_sound(IDToSound(SND_SHOOT));
762 Sector::add_smoke_cloud(const Vector& pos)
764 add_object(new SmokeCloud(pos));
769 Sector::add_floating_text(const Vector& pos, const std::string& text)
771 add_object(new FloatingText(pos, text));
780 level_song = SoundManager::get()->load_music(datadir + "/music/" + song_title);
782 song_path = (char *) malloc(sizeof(char) * datadir.length() +
783 strlen(song_title.c_str()) + 8 + 5);
784 song_subtitle = strdup(song_title.c_str());
785 strcpy(strstr(song_subtitle, "."), "\0");
786 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
787 song_subtitle, strstr(song_title.c_str(), "."));
788 if(!SoundManager::get()->exists_music(song_path)) {
789 level_song_fast = level_song;
791 level_song_fast = SoundManager::get()->load_music(song_path);
798 Sector::play_music(int type)
801 switch(currentmusic) {
803 SoundManager::get()->play_music(level_song_fast);
806 SoundManager::get()->play_music(level_song);
809 SoundManager::get()->play_music(herring_song);
812 SoundManager::get()->halt_music();
818 Sector::get_music_type()
824 Sector::get_total_badguys()
826 int total_badguys = 0;
828 for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i)
830 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
835 return total_badguys;
839 Sector::inside(const Rectangle& rect) const
841 if(rect.p1.x > solids->get_width() * 32
842 || rect.p1.y > solids->get_height() * 32
843 || rect.p2.x < 0 || rect.p2.y < 0)