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 "badguy/spike.hpp"
60 #include "trigger/sequence_trigger.hpp"
61 #include "player_status.hpp"
62 #include "scripting/script_interpreter.hpp"
63 #include "scripting/sound.hpp"
64 #include "scripting/scripted_object.hpp"
65 #include "scripting/text.hpp"
69 Sector* Sector::_current = 0;
72 : gravity(10), player(0), solids(0), camera(0),
73 currentmusic(LEVEL_MUSIC)
75 song_title = "chipdisko.ogg";
76 player = new Player(&player_status);
80 grid = new CollisionGrid(32000, 32000);
88 update_game_objects();
89 assert(gameobjects_new.size() == 0);
93 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
98 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
107 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
109 if(name == "camera") {
110 Camera* camera = new Camera(this);
111 camera->parse(reader);
113 } else if(name == "particles-snow") {
114 SnowParticleSystem* partsys = new SnowParticleSystem();
115 partsys->parse(reader);
117 } else if(name == "particles-rain") {
118 RainParticleSystem* partsys = new RainParticleSystem();
119 partsys->parse(reader);
121 } else if(name == "particles-comets") {
122 CometParticleSystem* partsys = new CometParticleSystem();
123 partsys->parse(reader);
125 } else if(name == "particles-ghosts") {
126 GhostParticleSystem* partsys = new GhostParticleSystem();
127 partsys->parse(reader);
129 } else if(name == "particles-clouds") {
130 CloudParticleSystem* partsys = new CloudParticleSystem();
131 partsys->parse(reader);
133 } else if(name == "money") { // for compatibility with old maps
134 return new Jumpy(reader);
138 return create_object(name, reader);
139 } catch(std::exception& e) {
140 std::cerr << e.what() << "\n";
147 Sector::parse(const lisp::Lisp& sector)
151 lisp::ListIterator iter(§or);
153 const std::string& token = iter.item();
154 if(token == "name") {
155 iter.value()->get(name);
156 } else if(token == "gravity") {
157 iter.value()->get(gravity);
158 } else if(token == "music") {
159 iter.value()->get(song_title);
160 } else if(token == "spawnpoint") {
161 SpawnPoint* sp = new SpawnPoint(iter.lisp());
162 spawnpoints.push_back(sp);
163 } else if(token == "init-script") {
164 iter.value()->get(init_script);
166 GameObject* object = parse_object(token, *(iter.lisp()));
173 update_game_objects();
176 std::cerr << "sector '" << name << "' does not contain a camera.\n";
177 update_game_objects();
178 add_object(new Camera(this));
181 throw std::runtime_error("sector does not contain a solid tile layer.");
183 update_game_objects();
187 Sector::parse_old_format(const lisp::Lisp& reader)
192 reader.get("gravity", gravity);
194 std::string backgroundimage;
195 reader.get("background", backgroundimage);
197 reader.get("bkgd_speed", bgspeed);
200 Color bkgd_top, bkgd_bottom;
201 int r = 0, g = 0, b = 128;
202 reader.get("bkgd_red_top", r);
203 reader.get("bkgd_green_top", g);
204 reader.get("bkgd_blue_top", b);
209 reader.get("bkgd_red_bottom", r);
210 reader.get("bkgd_green_bottom", g);
211 reader.get("bkgd_blue_bottom", b);
213 bkgd_bottom.green = g;
214 bkgd_bottom.blue = b;
216 if(backgroundimage != "") {
217 Background* background = new Background;
218 background->set_image(backgroundimage, bgspeed);
219 add_object(background);
221 Background* background = new Background;
222 background->set_gradient(bkgd_top, bkgd_bottom);
223 add_object(background);
226 std::string particlesystem;
227 reader.get("particle_system", particlesystem);
228 if(particlesystem == "clouds")
229 add_object(new CloudParticleSystem());
230 else if(particlesystem == "snow")
231 add_object(new SnowParticleSystem());
232 else if(particlesystem == "rain")
233 add_object(new RainParticleSystem());
235 Vector startpos(100, 170);
236 reader.get("start_pos_x", startpos.x);
237 reader.get("start_pos_y", startpos.y);
239 SpawnPoint* spawn = new SpawnPoint;
240 spawn->pos = startpos;
241 spawn->name = "main";
242 spawnpoints.push_back(spawn);
244 song_title = "chipdisko.ogg";
245 reader.get("music", song_title);
247 int width, height = 15;
248 reader.get("width", width);
249 reader.get("height", height);
251 std::vector<unsigned int> tiles;
252 if(reader.get_vector("interactive-tm", tiles)
253 || reader.get_vector("tilemap", tiles)) {
254 TileMap* tilemap = new TileMap();
255 tilemap->set(width, height, tiles, LAYER_TILES, true);
259 if(reader.get_vector("background-tm", tiles)) {
260 TileMap* tilemap = new TileMap();
261 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
265 if(reader.get_vector("foreground-tm", tiles)) {
266 TileMap* tilemap = new TileMap();
267 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
271 // read reset-points (now spawn-points)
272 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
274 lisp::ListIterator iter(resetpoints);
276 if(iter.item() == "point") {
278 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
280 SpawnPoint* sp = new SpawnPoint;
283 spawnpoints.push_back(sp);
286 std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
292 const lisp::Lisp* objects = reader.get_lisp("objects");
294 lisp::ListIterator iter(objects);
296 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
300 std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
306 Camera* camera = new Camera(this);
309 update_game_objects();
311 update_game_objects();
313 throw std::runtime_error("sector does not contain a solid tile layer.");
317 Sector::fix_old_tiles()
320 for(size_t x=0; x < solids->get_width(); ++x) {
321 for(size_t y=0; y < solids->get_height(); ++y) {
322 const Tile* tile = solids->get_tile(x, y);
323 Vector pos(x*32, y*32);
325 if(tile->getID() == 112) {
326 add_object(new InvisibleBlock(pos));
327 solids->change(x, y, 0);
328 } else if(tile->getID() == 295) {
329 add_object(new Spike(pos, Spike::NORTH));
330 solids->change(x, y, 0);
331 } else if(tile->getID() == 296) {
332 add_object(new Spike(pos, Spike::EAST));
333 solids->change(x, y, 0);
334 } else if(tile->getID() == 297) {
335 add_object(new Spike(pos, Spike::SOUTH));
336 solids->change(x, y, 0);
337 } else if(tile->getID() == 298) {
338 add_object(new Spike(pos, Spike::WEST));
339 solids->change(x, y, 0);
340 } else if(tile->getAttributes() & Tile::COIN) {
341 add_object(new Coin(pos));
342 solids->change(x, y, 0);
343 } else if(tile->getAttributes() & Tile::FULLBOX) {
344 add_object(new BonusBlock(pos, tile->getData()));
345 solids->change(x, y, 0);
346 } else if(tile->getAttributes() & Tile::BRICK) {
347 add_object(new Brick(pos, tile->getData()));
348 solids->change(x, y, 0);
349 } else if(tile->getAttributes() & Tile::GOAL) {
350 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
351 add_object(new SequenceTrigger(pos, sequence));
352 solids->change(x, y, 0);
359 Sector::write(lisp::Writer& writer)
361 writer.write_string("name", name);
362 writer.write_float("gravity", gravity);
363 writer.write_string("music", song_title);
366 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
368 SpawnPoint* spawn = *i;
369 writer.start_list("spawn-points");
370 writer.write_string("name", spawn->name);
371 writer.write_float("x", spawn->pos.x);
372 writer.write_float("y", spawn->pos.y);
373 writer.end_list("spawn-points");
377 for(GameObjects::iterator i = gameobjects.begin();
378 i != gameobjects.end(); ++i) {
379 Serializable* serializable = dynamic_cast<Serializable*> (*i);
381 serializable->write(writer);
386 Sector::add_object(GameObject* object)
388 // make sure the object isn't already in the list
390 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
393 assert("object already added to sector" == 0);
396 for(GameObjects::iterator i = gameobjects_new.begin();
397 i != gameobjects_new.end(); ++i) {
399 assert("object already added to sector" == 0);
404 gameobjects_new.push_back(object);
408 Sector::activate(const std::string& spawnpoint)
411 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
413 if((*i)->name == spawnpoint) {
419 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
420 if(spawnpoint != "main") {
423 activate(Vector(0, 0));
430 if(init_script != "") {
431 ScriptInterpreter::add_script_object(this,
432 std::string("Sector(") + name + ") - init", init_script);
437 Sector::activate(const Vector& player_pos)
441 player->move(player_pos);
442 camera->reset(player->get_pos());
446 Sector::get_active_region()
449 camera->get_translation() - Vector(1600, 1200),
450 camera->get_translation() + Vector(1600, 1200));
454 Sector::update(float elapsed_time)
456 player->check_bounds(camera);
459 CollisionGridIterator iter(*grid, get_active_region());
460 while(MovingObject* object = iter.next()) {
461 if(!object->is_valid())
464 object->update(elapsed_time);
468 for(GameObjects::iterator i = gameobjects.begin();
469 i != gameobjects.end(); ++i) {
470 GameObject* object = *i;
471 if(!object->is_valid())
474 object->update(elapsed_time);
478 /* Handle all possible collisions. */
480 update_game_objects();
484 Sector::update_game_objects()
486 /** cleanup marked objects */
487 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
488 i != gameobjects.end(); /* nothing */) {
489 GameObject* object = *i;
491 if(object->is_valid()) {
496 Bullet* bullet = dynamic_cast<Bullet*> (object);
499 std::remove(bullets.begin(), bullets.end(), bullet),
503 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
505 grid->remove_object(movingobject);
509 i = gameobjects.erase(i);
512 /* add newly created objects */
513 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
514 i != gameobjects_new.end(); ++i)
516 GameObject* object = *i;
518 Bullet* bullet = dynamic_cast<Bullet*> (object);
520 bullets.push_back(bullet);
523 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
525 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());
558 CollisionGridIterator iter(*grid, get_active_region());
559 while(MovingObject* object = iter.next()) {
560 if(!object->is_valid())
563 object->draw(context);
566 for(GameObjects::iterator i = gameobjects.begin();
567 i != gameobjects.end(); ++i) {
568 GameObject* object = *i;
569 if(!object->is_valid())
572 object->draw(context);
576 context.pop_transform();
579 static const float DELTA = .001;
582 Sector::collision_tilemap(MovingObject* object, int depth)
586 std::cout << "Max collision depth reached.\n";
588 object->movement = Vector(0, 0);
592 // calculate rectangle where the object will move
594 if(object->get_movement().x >= 0) {
595 x1 = object->get_pos().x;
596 x2 = object->get_bbox().p2.x + object->get_movement().x;
598 x1 = object->get_pos().x + object->get_movement().x;
599 x2 = object->get_bbox().p2.x;
602 if(object->get_movement().y >= 0) {
603 y1 = object->get_pos().y;
604 y2 = object->get_bbox().p2.y + object->get_movement().y;
606 y1 = object->get_pos().y + object->get_movement().y;
607 y2 = object->get_bbox().p2.y;
610 // test with all tiles in this rectangle
611 int starttilex = int(x1-1) / 32;
612 int starttiley = int(y1-1) / 32;
613 int max_x = int(x2+1);
614 int max_y = int(y2+1);
616 CollisionHit temphit, hit;
617 Rect dest = object->get_bbox();
618 dest.move(object->movement);
619 hit.time = -1; // represents an invalid value
620 for(int x = starttilex; x*32 < max_x; ++x) {
621 for(int y = starttiley; y*32 < max_y; ++y) {
622 const Tile* tile = solids->get_tile(x, y);
625 // skip non-solid tiles
626 if(!(tile->getAttributes() & Tile::SOLID))
628 // only handle unisolid when the player is falling down and when he was
629 // above the tile before
630 if(tile->getAttributes() & Tile::UNISOLID) {
631 if(object->movement.y < 0 || object->get_bbox().p2.y > y*32)
635 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
637 Vector p1(x*32, y*32);
638 Vector p2((x+1)*32, (y+1)*32);
639 triangle = AATriangle(p1, p2, tile->getData());
641 if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
643 if(temphit.time > hit.time)
646 } else { // normal rectangular tile
647 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
648 if(Collision::rectangle_rectangle(temphit, dest,
649 object->movement, rect)) {
650 if(temphit.time > hit.time)
657 // did we collide at all?
661 // call collision function
662 HitResponse response = object->collision(*solids, hit);
663 if(response == ABORT_MOVE) {
664 object->movement = Vector(0, 0);
667 if(response == FORCE_MOVE) {
670 // move out of collision and try again
671 object->movement += hit.normal * (hit.depth + DELTA);
672 collision_tilemap(object, depth+1);
676 Sector::collision_object(MovingObject* object1, MovingObject* object2)
679 Rect dest1 = object1->get_bbox();
680 dest1.move(object1->get_movement());
681 Rect dest2 = object2->get_bbox();
682 dest2.move(object2->get_movement());
684 Vector movement = object1->get_movement() - object2->get_movement();
685 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
686 HitResponse response1 = object1->collision(*object2, hit);
688 HitResponse response2 = object2->collision(*object1, hit);
690 if(response1 != CONTINUE) {
691 if(response1 == ABORT_MOVE)
692 object1->movement = Vector(0, 0);
693 if(response2 == CONTINUE)
694 object2->movement += hit.normal * (hit.depth + DELTA);
695 } else if(response2 != CONTINUE) {
696 if(response2 == ABORT_MOVE)
697 object2->movement = Vector(0, 0);
698 if(response1 == CONTINUE)
699 object1->movement += -hit.normal * (hit.depth + DELTA);
701 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
702 object2->movement += hit.normal * (hit.depth/2 + DELTA);
708 Sector::collision_handler()
711 grid->check_collisions();
713 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
714 i != gameobjects.end(); ++i) {
715 GameObject* gameobject = *i;
716 if(!gameobject->is_valid())
718 MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
721 if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
722 movingobject->bbox.move(movingobject->movement);
723 movingobject->movement = Vector(0, 0);
727 // collision with tilemap
728 if(! (movingobject->movement == Vector(0, 0)))
729 collision_tilemap(movingobject, 0);
731 // collision with other objects
732 for(std::vector<GameObject*>::iterator i2 = i+1;
733 i2 != gameobjects.end(); ++i2) {
734 GameObject* other_object = *i2;
735 if(!other_object->is_valid()
736 || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
738 MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
742 collision_object(movingobject, movingobject2);
745 movingobject->bbox.move(movingobject->get_movement());
746 movingobject->movement = Vector(0, 0);
752 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
754 // TODO remove this function and move these checks elsewhere...
755 static const size_t MAX_FIRE_BULLETS = 2;
756 static const size_t MAX_ICE_BULLETS = 1;
758 Bullet* new_bullet = 0;
759 if(player_status.bonus == FIRE_BONUS) {
760 if(bullets.size() > MAX_FIRE_BULLETS-1)
762 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
763 } else if(player_status.bonus == ICE_BONUS) {
764 if(bullets.size() > MAX_ICE_BULLETS-1)
766 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
770 add_object(new_bullet);
772 sound_manager->play("sounds/shoot.wav");
778 Sector::add_smoke_cloud(const Vector& pos)
780 add_object(new SmokeCloud(pos));
785 Sector::add_floating_text(const Vector& pos, const std::string& text)
787 add_object(new FloatingText(pos, text));
791 Sector::play_music(MusicType type)
794 switch(currentmusic) {
796 sound_manager->play_music(std::string("music/") + song_title);
799 sound_manager->play_music("music/salcon.ogg");
802 sound_manager->play_music("");
808 Sector::get_music_type()
814 Sector::get_total_badguys()
816 int total_badguys = 0;
817 for(GameObjects::iterator i = gameobjects.begin();
818 i != gameobjects.end(); ++i) {
819 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
820 if (badguy && badguy->countMe)
824 return total_badguys;
828 Sector::inside(const Rect& rect) const
830 if(rect.p1.x > solids->get_width() * 32
831 || rect.p1.y > solids->get_height() * 32
832 || rect.p2.x < 0 || rect.p2.y < 0)