-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathStnSciScenario.cs
executable file
·304 lines (270 loc) · 11 KB
/
StnSciScenario.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
This file is part of Station Science.
Station Science is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Station Science is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Station Science. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace StationScience
{
public class StnSciContractReward : IConfigNode
{
[Persistent] public float y_intercept;
[Persistent] public float slope;
[Persistent] public float advance_multiplier;
[Persistent] public float failure_multiplier;
[Persistent] public float first_time_multiplier;
public StnSciContractReward(float y_intercept = 0, float slope = 0,
float advance_multiplier = 0, float failure_multiplier = 0, float first_time_multiplier = 1)
{
this.y_intercept = y_intercept;
this.slope = slope;
this.advance_multiplier = advance_multiplier;
this.failure_multiplier = failure_multiplier;
this.first_time_multiplier = first_time_multiplier;
}
public float calcReward(float value, bool first_time = false)
{
Debug.Log("calcReward: " + y_intercept + " + " + value + " * " + slope + " * ( " + first_time + " ? " + first_time_multiplier + ")");
return (y_intercept + value * slope) * (first_time ? first_time_multiplier : 1);
}
public float calcAdvance(float value, bool first_time = false)
{
return calcReward(value, first_time) * advance_multiplier;
}
public float calcFailure(float value, bool first_time = false)
{
return calcReward(value, first_time) * failure_multiplier;
}
public void Load(ConfigNode node)
{
}
public void Save(ConfigNode node)
{
}
}
public class CNMap<TValue> : Dictionary<string, TValue>, IConfigNode where TValue : IConvertible
{
public void Load(ConfigNode node)
{
Debug.Log("CNMap: Load called");
foreach (ConfigNode.Value val in node.values)
{
try
{
this[val.name] = (TValue)System.Convert.ChangeType(val.value, typeof(TValue));
}
catch (Exception e)
{
Debug.LogException(e);
this[val.name] = default(TValue);
}
}
}
public void Save(ConfigNode node)
{
Debug.LogError("CNMap.Save called; not implemented");
}
}
public class CNMapSet<TValue> : Dictionary<string, HashSet<TValue> > where TValue : IConvertible
{
private static readonly char[] sep = { ',', ' ', '\t', '|' };
public static HashSet<TValue> parse(string s)
{
HashSet<TValue> ret = new HashSet<TValue>();
foreach(string cur in s.Split(sep))
{
ret.Add((TValue)System.Convert.ChangeType(cur, typeof(TValue)));
}
return ret;
}
public void Load(ConfigNode node)
{
Debug.Log("CNMapList: Load called");
foreach (ConfigNode.Value val in node.values)
{
if(!this.ContainsKey(val.name))
this[val.name] = new HashSet<TValue>();
try
{
this[val.name].UnionWith(parse(val.value));
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}
public void Save(ConfigNode node)
{
Debug.LogError("CNMapList.Save called; not implemented");
}
}
public class StnSciSettings : IConfigNode
{
[Persistent] public int maxContracts = 4;
[Persistent] public float progressionFactor = 0.5f;
[Persistent] public float reputationFactor = 0.01f;
[Persistent] public float trivialMultiplier = 0.25f;
[Persistent] public float significantMultiplier = 1;
[Persistent] public float exceptionalMultiplier = 1.5f;
[Persistent] public StnSciContractReward contractScience = new StnSciContractReward(50, 10);
[Persistent] public StnSciContractReward contractFunds = new StnSciContractReward(10000, 2000, 1.0f/3.0f, 0.5f, 30);
[Persistent] public StnSciContractReward contractReputation = new StnSciContractReward(10, 1, failure_multiplier: 1.5f);
[Persistent] public StnSciContractReward contractDeadline = new StnSciContractReward(2, 0.1f, first_time_multiplier: 2);
[Persistent] public CNMap<double> experimentChallenge = new CNMap<double>() {
{ "StnSciExperiment1", 1 },
{ "StnSciExperiment2", 2 },
{ "StnSciExperiment3", 2.5 },
{ "StnSciExperiment4", 3 },
{ "StnSciExperiment5", 2.5 },
{ "StnSciExperiment6", 3.5 },
};
[Persistent] public CNMap<double> planetChallenge = new CNMap<double>() {
{ "Kerbin", 1 },
{ "Mun", 3 },
{ "Minmus", 3.25 },
{ "Duna", 6 },
{ "Ike", 6.5 },
{ "Eve", 6 },
{ "Gilly", 6.5 },
{ "Dres", 8 },
{ "Jool", 10 },
{ "Laythe", 11 },
{ "Vall", 11.5 },
{ "Tylo", 12 },
{ "Pol", 11 },
{ "Bop", 11 },
{ "Eeloo", 13 },
};
[Persistent]
public CNMapSet<string> experimentPrereqs = new CNMapSet<string>() {
{ "StnSciExperiment1", CNMapSet<string>.parse("StnSciExperiment1,StnSciLab") },
{ "StnSciExperiment2", CNMapSet<string>.parse("StnSciExperiment2,StnSciLab,StnSciCyclo") },
{ "StnSciExperiment3", CNMapSet<string>.parse("StnSciExperiment3,StnSciLab,StnSciCyclo") },
{ "StnSciExperiment4", CNMapSet<string>.parse("StnSciExperiment4,StnSciLab,StnSciCyclo") },
{ "StnSciExperiment5", CNMapSet<string>.parse("StnSciExperiment5,StnSciLab,StnSciZoo") },
{ "StnSciExperiment6", CNMapSet<string>.parse("StnSciExperiment6,StnSciLab,StnSciZoo,StnSciCyclo") },
};
public StnSciSettings()
{ }
public void Load(ConfigNode node)
{
Debug.Log("Setings: load called");
foreach (ConfigNode n in node.GetNodes("experimentChallenge"))
experimentChallenge.Load(n);
foreach (ConfigNode n in node.GetNodes("planetChallenge"))
planetChallenge.Load(n);
foreach (ConfigNode n in node.GetNodes("experimentPrereqs"))
experimentPrereqs.Load(n);
}
public void Save(ConfigNode node)
{
}
}
[KSPAddon(KSPAddon.Startup.Instantly, false)]
public class StnSciContractsUpdater : MonoBehaviour
{
static bool started = false;
internal void Awake()
{
if (started)
{
print("StnSciContractsUpdater already started");
Destroy(gameObject);
}
print("StnSciContractsUpdater started");
GameEvents.Contract.onContractsLoaded.Add(OnContractsLoaded);
started = true;
}
public void OnContractsLoaded()
{
print("Contracts Loaded");
foreach (var contract in global::Contracts.ContractSystem.Instance.Contracts)
{
if (contract is FinePrint.Contracts.BaseContract || contract is FinePrint.Contracts.StationContract)
{
foreach (var param in contract.AllParameters)
{
var pr = param as FinePrint.Contracts.Parameters.PartRequestParameter;
if (pr != null)
{
ConfigNode node = new ConfigNode("PARAM");
pr.Save(node);
print(node.ToString());
FixParam(node);
pr.Load(node);
}
}
}
}
}
public void FixParam(ConfigNode param)
{
print("PARAM " + param.GetValue("partNames"));
if (param.HasValue("partNames"))
{
string partNames = param.GetValue("partNames");
if(partNames.Contains("Large_Crewed_Lab"))
{
bool doUpdate = false;
if(!partNames.Contains("StnSciLab"))
{
partNames += ",StnSciLab";
doUpdate = true;
}
if(!partNames.Contains("StnSciZoo"))
{
partNames += ",StnSciZoo";
doUpdate = true;
}
if (doUpdate)
{
print("Updating to: partNames = " + partNames);
param.SetValue("partNames", partNames);
}
}
}
}
}
[KSPScenario(ScenarioCreationOptions.AddToAllGames, GameScenes.SPACECENTER,
GameScenes.FLIGHT, GameScenes.EDITOR, GameScenes.TRACKSTATION)]
public class StnSciScenario : ScenarioModule
{
public static StnSciScenario Instance { get; private set; }
public StnSciSettings settings { get; private set; }
[KSPField(isPersistant = true)]
public float xp = 0;
public StnSciScenario()
{
Instance = this;
if(settings == null)
{
settings = new StnSciSettings();
foreach(ConfigNode node in GameDatabase.Instance.GetConfigNodes("STN_SCI_SETTINGS"))
{
if (!ConfigNode.LoadObjectFromConfig(settings, node))
{
Debug.Log("Station Science: failed to load settings");
}
settings.Load(node);
}
}
}
void Start()
{
print("StnSciScenario started");
}
}
}