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
|
/*
Minetest-c55
Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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 "craftdef.h"
#include "irrlichttypes.h"
#include "log.h"
#include <sstream>
#include "utility.h"
#include "gamedef.h"
#include "inventory.h"
CraftPointerInput::~CraftPointerInput()
{
for(u32 i=0; i<items.size(); i++)
delete items[i];
}
CraftPointerInput createPointerInput(const CraftInput &ci, IGameDef *gamedef)
{
std::vector<InventoryItem*> items;
for(u32 i=0; i<ci.items.size(); i++){
InventoryItem *item = NULL;
if(ci.items[i] != ""){
std::istringstream iss(ci.items[i], std::ios::binary);
item = InventoryItem::deSerialize(iss, gamedef);
}
items.push_back(item);
}
return CraftPointerInput(ci.width, items);
}
CraftInput createInput(const CraftPointerInput &cpi)
{
std::vector<std::string> items;
for(u32 i=0; i<cpi.items.size(); i++){
if(cpi.items[i] == NULL)
items.push_back("");
else{
std::ostringstream oss(std::ios::binary);
cpi.items[i]->serialize(oss);
items.push_back(oss.str());
}
}
return CraftInput(cpi.width, items);
}
std::string CraftInput::dump() const
{
std::ostringstream os(std::ios::binary);
os<<"(width="<<width<<"){";
for(u32 i=0; i<items.size(); i++)
os<<"\""<<items[i]<<"\",";
os<<"}";
return os.str();
}
std::string CraftDefinition::dump() const
{
std::ostringstream os(std::ios::binary);
os<<"{output=\""<<output<<"\", input={";
for(u32 i=0; i<input.items.size(); i++)
os<<"\""<<input.items[i]<<"\",";
os<<"}, (input.width="<<input.width<<")}";
return os.str();
}
void CraftDefinition::serialize(std::ostream &os) const
{
writeU8(os, 0); // version
os<<serializeString(output);
writeU8(os, input.width);
writeU16(os, input.items.size());
for(u32 i=0; i<input.items.size(); i++)
os<<serializeString(input.items[i]);
}
void CraftDefinition::deSerialize(std::istream &is)
{
int version = readU8(is);
if(version != 0) throw SerializationError(
"unsupported CraftDefinition version");
output = deSerializeString(is);
input.width = readU8(is);
u32 count = readU16(is);
for(u32 i=0; i<count; i++)
input.items.push_back(deSerializeString(is));
}
class CCraftDefManager: public IWritableCraftDefManager
{
public:
virtual ~CCraftDefManager()
{
clear();
}
virtual InventoryItem* getCraftResult(const CraftPointerInput &input_cpi,
IGameDef *gamedef) const
{
if(input_cpi.width > 3){
errorstream<<"getCraftResult(): ERROR: "
<<"input_cpi.width > 3; Failing to craft."<<std::endl;
return NULL;
}
InventoryItem *input_items[9];
for(u32 y=0; y<3; y++)
for(u32 x=0; x<3; x++)
{
u32 i=y*3+x;
if(x >= input_cpi.width || y >= input_cpi.height())
input_items[i] = NULL;
else
input_items[i] = input_cpi.items[y*input_cpi.width+x];
}
for(core::list<CraftDefinition*>::ConstIterator
i = m_craft_definitions.begin();
i != m_craft_definitions.end(); i++)
{
CraftDefinition *def = *i;
/*infostream<<"Checking "<<createInput(input_cpi).dump()<<std::endl
<<" against "<<def->input.dump()
<<" (output=\""<<def->output<<"\")"<<std::endl;*/
try {
CraftPointerInput spec_cpi = createPointerInput(def->input, gamedef);
if(spec_cpi.width > 3){
errorstream<<"getCraftResult: ERROR: "
<<"spec_cpi.width > 3 in recipe "
<<def->dump()<<std::endl;
continue;
}
InventoryItem *spec_items[9];
for(u32 y=0; y<3; y++)
for(u32 x=0; x<3; x++)
{
u32 i=y*3+x;
if(x >= spec_cpi.width || y >= spec_cpi.height())
spec_items[i] = NULL;
else
spec_items[i] = spec_cpi.items[y*spec_cpi.width+x];
}
bool match = checkItemCombination(input_items, spec_items);
if(match){
std::istringstream iss(def->output, std::ios::binary);
return InventoryItem::deSerialize(iss, gamedef);
}
}
catch(SerializationError &e)
{
errorstream<<"getCraftResult: ERROR: "
<<"Serialization error in recipe "
<<def->dump()<<std::endl;
// then go on with the next craft definition
}
}
return NULL;
}
virtual void registerCraft(const CraftDefinition &def)
{
infostream<<"registerCraft: registering craft definition: "
<<def.dump()<<std::endl;
if(def.input.width > 3 || def.input.height() > 3){
errorstream<<"registerCraft: input size is larger than 3x3,"
<<" ignoring"<<std::endl;
return;
}
m_craft_definitions.push_back(new CraftDefinition(def));
}
virtual void clear()
{
for(core::list<CraftDefinition*>::Iterator
i = m_craft_definitions.begin();
i != m_craft_definitions.end(); i++){
delete *i;
}
m_craft_definitions.clear();
}
virtual void serialize(std::ostream &os)
{
writeU8(os, 0); // version
u16 count = m_craft_definitions.size();
writeU16(os, count);
for(core::list<CraftDefinition*>::Iterator
i = m_craft_definitions.begin();
i != m_craft_definitions.end(); i++){
CraftDefinition *def = *i;
// Serialize wrapped in a string
std::ostringstream tmp_os(std::ios::binary);
def->serialize(tmp_os);
os<<serializeString(tmp_os.str());
}
}
virtual void deSerialize(std::istream &is)
{
// Clear everything
clear();
// Deserialize
int version = readU8(is);
if(version != 0) throw SerializationError(
"unsupported CraftDefManager version");
u16 count = readU16(is);
for(u16 i=0; i<count; i++){
// Deserialize a string and grab a CraftDefinition from it
std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
CraftDefinition def;
def.deSerialize(tmp_is);
// Register
registerCraft(def);
}
}
private:
core::list<CraftDefinition*> m_craft_definitions;
};
IWritableCraftDefManager* createCraftDefManager()
{
return new CCraftDefManager();
}
|