Welcome to the Creatures Wiki! Log in and join the community.

Kaos

From Creatures Wiki
Revision as of 04:04, 7 April 2005 by Bdonlan (talk) (→‎Variables: change example)
Jump to navigation Jump to search

Kaos is a high level language designed by bd_ to make writing agents easier. The Kaos compiler then translates this high level language into CAOS. Currently, it is still in an early stage of development, but is already capable of automatically compiling complex expressions involving the use of TARG and such attributes as BHVR.

Syntax

Kaos borrows its syntax primarily from the C family of languages.

The overall structure of a Kaos file is a series of install, remove, and/or agent blocks. Here is an example of all of them:

install {
    atomic {
        object it;
        it = newsimp(2, 21, 47494, "rubber ball", 1, 7, 5000);
        it.attr = 214;
        it.bhvr = 0;
        it.accg = 5;
        it.elas = 25;
        it.mvsf(6100,9200);
    }
}
agent (2, 21, 47494) {
    script 1 {
        ownr.vely = ownr.vely - 20;
    }
}
remove {
    enum thing (3, 21, 47494) {
        kill(thing);
    }
}

Additionally, if you only need a single install block, you can simply place its code in the top level:

print("Hello, world!\n");

Expressions

Kaos expressions may use the standard arithmetic operators *, /, +, -, with the normal precedence and associativity:

print("The Answer is: ", 40 + 2, "\n");

OVxx variables may be accessed with the [] operator:

norn[42] = pntr;

There are also a set of primitive functions for accessing CAOS opcodes such as RAND and the OUT* functions. They have a syntax like the following:

print("And the die roll is: ", rand(1, 6), "\n");

Some functions work on objects:

ownr.kill;

Objects also have a set of predefined attributes, such as:

norn.bhvr = 12345;
pntr[47] = norn.bhvr;

Kaos automatically takes care of TARG, so you can do things like:

norn.bhvr = pntr[47].attr + ownr.velx;

This compiles to:

  targ avar pntr 47
  setv va00 attr
   targ ownr
  addv va00 velx
  targ norn
  bhvr va00

Variables

Local variables are declared with a statement like one of the following:

number fortytwo = 42;
object maria = norn;
string yum = "pizza";

Once a variable has been declared, it can be accessed simply with its name. Assignment is done with the = operator:

number meaningoflife;
meaningoflife = 42;
print("The Answer is: ", meaningoflife, "\n");
meaningoflife = meaningoflife * 2;
print("Twice the answer equals ", meaningoflife, "\n");

Variables are lexically scoped to the curly-braces-delimited block they are declared in. It is possible to mask them, like so:

number foo;
foo = 42;
print ("foo = ", foo); # prints 42
{
  print(foo); # 42
  number foo;
  foo = 24;
  print(foo); # 24
}
print(foo); # 42

Global (GAME and EAME) variables may be accessed in two ways. They can be aliased to a named lexical variable: