worldmap_sector.cpp 19 KB

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