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.
29 #include "app/globals.h"
31 #include "utils/lispreader.h"
32 #include "object/gameobjs.h"
33 #include "object/camera.h"
34 #include "object/background.h"
35 #include "object/particlesystem.h"
36 #include "object/tilemap.h"
38 #include "audio/sound_manager.h"
40 #include "resources.h"
41 #include "statistics.h"
42 #include "special/collision.h"
43 #include "math/rectangle.h"
44 #include "math/aatriangle.h"
45 #include "object/coin.h"
46 #include "object/block.h"
47 #include "object/invisible_block.h"
48 #include "object/platform.h"
49 #include "object/bullet.h"
50 #include "badguy/jumpy.h"
51 #include "badguy/snowball.h"
52 #include "badguy/bouncing_snowball.h"
53 #include "badguy/flame.h"
54 #include "badguy/mriceblock.h"
55 #include "badguy/mrbomb.h"
56 #include "badguy/dispenser.h"
57 #include "badguy/spike.h"
58 #include "badguy/nolok_01.h"
59 #include "trigger/door.h"
60 #include "trigger/sequence_trigger.h"
61 #include "trigger/secretarea_trigger.h"
63 Sector* Sector::_current = 0;
66 : gravity(10), player(0), solids(0), camera(0),
67 currentmusic(LEVEL_MUSIC)
69 song_title = "Mortimers_chipdisko.mod";
70 player = new Player();
76 update_game_objects();
77 assert(gameobjects_new.size() == 0);
79 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
84 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
93 Sector::parse_object(const std::string& name, LispReader& reader)
95 if(name == "background") {
96 return new Background(reader);
97 } else if(name == "camera") {
98 Camera* camera = new Camera(this);
99 camera->parse(reader);
101 } else if(name == "tilemap") {
102 return new TileMap(reader);
103 } else if(name == "particles-snow") {
104 SnowParticleSystem* partsys = new SnowParticleSystem();
105 partsys->parse(reader);
107 } else if(name == "particles-clouds") {
108 CloudParticleSystem* partsys = new CloudParticleSystem();
109 partsys->parse(reader);
111 } else if(name == "door") {
112 return new Door(reader);
113 } else if(name == "secretarea") {
114 return new SecretAreaTrigger(reader);
115 } else if(name == "platform") {
116 return new Platform(reader);
117 } else if(name == "jumpy" || name == "money") {
118 return new Jumpy(reader);
119 } else if(name == "snowball") {
120 return new SnowBall(reader);
121 } else if(name == "bouncingsnowball") {
122 return new BouncingSnowball(reader);
123 } else if(name == "flame") {
124 return new Flame(reader);
125 } else if(name == "mriceblock") {
126 return new MrIceBlock(reader);
127 } else if(name == "mrbomb") {
128 return new MrBomb(reader);
129 } else if(name == "dispenser") {
130 return new Dispenser(reader);
131 } else if(name == "spike") {
132 return new Spike(reader);
133 } else if(name == "nolok_01") {
134 return new Nolok_01(reader);
137 std::cerr << "Unknown object type '" << name << "'.\n";
142 Sector::parse(LispReader& lispreader)
146 for(lisp_object_t* cur = lispreader.get_lisp(); !lisp_nil_p(cur);
147 cur = lisp_cdr(cur)) {
148 std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
149 // FIXME: doesn't handle empty data
150 lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
151 LispReader reader(lisp_cdr(lisp_car(cur)));
153 if(token == "name") {
154 name = lisp_string(data);
155 } else if(token == "gravity") {
156 gravity = lisp_real(data);
157 } else if(token == "music") {
158 song_title = lisp_string(data);
160 } else if(token == "spawn-points") {
161 SpawnPoint* sp = new SpawnPoint;
162 reader.read_string("name", sp->name);
163 reader.read_float("x", sp->pos.x);
164 reader.read_float("y", sp->pos.y);
165 spawnpoints.push_back(sp);
167 GameObject* object = parse_object(token, reader);
174 update_game_objects();
176 update_game_objects();
178 std::cerr << "sector '" << name << "' does not contain a camera.\n";
179 camera = new Camera(this);
183 throw std::runtime_error("sector does not contain a solid tile layer.");
187 Sector::parse_old_format(LispReader& reader)
192 reader.read_float("gravity", gravity);
194 std::string backgroundimage;
195 reader.read_string("background", backgroundimage);
197 reader.read_float("bkgd_speed", bgspeed);
200 Color bkgd_top, bkgd_bottom;
201 int r = 0, g = 0, b = 128;
202 reader.read_int("bkgd_red_top", r);
203 reader.read_int("bkgd_green_top", g);
204 reader.read_int("bkgd_blue_top", b);
209 reader.read_int("bkgd_red_bottom", r);
210 reader.read_int("bkgd_green_bottom", g);
211 reader.read_int("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.read_string("particle_system", particlesystem);
228 if(particlesystem == "clouds")
229 add_object(new CloudParticleSystem());
230 else if(particlesystem == "snow")
231 add_object(new SnowParticleSystem());
233 Vector startpos(100, 170);
234 reader.read_float("start_pos_x", startpos.x);
235 reader.read_float("start_pos_y", startpos.y);
237 SpawnPoint* spawn = new SpawnPoint;
238 spawn->pos = startpos;
239 spawn->name = "main";
240 spawnpoints.push_back(spawn);
242 song_title = "Mortimers_chipdisko.mod";
243 reader.read_string("music", song_title);
246 int width, height = 15;
247 reader.read_int("width", width);
248 reader.read_int("height", height);
250 std::vector<unsigned int> tiles;
251 if(reader.read_int_vector("interactive-tm", tiles)
252 || reader.read_int_vector("tilemap", tiles)) {
253 TileMap* tilemap = new TileMap();
254 tilemap->set(width, height, tiles, LAYER_TILES, true);
258 if(reader.read_int_vector("background-tm", tiles)) {
259 TileMap* tilemap = new TileMap();
260 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
264 if(reader.read_int_vector("foreground-tm", tiles)) {
265 TileMap* tilemap = new TileMap();
266 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
270 // read reset-points (now spawn-points)
272 lisp_object_t* cur = 0;
273 if(reader.read_lisp("reset-points", cur)) {
274 while(!lisp_nil_p(cur)) {
275 lisp_object_t* data = lisp_car(cur);
276 LispReader reader(lisp_cdr(data));
279 if(reader.read_float("x", sp_pos.x) && reader.read_float("y", sp_pos.y))
281 SpawnPoint* sp = new SpawnPoint;
284 spawnpoints.push_back(sp);
294 lisp_object_t* cur = 0;
295 if(reader.read_lisp("objects", cur)) {
296 while(!lisp_nil_p(cur)) {
297 lisp_object_t* data = lisp_car(cur);
298 std::string object_type = lisp_symbol(lisp_car(data));
300 LispReader reader(lisp_cdr(data));
302 GameObject* object = parse_object(object_type, reader);
306 std::cerr << "Unknown object '" << object_type << "' in level.\n";
315 Camera* camera = new Camera(this);
318 update_game_objects();
320 update_game_objects();
322 throw std::runtime_error("sector does not contain a solid tile layer.");
326 Sector::fix_old_tiles()
329 for(size_t x=0; x < solids->get_width(); ++x) {
330 for(size_t y=0; y < solids->get_height(); ++y) {
331 const Tile* tile = solids->get_tile(x, y);
332 Vector pos(x*32, y*32);
334 if(tile->getID() == 112) {
335 add_object(new InvisibleBlock(pos));
336 solids->change(x, y, 0);
337 } else if(tile->getID() == 295) {
338 add_object(new Spike(pos, Spike::NORTH));
339 solids->change(x, y, 0);
340 } else if(tile->getID() == 296) {
341 add_object(new Spike(pos, Spike::EAST));
342 solids->change(x, y, 0);
343 } else if(tile->getID() == 297) {
344 add_object(new Spike(pos, Spike::SOUTH));
345 solids->change(x, y, 0);
346 } else if(tile->getID() == 298) {
347 add_object(new Spike(pos, Spike::WEST));
348 solids->change(x, y, 0);
349 } else if(tile->getAttributes() & Tile::COIN) {
350 add_object(new Coin(pos));
351 solids->change(x, y, 0);
352 } else if(tile->getAttributes() & Tile::FULLBOX) {
353 add_object(new BonusBlock(pos, tile->getData()));
354 solids->change(x, y, 0);
355 } else if(tile->getAttributes() & Tile::BRICK) {
356 add_object(new Brick(pos, tile->getData()));
357 solids->change(x, y, 0);
358 } else if(tile->getAttributes() & Tile::GOAL) {
359 add_object(new SequenceTrigger(pos, "endsequence"));
360 solids->change(x, y, 0);
367 Sector::write(LispWriter& writer)
369 writer.write_string("name", name);
370 writer.write_float("gravity", gravity);
371 writer.write_string("music", song_title);
374 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
376 SpawnPoint* spawn = *i;
377 writer.start_list("spawn-points");
378 writer.write_string("name", spawn->name);
379 writer.write_float("x", spawn->pos.x);
380 writer.write_float("y", spawn->pos.y);
381 writer.end_list("spawn-points");
385 for(GameObjects::iterator i = gameobjects.begin();
386 i != gameobjects.end(); ++i) {
387 Serializable* serializable = dynamic_cast<Serializable*> (*i);
389 serializable->write(writer);
394 Sector::add_object(GameObject* object)
396 // make sure the object isn't already in the list
398 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
401 assert("object already added to sector" == 0);
404 for(GameObjects::iterator i = gameobjects_new.begin();
405 i != gameobjects_new.end(); ++i) {
407 assert("object already added to sector" == 0);
412 gameobjects_new.push_back(object);
416 Sector::activate(const std::string& spawnpoint)
420 // Apply bonuses from former levels
421 switch (player_status.bonus)
423 case PlayerStatus::NO_BONUS:
426 case PlayerStatus::FLOWER_BONUS:
427 player->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
430 case PlayerStatus::GROWUP_BONUS:
436 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
438 if((*i)->name == spawnpoint) {
444 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
446 player->move(sp->pos);
449 camera->reset(player->get_pos());
453 Sector::get_best_spawn_point(Vector pos)
455 Vector best_reset_point = Vector(-1,-1);
457 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
459 if((*i)->name != "main")
461 if((*i)->pos.x > best_reset_point.x && (*i)->pos.x < pos.x)
462 best_reset_point = (*i)->pos;
465 return best_reset_point;
469 Sector::action(float elapsed_time)
471 player->check_bounds(camera);
474 for(GameObjects::iterator i = gameobjects.begin();
475 i != gameobjects.end(); ++i) {
476 GameObject* object = *i;
477 if(!object->is_valid())
480 object->action(elapsed_time);
483 /* Handle all possible collisions. */
485 update_game_objects();
489 Sector::update_game_objects()
491 /** cleanup marked objects */
492 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
493 i != gameobjects.end(); /* nothing */) {
494 if((*i)->is_valid() == false) {
495 Bullet* bullet = dynamic_cast<Bullet*> (*i);
498 std::remove(bullets.begin(), bullets.end(), bullet),
502 i = gameobjects.erase(i);
508 /* add newly created objects */
509 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
510 i != gameobjects_new.end(); ++i)
512 Bullet* bullet = dynamic_cast<Bullet*> (*i);
514 bullets.push_back(bullet);
516 TileMap* tilemap = dynamic_cast<TileMap*> (*i);
517 if(tilemap && tilemap->is_solid()) {
521 std::cerr << "Another solid tilemaps added. Ignoring.";
525 Camera* camera = dynamic_cast<Camera*> (*i);
527 if(this->camera != 0) {
528 std::cerr << "Warning: Multiple cameras added. Ignoring.";
531 this->camera = camera;
534 gameobjects.push_back(*i);
536 gameobjects_new.clear();
540 Sector::draw(DrawingContext& context)
542 context.push_transform();
543 context.set_translation(camera->get_translation());
545 for(GameObjects::iterator i = gameobjects.begin();
546 i != gameobjects.end(); ++i) {
547 GameObject* object = *i;
548 if(!object->is_valid())
551 object->draw(context);
554 context.pop_transform();
557 static const float DELTA = .001;
560 Sector::collision_tilemap(MovingObject* object, int depth)
564 std::cout << "Max collision depth reached.\n";
566 object->movement = Vector(0, 0);
570 // calculate rectangle where the object will move
572 if(object->get_movement().x >= 0) {
573 x1 = object->get_pos().x;
574 x2 = object->get_bbox().p2.x + object->get_movement().x;
576 x1 = object->get_pos().x + object->get_movement().x;
577 x2 = object->get_bbox().p2.x;
580 if(object->get_movement().y >= 0) {
581 y1 = object->get_pos().y;
582 y2 = object->get_bbox().p2.y + object->get_movement().y;
584 y1 = object->get_pos().y + object->get_movement().y;
585 y2 = object->get_bbox().p2.y;
588 // test with all tiles in this rectangle
589 int starttilex = int(x1-1) / 32;
590 int starttiley = int(y1-1) / 32;
591 int max_x = int(x2+1);
592 int max_y = int(y2+1);
594 CollisionHit temphit, hit;
595 Rectangle dest = object->get_bbox();
596 dest.move(object->movement);
597 hit.time = -1; // represents an invalid value
598 for(int x = starttilex; x*32 < max_x; ++x) {
599 for(int y = starttiley; y*32 < max_y; ++y) {
600 const Tile* tile = solids->get_tile(x, y);
603 if(!(tile->getAttributes() & Tile::SOLID))
605 if((tile->getAttributes() & Tile::UNISOLID) && object->movement.y < 0)
608 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
610 Vector p1(x*32, y*32);
611 Vector p2((x+1)*32, (y+1)*32);
612 triangle = AATriangle(p1, p2, tile->getData());
614 if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
616 if(temphit.time > hit.time)
619 } else { // normal rectangular tile
620 Rectangle rect(x*32, y*32, (x+1)*32, (y+1)*32);
621 if(Collision::rectangle_rectangle(temphit, dest,
622 object->movement, rect)) {
623 if(temphit.time > hit.time)
630 // did we collide at all?
634 // call collision function
635 HitResponse response = object->collision(*solids, hit);
636 if(response == ABORT_MOVE) {
637 object->movement = Vector(0, 0);
640 if(response == FORCE_MOVE) {
643 // move out of collision and try again
644 object->movement += hit.normal * (hit.depth + DELTA);
645 collision_tilemap(object, depth+1);
649 Sector::collision_object(MovingObject* object1, MovingObject* object2)
652 Rectangle dest1 = object1->get_bbox();
653 dest1.move(object1->get_movement());
654 Rectangle dest2 = object2->get_bbox();
655 dest2.move(object2->get_movement());
657 Vector movement = object1->get_movement() - object2->get_movement();
658 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
659 HitResponse response1 = object1->collision(*object2, hit);
661 HitResponse response2 = object2->collision(*object1, hit);
663 if(response1 != CONTINUE) {
664 if(response1 == ABORT_MOVE)
665 object1->movement = Vector(0, 0);
666 if(response2 == CONTINUE)
667 object2->movement += hit.normal * (hit.depth + DELTA);
668 } else if(response2 != CONTINUE) {
669 if(response2 == ABORT_MOVE)
670 object2->movement = Vector(0, 0);
671 if(response1 == CONTINUE)
672 object1->movement += -hit.normal * (hit.depth + DELTA);
674 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
675 object2->movement += hit.normal * (hit.depth/2 + DELTA);
681 Sector::collision_handler()
683 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
684 i != gameobjects.end(); ++i) {
685 GameObject* gameobject = *i;
686 if(!gameobject->is_valid())
688 MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
691 if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
692 movingobject->bbox.move(movingobject->movement);
693 movingobject->movement = Vector(0, 0);
697 // collision with tilemap
698 if(! (movingobject->movement == Vector(0, 0)))
699 collision_tilemap(movingobject, 0);
701 // collision with other objects
702 for(std::vector<GameObject*>::iterator i2 = i+1;
703 i2 != gameobjects.end(); ++i2) {
704 GameObject* other_object = *i2;
705 if(!other_object->is_valid()
706 || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
708 MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
712 collision_object(movingobject, movingobject2);
715 movingobject->bbox.move(movingobject->get_movement());
716 movingobject->movement = Vector(0, 0);
721 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
723 if(player->got_power == Player::FIRE_POWER) {
724 if(bullets.size() > MAX_FIRE_BULLETS-1)
726 } else if(player->got_power == Player::ICE_POWER) {
727 if(bullets.size() > MAX_ICE_BULLETS-1)
731 Bullet* new_bullet = 0;
732 if(player->got_power == Player::FIRE_POWER)
733 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
734 else if(player->got_power == Player::ICE_POWER)
735 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
737 throw std::runtime_error("wrong bullet type.");
738 add_object(new_bullet);
740 SoundManager::get()->play_sound(IDToSound(SND_SHOOT));
746 Sector::add_smoke_cloud(const Vector& pos)
748 add_object(new SmokeCloud(pos));
753 Sector::add_floating_text(const Vector& pos, const std::string& text)
755 add_object(new FloatingText(pos, text));
764 level_song = SoundManager::get()->load_music(datadir + "/music/" + song_title);
766 song_path = (char *) malloc(sizeof(char) * datadir.length() +
767 strlen(song_title.c_str()) + 8 + 5);
768 song_subtitle = strdup(song_title.c_str());
769 strcpy(strstr(song_subtitle, "."), "\0");
770 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
771 song_subtitle, strstr(song_title.c_str(), "."));
772 if(!SoundManager::get()->exists_music(song_path)) {
773 level_song_fast = level_song;
775 level_song_fast = SoundManager::get()->load_music(song_path);
782 Sector::play_music(int type)
785 switch(currentmusic) {
787 SoundManager::get()->play_music(level_song_fast);
790 SoundManager::get()->play_music(level_song);
793 SoundManager::get()->play_music(herring_song);
796 SoundManager::get()->halt_music();
802 Sector::get_music_type()
808 Sector::get_total_badguys()
810 int total_badguys = 0;
812 for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i)
814 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
819 return total_badguys;
823 Sector::inside(const Rectangle& 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)