Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 322 lines (290 sloc) 7.342 kB
da69235 @celeron55 Created and moved stuff to content_nodemeta.{h,cpp}
celeron55 authored
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "content_nodemeta.h"
21 #include "inventory.h"
22 #include "content_mapnode.h"
23
24 /*
25 SignNodeMetadata
26 */
27
28 // Prototype
29 SignNodeMetadata proto_SignNodeMetadata("");
30
31 SignNodeMetadata::SignNodeMetadata(std::string text):
32 m_text(text)
33 {
34 NodeMetadata::registerType(typeId(), create);
35 }
36 u16 SignNodeMetadata::typeId() const
37 {
38 return CONTENT_SIGN_WALL;
39 }
40 NodeMetadata* SignNodeMetadata::create(std::istream &is)
41 {
42 std::string text = deSerializeString(is);
43 return new SignNodeMetadata(text);
44 }
45 NodeMetadata* SignNodeMetadata::clone()
46 {
47 return new SignNodeMetadata(m_text);
48 }
49 void SignNodeMetadata::serializeBody(std::ostream &os)
50 {
51 os<<serializeString(m_text);
52 }
53 std::string SignNodeMetadata::infoText()
54 {
55 return std::string("\"")+m_text+"\"";
56 }
57
58 /*
59 ChestNodeMetadata
60 */
61
62 // Prototype
63 ChestNodeMetadata proto_ChestNodeMetadata;
64
65 ChestNodeMetadata::ChestNodeMetadata()
66 {
67 NodeMetadata::registerType(typeId(), create);
68
69 m_inventory = new Inventory();
70 m_inventory->addList("0", 8*4);
71 }
72 ChestNodeMetadata::~ChestNodeMetadata()
73 {
74 delete m_inventory;
75 }
76 u16 ChestNodeMetadata::typeId() const
77 {
78 return CONTENT_CHEST;
79 }
80 NodeMetadata* ChestNodeMetadata::create(std::istream &is)
81 {
82 ChestNodeMetadata *d = new ChestNodeMetadata();
83 d->m_inventory->deSerialize(is);
84 return d;
85 }
86 NodeMetadata* ChestNodeMetadata::clone()
87 {
88 ChestNodeMetadata *d = new ChestNodeMetadata();
89 *d->m_inventory = *m_inventory;
90 return d;
91 }
92 void ChestNodeMetadata::serializeBody(std::ostream &os)
93 {
94 m_inventory->serialize(os);
95 }
96 std::string ChestNodeMetadata::infoText()
97 {
98 return "Chest";
99 }
100 bool ChestNodeMetadata::nodeRemovalDisabled()
101 {
102 /*
103 Disable removal if chest contains something
104 */
105 InventoryList *list = m_inventory->getList("0");
106 if(list == NULL)
107 return false;
108 if(list->getUsedSlots() == 0)
109 return false;
110 return true;
111 }
9314746 @celeron55 moved inventory menu definition of chest and furnace to content_nodem…
celeron55 authored
112 std::string ChestNodeMetadata::getInventoryDrawSpecString()
113 {
114 return
115 "invsize[8,9;]"
116 "list[current_name;0;0,0;8,4;]"
117 "list[current_player;main;0,5;8,4;]";
118 }
da69235 @celeron55 Created and moved stuff to content_nodemeta.{h,cpp}
celeron55 authored
119
120 /*
121 FurnaceNodeMetadata
122 */
123
124 // Prototype
125 FurnaceNodeMetadata proto_FurnaceNodeMetadata;
126
127 FurnaceNodeMetadata::FurnaceNodeMetadata()
128 {
129 NodeMetadata::registerType(typeId(), create);
130
131 m_inventory = new Inventory();
132 m_inventory->addList("fuel", 1);
133 m_inventory->addList("src", 1);
134 m_inventory->addList("dst", 4);
135
136 m_step_accumulator = 0;
137 m_fuel_totaltime = 0;
138 m_fuel_time = 0;
139 m_src_totaltime = 0;
140 m_src_time = 0;
141 }
142 FurnaceNodeMetadata::~FurnaceNodeMetadata()
143 {
144 delete m_inventory;
145 }
146 u16 FurnaceNodeMetadata::typeId() const
147 {
148 return CONTENT_FURNACE;
149 }
150 NodeMetadata* FurnaceNodeMetadata::clone()
151 {
152 FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
153 *d->m_inventory = *m_inventory;
154 return d;
155 }
156 NodeMetadata* FurnaceNodeMetadata::create(std::istream &is)
157 {
158 FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
159
160 d->m_inventory->deSerialize(is);
161
162 int temp;
163 is>>temp;
164 d->m_fuel_totaltime = (float)temp/10;
165 is>>temp;
166 d->m_fuel_time = (float)temp/10;
167
168 return d;
169 }
170 void FurnaceNodeMetadata::serializeBody(std::ostream &os)
171 {
172 m_inventory->serialize(os);
173 os<<itos(m_fuel_totaltime*10)<<" ";
174 os<<itos(m_fuel_time*10)<<" ";
175 }
176 std::string FurnaceNodeMetadata::infoText()
177 {
178 //return "Furnace";
179 if(m_fuel_time >= m_fuel_totaltime)
180 {
181 InventoryList *src_list = m_inventory->getList("src");
182 assert(src_list);
183 InventoryItem *src_item = src_list->getItem(0);
184
185 if(src_item)
186 return "Furnace is out of fuel";
187 else
188 return "Furnace is inactive";
189 }
190 else
191 {
192 std::string s = "Furnace is active (";
193 s += itos(m_fuel_time/m_fuel_totaltime*100);
194 s += "%)";
195 return s;
196 }
197 }
198 void FurnaceNodeMetadata::inventoryModified()
199 {
200 dstream<<"Furnace inventory modification callback"<<std::endl;
201 }
202 bool FurnaceNodeMetadata::step(float dtime)
203 {
204 if(dtime > 60.0)
205 dstream<<"Furnace stepping a long time ("<<dtime<<")"<<std::endl;
206 // Update at a fixed frequency
207 const float interval = 2.0;
208 m_step_accumulator += dtime;
209 bool changed = false;
210 while(m_step_accumulator > interval)
211 {
212 m_step_accumulator -= interval;
213 dtime = interval;
214
215 //dstream<<"Furnace step dtime="<<dtime<<std::endl;
216
217 InventoryList *dst_list = m_inventory->getList("dst");
218 assert(dst_list);
219
220 InventoryList *src_list = m_inventory->getList("src");
221 assert(src_list);
222 InventoryItem *src_item = src_list->getItem(0);
223
224 // Start only if there are free slots in dst, so that it can
225 // accomodate any result item
226 if(dst_list->getFreeSlots() > 0 && src_item && src_item->isCookable())
227 {
228 m_src_totaltime = 3;
229 }
230 else
231 {
232 m_src_time = 0;
233 m_src_totaltime = 0;
234 }
235
236 /*
237 If fuel is burning, increment the burn counters.
238 If item finishes cooking, move it to result.
239 */
240 if(m_fuel_time < m_fuel_totaltime)
241 {
242 //dstream<<"Furnace is active"<<std::endl;
243 m_fuel_time += dtime;
244 m_src_time += dtime;
245 if(m_src_time >= m_src_totaltime && m_src_totaltime > 0.001
246 && src_item)
247 {
248 InventoryItem *cookresult = src_item->createCookResult();
249 dst_list->addItem(cookresult);
250 src_list->decrementMaterials(1);
251 m_src_time = 0;
252 m_src_totaltime = 0;
253 }
254 changed = true;
255 continue;
256 }
257
258 /*
259 If there is no source item or source item is not cookable, stop loop.
260 */
261 if(src_item == NULL || m_src_totaltime < 0.001)
262 {
263 m_step_accumulator = 0;
264 break;
265 }
266
267 //dstream<<"Furnace is out of fuel"<<std::endl;
268
269 InventoryList *fuel_list = m_inventory->getList("fuel");
270 assert(fuel_list);
271 InventoryItem *fuel_item = fuel_list->getItem(0);
272
273 if(ItemSpec(ITEM_MATERIAL, CONTENT_TREE).checkItem(fuel_item))
274 {
275 m_fuel_totaltime = 30;
276 m_fuel_time = 0;
277 fuel_list->decrementMaterials(1);
278 changed = true;
279 }
280 else if(ItemSpec(ITEM_MATERIAL, CONTENT_WOOD).checkItem(fuel_item))
281 {
282 m_fuel_totaltime = 30/4;
283 m_fuel_time = 0;
284 fuel_list->decrementMaterials(1);
285 changed = true;
286 }
287 else if(ItemSpec(ITEM_CRAFT, "Stick").checkItem(fuel_item))
288 {
289 m_fuel_totaltime = 30/4/4;
290 m_fuel_time = 0;
291 fuel_list->decrementMaterials(1);
292 changed = true;
293 }
294 else if(ItemSpec(ITEM_CRAFT, "lump_of_coal").checkItem(fuel_item))
295 {
296 m_fuel_totaltime = 40;
297 m_fuel_time = 0;
298 fuel_list->decrementMaterials(1);
299 changed = true;
300 }
301 else
302 {
303 //dstream<<"No fuel found"<<std::endl;
304 // No fuel, stop loop.
305 m_step_accumulator = 0;
306 break;
307 }
308 }
309 return changed;
310 }
9314746 @celeron55 moved inventory menu definition of chest and furnace to content_nodem…
celeron55 authored
311 std::string FurnaceNodeMetadata::getInventoryDrawSpecString()
312 {
313 return
314 "invsize[8,9;]"
315 "list[current_name;fuel;2,4;1,1;]"
316 "list[current_name;src;2,1;1,1;]"
317 "list[current_name;dst;5,1;2,2;]"
318 "list[current_player;main;0,5;8,4;]";
319 }
da69235 @celeron55 Created and moved stuff to content_nodemeta.{h,cpp}
celeron55 authored
320
321
Something went wrong with that request. Please try again.