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 "content_nodemeta.h"
21 #include "inventory.h"
22 #include "content_mapnode.h"
29 SignNodeMetadata proto_SignNodeMetadata("");
31 SignNodeMetadata::SignNodeMetadata(std::string text):
34 NodeMetadata::registerType(typeId(), create);
36 u16 SignNodeMetadata::typeId() const
38 return CONTENT_SIGN_WALL;
40 NodeMetadata* SignNodeMetadata::create(std::istream &is)
42 std::string text = deSerializeString(is);
43 return new SignNodeMetadata(text);
45 NodeMetadata* SignNodeMetadata::clone()
47 return new SignNodeMetadata(m_text);
49 void SignNodeMetadata::serializeBody(std::ostream &os)
51 os<<serializeString(m_text);
53 std::string SignNodeMetadata::infoText()
55 return std::string("\"")+m_text+"\"";
63 ChestNodeMetadata proto_ChestNodeMetadata;
65 ChestNodeMetadata::ChestNodeMetadata()
67 NodeMetadata::registerType(typeId(), create);
69 m_inventory = new Inventory();
70 m_inventory->addList("0", 8*4);
72 ChestNodeMetadata::~ChestNodeMetadata()
76 u16 ChestNodeMetadata::typeId() const
80 NodeMetadata* ChestNodeMetadata::create(std::istream &is)
82 ChestNodeMetadata *d = new ChestNodeMetadata();
83 d->m_inventory->deSerialize(is);
86 NodeMetadata* ChestNodeMetadata::clone()
88 ChestNodeMetadata *d = new ChestNodeMetadata();
89 *d->m_inventory = *m_inventory;
92 void ChestNodeMetadata::serializeBody(std::ostream &os)
94 m_inventory->serialize(os);
96 std::string ChestNodeMetadata::infoText()
100 bool ChestNodeMetadata::nodeRemovalDisabled()
103 Disable removal if chest contains something
105 InventoryList *list = m_inventory->getList("0");
108 if(list->getUsedSlots() == 0)
112 std::string ChestNodeMetadata::getInventoryDrawSpecString()
116 "list[current_name;0;0,0;8,4;]"
117 "list[current_player;main;0,5;8,4;]";
125 FurnaceNodeMetadata proto_FurnaceNodeMetadata;
127 FurnaceNodeMetadata::FurnaceNodeMetadata()
129 NodeMetadata::registerType(typeId(), create);
131 m_inventory = new Inventory();
132 m_inventory->addList("fuel", 1);
133 m_inventory->addList("src", 1);
134 m_inventory->addList("dst", 4);
136 m_step_accumulator = 0;
137 m_fuel_totaltime = 0;
142 FurnaceNodeMetadata::~FurnaceNodeMetadata()
146 u16 FurnaceNodeMetadata::typeId() const
148 return CONTENT_FURNACE;
150 NodeMetadata* FurnaceNodeMetadata::clone()
152 FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
153 *d->m_inventory = *m_inventory;
156 NodeMetadata* FurnaceNodeMetadata::create(std::istream &is)
158 FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
160 d->m_inventory->deSerialize(is);
164 d->m_fuel_totaltime = (float)temp/10;
166 d->m_fuel_time = (float)temp/10;
170 void FurnaceNodeMetadata::serializeBody(std::ostream &os)
172 m_inventory->serialize(os);
173 os<<itos(m_fuel_totaltime*10)<<" ";
174 os<<itos(m_fuel_time*10)<<" ";
176 std::string FurnaceNodeMetadata::infoText()
179 if(m_fuel_time >= m_fuel_totaltime)
181 const InventoryList *src_list = m_inventory->getList("src");
183 const InventoryItem *src_item = src_list->getItem(0);
186 return "Furnace is out of fuel";
188 return "Furnace is inactive";
192 std::string s = "Furnace is active (";
193 s += itos(m_fuel_time/m_fuel_totaltime*100);
198 bool FurnaceNodeMetadata::nodeRemovalDisabled()
201 Disable removal if furnace is not empty
203 InventoryList *list[3] = {m_inventory->getList("src"),
204 m_inventory->getList("dst"), m_inventory->getList("fuel")};
206 for(int i = 0; i < 3; i++) {
209 if(list[i]->getUsedSlots() == 0)
216 void FurnaceNodeMetadata::inventoryModified()
218 dstream<<"Furnace inventory modification callback"<<std::endl;
220 bool FurnaceNodeMetadata::step(float dtime)
223 dstream<<"Furnace stepping a long time ("<<dtime<<")"<<std::endl;
224 // Update at a fixed frequency
225 const float interval = 2.0;
226 m_step_accumulator += dtime;
227 bool changed = false;
228 while(m_step_accumulator > interval)
230 m_step_accumulator -= interval;
233 //dstream<<"Furnace step dtime="<<dtime<<std::endl;
235 InventoryList *dst_list = m_inventory->getList("dst");
238 InventoryList *src_list = m_inventory->getList("src");
240 const InventoryItem *src_item = src_list->getItem(0);
242 // Start only if there are free slots in dst, so that it can
243 // accomodate any result item
244 if(dst_list->getFreeSlots() > 0 && src_item && src_item->isCookable())
255 If fuel is burning, increment the burn counters.
256 If item finishes cooking, move it to result.
258 if(m_fuel_time < m_fuel_totaltime)
260 //dstream<<"Furnace is active"<<std::endl;
261 m_fuel_time += dtime;
263 if(m_src_time >= m_src_totaltime && m_src_totaltime > 0.001
266 InventoryItem *cookresult = src_item->createCookResult();
267 dst_list->addItem(cookresult);
268 src_list->decrementMaterials(1);
277 If there is no source item or source item is not cookable, stop loop.
279 if(src_item == NULL || m_src_totaltime < 0.001)
281 m_step_accumulator = 0;
285 //dstream<<"Furnace is out of fuel"<<std::endl;
287 InventoryList *fuel_list = m_inventory->getList("fuel");
289 const InventoryItem *fuel_item = fuel_list->getItem(0);
291 if(ItemSpec(ITEM_MATERIAL, CONTENT_TREE).checkItem(fuel_item))
293 m_fuel_totaltime = 30;
295 fuel_list->decrementMaterials(1);
298 else if(ItemSpec(ITEM_MATERIAL, CONTENT_JUNGLETREE).checkItem(fuel_item))
300 m_fuel_totaltime = 30;
302 fuel_list->decrementMaterials(1);
305 else if(ItemSpec(ITEM_MATERIAL, CONTENT_FENCE).checkItem(fuel_item))
307 m_fuel_totaltime = 30/2;
309 fuel_list->decrementMaterials(1);
312 else if(ItemSpec(ITEM_MATERIAL, CONTENT_WOOD).checkItem(fuel_item))
314 m_fuel_totaltime = 30/4;
316 fuel_list->decrementMaterials(1);
319 else if(ItemSpec(ITEM_MATERIAL, CONTENT_BOOKSHELF).checkItem(fuel_item))
321 m_fuel_totaltime = 30/4;
323 fuel_list->decrementMaterials(1);
326 else if(ItemSpec(ITEM_MATERIAL, CONTENT_LEAVES).checkItem(fuel_item))
328 m_fuel_totaltime = 30/16;
330 fuel_list->decrementMaterials(1);
333 else if(ItemSpec(ITEM_MATERIAL, CONTENT_PAPYRUS).checkItem(fuel_item))
335 m_fuel_totaltime = 30/32;
337 fuel_list->decrementMaterials(1);
340 else if(ItemSpec(ITEM_MATERIAL, CONTENT_JUNGLEGRASS).checkItem(fuel_item))
342 m_fuel_totaltime = 30/32;
344 fuel_list->decrementMaterials(1);
347 else if(ItemSpec(ITEM_MATERIAL, CONTENT_CACTUS).checkItem(fuel_item))
349 m_fuel_totaltime = 30/4;
351 fuel_list->decrementMaterials(1);
354 else if(ItemSpec(ITEM_CRAFT, "Stick").checkItem(fuel_item))
356 m_fuel_totaltime = 30/4/4;
358 fuel_list->decrementMaterials(1);
361 else if(ItemSpec(ITEM_CRAFT, "lump_of_coal").checkItem(fuel_item))
363 m_fuel_totaltime = 40;
365 fuel_list->decrementMaterials(1);
370 //dstream<<"No fuel found"<<std::endl;
371 // No fuel, stop loop.
372 m_step_accumulator = 0;
378 std::string FurnaceNodeMetadata::getInventoryDrawSpecString()
382 "list[current_name;fuel;2,3;1,1;]"
383 "list[current_name;src;2,1;1,1;]"
384 "list[current_name;dst;5,1;2,2;]"
385 "list[current_player;main;0,5;8,4;]";