~ubuntu-branches/ubuntu/saucy/lordsawar/saucy

« back to all changes in this revision

Viewing changes to src/Backpack.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese
  • Date: 2008-12-20 13:52:12 UTC
  • mfrom: (1.1.6 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20081220135212-noeb2w3y98ebo7o9
Tags: 0.1.4-1
[ Barry deFreese ]
* New upstream release.
* Move 0.0.8-2.1 changelog entry to correct point in changelog.
* Make lordsawar-data suggest lordsawar.
* Update my e-mail address.
* Add build-depends on intltool, uuid-dev, and libboost-dev.
* Don't install locales since there are no translations currently.
* Add simple man page for new lordsawar-pbm binary.
* Drop gcc4.3 patches as they have been fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  Copyright (C) 2008 Ben Asselstine
 
2
//
 
3
//  This program is free software; you can redistribute it and/or modify
 
4
//  it under the terms of the GNU General Public License as published by
 
5
//  the Free Software Foundation; either version 2 of the License, or
 
6
//  (at your option) any later version.
 
7
//
 
8
//  This program is distributed in the hope that it will be useful,
 
9
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
//  GNU Library General Public License for more details.
 
12
//
 
13
//  You should have received a copy of the GNU General Public License
 
14
//  along with this program; if not, write to the Free Software
 
15
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 
16
//  02110-1301, USA.
 
17
 
 
18
#include <iostream>
 
19
#include <sstream>
 
20
#include <sigc++/functors/mem_fun.h>
 
21
 
 
22
#include "Backpack.h"
 
23
 
 
24
#include "xmlhelper.h"
 
25
 
 
26
#include "Item.h"
 
27
 
 
28
std::string Backpack::d_tag = "backpack";
 
29
 
 
30
using namespace std;
 
31
 
 
32
//#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<flush;}
 
33
#define debug(x)
 
34
 
 
35
 
 
36
Backpack::Backpack()
 
37
{
 
38
    debug("Backpack()");
 
39
}
 
40
 
 
41
Backpack::Backpack(XML_Helper* helper)
 
42
{
 
43
  helper->registerTag(Item::d_tag, sigc::mem_fun(this, &Backpack::loadItem));
 
44
}
 
45
 
 
46
Backpack::Backpack(const Backpack& backpack)
 
47
{
 
48
  for (const_iterator it = backpack.begin(); it != backpack.end(); it++)
 
49
    push_back(new Item(**it));
 
50
}
 
51
 
 
52
Backpack::~Backpack()
 
53
{
 
54
  for (iterator it = begin(); it != end(); it++)
 
55
    delete (*it);
 
56
}
 
57
 
 
58
bool Backpack::saveData(XML_Helper* helper) const
 
59
{
 
60
  bool retval = true;
 
61
  for (const_iterator it = begin(); it != end(); it++)
 
62
    retval &= (*it)->save(helper);
 
63
  return true;
 
64
}
 
65
 
 
66
bool Backpack::save(XML_Helper* helper) const
 
67
{
 
68
    bool retval = true;
 
69
 
 
70
    retval &= helper->openTag(Backpack::d_tag);
 
71
    retval &= saveData(helper);
 
72
    retval &= helper->closeTag();
 
73
 
 
74
    return retval;
 
75
}
 
76
 
 
77
bool Backpack::loadItem(std::string tag, XML_Helper* helper)
 
78
{
 
79
  if (tag == Backpack::d_tag)
 
80
    return true;
 
81
 
 
82
  if (tag == Item::d_tag)
 
83
    {
 
84
      Item* item = new Item(helper);
 
85
      push_back(item);
 
86
      return true;
 
87
    }
 
88
 
 
89
  return false;
 
90
}
 
91
 
 
92
Uint32 Backpack::countStrengthBonuses()
 
93
{
 
94
  Uint32 bonus = 0;
 
95
  for (iterator it = begin(); it != end(); it++)
 
96
    {
 
97
      if ((*it)->getBonus(Item::ADD1STR))
 
98
        bonus += 1;
 
99
      if ((*it)->getBonus(Item::ADD2STR))
 
100
        bonus += 2;
 
101
      if ((*it)->getBonus(Item::ADD3STR))
 
102
        bonus += 3;
 
103
    }
 
104
  return bonus;
 
105
}
 
106
 
 
107
Uint32 Backpack::countStackStrengthBonuses()
 
108
{
 
109
  Uint32 bonus = 0;
 
110
  for (iterator it = begin(); it != end(); it++)
 
111
    {
 
112
      if ((*it)->getBonus(Item::ADD1STACK))
 
113
        bonus += 1;
 
114
      if ((*it)->getBonus(Item::ADD2STACK))
 
115
        bonus += 2;
 
116
      if ((*it)->getBonus(Item::ADD3STACK))
 
117
        bonus += 3;
 
118
    }
 
119
  return bonus;
 
120
}
 
121
 
 
122
 
 
123
Uint32 Backpack::countGoldBonuses()
 
124
{
 
125
  Uint32 bonus = 0;
 
126
  for (iterator it = begin(); it != end(); it++)
 
127
    {
 
128
      if ((*it)->getBonus(Item::ADD2GOLDPERCITY))
 
129
        bonus += 2;
 
130
      if ((*it)->getBonus(Item::ADD3GOLDPERCITY))
 
131
        bonus += 3;
 
132
      if ((*it)->getBonus(Item::ADD4GOLDPERCITY))
 
133
        bonus += 4;
 
134
      if ((*it)->getBonus(Item::ADD5GOLDPERCITY))
 
135
        bonus += 5;
 
136
    }
 
137
  return bonus;
 
138
}
 
139
 
 
140
Uint32 Backpack::countMovementDoublers()
 
141
{
 
142
  Uint32 bonus = 0;
 
143
  for (iterator it = begin(); it != end(); it++)
 
144
    if ((*it)->getBonus(Item::DOUBLEMOVESTACK))
 
145
      bonus++;
 
146
  return bonus;
 
147
}
 
148
 
 
149
Uint32 Backpack::countStackFlightGivers()
 
150
{
 
151
  Uint32 bonus = 0;
 
152
  for (iterator it = begin(); it != end(); it++)
 
153
    if ((*it)->getBonus(Item::FLYSTACK))
 
154
      bonus++;
 
155
  return bonus;
 
156
}
 
157
 
 
158
Uint32 Backpack::countPlantableItems()
 
159
{
 
160
  Uint32 count = 0;
 
161
  for (iterator it = begin(); it != end(); it++)
 
162
    if ((*it)->isPlantable())
 
163
      count++;
 
164
  return count;
 
165
}
 
166
 
 
167
Item *Backpack::getPlantableItem(Player *player)
 
168
{
 
169
  for (iterator it = begin(); it != end(); it++)
 
170
    if ((*it)->isPlantable() && (*it)->getPlantableOwner() == player)
 
171
      return *it;
 
172
  return NULL;
 
173
}
 
174
        
 
175
Item *Backpack::getItemById(Uint32 id)
 
176
{
 
177
  for (iterator it = begin(); it != end(); it++)
 
178
    if ((*it)->getId() == id)
 
179
      return *it;
 
180
  return NULL;
 
181
}
 
182
 
 
183
bool Backpack::addToBackpack(Item* item, int position)
 
184
{
 
185
  iterator it = begin();
 
186
  for (; position > 0; position--, it++);
 
187
  insert(it, item);
 
188
  return true;
 
189
}
 
190
 
 
191
bool Backpack::addToBackpack(Item* item)
 
192
{
 
193
  iterator it = end();
 
194
  insert(it, item);
 
195
  return true;
 
196
}
 
197
 
 
198
bool Backpack::removeFromBackpack(Item* item)
 
199
{
 
200
  for (iterator it = begin(); it != end(); it++)
 
201
    if ((*it) == item)
 
202
      {
 
203
        //FIXME: delete the item?
 
204
        erase(it);
 
205
        return true;
 
206
      }
 
207
 
 
208
  return false;
 
209
}
 
210
// End of file