1+ /*
2+ * This file is part of Sim2Editor-CPPCore
3+ * Copyright (C) 2020-2021 SuperSaiyajinStackZ, Universal-Team
4+ *
5+ * This program is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * (at your option) any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU General Public License
16+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17+ *
18+ * Additional Terms 7.b and 7.c of GPLv3 apply to this file:
19+ * * Requiring preservation of specified reasonable legal notices or
20+ * author attributions in that material or in the Appropriate Legal
21+ * Notices displayed by works containing it.
22+ * * Prohibiting misrepresentation of the origin of that material,
23+ * or requiring that modified versions of such material be marked in
24+ * reasonable ways as different from the original version.
25+ */
26+
27+ #include " GBAHouseItem.hpp"
28+ #include " ../shared/SAVUtils.hpp"
29+ #include < cstring>
30+
31+ /* Get and Set the Item Count. */
32+ uint8_t GBAHouseItem::Count () const { return GBASAVUtils::Read<uint8_t >(this ->Offs ); };
33+ void GBAHouseItem::Count (const uint8_t V) { GBASAVUtils::Write<uint8_t >(this ->Offs , V); };
34+
35+ /* Get and Set the Item ID. */
36+ uint8_t GBAHouseItem::ID (const uint8_t Index) const { return GBASAVUtils::Read<uint8_t >(this ->Offs + 0x1 + (std::min<uint8_t >(this ->Count (), Index) * 0x6 )); };
37+ void GBAHouseItem::ID (const uint8_t Index, const uint8_t V) { GBASAVUtils::Write<uint8_t >(this ->Offs + 0x1 + (std::min<uint8_t >(this ->Count (), Index) * 0x6 ), V); };
38+
39+ /* Get and Set the Item Flag. */
40+ uint8_t GBAHouseItem::Flag (const uint8_t Index) const { return GBASAVUtils::Read<uint8_t >(this ->Offs + 0x2 + (std::min<uint8_t >(this ->Count (), Index) * 0x6 )); };
41+ void GBAHouseItem::Flag (const uint8_t Index, const uint8_t V) { GBASAVUtils::Write<uint8_t >(this ->Offs + 0x2 + (std::min<uint8_t >(this ->Count (), Index) * 0x6 ), V); };
42+
43+ /* Get and Set the Use Count(?). */
44+ uint8_t GBAHouseItem::UseCount (const uint8_t Index) const { return GBASAVUtils::Read<uint8_t >(this ->Offs + 0x3 + (std::min<uint8_t >(this ->Count (), Index) * 0x6 )); };
45+ void GBAHouseItem::UseCount (const uint8_t Index, const uint8_t V) { GBASAVUtils::Write<uint8_t >(this ->Offs + 0x3 + (std::min<uint8_t >(this ->Count (), Index) * 0x6 ), V); };
46+
47+ /* Get and Set the X Position of the Item. */
48+ uint8_t GBAHouseItem::XPos (const uint8_t Index) const { return GBASAVUtils::Read<uint8_t >(this ->Offs + 0x4 + (std::min<uint8_t >(this ->Count (), Index) * 0x6 )); };
49+ void GBAHouseItem::XPos (const uint8_t Index, const uint8_t V) { GBASAVUtils::Write<uint8_t >(this ->Offs + 0x4 + (std::min<uint8_t >(this ->Count (), Index) * 0x6 ), V); };
50+
51+ /* Get and Set the Y Position of the Item. */
52+ uint8_t GBAHouseItem::YPos (const uint8_t Index) const { return GBASAVUtils::Read<uint8_t >(this ->Offs + 0x5 + (std::min<uint8_t >(this ->Count (), Index) * 0x6 )); };
53+ void GBAHouseItem::YPos (const uint8_t Index, const uint8_t V) { GBASAVUtils::Write<uint8_t >(this ->Offs + 0x5 + (std::min<uint8_t >(this ->Count (), Index) * 0x6 ), V); };
54+
55+ /* Get and Set the Item Direction. */
56+ GBAHouseItemDirection GBAHouseItem::Direction (const uint8_t Index) const {
57+ const uint8_t D = GBASAVUtils::Read<uint8_t >(this ->Offs + 0x6 + (std::min<uint8_t >(this ->Count (), Index)) * 0x6 );
58+
59+ switch (D) {
60+ case 0x1 :
61+ return GBAHouseItemDirection::Right;
62+
63+ case 0x3 :
64+ return GBAHouseItemDirection::Down;
65+
66+ case 0x5 :
67+ return GBAHouseItemDirection::Left;
68+
69+ case 0x7 :
70+ return GBAHouseItemDirection::Up;
71+ }
72+
73+ return GBAHouseItemDirection::Invalid;
74+ };
75+ void GBAHouseItem::Direction (const uint8_t Index, const GBAHouseItemDirection V) {
76+ switch (V) {
77+ case GBAHouseItemDirection::Right:
78+ GBASAVUtils::Write<uint8_t >(this ->Offs + 0x6 + (std::min<uint8_t >(this ->Count (), Index)) * 0x6 , 0x1 );
79+ break ;
80+
81+ case GBAHouseItemDirection::Down:
82+ GBASAVUtils::Write<uint8_t >(this ->Offs + 0x6 + (std::min<uint8_t >(this ->Count (), Index)) * 0x6 , 0x3 );
83+ break ;
84+
85+ case GBAHouseItemDirection::Left:
86+ GBASAVUtils::Write<uint8_t >(this ->Offs + 0x6 + (std::min<uint8_t >(this ->Count (), Index)) * 0x6 , 0x5 );
87+ break ;
88+
89+ case GBAHouseItemDirection::Up:
90+ GBASAVUtils::Write<uint8_t >(this ->Offs + 0x6 + (std::min<uint8_t >(this ->Count (), Index)) * 0x6 , 0x7 );
91+ break ;
92+
93+ case GBAHouseItemDirection::Invalid:
94+ break ;
95+ }
96+ };
97+
98+ /*
99+ Add an Item to the House.
100+ This needs to be handled like this, because things move 0x6 bytes up when an Item is being added.
101+ */
102+ void GBAHouseItem::AddItem (const uint8_t ID, const uint8_t Flag, const uint8_t UseCount, const uint8_t XPos, const uint8_t YPos, const GBAHouseItemDirection Direction) {
103+ if (this ->Count () == 0xC ) return ; // Not allowed to add more than 0xC / 12 Items.
104+
105+ const uint8_t CT = this ->Count ();
106+ this ->Count (CT + 0x1 );
107+
108+ std::unique_ptr<uint8_t []> TMP = std::make_unique<uint8_t []>(0xF26 - (this ->Count () * 6 ));
109+ memcpy ( // Copy first to a TMP pointer.
110+ TMP.get (),
111+ GBASAVUtils::SAV->GetData () + (this ->Offs + 0x1 ) + (CT * 0x6 ),
112+ 0xF26 - (this ->Count () * 6 )
113+ );
114+
115+ memcpy ( // Then copy to the actual location from the TMP pointer.
116+ GBASAVUtils::SAV->GetData () + (this ->Offs + 0x1 ) + (this ->Count () * 0x6 ),
117+ TMP.get (),
118+ 0xF26 - (this ->Count () * 6 )
119+ );
120+
121+ /* Set Item Data. */
122+ this ->ID (CT, ID);
123+ this ->Flag (CT, Flag);
124+ this ->UseCount (CT, UseCount);
125+ this ->XPos (CT, XPos);
126+ this ->YPos (CT, YPos);
127+ this ->Direction (CT, Direction);
128+ };
129+
130+ /*
131+ Remove an Item from the House.
132+ This needs to be handled like this, because things move 0x6 bytes down when an Item is being removed.
133+ */
134+ void GBAHouseItem::RemoveItem (const uint8_t Index) {
135+ if ((this ->Count () == 0 ) || (this ->Count () - 1 < Index)) return ; // Nanana, Index and or Count is not good.
136+
137+ const uint8_t CT = this ->Count ();
138+ this ->Count (this ->Count () - 0x1 );
139+
140+ std::unique_ptr<uint8_t []> TMP = std::make_unique<uint8_t []>(0xF26 - (this ->Count () * 6 ));
141+ memcpy ( // Copy first to a TMP pointer.
142+ TMP.get (),
143+ GBASAVUtils::SAV->GetData () + (this ->Offs + 0x1 ) + ((Index + 0x1 ) * 0x6 ),
144+ 0xF26 - (this ->Count () * 6 )
145+ );
146+
147+ memcpy ( // Then copy to the actual location from the TMP pointer.
148+ GBASAVUtils::SAV->GetData () + (this ->Offs + 0x1 ) + (Index * 0x6 ),
149+ TMP.get (),
150+ 0xF26 - (this ->Count () * 6 )
151+ );
152+ };
0 commit comments