Starsector API
Loading...
Searching...
No Matches
FactionHostilityEvent.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.events;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Map;
6
7import org.apache.log4j.Logger;
8
9import com.fs.starfarer.api.Global;
10import com.fs.starfarer.api.campaign.BaseOnMessageDeliveryScript;
11import com.fs.starfarer.api.campaign.CampaignFleetAPI;
12import com.fs.starfarer.api.campaign.CargoAPI;
13import com.fs.starfarer.api.campaign.FactionAPI;
14import com.fs.starfarer.api.campaign.InteractionDialogAPI;
15import com.fs.starfarer.api.campaign.RepLevel;
16import com.fs.starfarer.api.campaign.comm.CommMessageAPI;
17import com.fs.starfarer.api.campaign.comm.MessagePriority;
18import com.fs.starfarer.api.campaign.events.CampaignEventTarget;
19import com.fs.starfarer.api.campaign.rules.MemoryAPI;
20import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.CustomRepImpact;
21import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActionEnvelope;
22import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActions;
23import com.fs.starfarer.api.impl.campaign.ids.Factions;
24import com.fs.starfarer.api.util.Misc;
25import com.fs.starfarer.api.util.Misc.Token;
26
33
34 public static final float HOSTILITY_PENALTY = 0.2f;
35
36 public static class FactionHostilityPairKey {
37 protected FactionAPI one;
38 protected FactionAPI two;
39 public FactionHostilityPairKey(FactionAPI one, FactionAPI two) {
40 this.one = one;
41 this.two = two;
42 if (one.getId().compareTo(two.getId()) > 0) {
43 FactionAPI temp = one;
44 one = two;
45 two = temp;
46 }
47 }
48 public FactionHostilityPairKey(String one, String two) {
49 this(Global.getSector().getFaction(one), Global.getSector().getFaction(two));
50 }
51 public FactionAPI getOne() {
52 return one;
53 }
54 public FactionAPI getTwo() {
55 return two;
56 }
57
58 @Override
59 public int hashCode() {
60 final int prime = 31;
61 int result = 1;
62 result = prime * result
63 + ((one == null) ? 0 : one.hashCode());
64 result = prime * result + ((two == null) ? 0 : two.hashCode());
65 return result;
66 }
67 @Override
68 public boolean equals(Object obj) {
69 if (this == obj)
70 return true;
71 if (obj == null)
72 return false;
73 if (getClass() != obj.getClass())
74 return false;
75 FactionHostilityPairKey other = (FactionHostilityPairKey) obj;
76 if (one == null) {
77 if (other.one != null)
78 return false;
79 } else if (one != other.one)
80 return false;
81 if (this.two == null) {
82 if (other.two != null)
83 return false;
84 } else if (this.two != other.two)
85 return false;
86 return true;
87 }
88 }
89
90
91 public static Logger log = Global.getLogger(FactionHostilityEvent.class);
92
93 protected float elapsedDays = 0f;
94 protected float duration = 0f;
95
96 protected FactionHostilityPairKey target = null;
97 protected FactionAPI one, two;
98 protected boolean ended = false;
99 protected float prevRel = 0f;
100
101 protected float prevRelOne = 0f;
102 protected float prevRelTwo = 0f;
103
104 public void init(String type, CampaignEventTarget eventTarget) {
105 super.init(type, eventTarget, false);
106 }
107
108 public void startEvent() {
109 super.startEvent(true);
110
111 if (!(eventTarget.getCustom() instanceof FactionHostilityPairKey)) {
112 endEvent();
113 return;
114 }
115 target = (FactionHostilityPairKey) eventTarget.getCustom();
116 one = target.one;
117 two = target.two;
118
119 duration = 365f * (0.5f + 0.5f * (float) Math.random());
120
121 log.info(String.format("Starting hostility event: %s -> %s", one.getDisplayName(), two.getDisplayName()));
122
123 FactionAPI commFac = Misc.getCommissionFaction();
124 if (commFac != null && (commFac == one || commFac == two)) {
125 Global.getSector().reportEventStage(this, "warning", Global.getSector().getPlayerFleet(), MessagePriority.ENSURE_DELIVERY, null);
126 }
127 }
128
129 protected void startHostilities() {
130 log.info(String.format("Making factions hostile: %s -> %s", one.getDisplayName(), two.getDisplayName()));
131 prevRel = one.getRelationship(two.getId());
132 one.setRelationship(two.getId(), RepLevel.HOSTILE);
133
134 prevRelOne = one.getRelationship(Factions.PLAYER);
135 prevRelTwo = two.getRelationship(Factions.PLAYER);
136
137 final FactionAPI commFac = Misc.getCommissionFaction();
138 if (commFac != null && (commFac == one || commFac == two)) {
139 Global.getSector().reportEventStage(this, "start", Global.getSector().getPlayerFleet(),
140 MessagePriority.ENSURE_DELIVERY, new BaseOnMessageDeliveryScript() {
141 public void beforeDelivery(CommMessageAPI message) {
142 Global.getSector().adjustPlayerReputation(
143 new RepActionEnvelope(RepActions.MAKE_HOSTILE_AT_BEST, null, null, true),
144 (commFac == one ? two : one).getId());
145 }
146 });
147 } else {
148 Global.getSector().reportEventStage(this, "start", Global.getSector().getPlayerFleet(), MessagePriority.ENSURE_DELIVERY, null);
149 }
150 }
151
152 //protected boolean started = false;
153
154 public void advance(float amount) {
155 if (!isEventStarted()) return;
156 if (isDone()) return;
157
158 float days = Global.getSector().getClock().convertToDays(amount);
159 elapsedDays += days;
160
161 if (!started && elapsedDays > 10f) {
163 started = true;
164 }
165
166 if (elapsedDays >= duration) {
167 endEvent();
168 }
169 }
170
171 protected void endEvent() {
172 if (ended) return;
173
174 ended = true;
175
176 one.setRelationship(two.getId(), Math.max(0f, prevRel));
177
178 Global.getSector().reportEventStage(this, "end", Global.getSector().getPlayerFleet(), MessagePriority.ENSURE_DELIVERY, new BaseOnMessageDeliveryScript() {
179 public void beforeDelivery(CommMessageAPI message) {
180 FactionAPI commFac = Misc.getCommissionFaction();
181 if (commFac == null) return;
182 if (commFac != one && commFac != two) return;
183
184 FactionAPI other = one;
185 float prevRel = prevRelOne;
186 if (other == commFac) {
187 other = two;
189 }
190
191 float currRel = other.getRelationship(Factions.PLAYER);
192 CustomRepImpact impact = new CustomRepImpact();
193 impact.delta = (prevRel - currRel - HOSTILITY_PENALTY);
194 if (impact.delta < 0) impact.delta = 0;
195 Global.getSector().adjustPlayerReputation(
196 new RepActionEnvelope(RepActions.CUSTOM, impact, null, true),
197 other.getId());
198 }
199 });
200 }
201
202 @Override
203 public String getEventName() {
204 String postfix = " hostilities";
205 if (isDone()) {
206 postfix = " hostilities - over";
207 }
208 //return "Hostilities: " + Misc.ucFirst(one.getDisplayName()) + " / " + Misc.ucFirst(two.getDisplayName()) + postfix;
209 return Misc.ucFirst(one.getDisplayName()) + " / " + Misc.ucFirst(two.getDisplayName()) + postfix;
210 }
211
212
213
214 @Override
215 public boolean callEvent(String ruleId, InteractionDialogAPI dialog,
216 List<Token> params, Map<String, MemoryAPI> memoryMap) {
217 String action = params.get(0).getString(memoryMap);
218 CampaignFleetAPI playerFleet = Global.getSector().getPlayerFleet();
219 CargoAPI cargo = playerFleet.getCargo();
220
221 if (action.equals("TODO")) {
222 }
223 return true;
224 }
225
226 @Override
227 public Map<String, String> getTokenReplacements() {
228 Map<String, String> map = super.getTokenReplacements();
229
230
231 addFactionNameTokens(map, "one", one);
232 addFactionNameTokens(map, "two", two);
233
234 FactionAPI commFac = Misc.getCommissionFaction();
235 if (commFac != null && (commFac == one || commFac == two)) {
236 map.put("$sender", commFac.getDisplayName());
237 addFactionNameTokens(map, "commission", commFac);
238 addFactionNameTokens(map, "other", commFac == one ? two : one);
239 }
240
241 //map.put("$sender", "Unknown");
242 return map;
243 }
244
245 public String[] getHighlights(String stageId) {
246 List<String> result = new ArrayList<String>();
247 //addTokensToList(result, "$duration");
248 return result.toArray(new String[0]);
249 }
250
251 public boolean isDone() {
252 return ended;
253 }
254
255}
256
257
258
259
260
261
262
static Logger getLogger(Class c)
Definition Global.java:26
static SectorAPI getSector()
Definition Global.java:59
static void addFactionNameTokens(Map< String, String > tokens, String prefix, FactionAPI faction)
boolean callEvent(String ruleId, InteractionDialogAPI dialog, List< Token > params, Map< String, MemoryAPI > memoryMap)
void init(String type, CampaignEventTarget eventTarget)