Skip to content

UnitSet beta ​

Class of @v12-engine/engine/ai

ts
export declare class UnitSet implements Iterable<EntityState>

A set of entities as the view gives them, in the snapshot's order, with the questions a brain asks as methods, each giving a set again, so that they chain: ai.view.mine.withRole('worker').idle(), ai.view.enemies.near(home, 20).fighters(). It reads as an array (length, for..of, map, filter, find, some, every, forEach, first, at; items is the plain array) but is not one: map and filter give plain arrays, where gives a set. (Not an Array subclass on purpose: the engines run filter and find on one several times slower, species lookup, and a think asks dozens.) A set is of one think: it reads the view it came from

Members ​

[Symbol.iterator] ​

ts
[Symbol.iterator](): Iterator<EntityState>;

Iterates the set's entities in order

constructor ​

ts
constructor(
    view: AiView, 
    items?: readonly EntityState[], remembered?: EntityState[]);

Constructs a new instance of the UnitSet class

alive ​

ts
alive(): UnitSet;

Those with health left

and ​

ts
and(other: UnitSet): UnitSet;

These and another set's, together, in the snapshot's order, each once

at ​

ts
at(index: number): EntityState | undefined;

The one at an index, from the end for a negative one

buildings ​

ts
buildings(): UnitSet;

The buildings among them, under construction too (finished leaves those out)

centre ​

ts
centre(): Vec2 | undefined;

The middle of them: none for an empty set

every ​

ts
every(fn: (entity: EntityState, index: number) => unknown): boolean;

Whether fn is true for every entity of the set

fighters ​

ts
fighters(): UnitSet;

Those that fight: soldiers and heroes

filter ​

ts
filter(fn: (entity: EntityState, index: number) => unknown): EntityState[];

As an array does: a plain array. where gives a set

find ​

ts
find(fn: (entity: EntityState, index: number) => unknown): EntityState | undefined;

The first entity fn is true for, or nothing; fn is given the entity and its index

finished ​

ts
finished(): UnitSet;

Those that stand finished: not under construction (a building turning into another type stands)

first ​

ts
first(): EntityState | undefined;

The first of them: none in an empty set

forEach ​

ts
forEach(fn: (entity: EntityState, index: number) => void): void;

Calls fn with each entity and its index, in order

has ​

ts
has(id: number): boolean;

Whether an entity, by id, is one of these

heroes ​

ts
heroes(): UnitSet;

The heroes among them

idle ​

ts
idle(): UnitSet;

Those of mine with nothing to do: no order, and not inside something (a worker in a mine). Only one's own say what they do

ids ​

ts
ids(): ReadonlySet<number>;

Their ids, as a set: for "is it one of these"

items ​

ts
readonly items: readonly EntityState[];

The entities, as a plain array, in the snapshot's order

known ​

ts
known(): UnitSet;

These and what is remembered of them beside what is in sight: for view.enemies, the enemy structures seen before and out of sight now (visible: false), which may be gone. Ask it of the view's set before narrowing it (view.enemies.known().buildings()): a narrowed set remembers nothing more

length ​

ts
get length(): number;

How many

map ​

ts
map<T>(fn: (entity: EntityState, index: number) => T): T[];

As an array does: a plain array

near ​

ts
near(point: Vec2, radius: number): UnitSet;

Those within a distance of a point, in the snapshot's order. A big set is asked of the view's grid, a small one is scanned: the answer is the same

nearest ​

ts
nearest(point: Vec2): EntityState | undefined;

The one nearest a point: none in an empty set. The first of two as near

ofOwner ​

ts
ofOwner(player: PlayerId): UnitSet;

Those of a player

ofResource ​

ts
ofResource(kind: ResourceId): UnitSet;

Deposits of a resource with something left in them (a gold mine, a tree): for the view's resources

ofType ​

ts
ofType(type: string): UnitSet;

Those of a unit type, or that count as it (UnitTypeDef.provides: a keep is a town hall too)

slice ​

ts
slice(start?: number, end?: number): UnitSet;

Some of them, as an array's slice takes them: as a set

some ​

ts
some(fn: (entity: EntityState, index: number) => unknown): boolean;

Whether fn is true for any entity of the set

strength ​

ts
strength(): number;

How strong they are together: the strength of each (Strength.of: by its stats and the health it has left), summed. 0 for what does not fight. Against someone in particular, ask view.strength.odds(these, those)

units ​

ts
units(): UnitSet;

The units among them: what moves, buildings and resources left out

view ​

ts
readonly view: AiView;

The view the set reads: the definitions, the roles, the grid

visible ​

ts
visible(): UnitSet;

Those in sight now: a structure seen before and out of sight is not (EntityState.visible)

where ​

ts
where(predicate: (entity: EntityState, def: UnitTypeDef) => boolean): UnitSet;

Those that satisfy a predicate, as a set

withRole ​

ts
withRole(role: AiRole): UnitSet;

Those whose type has a role (AiRole: 'worker', 'hall', a game's own word)

withTag ​

ts
withTag(tag: string): UnitSet;

Those whose type has a tag: the kit's (ranged, melee, hitsAir) or the game's (UnitTypeDef.ai.tags)

workers ​

ts
workers(): UnitSet;

Those that gather and build (role worker)