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

« back to all changes in this revision

Viewing changes to src/armybase.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) 2000, 2001, 2003 Michael Bartl
 
2
// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
 
3
// Copyright (C) 2004, 2005 Andrea Paternesi
 
4
// Copyright (C) 2007, 2008 Ben Asselstine
 
5
// Copyright (C) 2007, 2008 Ole Laursen
 
6
//
 
7
//  This program is free software; you can redistribute it and/or modify
 
8
//  it under the terms of the GNU General Public License as published by
 
9
//  the Free Software Foundation; either version 2 of the License, or
 
10
//  (at your option) any later version.
 
11
//
 
12
//  This program is distributed in the hope that it will be useful,
 
13
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
//  GNU Library General Public License for more details.
 
16
//
 
17
//  You should have received a copy of the GNU General Public License
 
18
//  along with this program; if not, write to the Free Software
 
19
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 
20
//  02110-1301, USA.
 
21
 
 
22
#include <iostream>
 
23
#include <sstream>
 
24
#include <algorithm>
 
25
#include "ucompose.hpp"
 
26
#include "armybase.h"
 
27
#include "xmlhelper.h"
 
28
#include "Tile.h"
 
29
#include "defs.h"
 
30
 
 
31
//#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
 
32
#define debug(x)
 
33
 
 
34
ArmyBase::ArmyBase(const ArmyBase& a)
 
35
: d_upkeep(a.d_upkeep), d_strength(a.d_strength), d_max_moves(a.d_max_moves), 
 
36
    d_sight(a.d_sight), d_move_bonus(a.d_move_bonus),
 
37
    d_army_bonus(a.d_army_bonus), d_xp_value(a.d_xp_value)
 
38
{
 
39
}
 
40
 
 
41
ArmyBase::ArmyBase()
 
42
  : d_upkeep(0), 
 
43
    d_strength(0), d_max_moves(0), d_sight(0), 
 
44
    d_move_bonus(0), d_army_bonus(0), d_xp_value(0.0)
 
45
{
 
46
}
 
47
 
 
48
ArmyBase::ArmyBase(XML_Helper* helper)
 
49
{
 
50
  helper->getData(d_upkeep, "upkeep");
 
51
  std::string move_bonus_str;
 
52
  helper->getData(move_bonus_str, "move_bonus");
 
53
  d_move_bonus = moveFlagsFromString(move_bonus_str);
 
54
  std::string army_bonus_str;
 
55
  helper->getData(army_bonus_str, "army_bonus");
 
56
  d_army_bonus = bonusFlagsFromString(army_bonus_str);
 
57
 
 
58
  helper->getData(d_max_moves, "max_moves");
 
59
  helper->getData(d_strength, "strength");
 
60
  helper->getData(d_sight, "sight");
 
61
  helper->getData(d_xp_value, "expvalue");
 
62
}
 
63
 
 
64
ArmyBase::~ArmyBase()
 
65
{
 
66
}
 
67
 
 
68
bool ArmyBase::saveData(XML_Helper* helper) const
 
69
{
 
70
  bool retval = true;
 
71
  retval &= helper->saveData("upkeep", d_upkeep);
 
72
  std::string move_bonus_str = moveFlagsToString(d_move_bonus);
 
73
  retval &= helper->saveData("move_bonus", move_bonus_str);
 
74
  std::string army_bonus_str = bonusFlagsToString(d_army_bonus);
 
75
  retval &= helper->saveData("army_bonus", army_bonus_str);
 
76
  retval &= helper->saveData("max_moves", d_max_moves);
 
77
  retval &= helper->saveData("strength", d_strength);
 
78
  retval &= helper->saveData("sight", d_sight);
 
79
  retval &= helper->saveData("expvalue", d_xp_value);
 
80
  return retval;
 
81
}
 
82
 
 
83
std::string ArmyBase::getArmyBonusDescription() const
 
