Skip to content

Commit 9722a23

Browse files
authored
Fix broken "load_worldmap" and "set_next_worldmap" functions (#2665)
1 parent 9f88337 commit 9722a23

File tree

5 files changed

+65
-33
lines changed

5 files changed

+65
-33
lines changed

src/scripting/functions.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "video/viewport.hpp"
3838
#include "worldmap/tux.hpp"
3939
#include "worldmap/worldmap.hpp"
40-
#include "worldmap/worldmap_screen.hpp"
4140

4241
namespace {
4342

@@ -226,24 +225,23 @@ void display_text_file(const std::string& filename)
226225
ScreenManager::current()->push_screen(std::make_unique<TextScrollerScreen>(filename));
227226
}
228227

229-
void load_worldmap(const std::string& filename)
228+
void load_worldmap(const std::string& filename, const std::string& sector, const std::string& spawnpoint)
230229
{
231230
using namespace worldmap;
232231

233-
if (!::worldmap::WorldMap::current())
232+
if (!WorldMap::current())
234233
{
235234
throw std::runtime_error("Can't start Worldmap without active WorldMap");
236235
}
237236
else
238237
{
239-
ScreenManager::current()->push_screen(std::make_unique<WorldMapScreen>(
240-
std::make_unique<::worldmap::WorldMap>(filename, ::worldmap::WorldMap::current()->get_savegame())));
238+
WorldMap::current()->change(filename, sector, spawnpoint);
241239
}
242240
}
243241

244-
void set_next_worldmap(const std::string& dirname, const std::string& spawnpoint)
242+
void set_next_worldmap(const std::string& dirname, const std::string& sector, const std::string& spawnpoint)
245243
{
246-
GameManager::current()->set_next_worldmap(dirname, spawnpoint);
244+
GameManager::current()->set_next_worldmap(dirname, sector, spawnpoint);
247245
}
248246

249247
void load_level(const std::string& filename)

src/scripting/functions.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,23 @@ bool is_christmas();
6464
void display_text_file(const std::string& filename);
6565

6666
/**
67-
* Loads and displays a worldmap (on next screenswitch).
67+
* Loads and displays a worldmap (on next screenswitch), using the savegame of the current worldmap.
6868
* @param string $filename
69+
* @param string $sector Forced sector to spawn in the worldmap on. Leave empty to use last sector from savegame.
70+
* @param string $spawnpoint Forced spawnpoint to spawn in the worldmap on. Leave empty to use last position from savegame.
6971
*/
70-
void load_worldmap(const std::string& filename);
72+
void load_worldmap(const std::string& filename, const std::string& sector, const std::string& spawnpoint);
7173

7274
/**
7375
* Switches to a different worldmap after unloading the current one, after ""exit_screen()"" is called.
74-
* @param string $dirname
75-
* @param string $spawnpoint
76+
* @param string $dirname The world directory, where the "worldmap.stwm" file is located.
77+
* @param string $sector Forced sector to spawn in the worldmap on. Leave empty to use last sector from savegame.
78+
* @param string $spawnpoint Forced spawnpoint to spawn in the worldmap on. Leave empty to use last position from savegame.
7679
*/
77-
void set_next_worldmap(const std::string& dirname, const std::string& spawnpoint);
80+
void set_next_worldmap(const std::string& dirname, const std::string& sector, const std::string& spawnpoint);
7881

7982
/**
80-
* Loads and displays a level (on next screenswitch).
83+
* Loads and displays a level (on next screenswitch), using the savegame of the current level.
8184
* @param string $filename
8285
*/
8386
void load_level(const std::string& filename);

src/scripting/wrapper.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11617,9 +11617,19 @@ static SQInteger load_worldmap_wrapper(HSQUIRRELVM vm)
1161711617
sq_throwerror(vm, _SC("Argument 1 not a string"));
1161811618
return SQ_ERROR;
1161911619
}
11620+
const SQChar* arg1;
11621+
if(SQ_FAILED(sq_getstring(vm, 3, &arg1))) {
11622+
sq_throwerror(vm, _SC("Argument 2 not a string"));
11623+
return SQ_ERROR;
11624+
}
11625+
const SQChar* arg2;
11626+
if(SQ_FAILED(sq_getstring(vm, 4, &arg2))) {
11627+
sq_throwerror(vm, _SC("Argument 3 not a string"));
11628+
return SQ_ERROR;
11629+
}
1162011630

1162111631
try {
11622-
scripting::load_worldmap(arg0);
11632+
scripting::load_worldmap(arg0, arg1, arg2);
1162311633

1162411634
return 0;
1162511635

@@ -11645,9 +11655,14 @@ static SQInteger set_next_worldmap_wrapper(HSQUIRRELVM vm)
1164511655
sq_throwerror(vm, _SC("Argument 2 not a string"));
1164611656
return SQ_ERROR;
1164711657
}
11658+
const SQChar* arg2;
11659+
if(SQ_FAILED(sq_getstring(vm, 4, &arg2))) {
11660+
sq_throwerror(vm, _SC("Argument 3 not a string"));
11661+
return SQ_ERROR;
11662+
}
1164811663

