forked from gecube/opencaesar3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oc3_building_service.cpp
138 lines (118 loc) · 3.32 KB
/
oc3_building_service.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
// 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_service.hpp"
#include <cstdlib>
#include <ctime>
#include "oc3_tile.hpp"
#include "oc3_walker_service.hpp"
#include "oc3_exception.hpp"
#include "oc3_gui_info_box.hpp"
#include "oc3_gettext.hpp"
#include "oc3_variant.hpp"
#include "oc3_city.hpp"
#include "oc3_resourcegroup.hpp"
class ServiceBuilding::Impl
{
public:
int serviceDelay;
Service::Type service;
int serviceTimer;
int serviceRange;
};
ServiceBuilding::ServiceBuilding(const Service::Type service,
const BuildingType type, const Size& size)
: WorkingBuilding( type, size ), _d( new Impl )
{
_d->service = service;
setMaxWorkers(5);
setWorkers(0);
setServiceDelay( 80 );
_d->serviceTimer = 0;
_d->serviceRange = 30;
}
void ServiceBuilding::setServiceDelay( const int delay )
{
_d->serviceDelay = delay;
}
Service::Type ServiceBuilding::getService() const
{
return _d->service;
}
void ServiceBuilding::timeStep(const unsigned long time)
{
WorkingBuilding::timeStep(time);
if (_d->serviceTimer == 0)
{
deliverService();
_d->serviceTimer = getServiceDelay();
}
else if (_d->serviceTimer > 0)
{
_d->serviceTimer -= 1;
}
_getAnimation().update( time );
const Picture& pic = _getAnimation().getCurrentPicture();
if( pic.isValid() )
{
int level = _fgPictures.size()-1;
_fgPictures[level] = _getAnimation().getCurrentPicture();
}
}
void ServiceBuilding::destroy()
{
WorkingBuilding::destroy();
}
void ServiceBuilding::deliverService()
{
// make a service walker and send him to his wandering
ServiceWalkerPtr serviceman = ServiceWalker::create( _getCity(), getService() );
serviceman->setMaxDistance( getWalkerDistance() );
serviceman->send2City( BuildingPtr( this ) );
if( !serviceman->isDeleted() )
{
addWalker( serviceman.as<Walker>() );
}
}
int ServiceBuilding::getServiceRange() const
{
return _d->serviceRange;
}
void ServiceBuilding::save( VariantMap& stream ) const
{
WorkingBuilding::save( stream );
stream[ "timer" ] = _d->serviceTimer;
stream[ "delay" ] = _d->serviceDelay;
stream[ "range" ] = _d->serviceRange;
}
void ServiceBuilding::load( const VariantMap& stream )
{
WorkingBuilding::load( stream );
_d->serviceTimer = (int)stream.get( "timer" );
_d->serviceDelay = (int)stream.get( "delay" );
_d->serviceRange = (int)stream.get( "range" );
}
int ServiceBuilding::getServiceDelay() const
{
return _d->serviceDelay;
}
ServiceBuilding::~ServiceBuilding()
{
}
unsigned int ServiceBuilding::getWalkerDistance() const
{
return 5;
}