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), background(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 background = new Background(reader);
98 } else if(name == "camera") {
99 Camera* camera = new Camera(this);
100 camera->parse(reader);
102 } else if(name == "tilemap") {
103 return new TileMap(reader);
104 } else if(name == "particles-snow") {
105 SnowParticleSystem* partsys = new SnowParticleSystem();
106 partsys->parse(reader);
108 } else if(name == "particles-clouds") {
109 CloudParticleSystem* partsys = new CloudParticleSystem();
110 partsys->parse(reader);
112 } else if(name == "door") {
113 return new Door(reader);
114 } else if(name == "secretarea") {
115 return new SecretAreaTrigger(reader);
116 } else if(name == "platform") {
117 return new Platform(reader);
118 } else if(name == "jumpy" || name == "money") {
119 return new Jumpy(reader);
120 } else if(name == "snowball") {
121 return new SnowBall(reader);
122 } else if(name == "bouncingsnowball") {
123 return new BouncingSnowball(reader);
124 } else if(name == "flame") {
125 return new Flame(reader);
126 } else if(name == "mriceblock") {
127 return new MrIceBlock(reader);
128 } else if(name == "mrbomb") {
129 return new MrBomb(reader);
130 } else if(name == "dispenser") {
131 return new Dispenser(reader);
132 } else if(name == "spike") {
133 return new Spike(reader);
134 } else if(name == "nolok_01") {
135 return new Nolok_01(reader);
138 std::cerr << "Unknown object type '" << name << "'.\n";
143 Sector::parse(LispReader& lispreader)
147 for(lisp_object_t* cur = lispreader.get_lisp(); !lisp_nil_p(cur);
148 cur = lisp_cdr(cur)) {
149 std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
150 // FIXME: doesn't handle empty data
151 lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
152 LispReader reader(lisp_cdr(lisp_car(cur)));
154 if(token == "name") {
155 name = lisp_string(data);
156 } else if(token == "gravity") {
157 gravity = lisp_real(data);
158 } else if(token == "music") {
159 song_title = lisp_string(data);
161 } else if(token == "spawn-points") {
162 SpawnPoint* sp = new SpawnPoint;
163 reader.read_string("name", sp->name);
164 reader.read_float("x", sp->pos.x);
165 reader.read_float("y", sp->pos.y);
166 spawnpoints.push_back(sp);
168 GameObject* object = parse_object(token, reader);
175 update_game_objects();
177 update_game_objects();
179 std::cerr << "sector '" << name << "' does not contain a camera.\n";
180 camera = new Camera(this);
184 throw std::runtime_error("sector does not contain a solid tile layer.");
188 Sector::parse_old_format(LispReader& reader)
193 reader.read_float("gravity", gravity);
195 std::string backgroundimage;
196 reader.read_string("background", backgroundimage);
198 reader.read_float("bkgd_speed", bgspeed);
201 Color bkgd_top, bkgd_bottom;
202 int r = 0, g = 0, b = 128;
203 reader.read_int("bkgd_red_top", r);
204 reader.read_int("bkgd_green_top", g);
205 reader.read_int("bkgd_blue_top", b);
210 reader.read_int("bkgd_red_bottom", r);
211 reader.read_int("bkgd_green_bottom", g);
212 reader.read_int("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.read_string("particle_system", particlesystem);
229 if(particlesystem == "clouds")
230 add_object(new CloudParticleSystem());
231 else if(particlesystem == "snow")
232 add_object(new SnowParticleSystem());
234 Vector startpos(100, 170);
235 reader.read_float("start_pos_x", startpos.x);
236 reader.read_float("start_pos_y", startpos.y);
238 SpawnPoint* spawn = new SpawnPoint;
239 spawn->pos = startpos;
240 spawn->name = "main";
241 spawnpoints.push_back(spawn);
243 song_title = "Mortimers_chipdisko.mod";
244 reader.read_string("music", song_title);
247 int width, height = 15;
248 reader.read_int("width", width);
249 reader.read_int("height", height);
251 std::vector<unsigned int> tiles;
252 if(reader.read_int_vector("interactive-tm", tiles)
253 || reader.read_int_vector("tilemap", tiles)) {
254 TileMap* tilemap = new TileMap();
255 tilemap->set(width, height, tiles, LAYER_TILES, true);
259 if(reader.read_int_vector("background-tm", tiles)) {
260 TileMap* tilemap = new TileMap();
261 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
265 if(reader.read_int_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)
273 lisp_object_t* cur = 0;
274 if(reader.read_lisp("reset-points", cur)) {
275 while(!lisp_nil_p(cur)) {
276 lisp_object_t* data = lisp_car(cur);
277 LispReader reader(lisp_cdr(data));
280 if(reader.read_float("x", sp_pos.x) && reader.read_float("y", sp_pos.y))
282 SpawnPoint* sp = new SpawnPoint;
285 spawnpoints.push_back(sp);
295 lisp_object_t* cur = 0;
296 if(reader.read_lisp("objects", cur)) {
297 while(!lisp_nil_p(cur)) {
298 lisp_object_t* data = lisp_car(cur);
299 std::string object_type = lisp_symbol(lisp_car(data));
301 LispReader reader(lisp_cdr(data));
303 GameObject* object = parse_object(object_type, reader);
307 std::cerr << "Unknown object '" << object_type << "' in level.\n";
316 Camera* camera = new Camera(this);
319 update_game_objects();
321 update_game_objects();
323 throw std::runtime_error("sector does not contain a solid tile layer.");
327 Sector::fix_old_tiles()
330 for(size_t x=0; x < solids->get_width(); ++x) {
331 for(size_t y=0; y < solids->get_height(); ++y) {
332 const Tile* tile = solids->get_tile(x, y);
333 Vector pos(x*32, y*32);
335 if(tile->id == 112) {
336 add_object(new InvisibleBlock(pos));
337 solids->change(x, y, 0);
338 } else if(tile->id == 295) {
339 add_object(new Spike(pos, Spike::NORTH));
340 solids->change(x, y, 0);
341 } else if(tile->id == 296) {
342 add_object(new Spike(pos, Spike::EAST));
343 solids->change(x, y, 0);
344 } else if(tile->id == 297) {
345 add_object(new Spike(pos, Spike::SOUTH));
346 solids->change(x, y, 0);
347 } else if(tile->id == 298) {
348 add_object(new Spike(pos, Spike::WEST));
349 solids->change(x, y, 0);
350 } else if(tile->attributes & Tile::COIN) {
351 add_object(new Coin(pos));
352 solids->change(x, y, 0);
353 } else if(tile->attributes & Tile::FULLBOX) {
354 add_object(new BonusBlock(pos, tile->data));
355 solids->change(x, y, 0);
356 } else if(tile->attributes & Tile::BRICK) {
357 add_object(new Brick(pos, tile->data));
358 solids->change(x, y, 0);
359 } else if(tile->attributes & Tile::GOAL) {
360 add_object(new SequenceTrigger(pos, "endsequence"));
361 solids->change(x, y, 0);
368 Sector::write(LispWriter& writer)
370 writer.write_string("name", name);
371 writer.write_float("gravity", gravity);
372 writer.write_string("music", song_title);
375 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
377 SpawnPoint* spawn = *i;
378 writer.start_list("spawn-points");
379 writer.write_string("name", spawn->name);
380 writer.write_float("x", spawn->pos.x);
381 writer.write_float("y", spawn->pos.y);
382 writer.end_list("spawn-points");
386 for(GameObjects::iterator i = gameobjects.begin();
387 i != gameobjects.end(); ++i) {
388 Serializable* serializable = dynamic_cast<Serializable*> (*i);
390 serializable->write(writer);
395 Sector::add_object(GameObject* object)
397 // make sure the object isn't already in the list
399 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
402 assert("object already added to sector" == 0);
405 for(GameObjects::iterator i = gameobjects_new.begin();
406 i != gameobjects_new.end(); ++i) {
408 assert("object already added to sector" == 0);
413 gameobjects_new.push_back(object);
417 Sector::activate(const std::string& spawnpoint)
421 // Apply bonuses from former levels
422 switch (player_status.bonus)
424 case PlayerStatus::NO_BONUS:
427 case PlayerStatus::FLOWER_BONUS:
428 player->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
431 case PlayerStatus::GROWUP_BONUS:
437 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
439 if((*i)->name == spawnpoint) {
445 std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n";
447 player->move(sp->pos);
450 camera->reset(player->get_pos());
454 Sector::get_best_spawn_point(Vector pos)
456 Vector best_reset_point = Vector(-1,-1);
458 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
460 if((*i)->name != "main")
462 if((*i)->pos.x > best_reset_point.x && (*i)->pos.x < pos.x)
463 best_reset_point = (*i)->pos;
466 return best_reset_point;
470 Sector::action(float elapsed_time)
472 player->check_bounds(camera);
475 for(GameObjects::iterator i = gameobjects.begin();
476 i != gameobjects.end(); ++i) {
477 GameObject* object = *i;
478 if(!object->is_valid())
481 object->action(elapsed_time);
484 /* Handle all possible collisions. */
486 update_game_objects();
490 Sector::update_game_objects()
492 /** cleanup marked objects */
493 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
494 i != gameobjects.end(); /* nothing */) {
495 if((*i)->is_valid() == false) {
496 Bullet* bullet = dynamic_cast<Bullet*> (*i);
499 std::remove(bullets.begin(), bullets.end(), bullet),
503 i = gameobjects.erase(i);
509 /* add newly created objects */
510 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
511 i != gameobjects_new.end(); ++i)
513 Bullet* bullet = dynamic_cast<Bullet*> (*i);
515 bullets.push_back(bullet);
517 TileMap* tilemap = dynamic_cast<TileMap*> (*i);
518 if(tilemap && tilemap->is_solid()) {
522 std::cerr << "Another solid tilemaps added. Ignoring.";
526 Camera* camera = dynamic_cast<Camera*> (*i);
528 if(this->camera != 0) {
529 std::cerr << "Warning: Multiple cameras added. Ignoring.";
532 this->camera = camera;
535 gameobjects.push_back(*i);
537 gameobjects_new.clear();
541 Sector::draw(DrawingContext& context)
543 context.push_transform();
544 context.set_translation(camera->get_translation());
546 for(GameObjects::iterator i = gameobjects.begin();
547 i != gameobjects.end(); ++i) {
548 GameObject* object = *i;
549 if(!object->is_valid())
552 object->draw(context);
555 context.pop_transform();
558 static const float DELTA = .001;
561 Sector::collision_tilemap(MovingObject* object, int depth)
565 std::cout << "Max collision depth reached.\n";
567 object->movement = Vector(0, 0);
571 // calculate rectangle where the object will move
573 if(object->get_movement().x >= 0) {
574 x1 = object->get_pos().x;
575 x2 = object->get_bbox().p2.x + object->get_movement().x;
577 x1 = object->get_pos().x + object->get_movement().x;
578 x2 = object->get_bbox().p2.x;
581 if(object->get_movement().y >= 0) {
582 y1 = object->get_pos().y;
583 y2 = object->get_bbox().p2.y + object->get_movement().y;
585 y1 = object->get_pos().y + object->get_movement().y;
586 y2 = object->get_bbox().p2.y;
589 // test with all tiles in this rectangle
590 int starttilex = int(x1-1) / 32;
591 int starttiley = int(y1-1) / 32;
592 int max_x = int(x2+1);
593 int max_y = int(y2+1);
595 CollisionHit temphit, hit;
596 Rectangle dest = object->get_bbox();
597 dest.move(object->movement);
598 hit.time = -1; // represents an invalid value
599 for(int x = starttilex; x*32 < max_x; ++x) {
600 for(int y = starttiley; y*32 < max_y; ++y) {
601 const Tile* tile = solids->get_tile(x, y);
604 if(!(tile->attributes & Tile::SOLID))
606 if((tile->attributes & Tile::UNISOLID) && object->movement.y < 0)
609 if(tile->attributes & Tile::SLOPE) { // slope tile
611 Vector p1(x*32, y*32);
612 Vector p2((x+1)*32, (y+1)*32);
613 triangle = AATriangle(p1, p2, tile->data);
615 if(Collision::rectangle_aatriangle(temphit, dest, object->movement,
617 if(temphit.time > hit.time)
620 } else { // normal rectangular tile
621 Rectangle rect(x*32, y*32, (x+1)*32, (y+1)*32);
622 if(Collision::rectangle_rectangle(temphit, dest,
623 object->movement, rect)) {
624 if(temphit.time > hit.time)
631 // did we collide at all?
635 // call collision function
636 HitResponse response = object->collision(*solids, hit);
637 if(response == ABORT_MOVE) {
638 object->movement = Vector(0, 0);
641 if(response == FORCE_MOVE) {
644 // move out of collision and try again
645 object->movement += hit.normal * (hit.depth + DELTA);
646 collision_tilemap(object, depth+1);
650 Sector::collision_object(MovingObject* object1, MovingObject* object2)
653 Rectangle dest1 = object1->get_bbox();
654 dest1.move(object1->get_movement());
655 Rectangle dest2 = object2->get_bbox();
656 dest2.move(object2->get_movement());
658 Vector movement = object1->get_movement() - object2->get_movement();
659 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
660 HitResponse response1 = object1->collision(*object2, hit);
662 HitResponse response2 = object2->collision(*object1, hit);
664 if(response1 != CONTINUE) {
665 if(response1 == ABORT_MOVE)
666 object1->movement = Vector(0, 0);
667 if(response2 == CONTINUE)
668 object2->movement += hit.normal * (hit.depth + DELTA);
669 } else if(response2 != CONTINUE) {
670 if(response2 == ABORT_MOVE)
671 object2->movement = Vector(0, 0);
672 if(response1 == CONTINUE)
673 object1->movement += -hit.normal * (hit.depth + DELTA);
675 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
676 object2->movement += hit.normal * (hit.depth/2 + DELTA);
682 Sector::collision_handler()
684 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
685 i != gameobjects.end(); ++i) {
686 GameObject* gameobject = *i;
687 if(!gameobject->is_valid())
689 MovingObject* movingobject = dynamic_cast<MovingObject*> (gameobject);
692 if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) {
693 movingobject->bbox.move(movingobject->movement);
694 movingobject->movement = Vector(0, 0);
698 // collision with tilemap
699 if(! (movingobject->movement == Vector(0, 0)))
700 collision_tilemap(movingobject, 0);
702 // collision with other objects
703 for(std::vector<GameObject*>::iterator i2 = i+1;
704 i2 != gameobjects.end(); ++i2) {
705 GameObject* other_object = *i2;
706 if(!other_object->is_valid()
707 || other_object->get_flags() & GameObject::FLAG_NO_COLLDET)
709 MovingObject* movingobject2 = dynamic_cast<MovingObject*> (other_object);
713 collision_object(movingobject, movingobject2);
716 movingobject->bbox.move(movingobject->get_movement());
717 movingobject->movement = Vector(0, 0);
722 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
724 if(player->got_power == Player::FIRE_POWER) {
725 if(bullets.size() > MAX_FIRE_BULLETS-1)
727 } else if(player->got_power == Player::ICE_POWER) {
728 if(bullets.size() > MAX_ICE_BULLETS-1)
732 Bullet* new_bullet = 0;
733 if(player->got_power == Player::FIRE_POWER)
734 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
735 else if(player->got_power == Player::ICE_POWER)
736 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
738 throw std::runtime_error("wrong bullet type.");
739 add_object(new_bullet);
741 SoundManager::get()->play_sound(IDToSound(SND_SHOOT));
747 Sector::add_smoke_cloud(const Vector& pos)
749 add_object(new SmokeCloud(pos));
754 Sector::add_floating_text(const Vector& pos, const std::string& text)
756 add_object(new FloatingText(pos, text));
765 level_song = SoundManager::get()->load_music(datadir + "/music/" + song_title);
767 song_path = (char *) malloc(sizeof(char) * datadir.length() +
768 strlen(song_title.c_str()) + 8 + 5);
769 song_subtitle = strdup(song_title.c_str());
770 strcpy(strstr(song_subtitle, "."), "\0");
771 sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(),
772 song_subtitle, strstr(song_title.c_str(), "."));
773 if(!SoundManager::get()->exists_music(song_path)) {
774 level_song_fast = level_song;
776 level_song_fast = SoundManager::get()->load_music(song_path);
783 Sector::play_music(int type)
786 switch(currentmusic) {
788 SoundManager::get()->play_music(level_song_fast);
791 SoundManager::get()->play_music(level_song);
794 SoundManager::get()->play_music(herring_song);
797 SoundManager::get()->halt_music();
803 Sector::get_music_type()
809 Sector::get_total_badguys()
811 int total_badguys = 0;
813 for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i)
815 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
820 return total_badguys;
824 Sector::inside(const Rectangle& rect) const
826 if(rect.p1.x > solids->get_width() * 32
827 || rect.p1.y > solids->get_height() * 32
828 || rect.p2.x < 0 || rect.p2.y < 0)