Skip to content
Yifan Zhu edited this page Jun 12, 2020 · 9 revisions

Welcome to the LogicExtensions wiki!

List of APIs

List of General Purpose APIs

print typeof int float str cli sti asleep irqv irq readSensor in clearTimeout

List of Math APIs

Math.newton Math.sin Math.cos Math.tan Math.asin Math.acos Math.atan Math.pow Math.log Math.exp Math.sqrt Math.abs Math.floor Math.ceil Math.round

List of Object APIs

Object.keys

API description

General Purpose APIs

print

Syntax:
    print(msg)
Prints message to console. 
Parameters:  
    msg - object to print

typeof

Syntax:
    typeof(obj)
Get object type
Parameters:
    obj - object to get type
Returns:
    type string

int

Syntax:
    int(value)
Convert to integer
Parameters:
    value - value to convert
Returns:
    int value

float

Syntax:
    float(value)
Convert to floating point number
Parameters:
    value - value to convert
Returns:
    floating point value

str

Syntax:
    str(value)
Convert to string
Parameters:
    value - value to convert
Returns:
    string

cli

Syntax:
    cli()
Clear interrupts

sti

Syntax:
    sti()
Set interrupts

asleep

Syntax:
    asleep()
Pause execution until next frame

irqv

Syntax:
    irqv(pio, handler, threshold, mode)
Set IRQ vector
Parameters:
    pio - PIO id
    handler - irq callback as function (id, treshold, front)
    threshold - (default=0) floating-point value, pio threshold in the range of [0..1)
    mode - (default=0) 0 - trigger on each front, 1 - uprising, 2 - downfalling 

irq

Syntax:
    irq(int pio)
Trigger IRQ vector
Parameters:
    pio - PIO id (= irq id)

readSensor

Syntax:
    readSensor(pio)
Read sensor (emulator) measures
Parameters:
    pio - PIO id, that sensor, speedometer, anglometer, or altimeter is emulating
Returns:
    Dictionary containing information that the block reads.

