worldmap_sector.cpp 18 KB

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