Skip to content

Game.Simulation.CityServiceBudgetSystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: class

Base: GameSystemBase, ICityServiceBudgetSystem

Summary:
CityServiceBudgetSystem collects and computes city-wide service budgets, upkeep and fees, and computes aggregated income/expense arrays used by the economy UI and other systems. It schedules several jobs (IJob / IJobChunk) to gather per-service and per-building upkeep, clear previous collected data, compute net upkeep for networks (roads), and to aggregate totals (taxes, service fees, subsidies, imports/exports and loans). The system exposes methods to read income/expense arrays, query service budgets/efficiencies, set service budgets, and query service building counts/workforce. It coordinates with CitySystem, TaxSystem, ServiceFeeSystem, ResourceSystem and other systems, and uses an EndFrameBarrier to mark budget changes.


Fields

  • private CitySystem m_CitySystem
    Initialised in OnCreate — reference to the city manager (current city entity).

  • private CityStatisticsSystem m_CityStatisticsSystem
    Used to fetch city statistics lookup for tax / income estimates.

  • private ResourceSystem m_ResourceSystem
    Used to read resource prefabs / prices when computing upkeep cost.

  • private ServiceFeeSystem m_ServiceFeeSystem
    Used to get collected service fee data (education, healthcare, utilities, etc).

  • private TaxSystem m_TaxSystem
    Used to query and get estimated tax income/expense by zone type.

  • private MapTilePurchaseSystem m_MapTilePurchaseSystem
    Used to calculate map tile upkeep when map tiles are locked.

  • private EndFrameBarrier m_EndFrameBarrier
    Used to mark updates to budget data (e.g. when SetServiceBudget changes the ServiceBudgetData buffer).

  • private CityConfigurationSystem m_CityConfigurationSystem
    Used to check configuration flags (e.g. whether map tiles are unlocked).

  • private GameModeGovernmentSubsidiesSystem m_GameModeGovernmentSubsidiesSystem
    Provides monthly subsidy amount for the GovernmentSubsidy income source.

  • private EntityQuery m_BudgetDataQuery
    Query to get singleton buffer of ServiceBudgetData.

  • private EntityQuery m_ServiceBuildingQuery
    Query for service buildings (prefab + building + CityServiceUpkeep).

  • private EntityQuery m_ServiceQuery
    Query for collected city service budget & upkeep components.

  • private EntityQuery m_UpkeepGroup
    Query for prefabs with CityServiceUpkeep (used to gather per-service upkeep from placed service buildings).

  • private EntityQuery m_ServiceObjectQuery
    Query for service object prefabs to collect per-prefab/placed stats.

  • private EntityQuery m_NetUpkeepQuery
    Query for net compositions (to compute road/net upkeep contribution).

  • private EntityQuery m_EconomyParameterQuery
    Query to obtain EconomyParameterData singleton.

  • private EntityQuery m_OutsideTradeParameterQuery
    Query to obtain OutsideTradeParameterData singleton.

  • private EntityQuery m_HealthcareFacilityQuery, m_DeathcareFacilityQuery, m_GarbageFacilityQuery, m_FireStationQuery, m_PoliceStationQuery
    Used to count facility building types for external service import logic.

  • protected NativeArray<int> m_Income
    Array (length 14) used to store per- IncomeSource values for the current computed frame (persisted and used by other systems via readers).

  • protected NativeArray<int> m_IncomeTemp
    Temporary array used by jobs; swapped/copy-managed with m_Income.

  • protected NativeArray<int> m_TotalIncome
    Stored totals per IncomeSource (used for UI/accumulation).

  • protected NativeArray<int> m_Expenses
    Array (length 15) used to store per- ExpenseSource values for the current computed frame.

  • protected NativeArray<int> m_ExpensesTemp
    Temporary array used by jobs; swapped/copy-managed with m_Expenses.

  • private int m_TotalTaxIncome
    Cached total tax income computed from m_Income.

  • private NativeReference<int> m_TotalTaxes
    NativeReference used in jobs to store total taxes.

  • private NativeParallelHashMap<Entity, CollectedCityServiceBudgetData> m_CityServiceBudgets
    Temporary map built by UpdateDataJob / CityServiceBudgetJob to hold collected per-service aggregated budget data.

  • private NativeParallelHashMap<Entity, int2> m_CityServiceUpkeepIndices
    Map from service entity => (startIndex, length) into m_CityServiceUpkeeps.

  • private NativeList<CollectedCityServiceUpkeepData> m_CityServiceUpkeeps
    Concatenated list of collected upkeep entries for all services (aggregated from service buildings), used to estimate upkeep per service.

  • private JobHandle m_TempArrayDeps
    Tracks dependencies for native arrays returned to callers (GetIncomeArray/GetExpenseArray) and for AddArrayReader.

  • private TypeHandle __TypeHandle
    Generated container of Component/Buffer type handles used by jobs (see __AssignHandles()).

  • private EntityQuery __query_844909884_0
    Generated query used by GetServiceEfficiency to access BuildingEfficiencyParameterData.

