-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconflict_resolver.py
More file actions
246 lines (211 loc) · 9.24 KB
/
Copy pathconflict_resolver.py
File metadata and controls
246 lines (211 loc) · 9.24 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""Probabilistic action resolution — outcomes weighted by readiness, terrain, intensity."""
import random
from config import ACTORS
# Cost per action type in USD (rough estimates per tick)
ACTION_COSTS = {
"no_action": 0,
"military_strike": 500_000_000,
"drone_attack": 50_000_000,
"missile_salvo": 200_000_000,
"naval_blockade": 100_000_000,
"sanctions": 10_000_000,
"diplomatic_pressure": 1_000_000,
"proxy_activation": 30_000_000,
"nuclear_alert": 0,
"ceasefire_offer": 5_000_000,
"troop_mobilization": 150_000_000,
"cyber_attack": 20_000_000,
"aid_package": 200_000_000,
"arms_shipment": 100_000_000,
}
# Munitions cost per action (fraction consumed)
MUNITIONS_COST = {
"military_strike": 0.08,
"drone_attack": 0.03,
"missile_salvo": 0.10,
"naval_blockade": 0.02,
"proxy_activation": 0.01,
"troop_mobilization": 0.02,
"cyber_attack": 0.01,
"arms_shipment": 0.05,
}
# Base casualty ranges per action type [attacker_min, attacker_max, defender_min, defender_max]
CASUALTY_TABLE = {
"military_strike": [5, 50, 50, 500],
"drone_attack": [0, 5, 20, 200],
"missile_salvo": [0, 10, 100, 1000],
"naval_blockade": [0, 5, 0, 20],
"proxy_activation": [0, 10, 10, 100],
"troop_mobilization": [0, 5, 0, 0],
"cyber_attack": [0, 0, 0, 10],
}
# Interception capability (reduces defender casualties)
INTERCEPTION_RATES = {
"ISRAEL": 0.85, # Iron Dome
"USA": 0.75,
"SAUDI": 0.50,
"RUSSIA": 0.40,
"CHINA": 0.45,
"TURKEY": 0.35,
}
# Asymmetric cost: cheap drones vs expensive interceptors
INTERCEPTOR_COST_PER_UNIT = 100_000 # cost to shoot down each incoming
def _success_probability(attacker, defender, action, intensity):
"""Calculate probability of action succeeding."""
base = 0.5
# Readiness advantage
base += (attacker["military_readiness"] - defender["military_readiness"]) * 0.2
# Munitions factor
if attacker["munitions"] < 0.1:
base -= 0.3
# Intensity bonus
base += intensity * 0.15
# Non-state actors are harder to strike (dispersed)
if not defender.get("is_state", True):
base -= 0.1
# Low-cost asymmetric attacks more likely to "succeed"
if action in ("drone_attack", "cyber_attack"):
base += 0.1
return max(0.1, min(0.95, base))
ACTION_UNIT_MAP = {
"military_strike": "plane",
"drone_attack": "drone",
"missile_salvo": "missile",
"naval_blockade": "ship",
"troop_mobilization": "troops",
"proxy_activation": "troops",
"arms_shipment": "ship",
"nuclear_alert": "nuke",
}
_unit_counter = 0
def resolve_actions(world, actions):
"""Resolve all actions for this tick. Returns outcomes dict per actor."""
global _unit_counter
world.active_units = []
outcomes = {} # actor_id -> {success, casualties_inflicted, casualties_taken, intercepted, cost}
for actor_id, action_data in actions.items():
action = action_data["action"]
target_id = action_data["target"]
intensity = action_data["intensity"]
if action == "no_action" or action == "ceasefire_offer":
outcomes[actor_id] = {"success": None, "status": "standby" if action == "no_action" else "offered"}
continue
attacker = world.actors[actor_id]
defender = world.actors.get(target_id) if target_id else None
# --- Munitions cost ---
muni_cost = MUNITIONS_COST.get(action, 0) * intensity
attacker["munitions"] = max(0, attacker["munitions"] - muni_cost)
# --- Financial cost ---
cost = int(ACTION_COSTS.get(action, 0) * intensity)
world.total_cost_usd += cost
# --- Actions that don't need a target ---
if action in ("troop_mobilization", "nuclear_alert"):
if action == "troop_mobilization":
attacker["military_readiness"] = min(1.0, attacker["military_readiness"] + 0.05)
world.log_event(f"{actor_id} mobilizes troops (readiness +5%)", "military")
elif action == "nuclear_alert":
world.log_event(f"NUCLEAR ALERT: {actor_id} raises nuclear readiness!", "military")
unit_type = ACTION_UNIT_MAP.get(action)
if unit_type:
_unit_counter += 1
world.active_units.append({
"id": f"u{_unit_counter}",
"type": unit_type,
"actor": actor_id,
"from": actor_id,
"to": actor_id,
"action": action,
"intensity": intensity,
"success": True,
})
outcomes[actor_id] = {"success": True, "status": "deployed", "cost": cost}
continue
if not defender:
outcomes[actor_id] = {"success": None, "status": "no_target"}
continue
# --- Success check ---
success_prob = _success_probability(attacker, defender, action, intensity)
success = random.random() < success_prob
# --- Casualties ---
cas = CASUALTY_TABLE.get(action, [0, 0, 0, 0])
intercepted = 0
def_casualties = 0
att_casualties = 0
if success:
att_casualties = random.randint(cas[0], cas[1])
def_casualties = random.randint(cas[2], cas[3])
# Scale by intensity
att_casualties = int(att_casualties * intensity)
def_casualties = int(def_casualties * intensity)
# Interception reduces defender casualties from incoming
intercept_rate = INTERCEPTION_RATES.get(target_id, 0.1)
intercepted = int(def_casualties * intercept_rate)
def_casualties -= intercepted
# Interception cost (asymmetric)
if intercepted > 0 and action in ("drone_attack", "missile_salvo"):
intercept_cost = intercepted * INTERCEPTOR_COST_PER_UNIT
world.total_cost_usd += intercept_cost
attacker["casualties"] += att_casualties
defender["casualties"] += max(0, def_casualties)
# Readiness hit
defender["military_readiness"] = max(0, defender["military_readiness"] - 0.02 * intensity)
# Track active conflicts
if target_id not in attacker.get("active_conflicts", []):
attacker.setdefault("active_conflicts", []).append(target_id)
if actor_id not in defender.get("active_conflicts", []):
defender.setdefault("active_conflicts", []).append(actor_id)
world.log_event(
f"{actor_id} {action.replace('_', ' ')} on {target_id}: "
f"SUCCESS — {def_casualties} casualties"
+ (f" ({intercepted} intercepted)" if intercepted > 0 else ""),
"military"
)
else:
att_casualties = random.randint(cas[0], max(cas[0], cas[1] // 2))
att_casualties = int(att_casualties * intensity)
attacker["casualties"] += att_casualties
world.log_event(
f"{actor_id} {action.replace('_', ' ')} on {target_id}: FAILED",
"military"
)
outcomes[actor_id] = {
"success": success,
"status": "hit" if success else "miss",
"casualties_inflicted": def_casualties if success else 0,
"casualties_taken": att_casualties,
"intercepted": intercepted,
"cost": cost,
}
# --- Generate unit for map animation ---
unit_type = ACTION_UNIT_MAP.get(action)
if unit_type:
_unit_counter += 1
world.active_units.append({
"id": f"u{_unit_counter}",
"type": unit_type,
"actor": actor_id,
"from": actor_id,
"to": target_id or actor_id,
"action": action,
"intensity": intensity,
"success": success,
})
# --- Sanctions / diplomatic ---
if action == "sanctions":
defender["economic_health"] = max(0, defender["economic_health"] - 0.03 * intensity)
world.log_event(f"{actor_id} imposes sanctions on {target_id}", "economic")
outcomes[actor_id] = {"success": True, "status": "imposed", "cost": cost}
if action == "diplomatic_pressure":
defender["legitimacy"] = max(0, defender["legitimacy"] - 0.02 * intensity)
world.log_event(f"{actor_id} applies diplomatic pressure on {target_id}", "diplomatic")
outcomes[actor_id] = {"success": True, "status": "applied", "cost": cost}
if action == "aid_package":
defender["economic_health"] = min(1.0, defender["economic_health"] + 0.05)
defender["public_sentiment"] = min(1.0, defender["public_sentiment"] + 0.03)
world.log_event(f"{actor_id} sends aid package to {target_id}", "diplomatic")
outcomes[actor_id] = {"success": True, "status": "delivered", "cost": cost}
if action == "arms_shipment":
defender["munitions"] = min(1.0, defender["munitions"] + 0.08)
world.log_event(f"{actor_id} ships arms to {target_id}", "military")
outcomes[actor_id] = {"success": True, "status": "shipped", "cost": cost}
return outcomes