Game.Simulation.XPAccumulationSystem
Assembly:
Assembly-CSharp
Namespace:
Game.Simulation
Type:
class
Base:
GameSystemBase
Summary:
Handles periodic accumulation of XP for the city. This system schedules a Burst IJob (XPAccumulateJob) at a fixed interval (kUpdatesPerDay = 32 updates per day rhythm) to compute XP gains based on population growth, average happiness and taxable incomes (residential, commercial, industrial, office). It reads XP parameter settings from a singleton XPParameterData and writes XPGain entries into the XPSystem's queue. The system also updates stored XP-related maxima (maximum population and maximum income) on the city's XP component. The implementation is ECS/Jobs-aware and uses ComponentLookup/BufferLookup plus a CityStatisticsSystem lookup to fetch statistic values.
Fields
-
public static readonly System.Int32 kUpdatesPerDay
Constant that defines how many XP accumulation updates occur per in-game day. Value: 32. Used to scale XP per-frame from per-day parameters. -
private XPSystem m_XPSystem
Reference to the global XPSystem used to obtain a writer queue for enqueuing XPGain items and to register the dependency when scheduling the job. -
private CitySystem m_CitySystem
Reference to the CitySystem used to obtain the current city entity (m_City), which is the target entity the job operates on. -
private CityStatisticsSystem m_CityStatisticsSystem
Reference to the CityStatisticsSystem used to get a NativeParallelHashMap lookup of statistics for reading incomes and other statistics. -
private EntityQuery m_XPSettingsQuery
An EntityQuery used to retrieve XPParameterData singleton (read-only) required by the job. The query is required for update in OnCreate. -
private TypeHandle __TypeHandle
Container struct holding ComponentLookup and BufferLookup handles (for Population, XP, and CityStatistic). Assigned during OnCreateForCompiler / __AssignHandles so they can be used in jobs. -
private struct XPAccumulateJob
Nested job struct (Burst-compiled) that performs the per-update XP accumulation logic on the main city entity. See Methods section for behavior. -
private struct TypeHandle
Nested struct used to cache component/buffer lookup handles and provide an __AssignHandles method to initialize them from a SystemState.
Properties
- This type has no public instance properties.
Constructors
public XPAccumulationSystem()
Default parameterless constructor. The system relies on OnCreate to initialize references to other systems and queries. Marked with [Preserve] attribute for linkage.
Methods
-
public override System.Int32 GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval in ticks (frame scheduling granularity) used by the engine to decide how often this system should run. Implementation: returns 262144 / kUpdatesPerDay, i.e. spacing updates so there are kUpdatesPerDay updates per in-game day. -
[Preserve] protected override void OnCreate()
Initializes system references and the EntityQuery for XPParameterData. Retrieves managed systems (XPSystem, CitySystem, CityStatisticsSystem) from the World and calls RequireForUpdate(m_XPSettingsQuery) so the system only runs when XP settings exist. -
[Preserve] protected override void OnUpdate()
Builds and schedules the XPAccumulateJob: - Initializes ComponentLookup/BufferLookup handles via InternalCompilerInterface.Get... using __TypeHandle fields and CheckedStateRef.
- Reads the XPParameterData singleton from m_XPSettingsQuery.
- Acquires the statistics lookup from m_CityStatisticsSystem.
- Obtains the city entity from m_CitySystem.
- Gets a NativeQueue
.Writer from m_XPSystem.GetQueue(out deps). - Schedules the job with combined dependencies and assigns base.Dependency.
-
Calls m_XPSystem.AddQueueWriter(base.Dependency) to notify XPSystem of the writer dependency.
-
private void __AssignQueries(ref SystemState state)
Compiler helper used during OnCreateForCompiler. In this implementation it creates and immediately disposes an EntityQueryBuilder placeholder (no queries added here beyond the explicit m_XPSettingsQuery initialized in OnCreate). -
protected override void OnCreateForCompiler()
Compiler-time initialization method that forwards to base.OnCreateForCompiler, calls __AssignQueries and invokes __TypeHandle.__AssignHandles to set up component/buffer lookups from the provided SystemState. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] public void __AssignHandles(ref SystemState state)
(TypeHandle.__AssignHandles)
Initializes internal ComponentLookup and BufferLookup members: - Population lookup as read-only
- XP lookup as read-write
-
CityStatistic buffer lookup as read-only
-
public void Execute()
(XPAccumulateJob.Execute)
Core job logic executed on a worker thread (Burst-compiled): - Reads city's Population component. If population < 10, does nothing.
- Reads/upserts the city's XP component to update m_MaximumPopulation and m_MaximumIncome:
- Determines population delta relative to stored maximum population and updates maximum if current population is higher.
- Reads taxable income statistics for residential (5 age groups) and sums taxable incomes for commercial, industrial, and office across resources via the CityStatisticsSystem lookup. Divides total income by 10 and updates m_MaximumIncome if higher.
- Enqueues two XPGain entries into the provided m_XPQueue:
- Population XP: floor(m_XPParameters.m_XPPerPopulation * (populationDelta) / kUpdatesPerDay)
- Happiness XP: floor(m_XPParameters.m_XPPerHappiness * averageHappiness / kUpdatesPerDay)
- Uses Entity.Null for the entity field on the XPGain and sets appropriate XPReason (Population, Happiness).
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initialization pattern used by the system:
m_XPSystem = base.World.GetOrCreateSystemManaged<XPSystem>();
m_CitySystem = base.World.GetOrCreateSystemManaged<CitySystem>();
m_CityStatisticsSystem = base.World.GetOrCreateSystemManaged<CityStatisticsSystem>();
m_XPSettingsQuery = GetEntityQuery(ComponentType.ReadOnly<XPParameterData>());
RequireForUpdate(m_XPSettingsQuery);
}
Notes and Modding Tips: - XP accumulation is spread across kUpdatesPerDay discrete updates; XPParameterData values are interpreted per day and divided by kUpdatesPerDay in the job. - If you need to alter XP gains externally, consider modifying XPParameterData singleton or intercepting/manipulating the XPSystem queue prior to processing. - The system assumes a single global city entity (m_City from CitySystem). For multi-city or different setups, ensure CitySystem.City returns the entity you expect.