-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathfurniture_factory.cpp
171 lines (149 loc) · 5.65 KB
/
furniture_factory.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
#include "stdafx.h"
#include "furniture_factory.h"
#include "furniture.h"
#include "furniture_type.h"
#include "view_id.h"
#include "view_layer.h"
#include "view_object.h"
#include "tribe.h"
#include "item_type.h"
#include "level.h"
#include "creature.h"
#include "creature_factory.h"
#include "movement_set.h"
#include "lasting_effect.h"
#include "furniture_usage.h"
#include "furniture_click.h"
#include "furniture_tick.h"
#include "furniture_entry.h"
#include "furniture_dropped_items.h"
#include "furniture_on_built.h"
#include "game_config.h"
SERIALIZE_DEF(FurnitureFactory, furniture, furnitureLists, trainingFurniture, upgrades, bedFurniture, needingLight, increasingPopulation, constructionObjects)
SERIALIZATION_CONSTRUCTOR_IMPL(FurnitureFactory)
bool FurnitureParams::operator == (const FurnitureParams& p) const {
return type == p.type && tribe == p.tribe;
}
FurnitureList FurnitureFactory::getFurnitureList(FurnitureListId id) const {
CHECK(furnitureLists.count(id)) << "Furniture list not found \"" << id.data() << "\"";
return furnitureLists.at(id);
}
int FurnitureFactory::getPopulationIncrease(FurnitureType type, int numBuilt) const {
auto info = getData(type).getPopulationIncrease();
return min(int(numBuilt * info.increase), info.limit.value_or(1000000));
}
PFurniture FurnitureFactory::getFurniture(FurnitureType type, TribeId tribe) const {
USER_CHECK(furniture.count(type)) << "Furniture type not found " << type.data();
auto ret = make_unique<Furniture>(*furniture.at(type));
ret->setTribe(tribe);
return ret;
}
const Furniture& FurnitureFactory::getData(FurnitureType type) const {
if (auto ret = getReferenceMaybe(furniture, type))
return **ret;
FATAL << "Furniture not found " << type.data();
fail();
}
const ViewObject& FurnitureFactory::getConstructionObject(FurnitureType type) const {
return constructionObjects.at(type);
}
void FurnitureFactory::merge(FurnitureFactory f) {
mergeMap(std::move(f.furniture), furniture);
}
FurnitureFactory::FurnitureFactory(map<FurnitureType, unique_ptr<Furniture> > f, map<FurnitureListId, FurnitureList> l)
: furniture(std::move(f)), furnitureLists(std::move(l)) {
}
void FurnitureFactory::initializeInfos() {
for (auto& f : furniture)
for (auto& elem : f.second->getMaxTraining())
trainingFurniture[elem.first].push_back(f.first);
for (auto& base : furniture)
for (auto& other : furniture)
if (isUpgrade(base.first, other.first))
upgrades[base.first].push_back(other.first);
for (auto& f : furniture) {
if (f.second->isRequiresLight())
needingLight.push_back(f.first);
if (f.second->getPopulationIncrease().increase > 0)
increasingPopulation.push_back(f.first);
if (auto type = f.second->getBedType())
bedFurniture[*type].push_back(f.first);
if (auto obj1 = f.second->getViewObject()) {
auto obj = *obj1;
obj.setModifier(ViewObject::Modifier::PLANNED);
constructionObjects[f.first] = obj;
}
}
}
bool FurnitureFactory::canBuild(FurnitureType type, Position pos) const {
auto& data = getData(type);
auto groundF = pos.getFurniture(FurnitureLayer::GROUND);
CHECK(groundF);
if (data.isBridge())
return !!groundF->getDefaultBridge();
if (auto otherPos = data.getSecondPart(pos)) {
if (auto f = otherPos->getFurniture(FurnitureLayer::MIDDLE))
// check for castle wall and prison as a hack to disallow digging into goblin matrons
if (!f->isWall() || f->getType() == FurnitureType("CASTLE_WALL"))
return false;
if (auto f = otherPos->getFurniture(FurnitureLayer::FLOOR))
if (f->getType() == FurnitureType("PRISON"))
return false;
}
if (!data.getBuiltOver().empty()) {
for (auto f : data.getBuiltOver())
if (!!pos.getFurniture(f))
return true;
return false;
}
auto original = pos.getFurniture(getData(type).getLayer());
return (groundF->getMovementSet().canEnter({MovementTrait::WALK}) || data.getLayer() == FurnitureLayer::GROUND) &&
(!original || original->silentlyReplace()) && !pos.isWall();
}
bool FurnitureFactory::isUpgrade(FurnitureType base, FurnitureType upgraded) const {
if (base == upgraded)
return false;
while (upgraded != base)
if (auto u = getData(upgraded).getUpgrade())
upgraded = *u;
else
return false;
return true;
}
const static vector<FurnitureType> emptyUpgrades;
const vector<FurnitureType>& FurnitureFactory::getUpgrades(FurnitureType base) const {
if (auto res = getReferenceMaybe(upgrades, base))
return *res;
else
return emptyUpgrades;
}
FurnitureType FurnitureFactory::getWaterType(double depth) const {
if (depth >= 2.0)
return FurnitureType("WATER");
else if (depth >= 1.0)
return FurnitureType("SHALLOW_WATER1");
else
return FurnitureType("SHALLOW_WATER2");
}
FurnitureFactory::~FurnitureFactory() {
}
FurnitureFactory::FurnitureFactory(FurnitureFactory&&) noexcept = default;
FurnitureFactory& FurnitureFactory::operator =(FurnitureFactory&&) = default;
const vector<FurnitureType>& FurnitureFactory::getTrainingFurniture(AttrType type) const {
static vector<FurnitureType> empty;
if (auto elem = getReferenceMaybe(trainingFurniture, type))
return *elem;
return empty;
}
const vector<FurnitureType>& FurnitureFactory::getFurnitureNeedingLight() const {
return needingLight;
}
const vector<FurnitureType>& FurnitureFactory::getFurnitureThatIncreasePopulation() const {
return increasingPopulation;
}
const vector<FurnitureType>& FurnitureFactory::getBedFurniture(BedType type) const {
return bedFurniture[type];
}
vector<FurnitureType> FurnitureFactory::getAllFurnitureType() const {
return getKeys(furniture).transform([](auto key) { return (FurnitureType) key; });
}