Debian Codegraph @

src/estate.cpp

raw
1 // Copyright © 2001-2003 Rob Kaper <[email protected]>,
2 // 2001 Erik Bourget <[email protected]>
3 // 2010-2015 Sylvain Rochet <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // version 2 as published by the Free Software Foundation.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING. If not, see
16 // <http://www.gnu.org/licenses/>.
17
18 #include "config.h"
19
20 #include <stdlib.h>
21
22 #include "estate.h"
23 #include "estategroup.h"
24
25 Estate::Estate(int id, std::vector<Estate *> *ev) : GameObject(id, GameObject::GEstate)
26 {
27 m_estates = ev;
28 m_group = 0;
29 m_payTarget = 0;
30 m_owner = 0;
31 m_takeCardGroup = 0;
32 m_rent[0] = m_rent[1] = m_rent[2] = m_rent[3] = m_rent[4] = m_rent[5] = 0;
33 }
34
35 int Estate::rent(Player *pLand, const std::string &rentMath)
36 {
37 if (getBoolProperty("mortgaged"))
38 return 0;
39
40 // Don't use m_housePrice, housePrice() also checks within estate group
41 if (housePrice() > 0)
42 {
43 // Rent for estates that can have houses
44 int houses = getIntProperty("houses");
45 if (!houses && estateGroupIsMonopoly())
46 return 2 * m_rent[houses];
47 else
48 return m_rent[houses];
49 }
50
51 // No custom rent rules, check group rules
52 if (m_group)
53 return m_group->rent(pLand, rentMath);
54 return 0;
55 }
56
57 int Estate::groupSize(Player *owner)
58 {
59 (void)owner;
60 int grouped = 0;
61
62 Estate *eTmp = 0;
63 for(std::vector<Estate *>::iterator it = m_estates->begin(); it != m_estates->end() && (eTmp = *it) ; ++it)
64 if (m_group == eTmp->group() && m_owner == eTmp->owner())
65 grouped++;
66
67 return grouped;
68 }
69
70 int Estate::minHouses()
71 {
72 int m_minHouses = 0;
73
74 Estate *eTmp = 0;
75 for(std::vector<Estate *>::iterator it = m_estates->begin(); it != m_estates->end() && (eTmp = *it) ; ++it)
76 {
77 if ((eTmp->group() == m_group) && (eTmp->getIntProperty("houses") > m_minHouses))
78 m_minHouses = eTmp->getIntProperty("houses") - 1;
79 }
80 return m_minHouses;
81 }
82
83 int Estate::maxHouses()
84 {
85 int m_maxHouses = 5;
86
87 Estate *eTmp = 0;
88 for(std::vector<Estate *>::iterator it = m_estates->begin(); it != m_estates->end() && (eTmp = *it) ; ++it)
89 {
90 if ((eTmp->group() == m_group) && (eTmp->getIntProperty("houses") < m_maxHouses))
91 m_maxHouses = eTmp->getIntProperty("houses") + 1;
92 }
93 return m_maxHouses;
94 }
95
96 bool Estate::canBuyHouses(Player *p)
97 {
98 // Don't use m_housePrice, housePrice() also checks within estate group
99 if ( (p == m_owner) && housePrice() && estateGroupIsMonopoly() && !groupHasMortgages() && (getIntProperty("houses") < maxHouses()) )
100 return true;
101 else
102 return false;
103 }
104
105 bool Estate::canSellHouses(Player *p)
106 {
107 if ( (p == m_owner) && estateGroupIsMonopoly() && (getIntProperty("houses") > minHouses()) )
108 return true;
109 else
110 return false;
111 }
112
113 int Estate::price()
114 {
115 int price = getIntProperty("price");
116 return (!price && m_group) ? m_group->price() : price;
117 }
118
119 int Estate::housePrice()
120 {
121 int housePrice = getIntProperty("houseprice");
122 return (!housePrice && m_group) ? m_group->housePrice() : housePrice;
123 }
124
125 bool Estate::canBeOwned()
126 {
127 return price();
128 }
129
130 bool Estate::canToggleMortgage(Player *p)
131 {
132 return (p == m_owner && !groupHasBuildings()) ? true : false;
133 }
134
135 bool Estate::groupHasBuildings()
136 {
137 Estate *eTmp = 0;
138 for(std::vector<Estate *>::iterator it = m_estates->begin(); it != m_estates->end() && (eTmp = *it) ; ++it) {
139 if((eTmp->group() == m_group) && eTmp->getIntProperty("houses"))
140 return true;
141 }
142 return false;
143 }
144
145 bool Estate::groupHasMortgages()
146 {
147 Estate *eTmp = 0;
148 for(std::vector<Estate *>::iterator it = m_estates->begin(); it != m_estates->end() && (eTmp = *it) ; ++it) {
149 if((eTmp->group() == m_group) && eTmp->getBoolProperty("mortgaged"))
150 return true;
151 }
152 return false;
153 }
154
155 bool Estate::estateGroupIsMonopoly()
156 {
157 unsigned int size = 0, sameOwner = 0;
158
159 Estate *eTmp = 0;
160 for(std::vector<Estate *>::iterator it = m_estates->begin(); it != m_estates->end() && (eTmp = *it) ; ++it)
161 if (eTmp->group() == m_group)
162 {
163 size++;
164 if (eTmp->owner() == m_owner)
165 sameOwner++;
166 }
167
168 return (sameOwner == size);
169 }
170
171 void Estate::addMoney(int money)
172 {
173 setProperty("money", getIntProperty("money") + money);
174 }
175
176 void Estate::setTakeCardGroup(CardGroup *takeCardGroup)
177 {
178 m_takeCardGroup = takeCardGroup;
179 }
180
181 CardGroup *Estate::takeCardGroup()
182 {
183 return m_takeCardGroup;
184 }