1 // Copyright (C) 2004, 2005, 2006 Ulf Lorenz
2 // Copyright (C) 2004 Andrea Paternesi
3 // Copyright (C) 2007, 2008, 2009, 2010, 2014 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 #include "maptile.h"
29 #include "xmlhelper.h"
30
31 Glib::ustring Item::d_tag = "item";
32
Item(XML_Helper * helper)33 Item::Item(XML_Helper* helper)
34 : ItemProto(helper), UniquelyIdentified(helper)
35 {
36
37 helper->getData(d_plantable, "plantable");
38 if (d_plantable)
39 {
40 helper->getData(d_plantable_owner_id, "plantable_owner");
41 helper->getData(d_planted, "planted");
42 }
43 else
44 {
45 d_plantable_owner_id = MAX_PLAYERS;
46 d_planted = false;
47 }
48
49 helper->getData(d_type, "type");
50
51 }
52
Item(Glib::ustring name,bool plantable,Player * plantable_owner)53 Item::Item(Glib::ustring name, bool plantable, Player *plantable_owner)
54 : ItemProto(name), UniquelyIdentified()
55 {
56 d_type = 0;
57 d_bonus = 0;
58 d_plantable = plantable;
59 if (plantable_owner)
60 d_plantable_owner_id = plantable_owner->getId();
61 else
62 d_plantable_owner_id = MAX_PLAYERS;
63 d_planted = false;
64 //std::cerr << "item created with id " << d_id << std::endl;
65 }
66
67 /*
68 Item::Item(Glib::ustring name, bool plantable, Player *plantable_owner, guint32 id)
69 : ItemProto(name), UniquelyIdentified(id)
70 {
71 d_type = 0;
72 d_bonus = 0;
73 d_plantable = plantable;
74 if (plantable_owner)
75 d_plantable_owner_id = plantable_owner->getId();
76 else
77 d_plantable_owner_id = MAX_PLAYERS;
78 d_planted = false;
79 //std::cerr << "item created with id " << d_id << std::endl;
80 }
81 */
82
Item(const Item & orig)83 Item::Item(const Item& orig)
84 :ItemProto(orig), UniquelyIdentified(orig),
85 d_plantable(orig.d_plantable),
86 d_plantable_owner_id(orig.d_plantable_owner_id), d_planted(orig.d_planted),
87 d_type(orig.d_type)
88 {
89 }
90
Item(const ItemProto & proto,guint32 type_id)91 Item::Item(const ItemProto &proto, guint32 type_id)
92 :ItemProto(proto), UniquelyIdentified()
93 {
94 d_type = type_id;
95 d_plantable = false;
96 d_plantable_owner_id = MAX_PLAYERS;
97 d_planted = false;
98 }
99
~Item()100 Item::~Item()
101 {
102 if (d_unique)
103 sdying.emit(this);
104 }
105
save(XML_Helper * helper) const106 bool Item::save(XML_Helper* helper) const
107 {
108 bool retval = true;
109
110 // A template is never saved, so we assume this class is a real-life item
111 retval &= helper->openTag(Item::d_tag);
112 retval &= saveContents(helper);
113 retval &= helper->saveData("plantable", d_plantable);
114 if (d_plantable)
115 {
116 retval &= helper->saveData("plantable_owner", d_plantable_owner_id);
117 retval &= helper->saveData("planted", d_planted);
118 }
119 retval &= helper->saveData("id", d_id);
120 retval &= helper->saveData("type", d_type);
121 retval &= helper->closeTag();
122
123 return retval;
124 }
125
use()126 bool Item::use()
127 {
128 if (d_uses_left)
129 {
130 d_uses_left--;
131 if (d_uses_left)
132 return false;
133 else
134 return true;
135 }
136 return true;
137 }
138
getPlantableOwner() const139 Player *Item::getPlantableOwner() const
140 {
141 return Playerlist::getInstance()->getPlayer(d_plantable_owner_id);
142 }
143