worldmap_sector.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. // SuperTux - A Jump'n Run
  2. // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmail.com>
  3. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  4. // 2023 Vankata453
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  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.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #include "worldmap/worldmap_sector.hpp"
  19. #include "audio/sound_manager.hpp"
  20. #include "object/ambient_light.hpp"
  21. #include "object/display_effect.hpp"
  22. #include "object/music_object.hpp"
  23. #include "object/tilemap.hpp"
  24. #include "physfs/ifile_stream.hpp"
  25. #include "scripting/worldmap_sector.hpp"
  26. #include "squirrel/squirrel_environment.hpp"
  27. #include "supertux/constants.hpp"
  28. #include "supertux/d_scope.hpp"
  29. #include "supertux/debug.hpp"
  30. #include "supertux/fadetoblack.hpp"
  31. #include "supertux/game_manager.hpp"
  32. #include "supertux/game_object_factory.hpp"
  33. #include "supertux/game_session.hpp"
  34. #include "supertux/gameconfig.hpp"
  35. #include "supertux/level.hpp"
  36. #include "supertux/player_status_hud.hpp"
  37. #include "supertux/resources.hpp"
  38. #include "supertux/screen_manager.hpp"
  39. #include "supertux/shrinkfade.hpp"
  40. #include "supertux/tile.hpp"
  41. #include "util/file_system.hpp"
  42. #include "worldmap/camera.hpp"
  43. #include "worldmap/level_tile.hpp"
  44. #include "worldmap/spawn_point.hpp"
  45. #include "worldmap/special_tile.hpp"
  46. #include "worldmap/teleporter.hpp"
  47. #include "worldmap/worldmap.hpp"
  48. namespace worldmap {
  49. WorldMapSector*
  50. WorldMapSector::current()
  51. {
  52. if (!WorldMap::current())
  53. return nullptr;
  54. return &WorldMap::current()->get_sector();
  55. }
  56. WorldMapSector::WorldMapSector(WorldMap& parent) :
  57. Base::Sector("worldmap"),
  58. m_parent(parent),
  59. m_camera(new Camera(*this)),
  60. m_tux(&add<Tux>(&parent)),
  61. m_spawnpoints(),
  62. m_initial_fade_tilemap(),
  63. m_fade_direction()
  64. {
  65. BIND_WORLDMAP_SECTOR(*this);
  66. add<PlayerStatusHUD>(m_parent.get_savegame().get_player_status());
  67. }
  68. WorldMapSector::~WorldMapSector()
  69. {
  70. m_spawnpoints.clear();
  71. clear_objects();
  72. }
  73. void
  74. WorldMapSector::finish_construction(bool editable)
  75. {
  76. flush_game_objects();
  77. if (!get_object_by_type<AmbientLight>())
  78. add<AmbientLight>(Color::WHITE);
  79. if (!get_object_by_type<MusicObject>())
  80. add<MusicObject>();
  81. if (!get_object_by_type<DisplayEffect>())
  82. add<DisplayEffect>("Effect");
  83. flush_game_objects();
  84. Base::Sector::finish_construction(editable);
  85. }
  86. void
  87. WorldMapSector::setup()
  88. {
  89. BIND_WORLDMAP_SECTOR(*this);
  90. ScreenManager::current()->set_screen_fade(std::make_unique<FadeToBlack>(FadeToBlack::FADEIN, 1.0f));
  91. // If we specified a fade tilemap, let's fade it:
  92. if (!m_initial_fade_tilemap.empty())
  93. {
  94. auto tilemap = get_object_by_name<TileMap>(m_initial_fade_tilemap);
  95. if (tilemap != nullptr)
  96. {
  97. if (m_fade_direction == 0)
  98. {
  99. tilemap->fade(1.0, 1);
  100. }
  101. else
  102. {
  103. tilemap->fade(0.0, 1);
  104. }
  105. }
  106. m_initial_fade_tilemap = "";
  107. }
  108. m_tux->setup();
  109. // register worldmap_table as "worldmap" in scripting
  110. m_squirrel_environment->expose_self();
  111. m_squirrel_environment->expose("settings", std::make_unique<scripting::WorldMapSector>(this));
  112. /** Perform scripting related actions. **/
  113. // Run default.nut just before init script
  114. try
  115. {
  116. IFileStream in(m_parent.get_levels_path() + "default.nut");
  117. m_squirrel_environment->run_script(in, "WorldMapSector::default.nut");
  118. }
  119. catch (...)
  120. {
  121. // doesn't exist or erroneous; do nothing
  122. }
  123. if (!m_init_script.empty())
  124. m_squirrel_environment->run_script(m_init_script, "WorldMapSector::init");
  125. // Check if Tux is on an auto-playing level.
  126. // No need to play music in that case.
  127. LevelTile* level = at_object<LevelTile>();
  128. if(level && level->is_auto_play() && !level->is_solved())
  129. return;
  130. auto& music_object = get_singleton_by_type<MusicObject>();
  131. music_object.play_music(MusicType::LEVEL_MUSIC);
  132. }
  133. void
  134. WorldMapSector::leave()
  135. {
  136. BIND_WORLDMAP_SECTOR(*this);
  137. // remove worldmap_table from roottable
  138. m_squirrel_environment->unexpose_self();
  139. }
  140. void
  141. WorldMapSector::draw(DrawingContext& context)
  142. {
  143. BIND_WORLDMAP_SECTOR(*this);
  144. if (get_width() < context.get_width() ||
  145. get_height() < context.get_height())
  146. {
  147. context.color().draw_filled_rect(context.get_rect(),
  148. Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
  149. }
  150. context.push_transform();
  151. context.set_translation(m_camera->get_offset());
  152. GameObjectManager::draw(context);
  153. if (g_debug.show_worldmap_path)
  154. {
  155. for (int x = 0; x < static_cast<int>(get_tiles_width()); x++) {
  156. for (int y = 0; y < static_cast<int>(get_tiles_height()); y++) {
  157. const int data = tile_data_at(Vector(static_cast<float>(x), static_cast<float>(y)));
  158. const int px = x * 32;
  159. const int py = y * 32;
  160. const int W = 4;
  161. const int layer = LAYER_FOREGROUND1 + 1000;
  162. const Color color(1.0f, 0.0f, 1.0f, 0.5f);
  163. if (data & Tile::WORLDMAP_NORTH) context.color().draw_filled_rect(Rect(px + 16-W, py , px + 16+W, py + 16-W), color, layer);
  164. if (data & Tile::WORLDMAP_SOUTH) context.color().draw_filled_rect(Rect(px + 16-W, py + 16+W, px + 16+W, py + 32 ), color, layer);
  165. if (data & Tile::WORLDMAP_EAST) context.color().draw_filled_rect(Rect(px + 16+W, py + 16-W, px + 32 , py + 16+W), color, layer);
  166. if (data & Tile::WORLDMAP_WEST) context.color().draw_filled_rect(Rect(px , py + 16-W, px + 16-W, py + 16+W), color, layer);
  167. 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);
  168. if (data & Tile::WORLDMAP_STOP) context.color().draw_filled_rect(Rect(px + 4 , py + 4 , px + 28 , py + 28 ), color, layer);
  169. }
  170. }
  171. }
  172. draw_status(context);
  173. }
  174. void
  175. WorldMapSector::draw_status(DrawingContext& context)
  176. {
  177. context.push_transform();
  178. context.set_translation(Vector(0, 0));
  179. if (!m_tux->is_moving()) {
  180. LevelTile* level = at_object<LevelTile>();
  181. if (level)
  182. {
  183. context.color().draw_text(Resources::normal_font, level->get_title(),
  184. Vector(context.get_width() / 2.0f,
  185. context.get_height() - Resources::normal_font->get_height() - 10),
  186. ALIGN_CENTER, LAYER_HUD, level->get_title_color());
  187. if (g_config->developer_mode) {
  188. context.color().draw_text(Resources::small_font, FileSystem::join(level->get_basedir(), level->get_level_filename()),
  189. Vector(context.get_width() / 2.0f,
  190. context.get_height() - Resources::normal_font->get_height() - 25),
  191. ALIGN_CENTER, LAYER_HUD, level->get_title_color());
  192. }
  193. // if level is solved, draw level picture behind stats
  194. /*
  195. if (level.solved) {
  196. if (const Surface* picture = level->get_picture()) {
  197. Vector pos = Vector(context.get_width() - picture->get_width(), context.get_height() - picture->get_height());
  198. context.push_transform();
  199. context.set_alpha(0.5);
  200. context.color().draw_surface(picture, pos, LAYER_FOREGROUND1-1);
  201. context.pop_transform();
  202. }
  203. }
  204. */
  205. level->get_statistics().draw_worldmap_info(context, level->get_target_time());
  206. }
  207. SpecialTile* special_tile = at_object<SpecialTile>();
  208. if (special_tile)
  209. {
  210. /* Display an in-map message in the map, if any as been selected */
  211. if (!special_tile->get_map_message().empty() && !special_tile->is_passive_message())
  212. context.color().draw_text(Resources::normal_font, special_tile->get_map_message(),
  213. Vector(context.get_width() / 2.0f,
  214. context.get_height() - static_cast<float>(Resources::normal_font->get_height()) - 60.0f),
  215. ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::s_message_color);
  216. }
  217. // display teleporter messages
  218. Teleporter* teleporter = at_object<Teleporter>();
  219. if (teleporter && (!teleporter->get_message().empty()))
  220. {
  221. Vector pos = Vector(context.get_width() / 2.0f,
  222. context.get_height() - Resources::normal_font->get_height() - 30.0f);
  223. context.color().draw_text(Resources::normal_font, teleporter->get_message(), pos, ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::s_teleporter_message_color);
  224. }
  225. }
  226. /* Display a passive message on the map, if set */
  227. if (m_parent.m_passive_message_timer.started())
  228. context.color().draw_text(Resources::normal_font, m_parent.m_passive_message,
  229. Vector(context.get_width() / 2.0f,
  230. context.get_height() - Resources::normal_font->get_height() - 60.0f),
  231. ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::s_message_color);
  232. context.pop_transform();
  233. }
  234. void
  235. WorldMapSector::update(float dt_sec)
  236. {
  237. BIND_WORLDMAP_SECTOR(*this);
  238. GameObjectManager::update(dt_sec);
  239. m_camera->update(dt_sec);
  240. {
  241. if(!m_tux->is_moving())
  242. {
  243. // check for teleporters
  244. auto teleporter = at_object<Teleporter>();
  245. if (teleporter && (teleporter->is_automatic() || (m_parent.m_enter_level))) {
  246. m_parent.m_enter_level = false;
  247. if (!teleporter->get_worldmap().empty())
  248. {
  249. // Change worldmap.
  250. m_parent.change(teleporter->get_worldmap(), teleporter->get_sector(),
  251. teleporter->get_spawnpoint());
  252. }
  253. else
  254. {
  255. // TODO: an animation, camera scrolling or a fading would be a nice touch
  256. SoundManager::current()->play("sounds/warp.wav");
  257. m_tux->m_back_direction = Direction::NONE;
  258. if (!teleporter->get_sector().empty())
  259. {
  260. // A target sector is set, so teleport to it at the specified spawnpoint.
  261. m_parent.set_sector(teleporter->get_sector(), teleporter->get_spawnpoint());
  262. }
  263. else
  264. {
  265. // No target sector is set, so teleport at the specified spawnpoint in the current one.
  266. move_to_spawnpoint(teleporter->get_spawnpoint(), true);
  267. }
  268. }
  269. }
  270. }
  271. }
  272. {
  273. if(!m_tux->is_moving())
  274. {
  275. // check for auto-play levels
  276. auto level = at_object<LevelTile>();
  277. if (level && level->is_auto_play() && !level->is_solved()) {
  278. m_parent.m_enter_level = true;
  279. // automatically mark these levels as solved in case player aborts
  280. level->set_solved(true);
  281. }
  282. }
  283. }
  284. {
  285. if (m_parent.m_enter_level && !m_tux->is_moving())
  286. {
  287. /* Check level action */
  288. auto level_ = at_object<LevelTile>();
  289. if (!level_) {
  290. //Respawn if player on a tile with no level and nowhere to go.
  291. int tile_data = tile_data_at(m_tux->get_tile_pos());
  292. if (!( tile_data & ( Tile::WORLDMAP_NORTH | Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
  293. log_warning << "Player at illegal position " << m_tux->get_tile_pos().x << ", " << m_tux->get_tile_pos().y << " respawning." << std::endl;
  294. move_to_spawnpoint(DEFAULT_SPAWNPOINT_NAME);
  295. return;
  296. }
  297. log_warning << "No level to enter at: " << m_tux->get_tile_pos().x << ", " << m_tux->get_tile_pos().y << std::endl;
  298. return;
  299. }
  300. if (level_->get_tile_pos() == m_tux->get_tile_pos()) {
  301. try {
  302. Vector shrinkpos = Vector(level_->get_pos().x + 16 - m_camera->get_offset().x,
  303. level_->get_pos().y + 8 - m_camera->get_offset().y);
  304. std::string levelfile = m_parent.m_levels_path + level_->get_level_filename();
  305. // update state and savegame
  306. m_parent.save_state();
  307. ScreenManager::current()->push_screen(std::make_unique<GameSession>(levelfile, m_parent.m_savegame, &level_->get_statistics()),
  308. std::make_unique<ShrinkFade>(shrinkpos, 1.0f, LAYER_LIGHTMAP - 1));
  309. m_parent.m_in_level = true;
  310. } catch(std::exception& e) {
  311. log_fatal << "Couldn't load level: " << e.what() << std::endl;
  312. }
  313. }
  314. }
  315. else
  316. {
  317. // tux->set_direction(input_direction);
  318. }
  319. }
  320. flush_game_objects();
  321. }
  322. MovingObject&
  323. WorldMapSector::add_object_scripting(const std::string& class_name, const std::string& name,
  324. const Vector& pos, const std::string& direction,
  325. const std::string& data)
  326. {
  327. if (!GameObjectFactory::instance().has_params(class_name, ObjectFactory::OBJ_PARAM_WORLDMAP))
  328. throw std::runtime_error("Object '" + class_name + "' cannot be added to a worldmap sector.");
  329. auto& obj = GameObjectManager::add_object_scripting(class_name, name, pos, direction, data);
  330. // Set position of non-WorldMapObjects from provided tile position.
  331. if (!dynamic_cast<WorldMapObject*>(&obj))
  332. obj.set_pos(obj.get_pos() * 32.f);
  333. return obj;
  334. }
  335. Vector
  336. WorldMapSector::get_next_tile(const Vector& pos, const Direction& direction) const
  337. {
  338. auto position = pos;
  339. switch (direction) {
  340. case Direction::WEST:
  341. position.x -= 1;
  342. break;
  343. case Direction::EAST:
  344. position.x += 1;
  345. break;
  346. case Direction::NORTH:
  347. position.y -= 1;
  348. break;
  349. case Direction::SOUTH:
  350. position.y += 1;
  351. break;
  352. case Direction::NONE:
  353. break;
  354. }
  355. return position;
  356. }
  357. int
  358. WorldMapSector::available_directions_at(const Vector& p) const
  359. {
  360. return tile_data_at(p) & Tile::WORLDMAP_DIR_MASK;
  361. }
  362. int
  363. WorldMapSector::tile_data_at(const Vector& p) const
  364. {
  365. int dirs = 0;
  366. for (const auto& tilemap : get_solid_tilemaps()) {
  367. const Tile& tile = tilemap->get_tile(static_cast<int>(p.x), static_cast<int>(p.y));
  368. int dirdata = tile.get_data();
  369. dirs |= dirdata;
  370. }
  371. return dirs;
  372. }
  373. size_t
  374. WorldMapSector::level_count() const
  375. {
  376. return get_object_count<LevelTile>();
  377. }
  378. size_t
  379. WorldMapSector::solved_level_count() const
  380. {
  381. size_t count = 0;
  382. for (auto& level : get_objects_by_type<LevelTile>()) {
  383. if (level.is_solved()) {
  384. count++;
  385. }
  386. }
  387. return count;
  388. }
  389. void
  390. WorldMapSector::finished_level(Level* gamelevel)
  391. {
  392. // TODO use Level* parameter here?
  393. auto level = at_object<LevelTile>();
  394. if (level == nullptr) {
  395. return;
  396. }
  397. bool old_level_state = level->is_solved();
  398. level->set_solved(true);
  399. // deal with statistics
  400. level->get_statistics().update(gamelevel->m_stats);
  401. if (level->get_statistics().completed(level->get_statistics(), level->get_target_time())) {
  402. level->set_perfect(true);
  403. }
  404. m_parent.save_state();
  405. if (old_level_state != level->is_solved()) {
  406. // Try to detect the next direction to which we should walk
  407. // FIXME: Mostly a hack
  408. Direction dir = Direction::NONE;
  409. int dirdata = available_directions_at(m_tux->get_tile_pos());
  410. // first, test for crossroads
  411. if (dirdata == Tile::WORLDMAP_CNSE ||
  412. dirdata == Tile::WORLDMAP_CNSW ||
  413. dirdata == Tile::WORLDMAP_CNEW ||
  414. dirdata == Tile::WORLDMAP_CSEW ||
  415. dirdata == Tile::WORLDMAP_CNSEW)
  416. dir = Direction::NONE;
  417. else if (dirdata & Tile::WORLDMAP_NORTH
  418. && m_tux->m_back_direction != Direction::NORTH)
  419. dir = Direction::NORTH;
  420. else if (dirdata & Tile::WORLDMAP_SOUTH
  421. && m_tux->m_back_direction != Direction::SOUTH)
  422. dir = Direction::SOUTH;
  423. else if (dirdata & Tile::WORLDMAP_EAST
  424. && m_tux->m_back_direction != Direction::EAST)
  425. dir = Direction::EAST;
  426. else if (dirdata & Tile::WORLDMAP_WEST
  427. && m_tux->m_back_direction != Direction::WEST)
  428. dir = Direction::WEST;
  429. if (dir != Direction::NONE) {
  430. m_tux->set_direction(dir);
  431. }
  432. }
  433. if (!level->get_extro_script().empty()) {
  434. try {
  435. run_script(level->get_extro_script(), "WorldMapSector:extro_script");
  436. } catch(std::exception& e) {
  437. log_warning << "Couldn't run level-extro-script: " << e.what() << std::endl;
  438. }
  439. }
  440. }
  441. SpawnPoint*
  442. WorldMapSector::get_spawnpoint_by_name(const std::string& spawnpoint_name) const
  443. {
  444. auto spawnpoint = std::find_if(m_spawnpoints.begin(), m_spawnpoints.end(),
  445. [spawnpoint_name](const auto& sp) {
  446. return sp->get_name() == spawnpoint_name;
  447. });
  448. return spawnpoint != m_spawnpoints.end() ? spawnpoint->get() : nullptr;
  449. }
  450. bool
  451. WorldMapSector::path_ok(const Direction& direction, const Vector& old_pos, Vector* new_pos) const
  452. {
  453. *new_pos = get_next_tile(old_pos, direction);
  454. if (!(new_pos->x >= 0 && new_pos->x < get_tiles_width()
  455. && new_pos->y >= 0 && new_pos->y < get_tiles_height()))
  456. { // New position is outsite the tilemap
  457. return false;
  458. }
  459. else
  460. { // Check if the tile allows us to go to new_pos
  461. int old_tile_data = tile_data_at(old_pos);
  462. int new_tile_data = tile_data_at(*new_pos);
  463. switch (direction)
  464. {
  465. case Direction::WEST:
  466. return (old_tile_data & Tile::WORLDMAP_WEST
  467. && new_tile_data & Tile::WORLDMAP_EAST);
  468. case Direction::EAST:
  469. return (old_tile_data & Tile::WORLDMAP_EAST
  470. && new_tile_data & Tile::WORLDMAP_WEST);
  471. case Direction::NORTH:
  472. return (old_tile_data & Tile::WORLDMAP_NORTH
  473. && new_tile_data & Tile::WORLDMAP_SOUTH);
  474. case Direction::SOUTH:
  475. return (old_tile_data & Tile::WORLDMAP_SOUTH
  476. && new_tile_data & Tile::WORLDMAP_NORTH);
  477. case Direction::NONE:
  478. log_warning << "path_ok() can't walk if direction is NONE" << std::endl;
  479. assert(false);
  480. }
  481. return false;
  482. }
  483. }
  484. void
  485. WorldMapSector::move_to_spawnpoint(const std::string& spawnpoint, bool pan)
  486. {
  487. auto sp = get_spawnpoint_by_name(spawnpoint);
  488. if (sp != nullptr) {
  489. Vector p = sp->get_pos();
  490. m_tux->set_tile_pos(p);
  491. m_tux->set_direction(sp->get_auto_dir());
  492. if (pan) {
  493. m_camera->pan();
  494. }
  495. return;
  496. }
  497. log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
  498. if (spawnpoint != DEFAULT_SPAWNPOINT_NAME) {
  499. move_to_spawnpoint(DEFAULT_SPAWNPOINT_NAME);
  500. }
  501. }
  502. void
  503. WorldMapSector::set_initial_fade_tilemap(const std::string& tilemap_name, int direction)
  504. {
  505. m_initial_fade_tilemap = tilemap_name;
  506. m_fade_direction = direction;
  507. }
  508. TileSet*
  509. WorldMapSector::get_tileset() const
  510. {
  511. return m_parent.m_tileset;
  512. }
  513. Vector
  514. WorldMapSector::get_tux_pos() const
  515. {
  516. return m_tux->get_pos();
  517. }
  518. } // namespace worldmap
  519. /* EOF */