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"
26 #include "content_mapnode.h"
32 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
34 NodeMetadata::NodeMetadata()
38 NodeMetadata::~NodeMetadata()
42 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
46 is.read((char*)buf, 2);
47 s16 id = readS16(buf);
50 std::string data = deSerializeString(is);
52 // Find factory function
53 core::map<u16, Factory>::Node *n;
57 // If factory is not found, just return.
58 dstream<<"WARNING: NodeMetadata: No factory for typeId="
63 // Try to load the metadata. If it fails, just return.
66 std::istringstream iss(data, std::ios_base::binary);
68 Factory f = n->getValue();
69 NodeMetadata *meta = (*f)(iss);
72 catch(SerializationError &e)
74 dstream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
79 void NodeMetadata::serialize(std::ostream &os)
82 writeU16(buf, typeId());
83 os.write((char*)buf, 2);
85 std::ostringstream oss(std::ios_base::binary);
87 os<<serializeString(oss.str());
90 void NodeMetadata::registerType(u16 id, Factory f)
92 core::map<u16, Factory>::Node *n;
96 m_types.insert(id, f);
104 SignNodeMetadata proto_SignNodeMetadata("");
106 SignNodeMetadata::SignNodeMetadata(std::string text):
109 NodeMetadata::registerType(typeId(), create);
111 u16 SignNodeMetadata::typeId() const
113 return CONTENT_SIGN_WALL;
115 NodeMetadata* SignNodeMetadata::create(std::istream &is)
117 std::string text = deSerializeString(is);
118 return new SignNodeMetadata(text);
120 NodeMetadata* SignNodeMetadata::clone()
122 return new SignNodeMetadata(m_text);
124 void SignNodeMetadata::serializeBody(std::ostream &os)
126 os<<serializeString(m_text);
128 std::string SignNodeMetadata::infoText()
130 return std::string("\"")+m_text+"\"";
138 ChestNodeMetadata proto_ChestNodeMetadata;
140 ChestNodeMetadata::ChestNodeMetadata()
142 NodeMetadata::registerType(typeId(), create);
144 m_inventory = new Inventory();
145 m_inventory->addList("0", 8*4);
147 ChestNodeMetadata::~ChestNodeMetadata()
151 u16 ChestNodeMetadata::typeId() const
153 return CONTENT_CHEST;
155 NodeMetadata* ChestNodeMetadata::create(std::istream &is)
157 ChestNodeMetadata *d = new ChestNodeMetadata();
158 d->m_inventory->deSerialize(is);
161 NodeMetadata* ChestNodeMetadata::clone()
163 ChestNodeMetadata *d = new ChestNodeMetadata();
164 *d->m_inventory = *m_inventory;
167 void ChestNodeMetadata::serializeBody(std::ostream &os)
169 m_inventory->serialize(os);
171 std::string ChestNodeMetadata::infoText()
175 bool ChestNodeMetadata::nodeRemovalDisabled()
178 Disable removal if chest contains something
180 InventoryList *list = m_inventory->getList("0");
183 if(list->getUsedSlots() == 0)
193 FurnaceNodeMetadata proto_FurnaceNodeMetadata;
195 FurnaceNodeMetadata::FurnaceNodeMetadata()
197 NodeMetadata::registerType(typeId(), create);
199 m_inventory = new Inventory();
200 m_inventory->addList("fuel", 1);
201 m_inventory->addList("src", 1);
202 m_inventory->addList("dst", 4);
204 m_step_accumulator = 0;
205 m_fuel_totaltime = 0;
210 FurnaceNodeMetadata::~FurnaceNodeMetadata()
214 u16 FurnaceNodeMetadata::typeId() const
216 return CONTENT_FURNACE;
218 NodeMetadata* FurnaceNodeMetadata::clone()
220 FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
221 *d->m_inventory = *m_inventory;
224 NodeMetadata* FurnaceNodeMetadata::create(std::istream &is)
226 FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
228 d->m_inventory->deSerialize(is);
232 d->m_fuel_totaltime = (float)temp/10;
234 d->m_fuel_time = (float)temp/10;
238 void FurnaceNodeMetadata::serializeBody(std::ostream &os)
240 m_inventory->serialize(os);
241 os<<itos(m_fuel_totaltime*10)<<" ";
242 os<<itos(m_fuel_time*10)<<" ";
244 std::string FurnaceNodeMetadata::infoText()
247 if(m_fuel_time >= m_fuel_totaltime)
249 InventoryList *src_list = m_inventory->getList("src");
251 InventoryItem *src_item = src_list->getItem(0);
254 return "Furnace is out of fuel";
256 return "Furnace is inactive";
260 std::string s = "Furnace is active (";
261 s += itos(m_fuel_time/m_fuel_totaltime*100);
266 void FurnaceNodeMetadata::inventoryModified()
268 dstream<<"Furnace inventory modification callback"<<std::endl;
270 bool FurnaceNodeMetadata::step(float dtime)
273 dstream<<"Furnace stepping a long time ("<<dtime<<")"<<std::endl;
274 // Update at a fixed frequency
275 const float interval = 2.0;
276 m_step_accumulator += dtime;
277 bool changed = false;
278 while(m_step_accumulator > interval)
280 m_step_accumulator -= interval;
283 //dstream<<"Furnace step dtime="<<dtime<<std::endl;
285 InventoryList *dst_list = m_inventory->getList("dst");
288 InventoryList *src_list = m_inventory->getList("src");
290 InventoryItem *src_item = src_list->getItem(0);
292 // Start only if there are free slots in dst, so that it can
293 // accomodate any result item
294 if(dst_list->getFreeSlots() > 0 && src_item && src_item->isCookable())
305 If fuel is burning, increment the burn counters.
306 If item finishes cooking, move it to result.
308 if(m_fuel_time < m_fuel_totaltime)
310 //dstream<<"Furnace is active"<<std::endl;
311 m_fuel_time += dtime;
313 if(m_src_time >= m_src_totaltime && m_src_totaltime > 0.001
316 InventoryItem *cookresult = src_item->createCookResult();
317 dst_list->addItem(cookresult);
318 src_list->decrementMaterials(1);
327 If there is no source item or source item is not cookable, stop loop.
329 if(src_item == NULL || m_src_totaltime < 0.001)
331 m_step_accumulator = 0;
335 //dstream<<"Furnace is out of fuel"<<std::endl;
337 InventoryList *fuel_list = m_inventory->getList("fuel");
339 InventoryItem *fuel_item = fuel_list->getItem(0);
341 if(ItemSpec(ITEM_MATERIAL, CONTENT_TREE).checkItem(fuel_item))
343 m_fuel_totaltime = 30;
345 fuel_list->decrementMaterials(1);
348 else if(ItemSpec(ITEM_MATERIAL, CONTENT_WOOD).checkItem(fuel_item))
350 m_fuel_totaltime = 30/4;
352 fuel_list->decrementMaterials(1);
355 else if(ItemSpec(ITEM_CRAFT, "Stick").checkItem(fuel_item))
357 m_fuel_totaltime = 30/4/4;
359 fuel_list->decrementMaterials(1);
362 else if(ItemSpec(ITEM_CRAFT, "lump_of_coal").checkItem(fuel_item))
364 m_fuel_totaltime = 40;
366 fuel_list->decrementMaterials(1);
371 //dstream<<"No fuel found"<<std::endl;
372 // No fuel, stop loop.
373 m_step_accumulator = 0;
384 void NodeMetadataList::serialize(std::ostream &os)
389 writeU16(buf, version);
390 os.write((char*)buf, 2);
392 u16 count = m_data.size();
393 writeU16(buf, count);
394 os.write((char*)buf, 2);
396 for(core::map<v3s16, NodeMetadata*>::Iterator
397 i = m_data.getIterator();
398 i.atEnd()==false; i++)
400 v3s16 p = i.getNode()->getKey();
401 NodeMetadata *data = i.getNode()->getValue();
403 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
405 os.write((char*)buf, 2);
411 void NodeMetadataList::deSerialize(std::istream &is)
417 is.read((char*)buf, 2);
418 u16 version = readU16(buf);
422 dstream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
424 throw SerializationError("NodeMetadataList::deSerialize");
427 is.read((char*)buf, 2);
428 u16 count = readU16(buf);
430 for(u16 i=0; i<count; i++)
432 is.read((char*)buf, 2);
433 u16 p16 = readU16(buf);
436 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
437 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
438 p.Y += p16 / MAP_BLOCKSIZE;
439 p16 -= p.Y * MAP_BLOCKSIZE;
442 NodeMetadata *data = NodeMetadata::deSerialize(is);
449 dstream<<"WARNING: NodeMetadataList::deSerialize(): "
450 <<"already set data at position"
451 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
457 m_data.insert(p, data);
461 NodeMetadataList::~NodeMetadataList()
463 for(core::map<v3s16, NodeMetadata*>::Iterator
464 i = m_data.getIterator();
465 i.atEnd()==false; i++)
467 delete i.getNode()->getValue();
471 NodeMetadata* NodeMetadataList::get(v3s16 p)
473 core::map<v3s16, NodeMetadata*>::Node *n;
477 return n->getValue();
480 void NodeMetadataList::remove(v3s16 p)
482 NodeMetadata *olddata = get(p);
490 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
496 bool NodeMetadataList::step(float dtime)
498 bool something_changed = false;
499 for(core::map<v3s16, NodeMetadata*>::Iterator
500 i = m_data.getIterator();
501 i.atEnd()==false; i++)
503 v3s16 p = i.getNode()->getKey();
504 NodeMetadata *meta = i.getNode()->getValue();
505 bool changed = meta->step(dtime);
507 something_changed = true;
508 /*if(res.inventory_changed)
511 inv_id += "nodemeta:";
518 c.current_player = NULL;
519 inv_mgr->inventoryModified(&c, inv_id);
522 return something_changed;