public void setCosts() { try { RoadsOfTheRimSettings settings = LoadedModManager.GetMod <RoadsOfTheRim>().GetSettings <RoadsOfTheRimSettings>(); RoadConstructionSite parentSite = this.parent as RoadConstructionSite; float elevationModifier = 0f; float hillinessModifier = 0f; float swampinessModifier = 0f; float bridgeModifier = 0f; GetCostsModifiers(parentSite.Tile, parentSite.GetNextLeg().Tile, ref elevationModifier, ref hillinessModifier, ref swampinessModifier, ref bridgeModifier); // Total cost modifier float totalCostModifier = (1 + elevationModifier + hillinessModifier + swampinessModifier + bridgeModifier) * ((float)settings.BaseEffort / 10); DefModExtension_RotR_RoadDef roadDefExtension = parentSite.roadDef.GetModExtension <DefModExtension_RotR_RoadDef>(); // Check existing roads for potential rebates when upgrading GetUpgradeModifiers(parentSite.Tile, parentSite.GetNextLeg().Tile, parentSite.roadDef, out Dictionary <string, int> rebate); List <string> s = new List <string>(); foreach (string resourceName in DefModExtension_RotR_RoadDef.allResourcesAndWork) { if (roadDefExtension.GetCost(resourceName) > 0) { int thisRebate = 0; // The cost modifier doesn't affect some advanced resources, as defined in static DefModExtension_RotR_RoadDef.allResourcesWithoutModifiers float costModifierForThisResource = ((DefModExtension_RotR_RoadDef.allResourcesWithoutModifiers.Contains(resourceName)) ? 1 : totalCostModifier); rebate.TryGetValue(resourceName, out thisRebate); // Minimum cost of anything that's needed is 1 costs[resourceName] = Math.Max((int)((roadDefExtension.GetCost(resourceName) - thisRebate) * costModifierForThisResource), 1); left[resourceName] = Math.Max(costs[resourceName], 1f); if (thisRebate > 0) { s.Add("RoadsOfTheRim_UpgradeRebateDetail".Translate((int)(thisRebate * costModifierForThisResource), resourceName)); } } } if (s.Count > 0) { Messages.Message("RoadsOfTheRim_UpgradeRebate".Translate(parentSite.roadDef.label, string.Join(", ", s.ToArray())), MessageTypeDefOf.PositiveEvent); } parentSite.UpdateProgressBarMaterial(); } catch (Exception e) { Log.Error("[RotR] : Exception when setting constructionSite costs = " + e); } }
/* * For resources (including work) that are part of the cost of both the road to build and the best existing road, * grant CostUpgradeRebate% (default 30%) of the best existing road build costs as a rebate on the costs of the road to be built * i.e. the exisitng road cost 300 stones, the new road cost 600 stones, the rebate is 300*30% = 90 stones */ public static void GetUpgradeModifiers(int fromTile_int, int toTile_int, RoadDef roadToBuild, out Dictionary <string, int> rebate) { rebate = new Dictionary <string, int>(); RoadDef bestExistingRoad = RoadsOfTheRim.BestExistingRoad(fromTile_int, toTile_int); if (bestExistingRoad != null) { DefModExtension_RotR_RoadDef bestExistingRoadDefModExtension = bestExistingRoad.GetModExtension <DefModExtension_RotR_RoadDef>(); DefModExtension_RotR_RoadDef roadToBuildRoadDefModExtension = roadToBuild.GetModExtension <DefModExtension_RotR_RoadDef>(); if (bestExistingRoadDefModExtension != null && roadToBuildRoadDefModExtension != null && RoadsOfTheRim.isRoadBetter(roadToBuild, bestExistingRoad)) { foreach (string resourceName in DefModExtension_RotR_RoadDef.allResourcesAndWork) { int existingCost = bestExistingRoadDefModExtension.GetCost(resourceName); int toBuildCost = roadToBuildRoadDefModExtension.GetCost(resourceName); if (existingCost != 0 && toBuildCost != 0) { if ((int)(existingCost * (float)RoadsOfTheRim.settings.CostUpgradeRebate / 100) > toBuildCost) { rebate[resourceName] = toBuildCost; } else { rebate[resourceName] = (int)(existingCost * (float)RoadsOfTheRim.settings.CostUpgradeRebate / 100); } } } } } }
public override void DoWindowContents(Rect inRect) { if (Event.current.isKey && site != null) { RoadsOfTheRim.DeleteConstructionSite(site.Tile); Close(); } //Resources icons for (int i = 0; i < 9; i++) { // Icon Rect ResourceRect = new Rect(0, 202f + i * 40f, 32f, 32f); ThingDef theDef; switch (i) { case 0: theDef = ThingDefOf.Spark; break; case 1: theDef = ThingDefOf.WoodLog; break; case 2: theDef = ThingDefOf.BlocksGranite; break; case 3: theDef = ThingDefOf.Steel; break; case 4: theDef = ThingDefOf.Chemfuel; break; case 5: theDef = ThingDefOf.Plasteel; break; case 6: theDef = ThingDefOf.Uranium; break; case 7: theDef = ThingDefOf.ComponentIndustrial; break; default: theDef = ThingDefOf.ComponentSpacer; break; } if (i == 0) { Widgets.ButtonImage(ResourceRect, ContentFinder <Texture2D> .Get("UI/Commands/AddConstructionSite")); } else { Widgets.ThingIcon(ResourceRect, theDef); } } // Sections : one per type of buildable road int nbOfSections = 0; Vector2 groupSize = new Vector2(144, 512 + 128); foreach (RoadDef aDef in buildableRoads) { DefModExtension_RotR_RoadDef roadDefExtension = aDef.GetModExtension <DefModExtension_RotR_RoadDef>(); // Check if a tech is necessary to build this road, don't display the road if it isn't researched yet ResearchProjectDef NeededTech = roadDefExtension.techNeededToBuild; bool TechResearched = false; if (NeededTech != null) { TechResearched = NeededTech.IsFinished; } else { TechResearched = true; } if (TechResearched) { GUI.BeginGroup(new Rect(new Vector2(64 + 144 * nbOfSections, 32f), groupSize)); // Buildable Road icon Texture2D theButton = ContentFinder <Texture2D> .Get("UI/Commands/Build_" + aDef.defName, true); Rect ButtonRect = new Rect(8, 8, 128, 128); if (Widgets.ButtonImage(ButtonRect, theButton)) { if (Event.current.button == 0) { SoundStarter.PlayOneShotOnCamera(SoundDefOf.Tick_High, null); site.roadDef = aDef; Close(); RoadsOfTheRim.RoadBuildingState.CurrentlyTargeting = site; RoadsOfTheRim.RoadBuildingState.Caravan = caravan; RoadConstructionLeg.Target(site); } } // Buildable Road label Text.Anchor = TextAnchor.MiddleCenter; Text.Font = GameFont.Medium; Rect NameRect = new Rect(0, 144, 144f, 32f); Widgets.Label(NameRect, aDef.label); // Resources amounts Text.Font = GameFont.Small; int i = 0; foreach (string resourceName in DefModExtension_RotR_RoadDef.allResourcesAndWork) { Rect ResourceAmountRect = new Rect(0, 176f + i++ *40f, 144f, 32f); Widgets.Label(ResourceAmountRect, (roadDefExtension.GetCost(resourceName) > 0) ? (roadDefExtension.GetCost(resourceName) * ((float)RoadsOfTheRim.settings.BaseEffort / 10)).ToString() : "-" ); } GUI.EndGroup(); nbOfSections++; } } Text.Anchor = TextAnchor.UpperLeft; }