Skip to content

Low-level queries and diagnostics

These calls expose specialised game data. They are more likely to change after a game or WololoLabs update than the everyday queries, so keep each use in a small function you can test after an update.

Wrong argument types raise a Lua error. A nil result means the value could not be read; calls that also return a reason say so.

Fact shortcuts

GetFact(factId[, parameter = 0]) reads any PER fact for the assigned player and returns value, available: 0, false when the read failed, 0, true for a real zero. Use a Fact value for factId.

The shortcuts below each read one fact and return the value, or nil when it can't be read.

Call Same as
GetCommodityBuyingPrice(resource) GetFact(Fact.COMMODITY_BUYING_PRICE, resource): gold to buy 100.
GetCommoditySellingPrice(resource) Fact.COMMODITY_SELLING_PRICE: gold for selling 100.
GetCurrentScore() Fact.CURRENT_SCORE
GetPlayerInGame(playerId) Fact.PLAYER_IN_GAME: 1 while playing, 0 once out.
GetPlayerDistance(playerId) Fact.PLAYER_DISTANCE, in tiles.
GetPlayersInGame([stance = 0]) Fact.PLAYERS_IN_GAME
GetEnemyBuildingsInTown() Fact.ENEMY_BUILDINGS_IN_TOWN
GetEnemyUnitsInTown() Fact.ENEMY_UNITS_IN_TOWN
GetEnemyVillagersInTown() Fact.ENEMY_VILLAGERS_IN_TOWN
GetBuildingTypeInTown(type) Fact.BUILDING_TYPE_IN_TOWN
GetUnitTypeInTown(type) Fact.UNIT_TYPE_IN_TOWN
GetVillagerTypeInTown(type) Fact.VILLAGER_TYPE_IN_TOWN
GetDefenderCount() Fact.DEFENDER_COUNT
GetGaiaTypeCount(type) Fact.GAIA_TYPE_COUNT
GetGaiaTypeCountTotal(type) Fact.GAIA_TYPE_COUNT_TOTAL
GetCcGaiaTypeCount(type) Fact.CC_GAIA_TYPE_COUNT
GetPlayersTribute(resource) Fact.PLAYERS_TRIBUTE
GetPlayersTributeMemory(resource) Fact.PLAYERS_TRIBUTE_MEMORY
GetBattleRoyaleTime() Fact.BATTLE_ROYALE_TIME

Xs queries

Each Xs* call runs the game's XS function of the same name (XsGetUnitType runs xsGetUnitType) and returns value, available. When available is false, value is nil. Where playerId is optional, it defaults to the assigned player.

local hitpoints, available = XsGetUnitHitpoints(unitId)
if available then LogInfo("HP", hitpoints) end
Call Value
XsDoesUnitExist(unitId) Whether the unit exists.
XsGetUnitHitpoints(unitId) Hit points.
XsGetUnitBuildPoints(unitId) Construction progress.
XsGetUnitCharge(unitId) Charge value, for charge attacks and similar abilities.
XsGetUnitAttributeHeld(unitId, attributeId) Amount of an attribute, such as a resource, held.
XsGetUnitTargetUnitId(unitId) ID of the unit it targets.
XsGetUnitType(unitId) Type ID.
XsGetUnitClass(unitId) UnitClass.
XsGetUnitOwner(unitId) Owner's player number.
XsGetUnitGroupId(unitId) Group ID.
XsGetUnitTaskCount(unitId) Number of tasks.
XsUnitTaskAmount(unitId, taskId) Task query for a unit.
XsGetTechState(technology[, playerId]) ResearchState value.
XsGetTechAttribute(playerId, technology, attribute[, indexOrCost = 0]) A technology attribute, such as a cost or research time.
XsIsObjectValid(objectId[, playerId]) Whether the object ID is valid for the player.
XsIsObjectAvailable(objectType[, playerId]) Whether the player may create the type.
XsObjectHasAction(playerId, unitType, actionId[, targetPlayerId[, targetType[, targetLevel]]]) Whether the type can perform the action.
XsGetObjectTaskCount(objectId[, playerId]) Number of tasks for the object.
XsObjectTaskAmount(objectId, [playerId,] taskId) Task query for an object.
XsGetTaskAmount(taskFieldId) A task field.
XsGetPlayerInGame(playerId) Whether the player is still in the game.
XsGetPlayerType(playerId) PlayerType value.
XsGetDiplomacy(fromPlayerId, toPlayerId) Raw stance value.
XsGetLocalPlayerId() The local player's number.
XsGetWorldPlayerId(scenarioPlayerId) The world player for a scenario slot.
XsGetDifficulty() Difficulty setting.

Object types

These read a type's definition for the assigned player; no object of the type needs to exist.

Call Returns
GetObjectTypeName(objectType) The type's name, or nil when the player has no such type.
GetObjectTypeClass(objectType) UnitClass (900 to 999), or nil, reason.
GetObjectTypeAttribute(objectType, attribute[, damageClass = 0]) An attribute, as Object:GetAttribute reads it. 0 if unreadable.
GetObjectTypeData(objectType, dataId) A PER object-data value. 0 if unreadable.
GetIconId(kind, typeId[, playerId]) The icon id of a unit type (kind = "unit") or a technology ("research"), as that player's civilisation shows it, or of a resource ("resource", typeId a PlayerAttribute from FOOD to GOLD; the same for every civilisation); the player defaults to the assigned one. Unit and research kinds match GetProductionQueue entries, and the id is what batch:AddIcon draws. nil, reason (unknown-type, no-icon, icon-unreadable, player-unavailable) when there is none.
AiCountTableHasType(playerId, objectType) true when the player's AI count tables have a column for the type. nil, reason if unreadable.

