-
Notifications
You must be signed in to change notification settings - Fork 6
/
Rules.cs
89 lines (79 loc) · 3.72 KB
/
Rules.cs
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
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using Verse;
namespace TechAdvancing
{
/// <summary>
/// Class storing all the Rules and helper methods.
/// </summary>
class Rules
{
/// <summary>
/// How many total projects there are for a given techlevel, given that they havent been ignored.
/// </summary>
public static Dictionary<TechLevel, int> researchProjectStoreTotal = new Dictionary<TechLevel, int>();
/// <summary>
/// How many finished projects there are for a given techlevel, given that they havent been ignored.
/// </summary>
public static Dictionary<TechLevel, int> researchProjectStoreFinished = new Dictionary<TechLevel, int>();
public static List<ResearchProjectDef> nonIgnoredTechs = new List<ResearchProjectDef>();
public static TechLevel baseFactionLevel = TechLevel.Undefined;
/// <summary>
/// Gets the final techlevel. This includes limits as a maximum and the base faction techlevel as a minimum.
/// </summary>
/// <returns></returns>
internal static TechLevel GetNewTechLevel()
{
if (TechAdvancing_Config_Tab.b_configCheckboxNeedTechColonists)
{
return (TechLevel)(Math.Min((int)GetRuleTechlevel(), (int)GetLowTechTL()));
}
return GetRuleTechlevel();
}
/// <summary>
/// Gets the max techlevel that was generated by any rule. Also takes the faction-min techlevel into account.
/// </summary>
/// <returns></returns>
internal static TechLevel GetRuleTechlevel()
{
LogOutput.WriteLogMessage(Errorlevel.Debug, $"A: {RuleA()} | B:{RuleB()}");
return Util.GetHighestTechlevel(TechAdvancing_Config_Tab.GetBaseTechlevel(), RuleA(), RuleB());
}
/// <summary>
/// Returns the lowTech colony limit. Only if the limit is applied.
/// </summary>
/// <returns></returns>
internal static TechLevel GetLowTechTL()
{
if (!TechAdvancing_Config_Tab.b_configCheckboxNeedTechColonists) // if the limit is not enabled at all
{
return TechLevel.Archotech;
}
else
{
return (TechAdvancing.MapCompSaveHandler.ColonyPeople.Any(x => x.Value?.def?.techLevel >= TechLevel.Industrial)) ? TechLevel.Archotech : TechAdvancing_Config_Tab.maxTechLevelForTribals;
}
}
internal static TechLevel RuleA()
{
var unfinished = researchProjectStoreTotal.Keys.ToDictionary(x => x, x => researchProjectStoreTotal[x] - (researchProjectStoreFinished.ContainsKey(x) ? researchProjectStoreFinished[x] : 0));
var maxProjectTechlevel = researchProjectStoreTotal.Where(x => x.Value > 0).Select(y => (int)y.Key).DefaultIfEmpty(0).Max();
var newTechlevel = unfinished.Where(x => x.Value > 0).Select(x => (int)x.Key).DefaultIfEmpty(maxProjectTechlevel + 1).Min();
return (TechLevel)Util.Clamp(0, newTechlevel - 1 + TechAdvancing_Config_Tab.conditionvalue_A, (int)TechLevel.Archotech);
}
internal static TechLevel RuleB()
{
int result = 0; //tl undef
foreach (var tl in researchProjectStoreTotal.Where(x => x.Value > 0))
{
if ((float)researchProjectStoreFinished[tl.Key] / (float)tl.Value > (TechAdvancing_Config_Tab.conditionvalue_B_s / 100f))
{
result = (int)tl.Key;
}
}
return (TechLevel)Util.Clamp(0, result + (int)TechAdvancing_Config_Tab.conditionvalue_B, (int)TechLevel.Archotech);
}
}
}