Game.FreeWorkplaces
Assembly: Game
Namespace: Game.Companies
Type: struct
Base: IComponentData, IQueryTypeParameter, ISerializable
Summary: Represents the number of free workplace slots available in a company/building broken down by citizen education levels. Each count is stored as a byte (0–255). The struct can be constructed from a Workplaces instance, refreshed against an employee buffer (to subtract occupied slots), and serialized/deserialized for save/load. Provides helpers to query the lowest available level, the best-fit level for a requested worker level, and per-level free counts.
Fields
-
public byte m_Uneducated
Free slots for uneducated workers (education level 0). Stored as a byte and clamped to 0–255. -
public byte m_PoorlyEducated
Free slots for poorly educated workers (education level 1). Stored as a byte and clamped to 0–255. -
public byte m_Educated
Free slots for educated workers (education level 2). Stored as a byte and clamped to 0–255. -
public byte m_WellEducated
Free slots for well-educated workers (education level 3). Stored as a byte and clamped to 0–255. -
public byte m_HighlyEducated
Free slots for highly educated workers (education level 4). Stored as a byte and clamped to 0–255.
Properties
public int Count => m_Uneducated + m_PoorlyEducated + m_Educated + m_WellEducated + m_HighlyEducated
Total number of free workplace slots across all education levels. Computed on access by summing the five byte fields.
Constructors
public FreeWorkplaces(Workplaces free)
Initializes a FreeWorkplaces instance from a Workplaces structure by copying each education-level count and clamping each value to the 0–255 byte range using math.clamp. Use this when creating the free-slot representation from an existing Workplaces calculation.
Methods
public void Refresh(DynamicBuffer<Employee> employees, int maxWorkers, WorkplaceComplexity complexity, int level)
Recomputes free workplace counts for the building/company given the current employees buffer and workplace capacity parameters. Internally:- Calculates the nominal number of workplaces using EconomyUtils.CalculateNumberOfWorkplaces(maxWorkers, complexity, level).
- Decrements the appropriate workplace count for each Employee in the provided DynamicBuffer (using Employee.m_Level).
-
Clamps results to 0–255 and stores them back into the struct fields. This is the primary method to update free-slot counts after employees change or building capacity/level changes.
-
public byte GetLowestFree()
Returns the lowest education-level index (0–4) that currently has at least one free slot. If no free slots exist at any level, returns 5. Useful for assigning the least-skilled available worker requirement. -
public int GetBestFor(int level)
Given a desired worker education level, searches downward (from the requested level to 0) for the highest matching level that has a free slot and returns its index. If none found, returns -1. This implements a best-fit policy by allowing lower-level placements when exact-level slots are unavailable. -
public byte GetFree(int level)
Returns the free-slot count for the specified education level (0..4). If an invalid level is supplied, returns 0. -
private void SetFree(int level, byte amount)
Internal helper to set the free-slot count for a specific education level. Used internally by Refresh/constructor logic. Does nothing for invalid levels. -
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Serializes the five byte fields to the provided writer in the order: m_Uneducated, m_PoorlyEducated, m_Educated, m_WellEducated, m_HighlyEducated. Keep the same order when deserializing to maintain compatibility. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads the five byte fields from the provided reader into the struct in the same order used by Serialize. Uses ref locals to write directly into fields.
Usage Example
// Example: create from a Workplaces struct and refresh after employees changed.
// (Requires: using Unity.Entities; using Unity.Collections; and the relevant Game namespaces)
Workplaces workplaces = new Workplaces {
m_Uneducated = 3,
m_PoorlyEducated = 2,
m_Educated = 4,
m_WellEducated = 1,
m_HighlyEducated = 0
};
// Construct FreeWorkplaces from a Workplaces value (values are clamped to 0..255)
FreeWorkplaces free = new FreeWorkplaces(workplaces);
// Suppose within a system you have a DynamicBuffer<Employee> named employeesBuffer,
// and building parameters maxWorkers, complexity and level:
free.Refresh(employeesBuffer, maxWorkers: 20, complexity: WorkplaceComplexity.Medium, level: 2);
// Querying:
int totalFree = free.Count; // total free slots
byte lowest = free.GetLowestFree(); // first level index that has a free slot (0..4), or 5 if none
int best = free.GetBestFor(2); // best (>=0) level to place a level-2 worker, or -1 if none
byte freeAt2 = free.GetFree(2); // free slots at education level 2
// Serialization example (pseudo):
// writer.Write(free.m_Uneducated); ... etc.
// Deserialization must read in the same order.