-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolarPanelMachine.cs
148 lines (134 loc) · 4.2 KB
/
SolarPanelMachine.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
using System.Collections.Generic;
using System.Linq;
using FortressCraft.ModFoundation;
using FortressCraft.ModFoundation.Block;
using FortressCraft.ModFoundation.Multiblock;
using static FortressCraft.ModFoundation.Util;
namespace GeniusIsme
{
public class SolarPanelMachine<G> : OverloadedMachine<G>
where G : MachineEntity
{
public SolarPanelMachine(
ModCreateSegmentEntityParameters parameters,
Position size,
G graphics,
float dayRate,
float nightRate)
: base(parameters, graphics)
{
this.Surveyor = new BlockSurveyor(this);
var center = new Position(parameters);
var shift = size / 2;
this.Box = new GridBox(new Box(center - shift, center + shift));
this.Clearances = this.Box.Side(Direction.Up()).Select(b => new Clearance(b)).ToList();
var difficultyFactor = 1.0f;
if (DifficultySettings.mbEasyPower)
difficultyFactor = 2.5f;
if (DifficultySettings.mbRushMode)
difficultyFactor = 3.5f;
this.DayRate = dayRate * difficultyFactor;
this.NightRate = nightRate * difficultyFactor;
var down = Direction.Up().Negate();
var probes = this.Box.Side(down)
.Select((pos) => new PciSurveyor.Probe(pos, down));
this.PciSurveyor = new PciSurveyor(probes, this);
}
public override void Update(float timeDelta)
{
var clearance = this.CheckClearance();
var power = this.GeneratePower(clearance, timeDelta);
this.DistributePower(power, timeDelta);
}
public override bool ShouldSave()
{
return false;
}
float CheckClearance()
{
if (new Position(this).Y < MinHeight)
{
return 0f;
}
var clearNum = this.Clearances
.Where(c => { c.Update(this.Surveyor); return c.Clear; })
.Count();
return (float)clearNum / (float)this.Clearances.Count();
}
float GeneratePower(float clearance, float timeDelta)
{
var angle = SurvivalWeatherManager.mrSunAngle;
if (angle > 0)
{
this.Generation = this.DayRate * angle * clearance;
}
else
{
this.Generation = -this.NightRate * angle * clearance;
}
return this.Generation * timeDelta;
}
void DistributePower(float power, float timeDelta)
{
foreach(var consumer in this.PciSurveyor.Survey())
{
var amount = new [] {
power,
consumer.GetRemainingPowerCapacity(),
consumer.GetMaximumDeliveryRate() * ToTicks(timeDelta),
}.Min();
if (consumer.DeliverPower(amount))
{
power -= amount;
}
}
}
public override string GetPopupText()
{
return "Collecting light at rate of " + (int) this.Generation + "pps" ;
}
BlockSurveyor Surveyor;
PciSurveyor PciSurveyor;
List<Clearance> Clearances;
GridBox Box;
float DayRate = 1f;
float NightRate = 0.1f;
float Generation = 0f;
private static readonly long MinHeight = 4611686017890516928L;
}
class Clearance
{
public Clearance(Position StartBlock)
{
this.StartBlock = StartBlock;
this.LastCheckedPosition = 0;
this.Clear = false;
}
public void Update(BlockSurveyor Surveyor)
{
var from = this.StartBlock + Direction.Up().Shift * this.LastCheckedPosition;
var to = from + Direction.Up().Shift * CheckChunk;
var clear = new GridBox(new Box(from, to)).Blocks()
.Select(b => CubeHelper.IsCubeSolid(Surveyor.Look().At(b).Block()?.Type ?? 1))
.NoneTrue();
if (clear)
{
this.LastCheckedPosition = (this.LastCheckedPosition + CheckChunk) % ClearHeight;
if (this.LastCheckedPosition == 0)
{
this.Clear = true;
}
}
else
{
this.Clear = false;
this.LastCheckedPosition = 0;
}
}
Position StartBlock;
int LastCheckedPosition;
public bool Clear { get; private set; }
static readonly int CheckChunk = 8;
static readonly int ClearHeight = 128;
}
}