initial commit, lordsawar source, slightly modified
[lordsawar] / src / Item.cpp
1 // Copyright (C) 2004, 2005, 2006 Ulf Lorenz
2 // Copyright (C) 2004 Andrea Paternesi
3 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
4 // Copyright (C) 2008 Ole Laursen
5 //
6 //  This program is free software; you can redistribute it and/or modify
7 //  it under the terms of the GNU General Public License as published by
8 //  the Free Software Foundation; either version 3 of the License, or
9 //  (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU Library General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
19 //  02110-1301, USA.
20
21 #include <sstream>
22 #include <map>
23 #include "Item.h"
24 #include "ItemProto.h"
25 #include "File.h"
26 #include "playerlist.h"
27 #include "ucompose.hpp"
28
29 std::string Item::d_tag = "item";
30 using namespace std;
31
32 Item::Item(XML_Helper* helper)
33         : ItemProto(helper), UniquelyIdentified(helper)
34 {
35     
36     helper->getData(d_plantable, "plantable");
37     if (d_plantable)
38       {
39         helper->getData(d_plantable_owner_id, "plantable_owner");
40         helper->getData(d_planted, "planted");
41       }
42     else
43       {
44         d_plantable_owner_id = MAX_PLAYERS;
45         d_planted = false;
46       }
47
48     helper->getData(d_type, "type");
49
50 }
51
52 Item::Item(std::string name, bool plantable, Player *plantable_owner)
53         : ItemProto(name, 0), UniquelyIdentified()
54 {
55   d_type = 0;
56   d_bonus = 0;
57   d_plantable = plantable;
58   if (plantable_owner)
59     d_plantable_owner_id = plantable_owner->getId();
60   else
61     d_plantable_owner_id = MAX_PLAYERS;
62   d_planted = false;
63   //std::cerr << "item created with id " << d_id << std::endl;
64 }
65
66 Item::Item(std::string name, bool plantable, Player *plantable_owner, guint32 id)
67         : ItemProto(name, 0), UniquelyIdentified(id)
68 {
69   d_type = 0;
70   d_bonus = 0;
71   d_plantable = plantable;
72   if (plantable_owner)
73     d_plantable_owner_id = plantable_owner->getId();
74   else
75     d_plantable_owner_id = MAX_PLAYERS;
76   d_planted = false;
77   //std::cerr << "item created with id " << d_id << std::endl;
78 }
79
80 Item::Item(const Item& orig)
81 :ItemProto(orig), UniquelyIdentified(orig), 
82     d_plantable(orig.d_plantable), 
83     d_plantable_owner_id(orig.d_plantable_owner_id), d_planted(orig.d_planted),
84     d_type(orig.d_type)
85 {
86 }
87
88 Item::Item(const ItemProto &proto)
89 :ItemProto(proto), UniquelyIdentified()
90 {
91   d_type = proto.getTypeId();
92   d_plantable = false;
93   d_plantable_owner_id = MAX_PLAYERS;
94   d_planted = false;
95 }
96
97 Item::~Item()
98 {
99   if (d_unique)
100     sdying.emit(this);
101 }
102
103 bool Item::save(XML_Helper* helper) const
104 {
105   bool retval = true;
106
107   // A template is never saved, so we assume this class is a real-life item
108   retval &= helper->openTag(Item::d_tag);
109   retval &= helper->saveData("name", getName(false));
110   retval &= helper->saveData("plantable", d_plantable);
111   if (d_plantable)
112     {
113       retval &= helper->saveData("plantable_owner", d_plantable_owner_id);
114       retval &= helper->saveData("planted", d_planted);
115     }
116   retval &= helper->saveData("id", d_id);
117   retval &= helper->saveData("type", d_type);
118
119   std::string bonus_str = ItemProto::bonusFlagsToString(d_bonus);
120   retval &= helper->saveData("bonus", bonus_str);
121
122   retval &= helper->closeTag();
123
124   return retval;
125 }
126
127 Item* Item::createNonUniqueItem(std::string name, bool plantable, 
128                                 Player *plantable_owner)
129 {
130   return new Item(name, plantable, plantable_owner, 0);
131 }