FERRETGame engine over Babylon.js
Ferret

Ferretic: playing a game by script

A Ferretic program plays a running game from inside its fixed step. It goes on until it WAITs; then game steps pass. A press made in a step is read by the game in the next step, so the same program on the same game gives the same run, however fast the machine or the network is.

How a test runs#

  • test.run {source} runs a program in the running game and returns its report: id, status (passed, failed, error, timeout, stopped), expectations, the number of failures and the first one (line, the line's code, message with the compared values), log (the last value of each name), shots, with RUNS runs and a summary (min, mean, max of each LOGged value over the runs), and the size of each table. The tables themselves (samples, log rows, prints, failures) are read with test.rows {id, table, offset?, count?}, page by page when long. A run has no limit of its own: it ends when its program ends (or at a TIMEOUT its author wrote). Any number of tests may run at once, and a test may start others. The game is paused at the end (keepRunning: true keeps it going); every held action is let go and TIMESCALE is set back.
  • The program goes on until it WAITs; then game steps pass. A press made in a step is read by the game in the next step.
  • Names of things: an entity's id or name, or a scene node's name (POS("car")). Values the game itself knows (rpm, gear, lap, score, health) are shown by the game's code with world.expose('rpm', () => car.rpm); the test reads them with GAME("rpm"), the command game.values lists them. Events the game emits on world.events are waited for with WAIT EVENT "race:finish" or handled with ON EVENT.
  • Tests belong to the project: keep them as tests/<what>.ferretic and run one with viewer_command test.run {file: "tests/<what>.ferretic"} (the editor gives the game the file's text). test.check {source | file} reads a program without running it.
  • test.help gives the whole language on one page; test.list, test.state {id?}, test.rows, test.stop {id?}, test.shot {name?, id?} (a picture SHOT took).

The language#

' comments start with an apostrophe; one statement a line or several with ":"; keywords in any case
TIMEOUT 60 s                         ' a limit in game time for this run (a run has none unless written)
SAMPLE EVERY 0.25 s z = POS("car").z, kmh = SPEED("car") * 3.6, rpm = GAME("rpm")
PRESS throttle                       ' the game's own action, held until RELEASE
PARALLEL ANY                         ' branches together; ANY: go on when the first one ends
  WAIT EVENT "race:finish" TIMEOUT 40 s
AND
  DO
    WAIT UNTIL GAME("rpm") >= 7000
    TAP shiftUp                      ' down for one step
  LOOP
END PARALLEL
RELEASE ALL
LOG time = TIME, gear = GAME("gear")
EXPECT TIME < 14 s, "the quarter mile in under 14 s"
EXPECT POS("car").z > POS("rival").z, "the player wins with good shifts"
  • Values: numbers with units (0.3 s, 250 ms, 100 km/h is kept in m/s, 90 deg, 30 cm, 7000 rpm), strings, TRUE/FALSE/NULL, vectors (VEC(x,y,z), .x .y .z .length), records from COMMAND and GAME (.field), arrays (DIM a(10)).
  • Flow: IF ... THEN ... ELSEIF ... ELSE ... END IF (or on one line), WHILE ... WEND, DO [WHILE|UNTIL c] ... LOOP [WHILE|UNTIL c], FOR i = 1 TO n [STEP s] ... NEXT, REPEAT [n] ... END REPEAT, REPEAT ... UNTIL c, EXIT/CONTINUE FOR|WHILE|DO|REPEAT, SUB name(a) ... END SUB (call: name 1 or CALL name(1)), FUNCTION f(x) ... RETURN v ... END FUNCTION (a FUNCTION may WAIT like a SUB), END.
  • Time: WAIT t, WAIT UNTIL c [TIMEOUT t], WAIT WHILE c, WAIT FRAMES n, WAIT EVENT "e" [TIMEOUT t]; after a TIMEOUT, TIMEDOUT; EVENTDATA is the event's payload. TIMESCALE 4 (faster), PAUSE, STEP n (while paused), RESUME, SEED n, RUNS n, RESTART ["level"] (enters the level again; a game that keeps its race in code is restarted with its own action: TAP restart).
  • Input: PRESS a, b, RELEASE a | RELEASE ALL, HOLD a [FOR t], TAP a, AXIS steer, -1 [, y] [FOR t]; ACTION(a), AXISX(a), AXISY(a), ACTIONS.
  • The game: TIME, FRAME, POS, ROT (degrees), VEL, SPEED (m/s), DIST(a, b), EXISTS, GAME("name"), PROP("entity", "Component.field"), COMMAND "camera.view" WITH name = "top" INTO r (any engine command).
  • Checks and records: EXPECT c, "words" (counts, goes on), ASSERT c, "words" (stops), FAIL "words", FAILURES, LOG name = v, SAMPLE EVERY t name = v, ..., SAMPLE STOP, PRINT, SHOT "name".
  • Library: ABS SGN SQR SIN COS TAN ATN ATAN2 ASIN ACOS (degrees) EXP LN LOG10 INT FIX FLOOR CEIL ROUND(x, n) MIN MAX CLAMP HYPOT RND RND(a, b) PI, LEN LEFT$ RIGHT$ MID$ UPPER$ LOWER$ TRIM$ STR$(x, decimals) VAL INSTR CONTAINS, VEC DOT NORM.

Probes#

Questions about the scene that are answered in numbers and yes or no, so that no picture is needed to know a fact:

FunctionValue
INVIEW("name")in the camera's view and seen past other things
VIEWSHARE("name")the share of the frame its box covers, 0 to 1
VISIBLE("name")the share of its sample points that are seen, 0 to 1
OCCLUDER("name")the name of what hides it, empty when nothing does
COUNT("what")how many carry that tag, name prefix or component
ONGROUND("name" [, tolerance])does it stand on what is under it
HEIGHT("name")metres over what is under it, negative when sunk
ERRORSthe errors of the page and the game so far

The same are engine commands (probe.view {name}, probe.count {what}, probe.ground {name, tolerance}, probe.errors), and probe.color {reference} gives the frame's mean colour, contrast and difference from a reference picture: COMMAND "probe.color" INTO c, then c.mean, c.contrast, c.difference.

EXPECT INVIEW("ship"), "the ship is in view"
EXPECT ONGROUND("guard", 0.1), "the guard stands on the deck"
EXPECT ERRORS = 0, "no errors"

Commands#

CommandWhat it does
`test.run {source \file, name?, runs?, timeout?, keepRunning?, wait?, seed?}`runs a program; returns the report when it ends (with wait: false at once, with its id)
test.rows {id?, table, offset?, count?, run?}rows of a table (samples, log, prints, failures), from offset, count of them (all the rest when not given); run picks a run of RUNS
test.listevery test run in this game, with its id and status
`test.check {source \file}`reads a program without running it: the first error with its line and column, the SUBs and FUNCTIONs, RUNS
test.state {id?}a test's report so far (the last started when no id)
test.stop {id?}stops a running test; its report says stopped
test.shot {name}a picture the last run took with SHOT
test.helpthe language on one page
game.values {names?}the values the game exposes with world.expose

{file} is a path of the project (tests/race.ferretic): the editor reads it and passes its text; the game itself cannot read the project.

In the game's code#

world.expose('rpm', () => car.rpm);           // GAME("rpm") in a test, game.values for tools
world.events.emit('race:finish', { time });   // WAIT EVENT "race:finish", ON EVENT, EVENTDATA.time

In code, bindFerretic(world) (done by the inspector) registers the commands; new FerreticRun(host, source) runs a program on any FerreticHost, also in a headless unit test.