-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.cpp
287 lines (272 loc) · 6.69 KB
/
util.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "util.hpp"
void readConfig(map<pair<string, int>, pair<string,int>> &recipe, map<string, tuple<int, string, string>> &itemConfig)
{
string configPath = "./config";
for (const auto &entry : filesystem::directory_iterator(configPath + "/recipe"))
{
ifstream checkLine(entry.path());
int numLine = 0;
for (string line; getline(checkLine, line);)
{
numLine++;
}
ifstream recipeConfigFile(entry.path());
string ret = "";
string infoItem = "";
int num = 0;
int ans=0;
int iter = 0;
for (string line; getline(recipeConfigFile, line);)
{
iter++;
if (iter == 1)
continue;
int n = line.size();
if (iter != numLine)
{
int cnt=0;
ans++;
for (int i = 0; i < n; i++)
{
if (line[i] == ' '){
cnt++;
ans++;
continue;
}else if(line[i] == '-'){
ans--;
}
ret += line[i];
}
for(;cnt<2;cnt++){
ret+="-";
}
}
else
{
for (int i = 0; i < n; i++)
{
if (line[i] == ' ')
break;
infoItem += line[i];
}
int mul = 1;
for (int i = n - 1; i >= 0; i--)
{
if (line[i] == ' ')
break;
num += (line[i] - '0') * mul;
mul *= 10;
}
}
}
int nRes = ret.size();
for (int i = nRes - 1; i >= 0; i--)
{
if (ret[i] != '-')
{
ret = ret.substr(0, i + 1);
break;
}
}
recipe[{infoItem, num}] = {ret,ans};
}
// Read item.txt
ifstream itemConfigFile(configPath + "/item.txt");
for (string line; getline(itemConfigFile, line);)
{
int start = 0;
int end = line.find(" ");
string data[4];
int i = 0;
while (end != -1) {
data[i] = line.substr(start, end - start);
start = end + 1;
end = line.find(" ", start);
i++;
}
data[i] = line.substr(start, end - start);
itemConfig[data[1]] = {stoi(data[0]), data[2], data[3]};
}
}
Item *getItemFromString(string s, int num)
{
if (s == "DIAMOND")
{
return new Diamond(num);
}
else if (s == "IRON_INGOT")
{
return new IronIngot(num);
}
else if (s == "IRON_NUGGET")
{
return new IronNugget(num);
}
else if (s == "OAK_LOG")
{
Oak OAK;
return new Log<Oak>(OAK, num);
}
else if (s == "SPRUCE_LOG")
{
Spruce SPRUCE;
return new Log<Spruce>(SPRUCE, num);
}
else if (s == "BIRCH_LOG")
{
Birch BIRCH;
return new Log<Birch>(BIRCH, num);
}
else if (s == "OAK_PLANK")
{
Oak OAK;
return new Plank<Oak>(OAK, num);
}
else if (s == "SPRUCE_PLANK")
{
Spruce SPRUCE;
return new Plank<Spruce>(SPRUCE, num);
}
else if (s == "BIRCH_PLANK")
{
Birch BIRCH;
return new Plank<Birch>(BIRCH, num);
}
else if (s == "STICK")
{
return new Stick(num);
}
else if (s == "COBBLESTONE")
{
Cobblestone COBBLESTONE;
return new Stone<Cobblestone>(COBBLESTONE, num);
}
else if (s == "BLACKSTONE")
{
Blackstone BLACKSTONE;
return new Stone<Blackstone>(BLACKSTONE, num);
}
else if (s == "WOODEN_AXE")
{
WoodenType WOODEN;
return new Axe<WoodenType>(WOODEN);
}
else if (s == "STONE_AXE")
{
StoneType STONE;
return new Axe<StoneType>(STONE);
}
else if (s == "IRON_AXE")
{
IronType IRON;
return new Axe<IronType>(IRON);
}
else if (s == "DIAMOND_AXE")
{
DiamondType DIAMOND;
return new Axe<DiamondType>(DIAMOND);
}
else if (s == "WOODEN_SWORD")
{
WoodenType WOODEN;
return new Sword<WoodenType>(WOODEN);
}
else if (s == "STONE_SWORD")
{
StoneType STONE;
return new Sword<StoneType>(STONE);
}
else if (s == "IRON_SWORD")
{
IronType IRON;
return new Sword<IronType>(IRON);
}
else if (s == "DIAMOND_SWORD")
{
DiamondType DIAMOND;
return new Sword<DiamondType>(DIAMOND);
}
else if (s == "WOODEN_PICKAXE")
{
WoodenType WOODEN;
return new Pickaxe<WoodenType>(WOODEN);
}
else if (s == "STONE_PICKAXE")
{
StoneType STONE;
return new Pickaxe<StoneType>(STONE);
}
else if (s == "IRON_PICKAXE")
{
IronType IRON;
return new Pickaxe<IronType>(IRON);
}
else if (s == "DIAMOND_PICKAXE")
{
DiamondType DIAMOND;
return new Pickaxe<DiamondType>(DIAMOND);
}
else if (s == "LADDER")
{
return new Ladder(num);
}
else if (s == "STRING")
{
return new String(num);
}
else if (s == "FISHING_ROD")
{
return new FishingRod(num);
}
else
{
throw new InvalidItemException();
}
return 0;
}
int StringToInt(string ID)
{
int ans = 0;
int mul = 1;
int n = ID.size();
for (int i = n - 1; i > 0; i--)
{
if(ID[i]-'0'<0 || ID[i]-'0'>9){
throw new IDException();
}
ans += (ID[i] - '0') * mul;
mul *= 10;
}
if(ID[0]!='I' && ID[0]!='C'){
throw new IDException();
}
if(ID[0]=='I' && ans>=27){
throw new IDException();
}
if(ID[0]=='C' && ans>=9){
throw new IDException();
}
return ans;
}
string cutString(string s){
bool found = false;
int i = 0;
while (i < 9 && !found){
if (s[i] != '-'){
found = true;
s = s.substr(i);
}
i++;
}
int stringsize = s.size();
i = stringsize - 1;
found = false;
while (i >= 0 && !found){
if (s[i] != '-'){
found = true;
s = s.substr(0,i+1);
}
i--;
}
return s;
}