1164911664
try {
11650-
scripting::set_next_worldmap(arg0, arg1);
11665+
scripting::set_next_worldmap(arg0, arg1, arg2);
1165111666

1165211667
return 0;
1165311668

@@ -13753,14 +13768,14 @@ void register_supertux_wrapper(HSQUIRRELVM v)
1375313768

1375413769
sq_pushstring(v, "load_worldmap", -1);
1375513770
sq_newclosure(v, &load_worldmap_wrapper, 0);
13756-
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".s");
13771+
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".sss");
1375713772
if(SQ_FAILED(sq_createslot(v, -3))) {
1375813773
throw SquirrelError(v, "Couldn't register function 'load_worldmap'");
1375913774
}
1376013775

1376113776
sq_pushstring(v, "set_next_worldmap", -1);
1376213777
sq_newclosure(v, &set_next_worldmap_wrapper, 0);
13763-
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".ss");
13778+
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".sss");
1376413779
if(SQ_FAILED(sq_createslot(v, -3))) {
1376513780
throw SquirrelError(v, "Couldn't register function 'set_next_worldmap'");
1376613781
}

src/supertux/game_manager.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434

3535
GameManager::GameManager() :
3636
m_savegame(),
37-
m_next_worldmap(),
38-
m_next_spawnpoint()
37+
m_next_worldmap()
3938
{
4039
}
4140

@@ -98,26 +97,28 @@ GameManager::start_worldmap(const World& world, const std::string& worldmap_file
9897
bool
9998
GameManager::load_next_worldmap()
10099
{
101-
if (m_next_worldmap.empty())
102-
{
100+
if (!m_next_worldmap)
103101
return false;
104-
}
105-
std::unique_ptr<World> world = World::from_directory(m_next_worldmap);
106-
m_next_worldmap = "";
102+
103+
const auto next_worldmap = std::move(*m_next_worldmap);
104+
m_next_worldmap.reset();
105+
106+
std::unique_ptr<World> world = World::from_directory(next_worldmap.world);
107107
if (!world)
108108
{
109-
log_warning << "Can't load world '" << m_next_worldmap << "'" << std::endl;
109+
log_warning << "Cannot load world '" << next_worldmap.world << "'" << std::endl;
110110
return false;
111111
}
112-
start_worldmap(*world, m_next_spawnpoint); // New world, new savegame.
112+
113+
start_worldmap(*world, "", next_worldmap.sector, next_worldmap.spawnpoint); // New world, new savegame.
113114
return true;
114115
}
115116

116117
void
117-
GameManager::set_next_worldmap(const std::string& worldmap, const std::string &spawnpoint)
118+
GameManager::set_next_worldmap(const std::string& world, const std::string& sector,
119+
const std::string& spawnpoint)
118120
{
119-
m_next_worldmap = worldmap;
120-
m_next_spawnpoint = spawnpoint;
121+
m_next_worldmap.emplace(world, sector, spawnpoint);
121122
}
122123

123124
/* EOF */

src/supertux/game_manager.hpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
#ifndef HEADER_SUPERTUX_SUPERTUX_GAME_MANAGER_HPP
1818
#define HEADER_SUPERTUX_SUPERTUX_GAME_MANAGER_HPP
1919

20+
#include "util/currenton.hpp"
21+
2022
#include <optional>
2123
#include <memory>
2224
#include <string>
25+
2326
#include "math/vector.hpp"
24-
#include "util/currenton.hpp"
2527

2628
class Savegame;
2729
class World;
@@ -39,13 +41,26 @@ class GameManager final : public Currenton<GameManager>
3941
const std::optional<std::pair<std::string, Vector>>& start_pos = std::nullopt);
4042

4143
bool load_next_worldmap();
42-
void set_next_worldmap(const std::string& worldmap, const std::string &spawnpoint);
44+
void set_next_worldmap(const std::string& world, const std::string& sector = "",
45+
const std::string& spawnpoint = "");
46+
47+
private:
48+
struct NextWorldMap
49+
{
50+
NextWorldMap(const std::string& w, const std::string& s,
51+
const std::string& sp) :
52+
world(w), sector(s), spawnpoint(sp)
53+
{}
54+
55+
const std::string world;
56+
const std::string sector;
57+
const std::string spawnpoint;
58+
};
4359

4460
private:
4561
std::unique_ptr<Savegame> m_savegame;
4662

47-
std::string m_next_worldmap;
48-
std::string m_next_spawnpoint;
63+
std::optional<NextWorldMap> m_next_worldmap;
4964

5065
private:
5166
GameManager(const GameManager&) = delete;

0 commit comments

Comments
 (0)