(There are also many nested job/struct fields defined inside job types — see Methods for job details.)

Properties

  • None public. (All stateful fields are private / protected; the system exposes functionality via methods.)

Constructors

  • public CityServiceBudgetSystem()
    Default constructor (system created by DOTS world). Initialization of persistent arrays and queries occurs in OnCreate().

Methods

  • protected override void OnCreate()
    Initializes references to other systems, creates and allocates NativeArray/NativeList/NativeParallelHashMap/NativeReference instances, and builds EntityQueries. Also calls RequireForUpdate for necessary queries.

  • protected override void OnGamePreload(Purpose purpose, GameMode mode)
    Enables/disables this system depending on game mode (system enabled only when mode.IsGame()).

  • protected override void OnGameLoaded(Context serializationContext)
    Ensures a singleton ServiceBudgetData entity exists (creates one if none present).

  • protected override void OnDestroy()
    Disposes all NativeArray/NativeList/NativeParallelHashMap/NativeReference allocations.

  • protected override void OnUpdate()
    Main scheduling method. It:

  • completes prior array dependencies,
  • copies temp arrays to live arrays,
  • collects entity lists and schedules UpdateDataJob to compute aggregated income/expense entries,
  • schedules ClearServiceDataJob to reset collected budget/upkeep buffers,
  • schedules CityServiceBudgetJob to gather upkeep from placed service buildings,
  • schedules ClearBuildingDataJob + CollectServiceBuildingBudgetDatasJob to aggregate per-prefab building counts/workers,
  • schedules NetServiceBudgetJob to add network upkeep (roads) for services.
  • registers dependencies with related systems (TaxSystem, ResourceSystem) and stores dependencies used by callers.

