forked from gecube/opencaesar3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oc3_building.cpp
469 lines (393 loc) · 11.7 KB
/
oc3_building.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// This file is part of openCaesar3.
//
// openCaesar3 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// openCaesar3 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with openCaesar3. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2012-2013 Gregoire Athanase, gathanase@gmail.com
#include "oc3_building.hpp"
#include "oc3_tile.hpp"
#include "oc3_walker_service.hpp"
#include "oc3_exception.hpp"
#include "oc3_building_data.hpp"
#include "oc3_resourcegroup.hpp"
#include "oc3_variant.hpp"
#include "oc3_stringhelper.hpp"
#include "oc3_city.hpp"
#include "oc3_foreach.hpp"
#include "oc3_game_event_mgr.hpp"
#include "oc3_tilemap.hpp"
Construction::Construction( const BuildingType type, const Size& size)
: LandOverlay( type, size )
{
}
bool Construction::canBuild( CityPtr city, const TilePos& pos ) const
{
Tilemap& tilemap = city->getTilemap();
bool is_constructible = true;
//return area for available tiles
TilemapArea area = tilemap.getFilledRectangle( pos, getSize() );
//on over map size
if( (int)area.size() != getSize().getArea() )
return false;
foreach( Tile* tile, area )
{
is_constructible &= tile->getFlag( Tile::isConstructible );
}
return is_constructible;
}
void Construction::build( CityPtr city, const TilePos& pos )
{
LandOverlay::build( city, pos );
computeAccessRoads();
_updateDesirabilityInfluence( duPositive );
}
void Construction::_updateDesirabilityInfluence( const DsbrlUpdate type )
{
Tilemap& tilemap = _getCity()->getTilemap();
int dsrblRange = getDesirabilityRange();
int step = getDesirabilityStep();
int desInfluence = getDesirabilityInfluence();
int mul = ( type == duPositive ? 1 : -1);
//change desirability in selfarea
TilemapArea area = tilemap.getFilledRectangle( getTilePos(), getSize() );
foreach( Tile* tile, area )
{
tile->appendDesirability( mul * desInfluence );
}
//change deisirability around
for( int curRange=1; curRange <= dsrblRange; curRange++ )
{
TilemapArea perimetr = tilemap.getRectangle( getTilePos() - TilePos( curRange, curRange ),
getSize() + Size( 2 * curRange - 1 ) );
foreach( Tile* tile, perimetr )
{
tile->appendDesirability( mul * desInfluence );
}
desInfluence += step;
}
}
const TilemapTiles& Construction::getAccessRoads() const
{
return _accessRoads;
}
// here the problem lays: if we remove road, it is left in _accessRoads array
// also we need to recompute _accessRoads if we place new road tile
// on next to this road tile buildings
void Construction::computeAccessRoads()
{
_accessRoads.clear();
if( !_getMasterTile() )
return;
Tilemap& tilemap = _getCity()->getTilemap();
int maxDst2road = getMaxDistance2Road();
TilemapTiles rect = tilemap.getRectangle( _getMasterTile()->getIJ() + TilePos( -maxDst2road, -maxDst2road ),
getSize() + Size( 2 * maxDst2road ), !Tilemap::checkCorners );
foreach( Tile* tile, rect )
{
if( tile->getFlag( Tile::tlRoad ) )
{
_accessRoads.push_back( tile );
}
}
}
int Construction::getMaxDistance2Road() const
{
return 1;
// it is default value
// for houses - 2
}
void Construction::burn()
{
deleteLater();
GameEventMgr::append( DisasterEvent::create( getTile().getIJ(), DisasterEvent::fire ) );
}
void Construction::collapse()
{
deleteLater();
GameEventMgr::append( DisasterEvent::create( getTile().getIJ(), DisasterEvent::collapse ) );
}
char Construction::getDesirabilityInfluence() const
{
return BuildingDataHolder::instance().getData( getType() ).getDesirbilityInfluence();
}
unsigned char Construction::getDesirabilityRange() const
{
return BuildingDataHolder::instance().getData( getType() ).getDesirbilityRange();
}
void Construction::destroy()
{
LandOverlay::destroy();
_updateDesirabilityInfluence( duNegative );
}
bool Construction::isNeedRoadAccess() const
{
return true;
}
char Construction::getDesirabilityStep() const
{
return BuildingDataHolder::instance().getData( getType() ).getDesirabilityStep();
}
Building::Building(const BuildingType type, const Size& size )
: Construction( type, size )
{
_damageLevel = 0.0;
_fireLevel = 0.0;
_damageIncrement = 1;
_fireIncrement = 1;
}
void Building::initTerrain( Tile &tile )
{
// here goes the problem
// when we reset tile, we delete information
// about it's original information
// try to fix
bool saveMeadow = tile.getFlag( Tile::tlMeadow );
tile.setFlag( Tile::clearAll, true );
tile.setFlag( Tile::tlBuilding, true);
tile.setFlag( Tile::tlMeadow, saveMeadow);
}
void Building::timeStep(const unsigned long time)
{
Construction::timeStep(time);
if (time % 64 == 0)
{
_damageLevel += _damageIncrement;
_fireLevel += _fireIncrement;
if (_damageLevel >= 100)
{
StringHelper::debug( 0xff, "Building destroyed!" );
collapse();
}
if (_fireLevel >= 100)
{
StringHelper::debug( 0xff, "Building catch fire!" );
burn();
}
}
}
float Building::getDamageLevel()
{
return _damageLevel;
}
void Building::setDamageLevel(const float value)
{
_damageLevel = value;
}
float Building::getFireLevel()
{
return _fireLevel;
}
void Building::setFireLevel(const float value)
{
_fireLevel = value;
}
void Building::storeGoods(GoodStock &stock, const int amount)
{
_OC3_DEBUG_BREAK_IF("This building should not store any goods");
}
float Building::evaluateService(ServiceWalkerPtr walker)
{
float res = 0.0;
Service::Type service = walker->getService();
if (_reservedServices.count(service) == 1)
{
// service is already reserved
return 0.0;
}
switch(service)
{
case Service::engineer:
res = _damageLevel;
break;
case Service::S_PREFECT:
res = _fireLevel;
break;
default: break;
}
return res;
}
void Building::reserveService(const Service::Type service)
{
// std::cout << "reserved service" << std::endl;
_reservedServices.insert(service);
}
void Building::cancelService(const Service::Type service)
{
// std::cout << "cancel service" << std::endl;
_reservedServices.erase(service);
}
void Building::applyService( ServiceWalkerPtr walker)
{
// std::cout << "apply service" << std::endl;
// remove service reservation
Service::Type service = walker->getService();
_reservedServices.erase(service);
switch( service )
{
case Service::engineer:
{
_damageLevel = 0;
}
break;
case Service::S_PREFECT:
{
_fireLevel = 0;
}
break;
default: break;
}
}
float Building::evaluateTrainee(const WalkerType traineeType)
{
float res = 0.0;
if( _reservedTrainees.count(traineeType) == 1 )
{
// don't allow two reservations of the same type
return 0.0;
}
if( _traineeMap.count(traineeType) == 1 )
{
int currentLevel = _traineeMap[traineeType];
res = (float)( 101 - currentLevel );
}
return res;
}
void Building::reserveTrainee(const WalkerType traineeType)
{
_reservedTrainees.insert(traineeType);
}
void Building::cancelTrainee(const WalkerType traineeType)
{
_reservedTrainees.erase(traineeType);
}
void Building::applyTrainee(const WalkerType traineeType)
{
_reservedTrainees.erase(traineeType);
_traineeMap[traineeType] += 100;
}
void Building::save( VariantMap& stream) const
{
Construction::save( stream );
stream[ Serializable::damageLevel ] = _damageLevel;
stream[ Serializable::fireLevel ] = _fireLevel;
// stream.write_int(_traineeMap.size(), 1, 0, WTT_MAX);
// for (std::map<WalkerTraineeType, int>::iterator itLevel = _traineeMap.begin(); itLevel != _traineeMap.end(); ++itLevel)
// {
// WalkerTraineeType traineeType = itLevel->first;
// int traineeLevel = itLevel->second;
// stream.write_int((int)traineeType, 1, 0, WTT_MAX);
// stream.write_int(traineeLevel, 1, 0, 200);
// }
//
// stream.write_int(_reservedTrainees.size(), 1, 0, WTT_MAX);
// for (std::set<WalkerTraineeType>::iterator itReservation = _reservedTrainees.begin(); itReservation != _reservedTrainees.end(); ++itReservation)
// {
// WalkerTraineeType traineeType = *itReservation;
// stream.write_int((int)traineeType, 1, 0, WTT_MAX);
// }
}
void Building::load( const VariantMap& stream )
{
Construction::load( stream );
_damageLevel = stream.get( Serializable::damageLevel ).toFloat();
_fireLevel = stream.get( Serializable::fireLevel ).toFloat();
// Construction::unserialize(stream);
// _damageLevel = (float)stream.read_int(1, 0, 100);
// _fireLevel = (float)stream.read_int(1, 0, 100);
//
// int size = stream.read_int(1, 0, WTT_MAX);
// for (int i=0; i<size; ++i)
// {
// WalkerTraineeType traineeType = (WalkerTraineeType) stream.read_int(1, 0, WTT_MAX);
// int traineeLevel = stream.read_int(1, 0, 200);
// _traineeMap[traineeType] = traineeLevel;
// }
//
// size = stream.read_int(1, 0, WTT_MAX);
// for (int i=0; i<size; ++i)
// {
// WalkerTraineeType traineeType = (WalkerTraineeType) stream.read_int(1, 0, WTT_MAX);
// _reservedTrainees.insert(traineeType);
// }
}
// govt 1 - small statue 1 x 1
// govt 2 - medium statue 2 x 2
// govt 3 - big statue 3 x 3
// land3a 43 44 - triumphal arch
// land3a 45 46 - triumphal arch
// transport 93 - missionaire post 2 x 2
// circus 1 ~ 18 hippodrome 5x(5 x 5)
SmallStatue::SmallStatue() : Building( B_STATUE1, Size(1) )
{
_fireIncrement = 0;
_damageIncrement = 0;
setPicture( Picture::load( ResourceGroup::govt, 1));
}
bool SmallStatue::isNeedRoadAccess() const
{
return false;
}
MediumStatue::MediumStatue() : Building( B_STATUE2, Size(2) )
{
_fireIncrement = 0;
_damageIncrement = 0;
setPicture( Picture::load( ResourceGroup::govt, 2));
}
bool MediumStatue::isNeedRoadAccess() const
{
return false;
}
BigStatue::BigStatue() : Building( B_STATUE3, Size(3))
{
_fireIncrement = 0;
_damageIncrement = 0;
setPicture( Picture::load( ResourceGroup::govt, 3));
}
bool BigStatue::isNeedRoadAccess() const
{
return false;
}
Shipyard::Shipyard() : Building( B_SHIPYARD, Size(2) )
{
setPicture( Picture::load( ResourceGroup::transport, 1));
// also transport 2 3 4 check position of river on map
}
// dock pictures
// transport 5 animation = 6~16
// transport 17 animation = 18~28
// transport 29 animation = 30~40
// transport 41 animation = 42~51
Dock::Dock() : Building( B_DOCK, Size(2) )
{
setPicture( Picture::load( ResourceGroup::transport, 5));
_getAnimation().load( ResourceGroup::transport, 6, 11);
// now fill in reverse order
_getAnimation().load( ResourceGroup::transport, 15, 10, Animation::reverse );
_getAnimation().setOffset( Point( 107, 61 ) );
_fgPictures.resize(1);
}
void Dock::timeStep(const unsigned long time)
{
_getAnimation().update( time );
// takes current animation frame and put it into foreground
_fgPictures[ 0 ] = _getAnimation().getCurrentPicture();
}
// second arch pictures is land3a 45 + 46
TriumphalArch::TriumphalArch() : Building( B_TRIUMPHAL_ARCH, Size(3) )
{
setPicture( Picture::load( "land3a", 43 ) );
_getAnimation().load("land3a", 44, 1);
_getAnimation().setOffset( Point( 63, 97 ) );
_fgPictures.resize(1);
_fgPictures.at(0) = _getAnimation().getCurrentPicture();
}