Debian Codegraph @

default/scripting/species/species_macros/happiness.py

raw
1 from focs._effects import (
2 Abs,
3 Adequate,
4 Capital,
5 Contains,
6 CurrentTurn,
7 EffectsGroup,
8 EmpireHasAdoptedPolicy,
9 GameRule,
10 GasGiantType,
11 Good,
12 HasSpecies,
13 HasTag,
14 Homeworld,
15 Hostile,
16 Huge,
17 InSystem,
18 IsBuilding,
19 IsSource,
20 JumpsBetween,
21 Large,
22 LocalCandidate,
23 MaxOf,
24 Medium,
25 Min,
26 MinOf,
27 NamedReal,
28 OwnedBy,
29 Planet,
30 Poor,
31 Population,
32 ResourceSupplyConnected,
33 SetHappiness,
34 SetTargetHappiness,
35 Small,
36 Source,
37 SpeciesDislikes,
38 SpeciesLikes,
39 Statistic,
40 Target,
41 Tiny,
42 Uninhabitable,
43 Unowned,
44 Value,
45 )
46 from macros.misc import DESCRIPTION_EFFECTSGROUP_MACRO
47 from macros.opinion import POLICY_DISLIKE_SCALING
48 from macros.priorities import AFTER_ALL_TARGET_MAX_METERS_PRIORITY
49
50 _STABILITY_PER_LIKED_FOCUS = 2.0
51
52 # Policy Liberty doubles de focus dislike effect, policy Conformance halves it, macro is in common/opinion.macros
53 _STABILITY_PER_DISLIKED_FOCUS = 2.0 * POLICY_DISLIKE_SCALING
54
55 _ENVIRONMENT_STABILITY_MODIFIER = [
56 EffectsGroup(
57 scope=IsSource,
58 activation=Planet()
59 & Planet(environment=[Uninhabitable])
60 & (GameRule(type=int, name="RULE_HOSTILE_ENVIRONMENT_STABILITY") != 0),
61 accountinglabel="UNINHABTIABLE_ENVIRONMENT_STABILITY_LABEL",
62 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_HOSTILE_ENVIRONMENT_STABILITY")),
63 ),
64 EffectsGroup(
65 scope=IsSource,
66 activation=Planet()
67 & Planet(environment=[Hostile])
68 & (GameRule(type=int, name="RULE_HOSTILE_ENVIRONMENT_STABILITY") != 0),
69 accountinglabel="HOSTILE_ENVIRONMENT_STABILITY_LABEL",
70 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_HOSTILE_ENVIRONMENT_STABILITY")),
71 ),
72 EffectsGroup(
73 scope=IsSource,
74 activation=Planet()
75 & Planet(environment=[Poor])
76 & (GameRule(type=int, name="RULE_POOR_ENVIRONMENT_STABILITY") != 0),
77 accountinglabel="POOR_ENVIRONMENT_STABILITY_LABEL",
78 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_POOR_ENVIRONMENT_STABILITY")),
79 ),
80 EffectsGroup(
81 scope=IsSource,
82 activation=Planet()
83 & Planet(environment=[Adequate])
84 & (GameRule(type=int, name="RULE_ADEQUATE_ENVIRONMENT_STABILITY") != 0),
85 accountinglabel="ADEQUATE_ENVIRONMENT_STABILITY_LABEL",
86 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_ADEQUATE_ENVIRONMENT_STABILITY")),
87 ),
88 EffectsGroup(
89 scope=IsSource,
90 activation=Planet()
91 & Planet(environment=[Good])
92 & (GameRule(type=int, name="RULE_GOOD_ENVIRONMENT_STABILITY") != 0),
93 accountinglabel="GOOD_ENVIRONMENT_STABILITY_LABEL",
94 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_GOOD_ENVIRONMENT_STABILITY")),
95 ),
96 ]
97
98 _PLANET_SIZE_STABILITY_MODIFIER = [
99 EffectsGroup(
100 scope=IsSource,
101 activation=Planet() & Planet(size=[Tiny]) & (GameRule(type=int, name="RULE_TINY_SIZE_STABILITY") != 0),
102 accountinglabel="TINY_PLANET_STABILITY_LABEL",
103 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_TINY_SIZE_STABILITY")),
104 ),
105 EffectsGroup(
106 scope=IsSource,
107 activation=Planet() & Planet(size=[Small]) & (GameRule(type=int, name="RULE_SMALL_SIZE_STABILITY") != 0),
108 accountinglabel="SMALL_PLANET_STABILITY_LABEL",
109 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_SMALL_SIZE_STABILITY")),
110 ),
111 EffectsGroup(
112 scope=IsSource,
113 activation=Planet() & Planet(size=[Medium]) & (GameRule(type=int, name="RULE_MEDIUM_SIZE_STABILITY") != 0),
114 accountinglabel="SMALL_MEDIUM_STABILITY_LABEL",
115 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_MEDIUM_SIZE_STABILITY")),
116 ),
117 EffectsGroup(
118 scope=IsSource,
119 activation=Planet() & Planet(size=[Large]) & (GameRule(type=int, name="RULE_LARGE_SIZE_STABILITY") != 0),
120 accountinglabel="LARGE_PLANET_STABILITY_LABEL",
121 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_LARGE_SIZE_STABILITY")),
122 ),
123 EffectsGroup(
124 scope=IsSource,
125 activation=Planet() & Planet(size=[Huge]) & (GameRule(type=int, name="RULE_HUGE_SIZE_STABILITY") != 0),
126 accountinglabel="HUGE_PLANET_STABILITY_LABEL",
127 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_HUGE_SIZE_STABILITY")),
128 ),
129 EffectsGroup(
130 scope=IsSource,
131 activation=Planet()
132 & Planet(type=[GasGiantType])
133 & (GameRule(type=int, name="RULE_GAS_GIANT_SIZE_STABILITY") != 0),
134 accountinglabel="GAS_GIANT_STABILITY_LABEL",
135 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_GAS_GIANT_SIZE_STABILITY")),
136 ),
137 ]
138
139 _COMMON_HAPPINESS_EFFECTS = [
140 *_ENVIRONMENT_STABILITY_MODIFIER,
141 *_PLANET_SIZE_STABILITY_MODIFIER,
142 EffectsGroup( # increase or decrease 1 per turn towards target
143 scope=IsSource,
144 activation=Planet() & (LocalCandidate.LastTurnConquered < CurrentTurn),
145 priority=AFTER_ALL_TARGET_MAX_METERS_PRIORITY,
146 effects=SetHappiness(
147 value=Value
148 + MinOf(float, Abs(float, Value(Target.TargetHappiness) - Value), 1)
149 * (1 - 2 * (Value > Value(Target.TargetHappiness)))
150 ),
151 ),
152 EffectsGroup( # artistic species make other planets with different artistic species on them and in the same system system more stable
153 scope=Planet()
154 & InSystem(id=Source.SystemID)
155 & ~IsSource
156 & HasSpecies()
157 & ~HasSpecies(name=[Source.Species])
158 & HasTag(name="ARTISTIC")
159 # (SpeciesSpeciesOpinion species = LocalCandidate.Species species = Source.Species > 0) # TODO: implement within an Or condition, once species-species opinions are working...
160 ,
161 activation=Planet() & HasSpecies() & HasTag(name="ARTISTIC"),
162 accountinglabel="ARTISAN_APPRECIATION",
163 effects=SetTargetHappiness(value=Value + 1.0),
164 ),
165 EffectsGroup( # when not owned by an empire, directly apply baseline stability
166 scope=IsSource,
167 activation=GameRule(type=int, name="RULE_BASELINE_PLANET_STABILITY") != 0,
168 accountinglabel="RULE_BASELINE_PLANET_STABILITY",
169 effects=SetTargetHappiness(value=Value + GameRule(type=float, name="RULE_BASELINE_PLANET_STABILITY")),
170 ),
171 EffectsGroup( # more stable when liked focuses are adopted
172 scope=IsSource,
173 activation=Population(low=0.001) & SpeciesLikes(name=LocalCandidate.Focus),
174 accountinglabel="LIKES_FOCUS_LABEL",
175 effects=SetTargetHappiness(value=Value + _STABILITY_PER_LIKED_FOCUS),
176 ),
177 EffectsGroup( # less stable when disliked focuses are adopted
178 scope=IsSource,
179 activation=~Unowned & Population(low=0.001) & SpeciesDislikes(name=LocalCandidate.Focus),
180 accountinglabel="DISLIKES_FOCUS_LABEL",
181 effects=SetTargetHappiness(value=Value - _STABILITY_PER_DISLIKED_FOCUS),
182 ),
183 EffectsGroup( # species on their own homeworld are more stable
184 scope=IsSource,
185 activation=Homeworld(name=[Source.Species]),
186 accountinglabel="HOMEWORLD_BONUS",
187 effects=SetTargetHappiness(value=Value + 5),
188 ),
189 EffectsGroup( # capital planet is more stable
190 scope=IsSource,
191 activation=Capital,
192 accountinglabel="CAPITAL_LABEL",
193 effects=SetTargetHappiness(value=Value + 10),
194 ),
195 EffectsGroup( # concentration camp nullifies stability
196 scope=IsSource,
197 activation=Contains(IsBuilding(name=["BLD_CONC_CAMP"])),
198 accountinglabel="CONCENTRATION_CAMPS_LABEL",
199 priority=AFTER_ALL_TARGET_MAX_METERS_PRIORITY,
200 effects=[SetHappiness(value=0), SetTargetHappiness(value=0)],
201 ),
202 ]
203
204 # this should ensure that there is a capital / admin for JUMPS_TO_CAPITAL_OR_REGAD
205 _SUPPLY_CONNECTED_TO_CAPITAL_OR_REGAD = ResourceSupplyConnected(
206 empire=Source.Owner, condition=(Capital | IsBuilding(name=["BLD_REGIONAL_ADMIN"])) & OwnedBy(empire=Source.Owner)
207 )
208
209
210 def _jumps_to_capital_or_regad(type):
211 return Statistic(
212 type,
213 Min,
214 # Replace JumpsBetween by JumpsBetweenByEmpireSupplyConnections once it is implemented
215 value=JumpsBetween(Source.ID, LocalCandidate.ID),
216 condition=(
217 (Capital | IsBuilding(name=["BLD_REGIONAL_ADMIN"]))
218 & OwnedBy(empire=Source.Owner)
219 & ResourceSupplyConnected(empire=Source.Owner, condition=IsSource)
220 ),
221 )
222
223
224 _STANDARD_SPECIES_CAPITAL_SUPPLY_CONNECTION_STABILITY = [
225 EffectsGroup( # close supply connected to capital planet is more stable
226 scope=IsSource,
227 activation=Planet()
228 & ~Unowned
229 & ~Capital
230 & _SUPPLY_CONNECTED_TO_CAPITAL_OR_REGAD
231 & (_jumps_to_capital_or_regad(int) < 5),
232 accountinglabel="CAPITAL_CONNECTION_LABEL",
233 effects=SetTargetHappiness(value=Value + MinOf(float, 5, 5 - _jumps_to_capital_or_regad(float))),
234 ),
235 EffectsGroup( # far supply connected to capital planet is less
236 scope=IsSource,
237 activation=Planet()
238 & ~Unowned
239 & ~Capital
240 & ~EmpireHasAdoptedPolicy(empire=Source.Owner, name="PLC_CONFEDERATION")
241 & _SUPPLY_CONNECTED_TO_CAPITAL_OR_REGAD
242 & (_jumps_to_capital_or_regad(int) > 5),
243 accountinglabel="CAPITAL_POOR_CONNECTION_LABEL",
244 effects=SetTargetHappiness(value=Value + MaxOf(float, -5, 5 - _jumps_to_capital_or_regad(float))),
245 ),
246 EffectsGroup( # no connection is much less stable
247 scope=IsSource,
248 activation=Planet()
249 & ~Unowned
250 & ~Capital
251 & ~EmpireHasAdoptedPolicy(empire=Source.Owner, name="PLC_CONFEDERATION")
252 & ~_SUPPLY_CONNECTED_TO_CAPITAL_OR_REGAD,
253 accountinglabel="CAPITAL_DISCONNECTION_LABEL",
254 effects=SetTargetHappiness(
255 value=Value - NamedReal(name="DISCONNECTED_FROM_CAPITAL_AND_REGIONAL_ADMIN_STABILITY_PENALTY", value=10)
256 ),
257 ),
258 ]
259
260 VERY_BAD_HAPPINESS = [
261 *_COMMON_HAPPINESS_EFFECTS,
262 *_STANDARD_SPECIES_CAPITAL_SUPPLY_CONNECTION_STABILITY,
263 EffectsGroup(
264 description="VERY_BAD_HAPPINESS_DESC",
265 scope=IsSource,
266 activation=Planet(),
267 accountinglabel="VERY_BAD_HAPPINESS_LABEL",
268 effects=SetTargetHappiness(value=Value + NamedReal(name="VERY_BAD_HAPPINESS_VAL", value=-5.0)),
269 ),
270 ]
271
272 BAD_HAPPINESS = [
273 *_COMMON_HAPPINESS_EFFECTS,
274 *_STANDARD_SPECIES_CAPITAL_SUPPLY_CONNECTION_STABILITY,
275 EffectsGroup(
276 description="BAD_HAPPINESS_DESC",
277 scope=IsSource,
278 activation=Planet(),
279 accountinglabel="BAD_HAPPINESS_LABEL",
280 effects=SetTargetHappiness(value=Value + NamedReal(name="BAD_HAPPINESS_VAL", value=-2.5)),
281 ),
282 ]
283
284 AVERAGE_HAPPINESS = [
285 *_COMMON_HAPPINESS_EFFECTS,
286 *_STANDARD_SPECIES_CAPITAL_SUPPLY_CONNECTION_STABILITY,
287 ]
288
289 GOOD_HAPPINESS = [
290 *_COMMON_HAPPINESS_EFFECTS,
291 *_STANDARD_SPECIES_CAPITAL_SUPPLY_CONNECTION_STABILITY,
292 EffectsGroup(
293 description="GOOD_HAPPINESS_DESC",
294 scope=IsSource,
295 activation=Planet(),
296 accountinglabel="GOOD_HAPPINESS_LABEL",
297 effects=SetTargetHappiness(value=Value + NamedReal(name="GOOD_HAPPINESS_VAL", value=2.5)),
298 ),
299 ]
300
301 GREAT_HAPPINESS = [
302 *_COMMON_HAPPINESS_EFFECTS,
303 *_STANDARD_SPECIES_CAPITAL_SUPPLY_CONNECTION_STABILITY,
304 EffectsGroup(
305 description="GREAT_HAPPINESS_DESC",
306 scope=IsSource,
307 activation=Planet(),
308 accountinglabel="GREAT_HAPPINESS_LABEL",
309 effects=SetTargetHappiness(value=Value + NamedReal(name="GREAT_HAPPINESS_VAL", value=5)),
310 ),
311 ]
312
313
314 INDEPENDENT_HAPPINESS = [DESCRIPTION_EFFECTSGROUP_MACRO("INDEPENDENT_HAPPINESS_DESC"), *_COMMON_HAPPINESS_EFFECTS]