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/pulsing_light.hpp"
60 #include "object/bullet.hpp"
61 #include "object/text_object.hpp"
62 #include "object/portable.hpp"
63 #include "badguy/jumpy.hpp"
64 #include "trigger/sequence_trigger.hpp"
65 #include "player_status.hpp"
66 #include "scripting/squirrel_util.hpp"
67 #include "script_interface.hpp"
70 Sector* Sector::_current = 0;
72 bool Sector::show_collrects = false;
73 bool Sector::draw_solids_only = false;
75 Sector::Sector(Level* parent)
76 : level(parent), currentmusic(LEVEL_MUSIC),
77 ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), gravity(10.0), player(0), camera(0)
79 add_object(new Player(player_status, "Tux"));
80 add_object(new DisplayEffect("Effect"));
81 add_object(new TextObject("Text"));
83 // create a new squirrel table for the sector
84 using namespace Scripting;
86 sq_collectgarbage(global_vm);
88 sq_newtable(global_vm);
89 sq_pushroottable(global_vm);
90 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
91 throw Scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate");
93 sq_resetobject(§or_table);
94 if(SQ_FAILED(sq_getstackobj(global_vm, -1, §or_table)))
95 throw Scripting::SquirrelError(global_vm, "Couldn't get sector table");
96 sq_addref(global_vm, §or_table);
102 using namespace Scripting;
106 for(ScriptList::iterator i = scripts.begin();
107 i != scripts.end(); ++i) {
108 HSQOBJECT& object = *i;
109 sq_release(global_vm, &object);
111 sq_release(global_vm, §or_table);
112 sq_collectgarbage(global_vm);
114 update_game_objects();
115 assert(gameobjects_new.size() == 0);
117 for(GameObjects::iterator i = gameobjects.begin();
118 i != gameobjects.end(); ++i) {
119 GameObject* object = *i;
120 before_object_remove(object);
124 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
136 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
138 if(name == "camera") {
139 Camera* camera = new Camera(this, "Camera");
140 camera->parse(reader);
142 } else if(name == "particles-snow") {
143 SnowParticleSystem* partsys = new SnowParticleSystem();
144 partsys->parse(reader);
146 } else if(name == "particles-rain") {
147 RainParticleSystem* partsys = new RainParticleSystem();
148 partsys->parse(reader);
150 } else if(name == "particles-comets") {
151 CometParticleSystem* partsys = new CometParticleSystem();
152 partsys->parse(reader);
154 } else if(name == "particles-ghosts") {
155 GhostParticleSystem* partsys = new GhostParticleSystem();
156 partsys->parse(reader);
158 } else if(name == "particles-clouds") {
159 CloudParticleSystem* partsys = new CloudParticleSystem();
160 partsys->parse(reader);
162 } else if(name == "money") { // for compatibility with old maps
163 return new Jumpy(reader);
167 return create_object(name, reader);
168 } catch(std::exception& e) {
169 log_warning << e.what() << "" << std::endl;
176 Sector::parse(const lisp::Lisp& sector)
178 lisp::ListIterator iter(§or);
180 const std::string& token = iter.item();
181 if(token == "name") {
182 iter.value()->get(name);
183 } else if(token == "gravity") {
184 iter.value()->get(gravity);
185 } else if(token == "music") {
186 iter.value()->get(music);
187 } else if(token == "spawnpoint") {
188 SpawnPoint* sp = new SpawnPoint(iter.lisp());
189 spawnpoints.push_back(sp);
190 } else if(token == "init-script") {
191 iter.value()->get(init_script);
192 } else if(token == "ambient-light") {
193 std::vector<float> vColor;
194 sector.get_vector( "ambient-light", vColor );
195 if(vColor.size() < 3) {
196 log_warning << "(ambient-light) requires a color as argument" << std::endl;
198 ambient_light = Color( vColor );
201 GameObject* object = parse_object(token, *(iter.lisp()));
208 update_game_objects();
210 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
214 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
215 update_game_objects();
216 add_object(new Camera(this, "Camera"));
219 update_game_objects();
223 Sector::parse_old_format(const lisp::Lisp& reader)
226 reader.get("gravity", gravity);
228 std::string backgroundimage;
229 reader.get("background", backgroundimage);
230 if (backgroundimage == "arctis2.jpg") backgroundimage = "arctis.jpg";
232 reader.get("bkgd_speed", bgspeed);
235 Color bkgd_top, bkgd_bottom;
236 int r = 0, g = 0, b = 128;
237 reader.get("bkgd_red_top", r);
238 reader.get("bkgd_green_top", g);
239 reader.get("bkgd_blue_top", b);
240 bkgd_top.red = static_cast<float> (r) / 255.0f;
241 bkgd_top.green = static_cast<float> (g) / 255.0f;
242 bkgd_top.blue = static_cast<float> (b) / 255.0f;
244 reader.get("bkgd_red_bottom", r);
245 reader.get("bkgd_green_bottom", g);
246 reader.get("bkgd_blue_bottom", b);
247 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
248 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
249 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
251 if(backgroundimage != "") {
252 Background* background = new Background();
253 background->set_image(
254 std::string("images/background/") + backgroundimage, bgspeed);
255 add_object(background);
257 Gradient* gradient = new Gradient();
258 gradient->set_gradient(bkgd_top, bkgd_bottom);
259 add_object(gradient);
262 std::string particlesystem;
263 reader.get("particle_system", particlesystem);
264 if(particlesystem == "clouds")
265 add_object(new CloudParticleSystem());
266 else if(particlesystem == "snow")
267 add_object(new SnowParticleSystem());
268 else if(particlesystem == "rain")
269 add_object(new RainParticleSystem());
271 Vector startpos(100, 170);
272 reader.get("start_pos_x", startpos.x);
273 reader.get("start_pos_y", startpos.y);
275 SpawnPoint* spawn = new SpawnPoint;
276 spawn->pos = startpos;
277 spawn->name = "main";
278 spawnpoints.push_back(spawn);
280 music = "chipdisko.ogg";
281 reader.get("music", music);
282 music = "music/" + music;
284 int width = 30, height = 15;
285 reader.get("width", width);
286 reader.get("height", height);
288 std::vector<unsigned int> tiles;
289 if(reader.get_vector("interactive-tm", tiles)
290 || reader.get_vector("tilemap", tiles)) {
291 TileMap* tilemap = new TileMap();
292 tilemap->set(width, height, tiles, LAYER_TILES, true);
294 // replace tile id 112 (old invisible tile) with 1311 (new invisible tile)
295 for(size_t x=0; x < tilemap->get_width(); ++x) {
296 for(size_t y=0; y < tilemap->get_height(); ++y) {
297 const Tile* tile = tilemap->get_tile(x, y);
298 if(tile->getID() == 112) tilemap->change(x, y, 1311);
305 if(reader.get_vector("background-tm", tiles)) {
306 TileMap* tilemap = new TileMap();
307 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
311 if(reader.get_vector("foreground-tm", tiles)) {
312 TileMap* tilemap = new TileMap();
313 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
317 // read reset-points (now spawn-points)
318 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
320 lisp::ListIterator iter(resetpoints);
322 if(iter.item() == "point") {
324 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
326 SpawnPoint* sp = new SpawnPoint;
329 spawnpoints.push_back(sp);
332 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
338 const lisp::Lisp* objects = reader.get_lisp("objects");
340 lisp::ListIterator iter(objects);
342 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
346 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
352 Camera* camera = new Camera(this, "Camera");
355 update_game_objects();
357 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
360 update_game_objects();
364 Sector::fix_old_tiles()
366 for(std::list<TileMap*>::iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
367 TileMap* solids = *i;
368 for(size_t x=0; x < solids->get_width(); ++x) {
369 for(size_t y=0; y < solids->get_height(); ++y) {
370 const Tile* tile = solids->get_tile(x, y);
371 Vector pos(solids->get_x_offset() + x*32, solids->get_y_offset() + y*32);
373 if(tile->getID() == 112) {
374 add_object(new InvisibleBlock(pos));
375 solids->change(x, y, 0);
376 } else if(tile->getAttributes() & Tile::COIN) {
377 add_object(new Coin(pos));
378 solids->change(x, y, 0);
379 } else if(tile->getAttributes() & Tile::FULLBOX) {
380 add_object(new BonusBlock(pos, tile->getData()));
381 solids->change(x, y, 0);
382 } else if(tile->getAttributes() & Tile::BRICK) {
383 add_object(new Brick(pos, tile->getData()));
384 solids->change(x, y, 0);
385 } else if(tile->getAttributes() & Tile::GOAL) {
386 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
387 add_object(new SequenceTrigger(pos, sequence));
388 solids->change(x, y, 0);
394 // add lights for special tiles
395 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); i++) {
396 TileMap* tm = dynamic_cast<TileMap*>(*i);
398 for(size_t x=0; x < tm->get_width(); ++x) {
399 for(size_t y=0; y < tm->get_height(); ++y) {
400 const Tile* tile = tm->get_tile(x, y);
401 Vector pos(tm->get_x_offset() + x*32, tm->get_y_offset() + y*32);
402 Vector center(pos.x + 16, pos.y + 16);
405 if (tile->getID() == 1517) {
406 float pseudo_rnd = (float)((int)pos.x % 10) / 10;
407 add_object(new PulsingLight(center, 1.0 + pseudo_rnd, 0.9, 1.0, Color(1.0, 1.0, 0.6, 1.0)));
410 if ((tile->getID() == 173) || (tile->getID() == 1700) || (tile->getID() == 1705) || (tile->getID() == 1706)) {
411 // space lights a bit
412 if (((tm->get_tile(x-1, y)->getID() != tm->get_tile(x,y)->getID())
413 && (tm->get_tile(x, y-1)->getID() != tm->get_tile(x,y)->getID()))
414 || ((x % 3 == 0) && (y % 3 == 0))) {
415 float pseudo_rnd = (float)((int)pos.x % 10) / 10;
416 add_object(new PulsingLight(center, 1.0 + pseudo_rnd, 0.8, 1.0, Color(1.0, 0.3, 0.0, 1.0)));
428 Sector::write(lisp::Writer& writer)
430 writer.write_string("name", name);
431 writer.write_float("gravity", gravity);
432 writer.write_string("music", music);
435 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
437 SpawnPoint* spawn = *i;
438 writer.start_list("spawn-points");
439 writer.write_string("name", spawn->name);
440 writer.write_float("x", spawn->pos.x);
441 writer.write_float("y", spawn->pos.y);
442 writer.end_list("spawn-points");
446 for(GameObjects::iterator i = gameobjects.begin();
447 i != gameobjects.end(); ++i) {
448 Serializable* serializable = dynamic_cast<Serializable*> (*i);
450 serializable->write(writer);
455 Sector::run_script(std::istream& in, const std::string& sourcename)
457 using namespace Scripting;
459 // garbage collect thread list
460 for(ScriptList::iterator i = scripts.begin();
461 i != scripts.end(); ) {
462 HSQOBJECT& object = *i;
463 HSQUIRRELVM vm = object_to_vm(object);
465 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
466 sq_release(global_vm, &object);
467 i = scripts.erase(i);
474 HSQOBJECT object = create_thread(global_vm);
475 scripts.push_back(object);
477 HSQUIRRELVM vm = object_to_vm(object);
479 // set sector_table as roottable for the thread
480 sq_pushobject(vm, sector_table);
483 compile_and_run(vm, in, sourcename);
489 Sector::add_object(GameObject* object)
491 // make sure the object isn't already in the list
493 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
496 assert("object already added to sector" == 0);
499 for(GameObjects::iterator i = gameobjects_new.begin();
500 i != gameobjects_new.end(); ++i) {
502 assert("object already added to sector" == 0);
508 gameobjects_new.push_back(object);
512 Sector::activate(const std::string& spawnpoint)
515 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
517 if((*i)->name == spawnpoint) {
523 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
524 if(spawnpoint != "main") {
527 activate(Vector(0, 0));
535 Sector::activate(const Vector& player_pos)
537 if(_current != this) {
539 _current->deactivate();
542 // register sectortable as sector in scripting
543 HSQUIRRELVM vm = Scripting::global_vm;
544 sq_pushroottable(vm);
545 sq_pushstring(vm, "sector", -1);
546 sq_pushobject(vm, sector_table);
547 if(SQ_FAILED(sq_createslot(vm, -3)))
548 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
551 for(GameObjects::iterator i = gameobjects.begin();
552 i != gameobjects.end(); ++i) {
553 GameObject* object = *i;
560 // spawn smalltux below spawnpoint
561 if (!player->is_big()) {
562 player->move(player_pos + Vector(0,32));
564 player->move(player_pos);
567 // spawning tux in the ground would kill him
568 if(!is_free_of_tiles(player->get_bbox())) {
569 log_warning << "Tried spawning Tux in solid matter. Compensating." << std::endl;
570 Vector npos = player->get_bbox().p1;
575 camera->reset(player->get_pos());
576 update_game_objects();
579 if(init_script != "") {
580 std::istringstream in(init_script);
581 run_script(in, std::string("Sector(") + name + ") - init");
591 // remove sector entry from global vm
592 HSQUIRRELVM vm = Scripting::global_vm;
593 sq_pushroottable(vm);
594 sq_pushstring(vm, "sector", -1);
595 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
596 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
599 for(GameObjects::iterator i = gameobjects.begin();
600 i != gameobjects.end(); ++i) {
601 GameObject* object = *i;
603 try_unexpose(object);
611 Sector::get_active_region()
614 camera->get_translation() - Vector(1600, 1200),
615 camera->get_translation() + Vector(1600, 1200));
619 Sector::update(float elapsed_time)
621 player->check_bounds(camera);
624 for(GameObjects::iterator i = gameobjects.begin();
625 i != gameobjects.end(); ++i) {
626 GameObject* object = *i;
627 if(!object->is_valid())
630 object->update(elapsed_time);
633 /* Handle all possible collisions. */
635 update_game_objects();
639 Sector::update_game_objects()
641 /** cleanup marked objects */
642 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
643 i != gameobjects.end(); /* nothing */) {
644 GameObject* object = *i;
646 if(object->is_valid()) {
651 before_object_remove(object);
654 i = gameobjects.erase(i);
657 /* add newly created objects */
658 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
659 i != gameobjects_new.end(); ++i)
661 GameObject* object = *i;
663 before_object_add(object);
665 gameobjects.push_back(object);
667 gameobjects_new.clear();
671 Sector::before_object_add(GameObject* object)
673 Bullet* bullet = dynamic_cast<Bullet*> (object);
675 bullets.push_back(bullet);
678 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
679 if(movingobject != NULL) {
680 moving_objects.push_back(movingobject);
683 Portable* portable = dynamic_cast<Portable*> (object);
684 if(portable != NULL) {
685 portables.push_back(portable);
688 TileMap* tilemap = dynamic_cast<TileMap*> (object);
689 if(tilemap != NULL && tilemap->is_solid()) {
690 solid_tilemaps.push_back(tilemap);
693 Camera* camera = dynamic_cast<Camera*> (object);
695 if(this->camera != 0) {
696 log_warning << "Multiple cameras added. Ignoring" << std::endl;
699 this->camera = camera;
702 Player* player = dynamic_cast<Player*> (object);
704 if(this->player != 0) {
705 log_warning << "Multiple players added. Ignoring" << std::endl;
708 this->player = player;
711 UsesPhysic *physic_object = dynamic_cast<UsesPhysic *>(object);
714 physic_object->physic.set_gravity(gravity);
718 if(_current == this) {
726 Sector::try_expose(GameObject* object)
728 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
729 if(interface != NULL) {
730 HSQUIRRELVM vm = Scripting::global_vm;
731 sq_pushobject(vm, sector_table);
732 interface->expose(vm, -1);
738 Sector::try_expose_me()
740 HSQUIRRELVM vm = Scripting::global_vm;
741 sq_pushobject(vm, sector_table);
742 Scripting::SSector* interface = static_cast<Scripting::SSector*> (this);
743 expose_object(vm, -1, interface, "settings", false);
748 Sector::before_object_remove(GameObject* object)
750 Portable* portable = dynamic_cast<Portable*> (object);
751 if(portable != NULL) {
752 portables.erase(std::find(portables.begin(), portables.end(), portable));
754 Bullet* bullet = dynamic_cast<Bullet*> (object);
756 bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));
758 MovingObject* moving_object = dynamic_cast<MovingObject*> (object);
759 if(moving_object != NULL) {
760 moving_objects.erase(
761 std::find(moving_objects.begin(), moving_objects.end(), moving_object));
765 try_unexpose(object);
769 Sector::try_unexpose(GameObject* object)
771 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
772 if(interface != NULL) {
773 HSQUIRRELVM vm = Scripting::global_vm;
774 SQInteger oldtop = sq_gettop(vm);
775 sq_pushobject(vm, sector_table);
777 interface->unexpose(vm, -1);
778 } catch(std::exception& e) {
779 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
781 sq_settop(vm, oldtop);
786 Sector::try_unexpose_me()
788 HSQUIRRELVM vm = Scripting::global_vm;
789 SQInteger oldtop = sq_gettop(vm);
790 sq_pushobject(vm, sector_table);
792 Scripting::unexpose_object(vm, -1, "settings");
793 } catch(std::exception& e) {
794 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
796 sq_settop(vm, oldtop);
799 Sector::draw(DrawingContext& context)
801 context.set_ambient_color( ambient_light );
802 context.push_transform();
803 context.set_translation(camera->get_translation());
805 for(GameObjects::iterator i = gameobjects.begin();
806 i != gameobjects.end(); ++i) {
807 GameObject* object = *i;
808 if(!object->is_valid())
811 if (draw_solids_only)
813 TileMap* tm = dynamic_cast<TileMap*>(object);
814 if (tm && !tm->is_solid())
818 object->draw(context);
822 Color col(0.2, 0.2, 0.2, 0.7);
823 for(MovingObjects::iterator i = moving_objects.begin();
824 i != moving_objects.end(); ++i) {
825 MovingObject* object = *i;
826 const Rect& rect = object->get_bbox();
828 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
832 context.pop_transform();
835 /*-------------------------------------------------------------------------
836 * Collision Detection
837 *-------------------------------------------------------------------------*/
839 static const float SHIFT_DELTA = 7.0f;
841 /** r1 is supposed to be moving, r2 a solid object */
842 void check_collisions(collision::Constraints* constraints,
843 const Vector& movement, const Rect& r1, const Rect& r2,
844 GameObject* object = NULL, MovingObject* other = NULL)
846 if(!collision::intersects(r1, r2))
849 // calculate intersection
850 float itop = r1.get_bottom() - r2.get_top();
851 float ibottom = r2.get_bottom() - r1.get_top();
852 float ileft = r1.get_right() - r2.get_left();
853 float iright = r2.get_right() - r1.get_left();
855 if(fabsf(movement.y) > fabsf(movement.x)) {
856 if(ileft < SHIFT_DELTA) {
857 constraints->right = std::min(constraints->right, r2.get_left());
859 } else if(iright < SHIFT_DELTA) {
860 constraints->left = std::max(constraints->left, r2.get_right());
864 // shiftout bottom/top
865 if(itop < SHIFT_DELTA) {
866 constraints->bottom = std::min(constraints->bottom, r2.get_top());
868 } else if(ibottom < SHIFT_DELTA) {
869 constraints->top = std::max(constraints->top, r2.get_bottom());
876 HitResponse response = other->collision(*object, dummy);
877 if(response == PASSTHROUGH)
879 if(other->get_movement() != Vector(0, 0)) {
880 // TODO what todo when we collide with 2 moving objects?!?
881 constraints->ground_movement = other->get_movement();
885 float vert_penetration = std::min(itop, ibottom);
886 float horiz_penetration = std::min(ileft, iright);
887 if(vert_penetration < horiz_penetration) {
889 constraints->bottom = std::min(constraints->bottom, r2.get_top());
890 constraints->hit.bottom = true;
892 constraints->top = std::max(constraints->top, r2.get_bottom());
893 constraints->hit.top = true;
897 constraints->right = std::min(constraints->right, r2.get_left());
898 constraints->hit.right = true;
900 constraints->left = std::max(constraints->left, r2.get_right());
901 constraints->hit.left = true;
906 static const float DELTA = .001;
909 Sector::collision_tilemap(collision::Constraints* constraints,
910 const Vector& movement, const Rect& dest) const
912 // calculate rectangle where the object will move
913 float x1 = dest.get_left();
914 float x2 = dest.get_right();
915 float y1 = dest.get_top();
916 float y2 = dest.get_bottom();
918 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
919 TileMap* solids = *i;
921 // test with all tiles in this rectangle
922 int starttilex = int(x1 - solids->get_x_offset()) / 32;
923 int starttiley = int(y1 - solids->get_y_offset()) / 32;
924 int max_x = int(x2 - solids->get_x_offset());
925 int max_y = int(y2+1 - solids->get_y_offset());
927 for(int x = starttilex; x*32 < max_x; ++x) {
928 for(int y = starttiley; y*32 < max_y; ++y) {
929 const Tile* tile = solids->get_tile(x, y);
932 // skip non-solid tiles
933 if((tile->getAttributes() & Tile::SOLID) == 0)
935 // only handle unisolid when the player is falling down and when he was
936 // above the tile before
937 if(tile->getAttributes() & Tile::UNISOLID) {
938 if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
942 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
944 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
945 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
946 triangle = AATriangle(p1, p2, tile->getData());
948 collision::rectangle_aatriangle(constraints, dest, triangle);
949 } else { // normal rectangular tile
950 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());
951 check_collisions(constraints, movement, dest, rect);
959 Sector::collision_tile_attributes(const Rect& dest) const
961 float x1 = dest.p1.x;
962 float y1 = dest.p1.y;
963 float x2 = dest.p2.x;
964 float y2 = dest.p2.y;
967 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
968 TileMap* solids = *i;
970 // test with all tiles in this rectangle
971 int starttilex = int(x1 - solids->get_x_offset()) / 32;
972 int starttiley = int(y1 - solids->get_y_offset()) / 32;
973 int max_x = int(x2 - solids->get_x_offset());
974 int max_y = int(y2+1 - solids->get_y_offset());
976 for(int x = starttilex; x*32 < max_x; ++x) {
977 for(int y = starttiley; y*32 < max_y; ++y) {
978 const Tile* tile = solids->get_tile(x, y);
981 result |= tile->getAttributes();
989 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
990 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
993 float itop = r1.get_bottom() - r2.get_top();
994 float ibottom = r2.get_bottom() - r1.get_top();
995 float ileft = r1.get_right() - r2.get_left();
996 float iright = r2.get_right() - r1.get_left();
998 float vert_penetration = std::min(itop, ibottom);
999 float horiz_penetration = std::min(ileft, iright);
1000 if(vert_penetration < horiz_penetration) {
1001 if(itop < ibottom) {
1003 normal.y = vert_penetration;
1006 normal.y = -vert_penetration;
1009 if(ileft < iright) {
1011 normal.x = horiz_penetration;
1014 normal.x = -horiz_penetration;
1020 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
1022 using namespace collision;
1024 const Rect& r1 = object1->dest;
1025 const Rect& r2 = object2->dest;
1028 if(intersects(object1->dest, object2->dest)) {
1030 get_hit_normal(r1, r2, hit, normal);
1032 HitResponse response1 = object1->collision(*object2, hit);
1033 std::swap(hit.left, hit.right);
1034 std::swap(hit.top, hit.bottom);
1035 HitResponse response2 = object2->collision(*object1, hit);
1036 assert( response1 != SOLID && response1 != PASSTHROUGH );
1037 assert( response2 != SOLID && response2 != PASSTHROUGH );
1038 if(response1 == CONTINUE && response2 == CONTINUE) {
1039 normal *= (0.5 + DELTA);
1040 object1->dest.move(-normal);
1041 object2->dest.move(normal);
1042 } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
1043 normal *= (1 + DELTA);
1044 object1->dest.move(-normal);
1045 } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
1046 normal *= (1 + DELTA);
1047 object2->dest.move(normal);
1053 Sector::collision_static(collision::Constraints* constraints,
1054 const Vector& movement, const Rect& dest,
1057 collision_tilemap(constraints, movement, dest);
1059 // collision with other (static) objects
1060 for(MovingObjects::iterator i = moving_objects.begin();
1061 i != moving_objects.end(); ++i) {
1062 MovingObject* moving_object = *i;
1063 if(moving_object->get_group() != COLGROUP_STATIC
1064 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1066 if(!moving_object->is_valid())
1069 if(moving_object != &object)
1070 check_collisions(constraints, movement, dest, moving_object->bbox,
1071 &object, moving_object);
1076 Sector::collision_static_constrains(MovingObject& object)
1078 using namespace collision;
1079 float infinity = (std::numeric_limits<float>::has_infinity ? std::numeric_limits<float>::infinity() : std::numeric_limits<float>::max());
1081 Constraints constraints;
1082 Vector movement = object.get_movement();
1083 Rect& dest = object.dest;
1084 float owidth = object.get_bbox().get_width();
1085 float oheight = object.get_bbox().get_height();
1087 for(int i = 0; i < 2; ++i) {
1088 collision_static(&constraints, Vector(0, movement.y), dest, object);
1089 if(!constraints.has_constraints())
1092 // apply calculated horizontal constraints
1093 if(constraints.bottom < infinity) {
1094 float height = constraints.bottom - constraints.top;
1095 if(height < oheight) {
1096 // we're crushed, but ignore this for now, we'll get this again
1097 // later if we're really crushed or things will solve itself when
1098 // looking at the vertical constraints
1100 dest.p2.y = constraints.bottom - DELTA;
1101 dest.p1.y = dest.p2.y - oheight;
1102 } else if(constraints.top > -infinity) {
1103 dest.p1.y = constraints.top + DELTA;
1104 dest.p2.y = dest.p1.y + oheight;
1107 if(constraints.has_constraints()) {
1108 if(constraints.hit.bottom) {
1109 dest.move(constraints.ground_movement);
1111 if(constraints.hit.top || constraints.hit.bottom) {
1112 constraints.hit.left = false;
1113 constraints.hit.right = false;
1114 object.collision_solid(constraints.hit);
1118 constraints = Constraints();
1119 for(int i = 0; i < 2; ++i) {
1120 collision_static(&constraints, movement, dest, object);
1121 if(!constraints.has_constraints())
1124 // apply calculated vertical constraints
1125 if(constraints.right < infinity) {
1126 float width = constraints.right - constraints.left;
1127 if(width + SHIFT_DELTA < owidth) {
1128 printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1129 constraints.left, constraints.right);
1134 object.collision_solid(h);
1136 dest.p2.x = constraints.right - DELTA;
1137 dest.p1.x = dest.p2.x - owidth;
1139 } else if(constraints.left > -infinity) {
1140 dest.p1.x = constraints.left + DELTA;
1141 dest.p2.x = dest.p1.x + owidth;
1145 if(constraints.has_constraints()) {
1146 if( constraints.hit.left || constraints.hit.right
1147 || constraints.hit.top || constraints.hit.bottom
1148 || constraints.hit.crush )
1149 object.collision_solid(constraints.hit);
1152 // an extra pass to make sure we're not crushed horizontally
1153 constraints = Constraints();
1154 collision_static(&constraints, movement, dest, object);
1155 if(constraints.bottom < infinity) {
1156 float height = constraints.bottom - constraints.top;
1157 if(height + SHIFT_DELTA < oheight) {
1158 printf("Object %p crushed vertically...\n", &object);
1163 object.collision_solid(h);
1169 Sector::handle_collisions()
1171 using namespace collision;
1173 // calculate destination positions of the objects
1174 for(MovingObjects::iterator i = moving_objects.begin();
1175 i != moving_objects.end(); ++i) {
1176 MovingObject* moving_object = *i;
1178 moving_object->dest = moving_object->get_bbox();
1179 moving_object->dest.move(moving_object->get_movement());
1182 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1183 for(MovingObjects::iterator i = moving_objects.begin();
1184 i != moving_objects.end(); ++i) {
1185 MovingObject* moving_object = *i;
1186 if((moving_object->get_group() != COLGROUP_MOVING
1187 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1188 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1189 || !moving_object->is_valid())
1192 collision_static_constrains(*moving_object);
1196 // part2: COLGROUP_MOVING vs tile attributes
1197 for(MovingObjects::iterator i = moving_objects.begin();
1198 i != moving_objects.end(); ++i) {
1199 MovingObject* moving_object = *i;
1200 if((moving_object->get_group() != COLGROUP_MOVING
1201 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1202 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1203 || !moving_object->is_valid())
1206 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1207 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1208 moving_object->collision_tile(tile_attributes);
1212 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1213 for(MovingObjects::iterator i = moving_objects.begin();
1214 i != moving_objects.end(); ++i) {
1215 MovingObject* moving_object = *i;
1216 if((moving_object->get_group() != COLGROUP_MOVING
1217 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1218 || !moving_object->is_valid())
1221 for(MovingObjects::iterator i2 = moving_objects.begin();
1222 i2 != moving_objects.end(); ++i2) {
1223 MovingObject* moving_object_2 = *i2;
1224 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1225 || !moving_object_2->is_valid())
1228 if(intersects(moving_object->dest, moving_object_2->dest)) {
1231 get_hit_normal(moving_object->dest, moving_object_2->dest,
1233 moving_object->collision(*moving_object_2, hit);
1234 moving_object_2->collision(*moving_object, hit);
1239 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1240 for(MovingObjects::iterator i = moving_objects.begin();
1241 i != moving_objects.end(); ++i) {
1242 MovingObject* moving_object = *i;
1244 if((moving_object->get_group() != COLGROUP_MOVING
1245 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1246 || !moving_object->is_valid())
1249 for(MovingObjects::iterator i2 = i+1;
1250 i2 != moving_objects.end(); ++i2) {
1251 MovingObject* moving_object_2 = *i2;
1252 if((moving_object_2->get_group() != COLGROUP_MOVING
1253 && moving_object_2->get_group() != COLGROUP_MOVING_STATIC)
1254 || !moving_object_2->is_valid())
1257 collision_object(moving_object, moving_object_2);
1261 // apply object movement
1262 for(MovingObjects::iterator i = moving_objects.begin();
1263 i != moving_objects.end(); ++i) {
1264 MovingObject* moving_object = *i;
1266 moving_object->bbox = moving_object->dest;
1267 moving_object->movement = Vector(0, 0);
1272 Sector::is_free_of_tiles(const Rect& rect) const
1274 using namespace collision;
1276 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1277 TileMap* solids = *i;
1279 // test with all tiles in this rectangle
1280 int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32;
1281 int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32;
1282 int max_x = int(rect.p2.x - solids->get_x_offset());
1283 int max_y = int(rect.p2.y - solids->get_y_offset());
1285 for(int x = starttilex; x*32 <= max_x; ++x) {
1286 for(int y = starttiley; y*32 <= max_y; ++y) {
1287 const Tile* tile = solids->get_tile(x, y);
1289 if(tile->getAttributes() & Tile::SLOPE) {
1290 AATriangle triangle;
1291 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
1292 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
1293 triangle = AATriangle(p1, p2, tile->getData());
1294 Constraints constraints;
1295 return collision::rectangle_aatriangle(&constraints, rect, triangle);
1297 if(tile->getAttributes() & Tile::SOLID) return false;
1306 Sector::is_free_of_statics(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_STATIC) {
1318 if(intersects(rect, moving_object->get_bbox())) return false;
1326 Sector::is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object) const
1328 using namespace collision;
1330 if (!is_free_of_tiles(rect)) return false;
1332 for(MovingObjects::const_iterator i = moving_objects.begin();
1333 i != moving_objects.end(); ++i) {
1334 const MovingObject* moving_object = *i;
1335 if (moving_object == ignore_object) continue;
1336 if (!moving_object->is_valid()) continue;
1337 if ((moving_object->get_group() == COLGROUP_MOVING)
1338 || (moving_object->get_group() == COLGROUP_MOVING_STATIC)
1339 || (moving_object->get_group() == COLGROUP_STATIC)) {
1340 if(intersects(rect, moving_object->get_bbox())) return false;
1348 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1350 // TODO remove this function and move these checks elsewhere...
1352 Bullet* new_bullet = 0;
1353 if((player_status->bonus == FIRE_BONUS &&
1354 (int)bullets.size() >= player_status->max_fire_bullets) ||
1355 (player_status->bonus == ICE_BONUS &&
1356 (int)bullets.size() >= player_status->max_ice_bullets))
1358 new_bullet = new Bullet(pos, xm, dir, player_status->bonus);
1359 add_object(new_bullet);
1361 sound_manager->play("sounds/shoot.wav");
1367 Sector::add_smoke_cloud(const Vector& pos)
1369 add_object(new SmokeCloud(pos));
1374 Sector::play_music(MusicType type)
1376 currentmusic = type;
1377 switch(currentmusic) {
1379 sound_manager->play_music(music);
1382 sound_manager->play_music("music/salcon.ogg");
1384 case HERRING_WARNING_MUSIC:
1385 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1388 sound_manager->play_music("");
1394 Sector::get_music_type()
1396 return currentmusic;
1400 Sector::get_total_badguys()
1402 int total_badguys = 0;
1403 for(GameObjects::iterator i = gameobjects.begin();
1404 i != gameobjects.end(); ++i) {
1405 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1406 if (badguy && badguy->countMe)
1410 return total_badguys;
1414 Sector::inside(const Rect& rect) const
1416 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1417 TileMap* solids = *i;
1418 bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
1419 bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
1420 if (horizontally && vertically) return true;
1426 Sector::get_width() const
1429 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1430 TileMap* solids = *i;
1431 if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset());
1437 Sector::get_height() const
1440 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1441 TileMap* solids = *i;
1442 if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset());
1448 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
1450 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1451 TileMap* solids = *i;
1452 solids->change_all(old_tile_id, new_tile_id);
1458 Sector::set_ambient_light(float red, float green, float blue)
1460 ambient_light.red = red;
1461 ambient_light.green = green;
1462 ambient_light.blue = blue;
1466 Sector::get_ambient_red()
1468 return ambient_light.red;
1472 Sector::get_ambient_green()
1474 return ambient_light.green;
1478 Sector::get_ambient_blue()
1480 return ambient_light.blue;