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.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"
42 #include "game_session.h"
43 #include "resources.h"
44 #include "statistics.h"
45 #include "collision_grid.h"
46 #include "collision_grid_iterator.h"
47 #include "object_factory.h"
48 #include "collision.h"
49 #include "math/rect.h"
50 #include "math/aatriangle.h"
51 #include "object/coin.h"
52 #include "object/block.h"
53 #include "object/invisible_block.h"
54 #include "object/bullet.h"
55 #include "badguy/jumpy.h"
56 #include "badguy/spike.h"
57 #include "trigger/sequence_trigger.h"
58 #include "player_status.h"
62 Sector* Sector::_current = 0;
65 : gravity(10), player(0), solids(0), camera(0),
66 currentmusic(LEVEL_MUSIC)
68 song_title = "Mortimers_chipdisko.mod";
69 player = new Player(&player_status);
72 grid = new CollisionGrid(32000, 32000);
77 update_game_objects();
78 assert(gameobjects_new.size() == 0);
82 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
87 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
96 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
98 if(name == "camera") {
99 Camera* camera = new Camera(this);
100 camera->parse(reader);
102 } else if(name == "particles-snow") {
103 SnowParticleSystem* partsys = new SnowParticleSystem();
104 partsys->parse(reader);
106 } else if(name == "particles-rain") {
107 RainParticleSystem* partsys = new RainParticleSystem();
108 partsys->parse(reader);
110 } else if(name == "particles-clouds") {
111 CloudParticleSystem* partsys = new CloudParticleSystem();
112 partsys->parse(reader);
114 } else if(name == "money") { // for compatibility with old maps
115 return new Jumpy(reader);
119 return create_object(name, reader);
120 } catch(std::exception& e) {
121 std::cerr << e.what() << "\n";
128 Sector::parse(const lisp::Lisp& sector)
132 lisp::ListIterator iter(§or);
134 const std::string& token = iter.item();
135 if(token == "name") {
136 iter.value()->get(name);
137 } else if(token == "gravity") {
138 iter.value()->get(gravity);
139 } else if(token == "music") {
140 iter.value()->get(song_title);
142 } else if(token == "spawnpoint") {
143 const lisp::Lisp* spawnpoint_lisp = iter.lisp();
145 SpawnPoint* sp = new SpawnPoint;
146 spawnpoint_lisp->get("name", sp->name);
147 spawnpoint_lisp->get("x", sp->pos.x);
148 spawnpoint_lisp->get("y", sp->pos.y);
149 spawnpoints.push_back(sp);
151 GameObject* object = parse_object(token, *(iter.lisp()));
158 update_game_objects();
161 std::cerr << "sector '" << name << "' does not contain a camera.\n";
162 update_game_objects();
163 add_object(new Camera(this));
166 throw std::runtime_error("sector does not contain a solid tile layer.");
168 update_game_objects();
172 Sector::parse_old_format(const lisp::Lisp& reader)
177 reader.get("gravity", gravity);
179 std::string backgroundimage;
180 reader.get("background", backgroundimage);
182 reader.get("bkgd_speed", bgspeed);
185 Color bkgd_top, bkgd_bottom;
186 int r = 0, g = 0, b = 128;
187 reader.get("bkgd_red_top", r);
188 reader.get("bkgd_green_top", g);
189 reader.get("bkgd_blue_top", b);
194 reader.get("bkgd_red_bottom", r);
195 reader.get("bkgd_green_bottom", g);
196 reader.get("bkgd_blue_bottom", b);
198 bkgd_bottom.green = g;
199 bkgd_bottom.blue = b;
201 if(backgroundimage != "") {
202 Background* background = new Background;
203 background->set_image(backgroundimage, bgspeed);
204 add_object(background);
206 Background* background = new Background;
207 background->set_gradient(bkgd_top, bkgd_bottom);
208 add_object(background);
211 std::string particlesystem;
212 reader.get("particle_system", particlesystem);
213 if(particlesystem == "clouds")
214 add_object(new CloudParticleSystem());
215 else if(particlesystem == "snow")
216 add_object(new SnowParticleSystem());
217 else if(particlesystem == "rain")
218 add_object(new RainParticleSystem());
220 Vector startpos(100, 170);
221 reader.get("start_pos_x", startpos.x);
222 reader.get("start_pos_y", startpos.y);
224 SpawnPoint* spawn = new SpawnPoint;
225 spawn->pos = startpos;
226 spawn->name = "main";
227 spawnpoints.push_back(spawn);
229 song_title = "Mortimers_chipdisko.mod";
230 reader.get("music", song_title);
233 int width, height = 15;
234 reader.get("width", width);
235 reader.get("height", height);
237 std::vector<unsigned int> tiles;
238 if(reader.get_vector("interactive-tm", tiles)
239 || reader.get_vector("tilemap", tiles)) {
240 TileMap* tilemap = new TileMap();
241 tilemap->set(width, height, tiles, LAYER_TILES, true);
245 if(reader.get_vector("background-tm", tiles)) {
246 TileMap* tilemap = new TileMap();
247 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
251 if(reader.get_vector("foreground-tm", tiles)) {
252 TileMap* tilemap = new TileMap();
253 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
257 // read reset-points (now spawn-points)
258 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
260 lisp::ListIterator iter(resetpoints);
262 if(iter.item() == "point") {
264 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
266 SpawnPoint* sp = new SpawnPoint;
269 spawnpoints.push_back(sp);
272 std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
278 const lisp::Lisp* objects = reader.get_lisp("objects");
280 lisp::ListIterator iter(objects);
282 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
286 std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
292 Camera* camera = new Camera(this);
295 update_game_objects();
297 update_game_objects();
299 throw std::runtime_error("sector does not contain a solid tile layer.");
303 Sector::fix_old_tiles()
306 for(size_t x=0; x < solids->get_width(); ++x) {
307 for(size_t y=0; y < solids->get_height(); ++y) {
308 const Tile* tile = solids->get_tile(x, y);
309 Vector pos(x*32, y*32);
311 if(tile->getID() == 112) {
312 add_object(new InvisibleBlock(pos));
313 solids->change(x, y, 0);
314 } else if(tile->getID() == 295) {
315 add_object(new Spike(pos, Spike::NORTH));
316 solids->change(x, y, 0);
317 } else if(tile->getID() == 296) {
318 add_object(new Spike(pos, Spike::EAST));
319 solids->change(x, y, 0);
320 } else if(tile->getID() == 297) {
321 add_object(new Spike(pos, Spike::SOUTH));
322 solids->change(x, y, 0);
323 } else if(tile->getID() == 298) {
324 add_object(new Spike(pos, Spike::WEST));
325 solids->change(x, y, 0);
326 } else if(tile->getAttributes() & Tile::COIN) {
327 add_object(new Coin(pos));
328 solids->change(x, y, 0);
329 } else if(tile->getAttributes() & Tile::FULLBOX) {
330 add_object(new BonusBlock(pos, tile->getData()));
331 solids->change(x, y, 0);
332 } else if(tile->getAttributes() & Tile::BRICK) {
333 add_object(new Brick(pos, tile->getData()));
334 solids->change(x, y, 0);
335 } else if(tile->getAttributes() & Tile::GOAL) {
336 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
337 add_object(new SequenceTrigger(pos, sequence));
338 solids->change(x, y, 0);
345 Sector::write(lisp::Writer& writer)
347 writer.write_string("name", name);
348 writer.write_float("gravity", gravity);
349 writer.write_string("music", song_title);
352 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
354 SpawnPoint* spawn = *i;
355 writer.start_list("spawn-points");
356 writer.write_string("name", spawn->name);
357 writer.write_float("x", spawn->pos.x);
358 writer.write_float("y", spawn->pos.y);
359 writer.end_list("spawn-points");
363 for(GameObjects::iterator i = gameobjects.begin();
364 i != gameobjects.end(); ++i) {
365 Serializable* serializable = dynamic_cast<Serializable*> (*i);
367 serializable->write(writer);
372 Sector::add_object(GameObject* object)
374 // make sure the object isn't already in the list
376 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
379 assert("object already added to sector" == 0);
382 for(GameObjects::iterator i = gameobjects_new.begin();
383 i != gameobjects_new.end(); ++i) {
385 assert("object already added to sector" == 0);
390 gameobjects_new.push_back(object);
394 Sector::activate(const std::string& spawnpoint)
397 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
399 if((*i)->name == spawnpoint) {
405 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
406 if(spawnpoint != "main") {
409 activate(Vector(0, 0));
417 Sector::activate(const Vector& player_pos)
421 player->move(player_pos);
422 camera->reset(player->get_pos());
426 Sector::get_active_region()
429 camera->get_translation() - Vector(1600, 1200),
430 camera->get_translation() + Vector(1600, 1200));
434 Sector::action(float elapsed_time)
436 player->check_bounds(camera);
439 CollisionGridIterator iter(*grid, get_active_region());
440 while(MovingObject* object = iter.next()) {
441 if(!object->is_valid())
444 object->action(elapsed_time);
448 for(GameObjects::iterator i = gameobjects.begin();
449 i != gameobjects.end(); ++i) {
450 GameObject* object = *i;
451 if(!object->is_valid())
454 object->action(elapsed_time);
458 /* Handle all possible collisions. */
460 update_game_objects();
464 Sector::update_game_objects()
466 /** cleanup marked objects */
467 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
468 i != gameobjects.end(); /* nothing */) {
469 GameObject* object = *i;
471 if(object->is_valid()) {
476 Bullet* bullet = dynamic_cast<Bullet*> (object);
479 std::remove(bullets.begin(), bullets.end(), bullet),
482 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
484 grid->remove_object(movingobject);
487 i = gameobjects.erase(i);
490 /* add newly created objects */
491 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
492 i != gameobjects_new.end(); ++i)
494 GameObject* object = *i;
496 Bullet* bullet = dynamic_cast<Bullet*> (object);
498 bullets.push_back(bullet);
500 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
502 grid->add_object(movingobject);
504 TileMap* tilemap = dynamic_cast<TileMap*> (object);
505 if(tilemap && tilemap->is_solid()) {
509 std::cerr << "Another solid tilemaps added. Ignoring.";
513 Camera* camera = dynamic_cast<Camera*> (object);
515 if(this->camera != 0) {
516 std::cerr << "Warning: Multiple cameras added. Ignoring.";
519 this->camera = camera;
522 gameobjects.push_back(object);
524 gameobjects_new.clear();
528 Sector::draw(DrawingContext& context)
530 context.push_transform();
531 context.set_translation(camera->get_translation());
534 CollisionGridIterator iter(*grid, get_active_region());
535 while(MovingObject* object = iter.next()) {
536 if(!object->is_valid())
539 object->draw(context);
542 for(GameObjects::iterator i = gameobjects.begin();
543 i != gameobjects.end(); ++i) {
544 GameObject* object = *i;
545 if(!object->is_valid())
548 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 Rect 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 // skip non-solid tiles
602 if(!(tile->getAttributes() & Tile::SOLID))
604 // only handle unisolid when the player is falling down and when he was
605 // above the tile before
606 if(tile->getAttributes() & Tile::UNISOLID) {
607 if(object->movement.y < 0 || object->get_bbox().p2.y > y*32)
611 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
613 Vector p1(x*32, y*32);
614 Vector p2((x+1)*32, (y+1)*32);
615 triangle = AATriangle(p1, p2, tile->getData());
617 if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
619 if(temphit.time > hit.time)
622 } else { // normal rectangular tile
623 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
624 if(Collision::rectangle_rectangle(temphit, dest,
625 object->movement, rect)) {
626 if(temphit.time > hit.time)
633 // did we collide at all?
637 // call collision function
638 HitResponse response = object->collision(*solids, hit);
639 if(response == ABORT_MOVE) {
640 object->movement = Vector(0, 0);
643 if(response == FORCE_MOVE) {
646 // move out of collision and try again
647 object->movement += hit.normal * (hit.depth + DELTA);
648 collision_tilemap(object, depth+1);
652 Sector::collision_object(MovingObject* object1, MovingObject* object2)
655 Rect dest1 = object1->get_bbox();
656 dest1.move(object1->get_movement());
657 Rect dest2 = object2->get_bbox();
658 dest2.move(object2->get_movement());
660 Vector movement = object1->get_movement() - object2->get_movement();
661 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
662 HitResponse response1 = object1->collision(*object2, hit);
664 HitResponse response2 = object2->collision(*object1, hit);
666 if(response1 != CONTINUE) {
667 if(response1 == ABORT_MOVE)
668 object1->movement = Vector(0, 0);
669 if(response2 == CONTINUE)
670 object2->movement += hit.normal * (hit.depth + DELTA);
671 } else if(response2 != CONTINUE) {
672 if(response2 == ABORT_MOVE)
673 object2->movement = Vector(0, 0);
674 if(response1 == CONTINUE)
675 object1->movement += -hit.normal * (hit.depth + DELTA);
677 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
678 object2->movement += hit.normal * (hit.depth/2 + DELTA);
684 Sector::collision_handler()
687 grid->check_collisions();
689 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
690 i != gameobjects.end(); ++i) {
691 GameObject* gameobject = *i;
692 if(!gameobject->is_valid())
694 MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
697 if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
698 movingobject->bbox.move(movingobject->movement);
699 movingobject->movement = Vector(0, 0);
703 // collision with tilemap
704 if(! (movingobject->movement == Vector(0, 0)))
705 collision_tilemap(movingobject, 0);
707 // collision with other objects
708 for(std::vector<GameObject*>::iterator i2 = i+1;
709 i2 != gameobjects.end(); ++i2) {
710 GameObject* other_object = *i2;
711 if(!other_object->is_valid()
712 || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
714 MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
718 collision_object(movingobject, movingobject2);
721 movingobject->bbox.move(movingobject->get_movement());
722 movingobject->movement = Vector(0, 0);
728 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
730 // TODO remove this function and move these checks elsewhere...
731 static const size_t MAX_FIRE_BULLETS = 2;
732 static const size_t MAX_ICE_BULLETS = 1;
734 Bullet* new_bullet = 0;
735 if(player_status.bonus == FIRE_BONUS) {
736 if(bullets.size() > MAX_FIRE_BULLETS-1)
738 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
739 } else if(player_status.bonus == ICE_BONUS) {
740 if(bullets.size() > MAX_ICE_BULLETS-1)
742 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
746 add_object(new_bullet);
748 sound_manager->play_sound("shoot");
754 Sector::add_smoke_cloud(const Vector& pos)
756 add_object(new SmokeCloud(pos));
761 Sector::add_floating_text(const Vector& pos, const std::string& text)
763 add_object(new FloatingText(pos, text));
769 level_song = sound_manager->load_music(
770 get_resource_filename("/music/" + song_title));
774 Sector::play_music(MusicType type)
777 switch(currentmusic) {
779 sound_manager->play_music(level_song);
782 sound_manager->play_music(herring_song);
785 sound_manager->halt_music();
791 Sector::get_music_type()
797 Sector::get_total_badguys()
799 int total_badguys = 0;
800 for(GameObjects::iterator i = gameobjects.begin();
801 i != gameobjects.end(); ++i) {
802 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
807 return total_badguys;
811 Sector::inside(const Rect& rect) const
813 if(rect.p1.x > solids->get_width() * 32
814 || rect.p1.y > solids->get_height() * 32
815 || rect.p2.x < 0 || rect.p2.y < 0)