Skip to content

Commit

Permalink
Events Rework
Browse files Browse the repository at this point in the history
  • Loading branch information
severedsolo committed Jan 21, 2020
1 parent e3521f2 commit c1e4ff6
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 77 deletions.
2 changes: 2 additions & 0 deletions .idea/.idea.Bureaucracy/.idea/contentModel.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 71 additions & 49 deletions .idea/.idea.Bureaucracy/.idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Bureaucracy/Budget/Costs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public double GetFacilityMaintenanceCosts()
{
BureaucracyFacility bf = FacilityManager.Instance.Facilities.ElementAt(i);
if(bf.IsClosed) continue;
d += bf.MaintenanceCost*Bureaucracy.Instance.qaModifier;
d += bf.MaintenanceCost*FacilityManager.Instance.CostMultiplier;
}
return d;
}
Expand Down
3 changes: 0 additions & 3 deletions Bureaucracy/Bureaucracy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class Bureaucracy : MonoBehaviour
{
public SettingsClass settings;
public static Bureaucracy Instance;
public float qaModifier = 1.0f;
public List<Manager> registeredManagers = new List<Manager>();

private void Awake()
Expand Down Expand Up @@ -90,7 +89,6 @@ public void OnLoad(ConfigNode node)
{
//ScenarioModule OnLoad event redirects here, each class handles it's own saving/loading (for better encapsulation).
Debug.Log("[Bureaucracy]: OnLoad");
if(float.TryParse(node.GetValue("QAModifier"), out float f))qaModifier = f;
SettingsClass.Instance.InGameLoad();
BudgetManager.Instance.OnLoad(node);
FacilityManager.Instance.OnLoad(node);
Expand All @@ -105,7 +103,6 @@ public void OnSave(ConfigNode node)
{
//ScenarioModule OnLoad event redirects here, each class handles it's own saving/loading (for better encapsulation).
Debug.Log("[Bureaucracy]: OnSave");
node.SetValue("QAModifier", qaModifier, true);
SettingsClass.Instance.InGameSave();
BudgetManager.Instance.OnSave(node);
FacilityManager.Instance.OnSave(node);
Expand Down
2 changes: 2 additions & 0 deletions Bureaucracy/Bureaucracy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RandomEvents\CurrencyEvent.cs" />
<Compile Include="RandomEvents\ExplosionEvent.cs" />
<Compile Include="RandomEvents\FireEvent.cs" />
<Compile Include="RandomEvents\QAEvent.cs" />
<Compile Include="RandomEvents\RandomEventBase.cs" />
<Compile Include="RandomEvents\RandomEventLoader.cs" />
<Compile Include="RandomEvents\TrainingEvent.cs" />
<Compile Include="RandomEvents\WageEvent.cs" />
<Compile Include="Report.cs" />
<Compile Include="Science\ResearchManager.cs" />
<Compile Include="Science\ScienceEvent.cs" />
Expand Down
3 changes: 2 additions & 1 deletion Bureaucracy/Crew/CrewMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class CrewMember
public readonly int MaxStrikes;
public readonly List<CrewUnhappiness> UnhappinessEvents = new List<CrewUnhappiness>();
public bool Unhappy;
public float WageModifier = 1.0f;

public string Name { get; private set; }

Expand All @@ -21,7 +22,7 @@ public double Wage
float experienceLevel = crewRef.experienceLevel;
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (experienceLevel == 0) experienceLevel = 0.5f;
return experienceLevel * SettingsClass.Instance.KerbalBaseWage;
return experienceLevel * SettingsClass.Instance.KerbalBaseWage*WageModifier;
}
}

Expand Down
41 changes: 31 additions & 10 deletions Bureaucracy/Facilities/BureaucracyFacility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ namespace Bureaucracy
{
public class BureaucracyFacility
{
private int level = 1;
private int level;
private readonly int upkeepCost;
private bool recentlyUpgraded;
public FacilityUpgradeEvent Upgrade;
private bool isClosed;
public bool IsPriority;
public int LaunchesThisMonth;

public int Level => level;

public bool IsClosed => isClosed;

private bool CanBeClosed { get; set; }
Expand Down Expand Up @@ -45,11 +47,11 @@ public BureaucracyFacility(SpaceCenterFacility spf)
{
Name = SetName(spf);
upkeepCost = SetCosts();
level = GetUpgradeableLevel();
level = GetFacilityLevel();
Debug.Log("[Bureaucracy]: Setup Facility " + Name);
}

private int GetUpgradeableLevel()
private int GetFacilityLevel()
{
foreach (KeyValuePair<string, ScenarioUpgradeableFacilities.ProtoUpgradeable> config in ScenarioUpgradeableFacilities.protoUpgradeables)
{
Expand Down Expand Up @@ -192,19 +194,38 @@ public void OnUpgradeCompleted()

public bool IsDestroyed()
{
foreach (KeyValuePair<string, ScenarioDestructibles.ProtoDestructible> kvp in ScenarioDestructibles.protoDestructibles)
foreach (DestructibleBuilding building in Destructibles())
{
if (!kvp.Key.Contains(Name)) continue;
List<DestructibleBuilding> buildingsToDestroy = kvp.Value.dBuildingRefs;
foreach (DestructibleBuilding building in buildingsToDestroy)
{
if (building.IsDestroyed) return true;
}
if (building.IsDestroyed) return true;
}

return false;
}

public void DestroyBuilding()
{
if (!CanBeDestroyed()) return;
foreach (DestructibleBuilding building in Destructibles())
{
building.Demolish();
}
}

public bool CanBeDestroyed()
{
return level > 2;
}

private List<DestructibleBuilding> Destructibles()
{
foreach (KeyValuePair<string, ScenarioDestructibles.ProtoDestructible> kvp in ScenarioDestructibles.protoDestructibles)
{
if (!kvp.Key.Contains(Name)) continue;
return kvp.Value.dBuildingRefs;
}
return null;
}

public void CancelUpgrade()
{
Upgrade = null;
Expand Down
7 changes: 7 additions & 0 deletions Bureaucracy/Facilities/FacilityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class FacilityManager : Manager
public readonly List<BureaucracyFacility> Facilities = new List<BureaucracyFacility>();
public static FacilityManager Instance;
[UsedImplicitly] private PopupDialog warningDialog;
public float FireChance = 0.0f;
public float CostMultiplier = 1.0f;

public FacilityManager()
{
Expand Down Expand Up @@ -76,6 +78,9 @@ public void OnLoad(ConfigNode cn)
if (managerNode == null) return;
int.TryParse(managerNode.GetValue("FundingAllocation"), out int funding);
FundingAllocation = funding;
float.TryParse(managerNode.GetValue("CostMultiplier"), out CostMultiplier);
float.TryParse(managerNode.GetValue("FireChance"), out FireChance);
if (CostMultiplier < 1.0f) CostMultiplier = 1.0f;
ConfigNode[] facilityNodes = managerNode.GetNodes("FACILITY");
for (int i = 0; i < Facilities.Count; i++)
{
Expand All @@ -93,6 +98,8 @@ public void OnSave(ConfigNode cn)
Debug.Log("[Bureaucracy]: FacilityManager OnSave");
ConfigNode managerNode = new ConfigNode("FACILITY_MANAGER");
managerNode.SetValue("FundingAllocation", FundingAllocation, true);
managerNode.SetValue("CostMultiplier", CostMultiplier, true);
managerNode.SetValue("FireChance", FireChance, true);
for (int i = 0; i < Facilities.Count; i++)
{
BureaucracyFacility bf = Facilities.ElementAt(i);
Expand Down
4 changes: 0 additions & 4 deletions Bureaucracy/GameEvents/ExternalListeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ private void OnContractOffered(Contract contract)

private void AddLaunch(ShipConstruct ship)
{
if (Utilities.Instance.Randomise.NextDouble() > Bureaucracy.Instance.qaModifier)
{
ExplosionEvent unused = new ExplosionEvent();
}
Costs.Instance.AddLaunch(ship);
string editor = ship.shipFacility == EditorFacility.VAB ? "VehicleAssemblyBuilding" : "SpaceplaneHangar";
BureaucracyFacility bf = FacilityManager.Instance.GetFacilityByName(editor);
Expand Down
32 changes: 32 additions & 0 deletions Bureaucracy/RandomEvents/FireEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Linq;

namespace Bureaucracy
{
public class FireEvent : RandomEventBase
{
private readonly BureaucracyFacility facilityToBurn;
public FireEvent()
{
facilityToBurn = FacilityManager.Instance.Facilities.ElementAt(Utilities.Instance.Randomise.Next(0, FacilityManager.Instance.Facilities.Count));
title = "Fire!";
body = "Recent cutbacks have resulted in poor safety protocols at " + facilityToBurn.Name + ". As a result, a small fire has gotten out of control";
acceptString = "Oh dear. (" + facilityToBurn.Name + " is destroyed)";
CanBeDeclined = false;
}
public override bool EventCanFire()
{
if (Utilities.Instance.Randomise.NextDouble() > FacilityManager.Instance.FireChance) return false;
return facilityToBurn.CanBeDestroyed();
}

protected override void OnEventAccepted()
{
facilityToBurn.DestroyBuilding();
}

protected override void OnEventDeclined()
{

}
}
}
8 changes: 3 additions & 5 deletions Bureaucracy/RandomEvents/QAEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@ public class QaEvent : RandomEventBase
public QaEvent(ConfigNode eventNode)
{
LoadConfig(eventNode);
CanBeDeclined = true;
DeclineString = "No thanks";
}

public override bool EventCanFire()
{
if (Bureaucracy.Instance.qaModifier < 0.8f && EventEffect < 0) return false;
if (Bureaucracy.Instance.qaModifier >= 1.0f && EventEffect > 0) return false;
if (EventEffect < 0.0f && FacilityManager.Instance.FireChance <= 0.0f) return false;
return true;
}

protected override void OnEventAccepted()
{
Bureaucracy.Instance.qaModifier += EventEffect;
FacilityManager.Instance.CostMultiplier += EventEffect;
}

protected override void OnEventDeclined()
{
FacilityManager.Instance.FireChance += EventEffect;
}
}
}
6 changes: 3 additions & 3 deletions Bureaucracy/RandomEvents/RandomEventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public abstract class RandomEventBase
{
[UsedImplicitly] private PopupDialog eventDialog;
public string Name;
private string title;
private string body;
private string acceptString;
protected string title;
protected string body;
protected string acceptString;
protected string DeclineString;
protected bool CanBeDeclined;
protected float EventEffect;
Expand Down
5 changes: 5 additions & 0 deletions Bureaucracy/RandomEvents/RandomEventLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private void RollEvent()
private void LoadEvents()
{
ConfigNode[] eventCache = GameDatabase.Instance.GetConfigNodes("BUREAUCRACY_EVENT");
loadedEvents.Add(new FireEvent());
for (int i = 0; i < eventCache.Length; i++)
{
ConfigNode eventNode = eventCache.ElementAt(i);
Expand All @@ -54,6 +55,10 @@ private void LoadEvents()
re = new QaEvent(eventNode);
loadedEvents.Add(re);
break;
case "Wage":
re = new WageEvent(eventNode);
loadedEvents.Add(re);
break;
default:
throw new ArgumentException("[Bureaucracy]: Event "+eventNode.GetValue("Name")+" is not a valid type!");
}
Expand Down
44 changes: 44 additions & 0 deletions Bureaucracy/RandomEvents/WageEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Linq;

namespace Bureaucracy
{
public class WageEvent : RandomEventBase
{
private CrewMember crewMember;

public WageEvent(ConfigNode eventNode)
{
CrewMember c = FindCrew();
if (c == null) return;
crewMember = c;
LoadConfig(eventNode);
body = body.Replace("<crew>", c.Name);
acceptString = acceptString.Replace("<crew>", c.Name);
}

private CrewMember FindCrew()
{
if (CrewManager.Instance.Kerbals.Count == 0) return null;
int i = Utilities.Instance.Randomise.Next(0, CrewManager.Instance.Kerbals.Count);
CrewMember c = CrewManager.Instance.Kerbals.ElementAt(i).Value;
return c;
}

public override bool EventCanFire()
{
if (crewMember.CrewReference().rosterStatus != ProtoCrewMember.RosterStatus.Available) return false;
if (crewMember.WageModifier <= 1.0f && EventEffect < 0.0f) return false;
return true;
}

protected override void OnEventAccepted()
{
crewMember.WageModifier += EventEffect;
}

protected override void OnEventDeclined()
{

}
}
}
6 changes: 5 additions & 1 deletion Bureaucracy/UI/UIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ private PopupDialog DrawFacilityUi()
if (upgradeCount == 0) innerElements.Add(new DialogGUIHorizontalLayout(PaddedLabel("No Facility Upgrades in progress", false)));
DialogGUIVerticalLayout vertical = new DialogGUIVerticalLayout(innerElements.ToArray());
dialogElements.Add(new DialogGUIScrollList(new Vector2(300, 300), false, false, vertical));
dialogElements.Add(new DialogGUILabel("Processing: $"+investmentNeeded));
DialogGUIBase[] horizontal = new DialogGUIBase[3];
horizontal[0] = new DialogGUILabel("Total Investment Needed: $"+investmentNeeded);
horizontal[1] = new DialogGUILabel("|");
horizontal[2] = new DialogGUILabel("Chance of Fire: "+Math.Round(FacilityManager.Instance.FireChance*100, 0)+"%");
dialogElements.Add(new DialogGUIHorizontalLayout(horizontal));
dialogElements.Add(GetBoxes("facility"));
return PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new MultiOptionDialog("FacilitiesDialog", "", "Bureaucracy: Facilities", UISkinManager.GetSkin("MainMenuSkin"), new Rect(0.5f, 0.5f, 320, 350), dialogElements.ToArray()), false, UISkinManager.GetSkin("MainMenuSkin"));
}
Expand Down

0 comments on commit c1e4ff6

Please sign in to comment.