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"
25 #define NODEMETA_SIGN 14
26 #define NODEMETA_CHEST 15
27 #define NODEMETA_LOCKABLE_CHEST 17
28 #define NODEMETA_FURNACE 16
35 SignNodeMetadata proto_SignNodeMetadata(NULL, "");
37 SignNodeMetadata::SignNodeMetadata(IGameDef *gamedef, std::string text):
38 NodeMetadata(gamedef),
41 NodeMetadata::registerType(typeId(), typeName(), create);
43 u16 SignNodeMetadata::typeId() const
47 NodeMetadata* SignNodeMetadata::create(std::istream &is, IGameDef *gamedef)
49 std::string text = deSerializeString(is);
50 return new SignNodeMetadata(gamedef, text);
52 NodeMetadata* SignNodeMetadata::clone(IGameDef *gamedef)
54 return new SignNodeMetadata(gamedef, m_text);
56 void SignNodeMetadata::serializeBody(std::ostream &os)
58 os<<serializeString(m_text);
60 std::string SignNodeMetadata::infoText()
62 return std::string("\"")+m_text+"\"";
70 ChestNodeMetadata proto_ChestNodeMetadata(NULL);
72 ChestNodeMetadata::ChestNodeMetadata(IGameDef *gamedef):
75 NodeMetadata::registerType(typeId(), typeName(), create);
77 m_inventory = new Inventory();
78 m_inventory->addList("0", 8*4);
80 ChestNodeMetadata::~ChestNodeMetadata()
84 u16 ChestNodeMetadata::typeId() const
86 return NODEMETA_CHEST;
88 NodeMetadata* ChestNodeMetadata::create(std::istream &is, IGameDef *gamedef)
90 ChestNodeMetadata *d = new ChestNodeMetadata(gamedef);
91 d->m_inventory->deSerialize(is, gamedef);
94 NodeMetadata* ChestNodeMetadata::clone(IGameDef *gamedef)
96 ChestNodeMetadata *d = new ChestNodeMetadata(gamedef);
97 *d->m_inventory = *m_inventory;
100 void ChestNodeMetadata::serializeBody(std::ostream &os)
102 m_inventory->serialize(os);
104 std::string ChestNodeMetadata::infoText()
108 bool ChestNodeMetadata::nodeRemovalDisabled()
111 Disable removal if chest contains something
113 InventoryList *list = m_inventory->getList("0");
116 if(list->getUsedSlots() == 0)
120 std::string ChestNodeMetadata::getInventoryDrawSpecString()
124 "list[current_name;0;0,0;8,4;]"
125 "list[current_player;main;0,5;8,4;]";
129 LockingChestNodeMetadata
133 LockingChestNodeMetadata proto_LockingChestNodeMetadata(NULL);
135 LockingChestNodeMetadata::LockingChestNodeMetadata(IGameDef *gamedef):
136 NodeMetadata(gamedef)
138 NodeMetadata::registerType(typeId(), typeName(), create);
140 m_inventory = new Inventory();
141 m_inventory->addList("0", 8*4);
143 LockingChestNodeMetadata::~LockingChestNodeMetadata()
147 u16 LockingChestNodeMetadata::typeId() const
149 return NODEMETA_LOCKABLE_CHEST;
151 NodeMetadata* LockingChestNodeMetadata::create(std::istream &is, IGameDef *gamedef)
153 LockingChestNodeMetadata *d = new LockingChestNodeMetadata(gamedef);
154 d->setOwner(deSerializeString(is));
155 d->m_inventory->deSerialize(is, gamedef);
158 NodeMetadata* LockingChestNodeMetadata::clone(IGameDef *gamedef)
160 LockingChestNodeMetadata *d = new LockingChestNodeMetadata(gamedef);
161 *d->m_inventory = *m_inventory;
164 void LockingChestNodeMetadata::serializeBody(std::ostream &os)
166 os<<serializeString(m_text);
167 m_inventory->serialize(os);
169 std::string LockingChestNodeMetadata::infoText()
171 return "Locking Chest";
173 bool LockingChestNodeMetadata::nodeRemovalDisabled()
176 Disable removal if chest contains something
178 InventoryList *list = m_inventory->getList("0");
181 if(list->getUsedSlots() == 0)
185 std::string LockingChestNodeMetadata::getInventoryDrawSpecString()
189 "list[current_name;0;0,0;8,4;]"
190 "list[current_player;main;0,5;8,4;]";
198 FurnaceNodeMetadata proto_FurnaceNodeMetadata(NULL);
200 FurnaceNodeMetadata::FurnaceNodeMetadata(IGameDef *gamedef):
201 NodeMetadata(gamedef)
203 NodeMetadata::registerType(typeId(), typeName(), create);
205 m_inventory = new Inventory();
206 m_inventory->addList("fuel", 1);
207 m_inventory->addList("src", 1);
208 m_inventory->addList("dst", 4);
210 m_step_accumulator = 0;
211 m_fuel_totaltime = 0;
216 FurnaceNodeMetadata::~FurnaceNodeMetadata()
220 u16 FurnaceNodeMetadata::typeId() const
222 return NODEMETA_FURNACE;
224 NodeMetadata* FurnaceNodeMetadata::clone(IGameDef *gamedef)
226 FurnaceNodeMetadata *d = new FurnaceNodeMetadata(m_gamedef);
227 *d->m_inventory = *m_inventory;
230 NodeMetadata* FurnaceNodeMetadata::create(std::istream &is, IGameDef *gamedef)
232 FurnaceNodeMetadata *d = new FurnaceNodeMetadata(gamedef);
234 d->m_inventory->deSerialize(is, gamedef);
238 d->m_fuel_totaltime = (float)temp/10;
240 d->m_fuel_time = (float)temp/10;
244 void FurnaceNodeMetadata::serializeBody(std::ostream &os)
246 m_inventory->serialize(os);
247 os<<itos(m_fuel_totaltime*10)<<" ";
248 os<<itos(m_fuel_time*10)<<" ";
250 std::string FurnaceNodeMetadata::infoText()
253 if(m_fuel_time >= m_fuel_totaltime)
255 const InventoryList *src_list = m_inventory->getList("src");
257 const InventoryItem *src_item = src_list->getItem(0);
259 if(src_item && src_item->isCookable()) {
260 InventoryList *dst_list = m_inventory->getList("dst");
261 if(!dst_list->roomForCookedItem(src_item))
262 return "Furnace is overloaded";
263 return "Furnace is out of fuel";
266 return "Furnace is inactive";
270 std::string s = "Furnace is active";
271 // Do this so it doesn't always show (0%) for weak fuel
272 if(m_fuel_totaltime > 3) {
274 s += itos(m_fuel_time/m_fuel_totaltime*100);
280 bool FurnaceNodeMetadata::nodeRemovalDisabled()
283 Disable removal if furnace is not empty
285 InventoryList *list[3] = {m_inventory->getList("src"),
286 m_inventory->getList("dst"), m_inventory->getList("fuel")};
288 for(int i = 0; i < 3; i++) {
291 if(list[i]->getUsedSlots() == 0)
298 void FurnaceNodeMetadata::inventoryModified()
300 infostream<<"Furnace inventory modification callback"<<std::endl;
302 bool FurnaceNodeMetadata::step(float dtime)
305 infostream<<"Furnace stepping a long time ("<<dtime<<")"<<std::endl;
306 // Update at a fixed frequency
307 const float interval = 2.0;
308 m_step_accumulator += dtime;
309 bool changed = false;
310 while(m_step_accumulator > interval)
312 m_step_accumulator -= interval;
315 //infostream<<"Furnace step dtime="<<dtime<<std::endl;
317 InventoryList *dst_list = m_inventory->getList("dst");
320 InventoryList *src_list = m_inventory->getList("src");
322 InventoryItem *src_item = src_list->getItem(0);
324 bool room_available = false;
326 if(src_item && src_item->isCookable())
327 room_available = dst_list->roomForCookedItem(src_item);
329 // Start only if there are free slots in dst, so that it can
330 // accomodate any result item
333 m_src_totaltime = src_item->getCookTime();
342 If fuel is burning, increment the burn counters.
343 If item finishes cooking, move it to result.
345 if(m_fuel_time < m_fuel_totaltime)
347 //infostream<<"Furnace is active"<<std::endl;
348 m_fuel_time += dtime;
350 if(m_src_time >= m_src_totaltime && m_src_totaltime > 0.001
353 InventoryItem *cookresult = src_item->createCookResult();
354 dst_list->addItem(cookresult);
355 src_list->decrementMaterials(1);
361 // If the fuel was not used up this step, just keep burning it
362 if(m_fuel_time < m_fuel_totaltime)
367 Get the source again in case it has all burned
369 src_item = src_list->getItem(0);
372 If there is no source item, or the source item is not cookable,
373 or the furnace is still cooking, or the furnace became overloaded, stop loop.
375 if(src_item == NULL || !room_available || m_fuel_time < m_fuel_totaltime ||
376 dst_list->roomForCookedItem(src_item) == false)
378 m_step_accumulator = 0;
382 //infostream<<"Furnace is out of fuel"<<std::endl;
384 InventoryList *fuel_list = m_inventory->getList("fuel");
386 const InventoryItem *fuel_item = fuel_list->getItem(0);
388 if(fuel_item && fuel_item->getBurnTime() >= 0){
389 m_fuel_totaltime = fuel_item->getBurnTime();
391 fuel_list->decrementMaterials(1);
394 //infostream<<"No fuel found"<<std::endl;
395 // No fuel, stop loop.
396 m_step_accumulator = 0;
402 std::string FurnaceNodeMetadata::getInventoryDrawSpecString()
406 "list[current_name;fuel;2,3;1,1;]"
407 "list[current_name;src;2,1;1,1;]"
408 "list[current_name;dst;5,1;2,2;]"
409 "list[current_player;main;0,5;8,4;]";