/// <summary> /// Refuels kerbal's EVA pack up to the maximum, and decreases canister reserve. /// </summary> /// <param name="item">Item to get fuel from.</param> protected virtual void RefillEVAPack(KIS_Item item) { var canisterFuelResource = GetCanisterFuelResource(item); var evaFuelResource = item.inventory.part.Resources.Get( item.inventory.part.GetComponent <KerbalEVA>().propellantResourceName); var needsFuel = evaFuelResource.maxAmount - evaFuelResource.amount; if (needsFuel < double.Epsilon) { ScreenMessaging.ShowPriorityScreenMessage(NoNeedToRefillMsg); } else { if (canisterFuelResource.amount < double.Epsilon) { ScreenMessaging.ShowPriorityScreenMessage(CanisterIsEmptyMsg); UISounds.PlayBipWrong(); } else { var canRefuel = Math.Min(needsFuel, canisterFuelResource.amount); item.UpdateResource(StockResourceNames.EvaPropellant, canisterFuelResource.amount - canRefuel); evaFuelResource.amount += canRefuel; if (canRefuel < needsFuel) { ScreenMessaging.ShowPriorityScreenMessage(NotEnoughPropellantMsg); } else { ScreenMessaging.ShowPriorityScreenMessage(JetpackRefueledMsg); } UISoundPlayer.instance.Play(refuelSndPath); } } }
/// <summary> /// Refuels kerbal's EVA pack up to the maximum, and decreases canister reserve. /// </summary> /// <param name="item">Item to get fuel from.</param> void RefillEvaPack(KIS_Item item) { var evaFuelResource = item.inventory.part.Resources.Get(StockResourceNames.EvaPropellant); var needsFuel = evaFuelResource.maxAmount - evaFuelResource.amount; if (needsFuel < double.Epsilon) { ScreenMessaging.ShowPriorityScreenMessage(NoNeedToRefillJetpackMsg); return; } var canisterFuelResource = GetCanisterFuelResource(item); if (canisterFuelResource.amount < double.Epsilon) { ScreenMessaging.ShowPriorityScreenMessage(CanisterIsEmptyMsg); UISounds.PlayBipWrong(); return; } var canRefuel = Math.Min(needsFuel, canisterFuelResource.amount); item.UpdateResource(_mainResourceName, -canRefuel, isAmountRelative: true); evaFuelResource.amount += canRefuel; if (canRefuel < needsFuel) { ScreenMessaging.ShowPriorityScreenMessage(JetpackPartiallyRefueledMsg.Format(canRefuel)); } else { ScreenMessaging.ShowPriorityScreenMessage(JetpackFullyRefueledMsg); } UISoundPlayer.instance.Play(refuelSndPath); }
/// <summary>Fills up canister to the maximum capacity.</summary> /// <param name="item">Item to refill.</param> void RefillCanister(KIS_Item item) { var canisterResource = GetCanisterFuelResource(item); var needResource = canisterResource.maxAmount - canisterResource.amount; if (needResource <= double.Epsilon) { ScreenMessaging.ShowPriorityScreenMessage(NoNeedToRefillCanisterMsg); return; } double newAmount; if (_mainResourceName != StockResourceNames.EvaPropellant) { var hasAmount = item.inventory.part.RequestResource(_mainResourceName, needResource); if (hasAmount <= double.Epsilon) { ScreenMessaging.ShowPriorityScreenMessage( NoResourceInVesselMsg.Format(canisterResource.resourceName)); UISounds.PlayBipWrong(); return; } newAmount = canisterResource.amount + hasAmount; ScreenMessaging.ShowPriorityScreenMessage( CanisterPartialRefilledMsg.Format(canisterResource.resourceName, hasAmount)); } else { newAmount = canisterResource.maxAmount; ScreenMessaging.ShowPriorityScreenMessage(CanisterFullyRefilledMsg); } item.UpdateResource(_mainResourceName, newAmount); UISoundPlayer.instance.Play(refuelSndPath); }
/// <summary>Fills up canister to the maximum capacity.</summary> /// <param name="item">Item to refill.</param> protected virtual void RefillCanister(KIS_Item item) { item.UpdateResource(StockResourceNames.EvaPropellant, GetCanisterFuelResource(item).maxAmount); ScreenMessaging.ShowPriorityScreenMessage(CanisterRefilledMsg); UISoundPlayer.instance.Play(refuelSndPath); }
public void UpdateResource(string name, double amount, bool isAmountRelative = false) { _kis_item.UpdateResource(name, amount, isAmountRelative); }
/// <summary> /// Refuels kerbal's EVA pack up to the maximum, and decreases canister reserve. /// </summary> /// <param name="item">Item to get fuel from.</param> void RefillEvaPack(KIS_Item item) { var p = item.inventory.part; if (!p.isVesselEVA) { HostedDebugLog.Error(this, "Cannot refill non-EVA kerbal"); return; } var stockInventory = p.FindModuleImplementing <ModuleInventoryPart>(); if (stockInventory == null) { HostedDebugLog.Error(this, "Cannot find stock inventory module"); return; } var evaModule = p.FindModuleImplementing <KerbalEVA>(); var propellantResourceField = evaModule.GetType() .GetField("propellantResource", BindingFlags.Instance | BindingFlags.NonPublic); if (propellantResourceField == null) { HostedDebugLog.Error(this, "Cannot access internal KerbalEVA logic: propellant field"); return; } var propellantResource = propellantResourceField.GetValue(evaModule) as PartResource; if (propellantResource == null) { HostedDebugLog.Error(this, "Cannot access internal KerbalEVA logic: propellant field value"); return; } var needAmount = propellantResource.maxAmount - propellantResource.amount; if (needAmount <= double.Epsilon) { ScreenMessaging.ShowPriorityScreenMessage(NoNeedToRefillJetpackMsg); return; } var canisterFuelResource = GetCanisterFuelResource(item); if (canisterFuelResource.amount < double.Epsilon) { ScreenMessaging.ShowPriorityScreenMessage(CanisterIsEmptyMsg); UISounds.PlayBipWrong(); return; } var canProvide = Math.Min(needAmount, canisterFuelResource.amount); var storedResources = stockInventory.storedParts.Values.SelectMany(x => x.snapshot.resources) .Where(x => x.resourceName == StockResourceNames.EvaPropellant) .ToArray(); if (storedResources.Length == 0) { UISounds.PlayBipWrong(); DebugEx.Error("Unexpectedly no EVA resource parts found in: {0}", evaModule); return; } item.UpdateResource(_mainResourceName, -canProvide, isAmountRelative: true); p.TransferResource(propellantResource, canProvide, p); var distributeAmount = canProvide; for (var i = 0; i < storedResources.Length && canProvide > double.Epsilon; i++) { var resource = storedResources[i]; var canAccept = resource.maxAmount - resource.amount; if (canAccept <= double.Epsilon) { continue; } var refillAmount = Math.Min(canAccept, distributeAmount); resource.amount += refillAmount; resource.UpdateConfigNodeAmounts(); distributeAmount -= refillAmount; } if (canProvide < needAmount) { ScreenMessaging.ShowPriorityScreenMessage(JetpackPartiallyRefueledMsg.Format(canProvide)); } else { ScreenMessaging.ShowPriorityScreenMessage(JetpackFullyRefueledMsg); } UISoundPlayer.instance.Play(refuelSndPath); }