84
{
 
85
  Uint32 bonus = d_army_bonus;
 
86
  Glib::ustring s = "";
 
87
  if (bonus & ArmyBase::ADD1STRINOPEN)
 
88
    s += String::ucompose(_("%1%2"), s == "" ? " " : "& ", 
 
89
                          _("+1 str in open"));
 
90
  if (bonus & ArmyBase::ADD2STRINOPEN)
 
91
    s += String::ucompose(_("%1%2"), s == "" ? " " : "& ", 
 
92
                          _("+2 str in open"));
 
93
  if (bonus & ArmyBase::ADD1STRINFOREST)
 
94
    s += String::ucompose(_("%1%2"), s == "" ? " " : "& ", 
 
95
                          _("+1 str in woods"));
 
96
  if (bonus & ArmyBase::ADD1STRINHILLS)
 
97
    s += String::ucompose(_("%1%2"), s == "" ? " " : " & ", 
 
98
                          _("+1 str in hills"));
 
99
  if (bonus & ArmyBase::ADD1STRINCITY)
 
100
    s += String::ucompose(_("%1%2"), s == "" ? " " : " & ", 
 
101
                          _("+1 str in city"));
 
102
  if (bonus & ArmyBase::ADD2STRINCITY)
 
103
    s += String::ucompose(_("%1%2"), s == "" ? " " : " & ", 
 
104
                          _("+2 str in city"));
 
105
  if (bonus & ArmyBase::ADD1STACKINHILLS)
 
106
    s += String::ucompose(_("%1%2"), s == "" ? " " : " & ", 
 
107
                          _("+1 stack in hills"));
 
108
  if (bonus & ArmyBase::SUBALLCITYBONUS)
 
109
    s += String::ucompose(_("%1%2"), s == "" ? " " : " & ", 
 
110
                          _("Cancel city bonus"));
 
111
  if (bonus & ArmyBase::SUB1ENEMYSTACK)
 
112
    s += String::ucompose(_("%1%2"), s == "" ? " " : " & ", 
 
113
                          _("-1 enemy stack"));
 
114
  if (bonus & ArmyBase::ADD1STACK)
 
115
    s += String::ucompose(_("%1%2"), s == "" ? " " : " & ", _("+1 stack"));
 
116
  if (bonus & ArmyBase::ADD2STACK)
 
117
    s += String::ucompose(_("%1%2"), s == "" ? " " : " & ", _("+2 stack"));
 
118
  if (bonus & ArmyBase::SUBALLNONHEROBONUS)
 
119
    s += String::ucompose(_("%1%2"), s == "" ? " " : " & ", 
 
120
                          _("cancel non-hero"));
 
121
  if (bonus & ArmyBase::SUBALLHEROBONUS)
 
122
    s += String::ucompose(_("%1%2"), s == "" ? " " : " & ", 
 
123
                          _("cancel hero"));
 
124
  return s;
 
125
}
 
126
 
 
127
std::string ArmyBase::moveFlagsToString(const Uint32 bonus)
 
128
{
 
129
  std::string move_bonuses;
 
130
  //we don't add grass, because it's always implied.
 
131
  if (bonus & Tile::WATER)
 
132
    move_bonuses += " " + Tile::tileTypeToString(Tile::WATER);
 
133
  if (bonus & Tile::FOREST)
 
134
    move_bonuses += " " + Tile::tileTypeToString(Tile::FOREST);
 
135
  if (bonus & Tile::HILLS)
 
136
    move_bonuses += " " + Tile::tileTypeToString(Tile::HILLS);
 
137
  if (bonus & Tile::MOUNTAIN)
 
138
    move_bonuses += " " + Tile::tileTypeToString(Tile::MOUNTAIN);
 
139
  if (bonus & Tile::SWAMP)
 
140
    move_bonuses += " " + Tile::tileTypeToString(Tile::SWAMP);
 
141
  if (bonus & Tile::VOID)
 
142
    move_bonuses += " " + Tile::tileTypeToString(Tile::VOID);
 
143
  return move_bonuses;
 
144
}
 
145
 
 
146
Uint32 ArmyBase::moveFlagsFromString(const std::string str)
 
