Difference between revisions of "AGI/Specifications/Logic"

Jump to navigation Jump to search
m
Text replacement - "</source>" to "</syntaxhighlight>"
(Fix syntax highlighting)
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(One intermediate revision by the same user not shown)
Line 18: Line 18:
think they are using a flag).
think they are using a flag).


<source lang="cpp">
<syntaxhighlight lang="cpp">
     assign.v(v50,0);
     assign.v(v50,0);
     program.control();
     program.control();
</source>
</syntaxhighlight>


Multiple commands may be placed on the one line:
Multiple commands may be placed on the one line:


<source lang="cpp">
<syntaxhighlight lang="cpp">
   reset(f6); reset(f7);
   reset(f6); reset(f7);
</source>
</syntaxhighlight>


Substitutions for the following action commands are available:
Substitutions for the following action commands are available:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     increment(v30);      v30++;
     increment(v30);      v30++;
     decrement(v30);      v30--;
     decrement(v30);      v30--;
Line 47: Line 47:
     lindirectv(v30,v32);  *v30 = v32;
     lindirectv(v30,v32);  *v30 = v32;
     rindirect(v30,v32);  v30 = *v32;
     rindirect(v30,v32);  v30 = *v32;
</source>
</syntaxhighlight>


===Conditionals and tests===
===Conditionals and tests===
Line 53: Line 53:
An '''if''' structure looks like this:
An '''if''' structure looks like this:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if (<test commands>) {
     if (<test commands>) {
         <action commands>
         <action commands>
     }
     }
</source>
</syntaxhighlight>


or like this :
or like this :


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if (<test commands>) {
     if (<test commands>) {
         <action commands>
         <action commands>
Line 68: Line 68:
       <more action commands>
       <more action commands>
     }
     }
</source>
</syntaxhighlight>


Multiple commands can be placed in a single line:
Multiple commands can be placed in a single line:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if (<test commands>) { <action Commands> } else { <more action commands> }
     if (<test commands>) { <action Commands> } else { <more action commands> }
</source>
</syntaxhighlight>


