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/particlesystem_absolute.h"
36 #include "object/tilemap.h"
37 #include "lisp/parser.h"
38 #include "lisp/lisp.h"
39 #include "lisp/writer.h"
40 #include "lisp/list_iterator.h"
42 #include "audio/sound_manager.h"
43 #include "game_session.h"
44 #include "resources.h"
45 #include "statistics.h"
46 #include "collision_grid.h"
47 #include "collision_grid_iterator.h"
48 #include "object_factory.h"
49 #include "collision.h"
50 #include "spawn_point.h"
51 #include "math/rect.h"
52 #include "math/aatriangle.h"
53 #include "object/coin.h"
54 #include "object/block.h"
55 #include "object/invisible_block.h"
56 #include "object/bullet.h"
57 #include "object/text_object.h"
58 #include "badguy/jumpy.h"
59 #include "badguy/spike.h"
60 #include "trigger/sequence_trigger.h"
61 #include "player_status.h"
62 #include "scripting/script_interpreter.h"
63 #include "scripting/sound.h"
64 #include "scripting/scripted_object.h"
65 #include "scripting/text.h"
69 Sector* Sector::_current = 0;
72 : gravity(10), player(0), solids(0), camera(0),
73 currentmusic(LEVEL_MUSIC)
75 song_title = "Mortimers_chipdisko.mod";
76 player = new Player(&player_status);
79 grid = new CollisionGrid(32000, 32000);
84 update_game_objects();
85 assert(gameobjects_new.size() == 0);
89 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
94 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
103 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
105 if(name == "camera") {
106 Camera* camera = new Camera(this);
107 camera->parse(reader);
109 } else if(name == "particles-snow") {
110 SnowParticleSystem* partsys = new SnowParticleSystem();
111 partsys->parse(reader);
113 } else if(name == "particles-rain") {
114 RainParticleSystem* partsys = new RainParticleSystem();
115 partsys->parse(reader);
117 } else if(name == "particles-ghost") {
118 GhostParticleSystem* partsys = new GhostParticleSystem();
119 partsys->parse(reader);
121 } else if(name == "particles-clouds") {
122 CloudParticleSystem* partsys = new CloudParticleSystem();
123 partsys->parse(reader);
125 } else if(name == "money") { // for compatibility with old maps
126 return new Jumpy(reader);
130 return create_object(name, reader);
131 } catch(std::exception& e) {
132 std::cerr << e.what() << "\n";
139 Sector::parse(const lisp::Lisp& sector)
143 lisp::ListIterator iter(§or);
145 const std::string& token = iter.item();
146 if(token == "name") {
147 iter.value()->get(name);
148 } else if(token == "gravity") {
149 iter.value()->get(gravity);
150 } else if(token == "music") {
151 iter.value()->get(song_title);
153 } else if(token == "spawnpoint") {
154 SpawnPoint* sp = new SpawnPoint(iter.lisp());
155 spawnpoints.push_back(sp);
156 } else if(token == "init-script") {
157 iter.value()->get(init_script);
159 GameObject* object = parse_object(token, *(iter.lisp()));
166 update_game_objects();
169 std::cerr << "sector '" << name << "' does not contain a camera.\n";
170 update_game_objects();
171 add_object(new Camera(this));
174 throw std::runtime_error("sector does not contain a solid tile layer.");
176 update_game_objects();
180 Sector::parse_old_format(const lisp::Lisp& reader)
185 reader.get("gravity", gravity);
187 std::string backgroundimage;
188 reader.get("background", backgroundimage);
190 reader.get("bkgd_speed", bgspeed);
193 Color bkgd_top, bkgd_bottom;
194 int r = 0, g = 0, b = 128;
195 reader.get("bkgd_red_top", r);
196 reader.get("bkgd_green_top", g);
197 reader.get("bkgd_blue_top", b);
202 reader.get("bkgd_red_bottom", r);
203 reader.get("bkgd_green_bottom", g);
204 reader.get("bkgd_blue_bottom", b);
206 bkgd_bottom.green = g;
207 bkgd_bottom.blue = b;
209 if(backgroundimage != "") {
210 Background* background = new Background;
211 background->set_image(backgroundimage, bgspeed);
212 add_object(background);
214 Background* background = new Background;
215 background->set_gradient(bkgd_top, bkgd_bottom);
216 add_object(background);
219 std::string particlesystem;
220 reader.get("particle_system", particlesystem);
221 if(particlesystem == "clouds")
222 add_object(new CloudParticleSystem());
223 else if(particlesystem == "snow")
224 add_object(new SnowParticleSystem());
225 else if(particlesystem == "rain")
226 add_object(new RainParticleSystem());
228 Vector startpos(100, 170);
229 reader.get("start_pos_x", startpos.x);
230 reader.get("start_pos_y", startpos.y);
232 SpawnPoint* spawn = new SpawnPoint;
233 spawn->pos = startpos;
234 spawn->name = "main";
235 spawnpoints.push_back(spawn);
237 song_title = "Mortimers_chipdisko.mod";
238 reader.get("music", song_title);
241 int width, height = 15;
242 reader.get("width", width);
243 reader.get("height", height);
245 std::vector<unsigned int> tiles;
246 if(reader.get_vector("interactive-tm", tiles)
247 || reader.get_vector("tilemap", tiles)) {
248 TileMap* tilemap = new TileMap();
249 tilemap->set(width, height, tiles, LAYER_TILES, true);
253 if(reader.get_vector("background-tm", tiles)) {
254 TileMap* tilemap = new TileMap();
255 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
259 if(reader.get_vector("foreground-tm", tiles)) {
260 TileMap* tilemap = new TileMap();
261 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
265 // read reset-points (now spawn-points)
266 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
268 lisp::ListIterator iter(resetpoints);
270 if(iter.item() == "point") {
272 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
274 SpawnPoint* sp = new SpawnPoint;
277 spawnpoints.push_back(sp);
280 std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
286 const lisp::Lisp* objects = reader.get_lisp("objects");
288 lisp::ListIterator iter(objects);
290 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
294 std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
300 Camera* camera = new Camera(this);
303 update_game_objects();
305 update_game_objects();
307 throw std::runtime_error("sector does not contain a solid tile layer.");
311 Sector::fix_old_tiles()
314 for(size_t x=0; x < solids->get_width(); ++x) {
315 for(size_t y=0; y < solids->get_height(); ++y) {
316 const Tile* tile = solids->get_tile(x, y);
317 Vector pos(x*32, y*32);
319 if(tile->getID() == 112) {
320 add_object(new InvisibleBlock(pos));
321 solids->change(x, y, 0);
322 } else if(tile->getID() == 295) {
323 add_object(new Spike(pos, Spike::NORTH));
324 solids->change(x, y, 0);
325 } else if(tile->getID() == 296) {
326 add_object(new Spike(pos, Spike::EAST));
327 solids->change(x, y, 0);
328 } else if(tile->getID() == 297) {
329 add_object(new Spike(pos, Spike::SOUTH));
330 solids->change(x, y, 0);
331 } else if(tile->getID() == 298) {
332 add_object(new Spike(pos, Spike::WEST));
333 solids->change(x, y, 0);
334 } else if(tile->getAttributes() & Tile::COIN) {
335 add_object(new Coin(pos));
336 solids->change(x, y, 0);
337 } else if(tile->getAttributes() & Tile::FULLBOX) {
338 add_object(new BonusBlock(pos, tile->getData()));
339 solids->change(x, y, 0);
340 } else if(tile->getAttributes() & Tile::BRICK) {
341 add_object(new Brick(pos, tile->getData()));
342 solids->change(x, y, 0);
343 } else if(tile->getAttributes() & Tile::GOAL) {
344 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
345 add_object(new SequenceTrigger(pos, sequence));
346 solids->change(x, y, 0);
353 Sector::write(lisp::Writer& writer)
355 writer.write_string("name", name);
356 writer.write_float("gravity", gravity);
357 writer.write_string("music", song_title);
360 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
362 SpawnPoint* spawn = *i;
363 writer.start_list("spawn-points");
364 writer.write_string("name", spawn->name);
365 writer.write_float("x", spawn->pos.x);
366 writer.write_float("y", spawn->pos.y);
367 writer.end_list("spawn-points");
371 for(GameObjects::iterator i = gameobjects.begin();
372 i != gameobjects.end(); ++i) {
373 Serializable* serializable = dynamic_cast<Serializable*> (*i);
375 serializable->write(writer);
380 Sector::add_object(GameObject* object)
382 // make sure the object isn't already in the list
384 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
387 assert("object already added to sector" == 0);
390 for(GameObjects::iterator i = gameobjects_new.begin();
391 i != gameobjects_new.end(); ++i) {
393 assert("object already added to sector" == 0);
398 gameobjects_new.push_back(object);
402 Sector::activate(const std::string& spawnpoint)
405 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
407 if((*i)->name == spawnpoint) {
413 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
414 if(spawnpoint != "main") {
417 activate(Vector(0, 0));
424 if(init_script != "") {
425 ScriptInterpreter::add_script_object(this,
426 std::string("Sector(") + name + ") - init", init_script);
431 Sector::activate(const Vector& player_pos)
435 player->move(player_pos);
436 camera->reset(player->get_pos());
440 Sector::get_active_region()
443 camera->get_translation() - Vector(1600, 1200),
444 camera->get_translation() + Vector(1600, 1200));
448 Sector::update(float elapsed_time)
450 player->check_bounds(camera);
453 CollisionGridIterator iter(*grid, get_active_region());
454 while(MovingObject* object = iter.next()) {
455 if(!object->is_valid())
458 object->update(elapsed_time);
462 for(GameObjects::iterator i = gameobjects.begin();
463 i != gameobjects.end(); ++i) {
464 GameObject* object = *i;
465 if(!object->is_valid())
468 object->update(elapsed_time);
472 /* Handle all possible collisions. */
474 update_game_objects();
478 Sector::update_game_objects()
480 /** cleanup marked objects */
481 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
482 i != gameobjects.end(); /* nothing */) {
483 GameObject* object = *i;
485 if(object->is_valid()) {
490 Bullet* bullet = dynamic_cast<Bullet*> (object);
493 std::remove(bullets.begin(), bullets.end(), bullet),
496 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
498 grid->remove_object(movingobject);
501 i = gameobjects.erase(i);
504 /* add newly created objects */
505 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
506 i != gameobjects_new.end(); ++i)
508 GameObject* object = *i;
510 Bullet* bullet = dynamic_cast<Bullet*> (object);
512 bullets.push_back(bullet);
514 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
516 grid->add_object(movingobject);
518 TileMap* tilemap = dynamic_cast<TileMap*> (object);
519 if(tilemap && tilemap->is_solid()) {
523 std::cerr << "Another solid tilemaps added. Ignoring.";
527 Camera* camera = dynamic_cast<Camera*> (object);
529 if(this->camera != 0) {
530 std::cerr << "Warning: Multiple cameras added. Ignoring.";
533 this->camera = camera;
536 gameobjects.push_back(object);
538 gameobjects_new.clear();
542 Sector::draw(DrawingContext& context)
544 context.push_transform();
545 context.set_translation(camera->get_translation());
548 CollisionGridIterator iter(*grid, get_active_region());
549 while(MovingObject* object = iter.next()) {
550 if(!object->is_valid())
553 object->draw(context);
556 for(GameObjects::iterator i = gameobjects.begin();
557 i != gameobjects.end(); ++i) {
558 GameObject* object = *i;
559 if(!object->is_valid())
562 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 Rect 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 // skip non-solid tiles
616 if(!(tile->getAttributes() & Tile::SOLID))
618 // only handle unisolid when the player is falling down and when he was
619 // above the tile before
620 if(tile->getAttributes() & Tile::UNISOLID) {
621 if(object->movement.y < 0 || object->get_bbox().p2.y > y*32)
625 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
627 Vector p1(x*32, y*32);
628 Vector p2((x+1)*32, (y+1)*32);
629 triangle = AATriangle(p1, p2, tile->getData());
631 if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
633 if(temphit.time > hit.time)
636 } else { // normal rectangular tile
637 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
638 if(Collision::rectangle_rectangle(temphit, dest,
639 object->movement, rect)) {
640 if(temphit.time > hit.time)
647 // did we collide at all?
651 // call collision function
652 HitResponse response = object->collision(*solids, hit);
653 if(response == ABORT_MOVE) {
654 object->movement = Vector(0, 0);
657 if(response == FORCE_MOVE) {
660 // move out of collision and try again
661 object->movement += hit.normal * (hit.depth + DELTA);
662 collision_tilemap(object, depth+1);
666 Sector::collision_object(MovingObject* object1, MovingObject* object2)
669 Rect dest1 = object1->get_bbox();
670 dest1.move(object1->get_movement());
671 Rect dest2 = object2->get_bbox();
672 dest2.move(object2->get_movement());
674 Vector movement = object1->get_movement() - object2->get_movement();
675 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
676 HitResponse response1 = object1->collision(*object2, hit);
678 HitResponse response2 = object2->collision(*object1, hit);
680 if(response1 != CONTINUE) {
681 if(response1 == ABORT_MOVE)
682 object1->movement = Vector(0, 0);
683 if(response2 == CONTINUE)
684 object2->movement += hit.normal * (hit.depth + DELTA);
685 } else if(response2 != CONTINUE) {
686 if(response2 == ABORT_MOVE)
687 object2->movement = Vector(0, 0);
688 if(response1 == CONTINUE)
689 object1->movement += -hit.normal * (hit.depth + DELTA);
691 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
692 object2->movement += hit.normal * (hit.depth/2 + DELTA);
698 Sector::collision_handler()
701 grid->check_collisions();
703 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
704 i != gameobjects.end(); ++i) {
705 GameObject* gameobject = *i;
706 if(!gameobject->is_valid())
708 MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
711 if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
712 movingobject->bbox.move(movingobject->movement);
713 movingobject->movement = Vector(0, 0);
717 // collision with tilemap
718 if(! (movingobject->movement == Vector(0, 0)))
719 collision_tilemap(movingobject, 0);
721 // collision with other objects
722 for(std::vector<GameObject*>::iterator i2 = i+1;
723 i2 != gameobjects.end(); ++i2) {
724 GameObject* other_object = *i2;
725 if(!other_object->is_valid()
726 || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
728 MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
732 collision_object(movingobject, movingobject2);
735 movingobject->bbox.move(movingobject->get_movement());
736 movingobject->movement = Vector(0, 0);
742 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
744 // TODO remove this function and move these checks elsewhere...
745 static const size_t MAX_FIRE_BULLETS = 2;
746 static const size_t MAX_ICE_BULLETS = 1;
748 Bullet* new_bullet = 0;
749 if(player_status.bonus == FIRE_BONUS) {
750 if(bullets.size() > MAX_FIRE_BULLETS-1)
752 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
753 } else if(player_status.bonus == ICE_BONUS) {
754 if(bullets.size() > MAX_ICE_BULLETS-1)
756 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
760 add_object(new_bullet);
762 sound_manager->play_sound("shoot");
768 Sector::add_smoke_cloud(const Vector& pos)
770 add_object(new SmokeCloud(pos));
775 Sector::add_floating_text(const Vector& pos, const std::string& text)
777 add_object(new FloatingText(pos, text));
783 level_song = sound_manager->load_music(
784 get_resource_filename("/music/" + song_title));
788 Sector::play_music(MusicType type)
791 switch(currentmusic) {
793 sound_manager->play_music(level_song);
796 sound_manager->play_music(herring_song);
799 sound_manager->halt_music();
805 Sector::get_music_type()
811 Sector::get_total_badguys()
813 int total_badguys = 0;
814 for(GameObjects::iterator i = gameobjects.begin();
815 i != gameobjects.end(); ++i) {
816 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
817 if (badguy && badguy->countMe)
821 return total_badguys;
825 Sector::inside(const Rect& rect) const
827 if(rect.p1.x > solids->get_width() * 32
828 || rect.p1.y > solids->get_height() * 32
829 || rect.p2.x < 0 || rect.p2.y < 0)