147
{
 
148
  Uint32 total = 0;
 
149
  std::stringstream bonuses;
 
150
  bonuses.str(str);
 
151
 
 
152
  while (bonuses.eof() == false)
 
153
    {
 
154
      std::string bonus;
 
155
      bonuses >> bonus;
 
156
      if (bonus.size() == 0)
 
157
        break;
 
158
      total += Tile::tileTypeFromString(bonus);
 
159
    }
 
160
  return total;
 
161
}
 
162
 
 
163
std::string ArmyBase::bonusFlagToString(const ArmyBase::Bonus bonus)
 
164
{
 
165
  switch (bonus)
 
166
    {
 
167
    case ArmyBase::ADD1STRINOPEN:
 
168
      return "ArmyBase::ADD1STRINOPEN";
 
169
    case ArmyBase::ADD2STRINOPEN:
 
170
      return "ArmyBase::ADD2STRINOPEN";
 
171
    case ArmyBase::ADD1STRINFOREST:
 
172
      return "ArmyBase::ADD1STRINFOREST";
 
173
    case ArmyBase::ADD1STRINHILLS:
 
174
      return "ArmyBase::ADD1STRINHILLS";
 
175
    case ArmyBase::ADD1STRINCITY:
 
176
      return "ArmyBase::ADD1STRINCITY";
 
177
    case ArmyBase::ADD2STRINCITY:
 
178
      return "ArmyBase::ADD2STRINCITY";
 
179
    case ArmyBase::ADD1STACKINHILLS:
 
180
      return "ArmyBase::ADD1STACKINHILLS";
 
181
    case ArmyBase::SUBALLCITYBONUS:
 
182
      return "ArmyBase::SUBALLCITYBONUS";
 
183
    case ArmyBase::SUB1ENEMYSTACK:
 
184
      return "ArmyBase::SUB1ENEMYSTACK";
 
185
    case ArmyBase::ADD1STACK:
 
186
      return "ArmyBase::ADD1STACK";
 
187
    case ArmyBase::ADD2STACK:
 
188
      return "ArmyBase::ADD2STACK";
 
189
    case ArmyBase::SUBALLNONHEROBONUS:
 
190
      return "ArmyBase::SUBALLNONHEROBONUS";
 
191
    case ArmyBase::SUBALLHEROBONUS:
 
192
      return "ArmyBase::SUBALLHEROBONUS";
 
193
    case ArmyBase::FORTIFY:
 
194
      return "ArmyBase::FORTIFY";
 
195
    }
 
196
  return "";
 
197
}
 
198
 
 
199
std::string ArmyBase::bonusFlagsToString(const Uint32 bonus)
 
200
{
 
201
  std::string bonuses;
 
202
  if (bonus & ArmyBase::ADD1STRINOPEN)
 
203
    bonuses += " " + bonusFlagToString(ArmyBase::ADD1STRINOPEN);
 
204
  if (bonus & ArmyBase::ADD2STRINOPEN)
 
205
    bonuses += " " + bonusFlagToString(ArmyBase::ADD2STRINOPEN);
 
206
  if (bonus & ArmyBase::ADD1STRINFOREST)
 
207
    bonuses += " " + bonusFlagToString(ArmyBase::ADD1STRINFOREST);
 
208
  if (bonus & ArmyBase::ADD1STRINHILLS)
 
209
    bonuses += " " + bonusFlagToString(ArmyBase::ADD1STRINHILLS);
 
210
  if (bonus & ArmyBase::ADD1STRINCITY)
 
211
    bonuses += " " + bonusFlagToString(ArmyBase::ADD1STRINCITY);
 
212
  if (bonus & ArmyBase::ADD2STRINCITY)
 
213
    bonuses += " " + bonusFlagToString(ArmyBase::ADD2STRINCITY);
 
214
  if (bonus & ArmyBase::ADD1STACKINHILLS)
 
215
    bonuses += " " + bonusFlagToString(ArmyBase::ADD1STACKINHILLS);
 
216
  if (bonus & ArmyBase::SUBALLCITYBONUS)
 
217
    bonuses += " " + bonusFlagToString(ArmyBase::SUBALLCITYBONUS);
 
218
  if (bonus & ArmyBase::SUB1ENEMYSTACK)
 
219
    bonuses += " " + bonusFlagToString(ArmyBase::SUB1ENEMYSTACK);
 
220
  if (bonus & ArmyBase::ADD1STACK)
 
221
    bonuses += " " + bonusFlagToString(ArmyBase::ADD1STACK);
 
222
  if (bonus & ArmyBase::ADD2STACK)
 
223
    bonuses += " " + bonusFlagToString(ArmyBase::ADD2STACK);
 
224
  if (bonus & ArmyBase::SUBALLNONHEROBONUS)
 
225
    bonuses += " " + bonusFlagToString(ArmyBase::SUBALLNONHEROBONUS);
 
226
  if (bonus & ArmyBase::SUBALLHEROBONUS)
 
227
    bonuses += " " + bonusFlagToString(ArmyBase::SUBALLHEROBONUS);
 
228
  if (bonus & ArmyBase::FORTIFY)
 
229
    bonuses += " " + bonusFlagToString(ArmyBase::FORTIFY);
 
230
  return bonuses;
 
231
}
 
