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 ambient_light = Color( vColor );
194 GameObject* object = parse_object(token, *(iter.lisp()));
201 update_game_objects();
203 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
207 log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
208 update_game_objects();
209 add_object(new Camera(this, "Camera"));
212 update_game_objects();
216 Sector::parse_old_format(const lisp::Lisp& reader)
219 reader.get("gravity", gravity);
221 std::string backgroundimage;
222 reader.get("background", backgroundimage);
224 reader.get("bkgd_speed", bgspeed);
227 Color bkgd_top, bkgd_bottom;
228 int r = 0, g = 0, b = 128;
229 reader.get("bkgd_red_top", r);
230 reader.get("bkgd_green_top", g);
231 reader.get("bkgd_blue_top", b);
232 bkgd_top.red = static_cast<float> (r) / 255.0f;
233 bkgd_top.green = static_cast<float> (g) / 255.0f;
234 bkgd_top.blue = static_cast<float> (b) / 255.0f;
236 reader.get("bkgd_red_bottom", r);
237 reader.get("bkgd_green_bottom", g);
238 reader.get("bkgd_blue_bottom", b);
239 bkgd_bottom.red = static_cast<float> (r) / 255.0f;
240 bkgd_bottom.green = static_cast<float> (g) / 255.0f;
241 bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
243 if(backgroundimage != "") {
244 Background* background = new Background();
245 background->set_image(
246 std::string("images/background/") + backgroundimage, bgspeed);
247 add_object(background);
249 Gradient* gradient = new Gradient();
250 gradient->set_gradient(bkgd_top, bkgd_bottom);
251 add_object(gradient);
254 std::string particlesystem;
255 reader.get("particle_system", particlesystem);
256 if(particlesystem == "clouds")
257 add_object(new CloudParticleSystem());
258 else if(particlesystem == "snow")
259 add_object(new SnowParticleSystem());
260 else if(particlesystem == "rain")
261 add_object(new RainParticleSystem());
263 Vector startpos(100, 170);
264 reader.get("start_pos_x", startpos.x);
265 reader.get("start_pos_y", startpos.y);
267 SpawnPoint* spawn = new SpawnPoint;
268 spawn->pos = startpos;
269 spawn->name = "main";
270 spawnpoints.push_back(spawn);
272 music = "chipdisko.ogg";
273 reader.get("music", music);
274 music = "music/" + music;
276 int width = 30, height = 15;
277 reader.get("width", width);
278 reader.get("height", height);
280 std::vector<unsigned int> tiles;
281 if(reader.get_vector("interactive-tm", tiles)
282 || reader.get_vector("tilemap", tiles)) {
283 TileMap* tilemap = new TileMap();
284 tilemap->set(width, height, tiles, LAYER_TILES, true);
288 if(reader.get_vector("background-tm", tiles)) {
289 TileMap* tilemap = new TileMap();
290 tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
294 if(reader.get_vector("foreground-tm", tiles)) {
295 TileMap* tilemap = new TileMap();
296 tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
300 // read reset-points (now spawn-points)
301 const lisp::Lisp* resetpoints = reader.get_lisp("reset-points");
303 lisp::ListIterator iter(resetpoints);
305 if(iter.item() == "point") {
307 if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
309 SpawnPoint* sp = new SpawnPoint;
312 spawnpoints.push_back(sp);
315 log_warning << "Unknown token '" << iter.item() << "' in reset-points." << std::endl;
321 const lisp::Lisp* objects = reader.get_lisp("objects");
323 lisp::ListIterator iter(objects);
325 GameObject* object = parse_object(iter.item(), *(iter.lisp()));
329 log_warning << "Unknown object '" << iter.item() << "' in level." << std::endl;
335 Camera* camera = new Camera(this, "Camera");
338 update_game_objects();
340 if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl;
343 update_game_objects();
347 Sector::fix_old_tiles()
349 for(std::list<TileMap*>::iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
350 TileMap* solids = *i;
351 for(size_t x=0; x < solids->get_width(); ++x) {
352 for(size_t y=0; y < solids->get_height(); ++y) {
353 const Tile* tile = solids->get_tile(x, y);
354 Vector pos(solids->get_x_offset() + x*32, solids->get_y_offset() + y*32);
356 if(tile->getID() == 112) {
357 add_object(new InvisibleBlock(pos));
358 solids->change(x, y, 0);
359 } else if(tile->getAttributes() & Tile::COIN) {
360 add_object(new Coin(pos));
361 solids->change(x, y, 0);
362 } else if(tile->getAttributes() & Tile::FULLBOX) {
363 add_object(new BonusBlock(pos, tile->getData()));
364 solids->change(x, y, 0);
365 } else if(tile->getAttributes() & Tile::BRICK) {
366 add_object(new Brick(pos, tile->getData()));
367 solids->change(x, y, 0);
368 } else if(tile->getAttributes() & Tile::GOAL) {
369 std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
370 add_object(new SequenceTrigger(pos, sequence));
371 solids->change(x, y, 0);
379 Sector::write(lisp::Writer& writer)
381 writer.write_string("name", name);
382 writer.write_float("gravity", gravity);
383 writer.write_string("music", music);
386 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
388 SpawnPoint* spawn = *i;
389 writer.start_list("spawn-points");
390 writer.write_string("name", spawn->name);
391 writer.write_float("x", spawn->pos.x);
392 writer.write_float("y", spawn->pos.y);
393 writer.end_list("spawn-points");
397 for(GameObjects::iterator i = gameobjects.begin();
398 i != gameobjects.end(); ++i) {
399 Serializable* serializable = dynamic_cast<Serializable*> (*i);
401 serializable->write(writer);
406 Sector::run_script(std::istream& in, const std::string& sourcename)
408 using namespace Scripting;
410 // garbage collect thread list
411 for(ScriptList::iterator i = scripts.begin();
412 i != scripts.end(); ) {
413 HSQOBJECT& object = *i;
414 HSQUIRRELVM vm = object_to_vm(object);
416 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
417 sq_release(global_vm, &object);
418 i = scripts.erase(i);
425 HSQOBJECT object = create_thread(global_vm);
426 scripts.push_back(object);
428 HSQUIRRELVM vm = object_to_vm(object);
430 // set sector_table as roottable for the thread
431 sq_pushobject(vm, sector_table);
434 compile_and_run(vm, in, sourcename);
440 Sector::add_object(GameObject* object)
442 // make sure the object isn't already in the list
444 for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
447 assert("object already added to sector" == 0);
450 for(GameObjects::iterator i = gameobjects_new.begin();
451 i != gameobjects_new.end(); ++i) {
453 assert("object already added to sector" == 0);
459 gameobjects_new.push_back(object);
463 Sector::activate(const std::string& spawnpoint)
466 for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
468 if((*i)->name == spawnpoint) {
474 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
475 if(spawnpoint != "main") {
478 activate(Vector(0, 0));
486 Sector::activate(const Vector& player_pos)
488 if(_current != this) {
490 _current->deactivate();
493 // register sectortable as sector in scripting
494 HSQUIRRELVM vm = Scripting::global_vm;
495 sq_pushroottable(vm);
496 sq_pushstring(vm, "sector", -1);
497 sq_pushobject(vm, sector_table);
498 if(SQ_FAILED(sq_createslot(vm, -3)))
499 throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable");
502 for(GameObjects::iterator i = gameobjects.begin();
503 i != gameobjects.end(); ++i) {
504 GameObject* object = *i;
510 player->move(player_pos);
511 camera->reset(player->get_pos());
512 update_game_objects();
515 if(init_script != "") {
516 std::istringstream in(init_script);
517 run_script(in, std::string("Sector(") + name + ") - init");
527 // remove sector entry from global vm
528 HSQUIRRELVM vm = Scripting::global_vm;
529 sq_pushroottable(vm);
530 sq_pushstring(vm, "sector", -1);
531 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
532 throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
535 for(GameObjects::iterator i = gameobjects.begin();
536 i != gameobjects.end(); ++i) {
537 GameObject* object = *i;
539 try_unexpose(object);
546 Sector::get_active_region()
549 camera->get_translation() - Vector(1600, 1200),
550 camera->get_translation() + Vector(1600, 1200));
554 Sector::update(float elapsed_time)
556 player->check_bounds(camera);
559 for(GameObjects::iterator i = gameobjects.begin();
560 i != gameobjects.end(); ++i) {
561 GameObject* object = *i;
562 if(!object->is_valid())
565 object->update(elapsed_time);
568 /* Handle all possible collisions. */
570 update_game_objects();
574 Sector::update_game_objects()
576 /** cleanup marked objects */
577 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
578 i != gameobjects.end(); /* nothing */) {
579 GameObject* object = *i;
581 if(object->is_valid()) {
586 before_object_remove(object);
589 i = gameobjects.erase(i);
592 /* add newly created objects */
593 for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
594 i != gameobjects_new.end(); ++i)
596 GameObject* object = *i;
598 before_object_add(object);
600 gameobjects.push_back(object);
602 gameobjects_new.clear();
606 Sector::before_object_add(GameObject* object)
608 Bullet* bullet = dynamic_cast<Bullet*> (object);
610 bullets.push_back(bullet);
613 MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
614 if(movingobject != NULL) {
615 moving_objects.push_back(movingobject);
618 Portable* portable = dynamic_cast<Portable*> (object);
619 if(portable != NULL) {
620 portables.push_back(portable);
623 TileMap* tilemap = dynamic_cast<TileMap*> (object);
624 if(tilemap != NULL && tilemap->is_solid()) {
625 solid_tilemaps.push_back(tilemap);
628 Camera* camera = dynamic_cast<Camera*> (object);
630 if(this->camera != 0) {
631 log_warning << "Multiple cameras added. Ignoring" << std::endl;
634 this->camera = camera;
637 Player* player = dynamic_cast<Player*> (object);
639 if(this->player != 0) {
640 log_warning << "Multiple players added. Ignoring" << std::endl;
643 this->player = player;
646 if(_current == this) {
654 Sector::try_expose(GameObject* object)
656 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
657 if(interface != NULL) {
658 HSQUIRRELVM vm = Scripting::global_vm;
659 sq_pushobject(vm, sector_table);
660 interface->expose(vm, -1);
666 Sector::before_object_remove(GameObject* object)
668 Portable* portable = dynamic_cast<Portable*> (object);
669 if(portable != NULL) {
670 portables.erase(std::find(portables.begin(), portables.end(), portable));
672 Bullet* bullet = dynamic_cast<Bullet*> (object);
674 bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));
676 MovingObject* moving_object = dynamic_cast<MovingObject*> (object);
677 if(moving_object != NULL) {
678 moving_objects.erase(
679 std::find(moving_objects.begin(), moving_objects.end(), moving_object));
683 try_unexpose(object);
687 Sector::try_unexpose(GameObject* object)
689 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
690 if(interface != NULL) {
691 HSQUIRRELVM vm = Scripting::global_vm;
692 SQInteger oldtop = sq_gettop(vm);
693 sq_pushobject(vm, sector_table);
695 interface->unexpose(vm, -1);
696 } catch(std::exception& e) {
697 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
699 sq_settop(vm, oldtop);
704 Sector::draw(DrawingContext& context)
706 context.set_ambient_color( ambient_light );
707 context.push_transform();
708 context.set_translation(camera->get_translation());
710 for(GameObjects::iterator i = gameobjects.begin();
711 i != gameobjects.end(); ++i) {
712 GameObject* object = *i;
713 if(!object->is_valid())
716 if (draw_solids_only)
718 TileMap* tm = dynamic_cast<TileMap*>(object);
719 if (tm && !tm->is_solid())
723 object->draw(context);
727 Color col(0.2, 0.2, 0.2, 0.7);
728 for(MovingObjects::iterator i = moving_objects.begin();
729 i != moving_objects.end(); ++i) {
730 MovingObject* object = *i;
731 const Rect& rect = object->get_bbox();
733 context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10);
737 context.pop_transform();
740 /*-------------------------------------------------------------------------
741 * Collision Detection
742 *-------------------------------------------------------------------------*/
744 static const float SHIFT_DELTA = 7.0f;
746 /** r1 is supposed to be moving, r2 a solid object */
747 void check_collisions(collision::Constraints* constraints,
748 const Vector& movement, const Rect& r1, const Rect& r2,
749 GameObject* object = NULL, MovingObject* other = NULL)
751 if(!collision::intersects(r1, r2))
754 // calculate intersection
755 float itop = r1.get_bottom() - r2.get_top();
756 float ibottom = r2.get_bottom() - r1.get_top();
757 float ileft = r1.get_right() - r2.get_left();
758 float iright = r2.get_right() - r1.get_left();
760 if(fabsf(movement.y) > fabsf(movement.x)) {
761 if(ileft < SHIFT_DELTA) {
762 constraints->right = std::min(constraints->right, r2.get_left());
764 } else if(iright < SHIFT_DELTA) {
765 constraints->left = std::max(constraints->left, r2.get_right());
769 // shiftout bottom/top
770 if(itop < SHIFT_DELTA) {
771 constraints->bottom = std::min(constraints->bottom, r2.get_top());
773 } else if(ibottom < SHIFT_DELTA) {
774 constraints->top = std::max(constraints->top, r2.get_bottom());
781 HitResponse response = other->collision(*object, dummy);
782 if(response == PASSTHROUGH)
784 if(other->get_movement() != Vector(0, 0)) {
785 // TODO what todo when we collide with 2 moving objects?!?
786 constraints->ground_movement = other->get_movement();
790 float vert_penetration = std::min(itop, ibottom);
791 float horiz_penetration = std::min(ileft, iright);
792 if(vert_penetration < horiz_penetration) {
794 constraints->bottom = std::min(constraints->bottom, r2.get_top());
795 constraints->hit.bottom = true;
797 constraints->top = std::max(constraints->top, r2.get_bottom());
798 constraints->hit.top = true;
802 constraints->right = std::min(constraints->right, r2.get_left());
803 constraints->hit.right = true;
805 constraints->left = std::max(constraints->left, r2.get_right());
806 constraints->hit.left = true;
811 static const float DELTA = .001;
814 Sector::collision_tilemap(collision::Constraints* constraints,
815 const Vector& movement, const Rect& dest) const
817 // calculate rectangle where the object will move
818 float x1 = dest.get_left();
819 float x2 = dest.get_right();
820 float y1 = dest.get_top();
821 float y2 = dest.get_bottom();
823 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
824 TileMap* solids = *i;
826 // test with all tiles in this rectangle
827 int starttilex = int(x1 - solids->get_x_offset()) / 32;
828 int starttiley = int(y1 - solids->get_y_offset()) / 32;
829 int max_x = int(x2 - solids->get_x_offset());
830 int max_y = int(y2+1 - solids->get_y_offset());
832 for(int x = starttilex; x*32 < max_x; ++x) {
833 for(int y = starttiley; y*32 < max_y; ++y) {
834 const Tile* tile = solids->get_tile(x, y);
837 // skip non-solid tiles
838 if((tile->getAttributes() & Tile::SOLID) == 0)
840 // only handle unisolid when the player is falling down and when he was
841 // above the tile before
842 if(tile->getAttributes() & Tile::UNISOLID) {
843 if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32)
847 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
849 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
850 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
851 triangle = AATriangle(p1, p2, tile->getData());
853 collision::rectangle_aatriangle(constraints, dest, triangle);
854 } else { // normal rectangular tile
855 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());
856 check_collisions(constraints, movement, dest, rect);
864 Sector::collision_tile_attributes(const Rect& dest) const
866 float x1 = dest.p1.x;
867 float y1 = dest.p1.y;
868 float x2 = dest.p2.x;
869 float y2 = dest.p2.y;
872 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
873 TileMap* solids = *i;
875 // test with all tiles in this rectangle
876 int starttilex = int(x1 - solids->get_x_offset()) / 32;
877 int starttiley = int(y1 - solids->get_y_offset()) / 32;
878 int max_x = int(x2 - solids->get_x_offset());
879 int max_y = int(y2+1 - solids->get_y_offset());
881 for(int x = starttilex; x*32 < max_x; ++x) {
882 for(int y = starttiley; y*32 < max_y; ++y) {
883 const Tile* tile = solids->get_tile(x, y);
886 result |= tile->getAttributes();
894 /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */
895 static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit,
898 float itop = r1.get_bottom() - r2.get_top();
899 float ibottom = r2.get_bottom() - r1.get_top();
900 float ileft = r1.get_right() - r2.get_left();
901 float iright = r2.get_right() - r1.get_left();
903 float vert_penetration = std::min(itop, ibottom);
904 float horiz_penetration = std::min(ileft, iright);
905 if(vert_penetration < horiz_penetration) {
908 normal.y = vert_penetration;
911 normal.y = -vert_penetration;
916 normal.x = horiz_penetration;
919 normal.x = -horiz_penetration;
925 Sector::collision_object(MovingObject* object1, MovingObject* object2) const
927 using namespace collision;
929 const Rect& r1 = object1->dest;
930 const Rect& r2 = object2->dest;
933 if(intersects(object1->dest, object2->dest)) {
935 get_hit_normal(r1, r2, hit, normal);
937 HitResponse response1 = object1->collision(*object2, hit);
938 std::swap(hit.left, hit.right);
939 std::swap(hit.top, hit.bottom);
940 HitResponse response2 = object2->collision(*object1, hit);
941 assert( response1 != SOLID && response1 != PASSTHROUGH );
942 assert( response2 != SOLID && response2 != PASSTHROUGH );
943 if(response1 == CONTINUE && response2 == CONTINUE) {
944 normal *= (0.5 + DELTA);
945 object1->dest.move(-normal);
946 object2->dest.move(normal);
947 } else if (response1 == CONTINUE && response2 == FORCE_MOVE) {
948 normal *= (1 + DELTA);
949 object1->dest.move(-normal);
950 } else if (response1 == FORCE_MOVE && response2 == CONTINUE) {
951 normal *= (1 + DELTA);
952 object2->dest.move(normal);
958 Sector::collision_static(collision::Constraints* constraints,
959 const Vector& movement, const Rect& dest,
962 collision_tilemap(constraints, movement, dest);
964 // collision with other (static) objects
965 for(MovingObjects::iterator i = moving_objects.begin();
966 i != moving_objects.end(); ++i) {
967 MovingObject* moving_object = *i;
968 if(moving_object->get_group() != COLGROUP_STATIC
969 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
971 if(!moving_object->is_valid())
974 if(moving_object != &object)
975 check_collisions(constraints, movement, dest, moving_object->bbox,
976 &object, moving_object);
981 Sector::collision_static_constrains(MovingObject& object)
983 using namespace collision;
985 Constraints constraints;
986 Vector movement = object.get_movement();
987 Rect& dest = object.dest;
988 float owidth = object.get_bbox().get_width();
989 float oheight = object.get_bbox().get_height();
991 for(int i = 0; i < 2; ++i) {
992 collision_static(&constraints, Vector(0, movement.y), dest, object);
993 if(!constraints.has_constraints())
996 // apply calculated horizontal constraints
997 if(constraints.bottom < INFINITY) {
998 float height = constraints.bottom - constraints.top;
999 if(height < oheight) {
1000 // we're crushed, but ignore this for now, we'll get this again
1001 // later if we're really crushed or things will solve itself when
1002 // looking at the vertical constraints
1004 dest.p2.y = constraints.bottom - DELTA;
1005 dest.p1.y = dest.p2.y - oheight;
1006 } else if(constraints.top > -INFINITY) {
1007 dest.p1.y = constraints.top + DELTA;
1008 dest.p2.y = dest.p1.y + oheight;
1011 if(constraints.has_constraints()) {
1012 if(constraints.hit.bottom) {
1013 dest.move(constraints.ground_movement);
1015 if(constraints.hit.top || constraints.hit.bottom) {
1016 constraints.hit.left = false;
1017 constraints.hit.right = false;
1018 object.collision_solid(constraints.hit);
1022 constraints = Constraints();
1023 for(int i = 0; i < 2; ++i) {
1024 collision_static(&constraints, movement, dest, object);
1025 if(!constraints.has_constraints())
1028 // apply calculated vertical constraints
1029 if(constraints.right < INFINITY) {
1030 float width = constraints.right - constraints.left;
1031 if(width + SHIFT_DELTA < owidth) {
1032 printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
1033 constraints.left, constraints.right);
1038 object.collision_solid(h);
1040 dest.p2.x = constraints.right - DELTA;
1041 dest.p1.x = dest.p2.x - owidth;
1043 } else if(constraints.left > -INFINITY) {
1044 dest.p1.x = constraints.left + DELTA;
1045 dest.p2.x = dest.p1.x + owidth;
1049 if(constraints.has_constraints()) {
1050 if( constraints.hit.left || constraints.hit.right
1051 || constraints.hit.top || constraints.hit.bottom
1052 || constraints.hit.crush )
1053 object.collision_solid(constraints.hit);
1056 // an extra pass to make sure we're not crushed horizontally
1057 constraints = Constraints();
1058 collision_static(&constraints, movement, dest, object);
1059 if(constraints.bottom < INFINITY) {
1060 float height = constraints.bottom - constraints.top;
1061 if(height + SHIFT_DELTA < oheight) {
1062 printf("Object %p crushed vertically...\n", &object);
1067 object.collision_solid(h);
1073 Sector::handle_collisions()
1075 using namespace collision;
1077 // calculate destination positions of the objects
1078 for(MovingObjects::iterator i = moving_objects.begin();
1079 i != moving_objects.end(); ++i) {
1080 MovingObject* moving_object = *i;
1082 moving_object->dest = moving_object->get_bbox();
1083 moving_object->dest.move(moving_object->get_movement());
1086 // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
1087 for(MovingObjects::iterator i = moving_objects.begin();
1088 i != moving_objects.end(); ++i) {
1089 MovingObject* moving_object = *i;
1090 if((moving_object->get_group() != COLGROUP_MOVING
1091 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1092 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1093 || !moving_object->is_valid())
1096 collision_static_constrains(*moving_object);
1100 // part2: COLGROUP_MOVING vs tile attributes
1101 for(MovingObjects::iterator i = moving_objects.begin();
1102 i != moving_objects.end(); ++i) {
1103 MovingObject* moving_object = *i;
1104 if((moving_object->get_group() != COLGROUP_MOVING
1105 && moving_object->get_group() != COLGROUP_MOVING_STATIC
1106 && moving_object->get_group() != COLGROUP_MOVING_ONLY_STATIC)
1107 || !moving_object->is_valid())
1110 uint32_t tile_attributes = collision_tile_attributes(moving_object->dest);
1111 if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) {
1112 moving_object->collision_tile(tile_attributes);
1116 // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
1117 for(MovingObjects::iterator i = moving_objects.begin();
1118 i != moving_objects.end(); ++i) {
1119 MovingObject* moving_object = *i;
1120 if((moving_object->get_group() != COLGROUP_MOVING
1121 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1122 || !moving_object->is_valid())
1125 for(MovingObjects::iterator i2 = moving_objects.begin();
1126 i2 != moving_objects.end(); ++i2) {
1127 MovingObject* moving_object_2 = *i2;
1128 if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
1129 || !moving_object_2->is_valid())
1132 if(intersects(moving_object->dest, moving_object_2->dest)) {
1135 get_hit_normal(moving_object->dest, moving_object_2->dest,
1137 moving_object->collision(*moving_object_2, hit);
1138 moving_object_2->collision(*moving_object, hit);
1143 // part3: COLGROUP_MOVING vs COLGROUP_MOVING
1144 for(MovingObjects::iterator i = moving_objects.begin();
1145 i != moving_objects.end(); ++i) {
1146 MovingObject* moving_object = *i;
1148 if((moving_object->get_group() != COLGROUP_MOVING
1149 && moving_object->get_group() != COLGROUP_MOVING_STATIC)
1150 || !moving_object->is_valid())
1153 for(MovingObjects::iterator i2 = i+1;
1154 i2 != moving_objects.end(); ++i2) {
1155 MovingObject* moving_object_2 = *i2;
1156 if((moving_object_2->get_group() != COLGROUP_MOVING
1157 && moving_object_2->get_group() != COLGROUP_MOVING_STATIC)
1158 || !moving_object_2->is_valid())
1161 collision_object(moving_object, moving_object_2);
1165 // apply object movement
1166 for(MovingObjects::iterator i = moving_objects.begin();
1167 i != moving_objects.end(); ++i) {
1168 MovingObject* moving_object = *i;
1170 moving_object->bbox = moving_object->dest;
1171 moving_object->movement = Vector(0, 0);
1176 Sector::is_free_space(const Rect& rect) const
1178 using namespace collision;
1180 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1181 TileMap* solids = *i;
1183 // test with all tiles in this rectangle
1184 int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32;
1185 int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32;
1186 int max_x = int(rect.p2.x - solids->get_x_offset());
1187 int max_y = int(rect.p2.y - solids->get_y_offset());
1189 for(int x = starttilex; x*32 <= max_x; ++x) {
1190 for(int y = starttiley; y*32 <= max_y; ++y) {
1191 const Tile* tile = solids->get_tile(x, y);
1193 if(tile->getAttributes() & Tile::SLOPE) {
1194 AATriangle triangle;
1195 Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset());
1196 Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset());
1197 triangle = AATriangle(p1, p2, tile->getData());
1198 Constraints constraints;
1199 return collision::rectangle_aatriangle(&constraints, rect, triangle);
1201 if(tile->getAttributes() & Tile::SOLID) return false;
1206 for(MovingObjects::const_iterator i = moving_objects.begin();
1207 i != moving_objects.end(); ++i) {
1208 const MovingObject* moving_object = *i;
1209 if(moving_object->get_group() != COLGROUP_STATIC
1210 || !moving_object->is_valid())
1213 if(intersects(rect, moving_object->get_bbox()))
1221 Sector::add_bullet(const Vector& pos, float xm, Direction dir)
1223 // TODO remove this function and move these checks elsewhere...
1225 Bullet* new_bullet = 0;
1226 if((int)bullets.size() >= player_status->max_fire_bullets)
1228 new_bullet = new Bullet(pos, xm, dir);
1229 add_object(new_bullet);
1231 sound_manager->play("sounds/shoot.wav");
1237 Sector::add_smoke_cloud(const Vector& pos)
1239 add_object(new SmokeCloud(pos));
1244 Sector::play_music(MusicType type)
1246 currentmusic = type;
1247 switch(currentmusic) {
1249 sound_manager->play_music(music);
1252 sound_manager->play_music("music/salcon.ogg");
1254 case HERRING_WARNING_MUSIC:
1255 sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING);
1258 sound_manager->play_music("");
1264 Sector::get_music_type()
1266 return currentmusic;
1270 Sector::get_total_badguys()
1272 int total_badguys = 0;
1273 for(GameObjects::iterator i = gameobjects.begin();
1274 i != gameobjects.end(); ++i) {
1275 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
1276 if (badguy && badguy->countMe)
1280 return total_badguys;
1284 Sector::inside(const Rect& rect) const
1286 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1287 TileMap* solids = *i;
1288 bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
1289 bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
1290 if (horizontally && vertically) return true;
1296 Sector::get_width() const
1299 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1300 TileMap* solids = *i;
1301 if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset());
1307 Sector::get_height() const
1310 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1311 TileMap* solids = *i;
1312 if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset());
1318 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
1320 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1321 TileMap* solids = *i;
1322 solids->change_all(old_tile_id, new_tile_id);