3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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 2 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "nodemetadata.h"
23 #include "exceptions.h"
24 #include "inventory.h"
31 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
33 NodeMetadata::NodeMetadata()
37 NodeMetadata::~NodeMetadata()
41 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
45 is.read((char*)buf, 2);
46 s16 id = readS16(buf);
49 std::string data = deSerializeString(is);
51 // Find factory function
52 core::map<u16, Factory>::Node *n;
56 // If factory is not found, just return.
57 dstream<<"WARNING: NodeMetadata: No factory for typeId="
62 // Try to load the metadata. If it fails, just return.
65 std::istringstream iss(data, std::ios_base::binary);
67 Factory f = n->getValue();
68 NodeMetadata *meta = (*f)(iss);
71 catch(SerializationError &e)
73 dstream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
78 void NodeMetadata::serialize(std::ostream &os)
81 writeU16(buf, typeId());
82 os.write((char*)buf, 2);
84 std::ostringstream oss(std::ios_base::binary);
86 os<<serializeString(oss.str());
89 void NodeMetadata::registerType(u16 id, Factory f)
91 core::map<u16, Factory>::Node *n;
95 m_types.insert(id, f);
103 SignNodeMetadata proto_SignNodeMetadata("");
105 SignNodeMetadata::SignNodeMetadata(std::string text):
108 NodeMetadata::registerType(typeId(), create);
110 u16 SignNodeMetadata::typeId() const
112 return CONTENT_SIGN_WALL;
114 NodeMetadata* SignNodeMetadata::create(std::istream &is)
116 std::string text = deSerializeString(is);
117 return new SignNodeMetadata(text);
119 NodeMetadata* SignNodeMetadata::clone()
121 return new SignNodeMetadata(m_text);
123 void SignNodeMetadata::serializeBody(std::ostream &os)
125 os<<serializeString(m_text);
127 std::string SignNodeMetadata::infoText()
129 return std::string("\"")+m_text+"\"";
137 ChestNodeMetadata proto_ChestNodeMetadata;
139 ChestNodeMetadata::ChestNodeMetadata()
141 NodeMetadata::registerType(typeId(), create);
143 m_inventory = new Inventory();
144 m_inventory->addList("0", 8*4);
146 ChestNodeMetadata::~ChestNodeMetadata()
150 u16 ChestNodeMetadata::typeId() const
152 return CONTENT_CHEST;
154 NodeMetadata* ChestNodeMetadata::create(std::istream &is)
156 ChestNodeMetadata *d = new ChestNodeMetadata();
157 d->m_inventory->deSerialize(is);
160 NodeMetadata* ChestNodeMetadata::clone()
162 ChestNodeMetadata *d = new ChestNodeMetadata();
163 *d->m_inventory = *m_inventory;
166 void ChestNodeMetadata::serializeBody(std::ostream &os)
168 m_inventory->serialize(os);
170 std::string ChestNodeMetadata::infoText()
174 bool ChestNodeMetadata::nodeRemovalDisabled()
177 Disable removal if chest contains something
179 InventoryList *list = m_inventory->getList("0");
182 if(list->getUsedSlots() == 0)
192 FurnaceNodeMetadata proto_FurnaceNodeMetadata;
194 FurnaceNodeMetadata::FurnaceNodeMetadata()
196 NodeMetadata::registerType(typeId(), create);
198 m_inventory = new Inventory();
199 m_inventory->addList("fuel", 1);
200 m_inventory->addList("src", 1);
201 m_inventory->addList("dst", 4);
203 m_step_accumulator = 0;
204 m_fuel_totaltime = 0;
209 FurnaceNodeMetadata::~FurnaceNodeMetadata()
213 u16 FurnaceNodeMetadata::typeId() const
215 return CONTENT_FURNACE;
217 NodeMetadata* FurnaceNodeMetadata::clone()
219 FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
220 *d->m_inventory = *m_inventory;
223 NodeMetadata* FurnaceNodeMetadata::create(std::istream &is)
225 FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
227 d->m_inventory->deSerialize(is);
231 d->m_fuel_totaltime = (float)temp/10;
233 d->m_fuel_time = (float)temp/10;
237 void FurnaceNodeMetadata::serializeBody(std::ostream &os)
239 m_inventory->serialize(os);
240 os<<itos(m_fuel_totaltime*10)<<" ";
241 os<<itos(m_fuel_time*10)<<" ";
243 std::string FurnaceNodeMetadata::infoText()
246 if(m_fuel_time >= m_fuel_totaltime)
248 InventoryList *src_list = m_inventory->getList("src");
250 InventoryItem *src_item = src_list->getItem(0);
253 return "Furnace is out of fuel";
255 return "Furnace is inactive";
259 std::string s = "Furnace is active (";
260 s += itos(m_fuel_time/m_fuel_totaltime*100);
265 void FurnaceNodeMetadata::inventoryModified()
267 dstream<<"Furnace inventory modification callback"<<std::endl;
269 bool FurnaceNodeMetadata::step(float dtime)
272 dstream<<"Furnace stepping a long time ("<<dtime<<")"<<std::endl;
273 // Update at a fixed frequency
274 const float interval = 2.0;
275 m_step_accumulator += dtime;
276 bool changed = false;
277 while(m_step_accumulator > interval)
279 m_step_accumulator -= interval;
282 //dstream<<"Furnace step dtime="<<dtime<<std::endl;
284 InventoryList *dst_list = m_inventory->getList("dst");
287 InventoryList *src_list = m_inventory->getList("src");
289 InventoryItem *src_item = src_list->getItem(0);
291 // Start only if there are free slots in dst, so that it can
292 // accomodate any result item
293 if(dst_list->getFreeSlots() > 0 && src_item && src_item->isCookable())
303 if(m_fuel_time < m_fuel_totaltime)
305 //dstream<<"Furnace is active"<<std::endl;
306 m_fuel_time += dtime;
308 if(m_src_time >= m_src_totaltime && m_src_totaltime > 0.001
311 InventoryItem *cookresult = src_item->createCookResult();
312 dst_list->addItem(cookresult);
313 src_list->decrementMaterials(1);
321 if(src_item == NULL || m_src_totaltime < 0.001)
326 //dstream<<"Furnace is out of fuel"<<std::endl;
328 InventoryList *fuel_list = m_inventory->getList("fuel");
330 InventoryItem *fuel_item = fuel_list->getItem(0);
332 if(ItemSpec(ITEM_MATERIAL, CONTENT_TREE).checkItem(fuel_item))
334 m_fuel_totaltime = 30;
336 fuel_list->decrementMaterials(1);
339 else if(ItemSpec(ITEM_MATERIAL, CONTENT_WOOD).checkItem(fuel_item))
341 m_fuel_totaltime = 30/4;
343 fuel_list->decrementMaterials(1);
346 else if(ItemSpec(ITEM_CRAFT, "Stick").checkItem(fuel_item))
348 m_fuel_totaltime = 30/4/4;
350 fuel_list->decrementMaterials(1);
353 else if(ItemSpec(ITEM_CRAFT, "lump_of_coal").checkItem(fuel_item))
355 m_fuel_totaltime = 40;
357 fuel_list->decrementMaterials(1);
362 //dstream<<"No fuel found"<<std::endl;
372 void NodeMetadataList::serialize(std::ostream &os)
377 writeU16(buf, version);
378 os.write((char*)buf, 2);
380 u16 count = m_data.size();
381 writeU16(buf, count);
382 os.write((char*)buf, 2);
384 for(core::map<v3s16, NodeMetadata*>::Iterator
385 i = m_data.getIterator();
386 i.atEnd()==false; i++)
388 v3s16 p = i.getNode()->getKey();
389 NodeMetadata *data = i.getNode()->getValue();
391 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
393 os.write((char*)buf, 2);
399 void NodeMetadataList::deSerialize(std::istream &is)
405 is.read((char*)buf, 2);
406 u16 version = readU16(buf);
410 dstream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
412 throw SerializationError("NodeMetadataList::deSerialize");
415 is.read((char*)buf, 2);
416 u16 count = readU16(buf);
418 for(u16 i=0; i<count; i++)
420 is.read((char*)buf, 2);
421 u16 p16 = readU16(buf);
424 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
425 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
426 p.Y += p16 / MAP_BLOCKSIZE;
427 p16 -= p.Y * MAP_BLOCKSIZE;
430 NodeMetadata *data = NodeMetadata::deSerialize(is);
437 dstream<<"WARNING: NodeMetadataList::deSerialize(): "
438 <<"already set data at position"
439 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
445 m_data.insert(p, data);
449 NodeMetadataList::~NodeMetadataList()
451 for(core::map<v3s16, NodeMetadata*>::Iterator
452 i = m_data.getIterator();
453 i.atEnd()==false; i++)
455 delete i.getNode()->getValue();
459 NodeMetadata* NodeMetadataList::get(v3s16 p)
461 core::map<v3s16, NodeMetadata*>::Node *n;
465 return n->getValue();
468 void NodeMetadataList::remove(v3s16 p)
470 NodeMetadata *olddata = get(p);
478 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
484 bool NodeMetadataList::step(float dtime)
486 bool something_changed = false;
487 for(core::map<v3s16, NodeMetadata*>::Iterator
488 i = m_data.getIterator();
489 i.atEnd()==false; i++)
491 v3s16 p = i.getNode()->getKey();
492 NodeMetadata *meta = i.getNode()->getValue();
493 bool changed = meta->step(dtime);
495 something_changed = true;
496 /*if(res.inventory_changed)
499 inv_id += "nodemeta:";
506 c.current_player = NULL;
507 inv_mgr->inventoryModified(&c, inv_id);
510 return something_changed;