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.
30 #include "player_status.hpp"
31 #include "object/gameobjs.hpp"
32 #include "object/camera.hpp"
33 #include "object/background.hpp"
34 #include "object/particlesystem.hpp"
35 #include "object/particlesystem_interactive.hpp"
36 #include "object/tilemap.hpp"
37 #include "lisp/parser.hpp"
38 #include "lisp/lisp.hpp"
39 #include "lisp/writer.hpp"
40 #include "lisp/list_iterator.hpp"
42 #include "audio/sound_manager.hpp"
43 #include "game_session.hpp"
44 #include "resources.hpp"
45 #include "statistics.hpp"
46 #include "collision_grid.hpp"
47 #include "collision_grid_iterator.hpp"
48 #include "object_factory.hpp"
49 #include "collision.hpp"
50 #include "spawn_point.hpp"
51 #include "math/rect.hpp"
52 #include "math/aatriangle.hpp"
53 #include "object/coin.hpp"
54 #include "object/block.hpp"
55 #include "object/invisible_block.hpp"
56 #include "object/bullet.hpp"
57 #include "object/text_object.hpp"
58 #include "badguy/jumpy.hpp"
59 #include "trigger/sequence_trigger.hpp"
60 #include "player_status.hpp"
61 #include "scripting/script_interpreter.hpp"
62 #include "scripting/sound.hpp"
63 #include "scripting/scripted_object.hpp"
64 #include "scripting/text.hpp"
68 Sector* Sector::_current = 0;
71 : gravity(10), player(0), solids(0), camera(0),
72 currentmusic(LEVEL_MUSIC)
74 song_title = "chipdisko.ogg";
75 player = new Player(player_status);
79 grid = new CollisionGrid(32000, 32000);
87 update_game_objects();
88 assert(gameobjects_new.size() == 0);
92 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
97 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
106 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
108 if(name == "camera") {
109 Camera* camera = new Camera(this);
110 camera->parse(reader);
112 } else if(name == "particles-snow") {
113 SnowParticleSystem* partsys = new SnowParticleSystem();
114 partsys->parse(reader);
116 } else if(name == "particles-rain") {
117 RainParticleSystem* partsys = new RainParticleSystem();
118 partsys->parse(reader);
120 } else if(name == "particles-comets") {
121 CometParticleSystem* partsys = new CometParticleSystem();
122 partsys->parse(reader);
124 } else if(name == "particles-ghosts") {
125 GhostParticleSystem* partsys = new GhostParticleSystem();
126 partsys->parse(reader);
128 } else if(name == "particles-clouds") {
129 CloudParticleSystem* partsys = new CloudParticleSystem();
130 partsys->parse(reader);
132 } else if(name == "money") { // for compatibility with old maps
133 return new Jumpy(reader);
137 return create_object(name, reader);
138 } catch(std::exception& e) {
139 std::cerr << e.what() << "\n";
146 Sector::parse(const lisp::Lisp& sector)
150 lisp::ListIterator iter(§or);
152 const std::string& token = iter.item();
153 if(token == "name") {
154 iter.value()->get(name);
155 } else if(token == "gravity") {
156 iter.value()->get(gravity);
157 } else if(token == "music") {
158 iter.value()->get(song_title);
159 } else if(token == "spawnpoint") {
160 SpawnPoint* sp = new SpawnPoint(iter.lisp());
161 spawnpoints.push_back(sp);
162 } else if(token == "init-script") {
163 iter.value()->get(init_script);
165 GameObject* object = parse_object(token, *(iter.lisp()));
172 update_game_objects();
175 throw std::runtime_error("sector does not contain a solid tile layer.");
179 std::cerr << "sector '" << name << "' does not contain a camera.\n";
180 update_game_objects();
181 add_object(new Camera(this));
184 update_game_objects();
188 Sector::parse_old_format(const lisp::Lisp& reader)
193 reader.get("gravity", gravity);
195 std::string backgroundimage;
196 reader.get("background", backgroundimage);
198 reader.get("bkgd_speed", bgspeed);
201 Color bkgd_top, bkgd_bottom;
202 int r = 0, g = 0, b = 128;
203 reader.get("bkgd_red_top", r);
204 reader.get("bkgd_green_top", g);
205 reader.get("bkgd_blue_top", b);
206 bkgd_top.red = static_cast<float> (r) / 255.0f;
207 bkgd_top.green = static_cast<float> (g) / 255.0f;
208 bkgd_top.blue = static_cast<float> (b) / 255.0f;
210 reader.get("bkgd_red_bottom", r);
211 reader.get("bkgd_green_bottom", g);
212 reader.get("bkgd_blue_bottom", b);
213 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
214 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
215 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
217 if(backgroundimage != "") {
218 Background* background = new Background;
219 background->set_image(backgroundimage, bgspeed);
220 add_object(background);
222 Background* background = new Background;
223 background->set_gradient(bkgd_top, bkgd_bottom);
224 add_object(background);
227 std::string particlesystem;
228 reader.get("particle_system", particlesystem);
229 if(particlesystem == "clouds")
230 add_object(new CloudParticleSystem());
231 else if(particlesystem == "snow")
232 add_object(new SnowParticleSystem());
233 else if(particlesystem == "rain")
234 add_object(new RainParticleSystem());
236 Vector startpos(100, 170);
237 reader.get("start_pos_x", startpos.x);
238 reader.get("start_pos_y", startpos.y);
240 SpawnPoint* spawn = new SpawnPoint;
241 spawn->pos = startpos;
242 spawn->name = "main";
243 spawnpoints.push_back(spawn);
245 song_title = "chipdisko.ogg";
246 reader.get("music", song_title);
248 int width = 30, height = 15;
249 reader.get("width", width);
250 reader.get("height", height);
252 std::vector<unsigned int> tiles;
253 if(reader.get_vector("interactive-tm", tiles)
254 || reader.get_vector("tilemap", tiles)) {
255 TileMap* tilemap = new TileMap();
256 tilemap->set(width, height, tiles, LAYER_TILES, true);
260 if(reader.get_vector("background-tm", tiles)) {
261 TileMap* tilemap = new TileMap();
262 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
266 if(reader.get_vector("foreground-tm", tiles)) {
267 TileMap* tilemap = new TileMap();
268 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
272 // read reset-points (now spawn-points)
273 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
275 lisp::ListIterator iter(resetpoints);
277 if(iter.item() == "point") {
279 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
281 SpawnPoint* sp = new SpawnPoint;
284 spawnpoints.push_back(sp);
287 std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
293 const lisp::Lisp* objects = reader.get_lisp("objects");
295 lisp::ListIterator iter(objects);
297 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
301 std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
307 Camera* camera = new Camera(this);
310 update_game_objects();
313 throw std::runtime_error("sector does not contain a solid tile layer.");
316 update_game_objects();
320 Sector::fix_old_tiles()
323 for(size_t x=0; x < solids->get_width(); ++x) {
324 for(size_t y=0; y < solids->get_height(); ++y) {
325 const Tile* tile = solids->get_tile(x, y);
326 Vector pos(x*32, y*32);
328 if(tile->getID() == 112) {
329 add_object(new InvisibleBlock(pos));
330 solids->change(x, y, 0);
331 } else if(tile->getAttributes() & Tile::COIN) {
332 add_object(new Coin(pos));
333 solids->change(x, y, 0);
334 } else if(tile->getAttributes() & Tile::FULLBOX) {
335 add_object(new BonusBlock(pos, tile->getData()));
336 solids->change(x, y, 0);
337 } else if(tile->getAttributes() & Tile::BRICK) {
338 add_object(new Brick(pos, tile->getData()));
339 solids->change(x, y, 0);
340 } else if(tile->getAttributes() & Tile::GOAL) {
341 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
342 add_object(new SequenceTrigger(pos, sequence));
343 solids->change(x, y, 0);
350 Sector::write(lisp::Writer& writer)
352 writer.write_string("name", name);
353 writer.write_float("gravity", gravity);
354 writer.write_string("music", song_title);
357 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
359 SpawnPoint* spawn = *i;
360 writer.start_list("spawn-points");
361 writer.write_string("name", spawn->name);
362 writer.write_float("x", spawn->pos.x);
363 writer.write_float("y", spawn->pos.y);
364 writer.end_list("spawn-points");
368 for(GameObjects::iterator i = gameobjects.begin();
369 i != gameobjects.end(); ++i) {
370 Serializable* serializable = dynamic_cast<Serializable*> (*i);
372 serializable->write(writer);
377 Sector::add_object(GameObject* object)
379 // make sure the object isn't already in the list
381 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
384 assert("object already added to sector" == 0);
387 for(GameObjects::iterator i = gameobjects_new.begin();
388 i != gameobjects_new.end(); ++i) {
390 assert("object already added to sector" == 0);
395 gameobjects_new.push_back(object);
399 Sector::activate(const std::string& spawnpoint)
402 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
404 if((*i)->name == spawnpoint) {
410 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
411 if(spawnpoint != "main") {
414 activate(Vector(0, 0));
421 if(init_script != "") {
422 ScriptInterpreter::add_script_object(this,
423 std::string("Sector(") + name + ") - init", init_script);
428 Sector::activate(const Vector& player_pos)
432 player->move(player_pos);
433 camera->reset(player->get_pos());
437 Sector::get_active_region()
440 camera->get_translation() - Vector(1600, 1200),
441 camera->get_translation() + Vector(1600, 1200));
445 Sector::update(float elapsed_time)
447 player->check_bounds(camera);
450 CollisionGridIterator iter(*grid, get_active_region());
451 while(MovingObject* object = iter.next()) {
452 if(!object->is_valid())
455 object->update(elapsed_time);
459 for(GameObjects::iterator i = gameobjects.begin();
460 i != gameobjects.end(); ++i) {
461 GameObject* object = *i;
462 if(!object->is_valid())
465 object->update(elapsed_time);
469 /* Handle all possible collisions. */
471 update_game_objects();
475 Sector::update_game_objects()
477 /** cleanup marked objects */
478 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
479 i != gameobjects.end(); /* nothing */) {
480 GameObject* object = *i;
482 if(object->is_valid()) {
487 Bullet* bullet = dynamic_cast<Bullet*> (object);
490 std::remove(bullets.begin(), bullets.end(), bullet),
494 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
496 grid->remove_object(movingobject);
500 i = gameobjects.erase(i);
503 /* add newly created objects */
504 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
505 i != gameobjects_new.end(); ++i)
507 GameObject* object = *i;
509 Bullet* bullet = dynamic_cast<Bullet*> (object);
511 bullets.push_back(bullet);
514 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
516 grid->add_object(movingobject);
519 TileMap* tilemap = dynamic_cast<TileMap*> (object);
520 if(tilemap && tilemap->is_solid()) {
524 std::cerr << "Another solid tilemaps added. Ignoring.";
528 Camera* camera = dynamic_cast<Camera*> (object);
530 if(this->camera != 0) {
531 std::cerr << "Warning: Multiple cameras added. Ignoring.";
534 this->camera = camera;
537 gameobjects.push_back(object);
539 gameobjects_new.clear();
543 Sector::draw(DrawingContext& context)
545 context.push_transform();
546 context.set_translation(camera->get_translation());
549 CollisionGridIterator iter(*grid, get_active_region());
550 while(MovingObject* object = iter.next()) {
551 if(!object->is_valid())
554 object->draw(context);
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);
567 context.pop_transform();
570 static const float DELTA = .001;
573 Sector::collision_tilemap(MovingObject* object, int depth)
577 std::cout << "Max collision depth reached.\n";
579 object->movement = Vector(0, 0);
583 // calculate rectangle where the object will move
585 if(object->get_movement().x >= 0) {
586 x1 = object->get_pos().x;
587 x2 = object->get_bbox().p2.x + object->get_movement().x;
589 x1 = object->get_pos().x + object->get_movement().x;
590 x2 = object->get_bbox().p2.x;
593 if(object->get_movement().y >= 0) {
594 y1 = object->get_pos().y;
595 y2 = object->get_bbox().p2.y + object->get_movement().y;
597 y1 = object->get_pos().y + object->get_movement().y;
598 y2 = object->get_bbox().p2.y;
601 // test with all tiles in this rectangle
602 int starttilex = int(x1-1) / 32;
603 int starttiley = int(y1-1) / 32;
604 int max_x = int(x2+1);
605 int max_y = int(y2+1);
607 TilemapCollisionHit temphit, hit;
608 Rect dest = object->get_bbox();
609 dest.move(object->movement);
611 hit.time = -1; // represents an invalid value
612 for(int x = starttilex; x*32 < max_x; ++x) {
613 for(int y = starttiley; y*32 < max_y; ++y) {
614 const Tile* tile = solids->get_tile(x, y);
617 // skip non-solid tiles
618 if(tile->getAttributes() == 0)
620 // only handle unisolid when the player is falling down and when he was
621 // above the tile before
622 if(tile->getAttributes() & Tile::UNISOLID) {
623 if(object->movement.y < 0 || object->get_bbox().p2.y > y*32)
627 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
629 Vector p1(x*32, y*32);
630 Vector p2((x+1)*32, (y+1)*32);
631 triangle = AATriangle(p1, p2, tile->getData());
633 if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
635 hit.tileflags |= tile->getAttributes();
636 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
637 temphit.tileflags = hit.tileflags;
641 } else { // normal rectangular tile
642 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
643 if(Collision::rectangle_rectangle(temphit, dest,
644 object->movement, rect)) {
645 hit.tileflags |= tile->getAttributes();
646 if(temphit.time > hit.time && (tile->getAttributes() & Tile::SOLID)) {
647 temphit.tileflags = hit.tileflags;
655 // did we collide at all?
656 if(hit.tileflags == 0)
659 // call collision function
660 HitResponse response = object->collision(*solids, hit);
661 if(response == ABORT_MOVE) {
662 object->movement = Vector(0, 0);
665 if(response == FORCE_MOVE) {
668 // move out of collision and try again
669 object->movement += hit.normal * (hit.depth + DELTA);
670 collision_tilemap(object, depth+1);
674 Sector::collision_object(MovingObject* object1, MovingObject* object2)
677 Rect dest1 = object1->get_bbox();
678 dest1.move(object1->get_movement());
679 Rect dest2 = object2->get_bbox();
680 dest2.move(object2->get_movement());
682 Vector movement = object1->get_movement() - object2->get_movement();
683 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
684 HitResponse response1 = object1->collision(*object2, hit);
686 HitResponse response2 = object2->collision(*object1, hit);
688 if(response1 != CONTINUE) {
689 if(response1 == ABORT_MOVE)
690 object1->movement = Vector(0, 0);
691 if(response2 == CONTINUE)
692 object2->movement += hit.normal * (hit.depth + DELTA);
693 } else if(response2 != CONTINUE) {
694 if(response2 == ABORT_MOVE)
695 object2->movement = Vector(0, 0);
696 if(response1 == CONTINUE)
697 object1->movement += -hit.normal * (hit.depth + DELTA);
699 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
700 object2->movement += hit.normal * (hit.depth/2 + DELTA);
706 Sector::collision_handler()
709 grid->check_collisions();
711 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
712 i != gameobjects.end(); ++i) {
713 GameObject* gameobject = *i;
714 if(!gameobject->is_valid())
716 MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
719 if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
720 movingobject->bbox.move(movingobject->movement);
721 movingobject->movement = Vector(0, 0);
725 // collision with tilemap
726 if(! (movingobject->movement == Vector(0, 0)))
727 collision_tilemap(movingobject, 0);
729 // collision with other objects
730 for(std::vector<GameObject*>::iterator i2 = i+1;
731 i2 != gameobjects.end(); ++i2) {
732 GameObject* other_object = *i2;
733 if(!other_object->is_valid()
734 || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
736 MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
740 collision_object(movingobject, movingobject2);
743 movingobject->bbox.move(movingobject->get_movement());
744 movingobject->movement = Vector(0, 0);
750 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
752 // TODO remove this function and move these checks elsewhere...
753 static const size_t MAX_FIRE_BULLETS = 2;
754 static const size_t MAX_ICE_BULLETS = 1;
756 Bullet* new_bullet = 0;
757 if(player_status->bonus == FIRE_BONUS) {
758 if(bullets.size() > MAX_FIRE_BULLETS-1)
760 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
761 } else if(player_status->bonus == ICE_BONUS) {
762 if(bullets.size() > MAX_ICE_BULLETS-1)
764 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
768 add_object(new_bullet);
770 sound_manager->play("sounds/shoot.wav");
776 Sector::add_smoke_cloud(const Vector& pos)
778 add_object(new SmokeCloud(pos));
783 Sector::add_floating_text(const Vector& pos, const std::string& text)
785 add_object(new FloatingText(pos, text));
789 Sector::play_music(MusicType type)
792 switch(currentmusic) {
794 sound_manager->play_music(std::string("music/") + song_title);
797 sound_manager->play_music("music/salcon.ogg");
800 sound_manager->play_music("");
806 Sector::get_music_type()
812 Sector::get_total_badguys()
814 int total_badguys = 0;
815 for(GameObjects::iterator i = gameobjects.begin();
816 i != gameobjects.end(); ++i) {
817 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
818 if (badguy && badguy->countMe)
822 return total_badguys;
826 Sector::inside(const Rect& rect) const
828 if(rect.p1.x > solids->get_width() * 32
829 || rect.p1.y > solids->get_height() * 32
830 || rect.p2.x < 0 || rect.p2.y < 0)