-
Notifications
You must be signed in to change notification settings - Fork 2
Entities
"Entities" form the foundation of flint's object-relational mapping (ORM). Freelancer-style INIs can be considered to form a crude relational database, and broadly speaking an entity here refers to anything in the game uniquely identified by a string nickname.
The base data class for any entity defined within Freelancer, distinguished by a nickname.
Attributes | Type | Notes |
---|---|---|
nickname |
str |
A unique string identifier for this entity |
ids_name |
int |
Resource id for name |
ids_info |
int |
Resource id for infocard |
Methods | ||
name() |
str |
The display name of this entity |
infocard(markup='html') |
str |
The infocard for this entity, formatted in the markup language (rdl , html or plain ) specified |
An Entity
's nickname uniquely identifies it - this means it is hashable.
Attributes (fields) of Entity
types represent entries defined in the INI file section type which this class represents. This allows Entities to be automatically constructed from parsed INI files. Fields may have default values - these are inferred from the behaviour of Freelancerrather than being defined in the INI files.
Methods represent derived fields. For example, the ids_name attribute stores the resource ID of an Entity's name as defined in the INI. The name() method looks up this resource ID in the resource table and returns the string it refers to.
If you want to extend these classes to cover non-standard fields (e.g. for an unsupported mod), you can use dataclassy.create_dataclass to dynamically define a dataclass and then use these classes as mixins.
An Entity
marked as Abstract means that no entities in the game are directly classified as it - in other words, it is never returned by flint but can be used for typing and inheritance.
As you would expect, all the usual rules of object inheritance apply, i.e. inherited classes retain all their previous methods and attributes, so System(Entity)
listed below has all the fields and methods of Entity
above. Similarly, PlanetaryBase(BaseSolar, Planet)
has all the attributes and methods of BaseSolar
and Planet
.
An EntitySet
is a set of entities of a particular type. An EntitySet
is constructed from an iterable producing Entity
objects, and it stores these in a hash table based on the nicknames of these entities. An EntitySet
is therefore indexable by nickname, for example fl.get_systems()['br01']
returns the Entity
for New London.
You can filter an EntitySet
in three ways:
- The
of_type(type_: Type)
method which returns a new, homogeneous EntitySet containing only entities which are instances of the specified type. - The
where
method which allows entities to be conveniently filtered based on their fields (meaning attributes and methods which take no arguments). For example,fl.get_systems().where(name='New Berlin')
returns anEntitySet
containing the oneEntity
it matches - the system of New Berlin. - For arbitrarily complex filtering, Python's excellent conditional generator expressions are recommended. For example,
EntitySet(s for s in fl.get_systems() if s.nickname.startswith('br'))
returns anEntitySet
containing all the systems in the house of Bretonia.
The EntitySet
also has a handy property, first
. This returns the first entity in the set, or None if it is empty. This is useful both for testing and extracting the one member of a unit set when it is expected that a query will return exactly one result.
EntitySet
s are immutable collections but two can be merged to create a new EntitySet
using +
or +=
.