1 #pragma once
2 #ifndef CATA_SRC_WORLDFACTORY_H
3 #define CATA_SRC_WORLDFACTORY_H
4 
5 #include <cstddef>
6 #include <functional>
7 #include <iosfwd>
8 #include <map>
9 #include <memory>
10 #include <vector>
11 #include <string>
12 
13 #include "options.h"
14 #include "pimpl.h"
15 #include "type_id.h"
16 
17 enum class special_game_type;
18 
19 class JsonIn;
20 class JsonObject;
21 
22 namespace catacurses
23 {
24 class window;
25 } // namespace catacurses
26 
27 class save_t
28 {
29     private:
30         std::string name;
31 
32         explicit save_t( const std::string &name );
33 
34     public:
35         std::string player_name() const;
36         std::string base_path() const;
37 
38         static save_t from_player_name( const std::string &name );
39         static save_t from_base_path( const std::string &base_path );
40 
41         bool operator==( const save_t &rhs ) const {
42             return name == rhs.name;
43         }
44         bool operator!=( const save_t &rhs ) const {
45             return !operator==( rhs );
46         }
47         save_t( const save_t & ) = default;
48         save_t &operator=( const save_t & ) = default;
49 };
50 
51 struct WORLD {
52     public:
53         /**
54          * @returns A path to a folder in the file system that should contain
55          * all the world specific files. It depends on @ref world_name,
56          * changing that will also change the result of this function.
57          */
58         std::string folder_path() const;
59 
60         std::string world_name;
61         options_manager::options_container WORLD_OPTIONS;
62         std::vector<save_t> world_saves;
63         /**
64          * A (possibly empty) list of (idents of) mods that
65          * should be loaded for this world.
66          */
67         std::vector<mod_id> active_mod_order;
68 
69         WORLD();
70         explicit WORLD( const std::string &name );
71         void COPY_WORLD( const WORLD *world_to_copy );
72 
73         bool save_exists( const save_t &name ) const;
74         void add_save( const save_t &name );
75 
76         bool save( bool is_conversion = false ) const;
77 
78         void load_options( JsonIn &jsin );
79         bool load_options();
80 };
81 
82 class mod_manager;
83 class mod_ui;
84 class input_context;
85 
86 using WORLDPTR = WORLD *;
87 
88 class worldfactory
89 {
90     public:
91         worldfactory();
92         ~worldfactory();
93 
94         // Generate a world
95         WORLDPTR make_new_world( bool show_prompt = true, const std::string &world_to_copy = "" );
96         WORLDPTR make_new_world( special_game_type special_type );
97         // Used for unit tests - does NOT verify if the mods can be loaded
98         WORLDPTR make_new_world( const std::string &name, const std::vector<mod_id> &mods );
99         WORLDPTR make_new_world( const std::vector<mod_id> &mods );
100         /// Returns the *existing* world of given name.
101         WORLDPTR get_world( const std::string &name );
102         bool has_world( const std::string &name ) const;
103 
104         void set_active_world( WORLDPTR world );
105 
106         void init();
107 
108         WORLDPTR pick_world( bool show_prompt = true );
109 
110         WORLDPTR active_world;
111 
112         std::vector<std::string> all_worldnames() const;
113 
114         std::string last_world_name;
115         std::string last_character_name;
116 
117         void save_last_world_info();
118 
119         mod_manager &get_mod_manager();
120 
121         void remove_world( const std::string &worldname );
122         bool valid_worldname( const std::string &name, bool automated = false );
123 
124         /**
125          * @param delete_folder If true: delete all the files and directories  of the given
126          * world folder. Else just avoid deleting the config files and the directory
127          * itself.
128          */
129         void delete_world( const std::string &worldname, bool delete_folder );
130 
131         static void draw_worldgen_tabs( const catacurses::window &w, size_t current );
132         void show_active_world_mods( const std::vector<mod_id> &world_mods );
133 
134     private:
135         std::map<std::string, std::unique_ptr<WORLD>> all_worlds;
136 
137         void load_last_world_info();
138 
139         std::string pick_random_name();
140         int show_worldgen_tab_options( const catacurses::window &win, WORLDPTR world,
141                                        const std::function<bool()> &on_quit );
142         int show_worldgen_tab_modselection( const catacurses::window &win, WORLDPTR world,
143                                             const std::function<bool()> &on_quit );
144         int show_worldgen_tab_confirm( const catacurses::window &win, WORLDPTR world,
145                                        const std::function<bool()> &on_quit );
146 
147         void draw_modselection_borders( const catacurses::window &win, const input_context &ctxtp );
148         void draw_mod_list( const catacurses::window &w, int &start, size_t cursor,
149                             const std::vector<mod_id> &mods, bool is_active_list, const std::string &text_if_empty,
150                             const catacurses::window &w_shift );
151 
152         WORLDPTR add_world( std::unique_ptr<WORLD> retworld );
153 
154         pimpl<mod_manager> mman;
155         pimpl<mod_ui> mman_ui;
156 
157         using worldgen_display = std::function<int ( const catacurses::window &, WORLDPTR,
158                                  std::function<bool()> )>;
159 
160         std::vector<worldgen_display> tabs;
161 };
162 
163 void load_world_option( const JsonObject &jo );
164 
165 //load external option from json
166 void load_external_option( const JsonObject &jo );
167 
168 extern std::unique_ptr<worldfactory> world_generator;
169 
170 #endif // CATA_SRC_WORLDFACTORY_H
171