1 /*
2  * This file is part of EasyRPG Player.
3  *
4  * EasyRPG Player 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  * EasyRPG Player 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
15  * along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef EP_GAME_SWITCHES_H
19 #define EP_GAME_SWITCHES_H
20 
21 // Headers
22 #include <vector>
23 #include <string>
24 #include <lcf/data.h>
25 #include "compiler.h"
26 #include "string_view.h"
27 
28 /**
29  * Game_Switches class
30  */
31 class Game_Switches {
32 public:
33 	using Switches_t = std::vector<bool>;
34 	static constexpr int kMaxWarnings = 10;
35 
36 	Game_Switches();
37 
38 	void SetData(Switches_t s);
39 	const Switches_t& GetData() const;
40 
41 	bool Get(int switch_id) const;
42 	int GetInt(int switch_id) const;
43 
44 	bool Set(int switch_id, bool value);
45 	void SetRange(int first_id, int last_id, bool value);
46 
47 	bool Flip(int switch_id);
48 	void FlipRange(int first_id, int last_id);
49 
50 	StringView GetName(int switch_id) const;
51 
52 	bool IsValid(int switch_id) const;
53 
54 	int GetSize() const;
55 
56 	void SetWarning(int w);
57 
58 private:
59 	bool ShouldWarn(int first_id, int last_id) const;
60 	void WarnGet(int variable_id) const;
61 
62 private:
63 	Switches_t _switches;
64 	mutable int _warnings = kMaxWarnings;
65 };
66 
67 
SetData(Switches_t s)68 inline void Game_Switches::SetData(Switches_t s) {
69 	_switches = std::move(s);
70 }
71 
GetData()72 inline const Game_Switches::Switches_t& Game_Switches::GetData() const {
73 	return _switches;
74 }
75 
GetSize()76 inline int Game_Switches::GetSize() const {
77 	return static_cast<int>(lcf::Data::switches.size());
78 }
79 
IsValid(int variable_id)80 inline bool Game_Switches::IsValid(int variable_id) const {
81 	return variable_id > 0 && variable_id <= GetSize();
82 }
83 
ShouldWarn(int first_id,int last_id)84 inline bool Game_Switches::ShouldWarn(int first_id, int last_id) const {
85 	return (first_id <= 0 || last_id > static_cast<int>(lcf::Data::switches.size())) && (_warnings > 0);
86 }
87 
Get(int switch_id)88 inline bool Game_Switches::Get(int switch_id) const {
89 	if (EP_UNLIKELY(ShouldWarn(switch_id, switch_id))) {
90 		WarnGet(switch_id);
91 	}
92 	if (switch_id <= 0 || switch_id > static_cast<int>(_switches.size())) {
93 		return false;
94 	}
95 	return _switches[switch_id - 1];
96 }
97 
GetInt(int switch_id)98 inline int Game_Switches::GetInt(int switch_id) const {
99 	return Get(switch_id) ? 1 : 0;
100 }
101 
SetWarning(int w)102 inline void Game_Switches::SetWarning(int w) {
103 	_warnings = w;
104 }
105 
106 #endif
107