073477dde4cc9a2f08323a6f5b52baa3660b5697
[oweals/minetest.git] / src / inventory.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 /*
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
22 */
23
24 #include "inventory.h"
25 #include "serialization.h"
26 #include "utility.h"
27 #include "debug.h"
28 #include <sstream>
29 #include "main.h"
30
31 /*
32         InventoryItem
33 */
34
35 InventoryItem::InventoryItem()
36 {
37 }
38
39 InventoryItem::~InventoryItem()
40 {
41 }
42
43 InventoryItem* InventoryItem::deSerialize(std::istream &is)
44 {
45         DSTACK(__FUNCTION_NAME);
46
47         //is.imbue(std::locale("C"));
48         // Read name
49         std::string name;
50         std::getline(is, name, ' ');
51         
52         if(name == "MaterialItem")
53         {
54                 // u16 reads directly as a number (u8 doesn't)
55                 u16 material;
56                 is>>material;
57                 u16 count;
58                 is>>count;
59                 if(material > 255)
60                         throw SerializationError("Too large material number");
61                 return new MaterialItem(material, count);
62         }
63         else if(name == "MBOItem")
64         {
65                 std::string inventorystring;
66                 std::getline(is, inventorystring, '|');
67                 return new MapBlockObjectItem(inventorystring);
68         }
69         else
70         {
71                 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
72                 throw SerializationError("Unknown InventoryItem name");
73         }
74 }
75
76 /*
77         MapBlockObjectItem
78 */
79 #ifndef SERVER
80 video::ITexture * MapBlockObjectItem::getImage()
81 {
82         if(m_inventorystring.substr(0,3) == "Rat")
83                 //return g_device->getVideoDriver()->getTexture("../data/rat.png");
84                 return g_irrlicht->getTexture("../data/rat.png");
85         
86         if(m_inventorystring.substr(0,4) == "Sign")
87                 //return g_device->getVideoDriver()->getTexture("../data/sign.png");
88                 return g_irrlicht->getTexture("../data/sign.png");
89
90         return NULL;
91 }
92 #endif
93 std::string MapBlockObjectItem::getText()
94 {
95         if(m_inventorystring.substr(0,3) == "Rat")
96                 return "";
97         
98         if(m_inventorystring.substr(0,4) == "Sign")
99                 return "";
100
101         return "obj";
102 }
103
104 MapBlockObject * MapBlockObjectItem::createObject
105                 (v3f pos, f32 player_yaw, f32 player_pitch)
106 {
107         std::istringstream is(m_inventorystring);
108         std::string name;
109         std::getline(is, name, ' ');
110         
111         if(name == "None")
112         {
113                 return NULL;
114         }
115         else if(name == "Sign")
116         {
117                 std::string text;
118                 std::getline(is, text, '|');
119                 SignObject *obj = new SignObject(NULL, -1, pos);
120                 obj->setText(text);
121                 obj->setYaw(-player_yaw);
122                 return obj;
123         }
124         else if(name == "Rat")
125         {
126                 RatObject *obj = new RatObject(NULL, -1, pos);
127                 return obj;
128         }
129         else
130         {
131                 return NULL;
132         }
133 }
134
135 /*
136         Inventory
137 */
138
139 InventoryList::InventoryList(std::string name, u32 size)
140 {
141         m_name = name;
142         m_size = size;
143         clearItems();
144 }
145
146 InventoryList::~InventoryList()
147 {
148         for(u32 i=0; i<m_items.size(); i++)
149         {
150                 if(m_items[i])
151                         delete m_items[i];
152         }
153 }
154
155 void InventoryList::clearItems()
156 {
157         for(u32 i=0; i<m_items.size(); i++)
158         {
159                 if(m_items[i])
160                         delete m_items[i];
161         }
162
163         m_items.clear();
164
165         for(u32 i=0; i<m_size; i++)
166         {
167                 m_items.push_back(NULL);
168         }
169 }
170
171 void InventoryList::serialize(std::ostream &os)
172 {
173         //os.imbue(std::locale("C"));
174         
175         for(u32 i=0; i<m_items.size(); i++)
176         {
177                 InventoryItem *item = m_items[i];
178                 if(item != NULL)
179                 {
180                         os<<"Item ";
181                         item->serialize(os);
182                 }
183                 else
184                 {
185                         os<<"Empty";
186                 }
187                 os<<"\n";
188         }
189
190         os<<"end\n";
191 }
192
193 void InventoryList::deSerialize(std::istream &is)
194 {
195         //is.imbue(std::locale("C"));
196
197         clearItems();
198         u32 item_i = 0;
199
200         for(;;)
201         {
202                 std::string line;
203                 std::getline(is, line, '\n');
204
205                 std::istringstream iss(line);
206                 //iss.imbue(std::locale("C"));
207
208                 std::string name;
209                 std::getline(iss, name, ' ');
210
211                 if(name == "end")
212                 {
213                         break;
214                 }
215                 else if(name == "Item")
216                 {
217                         if(item_i > getSize() - 1)
218                                 throw SerializationError("too many items");
219                         InventoryItem *item = InventoryItem::deSerialize(iss);
220                         m_items[item_i++] = item;
221                 }
222                 else if(name == "Empty")
223                 {
224                         if(item_i > getSize() - 1)
225                                 throw SerializationError("too many items");
226                         m_items[item_i++] = NULL;
227                 }
228                 else
229                 {
230                         throw SerializationError("Unknown inventory identifier");
231                 }
232         }
233 }
234
235 InventoryList::InventoryList(const InventoryList &other)
236 {
237         /*
238                 Do this so that the items get cloned. Otherwise the pointers
239                 in the array will just get copied.
240         */
241         *this = other;
242 }
243
244 InventoryList & InventoryList::operator = (const InventoryList &other)
245 {
246         m_name = other.m_name;
247         m_size = other.m_size;
248         clearItems();
249         for(u32 i=0; i<other.m_items.size(); i++)
250         {
251                 InventoryItem *item = other.m_items[i];
252                 if(item != NULL)
253                 {
254                         m_items[i] = item->clone();
255                 }
256         }
257
258         return *this;
259 }
260
261 std::string InventoryList::getName()
262 {
263         return m_name;
264 }
265
266 u32 InventoryList::getSize()
267 {
268         return m_items.size();
269 }
270
271 u32 InventoryList::getUsedSlots()
272 {
273         u32 num = 0;
274         for(u32 i=0; i<m_items.size(); i++)
275         {
276                 InventoryItem *item = m_items[i];
277                 if(item != NULL)
278                         num++;
279         }
280         return num;
281 }
282
283 InventoryItem * InventoryList::getItem(u32 i)
284 {
285         if(i > m_items.size() - 1)
286                 return NULL;
287         return m_items[i];
288 }
289
290 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
291 {
292         assert(i < m_items.size());
293
294         InventoryItem *olditem = m_items[i];
295         m_items[i] = newitem;
296         return olditem;
297 }
298
299 void InventoryList::deleteItem(u32 i)
300 {
301         assert(i < m_items.size());
302         InventoryItem *item = changeItem(i, NULL);
303         if(item)
304                 delete item;
305 }
306
307 bool InventoryList::addItem(InventoryItem *newitem)
308 {
309         // If it is a MaterialItem, try to find an already existing one
310         // and just increment the counter
311         if(std::string("MaterialItem") == newitem->getName())
312         {
313                 u8 material = ((MaterialItem*)newitem)->getMaterial();
314                 u8 count = ((MaterialItem*)newitem)->getCount();
315                 for(u32 i=0; i<m_items.size(); i++)
316                 {
317                         InventoryItem *item2 = m_items[i];
318                         if(item2 == NULL)
319                                 continue;
320                         if(std::string("MaterialItem") != item2->getName())
321                                 continue;
322                         // Found one. Check if it is of the right material and has
323                         // free space
324                         MaterialItem *mitem2 = (MaterialItem*)item2;
325                         if(mitem2->getMaterial() != material)
326                                 continue;
327                         //TODO: Add all that can be added and add remaining part
328                         // to another place
329                         if(mitem2->freeSpace() < count)
330                                 continue;
331                         // Add to the counter
332                         mitem2->add(count);
333                         // Dump the parameter
334                         delete newitem;
335                         return true;
336                 }
337         }
338         // Else find an empty position
339         for(u32 i=0; i<m_items.size(); i++)
340         {
341                 InventoryItem *item = m_items[i];
342                 if(item != NULL)
343                         continue;
344                 m_items[i] = newitem;
345                 return true;
346         }
347         // Failed
348         return false;
349 }
350
351 bool InventoryList::addItem(u32 i, InventoryItem *newitem)
352 {
353         // If it is an empty position, it's an easy job.
354         InventoryItem *item = m_items[i];
355         if(item == NULL)
356         {
357                 m_items[i] = newitem;
358                 return true;
359         }
360
361         // If it is a material item, try to 
362         if(std::string("MaterialItem") == newitem->getName())
363         {
364                 u8 material = ((MaterialItem*)newitem)->getMaterial();
365                 u8 count = ((MaterialItem*)newitem)->getCount();
366                 InventoryItem *item2 = m_items[i];
367
368                 if(item2 != NULL
369                         && std::string("MaterialItem") == item2->getName())
370                 {
371                         // Check if it is of the right material and has free space
372                         MaterialItem *mitem2 = (MaterialItem*)item2;
373                         if(mitem2->getMaterial() == material
374                                         && mitem2->freeSpace() >= count)
375                         {
376                                 // Add to the counter
377                                 mitem2->add(count);
378                                 // Dump the parameter
379                                 delete newitem;
380                                 // Done
381                                 return true;
382                         }
383                 }
384         }
385         
386         return false;
387 }
388
389 void InventoryList::decrementMaterials(u16 count)
390 {
391         for(u32 i=0; i<m_items.size(); i++)
392         {
393                 InventoryItem *item = m_items[i];
394                 if(item == NULL)
395                         continue;
396                 if(std::string("MaterialItem") == item->getName())
397                 {
398                         MaterialItem *mitem = (MaterialItem*)item;
399                         if(mitem->getCount() < count)
400                         {
401                                 dstream<<__FUNCTION_NAME<<": decrementMaterials():"
402                                                 <<" too small material count"<<std::endl;
403                         }
404                         else if(mitem->getCount() == count)
405                         {
406                                 deleteItem(i);
407                         }
408                         else
409                         {
410                                 mitem->remove(1);
411                         }
412                 }
413         }
414 }
415
416 void InventoryList::print(std::ostream &o)
417 {
418         o<<"InventoryList:"<<std::endl;
419         for(u32 i=0; i<m_items.size(); i++)
420         {
421                 InventoryItem *item = m_items[i];
422                 if(item != NULL)
423                 {
424                         o<<i<<": ";
425                         item->serialize(o);
426                         o<<"\n";
427                 }
428         }
429 }
430
431 /*
432         Inventory
433 */
434
435 Inventory::~Inventory()
436 {
437         clear();
438 }
439
440 void Inventory::clear()
441 {
442         for(u32 i=0; i<m_lists.size(); i++)
443         {
444                 delete m_lists[i];
445         }
446         m_lists.clear();
447 }
448
449 Inventory::Inventory()
450 {
451 }
452
453 Inventory::Inventory(const Inventory &other)
454 {
455         *this = other;
456 }
457
458 Inventory & Inventory::operator = (const Inventory &other)
459 {
460         clear();
461         for(u32 i=0; i<other.m_lists.size(); i++)
462         {
463                 m_lists.push_back(new InventoryList(*other.m_lists[i]));
464         }
465         return *this;
466 }
467
468 void Inventory::serialize(std::ostream &os)
469 {
470         for(u32 i=0; i<m_lists.size(); i++)
471         {
472                 InventoryList *list = m_lists[i];
473                 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
474                 list->serialize(os);
475         }
476
477         os<<"end\n";
478 }
479
480 void Inventory::deSerialize(std::istream &is)
481 {
482         clear();
483
484         for(;;)
485         {
486                 std::string line;
487                 std::getline(is, line, '\n');
488
489                 std::istringstream iss(line);
490
491                 std::string name;
492                 std::getline(iss, name, ' ');
493
494                 if(name == "end")
495                 {
496                         break;
497                 }
498                 else if(name == "List")
499                 {
500                         std::string listname;
501                         u32 listsize;
502
503                         std::getline(iss, listname, ' ');
504                         iss>>listsize;
505
506                         InventoryList *list = new InventoryList(listname, listsize);
507                         list->deSerialize(is);
508
509                         m_lists.push_back(list);
510                 }
511                 else
512                 {
513                         throw SerializationError("Unknown inventory identifier");
514                 }
515         }
516 }
517
518 InventoryList * Inventory::addList(const std::string &name, u32 size)
519 {
520         s32 i = getListIndex(name);
521         if(i != -1)
522         {
523                 if(m_lists[i]->getSize() != size)
524                 {
525                         delete m_lists[i];
526                         m_lists[i] = new InventoryList(name, size);
527                 }
528                 return m_lists[i];
529         }
530         else
531         {
532                 m_lists.push_back(new InventoryList(name, size));
533                 return m_lists.getLast();
534         }
535 }
536
537 InventoryList * Inventory::getList(const std::string &name)
538 {
539         s32 i = getListIndex(name);
540         if(i == -1)
541                 return NULL;
542         return m_lists[i];
543 }
544
545 s32 Inventory::getListIndex(const std::string &name)
546 {
547         for(u32 i=0; i<m_lists.size(); i++)
548         {
549                 if(m_lists[i]->getName() == name)
550                         return i;
551         }
552         return -1;
553 }
554
555 /*
556         InventoryAction
557 */
558
559 InventoryAction * InventoryAction::deSerialize(std::istream &is)
560 {
561         std::string type;
562         std::getline(is, type, ' ');
563
564         InventoryAction *a = NULL;
565
566         if(type == "Move")
567         {
568                 a = new IMoveAction(is);
569         }
570
571         return a;
572 }
573
574 void IMoveAction::apply(Inventory *inventory)
575 {
576         /*dstream<<"from_name="<<from_name<<" to_name="<<to_name<<std::endl;
577         dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
578         InventoryList *list_from = inventory->getList(from_name);
579         InventoryList *list_to = inventory->getList(to_name);
580         /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
581                         <<std::endl;*/
582         /*if(list_from)
583                 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
584                                 <<std::endl;
585         if(list_to)
586                 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
587                                 <<std::endl;*/
588         
589         if(!list_from || !list_to || list_from->getItem(from_i) == NULL
590                         || (list_from == list_to && from_i == to_i))
591         {
592                 dstream<<__FUNCTION_NAME<<": Operation not allowed"<<std::endl;
593                 return;
594         }
595         
596         // Take item from source list
597         InventoryItem *item1 = list_from->changeItem(from_i, NULL);
598         // Try to add the item to destination list
599         if(list_to->addItem(to_i, item1))
600         {
601                 // Done.
602                 return;
603         }
604         // Adding was not possible, switch it.
605         // Switch it to the destination list
606         InventoryItem *item2 = list_to->changeItem(to_i, item1);
607         // Put item from destination list to the source list
608         list_from->changeItem(from_i, item2);
609 }
610         
611 //END