summaryrefslogtreecommitdiffstats
path: root/pokemod/Store.cpp
blob: 05315a437db8df1e9b85235d491dc272e402bdae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/////////////////////////////////////////////////////////////////////////////
// Name:        pokemod/Store.cpp
// Purpose:     Define a store that the player can shop at
// Author:      Ben Boeckel
// Modified by: Ben Boeckel
// Created:     Tue Mar 20 18:31:50 2007
// Copyright:   ©2007 Ben Boeckel and Nerdy Productions
// Licence:
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License along
//    with this program; if not, write to the Free Software Foundation, Inc.,
//    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
/////////////////////////////////////////////////////////////////////////////

#include "Store.h"

extern PokeGen::PokeMod::PokeMod curPokeMod;

PokeGen::PokeMod::Store::Store(unsigned _id)
{
   LogCtor("Store", _id);
   name = "";
   items.clear();
   id = _id;
}

PokeGen::PokeMod::Store::Store(Ini &ini, unsigned _id)
{
   LogCtorIni("Store", _id);
   ImportIni(ini);
   if (id == UINT_MAX)
      LogIdError("Store");
}

PokeGen::PokeMod::Store::~Store()
{
   LogDtor("Store", id, name);
}

void PokeGen::PokeMod::Store::Validate()
{
   isValid = true;
   LogValidateStart("Store", id, name);
   if (name == "")
   {
      LogVarNotSet("Store", id, "name", name);
      isValid = false;
   }
   for (unsigned i = 0; i < GetItemCount(); ++i)
   {
      if (!curPokeMod.GetItem(items[i]))
      {
         LogSubmoduleEmpty("Store", id, "item", name);
         isValid = false;
      }
   }
   LogValidateOver("Store", id, isValid, name);
}

#ifdef PG_DEBUG_WINDOW
void PokeGen::PokeMod::Store::Validate(wxListBox &output)
{
   isValid = true;
   LogValidateStart("Store", id, name);
   if (name == "")
   {
      LogVarNotSet("Store", id, "name", name);
      output.Append(ConsoleLogVarNotSet("Store", id, "name", name));
      isValid = false;
   }
   for (unsigned i = 0; i < GetItemCount(); ++i)
   {
      if (!curPokeGen::PokeMod.GetItem(items[i]))
      {
         LogSubmoduleEmpty("Store", id, "item", name);
         output.Append(ConsoleLogVarNotSet("Store", id, "item", name));
         isValid = false;
      }
   }
   LogValidateOver("Store", id, isValid, name);
}
#endif

void PokeGen::PokeMod::Store::ImportIni(Ini &ini, unsigned _id)
{
   LogImportStart("Store");
   if (_id == UINT_MAX)
   {
      ini.GetValue("id", id, UINT_MAX);
      // Was there an id associated with the element?
      if (id == UINT_MAX)
         LogIdNotFound("Store");
   }
   else
      id = _id;
   items.clear();
   unsigned i;
   unsigned j;
   ini.GetValue("name", name, "");
   ini.GetValue("numItems", i, 0);
   for (unsigned k = 0; k < i; ++k)
   {
      ini.GetValue(String("item-%u", k), j, UINT_MAX);
      if (j != UINT_MAX)
         items.push_back(j);
   }
   LogImportOver("Store", id, name);
}

void PokeGen::PokeMod::Store::ExportIni(std::ofstream &fout) const
{
   LogExportStart("Store", id, name);
   // Make elements
   Ini exStore("store");
   exStore.AddField("id", id);
   exStore.AddField("name", name);
   exStore.AddField("numItems", items.size());
   for (unsigned i = 0; i < items.size(); ++i)
      exStore.AddField(String("items-%u", i), items[i]);
   exStore.Export(fout);
   LogExportOver("Store", id, name);
}

void PokeGen::PokeMod::Store::SetName(const String &n)
{
   LogSetVar("Store", id, "name", n, name);
   name = n;
}

PokeGen::PokeMod::String PokeGen::PokeMod::Store::GetName()
{
   LogFetchVar("Store", id, "name", name, name);
   return name;
}

int PokeGen::PokeMod::Store::GetItem(unsigned i) const
{
   LogSubmoduleFetch("Store", id, "item", i, name);
   if (GetItemCount() <= i)
   {
      LogSubmoduleFetchFail("Store", id, "item", i, name);
      return -1;
   }
   return items[i];
}

bool PokeGen::PokeMod::Store::HasItem(unsigned _id) const
{
   LogSubmoduleFetch("Store", id, "item", _id, name);
   return (GetItem(_id) != -1);
}

bool PokeGen::PokeMod::Store::HasItem(const String &n) const
{
   LogSubmoduleFetch("Store", id, "item", UINT_MAX, name);
   for (unsigned i = 0; i < GetItemCount(); ++i)
   {
      if (curPokeMod.GetItem(items[i])->GetName() == n)
         return true;
   }
   LogSubmoduleFetchFail("Store", id, "item", UINT_MAX, name);
   return false;
}

unsigned PokeGen::PokeMod::Store::GetItemCount() const
{
   LogSubmoduleCount("Store", id, "item", name);
   return items.size();
}

void PokeGen::PokeMod::Store::NewItem(unsigned i)
{
   LogSubmoduleNew("Store", id, "item", items.size());
   items.push_back(i);
}

void PokeGen::PokeMod::Store::DeleteItem(unsigned i)
{
   LogSubmoduleRemoveStart("Store", id, "item", i, name);
   if (GetItemCount() <= i)
      LogSubmoduleRemoveFail("Store", id, "item", i, name);
   else
      DeleteItemByID(items[i]);
}

void PokeGen::PokeMod::Store::DeleteItemByID(unsigned _id)
{
   LogSubmoduleRemoveStart("Store", id, "item", _id, name);
   for (std::vector<unsigned>::iterator i = items.begin(); i != items.end(); ++i)
   {
      if (*i == _id)
      {
         LogSubmoduleRemoved("Store", id, "item", _id, name);
         items.erase(i);
      }
   }
   LogSubmoduleRemoveFail("Store", id, "item", _id, name);
}