Advertisement
ZoriaRPG

Debug Cheat Shell (v1.12)

Nov 1st, 2018
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 47.38 KB | None | 0 0
  1. /////////////////////////////////
  2. /// Debug Shell for ZC Quests ///
  3. /// Alpha Version 1.12.0      ///
  4. /// 1st November, 2018        ///
  5. /// By: ZoriaRPG              ///
  6. /// Requires: ZC Necromancer  ///
  7. /////////////////////////////////
  8. //
  9. // v1.2   : Finished working code. now it all functions as I intend.
  10. // v1.2.1 : Added a code comment block with examples on how to add more instructions to match_instruction().
  11. // v1.2.1 : Added a sanity check to setting Link->Item[]. It now only works on inventory items.
  12. // v1.3.0 : Added the SAVE instruction.
  13. // v1.4.0 : Added CREATEITEM ( cri,id,x,y )
  14. // v1.4.0 : Added CREATENPC ( crn,id,x,y )
  15. // v1.4.0 : Fixed bug where buffer persists through saves.
  16. // v1.5.0 : Added LX and LY as literal args for Link's X and Y positions.
  17. // v1.6.0 : Added LX and LY tracing.
  18. // v1.6.0 : Added PALETTE as pal,n1,n2 -- POS now requires more than 'p' -- to change DMap Palette. -1 for current DMap.
  19. // v1.6.0 :       This sets Game->DmapPalette[n1] = n2
  20. // v1.6.0 : Added MONOCHROME as mon,n to set Graphics->Monochrome(n1)
  21. // v1.6.1 : Added break instructiosn to fix invalid rval and other invalid returns in switch statements.
  22. // v1.6.2 : Added clear instructions to case for NONE in switch(instr).
  23. // v1.7.0 : Added LTRIFORCE to set if Link has the triforce for a given level as 'lt,id,true|false'
  24. // v1.7.0 : Added LCOMPASS to set if Link has the compass for a given level as 'lc,id,true|false'
  25. // v1.7.0 : Added LMAP to set if Link has the map for a given level as 'lm,id,true|false'
  26. // v1.7.0 : Added LBOSSKEY to set if Link has the boss key for a given level as 'lb,id,true|false'
  27. // v1.7.0 : Added LKEYS to set the current number of LEVEL KEYS for a given level ID as 'lk,levelid,number'
  28. // v1.7.0 : Added BOMBS to set the current number of bombs as 'b,number'
  29. // v1.7.0 : Added MBOMBS to set the current number of max bombs as 'b,number'
  30. // v1.7.0 : Added ARROWS to set the current number of arrows as 'a,number'
  31. // v1.7.0 : Added MARROWS to set the current number of max arrows as 'a,number'
  32. // v1.7.0 : Added RUPEES to set the current number of rupees as 'r,number'
  33. // v1.7.0 : Added MRUPEES to set the current number of max rupees as 'r,number'
  34. // v1.7.0 : Added KEYS to set the current number of keys as 'k,number'
  35. // v1.7.0 : Added BIGHITBOX to set the if Link's hitbox is large (full tile collision), or small, as 'h,t|f'
  36. // v1.7.0 : Added LINKDIAGONAL to set the if Link may move diagonally, as 'd,t|f'
  37. // v1.7.1 : orrected a bug where RUPEES was using Game->Counter[RUPEES} insead of CR_RUPEES.
  38. // v1.7.1 : Added a NULL case for token[1] of command 'r', so that 'r' without any other legal char as the next token is RUPEES.
  39. // v1.8.0 : Added SETFFSCRIPT as 'fs,ffc_id,script_id'
  40. // v1.8.0 : Added SETFFCDATA as 'fs,ffc_id,combo_id'
  41. // v1.8.0 : Added RUNFFCSCRIPTID as 'run,script_id'
  42. // v1.8.0 : Fixed missing break statements in execute() switch(instr).
  43. // v1.9.0 : Added HUE as 'hu,r,g,b,t|f'
  44. // v1.9.0 : Added TINT as 't,r,g,b'
  45. // v1.9.0 : Added CLEARTINT as 'cl'
  46. /* v1.10.0 :
  47.     Added all FFC vars:
  48.     fc FCSET; fx FX; fy FY; fvx FVX; fvy FVY; fax FAX ; fay FAY; ffl FFLAGS; fth FTHEIGHT; ftw FTWIDTH ; feh FEHEIGHT ; few FEWIDTH
  49.     fl FLINK; fm FMISC
  50.     Added PLAYSOUND as 'pls,sound_id'
  51.     Added PLAYMIDI as 'plm,midi_id'
  52.     Added DMAPMIDI as 'dmm,dmap_id,midi_id'
  53. */
  54. // v1.10.1 : Added KEY_STOP (period) to the list of legal keys, to permit floating point values.
  55. // v1.10.1 : Fixed a bug where instructions missing params would not abort, and clear Start presses on abort().
  56. /* v1.11.0 :
  57.     The key used to open the shell is now a config option, with a base setting of F7.
  58.     Added an instruction QUEUE. You can now store up to 20 instructions.
  59.     Press the DOWN ARROW KEY to store an instruction.
  60.     Press the ENTER key on an empty line to process all stored instructions.
  61. */
  62. /* v1.11.1 :
  63.     Fixed some issues with the number of ENQUEUED instructions being offset based on whether the user tried
  64.     to press the ENTER key on an empty line, or on a line with an instruction.
  65. */
  66. /* v1.11.2 : Further patches to enqueued counts and behaviour.
  67.     Both of these now work properly, without any error codes.
  68.     I converted 'bool type()' into 'int type()' and now I return three possible conditions:
  69.         NONE: The user escaped out of the shell, or there were no instructions to process on pressing ENTER.
  70.         ENQUEUE: There are instructions in the queue.
  71.         RAW : This is when there is only one instruction, and no prior instructions entered,
  72.             and the user presses ENTER.
  73. */
  74.  
  75. // v1.12.0 : Added 'h,value' for SETLIFE.
  76. // v1.12.0 : Added 'm,value' for SETMAGIC.
  77. // v1.12.0 : Added 'co,counter_id,value' for SETCOUNTER.
  78. // v1.12.0 : Changed BIGHITBOX from 'h,t|f' to 'hb,t|f'.
  79.  
  80.  
  81. import "std.zh"
  82.  
  83. /*
  84. DEFINED INSTRUCTION VALUES
  85.     w WARP: return 2;   //dmap,screen
  86.     p POS: return 2;        //x,y
  87.     mx MOVEX: return 1;     //pixels (+/-)
  88.     my MOVEY: return 1;     //pixels (+/-)
  89.     rh REFILLHP: return 0;  //aNONE
  90.     rm REFILLMP: return 0;  //NONE
  91.     rc REFILLCTR: return 1; //counter
  92.     mh MAXHP: return 1; //amount
  93.     mm MAXMP: return 1; //amount
  94.     mc MAXCTR: return 2;    //counter, amount
  95.     save SAVE: return 0;   
  96.     cri CREATEITEM: return 3;   //id, x, y
  97.     crn CREATENPC: return 3;    //id, x, y
  98.    
  99.     inv INVINCIBLE: return 1;   //(BOOL) on / off
  100.     itm LINKITEM: return 2; //item, (BOOL), on / off
  101.     pal PALETTE return 2
  102.     mon MONOCHROME return 1
  103.    
  104.     h BIGHITBOX
  105.     d LINKDIAGONAL
  106.    
  107.     a ARROWS
  108.     b BOMBS
  109.     r RUPEES
  110.     mb MAXBOMBS
  111.     ma MAXARROWS
  112.     mr MAXRUPEES
  113.     k KEYS
  114.     lk LKEYS
  115.     lm LMAP
  116.     lc LCOMPASS
  117.     lt LTRIFORCE
  118.    
  119.     hu HUE
  120.     t TINT
  121.     cl CLEARTINT
  122.    
  123.     fc FCSET
  124.     fx FX
  125.     fy FY
  126.     fvx FVX
  127.     fvy FVY
  128.     fax FAX
  129.     fay FAY
  130.     ffl FFLAGS
  131.     fth FTHEIGHT
  132.     ftw FTWIDTH
  133.     feh FEHEIGHT
  134.     few FEWIDTH
  135.     fl FLINK
  136.     fm FMISC
  137.    
  138.     pls PLAYSOUND
  139.     plm PLAYMIDI
  140.     dmm DMAPMIDI
  141.  
  142. //COMMAND LIST
  143.     w: Warp Link to a specific dmap and screen
  144.     p: Reposition Link on the screen.
  145.     mx: Move link by +/-n pixels on the X axis.
  146.     my: Move link by +/-n pixels on the Y axis.
  147.     rh: Refill Link's HP to Max.
  148.     rm: Refill Link's HP to Max.
  149.     rc: Refill a specific counter to Max.
  150.     mh: Set Link's Max HP.
  151.     mm: Set Link's Max MP
  152.     mc: Set the maximum value of a specific counter.
  153.     inv: Set Link's Invisible state.
  154.     itm: Set the state of a specific item in Link's inventory.
  155.     save: Save the game.
  156.     cri: Create an item.
  157.     crn: Create an npc.
  158.     pal: Change a DMap palette; -1 for current dmap.
  159.     mono: Set monochrome effect.
  160.    
  161.     h: Set if Link uses a full tile hitbox. (hitbox)
  162.     d: Set if Link can move diagonally.
  163.    
  164.     a: Set the current number of Arrows
  165.     b: Set the current number of Bombs
  166.     r: Set the current number of Rupees
  167.     mb: Set the current number of Max Bombs
  168.     ma: Set the current number of Max Arrows
  169.     mr: Set the current number of Max Rupees
  170.     k: Set the current number of Keys
  171.     lk: Set the current number of Level Keys for a specific level ID.
  172.     lm: Set if the MAP item for a specific Level is in inventory.
  173.     lc: Set if the COMPASS item for a specific Level is in inventory.
  174.     lt: Set if the TRIFORCE item for a specific Level is in inventory.
  175.     lb: Set if the BOSS KEy item for a specific Level is in inventory.
  176.    
  177.     fd: Set the Data value of one ffc.
  178.     fs: Set the Script value of one ffc.
  179.     run: Attempt to run an ffc script.
  180.    
  181.     hu: Set a specific hue effect.
  182.     t: Set a specific tint effect.
  183.     cl: Clear hue/tint.
  184.    
  185.     fc: Set the CSet of an ffc.
  186.     fx: Set the X component of an ffc.
  187.     fy: Set the Y component of an ffc.
  188.     fvx: Set the X Velocity Component of an ffc.
  189.     fvy: Set the Y Velocity Component of an ffc.
  190.     fax: Set the X Accel. Component of an ffc.
  191.     fay: Set the XYAccel. Component of an ffc.
  192.    
  193.     ffl: Set an ffc flag state true or false.
  194.     fth: Set the TileHeight of an ffc.
  195.     ftw: Set the TileWidth of an ffc.
  196.     feh: Set the EffectHeight of an ffc.
  197.     few: Set the EffectWidth of an ffc.
  198.     fl: Link an ffc to another, or clear a link.
  199.     fm: Write to the Misc[] values of an ffc.
  200.    
  201.     pls: Play a sound effect.
  202.     plm: Play a MIDI.
  203.     dmm: Set the MIDI for a specific DMap to a desired ID.
  204.    
  205. //SYNTAX
  206. //command,args
  207.     w,1,2
  208.     p,1,2
  209.     mx,1
  210.     mx,-1
  211.     my,1
  212.     my,-1
  213.     rh
  214.     rm
  215.     rc,1
  216.     mh,1
  217.     mm,1
  218.     mc,1,2
  219.     inv,true
  220.     inv,false
  221.     itm,1,true
  222.     itm,1,false
  223.     save
  224.     cri,1,2,3 //id,x,y
  225.     crn,1,2,3 //id,x,y
  226.     pal,1,2 //dmap (-1 for current), palette
  227.     mono,1 : mono,type
  228.     h,true|false
  229.     d,true|false
  230.    
  231.     a,1
  232.     b,1
  233.     r,1
  234.     mb,1
  235.     ma,1
  236.     mr,1
  237.     k,1
  238.     lk,1,2 (level id, number)
  239.     lm,1,t|f (level id, true|false)
  240.     lc,1,t|f (level id, true|false)
  241.     lt,1,t|f (level id, true|false)
  242.     lb,1,t|f (level id, true|false)
  243.    
  244.     fd,1,2 (fs,ffc_id,combo_id)
  245.     fs,1,2 (fs,ffc_id,script_id)
  246.     run,1 (run,ffc_script_id)
  247.    
  248.     hu,1,2,3,t|f (hu,red,green,blue,true|false)
  249.     t,1,2,3 (t,red,green,blue)
  250.     cl
  251.    
  252.     fc,1,2 (fs,ffc_id,cset)
  253.     fx,1,2 (fs,ffc_id,x)
  254.     fy,1,2 (fs,ffc_id,y)
  255.     fvx,1,2 (fs,ffc_id,vx)
  256.     fvy,1,2 (fs,ffc_id,vy)
  257.     fax,1,2 (fs,ffc_id,ax)
  258.     fay,1,2 (fs,ffc_id,ay)
  259.    
  260.     ffl,1,2,t|f  (fs,ffc_id,flag,true|false)
  261.     fth,1,2 (fs,ffc_id,tileheight)
  262.     ftw,1,2 (fs,ffc_id,tilewidth)
  263.     feh,1,2 (fs,ffc_id,effectheight)
  264.     few,1,2 (fs,ffc_id,effectwidth)
  265.     fl,1,2 (fs,ffc_id,link_id)
  266.     fm,1,2,3 (fs,ffc_id,index,value)
  267.    
  268.     pls,1 (pls,sound_id)
  269.     plm,1 (pls,midi_id)
  270.     dmm,1,2 (dmm,dmap_id,midi_id)
  271.    
  272.        
  273.  
  274. */
  275.  
  276. script typedef ffc namespace;
  277. typedef const int define;
  278. typedef const int CFG;
  279.  
  280.  
  281.  
  282. namespace script debugshell
  283. {
  284.     CFG INVISIBLE_COMBO = 1;
  285.    
  286.     define INSTRUCTION_SIZE = 1; //The number of stack registers that any given *instruction* requires.
  287.     define MAX_INSTR_QUEUE = 20; //The number of instructions that can be enqueued.
  288.     define MAX_ARGS     = 4; //The maximum number of args that any instruction can use/require.
  289.     define STACK_SIZE   = 2 + ((INSTRUCTION_SIZE+MAX_ARGS)*MAX_INSTR_QUEUE);  //+2 now includes TOP
  290.     define MAX_TOKEN_LENGTH = 16;
  291.     define BUFFER_LENGTH    = 42;
  292.     int stack[STACK_SIZE];
  293.     int SP;
  294.     int ENQUEUED;
  295.     define TOP = ((INSTRUCTION_SIZE+MAX_ARGS)*MAX_INSTR_QUEUE)+1;
  296.     int debug_buffer[BUFFER_LENGTH];
  297.     define rERROR = 0;
  298.     define rRAW = 1;
  299.     define rENQUEUED = 2;
  300.    
  301.     define YES = 1;
  302.     define NO = 0;
  303.    
  304.     CFG log_actions = YES;
  305.     CFG WINDOW_F_KEY = 53; //We use F7 to open the debug window.
  306.    
  307.    
  308.     define FONT = FONT_APPLE2; //Apple II
  309.     define F_COLOUR = 0x01; //font colour, white
  310.     define F_BCOLOUR = -1; //font background colour, translucent
  311.     define W_COLOUR = 0x03; //window colour (background), black
  312.     define W_S_COLOUR = 0xC5; //window colour (background), black
  313.     define CHAR_WIDTH = 6; //5 + one space
  314.     define CHAR_HEIGHT = 9; //8 + one space
  315.     define WINDOW_X = 15; //window indent over screen
  316.     define WINDOW_Y = 19; //window indent over screen
  317.     define WINDOW_H = 50;//CHAR_WIDTH * BUFFER_LENGTH;
  318.     define WINDOW_W = 180; //CHAR_HEIGHT * 3;
  319.     define WINDOW_S_X = 12; //window indent over screen
  320.     define WINDOW_S_Y = 16; //window indent over screen
  321.     define WINDOW_S_H = 50; //CHAR_WIDTH * BUFFER_LENGTH;
  322.     define WINDOW_S_W = 180; //CHAR_HEIGHT * 3;
  323.     define CHAR_X = 2; //Initial x indent
  324.     define CHAR_Y = 2; //Initial y indent
  325.     define W_OPACITY = OP_OPAQUE; //Window translucency.
  326.     define F_OPACITY = OP_OPAQUE; //Font translucency.
  327.     define W_LAYER = 6; //window draw layer
  328.     define F_LAYER = 6; //font draw layer
  329.    
  330.     CFG KEY_DELAY = 6; //frames between keystrokes
  331.    
  332.     define TYPESFX = 63;
  333.    
  334.     void process()
  335.     {
  336.         if ( Input->ReadKey[WINDOW_F_KEY] ) //46+WINDOW_F_KEY] )
  337.         {
  338.             TraceS("Enabled Deb ug Shell");
  339.             int typeval = type();
  340.             if ( typeval == rRAW ) //maybe type should be int with 0 being no return, 1 being enqueued, and 2 being raw?
  341.             {
  342.                 TraceS("process() evaluated type() true");
  343.                 if ( !ENQUEUED )
  344.                 {
  345.                     int r = read(debug_buffer,false);
  346.                     if ( r ) execute();
  347.                 }
  348.                 else execute();
  349.             }
  350.             else if ( typeval == rENQUEUED ) //maybe type should be int with 0 being no return, 1 being enqueued, and 2 being raw?
  351.             {
  352.                 TraceS("process() evaluated type() true");
  353.                 --ENQUEUED;
  354.                 execute();
  355.             }
  356.             else
  357.             {
  358.                 TraceErrorS("type() returned: ", "false");
  359.                 Link->PressStart = false;
  360.                 Link->InputStart = false;
  361.             }
  362.         }
  363.     }
  364.    
  365.     //if ( type() execute() )
  366.     //returns true if the user presses enter
  367.     int type()
  368.     {
  369.         int frame = 0;
  370.         if ( !frame ) TraceS("Starting type()");
  371.         ++frame;
  372.         Game->TypingMode = true;
  373.         int key_timer; int buffer_pos = 0;
  374.         bool typing = true; int e;
  375.         //while(!Input->ReadKey[KEY_ENTER] || Input->ReadKey[KEY_ENTER_PAD])
  376.         while(typing)
  377.         {
  378.             if ( key_timer <= 0 )
  379.             {
  380.                 if ( Input->ReadKey[KEY_BACKSPACE] ) //backspace
  381.                 {
  382.                    
  383.                     if ( buffer_pos > 0 )
  384.                     {
  385.                         debug_buffer[buffer_pos] = 0;
  386.                         --buffer_pos;
  387.                         debug_buffer[buffer_pos] = 0;
  388.                     }
  389.                     key_timer = KEY_DELAY;
  390.                     continue;
  391.                 }
  392.                 else if ( Input->ReadKey[KEY_DOWN] )
  393.                 {
  394.                     e = enqueue();
  395.                     TraceError("type() enqueued an instruction, queue ID: ", e);
  396.                    
  397.                 }
  398.                 else if ( Input->ReadKey[KEY_ENTER] || Input->ReadKey[KEY_ENTER_PAD] )
  399.                 {
  400.                     Game->TypingMode = false;
  401.                     TraceNL(); TraceS("Read enter key, and buffer position is: "); Trace(buffer_pos); TraceNL();
  402.                     if ( !buffer_pos )
  403.                     {
  404.                         if ( !ENQUEUED ) return 0; //do not execute if there are no commands
  405.                         else return rENQUEUED;
  406.                     }
  407.                     else //we've typed something
  408.                     {
  409.                         if ( ENQUEUED )
  410.                         {
  411.                             e = enqueue(); return rENQUEUED; //also enqueue this line
  412.                         }
  413.                         else return rRAW;
  414.                     }
  415.                 }
  416.                 else if ( EscKey() )
  417.                 {
  418.                     for ( int q = 0; q < BUFFER_LENGTH; ++q ) debug_buffer[q] = 0;
  419.                     clearstack();
  420.                    
  421.                     Game->TypingMode = false;
  422.                     return 0; //exit and do not process.
  423.                 }
  424.                
  425.                 else
  426.                 {
  427.                     //else normal key
  428.                     int k;
  429.                     int LegalKeys[]=
  430.                     {
  431.                         KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H,
  432.                         KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P,
  433.                         KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U, KEY_V, KEY_W, KEY_X,
  434.                         KEY_Y, KEY_Z, KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5,
  435.                         KEY_6, KEY_7, KEY_8, KEY_9, KEY_0_PAD, KEY_1_PAD, KEY_2_PAD,
  436.                         KEY_3_PAD, KEY_4_PAD, KEY_5_PAD,
  437.                         KEY_6_PAD, KEY_7_PAD, KEY_8_PAD, KEY_9_PAD, KEY_STOP, //period
  438.                         //KEY_TILDE,
  439.                         KEY_MINUS,
  440.                         //KEY_EQUALS, KEY_OPENBRACE, KEY_CLOSEBRACE,
  441.                         //KEY_COLON, KEY_QUOTE, KEY_BACKSLASH, KEY_BACKSLASH2,
  442.                         KEY_COMMA,
  443.                         //KEY_SEMICOLON, KEY_SLASH, KEY_SPACE, KEY_SLASH_PAD,
  444.                         //KEY_ASTERISK,
  445.                         KEY_MINUS_PAD
  446.                         //KEY_PLUS_PAD, KEY_CIRCUMFLEX, KEY_COLON2, KEY_EQUALS_PAD, KEY_STOP
  447.                     };
  448.  
  449.                    
  450.                     for ( int kk = SizeOfArray(LegalKeys)-1; kk >= 0; --kk )
  451.                     {
  452.                         k = LegalKeys[kk];
  453.                         if ( Input->ReadKey[k] )
  454.                         {
  455.                             TraceS("Read a key: "); Trace(k); TraceNL();
  456.                             debug_buffer[buffer_pos] = KeyToChar(k,(Input->ReadKey[KEY_LSHIFT])||(Input->ReadKey[KEY_RSHIFT])); //Warning!: Some masking may occur. :P
  457.                             TraceNL(); TraceS(debug_buffer); TraceNL();
  458.                             ++buffer_pos;
  459.                             key_timer = KEY_DELAY;
  460.                             break;
  461.                         }
  462.                     }
  463.                    
  464.                     //continue;
  465.                 }
  466.             }
  467.             else { --key_timer; }
  468.             if ( e )
  469.             {
  470.                 clearbuffer();
  471.                 buffer_pos = 0;
  472.                 e = 0;
  473.             }
  474.             draw();
  475.             Waitframe();
  476.         }
  477.        
  478.     }
  479.    
  480.     void draw()
  481.     {
  482.         Screen->Rectangle(W_LAYER, WINDOW_S_X, WINDOW_S_Y, WINDOW_S_X+WINDOW_W, WINDOW_S_Y+WINDOW_H, W_S_COLOUR, 1, 0,0,0,true,W_OPACITY);
  483.         Screen->Rectangle(W_LAYER, WINDOW_X, WINDOW_Y, WINDOW_X+WINDOW_W, WINDOW_Y+WINDOW_H, W_COLOUR, 1, 0,0,0,true,W_OPACITY);
  484.         Screen->DrawString(F_LAYER,WINDOW_X+CHAR_X,WINDOW_Y+CHAR_Y,FONT,F_COLOUR,F_BCOLOUR,0,debug_buffer,F_OPACITY);
  485.     }
  486.    
  487.     void TraceErrorS(int s, int s2)
  488.     {
  489.         TraceS(s); TraceS(": "); TraceS(s2); TraceNL();
  490.     }
  491.    
  492.     void TraceError(int s, float v, float v2)
  493.     {
  494.         int buf[12]; int buf2[12];
  495.         ftoa(buf,v);
  496.         ftoa(buf2,v2);
  497.         TraceS(s); TraceS(": "); TraceS(buf); TraceS(", "); TraceS(buf2); TraceNL();
  498.     }
  499.    
  500.     void TraceErrorVS(int s, float v, int s2)
  501.     {
  502.         int buf[12];
  503.         ftoa(buf,v);
  504.         TraceS(s); TraceS(": "); TraceS(buf); TraceS(", "); TraceS(s2); TraceNL();
  505.     }
  506.    
  507.     //instruction       //variables
  508.     define NONE =   0;  //NONE
  509.     define WARP     =   1;  //dmap,screen
  510.     define POS  =   2;  //x,y
  511.     define MOVEX    =   3;  //pixels (+/-)
  512.     define MOVEY    =   4;  //pixels (+/-)
  513.     define REFILLHP =   5;  //aNONE
  514.     define REFILLMP =   6;  //NONE
  515.     define REFILLCTR =  7;  //counter
  516.     define MAXHP    =   8;  //amount
  517.     define MAXMP    =   9;  //amount
  518.     define MAXCTR   =   10; //counter, amount
  519.    
  520.     define INVINCIBLE =     11; //(BOOL) on / off
  521.     define LINKITEM =   12; //item, (BOOL), on / off
  522.     define SAVE =       13; //item, (BOOL), on / off
  523.     define CREATEITEM =     14; //item, (BOOL), on / off
  524.     define CREATENPC =  15; //item, (BOOL), on / off
  525.     define PALETTE =    16; //item, (BOOL), on / off
  526.     define MONOCHROME =     17; //item, (BOOL), on / off
  527.     define BOMBS =      18; //item, (BOOL), on / off
  528.     define MBOMBS =     19; //item, (BOOL), on / off
  529.     define ARROWS =     20; //item, (BOOL), on / off
  530.     define MARROWS =    21; //item, (BOOL), on / off
  531.     define KEYS =       22; //item, (BOOL), on / off
  532.     define LKEYS =      23; //item, (BOOL), on / off
  533.     define RUPEES =     24; //item, (BOOL), on / off
  534.     define MRUPEES =    25; //item, (BOOL), on / off
  535.     define LMAP =       26; //level map, level id, true|false
  536.     define LBOSSKEY =   27; //level map, level id, true|false
  537.     define BIGHITBOX =  28; //level map, level id, true|false
  538.     define LINKDIAGONAL =   29; //level map, level id, true|false
  539.     define LTRIFORCE =  30; //level map, level id, true|false
  540.     define LCOMPASS =   31; //level map, level id, true|false
  541.     define RUNFFCSCRIPTID = 32;
  542.     define SETFFSCRIPT =    33;
  543.     define SETFFDATA =  34;
  544.    
  545.     define TINT =       35;
  546.     define HUE =        36;
  547.     define CLEARTINT =  37;
  548.    
  549.     define FCSET =      38;
  550.     define FX =     39;
  551.     define FY =     40;
  552.     define FVX =        41;
  553.     define FVY =        42;
  554.     define FAX =        43;
  555.     define FAY =        44;
  556.     define FFLAGS =     45; //ffc flags
  557.     define FTHEIGHT =   46;
  558.     define FTWIDTH =    47;
  559.     define FEHEIGHT =   48;
  560.     define FEWIDTH =    49;
  561.     define FLINK =      50;
  562.     define FMISC =      51;
  563.    
  564.     define PLAYSOUND =  52;
  565.     define PLAYMIDI =   53;
  566.     define DMAPMIDI =   54;
  567.    
  568.     define SETLIFE =    55;
  569.     define SETMAGIC =   56;
  570.     define SETCOUNTER = 57;
  571.    
  572.    
  573.    
  574.    
  575.    
  576.     int num_instruction_params(int instr)
  577.     {
  578.         switch(instr)
  579.         {
  580.             //instruction       //variables
  581.             case NONE: return 0;
  582.             case WARP: return 2;    //dmap,screen
  583.             case POS: return 2;     //x,y
  584.             case MOVEX: return 1;   //pixels (+/-)
  585.             case MOVEY: return 1;   //pixels (+/-)
  586.             case REFILLHP: return 0;    //aNONE
  587.             case REFILLMP: return 0;    //NONE
  588.             case REFILLCTR: return 1;   //counter
  589.             case MAXHP: return 1;   //amount
  590.             case MAXMP: return 1;   //amount
  591.             case MAXCTR: return 2;  //counter, amount
  592.            
  593.             case INVINCIBLE: return 1;  //(BOOL) on / off
  594.             case LINKITEM: return 2;    //item, (BOOL), on / off
  595.             case SAVE: return 0;    //item, (BOOL), on / off
  596.             case CREATEITEM: return 3;  //item, (BOOL), on / off
  597.             case CREATENPC: return 3;   //item, (BOOL), on / off
  598.             case PALETTE: return 2; //item, (BOOL), on / off
  599.             case MONOCHROME: return 1;  //item, (BOOL), on / off
  600.            
  601.             case BOMBS: return 1;
  602.             case MBOMBS: return 1;
  603.             case ARROWS: return 1;
  604.             case MARROWS: return 1;
  605.             case KEYS: return 1;
  606.             case LKEYS: return 2; //level, number
  607.             case RUPEES: return 1;
  608.             case MRUPEES: return 1;
  609.             case LMAP: return 2;    //level map, level id, true|false
  610.             case LBOSSKEY: return 2;    //level bosskey, level id, true|false
  611.             case LTRIFORCE: return 2;   //level bosskey, level id, true|false
  612.             case LCOMPASS: return 2;    //level bosskey, level id, true|false
  613.             case BIGHITBOX: return 1;   //true|false
  614.             case LINKDIAGONAL: return 1;    //true|false
  615.             case RUNFFCSCRIPTID: return 1;
  616.             case SETFFSCRIPT: return 2;
  617.             case SETFFDATA: return 2;
  618.            
  619.             case TINT: return 3;
  620.             case HUE: return 4;
  621.             case CLEARTINT: return 0;
  622.            
  623.             case FCSET: return 2;
  624.             case FX: return 2;
  625.             case FCSET: return 2;
  626.             case FX: return 2;
  627.             case FY: return 2;
  628.             case FVX: return 2;
  629.             case FVY: return 2;
  630.             case FAX: return 2;
  631.             case FAY: return 2;
  632.             case FFLAGS: return 3;
  633.             case FTHEIGHT: return 2;
  634.             case FTWIDTH: return 2;
  635.             case FEHEIGHT: return 2;
  636.             case FEWIDTH: return 2;
  637.             case FLINK: return 2;
  638.             case FMISC: return 3;
  639.            
  640.             case PLAYSOUND: return 1;
  641.             case PLAYMIDI: return 1;
  642.             case DMAPMIDI: return 3;
  643.            
  644.             case SETLIFE: return 1;
  645.             case SETMAGIC: return 1;
  646.             case SETCOUNTER: return 2;
  647.    
  648.             default:
  649.             {
  650.                
  651.                 TraceError("Invalid instruction passed to stack",instr);
  652.                 clearbuffer();
  653.                 return 0;
  654.             }
  655.         }
  656.     }
  657.    
  658.    
  659.    
  660.     int match_instruction(int token)
  661.     {
  662.         TraceNL(); TraceS("Input token into match_instruction is: "); TraceS(token); TraceNL();
  663.        
  664.         TraceNL(); TraceErrorS("match_instruction() token is: ",token); TraceNL();
  665.         TraceNL(); TraceError("Matching string with strcmp to 'w': ", strcmp(token,"w")); TraceNL();
  666.        
  667.         /* ONE WAY TO DO THIS. I did this with individual characters, and switches, to minimise the checks down
  668.         to the absolute minimum. -Z
  669.        
  670.         You could add specific instructions this way, if you wish.
  671.        
  672.         if ( !(strcmp(token,"w") ) ) TraceErrorS("Token in match_instruction() matched to WARP. Token: ", token);
  673.         if ( !(strcmp(token,"W") ) ) TraceErrorS("Token in match_instruction() matched to WARP. Token: ", token);
  674.         if ( !(strcmp(token,"p") ) ) TraceErrorS("Token in match_instruction() matched to POS. Token: ", token);
  675.         if ( !(strcmp(token,"P") ) ) TraceErrorS("Token in match_instruction() matched to POS. Token: ", token);
  676.         if ( !(strcmp(token,"rh") ) ) TraceErrorS("Token in match_instruction() matched to REFILLHP. Token: ", token);
  677.         if ( !(strcmp(token,"RH") ) ) TraceErrorS("Token in match_instruction() matched to REFILLHP. Token: ", token);
  678.         if ( !(strcmp(token,"Rh") ) ) TraceErrorS("Token in match_instruction() matched to REFILLHP. Token: ", token);
  679.         if ( !(strcmp(token,"rH") ) ) TraceErrorS("Token in match_instruction() matched to REFILLHP. Token: ", token);
  680.         */
  681.        
  682.         switch(token[0])
  683.         {
  684.             //A
  685.             case 'a':
  686.             case 'A':
  687.                 return ARROWS;
  688.             //B
  689.             case 'b':
  690.             case 'B':
  691.             {
  692.                 return BOMBS;
  693.                 /*
  694.                 switch(token[1])
  695.                 {
  696.                     case 'i':
  697.                     case 'I':
  698.                         return BIGHITBOX;
  699.                     case 'o':
  700.                     case 'O':
  701.                         return BOMBS;
  702.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  703.                 }
  704.                 */
  705.             }
  706.             case 'c':
  707.             case 'C':
  708.             {
  709.                 switch(token[1])
  710.                 {
  711.                     case NULL:
  712.                     case 'o':
  713.                     case 'O':
  714.                         return SETCOUNTER;
  715.                     case 'l':
  716.                     case 'L':
  717.                         return CLEARTINT;
  718.                     case 'r':
  719.                     case 'R':
  720.                     {
  721.                         switch(token[2])
  722.                         {
  723.                             case 'i':
  724.                             case 'I':
  725.                             {
  726.                                 TraceNL(); TraceS("instr() found token 'cri'"); TraceNL(); return CREATEITEM;
  727.                             }
  728.                            
  729.                             case 'n':
  730.                             case 'N':
  731.                             {
  732.                                 TraceNL(); TraceS("instr() found token 'cri'"); TraceNL(); return CREATENPC;
  733.                             }
  734.                
  735.                             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  736.                         }
  737.                     }
  738.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  739.                 }
  740.                 break;
  741.             }
  742.             //D
  743.             case 'd':
  744.             case 'D':
  745.             {
  746.                 switch(token[1])
  747.                 {
  748.                     case NULL:
  749.                     case 'i':
  750.                     case 'I':
  751.                         return LINKDIAGONAL;
  752.                     case 'm':
  753.                     case 'M': //dmap stuff
  754.                     {
  755.                         switch(token[2])
  756.                         {
  757.                            
  758.                             case NULL:
  759.                             case 'm':
  760.                             case 'M':
  761.                                 return DMAPMIDI;
  762.                             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  763.                
  764.                         }
  765.                     }  
  766.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  767.                
  768.                 }
  769.                 break;
  770.             }
  771.             //E
  772.             //F
  773.             case 'f':
  774.             case 'F':
  775.             {
  776.                 switch(token[1])
  777.                 {
  778.                     case 'a':
  779.                     case 'A':
  780.                     {
  781.                         switch(token[2])
  782.                         {
  783.                             case 'x':
  784.                             case 'X':
  785.                                 return FAX;
  786.                             case 'Y':
  787.                             case 'y':
  788.                                 return FAY;
  789.                             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  790.                
  791.                         }
  792.                         break;
  793.                        
  794.                     }
  795.                     case 'c':
  796.                     case 'C':
  797.                         return FCSET;
  798.                     case 'd':
  799.                     case 'D':
  800.                         return SETFFDATA;
  801.                     case 'e':
  802.                     case 'E':
  803.                     {
  804.                         switch(token[2])
  805.                         {
  806.                             case 'h':
  807.                             case 'H':
  808.                                 return FEHEIGHT;
  809.                             case 'w':
  810.                             case 'W':
  811.                                 return FEWIDTH;
  812.                             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  813.                         }
  814.                         break;
  815.                     }
  816.                     case 'f':
  817.                     case 'F':
  818.                     {
  819.                         switch(token[2])
  820.                         {
  821.                             case NULL:
  822.                             case 'l':
  823.                             case 'L':
  824.                                 return FFLAGS;
  825.                            
  826.                                
  827.                             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  828.                         }
  829.                         break;
  830.                        
  831.                     }
  832.                     case 'l':
  833.                     case 'L':
  834.                         return FLINK;
  835.                     case 'M':
  836.                     case 'm':
  837.                         return FMISC;
  838.                     case 'S':
  839.                     case 's':
  840.                         return SETFFSCRIPT;
  841.                     case 't':
  842.                     case 'T':
  843.                     {
  844.                         switch(token[2])
  845.                         {
  846.                             case 'H':
  847.                             case 'h':
  848.                                 return FTHEIGHT;
  849.                             case 'w':
  850.                             case 'W':
  851.                                 return FTWIDTH;
  852.                             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  853.                         }
  854.                         break;
  855.                        
  856.                     }
  857.                     case 'v':
  858.                     case 'V':
  859.                     {
  860.                         switch(token[2])
  861.                         {
  862.                             case 'x':
  863.                             case 'X':
  864.                                 return FVX;
  865.                             case 'y':
  866.                             case 'Y':
  867.                                 return FVY;
  868.                             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  869.                         }
  870.                         break;
  871.                        
  872.                     }
  873.                     case 'x':
  874.                     case 'X':
  875.                         return FX;
  876.                     case 'y':
  877.                     case 'Y':
  878.                         return FY;
  879.                    
  880.                    
  881.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  882.                 }
  883.                 break;
  884.             }
  885.             //G
  886.             //H
  887.             case 'h':
  888.             case 'H':
  889.             {
  890.                 switch(token[1])
  891.                 {
  892.                     case NULL:
  893.                         return SETLIFE;
  894.                     case 'b':
  895.                     case 'B':
  896.                         return BIGHITBOX;
  897.                     case 'u':
  898.                     case 'U':
  899.                         return HUE;
  900.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  901.                 }
  902.             }
  903.             //I
  904.             case 'i':
  905.             case 'I':
  906.             {
  907.                 switch(token[1])
  908.                 {
  909.                     case 'n':
  910.                     case 'N':
  911.                         return INVINCIBLE;
  912.                     case 't':
  913.                     case 'T':
  914.                         return LINKITEM;
  915.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  916.                 }
  917.                 break;
  918.             }
  919.             //J
  920.             //K
  921.             case 'k':
  922.             case 'K':
  923.                 return KEYS;
  924.             //L
  925.             case 'l':
  926.             case 'L':
  927.             {
  928.                 switch(token[1])
  929.                 {
  930.                     case 'b':
  931.                     case 'B':
  932.                         return LBOSSKEY;
  933.                     case 'c':
  934.                     case 'C':
  935.                         return LCOMPASS;
  936.                     case 'K':
  937.                     case 'k':
  938.                         return LKEYS;
  939.                     case 'M':
  940.                     case 'm':
  941.                         return LMAP;
  942.                     case 't':
  943.                     case 'T':
  944.                         return LTRIFORCE;
  945.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  946.                 }
  947.                
  948.             }
  949.             //M
  950.             case 'm':
  951.             case 'M':
  952.             {
  953.                 switch(token[1])
  954.                 {
  955.                     case NULL:
  956.                         return SETMAGIC;
  957.                     case 'x':
  958.                     case 'X':
  959.                         TraceNL(); TraceS("instr() found token 'mx'"); return MOVEX;
  960.                     case 'y':
  961.                     case 'Y':
  962.                         TraceNL(); TraceS("instr() found token 'my'"); return MOVEY;
  963.                     case 'h':
  964.                     case 'H':
  965.                         return MAXHP;
  966.                     case 'm':
  967.                     case 'M':
  968.                         return MAXMP;
  969.                     case 'c':
  970.                     case 'C':
  971.                         return MAXCTR;
  972.                     case 'o':
  973.                     case 'O':
  974.                         return MONOCHROME;
  975.                     case 'b':
  976.                     case 'B':
  977.                         return MBOMBS;
  978.                    
  979.                     case 'a':
  980.                     case 'A':
  981.                         return MARROWS;
  982.                     case 'R':
  983.                     case 'r':
  984.                         return MRUPEES;
  985.                    
  986.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  987.                 }
  988.                 break;
  989.             }
  990.            
  991.             //P
  992.             case 'p':
  993.             case 'P':
  994.             {
  995.                 switch(token[1])
  996.                 {
  997.                    
  998.                     case 'a':
  999.                     case 'A':
  1000.                         TraceNL(); TraceS("instr() found token 'p'"); TraceNL(); return PALETTE;
  1001.                    
  1002.                     case 'l':
  1003.                     case 'L':
  1004.                     {
  1005.                         switch(token[2])
  1006.                         {
  1007.                             case 'm':
  1008.                             case 'M':
  1009.                                 return PLAYMIDI;
  1010.                             case 's':
  1011.                             case 'S':
  1012.                                 return PLAYSOUND;
  1013.                            
  1014.                             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  1015.                         }
  1016.                     }
  1017.                     case 'o':
  1018.                     case 'O':
  1019.                         TraceNL(); TraceS("instr() found token 'p'"); TraceNL(); return POS;
  1020.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  1021.                 }
  1022.                 break;
  1023.             }
  1024.             //Q
  1025.             //R
  1026.             case 'r':
  1027.             case 'R':
  1028.             {
  1029.                 switch(token[1])
  1030.                 {
  1031.                     case NULL:
  1032.                         return RUPEES;
  1033.                     case 'h':
  1034.                     case 'H':
  1035.                         return REFILLHP;
  1036.                     case 'm':
  1037.                     case 'M':
  1038.                         return REFILLMP;
  1039.                     case 'c':
  1040.                     case 'C':
  1041.                         return REFILLCTR;
  1042.                     case 'U':
  1043.                     case 'u':
  1044.                     {
  1045.                         switch(token[2])
  1046.                         {
  1047.                             case NULL:
  1048.                                 return RUPEES;
  1049.                             case 'n':
  1050.                             case 'N':
  1051.                                 return RUNFFCSCRIPTID;
  1052.                             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  1053.                         }
  1054.                     }
  1055.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  1056.                 }
  1057.                 break;
  1058.             }
  1059.             //S
  1060.             case 's':
  1061.             case 'S':
  1062.             {
  1063.                 switch(token[1])
  1064.                 {
  1065.                     case 'a':
  1066.                     case 'A':
  1067.                     case 'V':
  1068.                     case 'v':
  1069.                     {
  1070.                         TraceNL(); TraceS("instr() found token 'save'"); return SAVE;
  1071.                     }
  1072.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  1073.                 }
  1074.                 break;
  1075.             }
  1076.             //T
  1077.             case 't':
  1078.             case 'T':
  1079.                 return TINT;
  1080.             //U
  1081.             //V
  1082.             //W
  1083.             case 'w':
  1084.             case 'W':
  1085.                 TraceNL(); TraceS("instr() found token 'w'"); TraceNL(); return WARP;
  1086.            
  1087.             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); abort(); return 0;
  1088.         }
  1089.        
  1090.         //if ( strcmp(token,"w") == 0) { TraceNL(); TraceS("instr() found token 'w'"); return WARP; }
  1091.         //else if ( strcmp(token,"p") == 0) { TraceNL(); TraceS("instr() found token 'p'"); return POS; }
  1092.         //else if ( strcmp(token,"mx") == 0) { TraceNL(); TraceS("instr() found token 'mx'"); return MOVEX; }
  1093.         //else if ( strcmp(token,"my") == 0) return MOVEY;
  1094.         //else if ( strcmp(token,"rh") == 0) return REFILLHP;
  1095.         //else if ( strcmp(token,"rm") == 0) return REFILLMP;
  1096.         //else if ( strcmp(token,"rc") == 0) return REFILLCTR;
  1097.         //else if ( strcmp(token,"mh") == 0) return MAXHP;
  1098.         //else if ( strcmp(token,"mm") == 0) return MAXMP;
  1099.         //else if ( strcmp(token,"mc") == 0) return MAXCTR;
  1100.         //else if ( strcmp(token,"inv") == 0) return INVINCIBLE;
  1101.         //else// if ( strcmp(token,"itm") == 0) return LINKITEM;
  1102.         //else
  1103.         //{
  1104.         //  TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token);
  1105.         //  return 0;
  1106.         //}
  1107.     }
  1108.     void clearstack()
  1109.     {
  1110.         for ( int q = 0; q <= stack[TOP]; ++q ) stack[q] = 0;
  1111.         SP = 0;
  1112.         stack[TOP] = 0;
  1113.     }
  1114.     int enqueue()
  1115.     {
  1116.         TraceErrorS("enqueue() is pushing a string.", " ");
  1117.         int r = read(debug_buffer,true);
  1118.         //clearbuffer();
  1119.         ++ENQUEUED;
  1120.         TraceError("Enqueued is: ", ENQUEUED);
  1121.         TraceStack();
  1122.         TraceError("SP is now: ",SP);
  1123.         return ENQUEUED;
  1124.     }
  1125.     void TraceStack()
  1126.     {
  1127.         for ( int q = stack[TOP]; q >= 0; --q )
  1128.         TraceError("Stack register and value: ", q, stack[q]);
  1129.     }
  1130.     void abort()
  1131.     {
  1132.         clearbuffer();
  1133.         Game->TypingMode = false;
  1134.         Link->PressStart = false;
  1135.         Link->InputStart = false;
  1136.     }
  1137.     void clearbuffer()
  1138.     {
  1139.         for ( int q = 0; q < BUFFER_LENGTH; ++q ) debug_buffer[q] = 0;
  1140.     }
  1141.     int read(int str, bool enqueued)
  1142.     {
  1143.         //debug
  1144.         if ( !enqueued ) {TraceNL(); TraceS("Starting read() with an initial buffer of: "); TraceS(str); TraceNL();}
  1145.         else {TraceNL(); TraceS("read() is running from enqueue() with an initial buffer of: "); TraceS(str); TraceNL();}
  1146.         int token[16]; int input_string_pos;
  1147.         int e; int token_pos = 0; int current_param;
  1148.         for ( input_string_pos = 0; input_string_pos < MAX_TOKEN_LENGTH; ++input_string_pos )
  1149.         {
  1150.             if (str[input_string_pos] == ',' ) { ++input_string_pos; break; }
  1151.             if (str[input_string_pos] == NULL ) break;
  1152.            
  1153.             token[token_pos] = str[input_string_pos];
  1154.             ++token_pos;
  1155.            
  1156.            
  1157.             //debug
  1158.            
  1159.             //++input_string_pos; //skip the comma now. If there are no params, we'll be on NULL.
  1160.         }
  1161.         //debug
  1162.         TraceNL(); TraceS("read() token: "); TraceS(token); TraceNL();
  1163.        
  1164.         //put the instruction onto the stack.
  1165.         //Right now, we are only allowing one instruction at a time.
  1166.         //This allows for future expansion.
  1167.         stack[SP] = match_instruction(token);
  1168.         TraceNL(); TraceS("SP is: "); Trace(stack[SP]); TraceNL();
  1169.         int num_params = num_instruction_params(stack[SP]);
  1170.         TraceNL(); TraceS("Number of expected params "); Trace(num_params); TraceNL();
  1171.        
  1172.         if ( num_params )
  1173.         {
  1174.             if ( str[input_string_pos] == NULL )
  1175.             {
  1176.                 //no params.
  1177.                 TraceErrorS("Input string is missing params. Token was:", token);
  1178.                 return 0;
  1179.             }
  1180.         }
  1181.        
  1182.         ++SP; //get the stack ready for the next instruction.
  1183.         stack[TOP] = SP+1;
  1184.         //push the variables onto the stack.
  1185.         while ( current_param < num_params )  //repeat this until we are out of params
  1186.             //NOT a Do loop, because some instructions have no params!
  1187.         {
  1188.             for ( token_pos = MAX_TOKEN_LENGTH-1; token_pos >= 0; --token_pos ) token[token_pos] = 0; //clear the token
  1189.            
  1190.             //copy over new token
  1191.             token_pos = 0;
  1192.             TraceNL(); TraceS("read() is seeking for params."); TraceNL();
  1193.             int temp_max = input_string_pos+MAX_TOKEN_LENGTH;
  1194.             for ( ; input_string_pos < temp_max; ++input_string_pos )
  1195.             {
  1196.                 if (str[input_string_pos] == ',' ) { ++input_string_pos; break; }
  1197.                 if (str[input_string_pos] == NULL ) break;
  1198.                
  1199.                 token[token_pos] = str[input_string_pos];
  1200.                 ++token_pos;
  1201.                
  1202.                
  1203.                 //debug
  1204.                
  1205.                 //++input_string_pos; //skip the comma now. If there are no params, we'll be on NULL.
  1206.             }
  1207.             /*
  1208.             while( str[input_string_pos] != ',' || str[input_string_pos] != NULL ) //|| current_param >= num_params ) //token terminates on a comma, or the end of the string
  1209.             {
  1210.                 token[token_pos] = str[input_string_pos]; //store the variable into a new token
  1211.                 ++token_pos;
  1212.             }
  1213.             */
  1214.             TraceNL(); TraceS("read() is getting tval"); TraceNL();
  1215.             int tval; //value of the param
  1216.             //first check the boolean types:
  1217.             TraceNL(); TraceS("The arg token is: "); TraceS(token); TraceNL();
  1218.             if ( !isNumber(token[0]) )
  1219.             {
  1220.                 switch(token[0])
  1221.                 {
  1222.                    
  1223.                     case '-': tval = atof(token); break;
  1224.                     case '.': tval = atof(token); break;
  1225.                    
  1226.                     case 't':
  1227.                     case 'T':
  1228.                         tval = 1; break;
  1229.                     case 'f':
  1230.                     case 'F':
  1231.                         tval = 0; break;
  1232.                    
  1233.                     case 'l':
  1234.                     case 'L':
  1235.                     {
  1236.                         switch(token[1])
  1237.                         {
  1238.                             case 'x':
  1239.                             case 'X':
  1240.                             {
  1241.                                 TraceError("tval set to Link->X: ", Link->X);
  1242.                                 tval = Link->X; break;
  1243.                             }
  1244.                             case 'y':
  1245.                             case 'Y':
  1246.                             {
  1247.                                 TraceError("tval set to Link->Y: ", Link->Y);
  1248.                                 tval = Link->Y; break;
  1249.                             }
  1250.                             default: TraceErrorS("Invalid token passed as an argument for instruction: ", token); tval = 0; break;
  1251.                         }
  1252.                         break;
  1253.                     }
  1254.                    
  1255.                     default: TraceErrorS("Invalid token passed as an argument for instruction: ", token); tval = 0; break;
  1256.                 }
  1257.                 //if ( strcmp(token,"true") ) tval = 1;
  1258.                 //else if ( strcmp(token,"T") ) tval = 1;
  1259.                 //else if ( strcmp(token,"false") ) tval = 0;
  1260.                 //else if ( strcmp(token,"F") ) tval = 0;
  1261.                
  1262.             }
  1263.             else //literals
  1264.             {
  1265.                
  1266.                 tval = atof(token);
  1267.                 TraceNL(); TraceS("found a literal var of: "); Trace(tval); TraceNL();
  1268.                
  1269.             }
  1270.             //push the token value onto the stack
  1271.             stack[SP] = tval;
  1272.        
  1273.             //now out stack looks like:
  1274.            
  1275.             //: PARAMn where n is the loop iteration
  1276.             //: PARAMn where n is the loop iteration
  1277.             //: PARAMn where n is the loop iteration
  1278.             //: INSTRUCTION
  1279.            
  1280.             ++SP; //this is why the stack size must be +1 larger than the3 total number of instructions and
  1281.             //params that it can hold.
  1282.             ++current_param;
  1283.            
  1284.         } //repeat this until we are out of params
  1285.         return 1;
  1286.        
  1287.     }
  1288.    
  1289.     //void getVarValue(int str)
  1290.     //{
  1291.     //  variables[VP] = atof(str);
  1292.     //  ++VP;
  1293.     //}
  1294.    
  1295.     void execute()
  1296.     {
  1297.        
  1298.         TraceNL(); TraceS("Stack Trace");
  1299.         for ( int q = stack[TOP]; q >= 0; --q )
  1300.         {
  1301.             TraceNL(); Trace(stack[q]);
  1302.         }
  1303.        
  1304.        
  1305.         TraceNL(); TraceS("Running execute()"); TraceNL();
  1306.         int reg_ptr = 0; //read the stack starting here, until we reach TOP.
  1307.         int args[MAX_ARGS];
  1308.         //evaluate the instruction:
  1309.         int instr;
  1310.         int current_arg = 0;
  1311.         int num_of_params = 0;
  1312.         for ( ; ENQUEUED >= 0; --ENQUEUED )
  1313.         {
  1314.             current_arg = 0; //we clear this for each enqueued instruction, so that we properly place args
  1315.                     //into their positions. Otherwise, we'd be trying to store args[5] instead of [2]!
  1316.             instr = stack[reg_ptr];
  1317.             ++reg_ptr;
  1318.             num_of_params = num_instruction_params(instr);
  1319.             TraceNL(); TraceS("execute() expects number of args to be: "); Trace(num_of_params); TraceNL();
  1320.             for ( ; num_of_params > 0; --num_of_params )
  1321.             {
  1322.                 args[current_arg] = stack[reg_ptr];
  1323.                 TraceNL(); TraceS("Putting an arg on the heap. Arg value: "); Trace(args[current_arg]); TraceNL();
  1324.                 ++current_arg;
  1325.                 ++reg_ptr;
  1326.                
  1327.             }
  1328.            
  1329.             TraceNL(); TraceS("execute believes that the present instruction is: "); Trace(instr); TraceNL();
  1330.             TraceNL(); TraceS("args[0] is: "); Trace(args[0]); TraceNL();
  1331.             TraceNL(); TraceS("args[1] is: "); Trace(args[1]); TraceNL();
  1332.            
  1333.             switch(instr)
  1334.             {
  1335.                 case NONE:
  1336.                 TraceError("STACK INSTRUCTION IS INVALID: ", instr);
  1337.                 Game->TypingMode = false;
  1338.                 clearbuffer();
  1339.                 break;
  1340.                 case WARP:
  1341.                 {
  1342.                     Link->Warp(args[0],args[1]);
  1343.                     if ( log_actions ) TraceError("Cheat System Warped Link to dmap,screen:",args[0],args[1]);
  1344.                     break;
  1345.                 }
  1346.                 case POS:
  1347.                 {
  1348.                     Link->X = args[0];
  1349.                     Link->Y = args[1];
  1350.                     if ( log_actions ) TraceError("Cheat System repositioned Link to X,Y:",args[0],args[1]);
  1351.                     break;
  1352.                 }
  1353.                
  1354.                 case MOVEX:
  1355.                 {
  1356.                     Link->X += args[0];
  1357.                     if ( log_actions ) TraceError("Cheat system moved Link on his X axis by: ", args[0]);
  1358.                     break;
  1359.                 }
  1360.                 case MOVEY:
  1361.                 {
  1362.                     Link->Y += args[0];
  1363.                     if ( log_actions ) TraceError("Cheat system moved Link on his Y axis by", args[0]);
  1364.                     break;
  1365.                 }
  1366.                 case REFILLHP:
  1367.                 {
  1368.                     Link->HP =  Link->MaxHP;
  1369.                     if ( log_actions ) TraceError("Cheat system refilled Link's HP to", Link->MaxHP);
  1370.                     break;
  1371.                 }
  1372.                 case REFILLMP:
  1373.                 {
  1374.                     Link->MP =  Link->MaxMP;
  1375.                     if ( log_actions ) TraceError("Cheat system refilled Link's MP to", Link->MaxHP);
  1376.                     break;
  1377.                 }
  1378.                 case REFILLCTR:
  1379.                 {
  1380.                     Game->Counter[args[0]] =  Game->MCounter[args[0]];
  1381.                     if ( log_actions ) TraceError("Cheat system refilled Counter", args[0]);
  1382.                     break;
  1383.                 }
  1384.                 case MAXHP:
  1385.                 {
  1386.                     Game->MCounter[CR_LIFE] = args[0];
  1387.                     if ( log_actions ) TraceError("Cheat system set Link's Max HP to:",args[0]);
  1388.                     break;
  1389.                 }
  1390.                 case MAXMP:
  1391.                 {
  1392.                     Game->MCounter[CR_MAGIC] = args[0];
  1393.                     if ( log_actions ) TraceError("Cheat system set Link's Max MP to:",args[0]);
  1394.                     break;
  1395.                 }
  1396.                 case MAXCTR:
  1397.                 {
  1398.                     Game->Counter[args[0]] = args[1];
  1399.                     if ( log_actions ) TraceError("Cheat system refilled Counter (id, amount):",args[0],args[1]);
  1400.                     break;
  1401.                 }
  1402.                
  1403.                 case INVINCIBLE:
  1404.                 {
  1405.                     if ( args[0] )
  1406.                     {
  1407.                         Link->Invisible = true;
  1408.                         if ( log_actions ) TraceErrorS("Cheat system set Link's Invisibility state to ","true");
  1409.                         break;
  1410.                     }
  1411.                     else
  1412.                     {
  1413.                         Link->Invisible = false;
  1414.                         if ( log_actions ) TraceErrorS("Cheat system set Link's Invisibility state to ","false");
  1415.                         break;
  1416.                        
  1417.                     }
  1418.                    
  1419.                 }
  1420.                 case LINKITEM:
  1421.                 {
  1422.                     itemdata id = Game->LoadItemData(args[0]);
  1423.                     if ( id->Keep )
  1424.                     {
  1425.                         if ( args[1] )
  1426.                         {
  1427.                            
  1428.                             Link->Item[args[0]] = true;
  1429.                             if ( log_actions ) TraceErrorS("Cheat system set Link's Inventory Item to (item, state)","true");
  1430.                             break;
  1431.                         }
  1432.                         else
  1433.                         {
  1434.                             Link->Item[args[0]] = false;
  1435.                             if ( log_actions ) TraceErrorS("Cheat system set Link's Inventory Item to (item, state)","false");
  1436.                             break;
  1437.                            
  1438.                         }
  1439.                     }
  1440.                     else break;
  1441.                 }
  1442.                 case SAVE:
  1443.                 {
  1444.                     TraceNL(); TraceS("Cheat system is saving the game.");
  1445.                     clearbuffer();
  1446.                     Game->Save();
  1447.                     break;
  1448.                 }
  1449.                 case CREATEITEM:
  1450.                 {
  1451.                     if ( log_actions ) TraceError("Cheat system is creating item ID: ", args[0]);
  1452.                     if ( log_actions ) TraceError("Cheat system is creating item at X Position: ", args[1]);
  1453.                     if ( log_actions ) TraceError("Cheat system is creating item at Y Position: ", args[2]);
  1454.                     item cci = Screen->CreateItem(args[0]);
  1455.                     cci->X = args[1];
  1456.                     cci->Y = args[2];
  1457.                     break;
  1458.                 }
  1459.                 case CREATENPC:
  1460.                 {
  1461.                     if ( log_actions ) TraceError("Cheat system is creating npc ID: ", args[0]);
  1462.                     if ( log_actions ) TraceError("Cheat system is creating npc at X Position: ", args[1]);
  1463.                     if ( log_actions ) TraceError("Cheat system is creating npc at Y Position: ", args[2]);
  1464.                     npc ccn = Screen->CreateNPC(args[0]);
  1465.                     ccn->X = args[1];
  1466.                     ccn->Y = args[2];
  1467.                     break;
  1468.                 }
  1469.                 case PALETTE:
  1470.                 {
  1471.                     if ( args[0] < 0 )
  1472.                     {
  1473.                         Game->DMapPalette[Game->GetCurDMap()] = args[1];
  1474.                     }
  1475.                     else Game->DMapPalette[args[0]] = args[1];
  1476.                     break;
  1477.                 }
  1478.                 case MONOCHROME:
  1479.                 {
  1480.                     Graphics->Monochrome(args[0]); break;
  1481.                 }
  1482.                 case MBOMBS: Game->MCounter[CR_BOMBS] = args[0]; break;
  1483.                 case BOMBS: Game->Counter[CR_BOMBS] = args[0]; break;
  1484.                 case MARROWS: Game->MCounter[CR_ARROWS] = args[0]; break;
  1485.                 case ARROWS: Game->Counter[CR_ARROWS] = args[0]; break;
  1486.                 case KEYS: Game->Counter[CR_KEYS] = args[0]; break;
  1487.                 case RUPEES: Game->Counter[CR_RUPEES] = args[0]; break;
  1488.                 case MRUPEES: Game->MCounter[CR_RUPEES] = args[0]; break;
  1489.                
  1490.                 case LKEYS: Game->LKeys[args[0]] = args[1]; break;
  1491.                 case LINKDIAGONAL: Link->Diagonal = Cond(args[0],true,false); break;
  1492.                 case BIGHITBOX: Link->BigHitbox = Cond(args[0],true,false); break;
  1493.                
  1494.                 case LMAP:
  1495.                 {
  1496.                     if ( args[1] ) //true
  1497.                     {  
  1498.                         Game->LItems[args[0]] |= LI_MAP;
  1499.                     }
  1500.                     else Game->LItems[args[0]] &= ~LI_MAP;
  1501.                     break;
  1502.                 }
  1503.                 case LBOSSKEY:
  1504.                 {
  1505.                     if ( args[1] ) //true
  1506.                     {  
  1507.                         Game->LItems[args[0]] |= LI_BOSSKEY;
  1508.                     }
  1509.                     else Game->LItems[args[0]] &= ~LI_BOSSKEY;
  1510.                     break;
  1511.                 }
  1512.                 case LCOMPASS:
  1513.                 {
  1514.                     if ( args[1] ) //true
  1515.                     {  
  1516.                         Game->LItems[args[0]] |= LI_COMPASS;
  1517.                     }
  1518.                     else Game->LItems[args[0]] &= ~LI_COMPASS;
  1519.                     break;
  1520.                 }
  1521.                 case LTRIFORCE:
  1522.                 {
  1523.                     if ( args[1] ) //true
  1524.                     {  
  1525.                         Game->LItems[args[0]] |= LI_TRIFORCE;
  1526.                     }
  1527.                     else Game->LItems[args[0]] &= ~LI_TRIFORCE;
  1528.                     break;
  1529.                 }
  1530.                 case SETFFDATA:
  1531.                 {
  1532.                     ffc f = Screen->LoadFFC(args[0]);
  1533.                     f->Data = args[1];
  1534.                     break;
  1535.                 }
  1536.                 case SETFFSCRIPT:
  1537.                 {
  1538.                     ffc f = Screen->LoadFFC(args[0]);
  1539.                     f->Script = args[1];
  1540.                     break;
  1541.                 }
  1542.                 case RUNFFCSCRIPTID:
  1543.                 {
  1544.                     ffc f; bool running;
  1545.                     for ( int q = 1; q < 33; ++q )
  1546.                     {  
  1547.                         f = Screen->LoadFFC(args[0]);
  1548.                         if ( !f->Script )
  1549.                         {
  1550.                             if ( !f->Data ) f->Data = INVISIBLE_COMBO;
  1551.                             f->Script = args[1];
  1552.                             running = true;
  1553.                             break;
  1554.                         }
  1555.                     }
  1556.                     if ( !running ) TraceError("Cheat system could not find a free ffc for command RUN. Try FS,id,scriptid instead.",NULL);
  1557.                     break;
  1558.                 }
  1559.                 case CLEARTINT:
  1560.                 {
  1561.                     TraceError("Cheat shell is clearing all Tint().",NULL);
  1562.                     Graphics->ClearTint();
  1563.                     break;
  1564.                 }
  1565.                 case TINT:
  1566.                 {
  1567.                     TraceError("Cheat shell is setting Tint().",NULL);
  1568.                     TraceError("Tint(red) is: ",args[0]);
  1569.                     TraceError("Tint(green) is: ",args[1]);
  1570.                     TraceError("Tint(blue) is: ",args[2]);
  1571.                    
  1572.                     Graphics->Tint(args[0],args[1],args[2]);
  1573.                     break;
  1574.                 }
  1575.                 case HUE:
  1576.                 {
  1577.                     TraceError("Cheat shell is setting Hue().",NULL);
  1578.                     TraceError("Hue(red) is: ",args[0]);
  1579.                     TraceError("Hue(green) is: ",args[1]);
  1580.                     TraceError("Hue(blue) is: ",args[2]);
  1581.                     if ( args[3] ) TraceErrorS("Hue(distribution) is: ","true");
  1582.                     else TraceErrorS("Hue(distribution) is: ","false");
  1583.                    
  1584.                     Graphics->MonochromeHue(args[0],args[1],args[2],Cond(args[3],true,false));
  1585.                     break;
  1586.                 }
  1587.                 case FCSET:
  1588.                 {
  1589.                     ffc f = Screen->LoadFFC(args[0]);
  1590.                     f->CSet = args[1];
  1591.                     break;
  1592.                 }
  1593.                 case FX:
  1594.                 {
  1595.                     ffc f = Screen->LoadFFC(args[0]);
  1596.                     f->X = args[1];
  1597.                     break;
  1598.                 }  
  1599.                 case FY:
  1600.                 {
  1601.                     ffc f = Screen->LoadFFC(args[0]);
  1602.                     f->Y = args[1];
  1603.                     break;
  1604.                 }
  1605.                 case FVX:
  1606.                 {
  1607.                     ffc f = Screen->LoadFFC(args[0]);
  1608.                     f->Vx = args[1];
  1609.                     break;
  1610.                 }  
  1611.                 case FVY:
  1612.                 {
  1613.                     ffc f = Screen->LoadFFC(args[0]);
  1614.                     f->Vy = args[1];
  1615.                     break;
  1616.                 }  
  1617.                 case FAX:
  1618.                 {
  1619.                     ffc f = Screen->LoadFFC(args[0]);
  1620.                     f->Ax = args[1];
  1621.                     break;
  1622.                 }  
  1623.                 case FAY:
  1624.                 {
  1625.                     ffc f = Screen->LoadFFC(args[0]);
  1626.                     f->Ay = args[1];
  1627.                     break;
  1628.                 }  
  1629.                 case FFLAGS:
  1630.                 {
  1631.                     ffc f = Screen->LoadFFC(args[0]);
  1632.                     f->Flags[args[1]] = (args[2]);
  1633.                     break;
  1634.                 }  
  1635.                 case FTHEIGHT:
  1636.                 {
  1637.                     ffc f = Screen->LoadFFC(args[0]);
  1638.                     f->TileHeight = args[1];
  1639.                     break;
  1640.                 }  
  1641.                 case FTWIDTH:
  1642.                 {
  1643.                     ffc f = Screen->LoadFFC(args[0]);
  1644.                     f->TileWidth = args[1];
  1645.                     break;
  1646.                 }  
  1647.                 case FEHEIGHT:
  1648.                 {
  1649.                     ffc f = Screen->LoadFFC(args[0]);
  1650.                     f->EffectHeight = args[1];
  1651.                     break;
  1652.                 }  
  1653.                 case FEWIDTH:
  1654.                 {
  1655.                     ffc f = Screen->LoadFFC(args[0]);
  1656.                     f->EffectWidth = args[1];
  1657.                     break;
  1658.                 }  
  1659.                 case FLINK:
  1660.                 {
  1661.                     ffc f = Screen->LoadFFC(args[0]);
  1662.                     f->Link = args[1];
  1663.                     break;
  1664.                 }  
  1665.                 case FMISC:
  1666.                 {
  1667.                     ffc f = Screen->LoadFFC(args[0]);
  1668.                     f->Misc[args[1]] = args[2];
  1669.                     break;
  1670.                 }  
  1671.                
  1672.                 case PLAYSOUND: Game->PlaySound(args[0]); break;
  1673.                 case PLAYMIDI: Game->PlayMIDI(args[0]); break;
  1674.                 case DMAPMIDI:
  1675.                 {
  1676.                     if ( args[0] < 0 )
  1677.                     {
  1678.                         TraceError("Cheat system is setting the DMap MIDI for the current DMap to: ",args[1]);
  1679.                         Game->DMapMIDI[Game->GetCurDMap()] = args[1];
  1680.                     }
  1681.                    
  1682.                     else
  1683.                     {
  1684.                         TraceError("Cheat system is setting the DMap MIDI for the DMap: ",args[0]);
  1685.                         TraceError("...to MIDI ID: ",args[1]);
  1686.                         Game->DMapMIDI[args[0]] = args[1];
  1687.                     }
  1688.                     break;
  1689.                 }
  1690.                
  1691.                 case SETLIFE: Game->Counter[CR_LIFE] = args[0]; break;
  1692.                 case SETMAGIC: Game->Counter[CR_MAGIC] = args[0]; break;
  1693.                 case SETCOUNTER: Game->Counter[args[0]] = args[1]; break;
  1694.                    
  1695.                 default:
  1696.                 {
  1697.                    
  1698.                     TraceError("Invalid instruction passed to stack",instr);
  1699.                     break;
  1700.                 }
  1701.                
  1702.             }
  1703.         }
  1704.         ///-----later, we'll add this: //pop everything off of the stack
  1705.         //just wipe the stack for now, as we only support one command at this time
  1706.         for ( int q = 0; q <= stack[TOP]; ++q ) stack[q] = 0;
  1707.         SP = 0;
  1708.        
  1709.         //clear the main buffer, too!
  1710.         for ( int cl = 0; cl < BUFFER_LENGTH; ++cl ) debug_buffer[cl] = 0;
  1711.         Game->TypingMode = false; //insurance clear
  1712.         Link->PressStart = false;
  1713.         Link->InputStart = false;
  1714.         ENQUEUED = 0;
  1715.        
  1716.        
  1717.     }
  1718.        
  1719.     void run()
  1720.     {
  1721.    
  1722.        
  1723.        
  1724.     }
  1725. }
  1726.  
  1727. global script test
  1728. {
  1729.     void run()
  1730.     {
  1731.         debugshell.SP = 0;
  1732.         debugshell.clearbuffer();
  1733.         while(1)
  1734.         {
  1735.             debugshell.process();
  1736.             Waitdraw();
  1737.             Waitframe();
  1738.         }
  1739.        
  1740.     }
  1741. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement