Debian Codegraph @

src/estategroup.cpp

raw
1 // Copyright © 2002-2003 Rob Kaper <[email protected]>
2 // 2010-2015 Sylvain Rochet <[email protected]>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // version 2 as published by the Free Software Foundation.
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 GNU
11 // 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; see the file COPYING. If not, see
15 // <http://www.gnu.org/licenses/>.
16
17 #include "config.h"
18
19 #include <stdlib.h>
20 #include <iostream>
21
22 #include <muParser.h>
23
24 #include "estate.h"
25 #include "estategroup.h"
26 #include "game.h"
27 #include "io.h"
28 #include "player.h"
29
30 EstateGroup::EstateGroup(int id, Game *game, std::string name) : GameObject(id, GameObject::Unknown, game)
31 {
32 setProperty("name", name, game);
33 m_rentMath = m_rentVar = "";
34 m_price = m_housePrice = 0;
35 }
36
37 void EstateGroup::setPrice(unsigned int price)
38 {
39 if (m_price != price)
40 m_price = price;
41 }
42
43 unsigned int EstateGroup::price()
44 {
45 return m_price;
46 }
47
48 void EstateGroup::setHousePrice(unsigned int housePrice)
49 {
50 if (m_housePrice != housePrice)
51 m_housePrice = housePrice;
52 }
53
54 unsigned int EstateGroup::housePrice()
55 {
56 return m_housePrice;
57 }
58
59 void EstateGroup::setRentMath(const std::string rentMath)
60 {
61 if (m_rentMath != rentMath)
62 m_rentMath = rentMath;
63 }
64
65 void EstateGroup::setRentVar(const std::string rentVar)
66 {
67 if (m_rentVar != rentVar)
68 m_rentVar = rentVar;
69 }
70
71 int EstateGroup::rent(Player *pLand, const std::string &rentMath)
72 {
73 std::string mathStr = rentMath.size() ? rentMath : m_rentMath;
74
75 if (!mathStr.size()) {
76 return 0;
77 }
78
79 Estate *estate = pLand->estate();
80 stringReplace(mathStr, "${DICE}", std::string(itoa((m_game->dice[0] + m_game->dice[1]))));
81 stringReplace(mathStr, "${GROUPOWNED}", std::string(itoa(estate->groupSize(estate->owner()))));
82 stringReplace(mathStr, "${HOUSES}", std::string(itoa(estate->getIntProperty("houses"))));
83
84 try {
85 mu::Parser p;
86 p.SetExpr(mathStr);
87 return (int)round(p.Eval());
88 } catch (mu::Parser::exception_type &e) {
89 return 0;
90 } catch (...) {
91 return 0;
92 }
93
94 return 0;
95 }