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.
32 #include "object/player.hpp"
33 #include "object/gameobjs.hpp"
34 #include "object/camera.hpp"
35 #include "object/background.hpp"
36 #include "object/gradient.hpp"
37 #include "object/particlesystem.hpp"
38 #include "object/particlesystem_interactive.hpp"
39 #include "object/tilemap.hpp"
40 #include "lisp/parser.hpp"
41 #include "lisp/lisp.hpp"
42 #include "lisp/writer.hpp"
43 #include "lisp/list_iterator.hpp"
45 #include "audio/sound_manager.hpp"
46 #include "game_session.hpp"
47 #include "resources.hpp"
48 #include "statistics.hpp"
49 #include "object_factory.hpp"
50 #include "collision.hpp"
51 #include "spawn_point.hpp"
52 #include "math/rect.hpp"
53 #include "math/aatriangle.hpp"
54 #include "object/coin.hpp"
55 #include "object/block.hpp"
56 #include "object/invisible_block.hpp"
57 #include "object/bullet.hpp"
58 #include "object/text_object.hpp"
59 #include "object/portable.hpp"
60 #include "badguy/jumpy.hpp"
61 #include "trigger/sequence_trigger.hpp"
62 #include "player_status.hpp"
63 #include "scripting/squirrel_util.hpp"
64 #include "script_interface.hpp"
67 Sector* Sector::_current = 0;
69 bool Sector::show_collrects = false;
70 bool Sector::draw_solids_only = false;
72 Sector::Sector(Level* parent)
73 : level(parent), currentmusic(LEVEL_MUSIC),
74 ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), gravity(10), player(0), camera(0)
76 add_object(new Player(player_status, "Tux"));
77 add_object(new DisplayEffect("Effect"));
78 add_object(new TextObject("Text"));
80 // create a new squirrel table for the sector
81 using namespace Scripting;
83 sq_collectgarbage(global_vm);
85 sq_newtable(global_vm);
86 sq_pushroottable(global_vm);
87 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
88 throw Scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate");
90 sq_resetobject(§or_table);
91 if(SQ_FAILED(sq_getstackobj(global_vm, -1, §or_table)))
92 throw Scripting::SquirrelError(global_vm, "Couldn't get sector table");
93 sq_addref(global_vm, §or_table);
99 using namespace Scripting;
103 for(ScriptList::iterator i = scripts.begin();
104 i != scripts.end(); ++i) {
105 HSQOBJECT& object = *i;
106 sq_release(global_vm, &object);
108 sq_release(global_vm, §or_table);
109 sq_collectgarbage(global_vm);
111 update_game_objects();
112 assert(gameobjects_new.size() == 0);
114 for(GameObjects::iterator i = gameobjects.begin();
115 i != gameobjects.end(); ++i) {
116 GameObject* object = *i;
117 before_object_remove(object);
121 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
133 Sector::parse_object(const std::string& name, const lisp::Lisp& reader)
135 if(name == "camera") {
136 Camera* camera = new Camera(this, "Camera");
137 camera->parse(reader);
139 } else if(name == "particles-snow") {
140 SnowParticleSystem* partsys = new SnowParticleSystem();
141 partsys->parse(reader);
143 } else if(name == "particles-rain") {
144 RainParticleSystem* partsys = new RainParticleSystem();
145 partsys->parse(reader);
147 } else if(name == "particles-comets") {
148 CometParticleSystem* partsys = new CometParticleSystem();
149 partsys->parse(reader);
151 } else if(name == "particles-ghosts") {
152 GhostParticleSystem* partsys = new GhostParticleSystem();
153 partsys->parse(reader);
155 } else if(name == "particles-clouds") {
156 CloudParticleSystem* partsys = new CloudParticleSystem();
157 partsys->parse(reader);
159 } else if(name == "money") { // for compatibility with old maps
160 return new Jumpy(reader);
164 return create_object(name, reader);
165 } catch(std::exception& e) {
166 log_warning << e.what() << "" << std::endl;
173 Sector::parse(const lisp::Lisp& sector)
175 lisp::ListIterator iter(§or);
177 const std::string& token = iter.item();
178 if(token == "name") {
179 iter.value()->get(name);
180 } else if(token == "gravity") {
181 iter.value()->get(gravity);
182 } else if(token == "music") {
183 iter.value()->get(music);
184 } else if(token == "spawnpoint") {
185 SpawnPoint* sp = new SpawnPoint(iter.lisp());
186 spawnpoints.push_back(sp);
187 } else if(token == "init-script") {
188 iter.value()->get(init_script);
189 } else if(token == "ambient-light") {
190 std::vector<float> vColor;
191 sector.get_vector( "ambient-light", vColor );
192 if(vColor.size() < 3) {
193 log_warning << "(ambient-light) requires a color as argument" << std::endl;
195 ambient_light = Color( vColor );
198 GameObject* object = parse_object(token, *(iter.lisp()));
205 update_game_objects();
207 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
211 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
212 update_game_objects();
213 add_object(new Camera(this, "Camera"));
216 update_game_objects();
220 Sector::parse_old_format(const lisp::Lisp& reader)
223 reader.get("gravity", gravity);
225 std::string backgroundimage;
226 reader.get("background", backgroundimage);
228 reader.get("bkgd_speed", bgspeed);
231 Color bkgd_top, bkgd_bottom;
232 int r = 0, g = 0, b = 128;
233 reader.get("bkgd_red_top", r);
234 reader.get("bkgd_green_top", g);
235 reader.get("bkgd_blue_top", b);
236 bkgd_top.red = static_cast<float> (r) / 255.0f;
237 bkgd_top.green = static_cast<float> (g) / 255.0f;
238 bkgd_top.blue = static_cast<float> (b) / 255.0f;
240 reader.get("bkgd_red_bottom", r);
241 reader.get("bkgd_green_bottom", g);
242 reader.get("bkgd_blue_bottom", b);
243 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
244 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
245 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
247 if(backgroundimage != "") {
248 Background* background = new Background();
249 background->set_image(
250 std::string("images/background/") + backgroundimage, bgspeed);
251 add_object(background);
253 Gradient* gradient = new Gradient();
254 gradient->set_gradient(bkgd_top, bkgd_bottom);
255 add_object(gradient);
258 std::string particlesystem;
259 reader.get("particle_system", particlesystem);
260 if(particlesystem == "clouds")
261 add_object(new CloudParticleSystem());
262 else if(particlesystem == "snow")
263 add_object(new SnowParticleSystem());
264 else if(particlesystem == "rain")
265 add_object(new RainParticleSystem());
267 Vector startpos(100, 170);
268 reader.get("start_pos_x", startpos.x);
269 reader.get("start_pos_y", startpos.y);
271 SpawnPoint* spawn = new SpawnPoint;
272 spawn->pos = startpos;
273 spawn->name = "main";
274 spawnpoints.push_back(spawn);
276 music = "chipdisko.ogg";
277 reader.get("music", music);
278 music = "music/" + music;
280 int width = 30, height = 15;
281 reader.get("width", width);
282 reader.get("height", height);
284 std::vector<unsigned int> tiles;
285 if(reader.get_vector("interactive-tm", tiles)
286 || reader.get_vector("tilemap", tiles)) {
287 TileMap* tilemap = new TileMap();
288 tilemap->set(width, height, tiles, LAYER_TILES, true);
292 if(reader.get_vector("background-tm", tiles)) {
293 TileMap* tilemap = new TileMap();
294 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
298 if(reader.get_vector("foreground-tm", tiles)) {
299 TileMap* tilemap = new TileMap();
300 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
304 // read reset-points (now spawn-points)
305 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
307 lisp::ListIterator iter(resetpoints);
309 if(iter.item() == "point") {
311 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
313 SpawnPoint* sp = new SpawnPoint;
316 spawnpoints.push_back(sp);
319 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
325 const lisp::Lisp* objects = reader.get_lisp("objects");
327 lisp::ListIterator iter(objects);
329 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
333 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
339 Camera* camera = new Camera(this, "Camera");
342 update_game_objects();
344 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
347 update_game_objects();
351 Sector::fix_old_tiles()
353 for(std::list<TileMap*>::iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
354 TileMap* solids = *i;
355 for(size_t x=0; x < solids->get_width(); ++x) {
356 for(size_t y=0; y < solids->get_height(); ++y) {
357 const Tile* tile = solids->get_tile(x, y);
358 Vector pos(solids->get_x_offset() + x*32, solids->get_y_offset() + y*32);
360 if(tile->getID() == 112) {
361 add_object(new InvisibleBlock(pos));
362 solids->change(x, y, 0);
363 } else if(tile->getAttributes() & Tile::COIN) {
364 add_object(new Coin(pos));
365 solids->change(x, y, 0);
366 } else if(tile->getAttributes() & Tile::FULLBOX) {
367 add_object(new BonusBlock(pos, tile->getData()));
368 solids->change(x, y, 0);
369 } else if(tile->getAttributes() & Tile::BRICK) {
370 add_object(new Brick(pos, tile->getData()));
371 solids->change(x, y, 0);
372 } else if(tile->getAttributes() & Tile::GOAL) {
373 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
374 add_object(new SequenceTrigger(pos, sequence));
375 solids->change(x, y, 0);
383 Sector::write(lisp::Writer& writer)
385 writer.write_string("name", name);
386 writer.write_float("gravity", gravity);
387 writer.write_string("music", music);
390 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
392 SpawnPoint* spawn = *i;
393 writer.start_list("spawn-points");
394 writer.write_string("name", spawn->name);
395 writer.write_float("x", spawn->pos.x);
396 writer.write_float("y", spawn->pos.y);
397 writer.end_list("spawn-points");
401 for(GameObjects::iterator i = gameobjects.begin();
402 i != gameobjects.end(); ++i) {
403 Serializable* serializable = dynamic_cast<Serializable*> (*i);
405 serializable->write(writer);
410 Sector::run_script(std::istream& in, const std::string& sourcename)
412 using namespace Scripting;
414 // garbage collect thread list
415 for(ScriptList::iterator i = scripts.begin();
416 i != scripts.end(); ) {
417 HSQOBJECT& object = *i;
418 HSQUIRRELVM vm = object_to_vm(object);
420 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
421 sq_release(global_vm, &object);
422 i = scripts.erase(i);
429 HSQOBJECT object = create_thread(global_vm);
430 scripts.push_back(object);
432 HSQUIRRELVM vm = object_to_vm(object);
434 // set sector_table as roottable for the thread
435 sq_pushobject(vm, sector_table);
438 compile_and_run(vm, in, sourcename);
444 Sector::add_object(GameObject* object)
446 // make sure the object isn't already in the list
448 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
451 assert("object already added to sector" == 0);
454 for(GameObjects::iterator i = gameobjects_new.begin();
455 i != gameobjects_new.end(); ++i) {
457 assert("object already added to sector" == 0);
463 gameobjects_new.push_back(object);
467 Sector::activate(const std::string& spawnpoint)
470 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
472 if((*i)->name == spawnpoint) {
478 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
479 if(spawnpoint != "main") {
482 activate(Vector(0, 0));
490 Sector::activate(const Vector& player_pos)
492 if(_current != this) {
494 _current->deactivate();
497 // register sectortable as sector in scripting
498 HSQUIRRELVM vm = Scripting::global_vm;
499 sq_pushroottable(vm);
500 sq_pushstring(vm, "sector", -1);
501 sq_pushobject(vm, sector_table);
502 if(SQ_FAILED(sq_createslot(vm, -3)))
503 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
506 for(GameObjects::iterator i = gameobjects.begin();
507 i != gameobjects.end(); ++i) {
508 GameObject* object = *i;
515 player->move(player_pos);
516 camera->reset(player->get_pos());
517 update_game_objects();
520 if(init_script != "") {
521 std::istringstream in(init_script);
522 run_script(in, std::string("Sector(") + name + ") - init");
532 // remove sector entry from global vm
533 HSQUIRRELVM vm = Scripting::global_vm;
534 sq_pushroottable(vm);
535 sq_pushstring(vm, "sector", -1);
536 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
537 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
540 for(GameObjects::iterator i = gameobjects.begin();
541 i != gameobjects.end(); ++i) {
542 GameObject* object = *i;
544 try_unexpose(object);
552 Sector::get_active_region()
555 camera->get_translation() - Vector(1600, 1200),
556 camera->get_translation() + Vector(1600, 1200));
560 Sector::update(float elapsed_time)
562 player->check_bounds(camera);
565 for(GameObjects::iterator i = gameobjects.begin();
566 i != gameobjects.end(); ++i) {
567 GameObject* object = *i;
568 if(!object->is_valid())
571 object->update(elapsed_time);
574 /* Handle all possible collisions. */
576 update_game_objects();
580 Sector::update_game_objects()
582 /** cleanup marked objects */
583 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
584 i != gameobjects.end(); /* nothing */) {
585 GameObject* object = *i;
587 if(object->is_valid()) {
592 before_object_remove(object);
595 i = gameobjects.erase(i);
598 /* add newly created objects */
599 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
600 i != gameobjects_new.end(); ++i)
602 GameObject* object = *i;
604 before_object_add(object);
606 gameobjects.push_back(object);
608 gameobjects_new.clear();
612 Sector::before_object_add(GameObject* object)
614 Bullet* bullet = dynamic_cast<Bullet*> (object);
616 bullets.push_back(bullet);
619 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
620 if(movingobject != NULL) {
621 moving_objects.push_back(movingobject);
624 Portable* portable = dynamic_cast<Portable*> (object);
625 if(portable != NULL) {
626 portables.push_back(portable);
629 TileMap* tilemap = dynamic_cast<TileMap*> (object);
630 if(tilemap != NULL && tilemap->is_solid()) {
631 solid_tilemaps.push_back(tilemap);
634 Camera* camera = dynamic_cast<Camera*> (object);
636 if(this->camera != 0) {
637 log_warning << "Multiple cameras added. Ignoring" << std::endl;
640 this->camera = camera;
643 Player* player = dynamic_cast<Player*> (object);
645 if(this->player != 0) {
646 log_warning << "Multiple players added. Ignoring" << std::endl;
649 this->player = player;
652 if(_current == this) {
660 Sector::try_expose(GameObject* object)
662 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
663 if(interface != NULL) {
664 HSQUIRRELVM vm = Scripting::global_vm;
665 sq_pushobject(vm, sector_table);
666 interface->expose(vm, -1);
672 Sector::try_expose_me()
674 HSQUIRRELVM vm = Scripting::global_vm;
675 sq_pushobject(vm, sector_table);
676 Scripting::SSector* interface = static_cast<Scripting::SSector*> (this);
677 expose_object(vm, -1, interface, "settings", false);
682 Sector::before_object_remove(GameObject* object)
684 Portable* portable = dynamic_cast<Portable*> (object);
685 if(portable != NULL) {
686 portables.erase(std::find(portables.begin(), portables.end(), portable));
688 Bullet* bullet = dynamic_cast<Bullet*> (object);
690 bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));
692 MovingObject* moving_object = dynamic_cast<MovingObject*> (object);
693 if(moving_object != NULL) {
694 moving_objects.erase(
695 std::find(moving_objects.begin(), moving_objects.end(), moving_object));
699 try_unexpose(object);
703 Sector::try_unexpose(GameObject* object)
705 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
706 if(interface != NULL) {
707 HSQUIRRELVM vm = Scripting::global_vm;
708 SQInteger oldtop = sq_gettop(vm);
709 sq_pushobject(vm, sector_table);
711 interface->unexpose(vm, -1);
712 } catch(std::exception& e) {
713 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
715 sq_settop(vm, oldtop);
720 Sector::try_unexpose_me()
722 HSQUIRRELVM vm = Scripting::global_vm;
723 SQInteger oldtop = sq_gettop(vm);
724 sq_pushobject(vm, sector_table);
726 Scripting::unexpose_object(vm, -1, "settings");
727 } catch(std::exception& e) {
728 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
730 sq_settop(vm, oldtop);
733 Sector::draw(DrawingContext& context)
735 context.set_ambient_color( ambient_light );
736 context.push_transform();
737 context.set_translation(camera->get_translation());
739 for(GameObjects::iterator i = gameobjects.begin();
740 i != gameobjects.end(); ++i) {
741 GameObject* object = *i;
742 if(!object->is_valid())
745 if (draw_solids_only)
747 TileMap* tm = dynamic_cast<TileMap*>(object);
748 if (tm && !tm->is_solid())
752 object->draw(context);
756 Color col(0.2, 0.2, 0.2, 0.7);
757 for(MovingObjects::iterator i = moving_objects.begin();
758 i != moving_objects.end(); ++i) {
759 MovingObject* object = *i;
760 const Rect& rect = object->get_bbox();
762 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
766 context.pop_transform();
769 /*-------------------------------------------------------------------------
770 * Collision Detection
771 *-------------------------------------------------------------------------*/
773 static const float SHIFT_DELTA = 7.0f;
775 /** r1 is supposed to be moving, r2 a solid object */
776 void check_collisions(collision::Constraints* constraints,
777 const Vector& movement, const Rect& r1, const Rect& r2,
778 GameObject* object = NULL, MovingObject* other = NULL)
780 if(!collision::intersects(r1, r2))
783 // calculate intersection
784 float itop = r1.get_bottom() - r2.get_top();
785 float ibottom = r2.get_bottom() - r1.get_top();
786 float ileft = r1.get_right() - r2.get_left();
787 float iright = r2.get_right() - r1.get_left();
789 if(fabsf(movement.y) > fabsf(movement.x)) {
790 if(ileft < SHIFT_DELTA) {
791 constraints->right = std::min(constraints->right, r2.get_left());
793 } else if(iright < SHIFT_DELTA) {
794 constraints->left = std::max(constraints->left, r2.get_right());
798 // shiftout bottom/top
799 if(itop < SHIFT_DELTA) {
800 constraints->bottom = std::min(constraints->bottom, r2.get_top());
802 } else if(ibottom < SHIFT_DELTA) {
803 constraints->top = std::max(constraints->top, r2.get_bottom());
810 HitResponse response = other->collision(*object, dummy);
811 if(response == PASSTHROUGH)
813 if(other->get_movement() != Vector(0, 0)) {
814 // TODO what todo when we collide with 2 moving objects?!?
815 constraints->ground_movement = other->get_movement();
819 float vert_penetration = std::min(itop, ibottom);
820 float horiz_penetration = std::min(ileft, iright);
821 if(vert_penetration < horiz_penetration) {
823 constraints->bottom = std::min(constraints->bottom, r2.get_top());
824 constraints->hit.bottom = true;
826 constraints->top = std::max(constraints->top, r2.get_bottom());
827 constraints->hit.top = true;
831 constraints->right = std::min(constraints->right, r2.get_left());
832 constraints->hit.right = true;
834 constraints->left = std::max(constraints->left, r2.get_right());
835 constraints->hit.left = true;
840 static const float DELTA = .001;
843 Sector::collision_tilemap(collision::Constraints* constraints,
844 const Vector& movement, const Rect& dest) const
846 // calculate rectangle where the object will move
847 float x1 = dest.get_left();
848 float x2 = dest.get_right();
849 float y1 = dest.get_top();
850 float y2 = dest.get_bottom();
852 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
853 TileMap* solids = *i;
855 // test with all tiles in this rectangle
856 int starttilex = int(x1 - solids->get_x_offset()) / 32;
857 int starttiley = int(y1 - solids->get_y_offset()) / 32;
858 int max_x = int(x2 - solids->get_x_offset());
859 int max_y = int(y2+1 - solids->get_y_offset());
861 for(int x = starttilex; x*32 < max_x; ++x) {
862 for(int y = starttiley; y*32 < max_y; ++y) {
863 const Tile* tile = solids->get_tile(x, y);
866 // skip non-solid tiles
867 if((tile->getAttributes() & Tile::SOLID) == 0)
869 // only handle unisolid when the player is falling down and when he was
870 // above the tile before
871 if(tile->getAttributes() & Tile::UNISOLID) {
872 if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
876 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
878 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
879 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
880 triangle = AATriangle(p1, p2, tile->getData());
882 collision::rectangle_aatriangle(constraints, dest, triangle);
883 } else { // normal rectangular tile
884 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());
885 check_collisions(constraints, movement, dest, rect);
893 Sector::collision_tile_attributes(const Rect& dest) const
895 float x1 = dest.p1.x;
896 float y1 = dest.p1.y;
897 float x2 = dest.p2.x;
898 float y2 = dest.p2.y;
901 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
902 TileMap* solids = *i;
904 // test with all tiles in this rectangle
905 int starttilex = int(x1 - solids->get_x_offset()) / 32;
906 int starttiley = int(y1 - solids->get_y_offset()) / 32;
907 int max_x = int(x2 - solids->get_x_offset());
908 int max_y = int(y2+1 - solids->get_y_offset());
910 for(int x = starttilex; x*32 < max_x; ++x) {
911 for(int y = starttiley; y*32 < max_y; ++y) {
912 const Tile* tile = solids->get_tile(x, y);
915 result |= tile->getAttributes();
923 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
924 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
927 float itop = r1.get_bottom() - r2.get_top();
928 float ibottom = r2.get_bottom() - r1.get_top();
929 float ileft = r1.get_right() - r2.get_left();
930 float iright = r2.get_right() - r1.get_left();
932 float vert_penetration = std::min(itop, ibottom);
933 float horiz_penetration = std::min(ileft, iright);
934 if(vert_penetration < horiz_penetration) {
937 normal.y = vert_penetration;
940 normal.y = -vert_penetration;
945 normal.x = horiz_penetration;
948 normal.x = -horiz_penetration;
954 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
956 using namespace collision;
958 const Rect& r1 = object1->dest;
959 const Rect& r2 = object2->dest;
962 if(intersects(object1->dest, object2->dest)) {
964 get_hit_normal(r1, r2, hit, normal);
966 HitResponse response1 = object1->collision(*object2, hit);
967 std::swap(hit.left, hit.right);
968 std::swap(hit.top, hit.bottom);
969 HitResponse response2 = object2->collision(*object1, hit);
970 assert( response1 != SOLID && response1 != PASSTHROUGH );
971 assert( response2 != SOLID && response2 != PASSTHROUGH );
972 if(response1 == CONTINUE && response2 == CONTINUE) {
973 normal *= (0.5 + DELTA);
974 object1->dest.move(-normal);
975 object2->dest.move(normal);
976 } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
977 normal *= (1 + DELTA);
978 object1->dest.move(-normal);
979 } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
980 normal *= (1 + DELTA);
981 object2->dest.move(normal);
987 Sector::collision_static(collision::Constraints* constraints,
988 const Vector& movement, const Rect& dest,
991 collision_tilemap(constraints, movement, dest);
993 // collision with other (static) objects
994 for(MovingObjects::iterator i = moving_objects.begin();
995 i != moving_objects.end(); ++i) {
996 MovingObject* moving_object = *i;
997 if(moving_object->get_group() != COLGROUP_STATIC
998 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1000 if(!moving_object->is_valid())
1003 if(moving_object != &object)
1004 check_collisions(constraints, movement, dest, moving_object->bbox,
1005 &object, moving_object);
1010 Sector::collision_static_constrains(MovingObject& object)
1012 using namespace collision;
1014 Constraints constraints;
1015 Vector movement = object.get_movement();
1016 Rect& dest = object.dest;
1017 float owidth = object.get_bbox().get_width();
1018 float oheight = object.get_bbox().get_height();
1020 for(int i = 0; i < 2; ++i) {
1021 collision_static(&constraints, Vector(0, movement.y), dest, object);
1022 if(!constraints.has_constraints())
1025 // apply calculated horizontal constraints
1026 if(constraints.bottom < INFINITY) {
1027 float height = constraints.bottom - constraints.top;
1028 if(height < oheight) {
1029 // we're crushed, but ignore this for now, we'll get this again
1030 // later if we're really crushed or things will solve itself when
1031 // looking at the vertical constraints
1033 dest.p2.y = constraints.bottom - DELTA;
1034 dest.p1.y = dest.p2.y - oheight;
1035 } else if(constraints.top > -INFINITY) {
1036 dest.p1.y = constraints.top + DELTA;
1037 dest.p2.y = dest.p1.y + oheight;
1040 if(constraints.has_constraints()) {
1041 if(constraints.hit.bottom) {
1042 dest.move(constraints.ground_movement);
1044 if(constraints.hit.top || constraints.hit.bottom) {
1045 constraints.hit.left = false;
1046 constraints.hit.right = false;
1047 object.collision_solid(constraints.hit);
1051 constraints = Constraints();
1052 for(int i = 0; i < 2; ++i) {
1053 collision_static(&constraints, movement, dest, object);
1054 if(!constraints.has_constraints())
1057 // apply calculated vertical constraints
1058 if(constraints.right < INFINITY) {
1059 float width = constraints.right - constraints.left;
1060 if(width + SHIFT_DELTA < owidth) {
1061 printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1062 constraints.left, constraints.right);
1067 object.collision_solid(h);
1069 dest.p2.x = constraints.right - DELTA;
1070 dest.p1.x = dest.p2.x - owidth;
1072 } else if(constraints.left > -INFINITY) {
1073 dest.p1.x = constraints.left + DELTA;
1074 dest.p2.x = dest.p1.x + owidth;
1078 if(constraints.has_constraints()) {
1079 if( constraints.hit.left || constraints.hit.right
1080 || constraints.hit.top || constraints.hit.bottom
1081 || constraints.hit.crush )
1082 object.collision_solid(constraints.hit);
1085 // an extra pass to make sure we're not crushed horizontally
1086 constraints = Constraints();
1087 collision_static(&constraints, movement, dest, object);
1088 if(constraints.bottom < INFINITY) {
1089 float height = constraints.bottom - constraints.top;
1090 if(height + SHIFT_DELTA < oheight) {
1091 printf("Object %p crushed vertically...\n", &object);
1096 object.collision_solid(h);
1102 Sector::handle_collisions()
1104 using namespace collision;
1106 // calculate destination positions of the objects
1107 for(MovingObjects::iterator i = moving_objects.begin();
1108 i != moving_objects.end(); ++i) {
1109 MovingObject* moving_object = *i;
1111 moving_object->dest = moving_object->get_bbox();
1112 moving_object->dest.move(moving_object->get_movement());
1115 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1116 for(MovingObjects::iterator i = moving_objects.begin();
1117 i != moving_objects.end(); ++i) {
1118 MovingObject* moving_object = *i;
1119 if((moving_object->get_group() != COLGROUP_MOVING
1120 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1121 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1122 || !moving_object->is_valid())
1125 collision_static_constrains(*moving_object);
1129 // part2: COLGROUP_MOVING vs tile attributes
1130 for(MovingObjects::iterator i = moving_objects.begin();
1131 i != moving_objects.end(); ++i) {
1132 MovingObject* moving_object = *i;
1133 if((moving_object->get_group() != COLGROUP_MOVING
1134 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1135 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1136 || !moving_object->is_valid())
1139 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1140 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1141 moving_object->collision_tile(tile_attributes);
1145 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1146 for(MovingObjects::iterator i = moving_objects.begin();
1147 i != moving_objects.end(); ++i) {
1148 MovingObject* moving_object = *i;
1149 if((moving_object->get_group() != COLGROUP_MOVING
1150 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1151 || !moving_object->is_valid())
1154 for(MovingObjects::iterator i2 = moving_objects.begin();
1155 i2 != moving_objects.end(); ++i2) {
1156 MovingObject* moving_object_2 = *i2;
1157 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1158 || !moving_object_2->is_valid())
1161 if(intersects(moving_object->dest, moving_object_2->dest)) {
1164 get_hit_normal(moving_object->dest, moving_object_2->dest,
1166 moving_object->collision(*moving_object_2, hit);
1167 moving_object_2->collision(*moving_object, hit);
1172 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1173 for(MovingObjects::iterator i = moving_objects.begin();
1174 i != moving_objects.end(); ++i) {
1175 MovingObject* moving_object = *i;
1177 if((moving_object->get_group() != COLGROUP_MOVING
1178 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1179 || !moving_object->is_valid())
1182 for(MovingObjects::iterator i2 = i+1;
1183 i2 != moving_objects.end(); ++i2) {
1184 MovingObject* moving_object_2 = *i2;
1185 if((moving_object_2->get_group() != COLGROUP_MOVING
1186 && moving_object_2->get_group() != COLGROUP_MOVING_STATIC)
1187 || !moving_object_2->is_valid())
1190 collision_object(moving_object, moving_object_2);
1194 // apply object movement
1195 for(MovingObjects::iterator i = moving_objects.begin();
1196 i != moving_objects.end(); ++i) {
1197 MovingObject* moving_object = *i;
1199 moving_object->bbox = moving_object->dest;
1200 moving_object->movement = Vector(0, 0);
1205 Sector::is_free_of_tiles(const Rect& rect) const
1207 using namespace collision;
1209 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1210 TileMap* solids = *i;
1212 // test with all tiles in this rectangle
1213 int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32;
1214 int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32;
1215 int max_x = int(rect.p2.x - solids->get_x_offset());
1216 int max_y = int(rect.p2.y - solids->get_y_offset());
1218 for(int x = starttilex; x*32 <= max_x; ++x) {
1219 for(int y = starttiley; y*32 <= max_y; ++y) {
1220 const Tile* tile = solids->get_tile(x, y);
1222 if(tile->getAttributes() & Tile::SLOPE) {
1223 AATriangle triangle;
1224 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
1225 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
1226 triangle = AATriangle(p1, p2, tile->getData());
1227 Constraints constraints;
1228 return collision::rectangle_aatriangle(&constraints, rect, triangle);
1230 if(tile->getAttributes() & Tile::SOLID) return false;
1239 Sector::is_free_of_statics(const Rect& rect, const MovingObject* ignore_object) const
1241 using namespace collision;
1243 if (!is_free_of_tiles(rect)) return false;
1245 for(MovingObjects::const_iterator i = moving_objects.begin();
1246 i != moving_objects.end(); ++i) {
1247 const MovingObject* moving_object = *i;
1248 if (moving_object == ignore_object) continue;
1249 if (!moving_object->is_valid()) continue;
1250 if (moving_object->get_group() == COLGROUP_STATIC) {
1251 if(intersects(rect, moving_object->get_bbox())) return false;
1259 Sector::is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object) const
1261 using namespace collision;
1263 if (!is_free_of_tiles(rect)) return false;
1265 for(MovingObjects::const_iterator i = moving_objects.begin();
1266 i != moving_objects.end(); ++i) {
1267 const MovingObject* moving_object = *i;
1268 if (moving_object == ignore_object) continue;
1269 if (!moving_object->is_valid()) continue;
1270 if ((moving_object->get_group() == COLGROUP_MOVING)
1271 || (moving_object->get_group() == COLGROUP_MOVING_STATIC)
1272 || (moving_object->get_group() == COLGROUP_STATIC)) {
1273 if(intersects(rect, moving_object->get_bbox())) return false;
1281 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1283 // TODO remove this function and move these checks elsewhere...
1285 Bullet* new_bullet = 0;
1286 if((player_status->bonus == FIRE_BONUS &&
1287 (int)bullets.size() >= player_status->max_fire_bullets) ||
1288 (player_status->bonus == ICE_BONUS &&
1289 (int)bullets.size() >= player_status->max_ice_bullets))
1291 new_bullet = new Bullet(pos, xm, dir, player_status->bonus);
1292 add_object(new_bullet);
1294 sound_manager->play("sounds/shoot.wav");
1300 Sector::add_smoke_cloud(const Vector& pos)
1302 add_object(new SmokeCloud(pos));
1307 Sector::play_music(MusicType type)
1309 currentmusic = type;
1310 switch(currentmusic) {
1312 sound_manager->play_music(music);
1315 sound_manager->play_music("music/salcon.ogg");
1317 case HERRING_WARNING_MUSIC:
1318 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1321 sound_manager->play_music("");
1327 Sector::get_music_type()
1329 return currentmusic;
1333 Sector::get_total_badguys()
1335 int total_badguys = 0;
1336 for(GameObjects::iterator i = gameobjects.begin();
1337 i != gameobjects.end(); ++i) {
1338 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1339 if (badguy && badguy->countMe)
1343 return total_badguys;
1347 Sector::inside(const Rect& rect) const
1349 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1350 TileMap* solids = *i;
1351 bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
1352 bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
1353 if (horizontally && vertically) return true;
1359 Sector::get_width() const
1362 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1363 TileMap* solids = *i;
1364 if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset());
1370 Sector::get_height() const
1373 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1374 TileMap* solids = *i;
1375 if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset());
1381 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
1383 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1384 TileMap* solids = *i;
1385 solids->change_all(old_tile_id, new_tile_id);
1391 Sector::set_ambient_light(float red, float green, float blue)
1393 ambient_light.red = red;
1394 ambient_light.green = green;
1395 ambient_light.blue = blue;
1399 Sector::get_ambient_red()
1401 return ambient_light.red;
1405 Sector::get_ambient_green()
1407 return ambient_light.green;
1411 Sector::get_ambient_blue()
1413 return ambient_light.blue;