Debian Codegraph @

test-scripting/species/species_macros/happiness.py

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