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 // Headers
19 #include "game_switches.h"
20 #include "output.h"
21 #include <lcf/reader_util.h>
22 #include <lcf/data.h>
23
24 constexpr int Game_Switches::kMaxWarnings;
25
Game_Switches()26 Game_Switches::Game_Switches() {
27 _switches.reserve(lcf::Data::switches.size());
28 }
29
WarnGet(int variable_id) const30 void Game_Switches::WarnGet(int variable_id) const {
31 Output::Debug("Invalid read sw[{}]!", variable_id);
32 --_warnings;
33 }
34
Set(int switch_id,bool value)35 bool Game_Switches::Set(int switch_id, bool value) {
36 if (EP_UNLIKELY(ShouldWarn(switch_id, switch_id))) {
37 Output::Debug("Invalid write sw[{}] = {}!", switch_id, value);
38 --_warnings;
39 }
40 if (switch_id <= 0) {
41 return false;
42 }
43 auto& ss = _switches;
44 if (switch_id > static_cast<int>(ss.size())) {
45 ss.resize(switch_id);
46 }
47 ss[switch_id - 1] = value;
48 return value;
49 }
50
SetRange(int first_id,int last_id,bool value)51 void Game_Switches::SetRange(int first_id, int last_id, bool value) {
52 if (EP_UNLIKELY(ShouldWarn(first_id, last_id))) {
53 Output::Debug("Invalid write sw[{},{}] = {}!", first_id, last_id, value);
54 --_warnings;
55 }
56 auto& ss = _switches;
57 if (last_id > static_cast<int>(ss.size())) {
58 ss.resize(last_id, false);
59 }
60 for (int i = std::max(0, first_id - 1); i < last_id; ++i) {
61 ss[i] = value;
62 }
63 }
64
Flip(int switch_id)65 bool Game_Switches::Flip(int switch_id) {
66 if (EP_UNLIKELY(ShouldWarn(switch_id, switch_id))) {
67 Output::Debug("Invalid flip sw[{}]!", switch_id);
68 --_warnings;
69 }
70 if (switch_id <= 0) {
71 return false;
72 }
73 auto& ss = _switches;
74 if (switch_id > static_cast<int>(ss.size())) {
75 ss.resize(switch_id);
76 }
77 ss[switch_id - 1].flip();
78 return ss[switch_id - 1];
79 }
80
FlipRange(int first_id,int last_id)81 void Game_Switches::FlipRange(int first_id, int last_id) {
82 if (EP_UNLIKELY(ShouldWarn(first_id, last_id))) {
83 Output::Debug("Invalid flip sw[{},{}]!", first_id, last_id);
84 --_warnings;
85 }
86 auto& ss = _switches;
87 if (last_id > static_cast<int>(ss.size())) {
88 ss.resize(last_id);
89 }
90 for (int i = std::max(0, first_id - 1); i < last_id; ++i) {
91 ss[i].flip();
92 }
93 }
94
GetName(int _id) const95 StringView Game_Switches::GetName(int _id) const {
96 const auto* sw = lcf::ReaderUtil::GetElement(lcf::Data::switches, _id);
97
98 if (!sw) {
99 // No warning, is valid because the switch array resizes dynamic during runtime
100 return {};
101 } else {
102 return sw->name;
103 }
104 }
105
106