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_interactive.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);
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);
161 } else if(token == "spawnpoint") {
162 SpawnPoint* sp = new SpawnPoint(iter.lisp());
163 spawnpoints.push_back(sp);
164 } else if(token == "init-script") {
165 iter.value()->get(init_script);
167 GameObject* object = parse_object(token, *(iter.lisp()));
174 update_game_objects();
177 std::cerr << "sector '" << name << "' does not contain a camera.\n";
178 update_game_objects();
179 add_object(new Camera(this));
182 throw std::runtime_error("sector does not contain a solid tile layer.");
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);
210 reader.get("bkgd_red_bottom", r);
211 reader.get("bkgd_green_bottom", g);
212 reader.get("bkgd_blue_bottom", b);
214 bkgd_bottom.green = g;
215 bkgd_bottom.blue = b;
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 = "Mortimers_chipdisko.mod";
246 reader.get("music", song_title);
249 int width, height = 15;
250 reader.get("width", width);
251 reader.get("height", height);
253 std::vector<unsigned int> tiles;
254 if(reader.get_vector("interactive-tm", tiles)
255 || reader.get_vector("tilemap", tiles)) {
256 TileMap* tilemap = new TileMap();
257 tilemap->set(width, height, tiles, LAYER_TILES, true);
261 if(reader.get_vector("background-tm", tiles)) {
262 TileMap* tilemap = new TileMap();
263 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
267 if(reader.get_vector("foreground-tm", tiles)) {
268 TileMap* tilemap = new TileMap();
269 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
273 // read reset-points (now spawn-points)
274 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
276 lisp::ListIterator iter(resetpoints);
278 if(iter.item() == "point") {
280 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
282 SpawnPoint* sp = new SpawnPoint;
285 spawnpoints.push_back(sp);
288 std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n";
294 const lisp::Lisp* objects = reader.get_lisp("objects");
296 lisp::ListIterator iter(objects);
298 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
302 std::cerr << "Unknown object '" << iter.item() << "' in level.\n";
308 Camera* camera = new Camera(this);
311 update_game_objects();
313 update_game_objects();
315 throw std::runtime_error("sector does not contain a solid tile layer.");
319 Sector::fix_old_tiles()
322 for(size_t x=0; x < solids->get_width(); ++x) {
323 for(size_t y=0; y < solids->get_height(); ++y) {
324 const Tile* tile = solids->get_tile(x, y);
325 Vector pos(x*32, y*32);
327 if(tile->getID() == 112) {
328 add_object(new InvisibleBlock(pos));
329 solids->change(x, y, 0);
330 } else if(tile->getID() == 295) {
331 add_object(new Spike(pos, Spike::NORTH));
332 solids->change(x, y, 0);
333 } else if(tile->getID() == 296) {
334 add_object(new Spike(pos, Spike::EAST));
335 solids->change(x, y, 0);
336 } else if(tile->getID() == 297) {
337 add_object(new Spike(pos, Spike::SOUTH));
338 solids->change(x, y, 0);
339 } else if(tile->getID() == 298) {
340 add_object(new Spike(pos, Spike::WEST));
341 solids->change(x, y, 0);
342 } else if(tile->getAttributes() & Tile::COIN) {
343 add_object(new Coin(pos));
344 solids->change(x, y, 0);
345 } else if(tile->getAttributes() & Tile::FULLBOX) {
346 add_object(new BonusBlock(pos, tile->getData()));
347 solids->change(x, y, 0);
348 } else if(tile->getAttributes() & Tile::BRICK) {
349 add_object(new Brick(pos, tile->getData()));
350 solids->change(x, y, 0);
351 } else if(tile->getAttributes() & Tile::GOAL) {
352 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
353 add_object(new SequenceTrigger(pos, sequence));
354 solids->change(x, y, 0);
361 Sector::write(lisp::Writer& writer)
363 writer.write_string("name", name);
364 writer.write_float("gravity", gravity);
365 writer.write_string("music", song_title);
368 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
370 SpawnPoint* spawn = *i;
371 writer.start_list("spawn-points");
372 writer.write_string("name", spawn->name);
373 writer.write_float("x", spawn->pos.x);
374 writer.write_float("y", spawn->pos.y);
375 writer.end_list("spawn-points");
379 for(GameObjects::iterator i = gameobjects.begin();
380 i != gameobjects.end(); ++i) {
381 Serializable* serializable = dynamic_cast<Serializable*> (*i);
383 serializable->write(writer);
388 Sector::add_object(GameObject* object)
390 // make sure the object isn't already in the list
392 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
395 assert("object already added to sector" == 0);
398 for(GameObjects::iterator i = gameobjects_new.begin();
399 i != gameobjects_new.end(); ++i) {
401 assert("object already added to sector" == 0);
406 gameobjects_new.push_back(object);
410 Sector::activate(const std::string& spawnpoint)
413 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
415 if((*i)->name == spawnpoint) {
421 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
422 if(spawnpoint != "main") {
425 activate(Vector(0, 0));
432 if(init_script != "") {
433 ScriptInterpreter::add_script_object(this,
434 std::string("Sector(") + name + ") - init", init_script);
439 Sector::activate(const Vector& player_pos)
443 player->move(player_pos);
444 camera->reset(player->get_pos());
448 Sector::get_active_region()
451 camera->get_translation() - Vector(1600, 1200),
452 camera->get_translation() + Vector(1600, 1200));
456 Sector::update(float elapsed_time)
458 player->check_bounds(camera);
461 CollisionGridIterator iter(*grid, get_active_region());
462 while(MovingObject* object = iter.next()) {
463 if(!object->is_valid())
466 object->update(elapsed_time);
470 for(GameObjects::iterator i = gameobjects.begin();
471 i != gameobjects.end(); ++i) {
472 GameObject* object = *i;
473 if(!object->is_valid())
476 object->update(elapsed_time);
480 /* Handle all possible collisions. */
482 update_game_objects();
486 Sector::update_game_objects()
488 /** cleanup marked objects */
489 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
490 i != gameobjects.end(); /* nothing */) {
491 GameObject* object = *i;
493 if(object->is_valid()) {
498 Bullet* bullet = dynamic_cast<Bullet*> (object);
501 std::remove(bullets.begin(), bullets.end(), bullet),
505 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
507 grid->remove_object(movingobject);
511 i = gameobjects.erase(i);
514 /* add newly created objects */
515 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
516 i != gameobjects_new.end(); ++i)
518 GameObject* object = *i;
520 Bullet* bullet = dynamic_cast<Bullet*> (object);
522 bullets.push_back(bullet);
525 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
527 grid->add_object(movingobject);
530 TileMap* tilemap = dynamic_cast<TileMap*> (object);
531 if(tilemap && tilemap->is_solid()) {
535 std::cerr << "Another solid tilemaps added. Ignoring.";
539 Camera* camera = dynamic_cast<Camera*> (object);
541 if(this->camera != 0) {
542 std::cerr << "Warning: Multiple cameras added. Ignoring.";
545 this->camera = camera;
548 gameobjects.push_back(object);
550 gameobjects_new.clear();
554 Sector::draw(DrawingContext& context)
556 context.push_transform();
557 context.set_translation(camera->get_translation());
560 CollisionGridIterator iter(*grid, get_active_region());
561 while(MovingObject* object = iter.next()) {
562 if(!object->is_valid())
565 object->draw(context);
568 for(GameObjects::iterator i = gameobjects.begin();
569 i != gameobjects.end(); ++i) {
570 GameObject* object = *i;
571 if(!object->is_valid())
574 object->draw(context);
578 context.pop_transform();
581 static const float DELTA = .001;
584 Sector::collision_tilemap(MovingObject* object, int depth)
588 std::cout << "Max collision depth reached.\n";
590 object->movement = Vector(0, 0);
594 // calculate rectangle where the object will move
596 if(object->get_movement().x >= 0) {
597 x1 = object->get_pos().x;
598 x2 = object->get_bbox().p2.x + object->get_movement().x;
600 x1 = object->get_pos().x + object->get_movement().x;
601 x2 = object->get_bbox().p2.x;
604 if(object->get_movement().y >= 0) {
605 y1 = object->get_pos().y;
606 y2 = object->get_bbox().p2.y + object->get_movement().y;
608 y1 = object->get_pos().y + object->get_movement().y;
609 y2 = object->get_bbox().p2.y;
612 // test with all tiles in this rectangle
613 int starttilex = int(x1-1) / 32;
614 int starttiley = int(y1-1) / 32;
615 int max_x = int(x2+1);
616 int max_y = int(y2+1);
618 CollisionHit temphit, hit;
619 Rect dest = object->get_bbox();
620 dest.move(object->movement);
621 hit.time = -1; // represents an invalid value
622 for(int x = starttilex; x*32 < max_x; ++x) {
623 for(int y = starttiley; y*32 < max_y; ++y) {
624 const Tile* tile = solids->get_tile(x, y);
627 // skip non-solid tiles
628 if(!(tile->getAttributes() & Tile::SOLID))
630 // only handle unisolid when the player is falling down and when he was
631 // above the tile before
632 if(tile->getAttributes() & Tile::UNISOLID) {
633 if(object->movement.y < 0 || object->get_bbox().p2.y > y*32)
637 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
639 Vector p1(x*32, y*32);
640 Vector p2((x+1)*32, (y+1)*32);
641 triangle = AATriangle(p1, p2, tile->getData());
643 if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
645 if(temphit.time > hit.time)
648 } else { // normal rectangular tile
649 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
650 if(Collision::rectangle_rectangle(temphit, dest,
651 object->movement, rect)) {
652 if(temphit.time > hit.time)
659 // did we collide at all?
663 // call collision function
664 HitResponse response = object->collision(*solids, hit);
665 if(response == ABORT_MOVE) {
666 object->movement = Vector(0, 0);
669 if(response == FORCE_MOVE) {
672 // move out of collision and try again
673 object->movement += hit.normal * (hit.depth + DELTA);
674 collision_tilemap(object, depth+1);
678 Sector::collision_object(MovingObject* object1, MovingObject* object2)
681 Rect dest1 = object1->get_bbox();
682 dest1.move(object1->get_movement());
683 Rect dest2 = object2->get_bbox();
684 dest2.move(object2->get_movement());
686 Vector movement = object1->get_movement() - object2->get_movement();
687 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
688 HitResponse response1 = object1->collision(*object2, hit);
690 HitResponse response2 = object2->collision(*object1, hit);
692 if(response1 != CONTINUE) {
693 if(response1 == ABORT_MOVE)
694 object1->movement = Vector(0, 0);
695 if(response2 == CONTINUE)
696 object2->movement += hit.normal * (hit.depth + DELTA);
697 } else if(response2 != CONTINUE) {
698 if(response2 == ABORT_MOVE)
699 object2->movement = Vector(0, 0);
700 if(response1 == CONTINUE)
701 object1->movement += -hit.normal * (hit.depth + DELTA);
703 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
704 object2->movement += hit.normal * (hit.depth/2 + DELTA);
710 Sector::collision_handler()
713 grid->check_collisions();
715 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
716 i != gameobjects.end(); ++i) {
717 GameObject* gameobject = *i;
718 if(!gameobject->is_valid())
720 MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
723 if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
724 movingobject->bbox.move(movingobject->movement);
725 movingobject->movement = Vector(0, 0);
729 // collision with tilemap
730 if(! (movingobject->movement == Vector(0, 0)))
731 collision_tilemap(movingobject, 0);
733 // collision with other objects
734 for(std::vector<GameObject*>::iterator i2 = i+1;
735 i2 != gameobjects.end(); ++i2) {
736 GameObject* other_object = *i2;
737 if(!other_object->is_valid()
738 || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
740 MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
744 collision_object(movingobject, movingobject2);
747 movingobject->bbox.move(movingobject->get_movement());
748 movingobject->movement = Vector(0, 0);
754 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
756 // TODO remove this function and move these checks elsewhere...
757 static const size_t MAX_FIRE_BULLETS = 2;
758 static const size_t MAX_ICE_BULLETS = 1;
760 Bullet* new_bullet = 0;
761 if(player_status.bonus == FIRE_BONUS) {
762 if(bullets.size() > MAX_FIRE_BULLETS-1)
764 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
765 } else if(player_status.bonus == ICE_BONUS) {
766 if(bullets.size() > MAX_ICE_BULLETS-1)
768 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
772 add_object(new_bullet);
774 sound_manager->play_sound("shoot");
780 Sector::add_smoke_cloud(const Vector& pos)
782 add_object(new SmokeCloud(pos));
787 Sector::add_floating_text(const Vector& pos, const std::string& text)
789 add_object(new FloatingText(pos, text));
795 level_song = sound_manager->load_music("/music/" + song_title);
799 Sector::play_music(MusicType type)
802 switch(currentmusic) {
804 sound_manager->play_music(level_song);
807 sound_manager->play_music(herring_song);
810 sound_manager->halt_music();
816 Sector::get_music_type()
822 Sector::get_total_badguys()
824 int total_badguys = 0;
825 for(GameObjects::iterator i = gameobjects.begin();
826 i != gameobjects.end(); ++i) {
827 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
828 if (badguy && badguy->countMe)
832 return total_badguys;
836 Sector::inside(const Rect& rect) const
838 if(rect.p1.x > solids->get_width() * 32
839 || rect.p1.y > solids->get_height() * 32
840 || rect.p2.x < 0 || rect.p2.y < 0)