123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- // SuperTux - A Jump'n Run
- // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmail.com>
- // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
- // 2023 Vankata453
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- #include "worldmap/worldmap_sector.hpp"
- #include "audio/sound_manager.hpp"
- #include "object/ambient_light.hpp"
- #include "object/display_effect.hpp"
- #include "object/music_object.hpp"
- #include "object/tilemap.hpp"
- #include "physfs/ifile_stream.hpp"
- #include "scripting/worldmap_sector.hpp"
- #include "squirrel/squirrel_environment.hpp"
- #include "supertux/constants.hpp"
- #include "supertux/d_scope.hpp"
- #include "supertux/debug.hpp"
- #include "supertux/fadetoblack.hpp"
- #include "supertux/game_manager.hpp"
- #include "supertux/game_object_factory.hpp"
- #include "supertux/game_session.hpp"
- #include "supertux/gameconfig.hpp"
- #include "supertux/level.hpp"
- #include "supertux/player_status_hud.hpp"
- #include "supertux/resources.hpp"
- #include "supertux/screen_manager.hpp"
- #include "supertux/shrinkfade.hpp"
- #include "supertux/tile.hpp"
- #include "util/file_system.hpp"
- #include "worldmap/camera.hpp"
- #include "worldmap/level_tile.hpp"
- #include "worldmap/spawn_point.hpp"
- #include "worldmap/special_tile.hpp"
- #include "worldmap/teleporter.hpp"
- #include "worldmap/worldmap.hpp"
- namespace worldmap {
- WorldMapSector*
- WorldMapSector::current()
- {
- if (!WorldMap::current())
- return nullptr;
- return &WorldMap::current()->get_sector();
- }
- WorldMapSector::WorldMapSector(WorldMap& parent) :
- Base::Sector("worldmap"),
- m_parent(parent),
- m_camera(new Camera(*this)),
- m_tux(&add<Tux>(&parent)),
- m_spawnpoints(),
- m_initial_fade_tilemap(),
- m_fade_direction()
- {
- BIND_WORLDMAP_SECTOR(*this);
- add<PlayerStatusHUD>(m_parent.get_savegame().get_player_status());
- }
- WorldMapSector::~WorldMapSector()
- {
- m_spawnpoints.clear();
- clear_objects();
- }
- void
- WorldMapSector::finish_construction(bool editable)
- {
- flush_game_objects();
- if (!get_object_by_type<AmbientLight>())
- add<AmbientLight>(Color::WHITE);
- if (!get_object_by_type<MusicObject>())
- add<MusicObject>();
- if (!get_object_by_type<DisplayEffect>())
- add<DisplayEffect>("Effect");
- flush_game_objects();
- Base::Sector::finish_construction(editable);
- }
- void
- WorldMapSector::setup()
- {
- BIND_WORLDMAP_SECTOR(*this);
- ScreenManager::current()->set_screen_fade(std::make_unique<FadeToBlack>(FadeToBlack::FADEIN, 1.0f));
- // If we specified a fade tilemap, let's fade it:
- if (!m_initial_fade_tilemap.empty())
- {
- auto tilemap = get_object_by_name<TileMap>(m_initial_fade_tilemap);
- if (tilemap != nullptr)
- {
- if (m_fade_direction == 0)
- {
- tilemap->fade(1.0, 1);
- }
- else
- {
- tilemap->fade(0.0, 1);
- }
- }
- m_initial_fade_tilemap = "";
- }
- m_tux->setup();
- // register worldmap_table as "worldmap" in scripting
- m_squirrel_environment->expose_self();
- m_squirrel_environment->expose("settings", std::make_unique<scripting::WorldMapSector>(this));
- /** Perform scripting related actions. **/
- // Run default.nut just before init script
- try
- {
- IFileStream in(m_parent.get_levels_path() + "default.nut");
- m_squirrel_environment->run_script(in, "WorldMapSector::default.nut");
- }
- catch (...)
- {
- // doesn't exist or erroneous; do nothing
- }
- if (!m_init_script.empty())
- m_squirrel_environment->run_script(m_init_script, "WorldMapSector::init");
- // Check if Tux is on an auto-playing level.
- // No need to play music in that case.
- LevelTile* level = at_object<LevelTile>();
- if(level && level->is_auto_play() && !level->is_solved())
- return;
- auto& music_object = get_singleton_by_type<MusicObject>();
- music_object.play_music(MusicType::LEVEL_MUSIC);
- }
- void
- WorldMapSector::leave()
- {
- BIND_WORLDMAP_SECTOR(*this);
- // remove worldmap_table from roottable
- m_squirrel_environment->unexpose_self();
- }
- void
- WorldMapSector::draw(DrawingContext& context)
- {
- BIND_WORLDMAP_SECTOR(*this);
- if (get_width() < context.get_width() ||
- get_height() < context.get_height())
- {
- context.color().draw_filled_rect(context.get_rect(),
- Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
- }
- context.push_transform();
- context.set_translation(m_camera->get_offset());
- GameObjectManager::draw(context);
- if (g_debug.show_worldmap_path)
- {
- for (int x = 0; x < static_cast<int>(get_tiles_width()); x++) {
- for (int y = 0; y < static_cast<int>(get_tiles_height()); y++) {
- const int data = tile_data_at(Vector(static_cast<float>(x), static_cast<float>(y)));
- const int px = x * 32;
- const int py = y * 32;
- const int W = 4;
- const int layer = LAYER_FOREGROUND1 + 1000;
- const Color color(1.0f, 0.0f, 1.0f, 0.5f);
- if (data & Tile::WORLDMAP_NORTH) context.color().draw_filled_rect(Rect(px + 16-W, py , px + 16+W, py + 16-W), color, layer);
- if (data & Tile::WORLDMAP_SOUTH) context.color().draw_filled_rect(Rect(px + 16-W, py + 16+W, px + 16+W, py + 32 ), color, layer);
- if (data & Tile::WORLDMAP_EAST) context.color().draw_filled_rect(Rect(px + 16+W, py + 16-W, px + 32 , py + 16+W), color, layer);
- if (data & Tile::WORLDMAP_WEST) context.color().draw_filled_rect(Rect(px , py + 16-W, px + 16-W, py + 16+W), color, layer);
- if (data & Tile::WORLDMAP_DIR_MASK) context.color().draw_filled_rect(Rect(px + 16-W, py + 16-W, px + 16+W, py + 16+W), color, layer);
- if (data & Tile::WORLDMAP_STOP) context.color().draw_filled_rect(Rect(px + 4 , py + 4 , px + 28 , py + 28 ), color, layer);
- }
- }
- }
- draw_status(context);
- }
- void
- WorldMapSector::draw_status(DrawingContext& context)
- {
- context.push_transform();
- context.set_translation(Vector(0, 0));
- if (!m_tux->is_moving()) {
- LevelTile* level = at_object<LevelTile>();
- if (level)
- {
- context.color().draw_text(Resources::normal_font, level->get_title(),
- Vector(context.get_width() / 2.0f,
- context.get_height() - Resources::normal_font->get_height() - 10),
- ALIGN_CENTER, LAYER_HUD, level->get_title_color());
- if (g_config->developer_mode) {
- context.color().draw_text(Resources::small_font, FileSystem::join(level->get_basedir(), level->get_level_filename()),
- Vector(context.get_width() / 2.0f,
- context.get_height() - Resources::normal_font->get_height() - 25),
- ALIGN_CENTER, LAYER_HUD, level->get_title_color());
- }
- // if level is solved, draw level picture behind stats
- /*
- if (level.solved) {
- if (const Surface* picture = level->get_picture()) {
- Vector pos = Vector(context.get_width() - picture->get_width(), context.get_height() - picture->get_height());
- context.push_transform();
- context.set_alpha(0.5);
- context.color().draw_surface(picture, pos, LAYER_FOREGROUND1-1);
- context.pop_transform();
- }
- }
- */
- level->get_statistics().draw_worldmap_info(context, level->get_target_time());
- }
- SpecialTile* special_tile = at_object<SpecialTile>();
- if (special_tile)
- {
- /* Display an in-map message in the map, if any as been selected */
- if (!special_tile->get_map_message().empty() && !special_tile->is_passive_message())
- context.color().draw_text(Resources::normal_font, special_tile->get_map_message(),
- Vector(context.get_width() / 2.0f,
- context.get_height() - static_cast<float>(Resources::normal_font->get_height()) - 60.0f),
- ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::s_message_color);
- }
- // display teleporter messages
- Teleporter* teleporter = at_object<Teleporter>();
- if (teleporter && (!teleporter->get_message().empty()))
- {
- Vector pos = Vector(context.get_width() / 2.0f,
- context.get_height() - Resources::normal_font->get_height() - 30.0f);
- context.color().draw_text(Resources::normal_font, teleporter->get_message(), pos, ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::s_teleporter_message_color);
- }
- }
- /* Display a passive message on the map, if set */
- if (m_parent.m_passive_message_timer.started())
- context.color().draw_text(Resources::normal_font, m_parent.m_passive_message,
- Vector(context.get_width() / 2.0f,
- context.get_height() - Resources::normal_font->get_height() - 60.0f),
- ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::s_message_color);
- context.pop_transform();
- }
- void
- WorldMapSector::update(float dt_sec)
- {
- BIND_WORLDMAP_SECTOR(*this);
- GameObjectManager::update(dt_sec);
- m_camera->update(dt_sec);
- {
- if(!m_tux->is_moving())
- {
- // check for teleporters
- auto teleporter = at_object<Teleporter>();
- if (teleporter && (teleporter->is_automatic() || (m_parent.m_enter_level))) {
- m_parent.m_enter_level = false;
- if (!teleporter->get_worldmap().empty())
- {
- // Change worldmap.
- m_parent.change(teleporter->get_worldmap(), teleporter->get_sector(),
- teleporter->get_spawnpoint());
- }
- else
- {
- // TODO: an animation, camera scrolling or a fading would be a nice touch
- SoundManager::current()->play("sounds/warp.wav");
- m_tux->m_back_direction = Direction::NONE;
- if (!teleporter->get_sector().empty())
- {
- // A target sector is set, so teleport to it at the specified spawnpoint.
- m_parent.set_sector(teleporter->get_sector(), teleporter->get_spawnpoint());
- }
- else
- {
- // No target sector is set, so teleport at the specified spawnpoint in the current one.
- move_to_spawnpoint(teleporter->get_spawnpoint(), true);
- }
- }
- }
- }
- }
- {
- if(!m_tux->is_moving())
- {
- // check for auto-play levels
- auto level = at_object<LevelTile>();
- if (level && level->is_auto_play() && !level->is_solved()) {
- m_parent.m_enter_level = true;
- // automatically mark these levels as solved in case player aborts
- level->set_solved(true);
- }
- }
- }
- {
- if (m_parent.m_enter_level && !m_tux->is_moving())
- {
- /* Check level action */
- auto level_ = at_object<LevelTile>();
- if (!level_) {
- //Respawn if player on a tile with no level and nowhere to go.
- int tile_data = tile_data_at(m_tux->get_tile_pos());
- if (!( tile_data & ( Tile::WORLDMAP_NORTH | Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
- log_warning << "Player at illegal position " << m_tux->get_tile_pos().x << ", " << m_tux->get_tile_pos().y << " respawning." << std::endl;
- move_to_spawnpoint(DEFAULT_SPAWNPOINT_NAME);
- return;
- }
- log_warning << "No level to enter at: " << m_tux->get_tile_pos().x << ", " << m_tux->get_tile_pos().y << std::endl;
- return;
- }
- if (level_->get_tile_pos() == m_tux->get_tile_pos()) {
- try {
- Vector shrinkpos = Vector(level_->get_pos().x + 16 - m_camera->get_offset().x,
- level_->get_pos().y + 8 - m_camera->get_offset().y);
- std::string levelfile = m_parent.m_levels_path + level_->get_level_filename();
- // update state and savegame
- m_parent.save_state();
- ScreenManager::current()->push_screen(std::make_unique<GameSession>(levelfile, m_parent.m_savegame, &level_->get_statistics()),
- std::make_unique<ShrinkFade>(shrinkpos, 1.0f, LAYER_LIGHTMAP - 1));
- m_parent.m_in_level = true;
- } catch(std::exception& e) {
- log_fatal << "Couldn't load level: " << e.what() << std::endl;
- }
- }
- }
- else
- {
- // tux->set_direction(input_direction);
- }
- }
- flush_game_objects();
- }
- MovingObject&
- WorldMapSector::add_object_scripting(const std::string& class_name, const std::string& name,
- const Vector& pos, const std::string& direction,
- const std::string& data)
- {
- if (!GameObjectFactory::instance().has_params(class_name, ObjectFactory::OBJ_PARAM_WORLDMAP))
- throw std::runtime_error("Object '" + class_name + "' cannot be added to a worldmap sector.");
- auto& obj = GameObjectManager::add_object_scripting(class_name, name, pos, direction, data);
- // Set position of non-WorldMapObjects from provided tile position.
- if (!dynamic_cast<WorldMapObject*>(&obj))
- obj.set_pos(obj.get_pos() * 32.f);
- return obj;
- }
- Vector
- WorldMapSector::get_next_tile(const Vector& pos, const Direction& direction) const
- {
- auto position = pos;
- switch (direction) {
- case Direction::WEST:
- position.x -= 1;
- break;
- case Direction::EAST:
- position.x += 1;
- break;
- case Direction::NORTH:
- position.y -= 1;
- break;
- case Direction::SOUTH:
- position.y += 1;
- break;
- case Direction::NONE:
- break;
- }
- return position;
- }
- int
- WorldMapSector::available_directions_at(const Vector& p) const
- {
- return tile_data_at(p) & Tile::WORLDMAP_DIR_MASK;
- }
- int
- WorldMapSector::tile_data_at(const Vector& p) const
- {
- int dirs = 0;
- for (const auto& tilemap : get_solid_tilemaps()) {
- const Tile& tile = tilemap->get_tile(static_cast<int>(p.x), static_cast<int>(p.y));
- int dirdata = tile.get_data();
- dirs |= dirdata;
- }
- return dirs;
- }
- size_t
- WorldMapSector::level_count() const
- {
- return get_object_count<LevelTile>();
- }
- size_t
- WorldMapSector::solved_level_count() const
- {
- size_t count = 0;
- for (auto& level : get_objects_by_type<LevelTile>()) {
- if (level.is_solved()) {
- count++;
- }
- }
- return count;
- }
- void
- WorldMapSector::finished_level(Level* gamelevel)
- {
- // TODO use Level* parameter here?
- auto level = at_object<LevelTile>();
- if (level == nullptr) {
- return;
- }
- bool old_level_state = level->is_solved();
- level->set_solved(true);
- // deal with statistics
- level->get_statistics().update(gamelevel->m_stats);
- if (level->get_statistics().completed(level->get_statistics(), level->get_target_time())) {
- level->set_perfect(true);
- }
- m_parent.save_state();
- if (old_level_state != level->is_solved()) {
- // Try to detect the next direction to which we should walk
- // FIXME: Mostly a hack
- Direction dir = Direction::NONE;
- int dirdata = available_directions_at(m_tux->get_tile_pos());
- // first, test for crossroads
- if (dirdata == Tile::WORLDMAP_CNSE ||
- dirdata == Tile::WORLDMAP_CNSW ||
- dirdata == Tile::WORLDMAP_CNEW ||
- dirdata == Tile::WORLDMAP_CSEW ||
- dirdata == Tile::WORLDMAP_CNSEW)
- dir = Direction::NONE;
- else if (dirdata & Tile::WORLDMAP_NORTH
- && m_tux->m_back_direction != Direction::NORTH)
- dir = Direction::NORTH;
- else if (dirdata & Tile::WORLDMAP_SOUTH
- && m_tux->m_back_direction != Direction::SOUTH)
- dir = Direction::SOUTH;
- else if (dirdata & Tile::WORLDMAP_EAST
- && m_tux->m_back_direction != Direction::EAST)
- dir = Direction::EAST;
- else if (dirdata & Tile::WORLDMAP_WEST
- && m_tux->m_back_direction != Direction::WEST)
- dir = Direction::WEST;
- if (dir != Direction::NONE) {
- m_tux->set_direction(dir);
- }
- }
- if (!level->get_extro_script().empty()) {
- try {
- run_script(level->get_extro_script(), "WorldMapSector:extro_script");
- } catch(std::exception& e) {
- log_warning << "Couldn't run level-extro-script: " << e.what() << std::endl;
- }
- }
- }
- SpawnPoint*
- WorldMapSector::get_spawnpoint_by_name(const std::string& spawnpoint_name) const
- {
- auto spawnpoint = std::find_if(m_spawnpoints.begin(), m_spawnpoints.end(),
- [spawnpoint_name](const auto& sp) {
- return sp->get_name() == spawnpoint_name;
- });
- return spawnpoint != m_spawnpoints.end() ? spawnpoint->get() : nullptr;
- }
- bool
- WorldMapSector::path_ok(const Direction& direction, const Vector& old_pos, Vector* new_pos) const
- {
- *new_pos = get_next_tile(old_pos, direction);
-
- if (!(new_pos->x >= 0 && new_pos->x < get_tiles_width()
- && new_pos->y >= 0 && new_pos->y < get_tiles_height()))
- { // New position is outsite the tilemap
- return false;
- }
- else
- { // Check if the tile allows us to go to new_pos
- int old_tile_data = tile_data_at(old_pos);
- int new_tile_data = tile_data_at(*new_pos);
- switch (direction)
- {
- case Direction::WEST:
- return (old_tile_data & Tile::WORLDMAP_WEST
- && new_tile_data & Tile::WORLDMAP_EAST);
- case Direction::EAST:
- return (old_tile_data & Tile::WORLDMAP_EAST
- && new_tile_data & Tile::WORLDMAP_WEST);
- case Direction::NORTH:
- return (old_tile_data & Tile::WORLDMAP_NORTH
- && new_tile_data & Tile::WORLDMAP_SOUTH);
- case Direction::SOUTH:
- return (old_tile_data & Tile::WORLDMAP_SOUTH
- && new_tile_data & Tile::WORLDMAP_NORTH);
- case Direction::NONE:
- log_warning << "path_ok() can't walk if direction is NONE" << std::endl;
- assert(false);
- }
- return false;
- }
- }
- void
- WorldMapSector::move_to_spawnpoint(const std::string& spawnpoint, bool pan)
- {
- auto sp = get_spawnpoint_by_name(spawnpoint);
- if (sp != nullptr) {
- Vector p = sp->get_pos();
- m_tux->set_tile_pos(p);
- m_tux->set_direction(sp->get_auto_dir());
- if (pan) {
- m_camera->pan();
- }
- return;
- }
- log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
- if (spawnpoint != DEFAULT_SPAWNPOINT_NAME) {
- move_to_spawnpoint(DEFAULT_SPAWNPOINT_NAME);
- }
- }
- void
- WorldMapSector::set_initial_fade_tilemap(const std::string& tilemap_name, int direction)
- {
- m_initial_fade_tilemap = tilemap_name;
- m_fade_direction = direction;
- }
- TileSet*
- WorldMapSector::get_tileset() const
- {
- return m_parent.m_tileset;
- }
- Vector
- WorldMapSector::get_tux_pos() const
- {
- return m_tux->get_pos();
- }
- } // namespace worldmap
- /* EOF */
|