YouDoNotKnowMyName Posted February 14, 2021 Share Posted February 14, 2021 Good evening everybody! Is there a way to make a script that "gets" the total power consumption value of a settlement? For example:A settlement might have 3 generators, each producting 20 units of power.That means, the "power value" for that settlement will be 60. I am pretty sure that you can "get" that value with a script.But that is not what I mean! I mean the ammount of power that gets actually consumed (for example 10 lights in that settlement that each consum 1 power, so the consumption would be 10) What I eventually want to do is to calculate the "unused power" of a settlement. Link to comment Share on other sites More sharing options...
niston Posted February 14, 2021 Share Posted February 14, 2021 I'm using this in my EEE mod, it is operating on a specific power grid (if you have multiple, disjoint grids it will not mix them up): Bool Function Test() Text_TestType = "Power Grid Analysis" ObjectReference[] aryGenerators = SampleGenerators() ObjectReference[] aryConsumers = SampleConsumers() Int Generators = CountSharedGridNodes(aryGenerators, scrWorkshopParent.PowerGenerated, false, false) Int GeneratorsActive = CountSharedGridNodes(aryGenerators, scrWorkshopParent.PowerGenerated, true, false) Int Consumers = CountSharedGridNodes(aryConsumers, scrWorkshopParent.PowerRequired, false, false) Int ConsumersActive = CountSharedGridNodes(aryConsumers, scrWorkshopParent.PowerRequired, true, true) Int Generation = MeasureSharedGridNodes(aryGenerators, scrWorkshopParent.PowerGenerated, false, false) Int GenerationActive = MeasureSharedGridNodes(aryGenerators, scrWorkshopParent.PowerGenerated, true, false) Int Load = MeasureSharedGridNodes(aryConsumers, scrWorkshopParent.PowerRequired, false, false) Int LoadActive = MeasureSharedGridNodes(aryConsumers, scrWorkshopParent.PowerRequired, true, true) Int Reserve = Generation - Load Int ReserveActive = GenerationActive - LoadActive Text_TestResult = "Generators: " + Generators + " Connected, " + GeneratorsActive + " Active<br>Capacity: " + Generation + " Maximum, " + GenerationActive + " Actual" Text_TestResult2 = "<br>Consumers: " + Consumers + " Connected, " + ConsumersActive + " Active<br>Load: " + Load + " Maximum, " + LoadActive + " Actual<br>" Text_TestResult2 += "<br>Reserve: " + Reserve+ " Maximum, " + ReserveActive + " Actual" EndFunction ObjectReference[] Function SampleGenerators() Return FindAllReferencesWithKeyword(kwdWorkshopPowerConnection as Form, 30000 as float) EndFunction ObjectReference[] Function SampleConsumers() Return FindAllReferencesWithKeyword(kwdWorkshopCanBePowered as Form, 30000 as float) EndFunction Int Function CountSharedGridNodes(ObjectReference[] aryNodes, ActorValue requiredAV, bool requireClosedState, bool requirePower) Int i = 0 Int c = 0 While (i < aryNodes.Length) If (aryNodes[i].HasSharedPowerGrid(Self as ObjectReference)) Bool bHasAV = (aryNodes[i].GetValue(requiredAV) > 0) Bool bIsClosed = (aryNodes[i].GetOpenState() == 3) Bool bHasPower = (aryNodes[i].IsPowered()) Bool bIsValid = true If ((requiredAV != none) && (!bHasAV)) bIsValid = false EndIf If ((requireClosedState) && (!bIsClosed)) bIsValid = false EndIf If ((requirePower) && (!bHasPower)) bIsValid = false EndIf If (bIsValid) c += 1 EndIf EndIf i += 1 EndWhile Return c EndFunction Int Function MeasureSharedGridNodes(ObjectReference[] aryNodes, ActorValue measuredAV, bool requireClosedState, bool requirePower) Int i = 0 Int s = 0 While (i < aryNodes.Length) If (aryNodes[i].HasSharedPowerGrid(Self as ObjectReference)) Int v = aryNodes[i].GetValue(measuredAV) as Int Bool bIsClosed = (aryNodes[i].GetOpenState() == 3) Bool bHasPower = (aryNodes[i].IsPowered()) Bool bIsValid = true If ((requireClosedState) && (!bIsClosed)) bIsValid = false EndIf If ((requirePower) && (!bHasPower)) bIsValid = false EndIf If (bIsValid) s += v EndIf EndIf i += 1 EndWhile Return s EndFunction Link to comment Share on other sites More sharing options...
Recommended Posts