232
 
 
233
Uint32 ArmyBase::bonusFlagsFromString(const std::string str)
 
234
{
 
235
  Uint32 total = 0;
 
236
  std::stringstream bonuses;
 
237
  bonuses.str(str);
 
238
 
 
239
  while (bonuses.eof() == false)
 
240
    {
 
241
      std::string bonus;
 
242
      bonuses >> bonus;
 
243
      if (bonus.size() == 0)
 
244
        break;
 
245
      total += bonusFlagFromString(bonus);
 
246
    }
 
247
  return total;
 
248
}
 
249
 
 
250
ArmyBase::Bonus ArmyBase::bonusFlagFromString(const std::string str)
 
251
{
 
252
  if (str.size() > 0 && isdigit(str.c_str()[0]))
 
253
    return ArmyBase::Bonus(atoi(str.c_str()));
 
254
  if (str == "ArmyBase::ADD1STRINOPEN")
 
255
    return ArmyBase::ADD1STRINOPEN;
 
256
  else if (str == "ArmyBase::ADD2STRINOPEN")
 
257
    return ArmyBase::ADD2STRINOPEN;
 
258
  else if (str == "ArmyBase::ADD1STRINFOREST")
 
259
    return ArmyBase::ADD1STRINFOREST;
 
260
  else if (str == "ArmyBase::ADD1STRINHILLS")
 
261
    return ArmyBase::ADD1STRINHILLS;
 
262
  else if (str == "ArmyBase::ADD1STRINCITY")
 
263
    return ArmyBase::ADD1STRINCITY;
 
264
  else if (str == "ArmyBase::ADD2STRINCITY")
 
265
    return ArmyBase::ADD2STRINCITY;
 
266
  else if (str == "ArmyBase::ADD1STACKINHILLS")
 
267
    return ArmyBase::ADD1STACKINHILLS;
 
268
  else if (str == "ArmyBase::SUBALLCITYBONUS")
 
269
    return ArmyBase::SUBALLCITYBONUS;
 
270
  else if (str == "ArmyBase::ADD2GOLDPERCITY")
 
271
    return ArmyBase::SUB1ENEMYSTACK;
 
272
  else if (str == "ArmyBase::SUB1ENEMYSTACK")
 
273
    return ArmyBase::ADD1STACK;
 
274
  else if (str == "ArmyBase::ADD1STACK")
 
275
    return ArmyBase::ADD2STACK;
 
276
  else if (str == "ArmyBase::ADD2STACK")
 
277
    return ArmyBase::ADD2STACK;
 
278
  else if (str == "ArmyBase::SUBALLNONHEROBONUS")
 
279
    return ArmyBase::SUBALLNONHEROBONUS;
 
280
  else if (str == "ArmyBase::SUBALLHEROBONUS")
 
281
    return ArmyBase::SUBALLHEROBONUS;
 
282
  return ArmyBase::ADD1STRINOPEN;
 
283
}