Table of Contents

Class ResourceSystem

Namespace
Core.Resources
Assembly
Core.dll

ENGINE LAYER: Generic resource storage and management system.

Stores and manages multiple resource types (gold, manpower, prestige, etc.) for all countries. Uses deterministic fixed-point math for multiplayer compatibility.

Architecture:

  • Dictionary storage: resourceId → array of country values
  • Fixed-size arrays: countryId → resource amount
  • Deterministic: FixedPoint64 only, no float operations
  • Event-driven: Emits ResourceChangedEvent via EventBus

Usage:

  1. Initialize with country capacity and EventBus
  2. Register resource types with RegisterResource(resourceId, definition)
  3. Use AddResource/RemoveResource/GetResource for all operations
  4. Use CanAfford/TrySpend for cost validation

Performance:

  • GetResource: O(1) dictionary lookup + O(1) array access
  • Memory: sizeof(FixedPoint64) × resourceCount × countryCount
  • Example: 10 resources × 200 countries × 8 bytes = 16 KB
public class ResourceSystem : IResourceProvider
Inheritance
object
ResourceSystem
Implements

Properties

IsBatchMode

Whether currently in batch mode.

public bool IsBatchMode { get; }

Property Value

bool

IsInitialized

Check if initialized.

public bool IsInitialized { get; }

Property Value

bool

MaxCountries

Get country capacity.

public int MaxCountries { get; }

Property Value

int

ResourceCount

Get number of registered resources.

public int ResourceCount { get; }

Property Value

int

Methods

AddResource(ushort, ushort, FixedPoint64)

Add resource to a country (clamped to max value).

public void AddResource(ushort countryId, ushort resourceId, FixedPoint64 amount)

Parameters

countryId ushort
resourceId ushort
amount FixedPoint64

AddResourceToAll(ushort, FixedPoint64)

Add resource to all countries.

public void AddResourceToAll(ushort resourceId, FixedPoint64 amount)

Parameters

resourceId ushort
amount FixedPoint64

BeginBatch()

Begin batch mode - suppresses events until EndBatch is called. Use during loading/setup to avoid thousands of individual events.

public void BeginBatch()

CanAfford(ushort, ResourceCost[])

Check if country can afford multiple costs.

public bool CanAfford(ushort countryId, ResourceCost[] costs)

Parameters

countryId ushort
costs ResourceCost[]

Returns

bool

CanAfford(ushort, ushort, FixedPoint64)

Check if country can afford a single cost.

public bool CanAfford(ushort countryId, ushort resourceId, FixedPoint64 amount)

Parameters

countryId ushort
resourceId ushort
amount FixedPoint64

Returns

bool

EndBatch()

End batch mode - emits a single batch completed event.

public void EndBatch()

GetAllResourceIds()

Get all registered resource IDs.

public IEnumerable<ushort> GetAllResourceIds()

Returns

IEnumerable<ushort>

GetAllResourcesForCountry(ushort)

Get all resources for a country as dictionary (for UI/debugging).

public Dictionary<ushort, FixedPoint64> GetAllResourcesForCountry(ushort countryId)

Parameters

countryId ushort

Returns

Dictionary<ushort, FixedPoint64>

GetResource(ushort, ushort)

Get current resource amount for a country.

public FixedPoint64 GetResource(ushort countryId, ushort resourceId)

Parameters

countryId ushort
resourceId ushort

Returns

FixedPoint64

GetResourceDefinition(ushort)

Get resource definition by ID.

public ResourceDefinition GetResourceDefinition(ushort resourceId)

Parameters

resourceId ushort

Returns

ResourceDefinition

GetTotalResourceInWorld(ushort)

Get total amount of a resource across all countries.

public FixedPoint64 GetTotalResourceInWorld(ushort resourceId)

Parameters

resourceId ushort

Returns

FixedPoint64

Initialize(int, EventBus)

Initialize ResourceSystem with country capacity and EventBus. Must be called before any resource operations.

public void Initialize(int countryCapacity, EventBus eventBus = null)

Parameters

countryCapacity int
eventBus EventBus

IsResourceRegistered(ushort)

Check if a resource type is registered.

public bool IsResourceRegistered(ushort resourceId)

Parameters

resourceId ushort

Returns

bool

RegisterResource(ushort, ResourceDefinition)

Register a resource type with its definition. Creates storage array for all countries and initializes with starting amount.

public void RegisterResource(ushort resourceId, ResourceDefinition definition)

Parameters

resourceId ushort
definition ResourceDefinition

RemoveResource(ushort, ushort, FixedPoint64)

Remove resource from a country (returns true if successful, false if insufficient).

public bool RemoveResource(ushort countryId, ushort resourceId, FixedPoint64 amount)

Parameters

countryId ushort
resourceId ushort
amount FixedPoint64

Returns

bool

SetResource(ushort, ushort, FixedPoint64)

Set resource to exact amount (for dev commands/testing). Clamped to min/max values.

public void SetResource(ushort countryId, ushort resourceId, FixedPoint64 amount)

Parameters

countryId ushort
resourceId ushort
amount FixedPoint64

SetResourceForAll(ushort, FixedPoint64)

Set resource for all countries.

public void SetResourceForAll(ushort resourceId, FixedPoint64 amount)

Parameters

resourceId ushort
amount FixedPoint64

Shutdown()

Shutdown resource system (cleanup).

public void Shutdown()

TransferResource(ushort, ushort, ushort, FixedPoint64)

Transfer resource between countries (returns false if insufficient).

public bool TransferResource(ushort fromCountryId, ushort toCountryId, ushort resourceId, FixedPoint64 amount)

Parameters

fromCountryId ushort
toCountryId ushort
resourceId ushort
amount FixedPoint64

Returns

bool

TrySpend(ushort, ResourceCost[])

Try to spend multiple costs atomically (all or nothing). Returns false if insufficient resources (no changes made).

public bool TrySpend(ushort countryId, ResourceCost[] costs)

Parameters

countryId ushort
costs ResourceCost[]

Returns

bool