1 #include "stdafx.h"
2 #include "immigrant_info.h"
3 #include "furniture.h"
4 #include "item_index.h"
5 #include "game.h"
6 #include "collective.h"
7 #include "creature.h"
8 #include "creature_attributes.h"
9 #include "tutorial.h"
10 
SERIALIZE_DEF(ImmigrantInfo,ids,frequency,requirements,traits,spawnLocation,groupSize,autoTeam,initialRecruitment,consumeIds,keybinding,sound,noAuto,tutorialHighlight,hiddenInHelp)11 SERIALIZE_DEF(ImmigrantInfo, ids, frequency, requirements, traits, spawnLocation, groupSize, autoTeam, initialRecruitment, consumeIds, keybinding, sound, noAuto, tutorialHighlight, hiddenInHelp)
12 SERIALIZATION_CONSTRUCTOR_IMPL(ImmigrantInfo)
13 
14 AttractionInfo::AttractionInfo(int cl,  AttractionType a)
15   : types({a}), amountClaimed(cl) {}
16 
getAttractionName(const AttractionType & attraction,int count)17 string AttractionInfo::getAttractionName(const AttractionType& attraction, int count) {
18   return attraction.match(
19       [&](FurnitureType type)->string {
20         return Furniture::getName(type, count);
21       },
22       [&](ItemIndex index)->string {
23         return getName(index, count);
24       }
25   );
26 }
27 
AttractionInfo(int cl,vector<AttractionType> a)28 AttractionInfo::AttractionInfo(int cl, vector<AttractionType> a)
29   : types(a), amountClaimed(cl) {}
30 
ImmigrantInfo(CreatureId id,EnumSet<MinionTrait> t)31 ImmigrantInfo::ImmigrantInfo(CreatureId id, EnumSet<MinionTrait> t) : ids({id}), traits(t) {}
ImmigrantInfo(vector<CreatureId> id,EnumSet<MinionTrait> t)32 ImmigrantInfo::ImmigrantInfo(vector<CreatureId> id, EnumSet<MinionTrait> t) : ids(id), traits(t), consumeIds(true) {
33 }
34 
getId(int numCreated) const35 CreatureId ImmigrantInfo::getId(int numCreated) const {
36   if (!consumeIds)
37     return Random.choose(ids);
38   else
39     return ids[numCreated];
40 }
41 
isAvailable(int numCreated) const42 bool ImmigrantInfo::isAvailable(int numCreated) const {
43   if (!consumeIds)
44     return true;
45   else
46     return numCreated < ids.size();
47 }
48 
getSpawnLocation() const49 const SpawnLocation& ImmigrantInfo::getSpawnLocation() const {
50   return spawnLocation;
51 }
52 
getGroupSize() const53 Range ImmigrantInfo::getGroupSize() const {
54   return groupSize;
55 }
56 
getInitialRecruitment() const57 int ImmigrantInfo::getInitialRecruitment() const {
58   return initialRecruitment;
59 }
60 
isAutoTeam() const61 bool ImmigrantInfo::isAutoTeam() const {
62   return autoTeam;
63 }
64 
getFrequency() const65 double ImmigrantInfo::getFrequency() const {
66   CHECK(frequency);
67   return *frequency;
68 }
69 
isPersistent() const70 bool ImmigrantInfo::isPersistent() const {
71   return !frequency;
72 }
73 
getTraits() const74 const EnumSet<MinionTrait>&ImmigrantInfo::getTraits() const {
75   return traits;
76 }
77 
getLimit() const78 optional<int> ImmigrantInfo::getLimit() const {
79   if (consumeIds)
80     return ids.size();
81   else
82     return none;
83 }
84 
getKeybinding() const85 optional<Keybinding> ImmigrantInfo::getKeybinding() const {
86   return keybinding;
87 }
88 
getSound() const89 optional<Sound> ImmigrantInfo::getSound() const {
90   return sound;
91 }
92 
isNoAuto() const93 bool ImmigrantInfo::isNoAuto() const {
94   return noAuto;
95 }
96 
getTutorialHighlight() const97 optional<TutorialHighlight> ImmigrantInfo::getTutorialHighlight() const {
98   return tutorialHighlight;
99 }
100 
isHiddenInHelp() const101 bool ImmigrantInfo::isHiddenInHelp() const {
102   return hiddenInHelp;
103 }
104 
addRequirement(ImmigrantRequirement t)105 ImmigrantInfo& ImmigrantInfo::addRequirement(ImmigrantRequirement t) {
106   requirements.push_back({t, 1});
107   return *this;
108 }
109 
addRequirement(double prob,ImmigrantRequirement t)110 ImmigrantInfo& ImmigrantInfo::addRequirement(double prob, ImmigrantRequirement t) {
111   requirements.push_back({t, prob});
112   return *this;
113 }
114 
setFrequency(double f)115 ImmigrantInfo& ImmigrantInfo::setFrequency(double f) {
116   frequency = f;
117   return *this;
118 }
119 
setSpawnLocation(SpawnLocation l)120 ImmigrantInfo& ImmigrantInfo::setSpawnLocation(SpawnLocation l) {
121   spawnLocation = l;
122   return *this;
123 }
124 
setInitialRecruitment(int i)125 ImmigrantInfo& ImmigrantInfo::setInitialRecruitment(int i) {
126   initialRecruitment = i;
127   return *this;
128 }
129 
setGroupSize(Range r)130 ImmigrantInfo& ImmigrantInfo::setGroupSize(Range r) {
131   groupSize = r;
132   return *this;
133 }
134 
setAutoTeam()135 ImmigrantInfo& ImmigrantInfo::setAutoTeam() {
136   autoTeam = true;
137   return *this;
138 }
139 
setKeybinding(Keybinding key)140 ImmigrantInfo& ImmigrantInfo::setKeybinding(Keybinding key) {
141   keybinding = key;
142   return *this;
143 }
144 
setSound(Sound s)145 ImmigrantInfo& ImmigrantInfo::setSound(Sound s) {
146   sound = s;
147   return *this;
148 }
149 
setNoAuto()150 ImmigrantInfo& ImmigrantInfo::setNoAuto() {
151   noAuto = true;
152   return *this;
153 }
154 
setLimit(int num)155 ImmigrantInfo&ImmigrantInfo::setLimit(int num) {
156   consumeIds = true;
157   ids = vector<CreatureId>(num, ids[0]);
158   return *this;
159 }
160 
setTutorialHighlight(TutorialHighlight h)161 ImmigrantInfo& ImmigrantInfo::setTutorialHighlight(TutorialHighlight h) {
162   tutorialHighlight = h;
163   return *this;
164 }
165 
setHiddenInHelp()166 ImmigrantInfo& ImmigrantInfo::setHiddenInHelp() {
167   hiddenInHelp = true;
168   return *this;
169 }
170 
getAllRecruits(WGame game,CreatureId id) const171 vector<WCreature> RecruitmentInfo::getAllRecruits(WGame game, CreatureId id) const {
172   vector<WCreature> ret;
173   if (WCollective col = findEnemy(game))
174     ret = col->getCreatures().filter([&](WConstCreature c) { return c->getAttributes().getCreatureId() == id; });
175   return ret;
176 }
177 
getAvailableRecruits(WGame game,CreatureId id) const178 vector<WCreature> RecruitmentInfo::getAvailableRecruits(WGame game, CreatureId id) const {
179   auto ret = getAllRecruits(game, id);
180   return getPrefix(ret, max(0, (int)ret.size() - minPopulation));
181 }
182 
findEnemy(WGame game) const183 WCollective RecruitmentInfo::findEnemy(WGame game) const {
184   for (WCollective col : game->getCollectives())
185     if (auto id = col->getEnemyId())
186       if (enemyId.contains(*id))
187         return col;
188   return nullptr;
189 }
190 
191 template <typename Archive>
serialize(Archive & ar,const unsigned)192 void TutorialRequirement::serialize(Archive& ar, const unsigned) {
193   ar(tutorial);
194 }
195