Skip to content

Commit

Permalink
Tooling now uses unlock credit
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanKell committed Jul 2, 2023
1 parent 107e94e commit 4b2ef26
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
3 changes: 1 addition & 2 deletions Source/Harmony/PartListTooltip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ private static void PatchButtons(PartListTooltip __instance, AvailablePart avail
}
else if (__instance.buttonPurchase.gameObject.activeSelf || __instance.buttonPurchaseRed.gameObject.activeSelf)
{
var cmq = CurrencyModifierQueryRP0.RunQuery(TransactionReasonsRP0.PartOrUpgradeUnlock, -eCost, 0d, 0d);
cmq.AddDeltaAuthorized(CurrencyRP0.Funds, Math.Min(-cmq.GetTotal(CurrencyRP0.Funds), UnlockCreditHandler.Instance.GetCreditAmount(techID)));
var cmq = UnlockCreditHandler.Instance.GetCMQ(-eCost, techID, TransactionReasonsRP0.PartOrUpgradeUnlock);
// If we still can't afford, bail without setting tooltip
if (!cmq.CanAfford())
return;
Expand Down
11 changes: 7 additions & 4 deletions Source/Tooling/ModuleTooling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ public virtual void ToolingEvent()

bool bypass = HighLogic.CurrentGame.Parameters.Difficulty.BypassEntryPurchaseAfterResearch;
float toolingCost = bypass ? 0f : GetToolingCost();
bool canAfford = CurrencyModifierQuery.RunQuery(TransactionReasonsRP0.ToolingPurchase.Stock(), -toolingCost, 0f, 0f).CanAfford();
var cmq = UnlockCreditHandler.Instance.GetPrePostCostAndAffordability(toolingCost, string.Empty, TransactionReasonsRP0.ToolingPurchase, out double preCost, out double postCost, out double credit, out bool canAfford);
string costline = cmq.GetCostLineOverride(true, false, true, true);
if (string.IsNullOrEmpty(costline))
costline = "nothing";

PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f),
new MultiOptionDialog(
"ConfirmToolingPurchase",
$"Tooling has not yet been set up for this part. It will cost { toolingCost:N0} funds.",
$"Tooling has not yet been set up for this part. It will cost {costline} (spending {credit:N0} credit).",
"Tooling Purchase",
HighLogic.UISkin,
new Rect(0.5f, 0.5f, 150f, 60f),
Expand All @@ -71,7 +74,7 @@ public virtual void ToolingEvent()
{
using (new CareerEventScope(CareerEventType.Tooling))
{
Funding.Instance.AddFunds(-toolingCost, TransactionReasonsRP0.ToolingPurchase.Stock());
UnlockCreditHandler.Instance.ProcessCredit(toolingCost, string.Empty, TransactionReasonsRP0.ToolingPurchase);
}
PurchaseTooling();
GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship);
Expand Down Expand Up @@ -229,7 +232,7 @@ public static float PurchaseToolingBatch(List<ModuleTooling> toolingColl, bool i
{
using (new CareerEventScope(CareerEventType.Tooling))
{
Funding.Instance.AddFunds(-totalCost, TransactionReasonsRP0.ToolingPurchase.Stock());
UnlockCreditHandler.Instance.ProcessCredit(totalCost, string.Empty, TransactionReasonsRP0.ToolingPurchase);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions Source/Tooling/ToolingGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ private static void RenderToolAllButton()
if (GUILayout.Button("Tool All", HighLogic.Skin.button))
{
GetUntooledPartsAndCost(out List<ModuleTooling> untooledParts, out float toolingCost);
var cmq = CurrencyModifierQueryRP0.RunQuery(TransactionReasonsRP0.ToolingPurchase, -toolingCost, 0d, 0d);
bool canAfford = cmq.CanAfford();
var cmq = UnlockCreditHandler.Instance.GetPrePostCostAndAffordability(toolingCost, string.Empty, TransactionReasonsRP0.ToolingPurchase, out double preCost, out double postCost, out double credit, out bool canAfford);
string buttonText = canAfford ? "Purchase All Toolings" : "Can't Afford";
string costline = cmq.GetCostLineOverride(true, false, true, true);
if (string.IsNullOrEmpty(costline))
costline = "nothing";

var dialog = new MultiOptionDialog(
"ConfirmAllToolingsPurchase",
$"Tooling for all untooled parts will cost {-cmq.GetTotal(CurrencyRP0.Funds):N0} funds.",
$"Tooling for all untooled parts will cost {costline} (spending {credit:N0} credit).",
"Tooling Purchase",
HighLogic.UISkin,
new Rect(0.5f, 0.5f, 150f, 60f),
Expand All @@ -167,7 +169,7 @@ private static void ToolAll()
{
GetUntooledPartsAndCost(out List<ModuleTooling> untooledParts, out float toolingCost);

bool canAfford = CurrencyModifierQueryRP0.RunQuery(TransactionReasonsRP0.ToolingPurchase, -toolingCost, 0d, 0d).CanAfford();
UnlockCreditHandler.Instance.GetPrePostCostAndAffordability(toolingCost, string.Empty, TransactionReasonsRP0.ToolingPurchase, out _, out _, out _, out bool canAfford);
if (canAfford)
{
ModuleTooling.PurchaseToolingBatch(untooledParts);
Expand Down
27 changes: 27 additions & 0 deletions Source/UnlockCreditHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ public double SpendCredit(double cost)
return excessCost;
}

public CurrencyModifierQueryRP0 GetCMQ(double cost, string tech, TransactionReasonsRP0 reason)
{
var cmq = CurrencyModifierQueryRP0.RunQuery(reason, -cost, 0d, 0d);
cmq.AddDeltaAuthorized(CurrencyRP0.Funds, Math.Min(-cmq.GetTotal(CurrencyRP0.Funds), GetCreditAmount(tech)));
return cmq;
}

public CurrencyModifierQueryRP0 GetPrePostCostAndAffordability(double cost, string tech, TransactionReasonsRP0 reason, out double preCreditCost, out double postCreditCost, out double credit, out bool canAfford)
{
var cmq = CurrencyModifierQueryRP0.RunQuery(reason, -cost, 0d, 0d);
preCreditCost = -cmq.GetTotal(CurrencyRP0.Funds);
credit = Math.Min(preCreditCost, GetCreditAmount(tech));
cmq.AddDeltaAuthorized(CurrencyRP0.Funds, credit);
postCreditCost = -cmq.GetTotal(CurrencyRP0.Funds);
canAfford = cmq.CanAfford();

return cmq;
}

public void ProcessCredit(double cost, string tech, TransactionReasonsRP0 reason)
{
var cmq = CurrencyModifierQueryRP0.RunQuery(reason, -cost, 0d, 0d);
double postCMQCost = -cmq.GetTotal(CurrencyRP0.Funds);
double remainingCost = SpendCredit(postCMQCost);
Funding.Instance.AddFunds(-(float)(remainingCost * (cost / postCMQCost)), reason.Stock());
}

/// <summary>
/// Transforms entrycost to post-strategy entrycost, spends credit,
/// and returns remaining (unsubsidized) cost
Expand Down

0 comments on commit 4b2ef26

Please sign in to comment.