### Logic Resource Header Example Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md Example of a logic resource header for KQ1 Room 2. ```text 12 34 Signature 01 vol.1 5F 06 Length = 0x065F BA 02 Text start = 0x02BA ``` -------------------------------- ### Command Argument Examples Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Demonstrates standard command usage with typed arguments and object-in-box checks. ```AGI move.obj(so4,80,120,2,f66); if (obj.in.box(so2,30,60,120,40)) { ..... ``` -------------------------------- ### Said Command Bytecode Examples Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md Shows the bytecode representation for the said command with varying numbers of arguments. ```C++ if (said(marble)) FF 0E 01 1E 01 FF if (said( open, door)) FF 0E 02 37 02 73 00 FF ``` -------------------------------- ### Sound Resource Management Commands Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Commands for starting and stopping playback of sound resources. ```AGI sound(n,m) ``` ```AGI stop.sound() ``` -------------------------------- ### If Condition Opcode Example Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md Demonstrates the use of the 0xFF opcode to initiate condition checking for flag 5. ```text FF 'if' conditions start. 07 07 = isset 05 05 = flag 5 FF 'if' conditions close. ``` -------------------------------- ### Room Logic Script Example Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md A sample logic script for King's Quest 4, Room 7, demonstrating conditional checks for player input and object positioning. ```C++ if (said( open, door)) // must be close enough { if (posn( ego, 86, 120, 106, 133)) { if (!night) { if ( door.open) { print("The door is already open"); } else { set( game.control); set.priority( ego, 11); start.update( door); end.of.loop( door, door.done); } } else { print("You can't -- it's locked"); } } else { set( notCloseEnough); } } ``` -------------------------------- ### Looping Logic in AGI Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md Examples of how loops are simulated in AGI using conditional tests and bytecode branching. ```C++ if (greatern(30, 45) && lessn(30, 55)) { print("You're in the hot zone!"); increment(30); } ``` ```C++ FF FD 0D FF 03 00 FE F7 FF do { } while (!havekey); ``` -------------------------------- ### If-Else Structure Example Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md Example showing the structure of an if-else block, noting the increased bracket distance due to the else statement. ```cpp if (isset(231)) { FF 07 E7 FF 05 00 printf("The door is already open."); 65 0F } else { FE 11 00 set(36); 0C 24 prevent.input(); 77 start.update(5); 3B 05 assignn(152, 3); 03 98 03 cycle.time(5, 152); 4C 05 98 end.of.loop(5, 232); 49 05 E8 sound(70, 154); 63 46 9A } ``` -------------------------------- ### Display AGI command trace output Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Overview.md Example of the output format generated when command tracing is enabled, showing the execution of test commands. ```Text ============================= 0: greatern (17, 0) (0, 0) :F ``` -------------------------------- ### AGI Message Print Statement Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md Example of a print command in AGI logic scripts. ```C++ if (said(look, alligators)) { print("The alligators are swimming in the moat."); } ``` -------------------------------- ### Said Command Usage Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Examples of the said command using word group numbers or literal word strings. ```AGI if (said(4,80)) { ..... if (said("look")) { ..... if (said("open","door")) { ..... ``` -------------------------------- ### AGI Test Command Logic Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Examples of combining test commands using logical operators and negation. ```AGI if (isset(f5) && greatern(v5,6)) { ...... ``` ```AGI if (lessn(v5,6) && (greatern(v5,2)) { ....... ``` ```AGI if (isset(f90) && equalv(v32,v34) && greatern(v34,20)) { ....... ``` ```AGI if (!isset(f7)) { ...... ``` ```AGI if ((isset(f1) || isset(f2)) { ...... ``` ```AGI if (isset(f1) && (isset(f2) || isset(f3))) { ...... ``` ```AGI if (isset(1) || (isset(2) && isset(3))) { // is NOT legal ``` -------------------------------- ### AGI Argument Bitmask Examples Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md Hexadecimal values representing argument type definitions for AGI commands. ```hex 0x80 ``` ```hex 0x60 ``` -------------------------------- ### If Statement Block Structure Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md Example of an if statement block including the bracket distance and command sequence. ```cpp FF 07 05 FF if (isset(5)) 84 00 { // For 0x0084 bytes. 18 00 load.pic(0); 19 00 draw.pic(0); 1B 00 discard.pic(0); ... ... } // Closed. 0x0084 bytes counted. ``` -------------------------------- ### Define Command Usage Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Shows how to create and use defines to replace raw values with descriptive names. ```AGI #define ego o0 #define room_descr "This is a large hall with tall pillars down each side." draw(ego); print(room_descr); ``` -------------------------------- ### Opening the Door Logic Comparison Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Comparison of door opening logic between the official book and the final game implementation. ```AGI if (said( open, door)) { // must be close enough if (posn( ego, 86, 120, 106, 133)) { if (!night) { if ( door.open) { print("The door is already open. . ."); } else { set( game.control); set.priority( ego, 11); start.update( door); end.of.loop( door, door.done); } } else { print("You can't -- it's locked..."); } } else { set( notCloseEnough); } } ``` ```AGI if (said(OPEN, DOOR||DOORS||DOORWAY||DOORWAYS)) { if (posn(0, 86, 120, 106, 133)) { if (!isset(38)) { if (isset(231)) { print("The door is already open."); } else { set(36); prevent.input(); start.update(5); assignn(152, 3); cycle.time(5, 152); end.of.loop(5, 232); sound(70, 154); } } else { print("You can't. It's locked and you don't have the key."); } } else { set(113); } } ``` -------------------------------- ### General VIEW Command Syntax Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/View.md The standard parameter structure for all VIEW boundary testing commands. ```C++ command(VIEW object num, left, top, right, bottom) ``` -------------------------------- ### Unlocking the Door Logic Comparison Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Comparison of door unlocking logic between the official book and the final game implementation. ```AGI if (said( unlock, door)) { // must be close enough if (posn( ego, 86, 120, 106, 133)) { if (!night) { print("The door is already unlocked. . ."); } else { printf("You can't, it's locked. . ."); } } else { set( notCloseEnough); } } ``` ```AGI if (said(UNLATCH||UNLOCK, DOOR||DOORS||DOORWAY||DOORWAYS)) { if (posn(0, 86, 120, 106, 133)) { if (!isset(38)) { print("The door is already unlocked."); } else { print("You can't. It's locked and you don't have the key."); } } else { set(113); } } ``` -------------------------------- ### Initialize an object in AGI LOGIC Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Overview.md Configures a game object's properties, including view, position, and timing, within an AGI LOGIC script. ```AGI animate.obj (smoke); ignore.horizon (smoke); set.view (smoke, v.fish.cabin); set.loop (smoke, 1); ignore.blocks (smoke); position (smoke, 95, 16); work = 3; step.time (smoke, work); cycle.time (smoke, work); draw (smoke); ``` -------------------------------- ### Labels and Goto Command Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Defines a label and uses the goto command to redirect execution flow. ```AGI Label1: goto(Label1); ``` -------------------------------- ### Conditional Logic Implementation Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md Demonstrates the high-level C++ representation of AGI if/else logic and its equivalent using goto statements. ```C++ if (said( open, door)) { // first block of AGI statements } else { // second block of AGI statements } ``` ```C++ if (!said( open, door)) goto label1; // first block of AGI statements goto label2: label1: // second block of AGI statements label2: ``` -------------------------------- ### Include external files Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Use the #include command to replace the line with the contents of the specified file. ```Sierra AGI #include "file.txt" ``` -------------------------------- ### Check for said input in C++ Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Data.md Demonstrates the usage of the said command with specific word arguments in the AGI interpreter. ```C++ if (said(take, anyword)) { print("You can't - Blackbeard has chopped both your arms off."); } ``` -------------------------------- ### Comment Syntax Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Demonstrates the three supported comment styles, including nested block comments. ```AGI // Rest of line is ignored [ Rest of line is ignored /* Text between these are ignored */ /* comment start print("Hello"); // won't be run /* // a new comment start (will be ignored!) v32 = 15; // won't be run */ // uncomments the most inner comment print("Hey!"); // won't be run, still inside comments */ // uncomments ``` -------------------------------- ### VIEW Test Command List Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/View.md The available LOGIC commands for testing VIEW object boundaries. ```C++ obj.in.box() posn() right.posn() centre.posn() ``` -------------------------------- ### Animate Smoke Object Comparison Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Comparison of smoke animation logic between the official book and the final game implementation. ```AGI animate.obj(smoke); ignore.horizon(smoke); set.view(smoke, v.fish.cabin); set.loop(smoke, 1); ignore.blocks(smoke); position(smoke, 95, 16); work = 3; step.time(smoke, work); cycle.time(smoke, work); draw(smoke); ``` ```AGI animate.obj(7); ignore.horizon(7); set.view(7, 114); set.loop(7, 1); ignore.objs(7); // These two lines have been added. set.priority(7, 5); // ignore.blocks(7); position(7, 95, 16); assignn(152, 3); // Equivalent to 'work = 3;' step.time(7, 152); cycle.time(7, 152); draw(7); ``` -------------------------------- ### Multi-line Message Formatting Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Demonstrates splitting a single message across multiple lines. ```AGI print("This message is split " "over multiple lines."); ``` -------------------------------- ### AGI Action Command Substitutions Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Shorthand syntax equivalents for common arithmetic and assignment operations in AGI scripts. ```AGI increment(v30); v30++; decrement(v30); v30--; assignn(v30,4); v30 = 4; assignv(v30,v32); v30 = v32; addn(v30,4); v30 = v30 + 4; or v30 += 4; addv(v30,v32); v30 = v30 + v32; or v30 += v32; subn(v30,4); v30 = v30 - 4; or v30 -= 4; subv(v30,v32); v30 = v30 - v32; or v30 -= v32; mul.n(v30,4); v30 = v30 * 4; or v30 *= 4; mul.v(v30,v32); v30 = v30 * v32; or v30 *= v32; div.n(v30,4); v30 = v30 / 4; or v30 /= 4; div.v(v30,v32); v30 = v30 / v32; or v30 /= v32; lindirectn(v30,4); *v30 = 4; lindirectv(v30,v32); *v30 = v32; rindirect(v30,v32); v30 = *v32; ``` -------------------------------- ### Picture Resource Management Commands Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Commands for loading, drawing, and overlaying picture resources in the interpreter buffer. ```AGI draw.pic(n) ``` ```AGI overlay.pic(n) ``` ```AGI add.to.pic(a,b,c,d,e,f,g), add.to.pic.v(a,b,c,d,e,f,g) ``` ```AGI show.pic() ``` -------------------------------- ### Program Control Commands Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Commands for changing rooms and managing interpreter state. ```APIDOC ## Program Control Commands ### new.room(n) Changes the current room to n, discards resources, resets variables, and initializes the new logic. ### new.room.v(n) Changes the current room to the value of variable vn, discards resources, resets variables, and initializes the new logic. ``` -------------------------------- ### Basic AGI Command Syntax Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Commands are defined by a name followed by arguments in brackets and terminated with a semicolon. ```AGI assign.v(v50,0); program.control(); ``` ```AGI reset(f6); reset(f7); ``` -------------------------------- ### AGI Comparison Equivalents Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Mapping of AGI test commands to standard comparison operators. ```AGI equaln(v30,4) v30 == 4 equalv(v30,v32) v30 == v32 greatern(v30,4) v30 > 4 greaterv(v30,v32) v30 > v32 lessn(v30,4) v30 < 4 lessv(v30,v32) v30 < v32 !equaln(v30,4) v30 != 4 !equalv(v30,v32) v30 != v32 !greatern(v30,4) v30 <= 4 !greaterv(v30,v32) v30 <= v32 !lessn(v30,4) v30 >= 4 !lessv(v30,v32) v30 >= v32 ``` -------------------------------- ### Implement Sierra-compatible line drawing in C Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Pic.md Uses a custom rounding function to match Sierra's pixel placement logic and handles line drawing across different axes. ```C int round(float aNumber, float dirn) { if (dirn < 0) return ((aNumber - floor(aNumber) <= 0.501) ? floor(aNumber) : ceil(aNumber)); return ((aNumber - floor(aNumber) < 0.499) ? floor(aNumber) : ceil(aNumber)); } void drawline(word x1, word y1, word x2, word y2) { int height, width; float x, y, addX, addY; height = (y2 - y1); width = (x2 - x1); addX = (height==0? height:(float)width/abs(height)); addY = (width==0? width:(float)height/abs(width)); if (abs(width) > abs(height)) { y = y1; addX = (width == 0? 0 : (width/abs(width))); for (x=x1; x!=x2; x+=addX) { pset(round(x, addX), round(y, addY)); y+=addY; } pset(x2,y2); } else { x = x1; addY = (height == 0? 0 : (height/abs(height))); for (y=y1; y!=y2; y+=addY) { pset(round(x, addX), round(y, addY)); x+=addX; } pset(x2,y2); } } ``` -------------------------------- ### Message and Inventory Item Usage Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Shows how to reference messages and inventory items using either literal strings or numerical/variable identifiers. ```AGI print("He's not here."); print(m12); if (has("Jetpack")) { ..... if (has(io9)) { ..... ``` -------------------------------- ### AGI Implementation: equaln Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md 8086 assembly implementation for the equaln command. ```ASM equaln (eg. if (work = 3) ) 0D71 AC LODSB ;get variable number 0D72 32FF XOR BH,BH 0D74 8AD8 MOV BL,AL 0D76 AC LODSB ;get test number 0D77 3A870900 CMP AL,[BX+0009] ;test if var = number 0D7B B000 MOV AL,00 ;return 0 if not equal 0D7D 7502 JNZ 0D81 0D7F FEC0 INC AL ;return 1 if equal 0D81 C3 RET ``` -------------------------------- ### Resource Management Commands Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Commands to load and discard resources into the interpreter's memory. ```APIDOC ## Resource Management Commands ### load.logic(n) Loads LOGIC resource number n into memory. ### load.logic.v(n) Loads LOGIC resource number i into memory, where i is the value of variable vn. ### load.pic(n) Loads PICTURE resource number i into memory, where i is the value of vn. ### load.view(n) Loads VIEW resource number n into memory. ### load.view.v(n) Loads VIEW resource number i into memory, where i is the value of vn. ### load.sound(n) Loads SOUND resource number n into memory. ### discard.pic(n) Unloads PICTURE resource number i, where i is the value of vn. ### discard.view(n) Unloads VIEW resource number n. ### discard.view.v(n) Unloads VIEW resource number i, where i is the value of vn. ``` -------------------------------- ### Play animation once using flags Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Other.md Uses a flag to track animation completion, returning control to the interpreter between cycles to avoid blocking. ```AGI if_ not_ isset 121 else_ A end_of_loop 1, 121 return A: .............. ``` -------------------------------- ### Evaluate said command logic Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Internals.md The interpreter checks the input sequence against parameters W(i) using specific matching rules for wildcards and exact codes. ```pseudocode If f2 == 0 or f4 == 1, return FALSE. ``` -------------------------------- ### AGI Implementation: equalv Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Resources.md 8086 assembly implementation for the equalv command. ```ASM equalv (eg. if (work = maxwork) ) 0D82 AC LODSB ;get first var number 0D83 32FF XOR BH,BH ;clear bh 0D85 8AD8 MOV BL,AL ;BX = variable number 0D87 8AA70900 MOV AH,[BX+0009] ;get first var value 0D8B AC LODSB ;get second var number 0D8C 8AD8 MOV BL,AL 0D8E 32C0 XOR AL,AL ;return 0 if not equal 0D90 3AA70900 CMP AH,[BX+0009] ;compare variables 0D94 7502 JNZ 0D98 0D96 FEC0 INC AL ;return 1 if equal 0D98 C3 RET ``` -------------------------------- ### Subroutine Call Commands Source: https://github.com/brushesm/sierra-agi-docs/blob/main/Sierra_AGI_Specification/Logic.md Commands for executing logic resources as subroutines and managing execution flow. ```APIDOC ## Subroutine Call Commands ### call(n) Executes LOGIC resource n as a subroutine. ### call.v(n) Executes LOGIC resource i as a subroutine, where i is the value of vn. ### return Returns control to the caller or the interpreter. ### jump