Job types (nested) — short descriptions: - UpdateDataJob (IJob): Aggregates service budgets/upkeeps into local native collections and computes m_Expenses and m_Income arrays for ExpenseSource / IncomeSource enumerations. Uses tax lookup, service fees, loan interest, import fees and map tile upkeep. Also stores total tax income in m_TotalTaxes. - ClearServiceDataJob (IJobChunk): Clears previous CollectedCityServiceBudgetData and collected upkeep buffers on service entities. - CityServiceBudgetJob (IJobChunk): Iterates placed service buildings, computes upkeep per prefab (resource costs, money upkeep scaled by budget and inactive state), increments collected service budget data & collected upkeep buffers for each relevant service entity. - ClearBuildingDataJob (IJobChunk): Clears collected per-prefab building budget data. - CollectServiceBuildingBudgetDatasJob (IJobChunk): Collects counts, workers and workplaces per service prefab (from placed service buildings). - NetServiceBudgetJob (IJobChunk): Adds per-network upkeep (e.g. net curves) to the corresponding service's base cost.

  • public NativeArray<int> GetIncomeArray(out JobHandle deps)
    Returns the income array (m_IncomeTemp) and outputs the job dependency the caller must respect.

  • public NativeArray<int> GetExpenseArray(out JobHandle deps)
    Returns the expense array (m_ExpensesTemp) and outputs the job dependency the caller must respect.

  • public void AddArrayReader(JobHandle deps)
    Adds a reader dependency so the system delays overwriting the arrays until the caller's jobs complete.

  • public int GetBalance()
    Returns current balance computed from live m_Income and m_Expenses.

  • public static int GetBalance(NativeArray<int> income, NativeArray<int> expenses)
    Static helper to compute balance = total income + total expenses (note expenses stored negative).

  • public int GetTotalIncome() / public static int GetTotalIncome(NativeArray<int> income)
    Returns total income (sum of m_Income).

  • public int GetTotalExpenses() / public static int GetTotalExpenses(NativeArray<int> expenses)
    Returns total expenses (sum of negative expense values).

  • public int GetTotalTaxIncome()
    Returns cached total tax income stored from last job run.

  • public int GetIncome(IncomeSource source) / public static int GetIncome(IncomeSource source, NativeArray<int> income)
    Get specific IncomeSource value.

  • public int GetTotalIncome(IncomeSource source)
    Returns stored total income for a source in m_TotalIncome.

  • public int GetExpense(ExpenseSource source) / public static int GetExpense(ExpenseSource source, NativeArray<int> expenses)
    Get specific ExpenseSource value.

  • public int GetMoneyDelta()
    Computes daily money delta scaled (sums incomes and expenses and divides by 24).

  • public int GetServiceBudget(Entity servicePrefab)
    Completes pending array deps and returns the current stored budget percentage for the provided service prefab (0 if not present in budgets).

  • public static int GetServiceBudget(Entity servicePrefab, NativeParallelHashMap<Entity, CollectedCityServiceBudgetData> budgets, Entity budgetEntity, BufferLookup<ServiceBudgetData> budgetDatas)
    Static version that looks up budgets map and fallback ServiceBudgetData buffer (returns 100 if not found in ServiceBudgetData buffer).

  • public void SetServiceBudget(Entity servicePrefab, int percentage)
    Sets the budget percentage in the ServiceBudgetData singleton buffer. If the value changed, an Updated component is added to the singleton entity using EndFrameBarrier command buffer to notify other systems.

  • public int GetServiceEfficiency(Entity servicePrefab, int budget)
    Computes service efficiency from BuildingEfficiencyParameterData.m_ServiceBudgetEfficiencyFactor curve (value in percent), using the generated query to access the parameter data.

  • private static int GetEstimatedServiceUpkeep(CollectedCityServiceBudgetData data, int2 indices, int budget, NativeList<CollectedCityServiceUpkeepData> upkeeps)
    Helper that computes estimated monetary upkeep from collected upkeep entries for a service given its budget percent and base cost.

  • public void GetEstimatedServiceBudget(Entity servicePrefab, out int upkeep) / public static void GetEstimatedServiceBudget(...)
    Instance and static variants to get estimated total upkeep cost for a given service prefab by using the collected data structures and ServiceBudgetData fallback.

  • public int GetNumberOfServiceBuildings(Entity serviceBuildingPrefab)
    Returns how many instances of a given service building prefab were counted during collection (uses collected per-prefab CollectedServiceBuildingBudgetData component).

  • public int2 GetWorkersAndWorkplaces(Entity serviceBuildingPrefab)
    Returns (workers, workplaces) aggregated for a given service building prefab.

  • public Entity[] GetServiceBuildings(Entity servicePrefab)
    Returns an array of placed service building entities whose ServiceObjectData.m_Service equals the provided service prefab (performs queries synchronously and disposes temporary arrays).

  • private static int GetTotalTaxIncome(NativeArray<int> income)
    Helper that adds up the four tax income sources (commercial, industrial, residential, office).

  • protected override void OnCreateForCompiler()
    Generated helper invoked during system creation in some compile configurations to assign queries and handles.

(Several private helper methods exist inside nested jobs, notably methods to compute import service fees based on population and outside trade parameters, and helpers to add upkeep costs to budget data.)

Usage Example

// Common usage inside other systems/mods:

// Read income/expense arrays (returns m_IncomeTemp / m_ExpensesTemp and dependency you must respect)
JobHandle deps;
var incomeArray = cityServiceBudgetSystem.GetIncomeArray(out deps);
var expenseArray = cityServiceBudgetSystem.GetExpenseArray(out deps);

// If you schedule jobs that read the returned arrays, call AddArrayReader to add your dependency:
cityServiceBudgetSystem.AddArrayReader(deps);

// Query a specific service's estimated upkeep:
int estimatedUpkeep;
cityServiceBudgetSystem.GetEstimatedServiceBudget(servicePrefabEntity, out estimatedUpkeep);

// Change a service budget (percentage 0-100)
cityServiceBudgetSystem.SetServiceBudget(servicePrefabEntity, 80);

// Read current balance
int balance = cityServiceBudgetSystem.GetBalance();

Notes and tips: - All GetIncomeArray/GetExpenseArray calls return native arrays tied to internal job scheduling — respect the returned JobHandle or register dependencies via AddArrayReader to avoid races. - SetServiceBudget modifies the ServiceBudgetData singleton buffer and will create an Updated tag on the singleton entity via EndFrameBarrier so other systems react at end-of-frame. - Many of the heavy computations are performed in burst-compiled IJobChunk jobs; modifying this system for a mod requires understanding DOTS Entity queries and proper job dependencies.