3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2006 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.
33 #include "object/player.hpp"
34 #include "object/gameobjs.hpp"
35 #include "object/camera.hpp"
36 #include "object/background.hpp"
37 #include "object/gradient.hpp"
38 #include "object/particlesystem.hpp"
39 #include "object/particlesystem_interactive.hpp"
40 #include "object/tilemap.hpp"
41 #include "lisp/parser.hpp"
42 #include "lisp/lisp.hpp"
43 #include "lisp/writer.hpp"
44 #include "lisp/list_iterator.hpp"
46 #include "audio/sound_manager.hpp"
47 #include "game_session.hpp"
48 #include "resources.hpp"
49 #include "statistics.hpp"
50 #include "object_factory.hpp"
51 #include "collision.hpp"
52 #include "spawn_point.hpp"
53 #include "math/rect.hpp"
54 #include "math/aatriangle.hpp"
55 #include "object/coin.hpp"
56 #include "object/block.hpp"
57 #include "object/invisible_block.hpp"
58 #include "object/light.hpp"
59 #include "object/bullet.hpp"
60 #include "object/text_object.hpp"
61 #include "object/portable.hpp"
62 #include "badguy/jumpy.hpp"
63 #include "trigger/sequence_trigger.hpp"
64 #include "player_status.hpp"
65 #include "scripting/squirrel_util.hpp"
66 #include "script_interface.hpp"
69 Sector* Sector::_current = 0;
71 bool Sector::show_collrects = false;
72 bool Sector::draw_solids_only = false;
74 Sector::Sector(Level* parent)
75 : level(parent), currentmusic(LEVEL_MUSIC),
76 ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), gravity(10), player(0), camera(0)
78 add_object(new Player(player_status, "Tux"));
79 add_object(new DisplayEffect("Effect"));
80 add_object(new TextObject("Text"));
82 // create a new squirrel table for the sector
83 using namespace Scripting;
85 sq_collectgarbage(global_vm);
87 sq_newtable(global_vm);
88 sq_pushroottable(global_vm);
89 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
90 throw Scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate");
92 sq_resetobject(§or_table);
93 if(SQ_FAILED(sq_getstackobj(global_vm, -1, §or_table)))
94 throw Scripting::SquirrelError(global_vm, "Couldn't get sector table");
95 sq_addref(global_vm, §or_table);
101 using namespace Scripting;
105 for(ScriptList::iterator i = scripts.begin();
106 i != scripts.end(); ++i) {
107 HSQOBJECT& object = *i;
108 sq_release(global_vm, &object);
110 sq_release(global_vm, §or_table);
111 sq_collectgarbage(global_vm);
113 update_game_objects();
114 assert(gameobjects_new.size() == 0);
116 for(GameObjects::iterator i = gameobjects.begin();
117 i != gameobjects.end(); ++i) {
118 GameObject* object = *i;
119 before_object_remove(object);
123 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
135 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
137 if(name == "camera") {
138 Camera* camera = new Camera(this, "Camera");
139 camera->parse(reader);
141 } else if(name == "particles-snow") {
142 SnowParticleSystem* partsys = new SnowParticleSystem();
143 partsys->parse(reader);
145 } else if(name == "particles-rain") {
146 RainParticleSystem* partsys = new RainParticleSystem();
147 partsys->parse(reader);
149 } else if(name == "particles-comets") {
150 CometParticleSystem* partsys = new CometParticleSystem();
151 partsys->parse(reader);
153 } else if(name == "particles-ghosts") {
154 GhostParticleSystem* partsys = new GhostParticleSystem();
155 partsys->parse(reader);
157 } else if(name == "particles-clouds") {
158 CloudParticleSystem* partsys = new CloudParticleSystem();
159 partsys->parse(reader);
161 } else if(name == "money") { // for compatibility with old maps
162 return new Jumpy(reader);
166 return create_object(name, reader);
167 } catch(std::exception& e) {
168 log_warning << e.what() << "" << std::endl;
175 Sector::parse(const lisp::Lisp& sector)
177 lisp::ListIterator iter(§or);
179 const std::string& token = iter.item();
180 if(token == "name") {
181 iter.value()->get(name);
182 } else if(token == "gravity") {
183 iter.value()->get(gravity);
184 } else if(token == "music") {
185 iter.value()->get(music);
186 } else if(token == "spawnpoint") {
187 SpawnPoint* sp = new SpawnPoint(iter.lisp());
188 spawnpoints.push_back(sp);
189 } else if(token == "init-script") {
190 iter.value()->get(init_script);
191 } else if(token == "ambient-light") {
192 std::vector<float> vColor;
193 sector.get_vector( "ambient-light", vColor );
194 if(vColor.size() < 3) {
195 log_warning << "(ambient-light) requires a color as argument" << std::endl;
197 ambient_light = Color( vColor );
200 GameObject* object = parse_object(token, *(iter.lisp()));
207 update_game_objects();
209 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
213 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
214 update_game_objects();
215 add_object(new Camera(this, "Camera"));
218 update_game_objects();
222 Sector::parse_old_format(const lisp::Lisp& reader)
225 reader.get("gravity", gravity);
227 std::string backgroundimage;
228 reader.get("background", backgroundimage);
230 reader.get("bkgd_speed", bgspeed);
233 Color bkgd_top, bkgd_bottom;
234 int r = 0, g = 0, b = 128;
235 reader.get("bkgd_red_top", r);
236 reader.get("bkgd_green_top", g);
237 reader.get("bkgd_blue_top", b);
238 bkgd_top.red = static_cast<float> (r) / 255.0f;
239 bkgd_top.green = static_cast<float> (g) / 255.0f;
240 bkgd_top.blue = static_cast<float> (b) / 255.0f;
242 reader.get("bkgd_red_bottom", r);
243 reader.get("bkgd_green_bottom", g);
244 reader.get("bkgd_blue_bottom", b);
245 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
246 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
247 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
249 if(backgroundimage != "") {
250 Background* background = new Background();
251 background->set_image(
252 std::string("images/background/") + backgroundimage, bgspeed);
253 add_object(background);
255 Gradient* gradient = new Gradient();
256 gradient->set_gradient(bkgd_top, bkgd_bottom);
257 add_object(gradient);
260 std::string particlesystem;
261 reader.get("particle_system", particlesystem);
262 if(particlesystem == "clouds")
263 add_object(new CloudParticleSystem());
264 else if(particlesystem == "snow")
265 add_object(new SnowParticleSystem());
266 else if(particlesystem == "rain")
267 add_object(new RainParticleSystem());
269 Vector startpos(100, 170);
270 reader.get("start_pos_x", startpos.x);
271 reader.get("start_pos_y", startpos.y);
273 SpawnPoint* spawn = new SpawnPoint;
274 spawn->pos = startpos;
275 spawn->name = "main";
276 spawnpoints.push_back(spawn);
278 music = "chipdisko.ogg";
279 reader.get("music", music);
280 music = "music/" + music;
282 int width = 30, height = 15;
283 reader.get("width", width);
284 reader.get("height", height);
286 std::vector<unsigned int> tiles;
287 if(reader.get_vector("interactive-tm", tiles)
288 || reader.get_vector("tilemap", tiles)) {
289 TileMap* tilemap = new TileMap();
290 tilemap->set(width, height, tiles, LAYER_TILES, true);
294 if(reader.get_vector("background-tm", tiles)) {
295 TileMap* tilemap = new TileMap();
296 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
300 if(reader.get_vector("foreground-tm", tiles)) {
301 TileMap* tilemap = new TileMap();
302 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
306 // read reset-points (now spawn-points)
307 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
309 lisp::ListIterator iter(resetpoints);
311 if(iter.item() == "point") {
313 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
315 SpawnPoint* sp = new SpawnPoint;
318 spawnpoints.push_back(sp);
321 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
327 const lisp::Lisp* objects = reader.get_lisp("objects");
329 lisp::ListIterator iter(objects);
331 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
335 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
341 Camera* camera = new Camera(this, "Camera");
344 update_game_objects();
346 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
349 update_game_objects();
353 Sector::fix_old_tiles()
355 for(std::list<TileMap*>::iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
356 TileMap* solids = *i;
357 for(size_t x=0; x < solids->get_width(); ++x) {
358 for(size_t y=0; y < solids->get_height(); ++y) {
359 const Tile* tile = solids->get_tile(x, y);
360 Vector pos(solids->get_x_offset() + x*32, solids->get_y_offset() + y*32);
362 if(tile->getID() == 112) {
363 add_object(new InvisibleBlock(pos));
364 solids->change(x, y, 0);
365 } else if(tile->getAttributes() & Tile::COIN) {
366 add_object(new Coin(pos));
367 solids->change(x, y, 0);
368 } else if(tile->getAttributes() & Tile::FULLBOX) {
369 add_object(new BonusBlock(pos, tile->getData()));
370 solids->change(x, y, 0);
371 } else if(tile->getAttributes() & Tile::BRICK) {
372 add_object(new Brick(pos, tile->getData()));
373 solids->change(x, y, 0);
374 } else if(tile->getAttributes() & Tile::GOAL) {
375 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
376 add_object(new SequenceTrigger(pos, sequence));
377 solids->change(x, y, 0);
383 // add lights for special tiles
384 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); i++) {
385 TileMap* tm = dynamic_cast<TileMap*>(*i);
387 for(size_t x=0; x < tm->get_width(); ++x) {
388 for(size_t y=0; y < tm->get_height(); ++y) {
389 const Tile* tile = tm->get_tile(x, y);
390 Vector pos(tm->get_x_offset() + x*32, tm->get_y_offset() + y*32);
391 Vector center(pos.x + 16, pos.y + 16);
394 if (tile->getID() == 1517) {
395 add_object(new Light(center, Color(1.0, 0.6, 0.3, 1.0)));
398 if ((tile->getID() == 173) || (tile->getID() == 1700) || (tile->getID() == 1705) || (tile->getID() == 1706)) {
399 // space lights a bit
400 if (((tm->get_tile(x-1, y)->getID() != tm->get_tile(x,y)->getID())
401 && (tm->get_tile(x, y-1)->getID() != tm->get_tile(x,y)->getID()))
402 || ((x % 3 == 0) && (y % 3 == 0))) {
403 add_object(new Light(center, Color(1.0, 0.6, 0.6, 1.0)));
415 Sector::write(lisp::Writer& writer)
417 writer.write_string("name", name);
418 writer.write_float("gravity", gravity);
419 writer.write_string("music", music);
422 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
424 SpawnPoint* spawn = *i;
425 writer.start_list("spawn-points");
426 writer.write_string("name", spawn->name);
427 writer.write_float("x", spawn->pos.x);
428 writer.write_float("y", spawn->pos.y);
429 writer.end_list("spawn-points");
433 for(GameObjects::iterator i = gameobjects.begin();
434 i != gameobjects.end(); ++i) {
435 Serializable* serializable = dynamic_cast<Serializable*> (*i);
437 serializable->write(writer);
442 Sector::run_script(std::istream& in, const std::string& sourcename)
444 using namespace Scripting;
446 // garbage collect thread list
447 for(ScriptList::iterator i = scripts.begin();
448 i != scripts.end(); ) {
449 HSQOBJECT& object = *i;
450 HSQUIRRELVM vm = object_to_vm(object);
452 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
453 sq_release(global_vm, &object);
454 i = scripts.erase(i);
461 HSQOBJECT object = create_thread(global_vm);
462 scripts.push_back(object);
464 HSQUIRRELVM vm = object_to_vm(object);
466 // set sector_table as roottable for the thread
467 sq_pushobject(vm, sector_table);
470 compile_and_run(vm, in, sourcename);
476 Sector::add_object(GameObject* object)
478 // make sure the object isn't already in the list
480 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
483 assert("object already added to sector" == 0);
486 for(GameObjects::iterator i = gameobjects_new.begin();
487 i != gameobjects_new.end(); ++i) {
489 assert("object already added to sector" == 0);
495 gameobjects_new.push_back(object);
499 Sector::activate(const std::string& spawnpoint)
502 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
504 if((*i)->name == spawnpoint) {
510 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
511 if(spawnpoint != "main") {
514 activate(Vector(0, 0));
522 Sector::activate(const Vector& player_pos)
524 if(_current != this) {
526 _current->deactivate();
529 // register sectortable as sector in scripting
530 HSQUIRRELVM vm = Scripting::global_vm;
531 sq_pushroottable(vm);
532 sq_pushstring(vm, "sector", -1);
533 sq_pushobject(vm, sector_table);
534 if(SQ_FAILED(sq_createslot(vm, -3)))
535 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
538 for(GameObjects::iterator i = gameobjects.begin();
539 i != gameobjects.end(); ++i) {
540 GameObject* object = *i;
547 // spawn smalltux below spawnpoint
548 if (!player->is_big()) {
549 player->move(player_pos + Vector(0,32));
551 player->move(player_pos);
554 // spawning tux in the ground would kill him
555 if(!is_free_of_tiles(player->get_bbox())) {
556 log_warning << "Tried spawning Tux in solid matter. Compensating." << std::endl;
557 Vector npos = player->get_bbox().p1;
562 camera->reset(player->get_pos());
563 update_game_objects();
566 if(init_script != "") {
567 std::istringstream in(init_script);
568 run_script(in, std::string("Sector(") + name + ") - init");
578 // remove sector entry from global vm
579 HSQUIRRELVM vm = Scripting::global_vm;
580 sq_pushroottable(vm);
581 sq_pushstring(vm, "sector", -1);
582 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
583 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
586 for(GameObjects::iterator i = gameobjects.begin();
587 i != gameobjects.end(); ++i) {
588 GameObject* object = *i;
590 try_unexpose(object);
598 Sector::get_active_region()
601 camera->get_translation() - Vector(1600, 1200),
602 camera->get_translation() + Vector(1600, 1200));
606 Sector::update(float elapsed_time)
608 player->check_bounds(camera);
611 for(GameObjects::iterator i = gameobjects.begin();
612 i != gameobjects.end(); ++i) {
613 GameObject* object = *i;
614 if(!object->is_valid())
617 object->update(elapsed_time);
620 /* Handle all possible collisions. */
622 update_game_objects();
626 Sector::update_game_objects()
628 /** cleanup marked objects */
629 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
630 i != gameobjects.end(); /* nothing */) {
631 GameObject* object = *i;
633 if(object->is_valid()) {
638 before_object_remove(object);
641 i = gameobjects.erase(i);
644 /* add newly created objects */
645 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
646 i != gameobjects_new.end(); ++i)
648 GameObject* object = *i;
650 before_object_add(object);
652 gameobjects.push_back(object);
654 gameobjects_new.clear();
658 Sector::before_object_add(GameObject* object)
660 Bullet* bullet = dynamic_cast<Bullet*> (object);
662 bullets.push_back(bullet);
665 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
666 if(movingobject != NULL) {
667 moving_objects.push_back(movingobject);
670 Portable* portable = dynamic_cast<Portable*> (object);
671 if(portable != NULL) {
672 portables.push_back(portable);
675 TileMap* tilemap = dynamic_cast<TileMap*> (object);
676 if(tilemap != NULL && tilemap->is_solid()) {
677 solid_tilemaps.push_back(tilemap);
680 Camera* camera = dynamic_cast<Camera*> (object);
682 if(this->camera != 0) {
683 log_warning << "Multiple cameras added. Ignoring" << std::endl;
686 this->camera = camera;
689 Player* player = dynamic_cast<Player*> (object);
691 if(this->player != 0) {
692 log_warning << "Multiple players added. Ignoring" << std::endl;
695 this->player = player;
698 if(_current == this) {
706 Sector::try_expose(GameObject* object)
708 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
709 if(interface != NULL) {
710 HSQUIRRELVM vm = Scripting::global_vm;
711 sq_pushobject(vm, sector_table);
712 interface->expose(vm, -1);
718 Sector::try_expose_me()
720 HSQUIRRELVM vm = Scripting::global_vm;
721 sq_pushobject(vm, sector_table);
722 Scripting::SSector* interface = static_cast<Scripting::SSector*> (this);
723 expose_object(vm, -1, interface, "settings", false);
728 Sector::before_object_remove(GameObject* object)
730 Portable* portable = dynamic_cast<Portable*> (object);
731 if(portable != NULL) {
732 portables.erase(std::find(portables.begin(), portables.end(), portable));
734 Bullet* bullet = dynamic_cast<Bullet*> (object);
736 bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));
738 MovingObject* moving_object = dynamic_cast<MovingObject*> (object);
739 if(moving_object != NULL) {
740 moving_objects.erase(
741 std::find(moving_objects.begin(), moving_objects.end(), moving_object));
745 try_unexpose(object);
749 Sector::try_unexpose(GameObject* object)
751 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
752 if(interface != NULL) {
753 HSQUIRRELVM vm = Scripting::global_vm;
754 SQInteger oldtop = sq_gettop(vm);
755 sq_pushobject(vm, sector_table);
757 interface->unexpose(vm, -1);
758 } catch(std::exception& e) {
759 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
761 sq_settop(vm, oldtop);
766 Sector::try_unexpose_me()
768 HSQUIRRELVM vm = Scripting::global_vm;
769 SQInteger oldtop = sq_gettop(vm);
770 sq_pushobject(vm, sector_table);
772 Scripting::unexpose_object(vm, -1, "settings");
773 } catch(std::exception& e) {
774 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
776 sq_settop(vm, oldtop);
779 Sector::draw(DrawingContext& context)
781 context.set_ambient_color( ambient_light );
782 context.push_transform();
783 context.set_translation(camera->get_translation());
785 for(GameObjects::iterator i = gameobjects.begin();
786 i != gameobjects.end(); ++i) {
787 GameObject* object = *i;
788 if(!object->is_valid())
791 if (draw_solids_only)
793 TileMap* tm = dynamic_cast<TileMap*>(object);
794 if (tm && !tm->is_solid())
798 object->draw(context);
802 Color col(0.2, 0.2, 0.2, 0.7);
803 for(MovingObjects::iterator i = moving_objects.begin();
804 i != moving_objects.end(); ++i) {
805 MovingObject* object = *i;
806 const Rect& rect = object->get_bbox();
808 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
812 context.pop_transform();
815 /*-------------------------------------------------------------------------
816 * Collision Detection
817 *-------------------------------------------------------------------------*/
819 static const float SHIFT_DELTA = 7.0f;
821 /** r1 is supposed to be moving, r2 a solid object */
822 void check_collisions(collision::Constraints* constraints,
823 const Vector& movement, const Rect& r1, const Rect& r2,
824 GameObject* object = NULL, MovingObject* other = NULL)
826 if(!collision::intersects(r1, r2))
829 // calculate intersection
830 float itop = r1.get_bottom() - r2.get_top();
831 float ibottom = r2.get_bottom() - r1.get_top();
832 float ileft = r1.get_right() - r2.get_left();
833 float iright = r2.get_right() - r1.get_left();
835 if(fabsf(movement.y) > fabsf(movement.x)) {
836 if(ileft < SHIFT_DELTA) {
837 constraints->right = std::min(constraints->right, r2.get_left());
839 } else if(iright < SHIFT_DELTA) {
840 constraints->left = std::max(constraints->left, r2.get_right());
844 // shiftout bottom/top
845 if(itop < SHIFT_DELTA) {
846 constraints->bottom = std::min(constraints->bottom, r2.get_top());
848 } else if(ibottom < SHIFT_DELTA) {
849 constraints->top = std::max(constraints->top, r2.get_bottom());
856 HitResponse response = other->collision(*object, dummy);
857 if(response == PASSTHROUGH)
859 if(other->get_movement() != Vector(0, 0)) {
860 // TODO what todo when we collide with 2 moving objects?!?
861 constraints->ground_movement = other->get_movement();
865 float vert_penetration = std::min(itop, ibottom);
866 float horiz_penetration = std::min(ileft, iright);
867 if(vert_penetration < horiz_penetration) {
869 constraints->bottom = std::min(constraints->bottom, r2.get_top());
870 constraints->hit.bottom = true;
872 constraints->top = std::max(constraints->top, r2.get_bottom());
873 constraints->hit.top = true;
877 constraints->right = std::min(constraints->right, r2.get_left());
878 constraints->hit.right = true;
880 constraints->left = std::max(constraints->left, r2.get_right());
881 constraints->hit.left = true;
886 static const float DELTA = .001;
889 Sector::collision_tilemap(collision::Constraints* constraints,
890 const Vector& movement, const Rect& dest) const
892 // calculate rectangle where the object will move
893 float x1 = dest.get_left();
894 float x2 = dest.get_right();
895 float y1 = dest.get_top();
896 float y2 = dest.get_bottom();
898 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
899 TileMap* solids = *i;
901 // test with all tiles in this rectangle
902 int starttilex = int(x1 - solids->get_x_offset()) / 32;
903 int starttiley = int(y1 - solids->get_y_offset()) / 32;
904 int max_x = int(x2 - solids->get_x_offset());
905 int max_y = int(y2+1 - solids->get_y_offset());
907 for(int x = starttilex; x*32 < max_x; ++x) {
908 for(int y = starttiley; y*32 < max_y; ++y) {
909 const Tile* tile = solids->get_tile(x, y);
912 // skip non-solid tiles
913 if((tile->getAttributes() & Tile::SOLID) == 0)
915 // only handle unisolid when the player is falling down and when he was
916 // above the tile before
917 if(tile->getAttributes() & Tile::UNISOLID) {
918 if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
922 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
924 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
925 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
926 triangle = AATriangle(p1, p2, tile->getData());
928 collision::rectangle_aatriangle(constraints, dest, triangle);
929 } else { // normal rectangular tile
930 Rect rect(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset(), (x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
931 check_collisions(constraints, movement, dest, rect);
939 Sector::collision_tile_attributes(const Rect& dest) const
941 float x1 = dest.p1.x;
942 float y1 = dest.p1.y;
943 float x2 = dest.p2.x;
944 float y2 = dest.p2.y;
947 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
948 TileMap* solids = *i;
950 // test with all tiles in this rectangle
951 int starttilex = int(x1 - solids->get_x_offset()) / 32;
952 int starttiley = int(y1 - solids->get_y_offset()) / 32;
953 int max_x = int(x2 - solids->get_x_offset());
954 int max_y = int(y2+1 - solids->get_y_offset());
956 for(int x = starttilex; x*32 < max_x; ++x) {
957 for(int y = starttiley; y*32 < max_y; ++y) {
958 const Tile* tile = solids->get_tile(x, y);
961 result |= tile->getAttributes();
969 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
970 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
973 float itop = r1.get_bottom() - r2.get_top();
974 float ibottom = r2.get_bottom() - r1.get_top();
975 float ileft = r1.get_right() - r2.get_left();
976 float iright = r2.get_right() - r1.get_left();
978 float vert_penetration = std::min(itop, ibottom);
979 float horiz_penetration = std::min(ileft, iright);
980 if(vert_penetration < horiz_penetration) {
983 normal.y = vert_penetration;
986 normal.y = -vert_penetration;
991 normal.x = horiz_penetration;
994 normal.x = -horiz_penetration;
1000 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
1002 using namespace collision;
1004 const Rect& r1 = object1->dest;
1005 const Rect& r2 = object2->dest;
1008 if(intersects(object1->dest, object2->dest)) {
1010 get_hit_normal(r1, r2, hit, normal);
1012 HitResponse response1 = object1->collision(*object2, hit);
1013 std::swap(hit.left, hit.right);
1014 std::swap(hit.top, hit.bottom);
1015 HitResponse response2 = object2->collision(*object1, hit);
1016 assert( response1 != SOLID && response1 != PASSTHROUGH );
1017 assert( response2 != SOLID && response2 != PASSTHROUGH );
1018 if(response1 == CONTINUE && response2 == CONTINUE) {
1019 normal *= (0.5 + DELTA);
1020 object1->dest.move(-normal);
1021 object2->dest.move(normal);
1022 } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
1023 normal *= (1 + DELTA);
1024 object1->dest.move(-normal);
1025 } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
1026 normal *= (1 + DELTA);
1027 object2->dest.move(normal);
1033 Sector::collision_static(collision::Constraints* constraints,
1034 const Vector& movement, const Rect& dest,
1037 collision_tilemap(constraints, movement, dest);
1039 // collision with other (static) objects
1040 for(MovingObjects::iterator i = moving_objects.begin();
1041 i != moving_objects.end(); ++i) {
1042 MovingObject* moving_object = *i;
1043 if(moving_object->get_group() != COLGROUP_STATIC
1044 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1046 if(!moving_object->is_valid())
1049 if(moving_object != &object)
1050 check_collisions(constraints, movement, dest, moving_object->bbox,
1051 &object, moving_object);
1056 Sector::collision_static_constrains(MovingObject& object)
1058 using namespace collision;
1059 float infinity = (std::numeric_limits<float>::has_infinity ? std::numeric_limits<float>::infinity() : std::numeric_limits<float>::max());
1061 Constraints constraints;
1062 Vector movement = object.get_movement();
1063 Rect& dest = object.dest;
1064 float owidth = object.get_bbox().get_width();
1065 float oheight = object.get_bbox().get_height();
1067 for(int i = 0; i < 2; ++i) {
1068 collision_static(&constraints, Vector(0, movement.y), dest, object);
1069 if(!constraints.has_constraints())
1072 // apply calculated horizontal constraints
1073 if(constraints.bottom < infinity) {
1074 float height = constraints.bottom - constraints.top;
1075 if(height < oheight) {
1076 // we're crushed, but ignore this for now, we'll get this again
1077 // later if we're really crushed or things will solve itself when
1078 // looking at the vertical constraints
1080 dest.p2.y = constraints.bottom - DELTA;
1081 dest.p1.y = dest.p2.y - oheight;
1082 } else if(constraints.top > -infinity) {
1083 dest.p1.y = constraints.top + DELTA;
1084 dest.p2.y = dest.p1.y + oheight;
1087 if(constraints.has_constraints()) {
1088 if(constraints.hit.bottom) {
1089 dest.move(constraints.ground_movement);
1091 if(constraints.hit.top || constraints.hit.bottom) {
1092 constraints.hit.left = false;
1093 constraints.hit.right = false;
1094 object.collision_solid(constraints.hit);
1098 constraints = Constraints();
1099 for(int i = 0; i < 2; ++i) {
1100 collision_static(&constraints, movement, dest, object);
1101 if(!constraints.has_constraints())
1104 // apply calculated vertical constraints
1105 if(constraints.right < infinity) {
1106 float width = constraints.right - constraints.left;
1107 if(width + SHIFT_DELTA < owidth) {
1108 printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1109 constraints.left, constraints.right);
1114 object.collision_solid(h);
1116 dest.p2.x = constraints.right - DELTA;
1117 dest.p1.x = dest.p2.x - owidth;
1119 } else if(constraints.left > -infinity) {
1120 dest.p1.x = constraints.left + DELTA;
1121 dest.p2.x = dest.p1.x + owidth;
1125 if(constraints.has_constraints()) {
1126 if( constraints.hit.left || constraints.hit.right
1127 || constraints.hit.top || constraints.hit.bottom
1128 || constraints.hit.crush )
1129 object.collision_solid(constraints.hit);
1132 // an extra pass to make sure we're not crushed horizontally
1133 constraints = Constraints();
1134 collision_static(&constraints, movement, dest, object);
1135 if(constraints.bottom < infinity) {
1136 float height = constraints.bottom - constraints.top;
1137 if(height + SHIFT_DELTA < oheight) {
1138 printf("Object %p crushed vertically...\n", &object);
1143 object.collision_solid(h);
1149 Sector::handle_collisions()
1151 using namespace collision;
1153 // calculate destination positions of the objects
1154 for(MovingObjects::iterator i = moving_objects.begin();
1155 i != moving_objects.end(); ++i) {
1156 MovingObject* moving_object = *i;
1158 moving_object->dest = moving_object->get_bbox();
1159 moving_object->dest.move(moving_object->get_movement());
1162 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1163 for(MovingObjects::iterator i = moving_objects.begin();
1164 i != moving_objects.end(); ++i) {
1165 MovingObject* moving_object = *i;
1166 if((moving_object->get_group() != COLGROUP_MOVING
1167 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1168 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1169 || !moving_object->is_valid())
1172 collision_static_constrains(*moving_object);
1176 // part2: COLGROUP_MOVING vs tile attributes
1177 for(MovingObjects::iterator i = moving_objects.begin();
1178 i != moving_objects.end(); ++i) {
1179 MovingObject* moving_object = *i;
1180 if((moving_object->get_group() != COLGROUP_MOVING
1181 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1182 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1183 || !moving_object->is_valid())
1186 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1187 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1188 moving_object->collision_tile(tile_attributes);
1192 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1193 for(MovingObjects::iterator i = moving_objects.begin();
1194 i != moving_objects.end(); ++i) {
1195 MovingObject* moving_object = *i;
1196 if((moving_object->get_group() != COLGROUP_MOVING
1197 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1198 || !moving_object->is_valid())
1201 for(MovingObjects::iterator i2 = moving_objects.begin();
1202 i2 != moving_objects.end(); ++i2) {
1203 MovingObject* moving_object_2 = *i2;
1204 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1205 || !moving_object_2->is_valid())
1208 if(intersects(moving_object->dest, moving_object_2->dest)) {
1211 get_hit_normal(moving_object->dest, moving_object_2->dest,
1213 moving_object->collision(*moving_object_2, hit);
1214 moving_object_2->collision(*moving_object, hit);
1219 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1220 for(MovingObjects::iterator i = moving_objects.begin();
1221 i != moving_objects.end(); ++i) {
1222 MovingObject* moving_object = *i;
1224 if((moving_object->get_group() != COLGROUP_MOVING
1225 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1226 || !moving_object->is_valid())
1229 for(MovingObjects::iterator i2 = i+1;
1230 i2 != moving_objects.end(); ++i2) {
1231 MovingObject* moving_object_2 = *i2;
1232 if((moving_object_2->get_group() != COLGROUP_MOVING
1233 && moving_object_2->get_group() != COLGROUP_MOVING_STATIC)
1234 || !moving_object_2->is_valid())
1237 collision_object(moving_object, moving_object_2);
1241 // apply object movement
1242 for(MovingObjects::iterator i = moving_objects.begin();
1243 i != moving_objects.end(); ++i) {
1244 MovingObject* moving_object = *i;
1246 moving_object->bbox = moving_object->dest;
1247 moving_object->movement = Vector(0, 0);
1252 Sector::is_free_of_tiles(const Rect& rect) const
1254 using namespace collision;
1256 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1257 TileMap* solids = *i;
1259 // test with all tiles in this rectangle
1260 int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32;
1261 int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32;
1262 int max_x = int(rect.p2.x - solids->get_x_offset());
1263 int max_y = int(rect.p2.y - solids->get_y_offset());
1265 for(int x = starttilex; x*32 <= max_x; ++x) {
1266 for(int y = starttiley; y*32 <= max_y; ++y) {
1267 const Tile* tile = solids->get_tile(x, y);
1269 if(tile->getAttributes() & Tile::SLOPE) {
1270 AATriangle triangle;
1271 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
1272 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
1273 triangle = AATriangle(p1, p2, tile->getData());
1274 Constraints constraints;
1275 return collision::rectangle_aatriangle(&constraints, rect, triangle);
1277 if(tile->getAttributes() & Tile::SOLID) return false;
1286 Sector::is_free_of_statics(const Rect& rect, const MovingObject* ignore_object) const
1288 using namespace collision;
1290 if (!is_free_of_tiles(rect)) return false;
1292 for(MovingObjects::const_iterator i = moving_objects.begin();
1293 i != moving_objects.end(); ++i) {
1294 const MovingObject* moving_object = *i;
1295 if (moving_object == ignore_object) continue;
1296 if (!moving_object->is_valid()) continue;
1297 if (moving_object->get_group() == COLGROUP_STATIC) {
1298 if(intersects(rect, moving_object->get_bbox())) return false;
1306 Sector::is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object) const
1308 using namespace collision;
1310 if (!is_free_of_tiles(rect)) return false;
1312 for(MovingObjects::const_iterator i = moving_objects.begin();
1313 i != moving_objects.end(); ++i) {
1314 const MovingObject* moving_object = *i;
1315 if (moving_object == ignore_object) continue;
1316 if (!moving_object->is_valid()) continue;
1317 if ((moving_object->get_group() == COLGROUP_MOVING)
1318 || (moving_object->get_group() == COLGROUP_MOVING_STATIC)
1319 || (moving_object->get_group() == COLGROUP_STATIC)) {
1320 if(intersects(rect, moving_object->get_bbox())) return false;
1328 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1330 // TODO remove this function and move these checks elsewhere...
1332 Bullet* new_bullet = 0;
1333 if((player_status->bonus == FIRE_BONUS &&
1334 (int)bullets.size() >= player_status->max_fire_bullets) ||
1335 (player_status->bonus == ICE_BONUS &&
1336 (int)bullets.size() >= player_status->max_ice_bullets))
1338 new_bullet = new Bullet(pos, xm, dir, player_status->bonus);
1339 add_object(new_bullet);
1341 sound_manager->play("sounds/shoot.wav");
1347 Sector::add_smoke_cloud(const Vector& pos)
1349 add_object(new SmokeCloud(pos));
1354 Sector::play_music(MusicType type)
1356 currentmusic = type;
1357 switch(currentmusic) {
1359 sound_manager->play_music(music);
1362 sound_manager->play_music("music/salcon.ogg");
1364 case HERRING_WARNING_MUSIC:
1365 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1368 sound_manager->play_music("");
1374 Sector::get_music_type()
1376 return currentmusic;
1380 Sector::get_total_badguys()
1382 int total_badguys = 0;
1383 for(GameObjects::iterator i = gameobjects.begin();
1384 i != gameobjects.end(); ++i) {
1385 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1386 if (badguy && badguy->countMe)
1390 return total_badguys;
1394 Sector::inside(const Rect& rect) const
1396 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1397 TileMap* solids = *i;
1398 bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
1399 bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
1400 if (horizontally && vertically) return true;
1406 Sector::get_width() const
1409 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1410 TileMap* solids = *i;
1411 if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset());
1417 Sector::get_height() const
1420 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1421 TileMap* solids = *i;
1422 if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset());
1428 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
1430 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1431 TileMap* solids = *i;
1432 solids->change_all(old_tile_id, new_tile_id);
1438 Sector::set_ambient_light(float red, float green, float blue)
1440 ambient_light.red = red;
1441 ambient_light.green = green;
1442 ambient_light.blue = blue;
1446 Sector::get_ambient_red()
1448 return ambient_light.red;
1452 Sector::get_ambient_green()
1454 return ambient_light.green;
1458 Sector::get_ambient_blue()
1460 return ambient_light.blue;