Skip to content

Commit a1b39f9

Browse files
committed
* 1.1 fixes and ui cahnges
1 parent 6f7073c commit a1b39f9

File tree

3 files changed

+96
-37
lines changed

3 files changed

+96
-37
lines changed

WarTech/WarTech/Helper.cs

+53-21
Original file line numberDiff line numberDiff line change
@@ -211,28 +211,48 @@ public static StarSystem EvaluateOwnerChange(FactionControl ownerControl, Planet
211211
}
212212

213213
public static void RefreshResources(SimGameState Sim) {
214-
foreach (KeyValuePair<Faction, FactionDef> pair in Sim.FactionsDict) {
215-
if (!IsExcluded(pair.Key)) {
216-
if (Fields.factionResources.Find(x => x.faction == pair.Key) == null) {
217-
Fields.factionResources.Add(new FactionResources(pair.Key, 0, 0));
214+
try {
215+
foreach (KeyValuePair<Faction, FactionDef> pair in Sim.FactionsDict) {
216+
if (!IsExcluded(pair.Key)) {
217+
if (Fields.factionResources.Find(x => x.faction == pair.Key) == null) {
218+
Fields.factionResources.Add(new FactionResources(pair.Key, 0, 0));
219+
}
220+
else {
221+
FactionResources resources = Fields.factionResources.Find(x => x.faction == pair.Key);
222+
resources.offence = 0;
223+
resources.defence = 0;
224+
}
218225
}
219-
else {
220-
FactionResources resources = Fields.factionResources.Find(x => x.faction == pair.Key);
221-
resources.offence = 0;
222-
resources.defence = 0;
226+
if (Fields.factionResources.Find(x => x.faction == Faction.Locals) == null) {
227+
Fields.factionResources.Add(new FactionResources(Faction.Locals, 0, 0));
223228
}
224229
}
225-
if (Fields.factionResources.Find(x => x.faction == Faction.Locals) == null) {
226-
Fields.factionResources.Add(new FactionResources(Faction.Locals, 0, 0));
230+
foreach (StarSystem system in Sim.StarSystems) {
231+
FactionResources resources = Fields.factionResources.Find(x => x.faction == system.Owner);
232+
if (resources != null && !IsExcluded(resources.faction)) {
233+
resources.offence += GetOffenceValue(system);
234+
resources.defence += GetDefenceValue(system);
235+
}
227236
}
228237
}
229-
foreach (StarSystem system in Sim.StarSystems) {
230-
FactionResources resources = Fields.factionResources.Find(x => x.faction == system.Owner);
231-
if (resources != null && !IsExcluded(resources.faction)) {
232-
resources.offence += GetOffenceValue(system);
233-
resources.defence += GetDefenceValue(system);
238+
catch (Exception ex) {
239+
Logger.LogError(ex);
240+
}
241+
}
242+
243+
public static int GetNeighborValue(Dictionary<Faction, int> factionNeighbours, Faction key) {
244+
try {
245+
if (factionNeighbours.ContainsKey(key)) {
246+
return factionNeighbours[key];
247+
}
248+
else {
249+
return 0;
234250
}
235251
}
252+
catch (Exception ex) {
253+
Logger.LogError(ex);
254+
return 0;
255+
}
236256
}
237257

238258
/*
@@ -370,19 +390,31 @@ public static bool IsExcluded(Faction faction) {
370390
public static StarSystem ChangeOwner(StarSystem system, FactionControl control, SimGameState Sim, bool battle) {
371391
try {
372392
if (battle) {
393+
Faction oldOwner = system.Owner;
394+
Faction newOwner = control.faction;
395+
TargetSystem target = null;
373396
if (control.faction != Faction.Locals && Fields.availableTargets.Count > 0) {
374-
TargetSystem target = Fields.availableTargets[control.faction].Find(x => x.system == system);
375-
Fields.availableTargets[control.faction].Remove(target);
376-
target.CalculateNeighbours(Sim);
377-
Fields.availableTargets[system.Owner].Add(target);
397+
target = Fields.availableTargets[newOwner].Find(x => x.system == system);
398+
Fields.availableTargets[newOwner].Remove(target);
378399
}
379-
string factiontag = GetFactionTag(system.Owner);
400+
string factiontag = GetFactionTag(oldOwner);
380401
if (!string.IsNullOrEmpty(factiontag))
381402
system.Tags.Remove(factiontag);
382-
factiontag = GetFactionTag(control.faction);
403+
factiontag = GetFactionTag(newOwner);
383404
if (!string.IsNullOrEmpty(factiontag))
384405
system.Tags.Add(factiontag);
385406
ReflectionHelper.InvokePrivateMethode(system.Def, "set_Owner", new object[] { control.faction });
407+
if (target != null) {
408+
target = new TargetSystem(system, new Dictionary<Faction, int>());
409+
target.CalculateNeighbours(Sim);
410+
if (Fields.availableTargets[oldOwner].Find(x => x.system.Name == system.Name) != null) {
411+
int index = Fields.availableTargets[oldOwner].FindIndex(x => x.system.Name == system.Name);
412+
Fields.availableTargets[oldOwner][index] = target;
413+
}
414+
else {
415+
Fields.availableTargets[oldOwner].Add(target);
416+
}
417+
}
386418
}
387419
if (IsBorder(system, Sim) && Sim.Starmap != null) {
388420
system.Tags.Add("planet_other_battlefield");

WarTech/WarTech/HolderClasses.cs

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class Settings {
1616
public float FatiquePerLostAttack = 0.5f;
1717
public float FatiqueLostPerMonth = 3f;
1818
public float FatiqueRecoveredPerDay = 0.5f;
19+
public string attackercolor = "#ee0000ff";
20+
public string defendercolor = "#00bb00ff";
21+
public string planetcolor = "#00ffffff";
1922
public bool debug = false;
2023
}
2124

WarTech/WarTech/Patch.cs

+40-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ static void Postfix(SimGameState __instance, Contract __result) {
1818
try {
1919
if (Fields.warmission) {
2020
__result.SetInitialReward(Mathf.RoundToInt(__result.InitialContractValue * Fields.settings.priorityContactPayPercentage));
21-
__result.Override.salvagePotential = Mathf.RoundToInt(__result.Override.salvagePotential * Fields.settings.priorityContactPayPercentage);
21+
int maxPriority = Mathf.FloorToInt(7 / __instance.Constants.Salvage.PrioritySalvageModifier);
22+
__result.Override.salvagePotential = Mathf.Min(maxPriority, Mathf.RoundToInt(__result.Override.salvagePotential * Fields.settings.priorityContactPayPercentage));
2223
ReflectionHelper.InvokePrivateMethode(__result, "set_SalvagePotential", new object[] { __result.Override.salvagePotential });
2324
Fields.warmission = false;
2425
}
@@ -216,7 +217,7 @@ static void Prefix(SimGameState __instance, int timeLapse) {
216217
if (!Helper.IsExcluded(pair.Key) && Helper.IsAtWar(pair.Key)) {
217218
List<TargetSystem> availableTargets = Fields.availableTargets[pair.Key];
218219
if (availableTargets.Count > 0) {
219-
List<TargetSystem> targets = availableTargets.OrderByDescending(x => Helper.GetOffenceValue(x.system) + Helper.GetDefenceValue(x.system) + x.factionNeighbours[pair.Key]).ToList();
220+
List<TargetSystem> targets = availableTargets.OrderByDescending(x => Helper.GetOffenceValue(x.system) + Helper.GetDefenceValue(x.system) + Helper.GetNeighborValue(x.factionNeighbours, pair.Key)).ToList();
220221
int numberOfAttacks = Mathf.Min(targets.Count, Mathf.CeilToInt(Fields.factionResources.Find(x => x.faction == pair.Key).offence / 100f));
221222
for (int i = 0; i < numberOfAttacks; i++) {
222223
StarSystem system = __instance.StarSystems.Find(x => x.Name == targets[i].system.Name);
@@ -239,9 +240,19 @@ static void Prefix(SimGameState __instance, int timeLapse) {
239240
StarSystem changedSystem = __instance.StarSystems.Find(x => x.Name.Equals(changes.Key));
240241
if (!Helper.GetFactionName(changedSystem.Owner, __instance.DataManager).Equals(changes.Value)) {
241242
War war = Helper.getWar(changedSystem.Owner);
242-
war.monthlyEvents.Add(Helper.GetFactionName(changedSystem.Owner, __instance.DataManager) + " took " + changes.Key + " from " + changes.Value + ".");
243+
if (war != null) {
244+
if (war.attackers.Contains(changedSystem.Owner)) {
245+
war.monthlyEvents.Add("<color=" + Fields.settings.attackercolor + ">" + Helper.GetFactionName(changedSystem.Owner, __instance.DataManager) + "</color>" + " took " + "<color=" + Fields.settings.planetcolor + ">" + changes.Key + "</color>" + " from " + "<color=" + Fields.settings.defendercolor + ">" + changes.Value + "</color>" + ".");
246+
}
247+
else {
248+
war.monthlyEvents.Add("<color=" + Fields.settings.defendercolor + ">" + Helper.GetFactionName(changedSystem.Owner, __instance.DataManager) + "</color>" + " took " + "<color=" + Fields.settings.planetcolor + ">" + changes.Key + "</color>" + " from " + "<color=" + Fields.settings.attackercolor + ">" + changes.Value + "</color>" + ".");
249+
}
250+
}
243251
}
244252
}
253+
foreach(War war in Fields.currentWars) {
254+
war.monthlyEvents.Add("\n");
255+
}
245256
Dictionary<Faction, FactionDef> factions = (Dictionary<Faction, FactionDef>)AccessTools.Field(typeof(SimGameState), "factions").GetValue(__instance);
246257
foreach (KeyValuePair<Faction, FactionDef> pair in factions) {
247258
List<Faction> fac = null;
@@ -259,21 +270,24 @@ static void Prefix(SimGameState __instance, int timeLapse) {
259270
foreach (War war in Fields.currentWars) {
260271
if (war.attackers.Contains(enemy) && !Fields.removeWars.Contains(war.name)) {
261272
war.defenders.Add(pair.Key);
262-
war.monthlyEvents.Add(Helper.GetFactionName(pair.Key, __instance.DataManager) + " joined the war on the defending side.");
273+
war.monthlyEvents.Add("<color=" + Fields.settings.defendercolor + ">" + Helper.GetFactionName(pair.Key, __instance.DataManager) + "</color>" + " joined the war.");
263274
joinedWar = true;
264275
break;
265276
}
266277
else if (war.defenders.Contains(enemy) && !Fields.removeWars.Contains(war.name)) {
267278
war.attackers.Add(pair.Key);
268-
war.monthlyEvents.Add(Helper.GetFactionName(pair.Key, __instance.DataManager) + " joined the war on the attacking side.");
279+
war.monthlyEvents.Add("<color=" + Fields.settings.attackercolor + ">" + Helper.GetFactionName(pair.Key, __instance.DataManager) + "</color>" + " joined the war.");
269280
joinedWar = true;
270281
break;
271282
}
272283
}
273284
if (!joinedWar) {
274285
string Name = Helper.GetFactionShortName(pair.Key, __instance.DataManager) + " VS " + Helper.GetFactionShortName(enemy, __instance.DataManager);
275286
War war = new War(Name, new List<Faction>() { pair.Key }, new List<Faction>() { enemy });
276-
war.monthlyEvents.Add(Helper.GetFactionName(pair.Key, __instance.DataManager) + " declared war on " + Helper.GetFactionName(enemy, __instance.DataManager) + ".");
287+
war.monthlyEvents.Add("<color=" + Fields.settings.attackercolor + ">" + Helper.GetFactionName(pair.Key, __instance.DataManager) + "</color>" + " declared war on " + "<color=" + Fields.settings.defendercolor + ">" + Helper.GetFactionName(enemy, __instance.DataManager) + "</color>" + ".\n");
288+
if (Fields.currentWars.Contains(war)) {
289+
Logger.LogLine(war.name + "already exists");
290+
}
277291
Fields.currentWars.Add(war);
278292
}
279293
}
@@ -282,25 +296,31 @@ static void Prefix(SimGameState __instance, int timeLapse) {
282296
Fields.WarFatique[pair.Key] += Fields.settings.FatiqueLostPerMonth;
283297
if (rand.Next(0, 101) < Fields.WarFatique[pair.Key]) {
284298
War war = Helper.getWar(pair.Key);
299+
if (war == null) {
300+
Logger.LogLine(pair.Key + " has no war at end war calculations");
301+
}
285302
if (war.duration >= Fields.settings.minMonthDuration) {
286303
if (war.duration < Fields.settings.maxMonthDuration || Fields.settings.maxMonthDuration == -1) {
287304
if (!(Fields.currentWars.Find(x => x.name.Equals(war.name)).attackers.Count <= 0) && !(Fields.currentWars.Find(x => x.name.Equals(war.name)).defenders.Count <= 0)) {
305+
string color = "";
288306
if (Fields.currentWars.Find(x => x.name.Equals(war.name)).attackers.Contains(pair.Key)) {
307+
color = Fields.settings.attackercolor;
289308
Fields.currentWars.Find(x => x.name.Equals(war.name)).attackers.Remove(pair.Key);
290309
}
291310
else {
311+
color = Fields.settings.defendercolor;
292312
Fields.currentWars.Find(x => x.name.Equals(war.name)).defenders.Remove(pair.Key);
293313
}
294-
Fields.currentWars.Find(x => x.name.Equals(war.name)).monthlyEvents.Add(Helper.GetFactionName(pair.Key, __instance.DataManager) + " left the war.");
314+
Fields.currentWars.Find(x => x.name.Equals(war.name)).monthlyEvents.Add("<color=" + color + ">" + Helper.GetFactionName(pair.Key, __instance.DataManager) + "</color>" + " left the war.");
295315

296316
if (Fields.currentWars.Find(x => x.name.Equals(war.name)).attackers.Count <= 0) {
297-
Fields.currentWars.Find(x => x.name.Equals(war.name)).monthlyEvents.Add("The attacking side lost the war.");
317+
Fields.currentWars.Find(x => x.name.Equals(war.name)).monthlyEvents.Add("\n<b>The attacking side lost the war.</b>");
298318
if (!Fields.removeWars.Contains(war.name)) {
299319
Fields.removeWars.Add(war.name);
300320
}
301321
}
302322
else if (Fields.currentWars.Find(x => x.name.Equals(war.name)).defenders.Count <= 0) {
303-
Fields.currentWars.Find(x => x.name.Equals(war.name)).monthlyEvents.Add("The Defending side lost the war.");
323+
Fields.currentWars.Find(x => x.name.Equals(war.name)).monthlyEvents.Add("\n<b>The defending side lost the war.</b>");
304324
if (!Fields.removeWars.Contains(war.name)) {
305325
Fields.removeWars.Add(war.name);
306326
}
@@ -328,13 +348,18 @@ static void Prefix(SimGameState __instance, int timeLapse) {
328348

329349
foreach (War war in Fields.currentWars) {
330350
war.duration += 1;
331-
war.monthlyEvents.Add("\nAttackers:");
332-
foreach (Faction fac in war.attackers) {
333-
war.monthlyEvents.Add(Helper.GetFactionName(fac, __instance.DataManager) + " | Exhaustion: " + Fields.WarFatique[fac] + "%");
351+
if (!Fields.removeWars.Contains(war.name)) {
352+
war.monthlyEvents.Add("<color=" + Fields.settings.attackercolor + ">" + "\nAttackers:" + "</color>");
353+
foreach (Faction fac in war.attackers) {
354+
war.monthlyEvents.Add(Helper.GetFactionName(fac, __instance.DataManager) + " | Exhaustion: " + Fields.WarFatique[fac] + "%");
355+
}
356+
war.monthlyEvents.Add("<color=" + Fields.settings.defendercolor + ">" + "\nDefenders:" + "</color>");
357+
foreach (Faction fac in war.defenders) {
358+
war.monthlyEvents.Add(Helper.GetFactionName(fac, __instance.DataManager) + " | Exhaustion: " + Fields.WarFatique[fac] + "%");
359+
}
334360
}
335-
war.monthlyEvents.Add("\nDefenders:");
336-
foreach (Faction fac in war.defenders) {
337-
war.monthlyEvents.Add(Helper.GetFactionName(fac, __instance.DataManager) + " | Exhaustion: " + Fields.WarFatique[fac] + "%");
361+
for(int i = 0; i < war.monthlyEvents.Count; i++) {
362+
war.monthlyEvents[i] = war.monthlyEvents[i].Replace("the ", "").Replace("The ", "");
338363
}
339364
interruptQueue.QueueGenericPopup_NonImmediate(war.name, string.Join("\n", war.monthlyEvents.ToArray()) + "\n", true); war.monthlyEvents.Clear();
340365
}
@@ -351,7 +376,6 @@ static void Prefix(SimGameState __instance, int timeLapse) {
351376
}
352377
catch (Exception e) {
353378
Logger.LogError(e);
354-
Logger.LogError(e);
355379
}
356380
}
357381
}

0 commit comments

Comments
 (0)