UnitSet beta
Class of @v12-engine/engine/ai
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]
[Symbol.iterator](): Iterator<EntityState>;Iterates the set's entities in order
constructor
constructor(
view: AiView,
items?: readonly EntityState[], remembered?: EntityState[]);Constructs a new instance of the UnitSet class
alive
alive(): UnitSet;Those with health left
and
and(other: UnitSet): UnitSet;These and another set's, together, in the snapshot's order, each once
at
at(index: number): EntityState | undefined;The one at an index, from the end for a negative one
buildings
buildings(): UnitSet;The buildings among them, under construction too (finished leaves those out)
centre
centre(): Vec2 | undefined;The middle of them: none for an empty set
every
every(fn: (entity: EntityState, index: number) => unknown): boolean;Whether fn is true for every entity of the set
fighters
fighters(): UnitSet;Those that fight: soldiers and heroes
filter
filter(fn: (entity: EntityState, index: number) => unknown): EntityState[];As an array does: a plain array. where gives a set
find
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
finished(): UnitSet;Those that stand finished: not under construction (a building turning into another type stands)
first
first(): EntityState | undefined;The first of them: none in an empty set
forEach
forEach(fn: (entity: EntityState, index: number) => void): void;Calls fn with each entity and its index, in order
has
has(id: number): boolean;Whether an entity, by id, is one of these
heroes
heroes(): UnitSet;The heroes among them
idle
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
ids(): ReadonlySet<number>;Their ids, as a set: for "is it one of these"
items
readonly items: readonly EntityState[];The entities, as a plain array, in the snapshot's order
known
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
get length(): number;How many
map
map<T>(fn: (entity: EntityState, index: number) => T): T[];As an array does: a plain array
near
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
nearest(point: Vec2): EntityState | undefined;The one nearest a point: none in an empty set. The first of two as near
ofOwner
ofOwner(player: PlayerId): UnitSet;Those of a player
ofResource
ofResource(kind: ResourceId): UnitSet;Deposits of a resource with something left in them (a gold mine, a tree): for the view's resources
ofType
ofType(type: string): UnitSet;Those of a unit type, or that count as it (UnitTypeDef.provides: a keep is a town hall too)
slice
slice(start?: number, end?: number): UnitSet;Some of them, as an array's slice takes them: as a set
some
some(fn: (entity: EntityState, index: number) => unknown): boolean;Whether fn is true for any entity of the set
strength
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
units(): UnitSet;The units among them: what moves, buildings and resources left out
view
readonly view: AiView;The view the set reads: the definitions, the roles, the grid
visible
visible(): UnitSet;Those in sight now: a structure seen before and out of sight is not (EntityState.visible)
where
where(predicate: (entity: EntityState, def: UnitTypeDef) => boolean): UnitSet;Those that satisfy a predicate, as a set
withRole
withRole(role: AiRole): UnitSet;Those whose type has a role (AiRole: 'worker', 'hall', a game's own word)
withTag
withTag(tag: string): UnitSet;Those whose type has a tag: the kit's (ranged, melee, hitsAir) or the game's (UnitTypeDef.ai.tags)
workers
workers(): UnitSet;Those that gather and build (role worker)