Pathing and placement

These use the game's pathfinder and placement code.

Call Returns
CanReachPoint(object, position, radius) true when the unit can get within radius tiles of the point. The search computer players run before sending a builder.
CalculatePathDistance(object, position[, mode = 0[, playerId]]) Path length to the point. With playerId, that player's own and allied gates are open, as for its AI during its turn; without, every gate is closed.
CanUnitStandAt(object, position) true when the point is on the map, suits the unit, and nothing else blocks it.
GetPlacementTile(x, y) terrainFlags, pathFlags, elevation, explored for a tile, ignoring fog.
CheckBuildCommand(buildingType, x, y, builderId) true when the game would accept the build; else false, stage, code, blockerId.
CheckInfluencePlacement(buildingType, x, y, adjacent) 0 when the computer player's placement search would accept the site; else a placement code and blocker ID.
CheckBuildLinePlacement(buildingType, x, y, requireExplored) 0 when the up-can-build-line test accepts the spot; else a placement code.
GetBuildingTypeRadii(buildingType) radiusX, radiusY in tiles for your building type, as the game snaps a build point; else nil, reason.
GetWallPlanLines(perimeter, playerId) The wall lines the game plans for a player, and the distance step used.
CountUnbuiltWallTiles(playerId, x0, y0, x1, y1) Tiles on a wall line that could still take a palisade, as wall-completed-percentage counts.

nil means "couldn't ask", never "unreachable" or "blocked". These calls must run on the game's own thread. Called from a script worker, they wait for the next game frame, at most 250 ms, and return nil (with frame-query-unavailable where a reason is returned) when the frame doesn't run them in time: while paused, during a reload, or when more than 64 are waiting. Each frame runs at most four of them. Cache answers and don't ask for every candidate every update. Other reasons include layout-unavailable (unsupported game version), player-unavailable, unknown-building-type, invalid-position and native-fault.

GetPlacementTile flags. Terrain flag 8 is water; flags 1, 2 and 4 mark terrain the placement search won't build on. Path bits 0 and 14, and bit 1 on land, mark blocked tiles.

CheckBuildCommand runs the game's checks in order and returns the first that refuses:

Stage Meaning Code
build-limit A game-mode build limit is reached. -1
builder This builder can't build the type. Try other builders: one passing is enough. -1
gated The game rejects the type before placement. -1
placement The site fails; code below. 1 to 11
cost The player can't pay. Resource lacked: 0 food, 1 wood, 2 gold, 3 stone

Placement codes, shared by the three placement checks: 1 required terrain missing, 2 invalid terrain, 3 invalid hill, 4 invalid overlay, 5 site not visible or not explored, 6 another player's object in the way, 7 off the map, 8 your own object in the way, 10 attacked nearby, 11 inside a treaty zone.

CheckInfluencePlacement and CheckBuildLinePlacement run only the placement test, with different flags from a real build, so a spot they accept can still be refused by CheckBuildCommand.

GetWallPlanLines returns an array of { x0, y0, x1, y1, length, open } lines (smaller x end first; open is false for water) and the step, 0 to 5; an empty array and -1 when no step qualifies. perimeter is 1 or 2.

Combat records

Call Returns
GetProjectileRecord(player, projectileType) targetClass, attackerPlayer, timeMs of the player's latest projectile of that type; zeros when none this match.

Player actions

Call Returns
GetPlayerActionCount(player) Actions the player's own interface has sent since WololoLabs loaded. Sample it over time for APM.

The human-override calls the built-in runner uses are listed with the other internal calls.

Game hooks

These switch on game hooks that only native code can install. A request belongs to the script that makes it: call it from Update or Render every update, and it lapses on its own when that script unloads, even without its Shutdown. The hooks run only during a match. Each call returns the hook's status text; one starting Unavailable says why the game build cannot run it.

Call Does
SetScoreboardResources(resources) Adds stockpiles to the game's own scoreboard. resources is a table such as { wood = true, food = true, gold = true, stone = true }; nil turns it off. Experimental.
SetAgeUpSound(enabled) Plays the game's age-up sound when any player starts researching the next age.

Troubleshooting

Call Returns
GetResearchDiagnostics(technology) A table explaining each part of CanResearch.
GetNativeFactDiagnostics([objectType = 109]) A table showing how fact reads resolve in this game version.
GetNativeXsDiagnostics([playerId[, unitId[, technology[, objectType]]]]) A table showing which XS functions resolve.

Their fields can change between versions; use them while investigating, not in normal script logic.

Internal calls

The built-in runner and the test harness use further calls, some of them only in Debug and Dumper builds. They are not public API and can change in any update; Internal runner and harness calls lists them.

Compatibility lists the replacements for the removed api table, and the API name index lists names that do nothing.