Test commands are coded like action commands except there is no semicolon. They are separated by && or || for AND or OR.
Test commands are coded like action commands except there is no semicolon. They are separated by && or || for AND or OR.


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if (isset(f5) &&
     if (isset(f5) &&
         greatern(v5,6)) { ......
         greatern(v5,6)) { ......
</source>
</syntaxhighlight>


  Multiple tests can be placed in a single line within the if statement:
  Multiple tests can be placed in a single line within the if statement:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if (lessn(v5,6) && (greatern(v5,2)) { .......
     if (lessn(v5,6) && (greatern(v5,2)) { .......


     if (isset(f90) && equalv(v32,v34)
     if (isset(f90) && equalv(v32,v34)
         && greatern(v34,20)) { .......
         && greatern(v34,20)) { .......
</source>
</syntaxhighlight>


A '''!''' placed in front of a command negates its result:
A '''!''' placed in front of a command negates its result:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if (!isset(f7)) {
     if (!isset(f7)) {
       ......
       ......
</source>
</syntaxhighlight>


Boolean expressions are not necessarily simplified so they must follow the rules set down by the file format. If test commands are to be ORred together, they must be placed in brackets.
Boolean expressions are not necessarily simplified so they must follow the rules set down by the file format. If test commands are to be ORred together, they must be placed in brackets.


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if ((isset(f1) || isset(f2)) {
     if ((isset(f1) || isset(f2)) {
       ......
       ......
Line 109: Line 109:


     if (isset(1) || (isset(2) && isset(3))) {    // is NOT legal
     if (isset(1) || (isset(2) && isset(3))) {    // is NOT legal
</source>
</syntaxhighlight>


Depending on the compiler, simplification of boolean expressions may be supported, so the above may not apply in all cases (although if these are rules are followed then the logic will work with all compilers).
Depending on the compiler, simplification of boolean expressions may be supported, so the above may not apply in all cases (although if these are rules are followed then the logic will work with all compilers).
Line 115: Line 115:
The following test commands and operations are equivalent:
The following test commands and operations are equivalent:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     equaln(v30,4)        v30 == 4
     equaln(v30,4)        v30 == 4
     equalv(v30,v32)      v30 == v32
     equalv(v30,v32)      v30 == v32
Line 128: Line 128:
     !lessn(v30,4)        v30 >= 4
     !lessn(v30,4)        v30 >= 4
     !lessv(v30,v32)      v30 >= v32
     !lessv(v30,v32)      v30 >= v32
</source>
</syntaxhighlight>


Also, flags can be tested for by just using the name of the flag:
Also, flags can be tested for by just using the name of the flag:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if (f6) { .....
     if (f6) { .....


     if (v7 > 0 && !f6) { .....
     if (v7 > 0 && !f6) { .....
</source>
</syntaxhighlight>


===Argument types===
===Argument types===
Line 160: Line 160:
Compilers can enforce type checking, so that the programmer must use the correct prefix for an argument so that they know they are using the right type. Decoders should display arguments with the right type.
Compilers can enforce type checking, so that the programmer must use the correct prefix for an argument so that they know they are using the right type. Decoders should display arguments with the right type.


<source lang="cpp">
<syntaxhighlight lang="cpp">
     move.obj(so4,80,120,2,f66);
     move.obj(so4,80,120,2,f66);
     if (obj.in.box(so2,30,60,120,40)) { .....
     if (obj.in.box(so2,30,60,120,40)) { .....
</source>
</syntaxhighlight>


A complete list of the commands and their argument types is available in [[AGI/Specifications/Resources#Command_list_and_argument_typesas | Command list and argument types table]].
A complete list of the commands and their argument types is available in [[AGI/Specifications/Resources#Command_list_and_argument_typesas | Command list and argument types table]].
Line 169: Line 169:
Messages and inventory items may be given in either numerical text format:
Messages and inventory items may be given in either numerical text format:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     print("He's not here.");
     print("He's not here.");
     print(m12);
     print(m12);
     if (has("Jetpack")) { .....
     if (has("Jetpack")) { .....
     if (has(io9)) { .....
     if (has(io9)) { .....
</source>
</syntaxhighlight>


Messages can also be split over multiple lines:
Messages can also be split over multiple lines:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     print("This message is split "
     print("This message is split "
           "over multiple lines.");
           "over multiple lines.");
</source>
</syntaxhighlight>


Quote marks must be used around messages and object names. This is important because some messages or object names may contain brackets or commas, which could confuse the compiler. This is also the case for the '''said''' command which will be described shortly.
Quote marks must be used around messages and object names. This is important because some messages or object names may contain brackets or commas, which could confuse the compiler. This is also the case for the '''said''' command which will be described shortly.


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if (has("Buckazoid(s)")) { .....        // no ambiguity here about where
     if (has("Buckazoid(s)")) { .....        // no ambiguity here about where
                                             // the argument ends
                                             // the argument ends
</source>
</syntaxhighlight>


The '''said''' test command uses different parameters to all the other commands. Where as the others use 8 bit arguments (0--255), '''said''' takes 16 bit arguments (0--65535). Also, the number of arguments in a '''said''' command can vary. The numbers given in the arguments are the word group numbers from the '''words.tok''' file.
The '''said''' test command uses different parameters to all the other commands. Where as the others use 8 bit arguments (0--255), '''said''' takes 16 bit arguments (0--65535). Also, the number of arguments in a '''said''' command can vary. The numbers given in the arguments are the word group numbers from the '''words.tok''' file.


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if (said(4,80)) { .....
     if (said(4,80)) { .....
</source>
</syntaxhighlight>


Words can also be given in place of the numbers:
Words can also be given in place of the numbers:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     if (said("look")) { .....
     if (said("look")) { .....
     if (said("open","door")) { .....
     if (said("open","door")) { .....
</source>
</syntaxhighlight>


Quote marks must also be used around the words.
Quote marks must also be used around the words.
Line 209: Line 209:
Labels are given like this:
Labels are given like this:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     Label1:
     Label1:
</source>
</syntaxhighlight>


The label name can contain letters, numbers, and the characters "_" and ".". No spaces are allowed. The '''goto''' command takes one parameter, the name of a label.
The label name can contain letters, numbers, and the characters "_" and ".". No spaces are allowed. The '''goto''' command takes one parameter, the name of a label.


<source lang="cpp">
<syntaxhighlight lang="cpp">
     goto(Label1);
     goto(Label1);
</source>
</syntaxhighlight>


===Comments===
===Comments===
Line 223: Line 223:
There are three ways that comments can be used.
There are three ways that comments can be used.


<source lang="cpp">
<syntaxhighlight lang="cpp">
     // Rest of line is ignored
     // Rest of line is ignored
     [  Rest of line is ignored
     [  Rest of line is ignored
     /* Text between these are ignored */
     /* Text between these are ignored */
</source>
</syntaxhighlight>


The /*...*/ can be nested:
The /*...*/ can be nested:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     /* comment start
     /* comment start
       print("Hello");    // won't be run
       print("Hello");    // won't be run
Line 239: Line 239:
       print("Hey!");    // won't be run, still inside comments
       print("Hey!");    // won't be run, still inside comments
     */                  // uncomments
     */                  // uncomments
</source>
</syntaxhighlight>


===Defines===
===Defines===
Line 245: Line 245:
To give vars, flags etc. proper names the #define command is used. The name of the define is given followed by the define value:
To give vars, flags etc. proper names the #define command is used. The name of the define is given followed by the define value:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     #define ego o0
     #define ego o0
     #define room_descr "This is a large hall with tall pillars down each side."
     #define room_descr "This is a large hall with tall pillars down each side."
</source>
</syntaxhighlight>


Then the define name can be used in place of the define value:
Then the define name can be used in place of the define value:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     draw(ego);
     draw(ego);
     print(room_descr);
     print(room_descr);
</source>
</syntaxhighlight>


Define names can only be used in arguments of commands (including gotos and the v0 == 3 type syntax), although some compilers may allow you to use them anywhere.
Define names can only be used in arguments of commands (including gotos and the v0 == 3 type syntax), although some compilers may allow you to use them anywhere.
Line 267: Line 267:
You can include another file in your logic source code by using the #include command:
You can include another file in your logic source code by using the #include command:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     #include "file.txt"
     #include "file.txt"
</source>
</syntaxhighlight>


When the compiler encounters the above line, it will replace it with the contents of file.txt.
When the compiler encounters the above line, it will replace it with the contents of file.txt.
Line 278: Line 278:
In some cases you may want to assign a specific number to a message so you can refer to it in other places. This is done by using the #message command, followed by the number of the message then the message itself:
In some cases you may want to assign a specific number to a message so you can refer to it in other places. This is done by using the #message command, followed by the number of the message then the message itself:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     #message 4 "You can't do that now."
     #message 4 "You can't do that now."
</source>
</syntaxhighlight>


Then you can give the message number as the parameter in commands:
Then you can give the message number as the parameter in commands:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     print(m4);
     print(m4);
</source>
</syntaxhighlight>


Or embed the message in commands as normal and the number you assigned to it before will be used:
Or embed the message in commands as normal and the number you assigned to it before will be used:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     print("You can't do that now.");
     print("You can't do that now.");
</source>
</syntaxhighlight>


#message can be used anywhere in the file, so you do not have to set the message before you use it.
#message can be used anywhere in the file, so you do not have to set the message before you use it.
Line 783: Line 783:
:If both objects n and m are on the screen, then  
:If both objects n and m are on the screen, then  


<source lang="cpp">vd = abs(x(n) - x(m)) + abs(y(n) - y(m))</source>
<syntaxhighlight lang="cpp">vd = abs(x(n) - x(m)) + abs(y(n) - y(m))</syntaxhighlight>


, otherwise vd = 255.
, otherwise vd = 255.
Line 849: Line 849:
:ATTENTION! Please use the following sequence of commands when loading PICTURE resources in the interpreter memory:
:ATTENTION! Please use the following sequence of commands when loading PICTURE resources in the interpreter memory:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     load.pic(n);
     load.pic(n);
     draw.pic(n);
     draw.pic(n);
Line 855: Line 855:
     ...
     ...
     show.pic();
     show.pic();
</source>
</syntaxhighlight>


:Any other order may crash the interpreter without any diagnostic messages.
:Any other order may crash the interpreter without any diagnostic messages.
Line 1,219: Line 1,219:
:Example:
:Example:


<source lang="cpp">
<syntaxhighlight lang="cpp">
     set.string(s1,"test");
     set.string(s1,"test");
     unknown170(1);
     unknown170(1);
</source>
</syntaxhighlight>


:will automatically load the savegame named "test".
:will automatically load the savegame named "test".
Line 1,253: Line 1,253:
From the book:
From the book:


<source lang="cpp">
<syntaxhighlight lang="cpp">
animate.obj(smoke);
animate.obj(smoke);
ignore.horizon(smoke);
ignore.horizon(smoke);
Line 1,264: Line 1,264:
cycle.time(smoke, work);
cycle.time(smoke, work);
draw(smoke);
draw(smoke);
</source>
</syntaxhighlight>


From the game:
From the game:


<source lang="cpp">
<syntaxhighlight lang="cpp">
animate.obj(7);
animate.obj(7);
ignore.horizon(7);
ignore.horizon(7);
Line 1,281: Line 1,281:
cycle.time(7, 152);
cycle.time(7, 152);
draw(7);
draw(7);
</source>
</syntaxhighlight>


Opening the door
Opening the door
Line 1,287: Line 1,287:
From the book:
From the book:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (said( open, door)) {    // must be close enough
if (said( open, door)) {    // must be close enough
   if (posn( ego, 86, 120, 106, 133)) {
   if (posn( ego, 86, 120, 106, 133)) {
Line 1,309: Line 1,309:
   }
   }
}
}
</source>
</syntaxhighlight>


From the game:
From the game:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (said(OPEN, DOOR||DOORS||DOORWAY||DOORWAYS)) {
if (said(OPEN, DOOR||DOORS||DOORWAY||DOORWAYS)) {
   if (posn(0, 86, 120, 106, 133)) {
   if (posn(0, 86, 120, 106, 133)) {
Line 1,339: Line 1,339:
   }
   }
}
}
</source>
</syntaxhighlight>


===Unlocking the door===
===Unlocking the door===
Line 1,345: Line 1,345:
From the book:
From the book:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (said( unlock, door)) {    // must be close enough
if (said( unlock, door)) {    // must be close enough
   if (posn( ego, 86, 120, 106, 133)) {
   if (posn( ego, 86, 120, 106, 133)) {
Line 1,359: Line 1,359:
   }
   }
}
}
</source>
</syntaxhighlight>


From the game:
From the game:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (said(UNLATCH||UNLOCK, DOOR||DOORS||DOORWAY||DOORWAYS)) {
if (said(UNLATCH||UNLOCK, DOOR||DOORS||DOORWAY||DOORWAYS)) {
   if (posn(0, 86, 120, 106, 133)) {
   if (posn(0, 86, 120, 106, 133)) {
Line 1,378: Line 1,378:
   }
   }
}
}
</source>
</syntaxhighlight>


===Knocking on the door===
===Knocking on the door===
Line 1,384: Line 1,384:
From the book:
From the book:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if ((said( knock, at, door) || said( knock) ||
if ((said( knock, at, door) || said( knock) ||
     said( knock, on, door) || said( knock, door)) {
     said( knock, on, door) || said( knock, door)) {
Line 1,401: Line 1,401:
   }
   }
}
}
</source>
</syntaxhighlight>


From the game:
From the game:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (said(BANG||KNOCK||RAP||TAP) ||
if (said(BANG||KNOCK||RAP||TAP) ||
     said(BANG||KNOCK||RAP||TAP, DOOR||DOORS||DOORWAY||DOORWAYS)) {
     said(BANG||KNOCK||RAP||TAP, DOOR||DOORS||DOORWAY||DOORWAYS)) {
Line 1,423: Line 1,423:
   }
   }
}
}
</source>
</syntaxhighlight>


===Fall rocks===
===Fall rocks===
Line 1,433: Line 1,433:
From the book:
From the book:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (hit.special) {
if (hit.special) {
   if ((rf2 || rf3 || rf4)) {
   if ((rf2 || rf3 || rf4)) {
Line 1,480: Line 1,480:
   }
   }
}
}
</source>
</syntaxhighlight>


From the game:
From the game:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (isset(3)) {    [ hit.special
if (isset(3)) {    [ hit.special
   if (isset(222) || isset(223) || isset(224)) {    // rf2, rf3, rf4
   if (isset(222) || isset(223) || isset(224)) {    // rf2, rf3, rf4
Line 1,534: Line 1,534:
   }
   }
}
}
</source>
</syntaxhighlight>
TrustedUser
2,147

edits

Navigation menu