ZoriaRPG

Debug Cheat Shell (v1.11.2, Enqueued)

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