Skip to content

Commit 8c4fc68

Browse files
authored
ability to clear out specific dimensions (attempt 2) (#82999)
* typo * add `clear_dimension` EOC function * update doc
1 parent 8c7a084 commit 8c4fc68

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

data/json/effects_on_condition/example_eocs.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,5 +544,24 @@
544544
{ "u_message": { "u_val": "destination_dimension" } },
545545
{ "if": { "current_dimension": "" }, "then": { "u_message": "home sweet home" } }
546546
]
547+
},
548+
{
549+
"type": "effect_on_condition",
550+
"id": "EOC_dimension_clear_test",
551+
"//": "clears out a dimension in '/dimensions' folder",
552+
"global": true,
553+
"effect": [
554+
{
555+
"set_string_var": "",
556+
"string_input": {
557+
"title": { "str": "clear the dimension named:" },
558+
"description": { "str": "the main dimension can't be cleared." },
559+
"width": 30
560+
},
561+
"target_var": { "u_val": "target_dimension" }
562+
},
563+
{ "clear_dimension": { "u_val": "target_dimension" } },
564+
{ "u_message": "if dimension with ID <u_val:target_dimension> existed, it doesn't anymore." }
565+
]
547566
}
548567
]

doc/JSON/EFFECT_ON_CONDITION.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,35 @@ Will give a message if you're in the dimension with the ID of "test".
13391339
"then": { "u_message": "Currently loaded dimension is the one with ID of 'test'." } }
13401340
```
13411341

1342+
### `clear_dimension`
1343+
- type: string or [variable object](#variable-object)
1344+
- deletes the dimension folder with the given string ID.
1345+
1346+
#### Valid talkers:
1347+
1348+
| Avatar | NPC | Monster | Furniture | Item | Vehicle |
1349+
| ------ --------- | ---- | ------- | --- | ---- |
1350+
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
1351+
1352+
#### Examples
1353+
Deletes (if present) the dimension with ID of 'test'.
1354+
```jsonc
1355+
{ { "clear_dimension": "test" } }
1356+
```
1357+
Asks for text input, will delete the dimension that matches the string ID.
1358+
```jsonc
1359+
{
1360+
"set_string_var": "",
1361+
"string_input": {
1362+
"title": { "str": "clear the dimension named:" },
1363+
"description": { "str": "the main dimension can't be cleared." },
1364+
"width": 30
1365+
},
1366+
"target_var": { "u_val": "target_dimension" }
1367+
},
1368+
{ "clear_dimension": { "u_val": "target_dimension" } }
1369+
```
1370+
13421371
### `player_see_u`, `player_see_npc`
13431372
- type: simple string
13441373
- return true if player can see alpha or beta talker

src/game.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13776,15 +13776,21 @@ cata_path PATH_INFO::world_base_save_path()
1377613776
return world_generator->active_world->folder_path();
1377713777
}
1377813778

13779+
cata_path PATH_INFO::dimensions_save_path()
13780+
{
13781+
return PATH_INFO::world_base_save_path() / "dimensions";
13782+
}
13783+
1377913784
cata_path PATH_INFO::current_dimension_save_path()
1378013785
{
1378113786
std::string dimension_prefix = g->get_dimension_prefix();
1378213787
if( !dimension_prefix.empty() ) {
13783-
return PATH_INFO::world_base_save_path() / "dimensions" / dimension_prefix;
13788+
return PATH_INFO::dimensions_save_path() / dimension_prefix;
1378413789
}
1378513790
return PATH_INFO::world_base_save_path();
1378613791
}
1378713792

13793+
1378813794
cata_path PATH_INFO::current_dimension_player_save_path()
1378913795
{
1379013796
return PATH_INFO::current_dimension_save_path() / base64_encode( get_avatar().get_save_id() );

src/game.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ class game
13731373
//currently used as a hacky workaround for dimension swapping
13741374
bool swapping_dimensions = false; // NOLINT (cata-serialize)
13751375
private:
1376-
// Stores the currently occupoed dimension.
1376+
// Stores the currently occupied dimension.
13771377
// TODO: should be an id instead of a string.
13781378
std::string dimension_prefix;
13791379
};

src/npctalk.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cmath>
66
#include <cstddef>
77
#include <exception>
8+
#include <filesystem>
89
#include <iterator>
910
#include <list>
1011
#include <map>
@@ -29,6 +30,7 @@
2930
#include "bodypart.h"
3031
#include "calendar.h"
3132
#include "cata_lazy.h"
33+
#include "cata_path.h"
3234
#include "cata_utility.h"
3335
#include "catacharset.h"
3436
#include "character.h"
@@ -58,6 +60,7 @@
5860
#include "faction.h"
5961
#include "faction_camp.h"
6062
#include "field.h"
63+
#include "filesystem.h"
6164
#include "flag.h"
6265
#include "flat_set.h"
6366
#include "flexbuffer_json.h"
@@ -107,6 +110,7 @@
107110
#include "output.h"
108111
#include "overmap_ui.h"
109112
#include "overmapbuffer.h"
113+
#include "path_info.h"
110114
#include "pickup.h"
111115
#include "pimpl.h"
112116
#include "player_activity.h"
@@ -4764,6 +4768,23 @@ talk_effect_fun_t::func f_place_override( const JsonObject &jo, std::string_view
47644768
};
47654769
}
47664770

4771+
talk_effect_fun_t::func f_clear_dimension( const JsonObject &jo, std::string_view member,
4772+
std::string_view )
4773+
{
4774+
str_or_var target_dimension = get_str_or_var( jo.get_member( member ), member, true );
4775+
4776+
return [target_dimension]( dialogue const & d ) {
4777+
const std::string target_dimension_id = target_dimension.evaluate(
4778+
d );
4779+
const std::vector<cata_path> dimensions_query = get_directories_with( target_dimension_id,
4780+
PATH_INFO::dimensions_save_path() );
4781+
if( dimensions_query.size() == 1 ) {
4782+
std::filesystem::remove_all( ( PATH_INFO::dimensions_save_path() /
4783+
target_dimension_id ).get_unrelative_path() );
4784+
}
4785+
};
4786+
}
4787+
47674788
talk_effect_fun_t::func f_mapgen_update( const JsonObject &jo, std::string_view member,
47684789
std::string_view )
47694790
{
@@ -7890,6 +7911,7 @@ parsers = {
78907911
{ "set_npc_cbm_reserve_rule", jarg::member, &talk_effect_fun::f_npc_cbm_reserve_rule },
78917912
{ "set_npc_cbm_recharge_rule", jarg::member, &talk_effect_fun::f_npc_cbm_recharge_rule },
78927913
{ "map_spawn_item", jarg::member, &talk_effect_fun::f_spawn_item },
7914+
{ "clear_dimension", jarg::member, &talk_effect_fun::f_clear_dimension },
78937915
{ "mapgen_update", jarg::member, &talk_effect_fun::f_mapgen_update },
78947916
{ "alter_timed_events", jarg::member, &talk_effect_fun::f_alter_timed_events },
78957917
{ "revert_location", jarg::member, &talk_effect_fun::f_revert_location },

src/path_info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ cata_path user_keybindings();
9393
cata_path user_moddir_path();
9494
cata_path user_sound();
9595
cata_path world_base_save_path();
96+
cata_path dimensions_save_path();
9697
cata_path current_dimension_save_path();
9798
cata_path current_dimension_player_save_path();
9899

0 commit comments

Comments
 (0)