1 /*
2  * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org
3  *
4  * Solarus is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Solarus is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef SOLARUS_SAVEGAME_H
18 #define SOLARUS_SAVEGAME_H
19 
20 #include "solarus/core/Common.h"
21 #include "solarus/core/Equipment.h"
22 #include "solarus/graphics/Transition.h"
23 #include "solarus/lua/ExportableToLua.h"
24 #include <map>
25 #include <string>
26 
27 struct lua_State;
28 
29 namespace Solarus {
30 
31 class LuaContext;
32 class MainLoop;
33 
34 /**
35  * \brief Manages the game data saved.
36  *
37  * This class provides read and write access to the saved data.
38  */
39 class SOLARUS_API Savegame: public ExportableToLua {
40 
41   public:
42 
43     static const int SAVEGAME_VERSION;  /**< Version number of the savegame file format. */
44 
45     // Keys to built-in values saved.
46     static const std::string KEY_SAVEGAME_VERSION;
47     static const std::string KEY_STARTING_MAP;
48     static const std::string KEY_STARTING_POINT;
49     static const std::string KEY_KEYBOARD_ACTION;
50     static const std::string KEY_KEYBOARD_ATTACK;
51     static const std::string KEY_KEYBOARD_ITEM_1;
52     static const std::string KEY_KEYBOARD_ITEM_2;
53     static const std::string KEY_KEYBOARD_PAUSE;
54     static const std::string KEY_KEYBOARD_RIGHT;
55     static const std::string KEY_KEYBOARD_UP;
56     static const std::string KEY_KEYBOARD_LEFT;
57     static const std::string KEY_KEYBOARD_DOWN;
58     static const std::string KEY_JOYPAD_ACTION;
59     static const std::string KEY_JOYPAD_ATTACK;
60     static const std::string KEY_JOYPAD_ITEM_1;
61     static const std::string KEY_JOYPAD_ITEM_2;
62     static const std::string KEY_JOYPAD_PAUSE;
63     static const std::string KEY_JOYPAD_RIGHT;
64     static const std::string KEY_JOYPAD_UP;
65     static const std::string KEY_JOYPAD_LEFT;
66     static const std::string KEY_JOYPAD_DOWN;
67     static const std::string KEY_CURRENT_LIFE;
68     static const std::string KEY_CURRENT_MONEY;
69     static const std::string KEY_CURRENT_MAGIC;
70     static const std::string KEY_MAX_LIFE;
71     static const std::string KEY_MAX_MONEY;
72     static const std::string KEY_MAX_MAGIC;
73     static const std::string KEY_ITEM_SLOT_1;
74     static const std::string KEY_ITEM_SLOT_2;
75     static const std::string KEY_ABILITY_TUNIC;
76     static const std::string KEY_ABILITY_SWORD;
77     static const std::string KEY_ABILITY_SWORD_KNOWLEDGE;
78     static const std::string KEY_ABILITY_SHIELD;
79     static const std::string KEY_ABILITY_LIFT;
80     static const std::string KEY_ABILITY_SWIM;
81     static const std::string KEY_ABILITY_JUMP_OVER_WATER;
82     static const std::string KEY_ABILITY_RUN;
83     static const std::string KEY_ABILITY_PUSH;
84     static const std::string KEY_ABILITY_GRAB;
85     static const std::string KEY_ABILITY_PULL;
86     static const std::string KEY_ABILITY_DETECT_WEAK_WALLS;
87     static const std::string KEY_ABILITY_GET_BACK_FROM_DEATH;
88 
89     // creation and destruction
90     Savegame(MainLoop& main_loop, const std::string& file_name);
91 
92     // file state
93     bool is_empty() const;
94     void initialize();
95     void save();
96     const std::string& get_file_name() const;
97 
98     // data
99     bool is_string(const std::string& key) const;
100     std::string get_string(const std::string& key) const;
101     void set_string(const std::string& key, const std::string& value);
102     bool is_integer(const std::string& key) const;
103     int get_integer(const std::string& key) const;
104     void set_integer(const std::string& key, int value);
105     bool is_boolean(const std::string& key) const;
106     bool get_boolean(const std::string& key) const;
107     void set_boolean(const std::string& key, bool value);
108     bool is_set(const std::string& key) const;
109     void unset(const std::string& key);
110 
111     void set_initial_values();
112     void set_default_keyboard_controls();
113     void set_default_joypad_controls();
114     void post_process_existing_savegame();
115 
116     // unsaved data
117     MainLoop& get_main_loop();
118     LuaContext& get_lua_context();
119     const Equipment& get_equipment() const;
120     Equipment& get_equipment();
121     const Game* get_game() const;
122     Game* get_game();
123     void set_game(Game* game);
124     void notify_game_started();
125     void notify_game_finished();
126     Transition::Style get_default_transition_style() const;
127     void set_default_transition_style(Transition::Style default_transition_style);
128 
129     virtual const std::string& get_lua_type_name() const override;
130 
131   private:
132 
133     struct SavedValue {
134 
135       enum {
136         VALUE_STRING,
137         VALUE_INTEGER,
138         VALUE_BOOLEAN
139       } type;
140 
141       std::string string_data;
142       int int_data;  // Also used for boolean
143     };
144 
145     std::map<std::string, SavedValue> saved_values;
146 
147     bool empty;
148     std::string file_name;         /**< Savegame file name relative to the quest write directory. */
149     MainLoop& main_loop;
150     Equipment equipment;
151     Game* game;                    /**< nullptr if this savegame is not currently running */
152     Transition::Style
153         default_transition_style;  /**< Transition style to use by default. */
154 
155     void import_from_file();
156     static int l_newindex(lua_State* l);
157 
158 };
159 
160 }
161 
162 #endif
163 
164