The function will return null unless the PIO being emulated has a non-zero value. So for example in anglometer you should set the minimal height to something negative and the maximum height to a huge value to make sure the output is always valid. All the axes are the axes of the game, not of the moving blocks.

  • When input is PIO id of a sensor, the return value contains fields
    • static: 1 when the object detected is static and 0 otherwise
    • type: type string of object. Possible object types are:
      • bomb
      • block
      • projectile
      • dead
      • unit
      • creature (fallback from unit, possibly you'll never see it)
      • breakable
      • entity
      • other
    • relativePos: a dict containing fields x, y, z for relative position of object detected
    • absolutePos: a dict containing fields x, y, z for absolute position of object detected
    • velocity: a dict containing fields x, y, z for velocity of object detected
  • When input is PIO id of a speedometer, the return value contains field
    • velocity: a dict containing fields x, y, z for velocity of speedometer
  • When input is PIO id of a altimeter, the return value contains field
    • absolutePos: a dict containing fields x, y, z for absolute position of altimeter
  • When input is PIO id of a anglometer, the return value contains fields
    • quaternion: a dict containing fields w, x, y, z for quaternion representing the rotation of the block
    • velocity: a dict containing fields x, y, z for angular velocity of block

in

Syntax:
    in(pio)
Read value from PIO.
Parameters:
    pio - PIO id
Returns:
    PIO input value in range of [0..1]

out

Syntax:
    out(pio, value)
Write value to PIO.
Parameters:
    pio - PIO id
    value - value in range of [0..1] to write

setTimeout

Syntax:
    setTimeout(timeout, callback)
Set callback that will be executed after timeout
Parameters:
    timeout: float - seconds until triggering timeout
    callback: func - function that will be executed
Returns:
    timeout id

clearTimeout

Syntax:
    clearTimeout(timeoutId)
Cancel timeout, set by setTimeout
Parameters:
    timeoutId: int - timeout id, returned by setTimeout

Math APIs

Math.newton

Find a zero of a real valued function using the Newton-Raphson (or
secant or Halley’s) method. Mimicks scipy's implementation of
scipy.optimize.newton
Syntax:
     Math.newton(func, x0, fprime=null, tol=1.48e-08, maxiter=50,
     fprime2=null, x1, rtol=0.0, full_output=false)
Parameters:
     func - function
          The function whose zero is wanted. It must be a function of a
          single variable
     x0 - float
          An initial estimate of the zero that should be somewhere near
          the actual zero.
     fprime - function, optional
          The derivative of the function when available and convenient. If it
          is null (default), then the secant method is used.
     tol - float, optional
          The allowable error of the zero value.
     maxiter - int, optional
          Maximum number of iterations.
     fprime2 - function, optional
          The second order derivative of the function when available and
          convenient. If it is null (default), then the normal Newton-Raphson
          or the secant method is used. If it is not null, then Halley's method
          is used.
     x1 - float, optional
          Another estimate of the zero that should be somewhere near the
          actual zero. Used if `fprime` is not provided.
     rtol - float, optional
          Tolerance (relative) for termination.
     full_output - bool, optional
          If `full_output` is false (default), the root is returned.
          If true, the dictionary {{"root": root}, {"converged": true/false},
          {"iter": numIter}} is returned.
Returns:
    If full_output is false (default), then a root is returned (even if the
    algorithm did not converge or ran into problems like a zero derivative
    for newton's method). If full_output is true, a dictionary is returned
    with fields
        * root: the root found
        * converged: whether the algorithm converged
        * iter: the number of iterations the algorithm ran
Notes:
    The convergence rate of the Newton-Raphson method is quadratic, the
    Halley method is cubic, and the secant method is sub-quadratic. The
    stopping criterion used is the step size and there is no guarantee that
    a zero has been found. 
Examples:
    function f(x) {
        return Math.pow(x, 4)  -  x * x - 12;
    }

    function df(x) {
        return 4 * Math.pow(x, 3) - 2 * x;
    }

    function df2(x) {
        return 12 * x * x - 2;
    }

    print("start");

    // secant method
    print(Math.newton(f, -1));
    // newton's method
    print(Math.newton(f, -1, df));
    print(Math.newton(f, -1, df, 0.01));
    print(Math.newton(f, -1, df, 0.01, 5));
    // Halley's method
    print(Math.newton(f, -1, df, 0.01, 5, df2));
    // Setting the derivative and second derivative to 0 while using other
    // optional arguments
    print(Math.newton(f, -1, null, 0.1, 10, null, -3));
    print(Math.newton(f, -1, null, 0.1, 10, null, -3, 0.001));
    // full_output = true
    print(Math.newton(f, -1, null, 0.0000001, 20, null, -3, 0, true));
    print(Math.newton(f, -1, df, 0.0000001, 20, null, -3, 0, true));
    print(Math.newton(f, -1, df, 0.0000001, 20, df2, -3, 0, true));

The following functions are just wrappers of the corresponding C# functions. You can refer to the C# documentation for handling of edge cases.

Math.sin

Syntax:
    Math.sin(a)
Parameters:
    a - angle in radians
Returns:
    sin

Math.cos

Syntax:
    Math.cos(a)
Parameters:
    a - angle in radians
Returns:
    cos

Math.tan

Syntax:
    Math.tan(a)
Parameters:
    a - angle in radians
Returns:
    tan

Math.asin

Syntax:
    Math.asin(a)
Parameters:
    a - sin
Returns:
    angle in radians

Math.acos

Syntax:
    Math.acos(a)
Parameters:
    a - cos
Returns:
    angle in radians

Math.atan

Syntax:
    Math.atan(a)
Parameters:
    a - tan
Returns:
    angle in radians

Math.pow

Syntax:
    Math.pow(x, y)
Calculate power x ^ y.
Parameters:
    x - value
    y - power
Returns:
    pow

Math.log

Syntax:
    Math.log(value, newBase=e)
Get logarithm.
Parameters:
    value - value to apply logarithm
    newBase - logarithm base, optional, defaults to e
Returns:
    log

Math.exp

Syntax:
    Math.exp(value)
Get exponential.
Parameters:
    value - value to apply exponential
Returns:
    exponential

Math.sqrt

Syntax:
    Math.sqrt(value)
Get square root.
Parameters:
    value - value to apply square root
Returns:
    square root of value

Math.abs

Syntax:
    Math.abs(value)
Get absolute value (change sign if negative).
Parameters:
    value - value to get absolute value
Returns:
    absolute value

Math.floor

Syntax:
    Math.floor(value)
Get floor (greatest integer smaller or equal to)
Parameters:
    value - value to get floor
Returns:
    floor

Math.ceil

Syntax:
    Math.ceil(value)
Get ceil (smallest integer greater or equal to)
Parameters:
    value - value to get ceil
Returns:
    ceil

Math.round

Syntax:
    Math.round(value)
Get nearest integer (rounds midway points to even integers)
Parameters:
    value - value to get nearest integer
Returns:
    nearest integer

Object APIs

Object.keys

Syntax:
    Object.keys(object)
Gets all keys for object.
Parameters:
    object - object to get keys
Returns:
    array of object keys