-
Notifications
You must be signed in to change notification settings - Fork 2
/
AnimatedResourceConsumer.cs
83 lines (73 loc) · 2.41 KB
/
AnimatedResourceConsumer.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
// HangarResourceUser.cs
//
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2015 Allis Tauri
using System;
using System.Linq;
using System.Collections.Generic;
using AT_Utils;
namespace AT_Utils
{
public abstract class AnimatedResourceConsumer : AnimatedConverterBase, IResourceConsumer
{
[KSPField] public string InputResources = string.Empty;
protected List<ResourceLine> input;
public List<PartResourceDefinition> GetConsumedResources()
{ return input.Select(i => i.Resource).ToList(); }
public override string GetInfo()
{
setup_resources();
var info = base.GetInfo();
if(input != null && input.Count > 0)
{
info += "Inputs:\n";
info += input.Aggregate("", (s, r) => s+"- "+r.Info+'\n');
}
return info;
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
//check energy consumption; even generators consume energy
if(EnergyConsumption <= 0)
EnergyConsumption = 0.01f;
}
protected virtual void setup_resources()
{
//parse input/output resources
input = ResourceLine.ParseResourcesToList(InputResources);
if(input == null)
{
this.ConfigurationInvalid("unable to initialize INPUT resources");
return;
}
input.ForEach(r => r.InitializePump(part, RatesMultiplier));
}
public override void OnStart(StartState state)
{
base.OnStart(state);
setup_resources();
}
public override void SetRatesMultiplier(float mult)
{
base.SetRatesMultiplier(mult);
setup_resources();
}
protected static string try_transfer(List<ResourceLine> resources, float rate = 1f, bool skip_failed = false)
{
string failed = "";
foreach(var r in resources)
{
if(r.TransferResource(rate) && r.PartialTransfer)
{
if(skip_failed)
failed += (failed == ""? "" : ", ") + Utils.ParseCamelCase(r.Name);
else return Utils.ParseCamelCase(r.Name);
}
}
return failed;
}
}
}