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