本文整理汇总了C#中Part.RequestResource方法的典型用法代码示例。如果您正苦于以下问题:C# Part.RequestResource方法的具体用法?C# Part.RequestResource怎么用?C# Part.RequestResource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Part
的用法示例。
在下文中一共展示了Part.RequestResource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Evaluate
public override EvalState Evaluate(Part part, float deltaTime, ExperimentState state)
{
bool valid;
if (!initialUpdate && initialOnly)
valid = true;
float consumption;
if (initialUpdate)
{
initialUpdate = false;
consumption = part.RequestResource(resourceName, initialConsumption);
if (state.CurrentState != ExperimentState.StateEnum.RESEARCHING)
part.RequestResource (resourceName, consumption * -1);
if (consumption.CompareTo(initialConsumption) != 0 && !validIfEmpty)
{
Debug.Log(String.Format("RealScience: Resource: initial consumption of {0:F6} != expected {1:F6}", consumption, initialConsumption));
valid = false;
}
else
valid = true;
}
else
{
consumption = part.RequestResource(resourceName, usagePerSecond * deltaTime);
if (state.CurrentState != ExperimentState.StateEnum.RESEARCHING)
part.RequestResource (resourceName, consumption * -1);
if (consumption.CompareTo(usagePerSecond * deltaTime) != 0 && !validIfEmpty)
{
Debug.Log(String.Format("RealScience: Resource: consumption of {0:F6} != expected {1:F6}", consumption, (usagePerSecond * deltaTime)));
valid = false;
}
else
valid = true;
}
if (!restriction)
{
if (valid)
return EvalState.VALID;
else
return EvalState.INVALID;
}
else
{
if (!valid)
return EvalState.VALID;
else
{
if (exclusion.ToLower() == "reset")
return EvalState.RESET;
else if (exclusion.ToLower() == "fail")
return EvalState.FAILED;
else
return EvalState.INVALID;
}
}
}
示例2: processBoiloff
public float processBoiloff(Part part, double seconds, float configMult, float fixedEffectiveness = -1)
{
double hours = seconds / 3600d;//convert from delta-seconds into delta-hours...
double resourceVolume = resource.amount * unitVolume;
double totalLoss = data.value * resourceVolume * hours * boiloffModifier * configMult;
double activePrevention = totalLoss * activeInsulationPrevention * activeInsulationPercent;
double inactivePrevention = 0f;
double activePreventionCost = activePrevention * activeECCost * data.cost;
double activePercent = 1.0f;
if (fixedEffectiveness >= 0)
{
activePercent = fixedEffectiveness;
inactivePrevention = activePrevention - (activePercent * activePrevention);
inactivePrevention *= totalLoss * inactiveInsulationPrevention;
activePrevention = activePrevention * activePercent;
activePreventionCost = activePercent * activePreventionCost;//only used XXX for this tick, though likely won't display long enough on the GUI to matter...
}
else if (activePreventionCost > 0.000005)
{
double activeECUsed = part.RequestResource("ElectricCharge", activePreventionCost);
if (activeECUsed < activePreventionCost)
{
activePercent = activeECUsed / activePreventionCost;
inactivePrevention = activePrevention - (activePercent * activePrevention);
inactivePrevention *= totalLoss * inactiveInsulationPrevention;
activePrevention = activePrevention * activePercent;
activePreventionCost = activePercent * activePreventionCost;//only used XXX for this tick, though likely won't display long enough on the GUI to matter...
}
}
double passivePrevention = totalLoss * passiveInsulationPrevention * (1.0 - activeInsulationPercent);
double totalPrevention = activePrevention + inactivePrevention + passivePrevention;
double actualLoss = totalLoss - totalPrevention;
if (actualLoss > 0.000005)
{
bool flowState = resource.flowState;
resource.flowState = true;//hack to enable using the resource even when disabled; you can't stop boiloff just by clicking the no-flow button on the UI
part.RequestResource(data.name, actualLoss / unitVolume, ResourceFlowMode.NO_FLOW);//no flow to only take resources from this part
resource.flowState = flowState;//re-hack to set resource enabled val back to previous
}
volumeLost = (float)actualLoss * (1 / (float)seconds);
ecCost = (float)activePreventionCost * ( 1 / (float)seconds);
//MonoBehaviour.print("boiloff : " + data.name);
//MonoBehaviour.print("volume : " + resourceVolume);
//MonoBehaviour.print("rawLoss : " + totalLoss);
//MonoBehaviour.print("actPrev : " + activePrevention);
//MonoBehaviour.print("inactPrev: " + inactivePrevention);
//MonoBehaviour.print("passPrev : " + passivePrevention);
//MonoBehaviour.print("actLoss : " + volumeLost);
//MonoBehaviour.print("ecCost : " + ecCost);
return (float)activePercent;
}
示例3: fixedRequestResource
public static double fixedRequestResource(Part part, string resourcename, double resource_amount)
{
List<PartResource> prl = new List<PartResource>();
List<Part> parts = new List<Part>();
part.GetConnectedResources(PartResourceLibrary.Instance.GetDefinition(resourcename).id, PartResourceLibrary.Instance.GetDefinition(resourcename).resourceFlowMode, prl);
ResourceFlowMode flow = PartResourceLibrary.Instance.GetDefinition(resourcename).resourceFlowMode;
prl = prl.Where(p => p.flowState == true).ToList();
double max_available = 0;
double spare_capacity = 0;
foreach (PartResource partresource in prl) {
parts.Add(partresource.part);
max_available += partresource.amount;
spare_capacity += partresource.maxAmount - partresource.amount;
}
if (flow == ResourceFlowMode.ALL_VESSEL) { // use our code
double resource_left_to_draw = 0;
double total_resource_change = 0;
double res_ratio = 0;
if (resource_amount > 0) {
resource_left_to_draw = Math.Min(resource_amount, max_available);
res_ratio = Math.Min(resource_amount / max_available,1);
} else {
resource_left_to_draw = Math.Max(-spare_capacity, resource_amount);
res_ratio = Math.Min(-resource_amount / spare_capacity,1);
}
if (double.IsNaN(res_ratio) || double.IsInfinity(res_ratio) || res_ratio == 0) {
return 0;
} else {
foreach (PartResource local_part_resource in prl) {
if (resource_amount > 0) {
local_part_resource.amount = local_part_resource.amount - local_part_resource.amount * res_ratio;
total_resource_change += local_part_resource.amount * res_ratio;
}else{
local_part_resource.amount = local_part_resource.amount + (local_part_resource.maxAmount - local_part_resource.amount) * res_ratio;
total_resource_change -= (local_part_resource.maxAmount - local_part_resource.amount) * res_ratio;
}
}
}
return total_resource_change;
} else {
if (resource_amount > 0) {
return part.RequestResource(resourcename, Math.Min(resource_amount, max_available));
} else {
return part.RequestResource(resourcename, Math.Max(-spare_capacity, resource_amount));
}
}
}
示例4: process
public void process(Part part, float percentage, float time)
{
double lowestInput = 1d;
double lowestOutput = 1d;
double p;
int len = inputs.Count;
for (int i = 0; i < len; i++)
{
inputs[i].updateAvailableResource(part);
p = inputs[i].getAvaiablePercent(percentage);
if (p < lowestInput) { lowestInput = p; }
}
len = outputs.Count;
for (int i = 0; i < len; i++)
{
outputs[i].updateAvailableResourceCapacity(part);
if (outputs[i].stopIfFull)
{
p = outputs[i].getAvailableCapacityPercent(percentage);
if (p < lowestOutput) { lowestOutput = p; }
}
}
float lowest = (float)(lowestInput < lowestOutput ? lowestInput : lowestOutput);
if (lowest == 0)
{
// MonoBehaviour.print ("input or output percent = 0, nothing to process (no inputs, or no room for outputs)");
return;
}
//MonoBehaviour.print ("Processing recipe with input/output percentages of: " + lowestInput + " :: " + lowestOutput+" for time: "+time+" and raw percent: "+percentage);
//loop back through inputs and outputs adding/removing resources as per the percentages listed above
lowest *= time;
len = inputs.Count;
for (int i = 0; i < len; i++)
{
part.RequestResource(inputs[i].resourceName, lowest * inputs[i].resourceAmount);
//MonoBehaviour.print("removing input of qty: "+(lowest * inputs[i].resourceAmount));
}
len = outputs.Count;
for (int i = 0; i < len; i++)
{
part.RequestResource(outputs[i].resourceName, -lowest * outputs[i].resourceAmount);
//MonoBehaviour.print("adding output of qty: "+(-lowest * outputs[i].resourceAmount));
}
}
示例5: fixedRequestResource
public static double fixedRequestResource(Part part, string resourcename, double resource_amount, ResourceFlowMode flow)
{
if (flow == ResourceFlowMode.NULL)
flow = PartResourceLibrary.Instance.GetDefinition(resourcename).resourceFlowMode;
if (flow != ResourceFlowMode.ALL_VESSEL)
return part.RequestResource(resourcename, resource_amount);
var partsWithResource = part.vessel.parts.Where(p => p.Resources.Contains(resourcename));
Dictionary<Part, ORSPropellantControl> partLookup;
if (orsPropellantDictionary.ContainsKey(part.vessel))
partLookup = orsPropellantDictionary[part.vessel];
else
{
partLookup = part.vessel.FindPartModulesImplementing<ORSPropellantControl>().ToDictionary(p => p.part);
orsPropellantDictionary.Add(part.vessel, partLookup);
}
var partResources = partsWithResource.Where(p => !partLookup.ContainsKey(p) || partLookup[p].isPropellant).Select(p => p.Resources[resourcename]);
IList<PartResource> relevant_part_resources = new List<PartResource>
(
resource_amount > 0
? partResources.Where(p => p.flowState && p.amount > 0)
: partResources.Where(p => p.flowState && p.maxAmount > p.amount)
);
if (!relevant_part_resources.Any())
return 0;
double total_resource_change = 0;
double res_ratio = resource_amount > 0
? Math.Min(resource_amount/relevant_part_resources.Sum(p => p.amount), 1)
: Math.Min(-resource_amount/relevant_part_resources.Sum(p => p.maxAmount - p.amount), 1);
if (res_ratio == 0 || double.IsNaN(res_ratio) || double.IsInfinity(res_ratio))
return 0;
foreach (PartResource local_part_resource in relevant_part_resources)
{
if (resource_amount > 0)
{
var part_resource_change = local_part_resource.amount*res_ratio;
local_part_resource.amount -= part_resource_change;
total_resource_change += part_resource_change;
}
else
{
var part_resource_change = (local_part_resource.maxAmount - local_part_resource.amount)*res_ratio;
local_part_resource.amount += part_resource_change;
total_resource_change -= part_resource_change;
}
}
return total_resource_change;
}
示例6: requestResourcePartial
public static double requestResourcePartial(Part part, string name, double amount)
{
if (amount > 0)
{
//UnityEngine.MonoBehaviour.print(name + " request: " + amount);
double taken = part.RequestResource(name, amount);
//UnityEngine.MonoBehaviour.print(name + " request taken: " + taken);
if (taken >= amount * .99999)
return taken;
double available = getAvailable(part, name);
//UnityEngine.MonoBehaviour.print(name + " request available: " + available);
double new_amount = Math.Min(amount, available) * .99999;
//UnityEngine.MonoBehaviour.print(name + " request new_amount: " + new_amount);
if (new_amount > taken)
return taken + part.RequestResource(name, new_amount - taken);
else
return taken;
}
else if (amount < 0)
{
//UnityEngine.MonoBehaviour.print(name + " request: " + amount);
double taken = part.RequestResource(name, amount);
//UnityEngine.MonoBehaviour.print(name+" request taken: " + taken);
if (taken <= amount * .99999)
return taken;
double available = getDemand(part, name);
//UnityEngine.MonoBehaviour.print(name + " request available: " + available);
double new_amount = Math.Max(amount, available) * .99999;
//UnityEngine.MonoBehaviour.print(name + " request new_amount: " + new_amount);
if (new_amount < taken)
return taken + part.RequestResource(name, new_amount - taken);
else
return taken;
}
else
return 0;
}
示例7: TakeResource_StackPriority
private static double TakeResource_StackPriority(Part part, PartResourceDefinition resource, double demand)
{
// FIXME finish implementing
return part.RequestResource(resource.id, demand);
}
示例8: ProcessElectricity
internal void ProcessElectricity(Part part, float time)
{
if (Mode == Modes.OFF) return;
var electricReq = 0.05f * time;
var result = part.RequestResource("ElectricCharge", electricReq) / electricReq;
var newMode = (result < 0.5f) ? Modes.STARVED : Modes.READY;
if (newMode == Modes.READY && Mode == Modes.STARVED)
{
Boot();
}
Mode = newMode;
}
示例9: ProcessElectricity
public void ProcessElectricity(Part part, float time)
{
if (Mode == CPUMode.OFF) return;
var electricReq = 0.01f*time;
var result = part.RequestResource("ElectricCharge", electricReq)/electricReq;
var newMode = (result < 0.5f) ? CPUMode.STARVED : CPUMode.READY;
if (newMode == CPUMode.READY && Mode == CPUMode.STARVED)
{
Boot();
}
Mode = newMode;
}
示例10: ProcessElectricity
private void ProcessElectricity(Part partObj, float time)
{
if (ProcessorMode == ProcessorModes.OFF) return;
double volumePower = 0;
if (shared.VolumeMgr.CheckCurrentVolumeRange())
{
// If the current volume is in range, check the capacity and calculate power
var volume = shared.VolumeMgr.CurrentVolume;
if (volume.Name == "Archive")
{
volumePower = ARCHIVE_EFFECTIVE_BYTES * ECPerBytePerSecond;
}
else
{
volumePower = volume.Capacity * ECPerBytePerSecond;
}
}
else
{
// if the volume isn't in range, assume it doesn't consume any power
volumePower = 0;
}
if (ProcessorMode == ProcessorModes.STARVED)
{
// If the processor is STARVED, check to see if there is enough EC to turn it back on.
var request = averagePower.Mean; // use the average power draw as a baseline of the power needed to restart.
if (request > 0)
{
var available = partObj.RequestResource("ElectricCharge", request);
if (available / request > 0.5)
{
SetMode(ProcessorModes.READY);
}
// Since we're just checking to see if there is enough power to restart, return
// the consumed EC. The actual demand value will be drawn on the next update after
// the cpu boots. This should give the ship a chance to collect a little more EC
// before the cpu actually boots.
partObj.RequestResource("ElectricCharge", -available);
}
else
{
// If there is no historical power request, simply turn the processor back on. This
// should not be possible, since it means that some how the processor got set to
// the STARVED mode, even though no power was requested.
SetMode(ProcessorModes.READY);
}
RequiredPower = (float)request; // Make sure RequiredPower matches the average.
}
else
{
// Because the processor is not STARVED, evaluate the power requirement based on actual operation.
// For EC drain purposes, always pretend atleast 1 instruction happened, so idle drain isn't quite zero:
int instructions = System.Math.Max(shared.Cpu.InstructionsThisUpdate, 1);
var request = volumePower * time + instructions * ECPerInstruction;
if (request > 0)
{
// only check the available EC if the request is greater than 0EC. If the request value
// is zero, then available will always be zero and it appears that mono/.net treat
// "0 / 0" as equaling "0", which prevents us from checking the ratio. Since getting
// "0" available of "0" requested is a valid state, the processor mode is only evaluated
// if request is greater than zero.
var available = partObj.RequestResource("ElectricCharge", request);
if (available / request < 0.5)
{
// 0.5 is an arbitrary ratio for triggering the STARVED mode. It allows for some
// fluctuation away from the exact requested EC, ando adds some fuzzy math to how
// we deal with the descreet physics frames. Essentially if there was enough power
// to run for half of a physics frame, the processor stays on.
SetMode(ProcessorModes.STARVED);
}
}
// Set RequiredPower to the average requested power. This should help "de-bounce" the value
// so that it doesn't fluctuate wildly (between 0.2 and 0.000001 in a single frame for example)
RequiredPower = (float)averagePower.Update(request) / TimeWarp.fixedDeltaTime;
}
}
示例11: ProcessElectricity
private void ProcessElectricity(Part partObj, float time)
{
if (ProcessorMode == ProcessorModes.OFF) return;
RequiredPower = shared.VolumeMgr.CurrentRequiredPower;
var electricReq = time * RequiredPower;
var result = partObj.RequestResource("ElectricCharge", electricReq) / electricReq;
var newMode = (result < 0.5f) ? ProcessorModes.STARVED : ProcessorModes.READY;
SetMode(newMode);
}
示例12: fixedRequestResource
public static double fixedRequestResource(Part part, string resourcename, double resource_amount, ResourceFlowMode flow)
{
if (flow == ResourceFlowMode.NULL)
flow = PartResourceLibrary.Instance.GetDefinition(resourcename).resourceFlowMode;
if (flow == ResourceFlowMode.ALL_VESSEL)
{ // use our code
var partsWithResource = part.vessel.parts.Where(p => p.Resources.Contains(resourcename));
Dictionary<Part, ORSPropellantControl> partLookup;
if (orsPropellantDictionary.ContainsKey(part.vessel))
partLookup = orsPropellantDictionary[part.vessel];
else
{
partLookup = part.vessel.FindPartModulesImplementing<ORSPropellantControl>().ToDictionary(p => p.part);
orsPropellantDictionary.Add(part.vessel, partLookup);
}
var partResources = partsWithResource.Where(p => !partLookup.ContainsKey(p) || ((ORSPropellantControl)partLookup[p]).isPropellant).Select(p => p.Resources[resourcename]);
var prl = partResources.Where(p => p.flowState == true).ToList();
double max_available = 0;
double spare_capacity = 0;
foreach (PartResource partresource in prl)
{
max_available += partresource.amount;
spare_capacity += partresource.maxAmount - partresource.amount;
}
double resource_left_to_draw = 0;
double total_resource_change = 0;
double res_ratio = 0;
if (resource_amount > 0)
{
resource_left_to_draw = Math.Min(resource_amount, max_available);
res_ratio = Math.Min(resource_amount / max_available,1);
}
else
{
resource_left_to_draw = Math.Max(-spare_capacity, resource_amount);
res_ratio = Math.Min(-resource_amount / spare_capacity,1);
}
if (double.IsNaN(res_ratio) || double.IsInfinity(res_ratio) || res_ratio == 0)
{
return 0;
}
else
{
foreach (PartResource local_part_resource in prl)
{
if (resource_amount > 0)
{
local_part_resource.amount = local_part_resource.amount - local_part_resource.amount * res_ratio;
total_resource_change += local_part_resource.amount * res_ratio;
}
else
{
local_part_resource.amount = local_part_resource.amount + (local_part_resource.maxAmount - local_part_resource.amount) * res_ratio;
total_resource_change -= (local_part_resource.maxAmount - local_part_resource.amount) * res_ratio;
}
}
}
return total_resource_change;
}
else
{
if (resource_amount > 0)
//return part.RequestResource(resourcename, Math.Min(resource_amount, max_available));
return part.RequestResource(resourcename, resource_amount);
else
//return part.RequestResource(resourcename, Math.Max(-spare_capacity, resource_amount));
return part.RequestResource(resourcename,resource_amount);
}
}
示例13: RequestPower
public static bool RequestPower(Part prt, float power)
{
if (TimeWarp.deltaTime != 0)
{
float amount = prt.RequestResource("ElectricCharge", power * TimeWarp.deltaTime);
return amount != 0;
}
else
{
return true;
}
}
示例14: EmptyEvaSuit
protected void EmptyEvaSuit(Part evaPart, Part container)
{
this.Log("Emptying the EVA suit from " + evaPart.name + " to " + container.name);
// Compute how much can be left in the container
double capacity = container.Resources[Spares.Name].maxAmount - container.Resources[Spares.Name].amount;
double deposit = Math.Min(evaPart.Resources[Spares.Name].amount, capacity);
// Add it to the spares container and drain it from the EVA part
container.RequestResource(Spares.Name, -deposit);
// Once again, MC2 breaks the RequestResource on evaPart, but with the above checks, decrementing should work just fine instead, I think! -TrypChangeling
//evaPart.RequestResource(Spares.Name, deposit);
evaPart.Resources[Spares.Name].amount -= deposit;
// GUI acknowledge
try
{
DangIt.Broadcast(evaPart.protoModuleCrew[0].name + " has left " + deposit + " spares", false, 1f);
}
catch (Exception) // The kerbal reenters before this method is called: in that case, trying to get his name will throw an exception
{
DangIt.Broadcast("You left " + deposit + " spares", false, 1f);
}
ResourceDisplay.Instance.Refresh();
}
示例15: FillEvaSuit
protected void FillEvaSuit(Part evaPart, Part container)
{
// Check if the EVA part contains the spare parts resource: if not, add a new config node
if (!evaPart.Resources.Contains(Spares.Name))
{
this.Log("The eva part doesn't contain spares, adding the config node");
ConfigNode node = new ConfigNode("RESOURCE");
node.AddValue("name", Spares.Name);
node.AddValue("maxAmount", Spares.MaxEvaAmount);
node.AddValue("amount", 0);
evaPart.Resources.Add(node);
}
// Override maxAmount set by other mods (such as MC2) causing taking of parts to fail -TrypChangeling
if (evaPart.Resources[Spares.Name].maxAmount < Spares.MaxEvaAmount)
{
evaPart.Resources[Spares.Name].maxAmount = Spares.MaxEvaAmount;
}
// Compute how much the kerbal can take
double desired = Spares.MaxEvaAmount - evaPart.Resources[Spares.Name].amount;
desired = Math.Min(desired, Spares.MinIncrement);
double amountTaken = Math.Min(desired, container.Resources[Spares.Name].amount);
// Take it from the container and add it to the EVA
container.RequestResource(Spares.Name, amountTaken);
// RequestResource is being overridden by MC2 for some reason - however, with above checks, simply incrementing the value should work... I think! - TrypChangeling
// evaPart.RequestResource(Spares.Name, -amountTaken);
evaPart.Resources[Spares.Name].amount += amountTaken;
// GUI stuff
DangIt.Broadcast(evaPart.vessel.GetVesselCrew().First().name + " has taken " + amountTaken + " spares", false, 1f);
ResourceDisplay.Instance.Refresh();
}