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->getAttributes() & Tile::COIN) {
329 add_object(new Coin(pos));
330 solids->change(x, y, 0);
331 } else if(tile->getAttributes() & Tile::FULLBOX) {
332 add_object(new BonusBlock(pos, tile->getData()));
333 solids->change(x, y, 0);
334 } else if(tile->getAttributes() & Tile::BRICK) {
335 add_object(new Brick(pos, tile->getData()));
336 solids->change(x, y, 0);
337 } else if(tile->getAttributes() & Tile::GOAL) {
338 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
339 add_object(new SequenceTrigger(pos, sequence));
340 solids->change(x, y, 0);
347 Sector::write(lisp::Writer& writer)
349 writer.write_string("name", name);
350 writer.write_float("gravity", gravity);
351 writer.write_string("music", song_title);
354 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
356 SpawnPoint* spawn = *i;
357 writer.start_list("spawn-points");
358 writer.write_string("name", spawn->name);
359 writer.write_float("x", spawn->pos.x);
360 writer.write_float("y", spawn->pos.y);
361 writer.end_list("spawn-points");
365 for(GameObjects::iterator i = gameobjects.begin();
366 i != gameobjects.end(); ++i) {
367 Serializable* serializable = dynamic_cast<Serializable*> (*i);
369 serializable->write(writer);
374 Sector::add_object(GameObject* object)
376 // make sure the object isn't already in the list
378 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
381 assert("object already added to sector" == 0);
384 for(GameObjects::iterator i = gameobjects_new.begin();
385 i != gameobjects_new.end(); ++i) {
387 assert("object already added to sector" == 0);
392 gameobjects_new.push_back(object);
396 Sector::activate(const std::string& spawnpoint)
399 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
401 if((*i)->name == spawnpoint) {
407 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
408 if(spawnpoint != "main") {
411 activate(Vector(0, 0));
418 if(init_script != "") {
419 ScriptInterpreter::add_script_object(this,
420 std::string("Sector(") + name + ") - init", init_script);
425 Sector::activate(const Vector& player_pos)
429 player->move(player_pos);
430 camera->reset(player->get_pos());
434 Sector::get_active_region()
437 camera->get_translation() - Vector(1600, 1200),
438 camera->get_translation() + Vector(1600, 1200));
442 Sector::update(float elapsed_time)
444 player->check_bounds(camera);
447 CollisionGridIterator iter(*grid, get_active_region());
448 while(MovingObject* object = iter.next()) {
449 if(!object->is_valid())
452 object->update(elapsed_time);
456 for(GameObjects::iterator i = gameobjects.begin();
457 i != gameobjects.end(); ++i) {
458 GameObject* object = *i;
459 if(!object->is_valid())
462 object->update(elapsed_time);
466 /* Handle all possible collisions. */
468 update_game_objects();
472 Sector::update_game_objects()
474 /** cleanup marked objects */
475 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
476 i != gameobjects.end(); /* nothing */) {
477 GameObject* object = *i;
479 if(object->is_valid()) {
484 Bullet* bullet = dynamic_cast<Bullet*> (object);
487 std::remove(bullets.begin(), bullets.end(), bullet),
491 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
493 grid->remove_object(movingobject);
497 i = gameobjects.erase(i);
500 /* add newly created objects */
501 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
502 i != gameobjects_new.end(); ++i)
504 GameObject* object = *i;
506 Bullet* bullet = dynamic_cast<Bullet*> (object);
508 bullets.push_back(bullet);
511 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
513 grid->add_object(movingobject);
516 TileMap* tilemap = dynamic_cast<TileMap*> (object);
517 if(tilemap && tilemap->is_solid()) {
521 std::cerr << "Another solid tilemaps added. Ignoring.";
525 Camera* camera = dynamic_cast<Camera*> (object);
527 if(this->camera != 0) {
528 std::cerr << "Warning: Multiple cameras added. Ignoring.";
531 this->camera = camera;
534 gameobjects.push_back(object);
536 gameobjects_new.clear();
540 Sector::draw(DrawingContext& context)
542 context.push_transform();
543 context.set_translation(camera->get_translation());
546 CollisionGridIterator iter(*grid, get_active_region());
547 while(MovingObject* object = iter.next()) {
548 if(!object->is_valid())
551 object->draw(context);
554 for(GameObjects::iterator i = gameobjects.begin();
555 i != gameobjects.end(); ++i) {
556 GameObject* object = *i;
557 if(!object->is_valid())
560 object->draw(context);
564 context.pop_transform();
567 static const float DELTA = .001;
570 Sector::collision_tilemap(MovingObject* object, int depth)
574 std::cout << "Max collision depth reached.\n";
576 object->movement = Vector(0, 0);
580 // calculate rectangle where the object will move
582 if(object->get_movement().x >= 0) {
583 x1 = object->get_pos().x;
584 x2 = object->get_bbox().p2.x + object->get_movement().x;
586 x1 = object->get_pos().x + object->get_movement().x;
587 x2 = object->get_bbox().p2.x;
590 if(object->get_movement().y >= 0) {
591 y1 = object->get_pos().y;
592 y2 = object->get_bbox().p2.y + object->get_movement().y;
594 y1 = object->get_pos().y + object->get_movement().y;
595 y2 = object->get_bbox().p2.y;
598 // test with all tiles in this rectangle
599 int starttilex = int(x1-1) / 32;
600 int starttiley = int(y1-1) / 32;
601 int max_x = int(x2+1);
602 int max_y = int(y2+1);
604 TilemapCollisionHit temphit, hit;
605 Rect dest = object->get_bbox();
606 dest.move(object->movement);
608 hit.time = -1; // represents an invalid value
609 for(int x = starttilex; x*32 < max_x; ++x) {
610 for(int y = starttiley; y*32 < max_y; ++y) {
611 const Tile* tile = solids->get_tile(x, y);
614 // skip non-solid tiles
615 if(tile->getAttributes() == 0)
617 // only handle unisolid when the player is falling down and when he was
618 // above the tile before
619 if(tile->getAttributes() & Tile::UNISOLID) {
620 if(object->movement.y < 0 || object->get_bbox().p2.y > y*32)
624 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
626 Vector p1(x*32, y*32);
627 Vector p2((x+1)*32, (y+1)*32);
628 triangle = AATriangle(p1, p2, tile->getData());
630 if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
632 hit.tileflags |= tile->getAttributes();
633 if(temphit.time > hit.time) {
634 temphit.tileflags = hit.tileflags;
638 } else { // normal rectangular tile
639 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
640 if(Collision::rectangle_rectangle(temphit, dest,
641 object->movement, rect)) {
642 hit.tileflags |= tile->getAttributes();
643 if(temphit.time > hit.time) {
644 temphit.tileflags = hit.tileflags;
652 // did we collide at all?
656 // call collision function
657 HitResponse response = object->collision(*solids, hit);
658 if(response == ABORT_MOVE) {
659 object->movement = Vector(0, 0);
662 if(response == FORCE_MOVE) {
665 // move out of collision and try again
666 object->movement += hit.normal * (hit.depth + DELTA);
667 collision_tilemap(object, depth+1);
671 Sector::collision_object(MovingObject* object1, MovingObject* object2)
674 Rect dest1 = object1->get_bbox();
675 dest1.move(object1->get_movement());
676 Rect dest2 = object2->get_bbox();
677 dest2.move(object2->get_movement());
679 Vector movement = object1->get_movement() - object2->get_movement();
680 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
681 HitResponse response1 = object1->collision(*object2, hit);
683 HitResponse response2 = object2->collision(*object1, hit);
685 if(response1 != CONTINUE) {
686 if(response1 == ABORT_MOVE)
687 object1->movement = Vector(0, 0);
688 if(response2 == CONTINUE)
689 object2->movement += hit.normal * (hit.depth + DELTA);
690 } else if(response2 != CONTINUE) {
691 if(response2 == ABORT_MOVE)
692 object2->movement = Vector(0, 0);
693 if(response1 == CONTINUE)
694 object1->movement += -hit.normal * (hit.depth + DELTA);
696 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
697 object2->movement += hit.normal * (hit.depth/2 + DELTA);
703 Sector::collision_handler()
706 grid->check_collisions();
708 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
709 i != gameobjects.end(); ++i) {
710 GameObject* gameobject = *i;
711 if(!gameobject->is_valid())
713 MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
716 if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
717 movingobject->bbox.move(movingobject->movement);
718 movingobject->movement = Vector(0, 0);
722 // collision with tilemap
723 if(! (movingobject->movement == Vector(0, 0)))
724 collision_tilemap(movingobject, 0);
726 // collision with other objects
727 for(std::vector<GameObject*>::iterator i2 = i+1;
728 i2 != gameobjects.end(); ++i2) {
729 GameObject* other_object = *i2;
730 if(!other_object->is_valid()
731 || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
733 MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
737 collision_object(movingobject, movingobject2);
740 movingobject->bbox.move(movingobject->get_movement());
741 movingobject->movement = Vector(0, 0);
747 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
749 // TODO remove this function and move these checks elsewhere...
750 static const size_t MAX_FIRE_BULLETS = 2;
751 static const size_t MAX_ICE_BULLETS = 1;
753 Bullet* new_bullet = 0;
754 if(player_status.bonus == FIRE_BONUS) {
755 if(bullets.size() > MAX_FIRE_BULLETS-1)
757 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
758 } else if(player_status.bonus == ICE_BONUS) {
759 if(bullets.size() > MAX_ICE_BULLETS-1)
761 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
765 add_object(new_bullet);
767 sound_manager->play("sounds/shoot.wav");
773 Sector::add_smoke_cloud(const Vector& pos)
775 add_object(new SmokeCloud(pos));
780 Sector::add_floating_text(const Vector& pos, const std::string& text)
782 add_object(new FloatingText(pos, text));
786 Sector::play_music(MusicType type)
789 switch(currentmusic) {
791 sound_manager->play_music(std::string("music/") + song_title);
794 sound_manager->play_music("music/salcon.ogg");
797 sound_manager->play_music("");
803 Sector::get_music_type()
809 Sector::get_total_badguys()
811 int total_badguys = 0;
812 for(GameObjects::iterator i = gameobjects.begin();
813 i != gameobjects.end(); ++i) {
814 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
815 if (badguy && badguy->countMe)
819 return total_badguys;
823 Sector::inside(const Rect& rect) const
825 if(rect.p1.x > solids->get_width() * 32
826 || rect.p1.y > solids->get_height() * 32
827 || rect.p2.x < 0 || rect.p2.y < 0)