JavaScript and TypeScript

JavaScript and TypeScript

> Programming

 

Since version 2.49, in addition to LeekScript, you can write your leeks' artificial intelligences in JavaScript, TypeScript or Python. AIs run in a secure, deterministic environment with access to the game's entire fight API, and can fight AIs written in any other language.

New here? Follow the JavaScript / TypeScript tutorial to learn how to code your first AI, step by step.

Choosing the language

The language of an AI is determined by its file extension:

| Language | Extension | |----------|-----------| | LeekScript | (none) | | JavaScript | .js or .mjs | | TypeScript | .ts or .mts | | Python | .py |

You pick the language when creating a file ("New AI" button in the editor) or as the default language of your first AI when signing up. Renaming a file with a different extension changes its language.

Entry point: turn()

Each turn, the engine runs your AI. There are two ways to write it:

Simple AI: just write your code; it is run again every turn (in a fresh scope, with no state kept):

const me = Fight.me
me.setWeapon(Weapon.pistol)
const enemy = Fight.getNearestEnemy()
me.moveToward(enemy)
me.useWeapon(enemy)

Stateful AI: define a turn() function. The file body runs only once (declare your classes and persistent variables there), then turn() runs every turn:

const memory = []          // persists between turns

function turn() {
    const me = Fight.me
    const enemy = Fight.getNearestEnemy()
    me.moveToward(enemy)
    me.useWeapon(enemy)
}

In TypeScript or JavaScript, if your file contains an import or an export, it becomes a module: a normally-declared turn() function stays private to the module and is not found by the engine. Declare it with export function turn() instead:

export {} // or any import/export: the file becomes a module

export function turn(): void {
    const me = Fight.me
    me.useWeapon(Fight.getNearestEnemy())
}

The fight API

The game API is exposed in an object-oriented form. There is no global me variable: get your entity via Fight.me.

Unlike LeekScript, there are no global functions and no "flat" constants (getNearestEnemy(), WEAPON_PISTOL…): the whole API goes through the objects below.

Class tree


Entity                    an entity of the fight (stats, position, effects…)
├── Leek                  a leek
├── Turret                a turret (team fights)
├── Bulb                  a summon                 (subtype: Bulb.Type)
├── Chest                 a chest                  (subtype: Chest.Type)
├── Mob                   a monster or a boss      (subtype: Mob.Type)
└── Me                    YOUR entity, carrying the actions (via Fight.me)

Cell                      a cell of the field
Item                      common base of weapons / chips
├── Weapon                a weapon  (instances: Weapon.pistol, …)
└── Chip                  a chip    (instances: Chip.bandage, …)
Effect                    an active effect on an entity
Feature                   a characteristic carried by a weapon or chip

Entities returned by the API are typed: enemy instanceof Leek, enemy instanceof Bulb…

| Class | Main members | |-------|--------------| | Entity | life, maxLife, tp, mp, strength, agility, wisdom, resistance, science, magic, level, name, cell, weapon, weapons, chips, effects, states, summons, alive, dead, isAlly(), isEnemy(), distance(target) | | Me | everything from Entity, plus the actions: moveToward(target), moveAwayFrom(target), useWeapon(target), useChip(chip, target), setWeapon(weapon), say(message), summon(chip, cell, ai), canUseWeapon(target), weaponCell(target), chipCells(chip, target), weaponTargets(cell)… | | Cell | x, y, empty, obstacle, entity, distance(target), pathLength(target), lineOfSight(target), path(target) | | Weapon / Chip | cost, minRange, maxRange, area, launchType, maxUses, needsLos, features (plus cooldown and currentCooldown for Chip) | | Effect | type, value, caster, turns, critical, item, target | | Feature | type, minValue, maxValue, turns, targets |

Alongside the classes, four singletons:

| Singleton | Role | Main members | |-----------|------|--------------| | Fight | the fight | me, turn, getNearestEnemy(), getFarthestEnemy(), getEnemies(), getAliveEnemies(), getAllies(), getAlliedTurret()… | | Field | the field | cellFromXY(x, y), getObstacles(), distance(a, b), pathLength(a, b), lineOfSight(a, b), path(a, b) | | Registers | storage persisted between fights | get(key), set(key, value), delete(key), all() | | Debug | field visualization | mark(cells, color), markText(cells, text), show(cell), clearMarks(), pause() |

Constants

Constants are grouped by family, in two forms:

The editor's autocompletion (Weapon., Effect., Entity.Stat.…) lists all available members.

Multiple files

Split your AI into ES modules (import / export):

// utils.js
export function bestTarget() { /* ... */ }
// main.js
import { bestTarget } from "./utils.js"

Only your own files are accessible: no disk, network or system access.

TypeScript

In .ts files, the editor checks your types live: the whole fight API is typed (classes, methods, constants), with full autocompletion. At fight time, TypeScript is transpiled to JavaScript by the official tsc compiler, then run by the same engine. A type error makes the AI invalid, just like a LeekScript compilation error.

function target(): Entity | null {
    return Fight.getNearestEnemy()
}

Execution environment

See also