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"
23 #include "inventory.h"
29 #define NODEMETA_GENERIC 1
30 #define NODEMETA_SIGN 14
31 #define NODEMETA_CHEST 15
32 #define NODEMETA_FURNACE 16
33 #define NODEMETA_LOCKABLE_CHEST 17
35 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
36 core::map<std::string, NodeMetadata::Factory2> NodeMetadata::m_names;
38 class SignNodeMetadata : public NodeMetadata
41 SignNodeMetadata(IGameDef *gamedef, std::string text);
42 //~SignNodeMetadata();
44 virtual u16 typeId() const;
45 virtual const char* typeName() const
47 static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
48 static NodeMetadata* create(IGameDef *gamedef);
49 virtual NodeMetadata* clone(IGameDef *gamedef);
50 virtual void serializeBody(std::ostream &os);
51 virtual std::string infoText();
53 virtual bool allowsTextInput(){ return true; }
54 virtual std::string getText(){ return m_text; }
55 virtual void setText(const std::string &t){ m_text = t; }
61 class ChestNodeMetadata : public NodeMetadata
64 ChestNodeMetadata(IGameDef *gamedef);
67 virtual u16 typeId() const;
68 virtual const char* typeName() const
70 static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
71 static NodeMetadata* create(IGameDef *gamedef);
72 virtual NodeMetadata* clone(IGameDef *gamedef);
73 virtual void serializeBody(std::ostream &os);
74 virtual std::string infoText();
75 virtual Inventory* getInventory() {return m_inventory;}
76 virtual bool nodeRemovalDisabled();
77 virtual std::string getInventoryDrawSpecString();
80 Inventory *m_inventory;
83 class LockingChestNodeMetadata : public NodeMetadata
86 LockingChestNodeMetadata(IGameDef *gamedef);
87 ~LockingChestNodeMetadata();
89 virtual u16 typeId() const;
90 virtual const char* typeName() const
91 { return "locked_chest"; }
92 static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
93 static NodeMetadata* create(IGameDef *gamedef);
94 virtual NodeMetadata* clone(IGameDef *gamedef);
95 virtual void serializeBody(std::ostream &os);
96 virtual std::string infoText();
97 virtual Inventory* getInventory() {return m_inventory;}
98 virtual bool nodeRemovalDisabled();
99 virtual std::string getInventoryDrawSpecString();
101 virtual std::string getOwner(){ return m_text; }
102 virtual void setOwner(std::string t){ m_text = t; }
105 Inventory *m_inventory;
109 class FurnaceNodeMetadata : public NodeMetadata
112 FurnaceNodeMetadata(IGameDef *gamedef);
113 ~FurnaceNodeMetadata();
115 virtual u16 typeId() const;
116 virtual const char* typeName() const
117 { return "furnace"; }
118 virtual NodeMetadata* clone(IGameDef *gamedef);
119 static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
120 static NodeMetadata* create(IGameDef *gamedef);
121 virtual void serializeBody(std::ostream &os);
122 virtual std::string infoText();
123 virtual Inventory* getInventory() {return m_inventory;}
124 virtual void inventoryModified();
125 virtual bool step(float dtime);
126 virtual bool nodeRemovalDisabled();
127 virtual std::string getInventoryDrawSpecString();
130 Inventory *m_inventory;
131 float m_step_accumulator;
132 float m_fuel_totaltime;
134 float m_src_totaltime;
143 SignNodeMetadata proto_SignNodeMetadata(NULL, "");
145 SignNodeMetadata::SignNodeMetadata(IGameDef *gamedef, std::string text):
146 NodeMetadata(gamedef),
149 NodeMetadata::registerType(typeId(), typeName(), create, create);
151 u16 SignNodeMetadata::typeId() const
153 return NODEMETA_SIGN;
155 NodeMetadata* SignNodeMetadata::create(std::istream &is, IGameDef *gamedef)
157 std::string text = deSerializeString(is);
158 return new SignNodeMetadata(gamedef, text);
160 NodeMetadata* SignNodeMetadata::create(IGameDef *gamedef)
162 return new SignNodeMetadata(gamedef, "");
164 NodeMetadata* SignNodeMetadata::clone(IGameDef *gamedef)
166 return new SignNodeMetadata(gamedef, m_text);
168 void SignNodeMetadata::serializeBody(std::ostream &os)
170 os<<serializeString(m_text);
172 std::string SignNodeMetadata::infoText()
174 return std::string("\"")+m_text+"\"";
182 ChestNodeMetadata proto_ChestNodeMetadata(NULL);
184 ChestNodeMetadata::ChestNodeMetadata(IGameDef *gamedef):
185 NodeMetadata(gamedef)
187 NodeMetadata::registerType(typeId(), typeName(), create, create);
189 m_inventory = new Inventory();
190 m_inventory->addList("0", 8*4);
192 ChestNodeMetadata::~ChestNodeMetadata()
196 u16 ChestNodeMetadata::typeId() const
198 return NODEMETA_CHEST;
200 NodeMetadata* ChestNodeMetadata::create(std::istream &is, IGameDef *gamedef)
202 ChestNodeMetadata *d = new ChestNodeMetadata(gamedef);
203 d->m_inventory->deSerialize(is, gamedef);
206 NodeMetadata* ChestNodeMetadata::create(IGameDef *gamedef)
208 ChestNodeMetadata *d = new ChestNodeMetadata(gamedef);
211 NodeMetadata* ChestNodeMetadata::clone(IGameDef *gamedef)
213 ChestNodeMetadata *d = new ChestNodeMetadata(gamedef);
214 *d->m_inventory = *m_inventory;
217 void ChestNodeMetadata::serializeBody(std::ostream &os)
219 m_inventory->serialize(os);
221 std::string ChestNodeMetadata::infoText()
225 bool ChestNodeMetadata::nodeRemovalDisabled()
228 Disable removal if chest contains something
230 InventoryList *list = m_inventory->getList("0");
233 if(list->getUsedSlots() == 0)
237 std::string ChestNodeMetadata::getInventoryDrawSpecString()
241 "list[current_name;0;0,0;8,4;]"
242 "list[current_player;main;0,5;8,4;]";
246 LockingChestNodeMetadata
250 LockingChestNodeMetadata proto_LockingChestNodeMetadata(NULL);
252 LockingChestNodeMetadata::LockingChestNodeMetadata(IGameDef *gamedef):
253 NodeMetadata(gamedef)
255 NodeMetadata::registerType(typeId(), typeName(), create, create);
257 m_inventory = new Inventory();
258 m_inventory->addList("0", 8*4);
260 LockingChestNodeMetadata::~LockingChestNodeMetadata()
264 u16 LockingChestNodeMetadata::typeId() const
266 return NODEMETA_LOCKABLE_CHEST;
268 NodeMetadata* LockingChestNodeMetadata::create(std::istream &is, IGameDef *gamedef)
270 LockingChestNodeMetadata *d = new LockingChestNodeMetadata(gamedef);
271 d->setOwner(deSerializeString(is));
272 d->m_inventory->deSerialize(is, gamedef);
275 NodeMetadata* LockingChestNodeMetadata::create(IGameDef *gamedef)
277 LockingChestNodeMetadata *d = new LockingChestNodeMetadata(gamedef);
280 NodeMetadata* LockingChestNodeMetadata::clone(IGameDef *gamedef)
282 LockingChestNodeMetadata *d = new LockingChestNodeMetadata(gamedef);
283 *d->m_inventory = *m_inventory;
286 void LockingChestNodeMetadata::serializeBody(std::ostream &os)
288 os<<serializeString(m_text);
289 m_inventory->serialize(os);
291 std::string LockingChestNodeMetadata::infoText()
293 return "Locking Chest";
295 bool LockingChestNodeMetadata::nodeRemovalDisabled()
298 Disable removal if chest contains something
300 InventoryList *list = m_inventory->getList("0");
303 if(list->getUsedSlots() == 0)
307 std::string LockingChestNodeMetadata::getInventoryDrawSpecString()
311 "list[current_name;0;0,0;8,4;]"
312 "list[current_player;main;0,5;8,4;]";
320 FurnaceNodeMetadata proto_FurnaceNodeMetadata(NULL);
322 FurnaceNodeMetadata::FurnaceNodeMetadata(IGameDef *gamedef):
323 NodeMetadata(gamedef)
325 NodeMetadata::registerType(typeId(), typeName(), create, create);
327 m_inventory = new Inventory();
328 m_inventory->addList("fuel", 1);
329 m_inventory->addList("src", 1);
330 m_inventory->addList("dst", 4);
332 m_step_accumulator = 0;
333 m_fuel_totaltime = 0;
338 FurnaceNodeMetadata::~FurnaceNodeMetadata()
342 u16 FurnaceNodeMetadata::typeId() const
344 return NODEMETA_FURNACE;
346 NodeMetadata* FurnaceNodeMetadata::clone(IGameDef *gamedef)
348 FurnaceNodeMetadata *d = new FurnaceNodeMetadata(m_gamedef);
349 *d->m_inventory = *m_inventory;
352 NodeMetadata* FurnaceNodeMetadata::create(std::istream &is, IGameDef *gamedef)
354 FurnaceNodeMetadata *d = new FurnaceNodeMetadata(gamedef);
356 d->m_inventory->deSerialize(is, gamedef);
360 d->m_fuel_totaltime = (float)temp/10;
362 d->m_fuel_time = (float)temp/10;
366 NodeMetadata* FurnaceNodeMetadata::create(IGameDef *gamedef)
368 FurnaceNodeMetadata *d = new FurnaceNodeMetadata(gamedef);
371 void FurnaceNodeMetadata::serializeBody(std::ostream &os)
373 m_inventory->serialize(os);
374 os<<itos(m_fuel_totaltime*10)<<" ";
375 os<<itos(m_fuel_time*10)<<" ";
377 std::string FurnaceNodeMetadata::infoText()
380 if(m_fuel_time >= m_fuel_totaltime)
382 const InventoryList *src_list = m_inventory->getList("src");
384 const InventoryItem *src_item = src_list->getItem(0);
386 if(src_item && src_item->isCookable()) {
387 InventoryList *dst_list = m_inventory->getList("dst");
388 if(!dst_list->roomForCookedItem(src_item))
389 return "Furnace is overloaded";
390 return "Furnace is out of fuel";
393 return "Furnace is inactive";
397 std::string s = "Furnace is active";
398 // Do this so it doesn't always show (0%) for weak fuel
399 if(m_fuel_totaltime > 3) {
401 s += itos(m_fuel_time/m_fuel_totaltime*100);
407 bool FurnaceNodeMetadata::nodeRemovalDisabled()
410 Disable removal if furnace is not empty
412 InventoryList *list[3] = {m_inventory->getList("src"),
413 m_inventory->getList("dst"), m_inventory->getList("fuel")};
415 for(int i = 0; i < 3; i++) {
418 if(list[i]->getUsedSlots() == 0)
425 void FurnaceNodeMetadata::inventoryModified()
427 infostream<<"Furnace inventory modification callback"<<std::endl;
429 bool FurnaceNodeMetadata::step(float dtime)
432 infostream<<"Furnace stepping a long time ("<<dtime<<")"<<std::endl;
433 // Update at a fixed frequency
434 const float interval = 2.0;
435 m_step_accumulator += dtime;
436 bool changed = false;
437 while(m_step_accumulator > interval)
439 m_step_accumulator -= interval;
442 //infostream<<"Furnace step dtime="<<dtime<<std::endl;
444 InventoryList *dst_list = m_inventory->getList("dst");
447 InventoryList *src_list = m_inventory->getList("src");
449 InventoryItem *src_item = src_list->getItem(0);
451 bool room_available = false;
453 if(src_item && src_item->isCookable())
454 room_available = dst_list->roomForCookedItem(src_item);
456 // Start only if there are free slots in dst, so that it can
457 // accomodate any result item
460 m_src_totaltime = src_item->getCookTime();
469 If fuel is burning, increment the burn counters.
470 If item finishes cooking, move it to result.
472 if(m_fuel_time < m_fuel_totaltime)
474 //infostream<<"Furnace is active"<<std::endl;
475 m_fuel_time += dtime;
477 if(m_src_time >= m_src_totaltime && m_src_totaltime > 0.001
480 InventoryItem *cookresult = src_item->createCookResult();
481 dst_list->addItem(cookresult);
482 src_list->decrementMaterials(1);
488 // If the fuel was not used up this step, just keep burning it
489 if(m_fuel_time < m_fuel_totaltime)
494 Get the source again in case it has all burned
496 src_item = src_list->getItem(0);
499 If there is no source item, or the source item is not cookable,
500 or the furnace is still cooking, or the furnace became overloaded, stop loop.
502 if(src_item == NULL || !room_available || m_fuel_time < m_fuel_totaltime ||
503 dst_list->roomForCookedItem(src_item) == false)
505 m_step_accumulator = 0;
509 //infostream<<"Furnace is out of fuel"<<std::endl;
511 InventoryList *fuel_list = m_inventory->getList("fuel");
513 const InventoryItem *fuel_item = fuel_list->getItem(0);
515 if(fuel_item && fuel_item->getBurnTime() >= 0){
516 m_fuel_totaltime = fuel_item->getBurnTime();
518 fuel_list->decrementMaterials(1);
521 //infostream<<"No fuel found"<<std::endl;
522 // No fuel, stop loop.
523 m_step_accumulator = 0;
529 std::string FurnaceNodeMetadata::getInventoryDrawSpecString()
533 "list[current_name;fuel;2,3;1,1;]"
534 "list[current_name;src;2,1;1,1;]"
535 "list[current_name;dst;5,1;2,2;]"
536 "list[current_player;main;0,5;8,4;]";
543 class GenericNodeMetadata : public NodeMetadata
546 Inventory *m_inventory;
550 std::string m_infotext;
551 std::string m_inventorydrawspec;
552 bool m_allow_text_input;
553 bool m_removal_disabled;
554 bool m_enforce_owner;
556 bool m_inventory_modified;
557 bool m_text_modified;
559 std::map<std::string, std::string> m_stringvars;
564 return NODEMETA_GENERIC;
566 const char* typeName() const
571 GenericNodeMetadata(IGameDef *gamedef):
572 NodeMetadata(gamedef),
574 m_inventory(new Inventory()),
578 m_infotext("GenericNodeMetadata"),
579 m_inventorydrawspec(""),
580 m_allow_text_input(false),
581 m_removal_disabled(false),
582 m_enforce_owner(false),
584 m_inventory_modified(false),
585 m_text_modified(false)
587 NodeMetadata::registerType(typeId(), typeName(), create, create);
589 virtual ~GenericNodeMetadata()
593 NodeMetadata* clone(IGameDef *gamedef)
595 GenericNodeMetadata *d = new GenericNodeMetadata(m_gamedef);
597 *d->m_inventory = *m_inventory;
599 d->m_owner = m_owner;
601 d->m_infotext = m_infotext;
602 d->m_inventorydrawspec = m_inventorydrawspec;
603 d->m_allow_text_input = m_allow_text_input;
604 d->m_removal_disabled = m_removal_disabled;
605 d->m_enforce_owner = m_enforce_owner;
606 d->m_inventory_modified = m_inventory_modified;
607 d->m_text_modified = m_text_modified;
610 static NodeMetadata* create(IGameDef *gamedef)
612 GenericNodeMetadata *d = new GenericNodeMetadata(gamedef);
615 static NodeMetadata* create(std::istream &is, IGameDef *gamedef)
617 GenericNodeMetadata *d = new GenericNodeMetadata(gamedef);
619 d->m_inventory->deSerialize(is, gamedef);
620 d->m_text = deSerializeLongString(is);
621 d->m_owner = deSerializeString(is);
623 d->m_infotext = deSerializeString(is);
624 d->m_inventorydrawspec = deSerializeString(is);
625 d->m_allow_text_input = readU8(is);
626 d->m_removal_disabled = readU8(is);
627 d->m_enforce_owner = readU8(is);
629 int num_vars = readU32(is);
630 for(int i=0; i<num_vars; i++){
631 std::string name = deSerializeString(is);
632 std::string var = deSerializeLongString(is);
633 d->m_stringvars[name] = var;
638 void serializeBody(std::ostream &os)
640 m_inventory->serialize(os);
641 os<<serializeLongString(m_text);
642 os<<serializeString(m_owner);
644 os<<serializeString(m_infotext);
645 os<<serializeString(m_inventorydrawspec);
646 writeU8(os, m_allow_text_input);
647 writeU8(os, m_removal_disabled);
648 writeU8(os, m_enforce_owner);
650 int num_vars = m_stringvars.size();
651 writeU32(os, num_vars);
652 for(std::map<std::string, std::string>::iterator
653 i = m_stringvars.begin(); i != m_stringvars.end(); i++){
654 os<<serializeString(i->first);
655 os<<serializeLongString(i->second);
659 std::string infoText()
663 Inventory* getInventory()
667 void inventoryModified()
669 m_inventory_modified = true;
671 bool step(float dtime)
675 bool nodeRemovalDisabled()
677 return m_removal_disabled;
679 std::string getInventoryDrawSpecString()
681 return m_inventorydrawspec;
683 bool allowsTextInput()
685 return m_allow_text_input;
687 std::string getText()
691 void setText(const std::string &t)
694 m_text_modified = true;
696 std::string getOwner()
703 void setOwner(std::string t)
708 /* Interface for GenericNodeMetadata */
710 void setInfoText(const std::string &text)
712 infostream<<"GenericNodeMetadata::setInfoText(\""
713 <<text<<"\")"<<std::endl;
716 void setInventoryDrawSpec(const std::string &text)
718 m_inventorydrawspec = text;
720 void setAllowTextInput(bool b)
722 m_allow_text_input = b;
724 void setRemovalDisabled(bool b)
726 m_removal_disabled = b;
728 void setEnforceOwner(bool b)
732 bool isInventoryModified()
734 return m_inventory_modified;
736 void resetInventoryModified()
738 m_inventory_modified = false;
740 bool isTextModified()
742 return m_text_modified;
744 void resetTextModified()
746 m_text_modified = false;
748 void setString(const std::string &name, const std::string &var)
750 m_stringvars[name] = var;
752 std::string getString(const std::string &name)
754 std::map<std::string, std::string>::iterator i;
755 i = m_stringvars.find(name);
756 if(i == m_stringvars.end())
763 GenericNodeMetadata proto_GenericNodeMetadata(NULL);