Advertisement
ZoriaRPG

Debug Cheat Shell (v1.6.2, Working)

Oct 30th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.45 KB | None | 0 0
  1. /////////////////////////////////
  2. /// Debug Shell for ZC Quests ///
  3. /// Alpha Version 1.6.2       ///
  4. /// 31st October, 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.  
  24. import "std.zh"
  25.  
  26. /*
  27. DEFINED INSTRUCTION VALUES
  28.     w WARP: return 2;   //dmap,screen
  29.     p POS: return 2;        //x,y
  30.     mx MOVEX: return 1;     //pixels (+/-)
  31.     my MOVEY: return 1;     //pixels (+/-)
  32.     rh REFILLHP: return 0;  //aNONE
  33.     rm REFILLMP: return 0;  //NONE
  34.     rc REFILLCTR: return 1; //counter
  35.     mh MAXHP: return 1; //amount
  36.     mm MAXMP: return 1; //amount
  37.     mc MAXCTR: return 2;    //counter, amount
  38.     save SAVE: return 0;   
  39.     cri CREATEITEM: return 3;   //id, x, y
  40.     crn CREATENPC: return 3;    //id, x, y
  41.    
  42.     inv INVINCIBLE: return 1;   //(BOOL) on / off
  43.     itm LINKITEM: return 2; //item, (BOOL), on / off
  44.     pal PALETTE return 2
  45.     mon MONOCHROME return 1
  46.  
  47. //COMMAND LIST
  48.     w: Warp Link to a specific dmap and screen
  49.     p: Reposition Link on the screen.
  50.     mx: Move link by +/-n pixels on the X axis.
  51.     my: Move link by +/-n pixels on the Y axis.
  52.     rh: Refill Link's HP to Max.
  53.     rm: Refill Link's HP to Max.
  54.     rc: Refill a specific counter to Max.
  55.     mh: Set Link's Max HP.
  56.     mm: Set Link's Max MP
  57.     mc: Set the maximum value of a specific counter.
  58.     inv: Set Link's Invisible state.
  59.     itm: Set the state of a specific item in Link's inventory.
  60.     save: Save the game.
  61.     cri: Create an item.
  62.     crn: Create an npc.
  63.     pal: Change a DMap palette; -1 for current dmap.
  64.     mono: Set monochrome effect.
  65. //SYNTAX
  66. //command,args
  67.     w,1,2
  68.     p,1,2
  69.     mx,1
  70.     mx,-1
  71.     my,1
  72.     my,-1
  73.     rh
  74.     rm
  75.     rc,1
  76.     mh,1
  77.     mm,1
  78.     mc,1,2
  79.     inv,true
  80.     inv,false
  81.     itm,1,true
  82.     itm,1,false
  83.     save
  84.     cri,1,2,3 //id,x,y
  85.     crn,1,2,3 //id,x,y
  86.     pal,1,2 //dmap (-1 for current), palette
  87.     mono,1 : mono,type
  88.        
  89.        
  90.  
  91. */
  92.  
  93. script typedef ffc namespace;
  94. typedef const int define;
  95. typedef const int CFG;
  96.  
  97.  
  98.  
  99. namespace script debugshell
  100. {
  101.     define INSTRUCTION_SIZE = 1; //The number of stack registers that any given *instruction* requires.
  102.     define MAX_INSTR_QUEUE = 1; //The number of instructions that can be enqueued.
  103.     define MAX_ARGS     = 3; //The maximum number of args that any instruction can use/require.
  104.     define STACK_SIZE   = 1 + ((INSTRUCTION_SIZE+MAX_ARGS)*MAX_INSTR_QUEUE);
  105.     define MAX_TOKEN_LENGTH = 16;
  106.     define BUFFER_LENGTH    = 42;
  107.     int stack[STACK_SIZE];
  108.     int SP;
  109.    
  110.     int debug_buffer[BUFFER_LENGTH];
  111.    
  112.     define YES = 1;
  113.     define NO = 0;
  114.    
  115.     CFG log_actions = YES;
  116.     CFG WINDOW_F_KEY = 7; //We use F7 to open the debug window.
  117.    
  118.    
  119.     define FONT = FONT_APPLE2; //Apple II
  120.     define F_COLOUR = 0x01; //font colour, white
  121.     define F_BCOLOUR = -1; //font background colour, translucent
  122.     define W_COLOUR = 0x03; //window colour (background), black
  123.     define W_S_COLOUR = 0xC5; //window colour (background), black
  124.     define CHAR_WIDTH = 6; //5 + one space
  125.     define CHAR_HEIGHT = 9; //8 + one space
  126.     define WINDOW_X = 15; //window indent over screen
  127.     define WINDOW_Y = 19; //window indent over screen
  128.     define WINDOW_H = 50;//CHAR_WIDTH * BUFFER_LENGTH;
  129.     define WINDOW_W = 180; //CHAR_HEIGHT * 3;
  130.     define WINDOW_S_X = 12; //window indent over screen
  131.     define WINDOW_S_Y = 16; //window indent over screen
  132.     define WINDOW_S_H = 50; //CHAR_WIDTH * BUFFER_LENGTH;
  133.     define WINDOW_S_W = 180; //CHAR_HEIGHT * 3;
  134.     define CHAR_X = 2; //Initial x indent
  135.     define CHAR_Y = 2; //Initial y indent
  136.     define W_OPACITY = OP_OPAQUE; //Window translucency.
  137.     define F_OPACITY = OP_OPAQUE; //Font translucency.
  138.     define W_LAYER = 6; //window draw layer
  139.     define F_LAYER = 6; //font draw layer
  140.    
  141.     CFG KEY_DELAY = 6; //frames between keystrokes
  142.    
  143.     define TYPESFX = 63;
  144.    
  145.     void process()
  146.     {
  147.         if ( Input->Key[51] ) //46+WINDOW_F_KEY] )
  148.         {
  149.             TraceS("Enabled Deb ug Shell");
  150.             if ( type() )
  151.             {
  152.                 TraceS("process() evaluated type() true");
  153.                
  154.                 int r = read(debug_buffer);
  155.                 if ( r ) execute();
  156.             }
  157.         }
  158.     }
  159.    
  160.     //if ( type() execute() )
  161.     //returns true if the user presses enter
  162.     bool type()
  163.     {
  164.         int frame = 0;
  165.         if ( !frame ) TraceS("Starting type()");
  166.         ++frame;
  167.         Game->TypingMode = true;
  168.         int key_timer; int buffer_pos = 0;
  169.         bool typing = true;
  170.         //while(!Input->ReadKey[KEY_ENTER] || Input->ReadKey[KEY_ENTER_PAD])
  171.         while(typing)
  172.         {
  173.             if ( key_timer <= 0 )
  174.             {
  175.                 if ( Input->ReadKey[KEY_BACKSPACE] ) //backspace
  176.                 {
  177.                    
  178.                     if ( buffer_pos > 0 )
  179.                     {
  180.                         debug_buffer[buffer_pos] = 0;
  181.                         --buffer_pos;
  182.                         debug_buffer[buffer_pos] = 0;
  183.                     }
  184.                     key_timer = KEY_DELAY;
  185.                     continue;
  186.                 }
  187.                 else if ( Input->ReadKey[KEY_ENTER] || Input->ReadKey[KEY_ENTER_PAD] )
  188.                 {
  189.                     Game->TypingMode = false;
  190.                     TraceNL(); TraceS("Read enter key, and buffer position is: "); Trace(buffer_pos); TraceNL();
  191.                     if ( !buffer_pos ) return false; //do not execute if there are no commands
  192.                     return true;
  193.                 }
  194.                 else if ( EscKey() )
  195.                 {
  196.                     for ( int q = 0; q < BUFFER_LENGTH; ++q ) debug_buffer[q] = 0;
  197.                    
  198.                    
  199.                     Game->TypingMode = false;
  200.                     return false; //exit and do not process.
  201.                 }
  202.                
  203.                 else
  204.                 {
  205.                     //else normal key
  206.                     int k;
  207.                     int LegalKeys[]=
  208.                     {
  209.                         KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H,
  210.                         KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P,
  211.                         KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U, KEY_V, KEY_W, KEY_X,
  212.                         KEY_Y, KEY_Z, KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5,
  213.                         KEY_6, KEY_7, KEY_8, KEY_9, KEY_0_PAD, KEY_1_PAD, KEY_2_PAD,
  214.                         KEY_3_PAD, KEY_4_PAD, KEY_5_PAD,
  215.                         KEY_6_PAD, KEY_7_PAD, KEY_8_PAD, KEY_9_PAD,
  216.                         //KEY_TILDE,
  217.                         KEY_MINUS,
  218.                         //KEY_EQUALS, KEY_OPENBRACE, KEY_CLOSEBRACE,
  219.                         //KEY_COLON, KEY_QUOTE, KEY_BACKSLASH, KEY_BACKSLASH2,
  220.                         KEY_COMMA,
  221.                         //KEY_SEMICOLON, KEY_SLASH, KEY_SPACE, KEY_SLASH_PAD,
  222.                         //KEY_ASTERISK,
  223.                         KEY_MINUS_PAD
  224.                         //KEY_PLUS_PAD, KEY_CIRCUMFLEX, KEY_COLON2, KEY_EQUALS_PAD, KEY_STOP
  225.                     };
  226.  
  227.                    
  228.                     for ( int kk = SizeOfArray(LegalKeys)-1; kk >= 0; --kk )
  229.                     {
  230.                         k = LegalKeys[kk];
  231.                         if ( Input->ReadKey[k] )
  232.                         {
  233.                             TraceS("Read a key: "); Trace(k); TraceNL();
  234.                             debug_buffer[buffer_pos] = KeyToChar(k,(Input->ReadKey[KEY_LSHIFT])||(Input->ReadKey[KEY_RSHIFT])); //Warning!: Some masking may occur. :P
  235.                             TraceNL(); TraceS(debug_buffer); TraceNL();
  236.                             ++buffer_pos;
  237.                             key_timer = KEY_DELAY;
  238.                             break;
  239.                         }
  240.                     }
  241.                    
  242.                     //continue;
  243.                 }
  244.             }
  245.             else { --key_timer; }
  246.            
  247.             draw();
  248.             Waitframe();
  249.         }
  250.        
  251.     }
  252.    
  253.     void draw()
  254.     {
  255.         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);
  256.         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);
  257.         Screen->DrawString(F_LAYER,WINDOW_X+CHAR_X,WINDOW_Y+CHAR_Y,FONT,F_COLOUR,F_BCOLOUR,0,debug_buffer,F_OPACITY);
  258.     }
  259.    
  260.     void TraceErrorS(int s, int s2)
  261.     {
  262.         TraceS(s); TraceS(": "); TraceS(s2); TraceNL();
  263.     }
  264.    
  265.     void TraceError(int s, float v, float v2)
  266.     {
  267.         int buf[12]; int buf2[12];
  268.         ftoa(buf,v);
  269.         ftoa(buf2,v2);
  270.         TraceS(s); TraceS(": "); TraceS(buf); TraceS(", "); TraceS(buf2); TraceNL();
  271.     }
  272.    
  273.     void TraceErrorVS(int s, float v, int s2)
  274.     {
  275.         int buf[12];
  276.         ftoa(buf,v);
  277.         TraceS(s); TraceS(": "); TraceS(buf); TraceS(", "); TraceS(s2); TraceNL();
  278.     }
  279.    
  280.     //instruction       //variables
  281.     define NONE = 0;    //NONE
  282.     define WARP     = 1;    //dmap,screen
  283.     define POS  = 2;    //x,y
  284.     define MOVEX    = 3;    //pixels (+/-)
  285.     define MOVEY    = 4;    //pixels (+/-)
  286.     define REFILLHP = 5;    //aNONE
  287.     define REFILLMP = 6;    //NONE
  288.     define REFILLCTR = 7;   //counter
  289.     define MAXHP    = 8;    //amount
  290.     define MAXMP    = 9;    //amount
  291.     define MAXCTR   = 10;   //counter, amount
  292.    
  293.     define INVINCIBLE = 11; //(BOOL) on / off
  294.     define LINKITEM = 12;   //item, (BOOL), on / off
  295.     define SAVE = 13;   //item, (BOOL), on / off
  296.     define CREATEITEM = 14; //item, (BOOL), on / off
  297.     define CREATENPC = 15;  //item, (BOOL), on / off
  298.     define PALETTE = 16;    //item, (BOOL), on / off
  299.     define MONOCHROME = 17; //item, (BOOL), on / off
  300.    
  301.    
  302.    
  303.    
  304.    
  305.     int num_instruction_params(int instr)
  306.     {
  307.         switch(instr)
  308.         {
  309.             //instruction       //variables
  310.             case NONE: return 0;
  311.             case WARP: return 2;    //dmap,screen
  312.             case POS: return 2;     //x,y
  313.             case MOVEX: return 1;   //pixels (+/-)
  314.             case MOVEY: return 1;   //pixels (+/-)
  315.             case REFILLHP: return 0;    //aNONE
  316.             case REFILLMP: return 0;    //NONE
  317.             case REFILLCTR: return 1;   //counter
  318.             case MAXHP: return 1;   //amount
  319.             case MAXMP: return 1;   //amount
  320.             case MAXCTR: return 2;  //counter, amount
  321.            
  322.             case INVINCIBLE: return 1;  //(BOOL) on / off
  323.             case LINKITEM: return 2;    //item, (BOOL), on / off
  324.             case SAVE: return 0;    //item, (BOOL), on / off
  325.             case CREATEITEM: return 3;  //item, (BOOL), on / off
  326.             case CREATENPC: return 3;   //item, (BOOL), on / off
  327.             case PALETTE: return 2; //item, (BOOL), on / off
  328.             case MONOCHROME: return 1;  //item, (BOOL), on / off
  329.             default:
  330.             {
  331.                
  332.                 TraceError("Invalid instruction passed to stack",instr);
  333.                 clearbuffer();
  334.                 return 0;
  335.             }
  336.         }
  337.     }
  338.    
  339.    
  340.    
  341.     int match_instruction(int token)
  342.     {
  343.         TraceNL(); TraceS("Input token into match_instruction is: "); TraceS(token); TraceNL();
  344.        
  345.         TraceNL(); TraceErrorS("match_instruction() token is: ",token); TraceNL();
  346.         TraceNL(); TraceError("Matching string with strcmp to 'w': ", strcmp(token,"w")); TraceNL();
  347.        
  348.         /* ONE WAY TO DO THIS. I did this with individual characters, and switches, to minimise the checks down
  349.         to the absolute minimum. -Z
  350.        
  351.         You could add specific instructions this way, if you wish.
  352.        
  353.         if ( !(strcmp(token,"w") ) ) TraceErrorS("Token in match_instruction() matched to WARP. Token: ", token);
  354.         if ( !(strcmp(token,"W") ) ) TraceErrorS("Token in match_instruction() matched to WARP. Token: ", token);
  355.         if ( !(strcmp(token,"p") ) ) TraceErrorS("Token in match_instruction() matched to POS. Token: ", token);
  356.         if ( !(strcmp(token,"P") ) ) TraceErrorS("Token in match_instruction() matched to POS. Token: ", token);
  357.         if ( !(strcmp(token,"rh") ) ) TraceErrorS("Token in match_instruction() matched to REFILLHP. Token: ", token);
  358.         if ( !(strcmp(token,"RH") ) ) TraceErrorS("Token in match_instruction() matched to REFILLHP. Token: ", token);
  359.         if ( !(strcmp(token,"Rh") ) ) TraceErrorS("Token in match_instruction() matched to REFILLHP. Token: ", token);
  360.         if ( !(strcmp(token,"rH") ) ) TraceErrorS("Token in match_instruction() matched to REFILLHP. Token: ", token);
  361.         */
  362.        
  363.         switch(token[0])
  364.         {
  365.             case 'c':
  366.             case 'C':
  367.             {
  368.                 switch(token[1])
  369.                 {
  370.                     case 'r':
  371.                     case 'R':
  372.                     {
  373.                         switch(token[2])
  374.                         {
  375.                             case 'i':
  376.                             case 'I':
  377.                             {
  378.                                 TraceNL(); TraceS("instr() found token 'cri'"); TraceNL(); return CREATEITEM;
  379.                             }
  380.                            
  381.                             case 'n':
  382.                             case 'N':
  383.                             {
  384.                                 TraceNL(); TraceS("instr() found token 'cri'"); TraceNL(); return CREATENPC;
  385.                             }
  386.                
  387.                             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); clearbuffer(); return 0;
  388.                         }
  389.                     }
  390.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); clearbuffer(); return 0;
  391.                 }
  392.                 break;
  393.             }
  394.             case 'p':
  395.             case 'P':
  396.             {
  397.                 switch(token[1])
  398.                 {
  399.                     case 'o':
  400.                     case 'O':
  401.                         TraceNL(); TraceS("instr() found token 'p'"); TraceNL(); return POS;
  402.                     case 'a':
  403.                     case 'A':
  404.                         TraceNL(); TraceS("instr() found token 'p'"); TraceNL(); return PALETTE;
  405.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); clearbuffer(); return 0;
  406.                 }
  407.                 break;
  408.             }
  409.             case 'w':
  410.             case 'W':
  411.                 TraceNL(); TraceS("instr() found token 'w'"); TraceNL(); return WARP;
  412.             case 'r':
  413.             case 'R':
  414.             {
  415.                 switch(token[1])
  416.                 {
  417.                     case 'h':
  418.                     case 'H':
  419.                         return REFILLHP;
  420.                     case 'm':
  421.                     case 'M':
  422.                         return REFILLMP;
  423.                     case 'c':
  424.                     case 'C':
  425.                         return REFILLCTR;
  426.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); clearbuffer(); return 0;
  427.                 }
  428.                 break;
  429.             }
  430.             case 'i':
  431.             case 'I':
  432.             {
  433.                 switch(token[1])
  434.                 {
  435.                     case 'n':
  436.                     case 'N':
  437.                         return INVINCIBLE;
  438.                     case 't':
  439.                     case 'T':
  440.                         return LINKITEM;
  441.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); clearbuffer(); return 0;
  442.                 }
  443.                 break;
  444.             }
  445.             case 'm':
  446.             case 'M':
  447.             {
  448.                 switch(token[1])
  449.                 {
  450.                     case 'x':
  451.                     case 'X':
  452.                         TraceNL(); TraceS("instr() found token 'mx'"); return MOVEX;
  453.                     case 'y':
  454.                     case 'Y':
  455.                         TraceNL(); TraceS("instr() found token 'my'"); return MOVEY;
  456.                     case 'h':
  457.                     case 'H':
  458.                         return MAXHP;
  459.                     case 'm':
  460.                     case 'M':
  461.                         return MAXMP;
  462.                     case 'c':
  463.                     case 'C':
  464.                         return MAXCTR;
  465.                     case 'o':
  466.                     case 'O':
  467.                         return MONOCHROME;
  468.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); clearbuffer(); return 0;
  469.                 }
  470.                 break;
  471.             }
  472.             case 's':
  473.             case 'S':
  474.             {
  475.                 switch(token[1])
  476.                 {
  477.                     case 'a':
  478.                     case 'A':
  479.                     case 'V':
  480.                     case 'v':
  481.                     {
  482.                         TraceNL(); TraceS("instr() found token 'save'"); return SAVE;
  483.                     }
  484.                     default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); clearbuffer(); return 0;
  485.                 }
  486.                 break;
  487.             }
  488.             default: TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token); clearbuffer(); return 0;
  489.         }
  490.        
  491.         //if ( strcmp(token,"w") == 0) { TraceNL(); TraceS("instr() found token 'w'"); return WARP; }
  492.         //else if ( strcmp(token,"p") == 0) { TraceNL(); TraceS("instr() found token 'p'"); return POS; }
  493.         //else if ( strcmp(token,"mx") == 0) { TraceNL(); TraceS("instr() found token 'mx'"); return MOVEX; }
  494.         //else if ( strcmp(token,"my") == 0) return MOVEY;
  495.         //else if ( strcmp(token,"rh") == 0) return REFILLHP;
  496.         //else if ( strcmp(token,"rm") == 0) return REFILLMP;
  497.         //else if ( strcmp(token,"rc") == 0) return REFILLCTR;
  498.         //else if ( strcmp(token,"mh") == 0) return MAXHP;
  499.         //else if ( strcmp(token,"mm") == 0) return MAXMP;
  500.         //else if ( strcmp(token,"mc") == 0) return MAXCTR;
  501.         //else if ( strcmp(token,"inv") == 0) return INVINCIBLE;
  502.         //else// if ( strcmp(token,"itm") == 0) return LINKITEM;
  503.         //else
  504.         //{
  505.         //  TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token);
  506.         //  return 0;
  507.         //}
  508.     }
  509.    
  510.     void clearbuffer()
  511.     {
  512.         for ( int q = 0; q < BUFFER_LENGTH; ++q ) debug_buffer[q] = 0;
  513.     }
  514.     int read(int str)
  515.     {
  516.         //debug
  517.         TraceNL(); TraceS("Starting read() with an initial buffer of: "); TraceS(str); TraceNL();
  518.         int token[16]; int input_string_pos;
  519.         int e; int token_pos = 0; int current_param;
  520.         for ( input_string_pos = 0; input_string_pos < MAX_TOKEN_LENGTH; ++input_string_pos )
  521.         {
  522.             if (str[input_string_pos] == ',' ) { ++input_string_pos; break; }
  523.             if (str[input_string_pos] == NULL ) break;
  524.            
  525.             token[token_pos] = str[input_string_pos];
  526.             ++token_pos;
  527.            
  528.            
  529.             //debug
  530.            
  531.             //++input_string_pos; //skip the comma now. If there are no params, we'll be on NULL.
  532.         }
  533.         //debug
  534.         TraceNL(); TraceS("read() token: "); TraceS(token); TraceNL();
  535.        
  536.         //put the instruction onto the stack.
  537.         //Right now, we are only allowing one instruction at a time.
  538.         //This allows for future expansion.
  539.         stack[SP] = match_instruction(token);
  540.         TraceNL(); TraceS("SP is: "); Trace(stack[SP]); TraceNL();
  541.         int num_params = num_instruction_params(stack[SP]);
  542.         TraceNL(); TraceS("Number of expected params "); Trace(num_params); TraceNL();
  543.        
  544.         if ( num_params )
  545.         {
  546.             if ( str[input_string_pos] == NULL )
  547.             {
  548.                 //no params.
  549.                 TraceErrorS("Input string is missing params. Token was:", token);
  550.                 return 0;
  551.             }
  552.         }
  553.        
  554.         ++SP; //get the stack ready for the next instruction.
  555.         //push the variables onto the stack.
  556.         while ( current_param < num_params )  //repeat this until we are out of params
  557.             //NOT a Do loop, because some instructions have no params!
  558.         {
  559.             for ( token_pos = MAX_TOKEN_LENGTH-1; token_pos >= 0; --token_pos ) token[token_pos] = 0; //clear the token
  560.            
  561.             //copy over new token
  562.             token_pos = 0;
  563.             TraceNL(); TraceS("read() is seeking for params."); TraceNL();
  564.             int temp_max = input_string_pos+MAX_TOKEN_LENGTH;
  565.             for ( ; input_string_pos < temp_max; ++input_string_pos )
  566.             {
  567.                 if (str[input_string_pos] == ',' ) { ++input_string_pos; break; }
  568.                 if (str[input_string_pos] == NULL ) break;
  569.                
  570.                 token[token_pos] = str[input_string_pos];
  571.                 ++token_pos;
  572.                
  573.                
  574.                 //debug
  575.                
  576.                 //++input_string_pos; //skip the comma now. If there are no params, we'll be on NULL.
  577.             }
  578.             /*
  579.             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
  580.             {
  581.                 token[token_pos] = str[input_string_pos]; //store the variable into a new token
  582.                 ++token_pos;
  583.             }
  584.             */
  585.             TraceNL(); TraceS("read() is getting tval"); TraceNL();
  586.             int tval; //value of the param
  587.             //first check the boolean types:
  588.             TraceNL(); TraceS("The arg token is: "); TraceS(token); TraceNL();
  589.             if ( !isNumber(token[0]) )
  590.             {
  591.                 switch(token[0])
  592.                 {
  593.                    
  594.                     case '-': tval = atof(token); break;
  595.                    
  596.                     case 't':
  597.                     case 'T':
  598.                         tval = 1; break;
  599.                     case 'f':
  600.                     case 'F':
  601.                         tval = 0; break;
  602.                    
  603.                     case 'l':
  604.                     case 'L':
  605.                     {
  606.                         switch(token[1])
  607.                         {
  608.                             case 'x':
  609.                             case 'X':
  610.                             {
  611.                                 TraceError("tval set to Link->X: ", Link->X);
  612.                                 tval = Link->X; break;
  613.                             }
  614.                             case 'y':
  615.                             case 'Y':
  616.                             {
  617.                                 TraceError("tval set to Link->Y: ", Link->Y);
  618.                                 tval = Link->Y; break;
  619.                             }
  620.                             default: TraceErrorS("Invalid token passed as an argument for instruction: ", token); tval = 0; break;
  621.                         }
  622.                         break;
  623.                     }
  624.                    
  625.                     default: TraceErrorS("Invalid token passed as an argument for instruction: ", token); tval = 0; break;
  626.                 }
  627.                 //if ( strcmp(token,"true") ) tval = 1;
  628.                 //else if ( strcmp(token,"T") ) tval = 1;
  629.                 //else if ( strcmp(token,"false") ) tval = 0;
  630.                 //else if ( strcmp(token,"F") ) tval = 0;
  631.                
  632.             }
  633.             else //literals
  634.             {
  635.                
  636.                 tval = atof(token);
  637.                 TraceNL(); TraceS("found a literal var of: "); Trace(tval); TraceNL();
  638.                
  639.             }
  640.             //push the token value onto the stack
  641.             stack[SP] = tval;
  642.        
  643.             //now out stack looks like:
  644.            
  645.             //: PARAMn where n is the loop iteration
  646.             //: PARAMn where n is the loop iteration
  647.             //: PARAMn where n is the loop iteration
  648.             //: INSTRUCTION
  649.            
  650.             ++SP; //this is why the stack size must be +1 larger than the3 total number of instructions and
  651.             //params that it can hold.
  652.             ++current_param;
  653.            
  654.         } //repeat this until we are out of params
  655.         return 1;
  656.        
  657.     }
  658.    
  659.     //void getVarValue(int str)
  660.     //{
  661.     //  variables[VP] = atof(str);
  662.     //  ++VP;
  663.     //}
  664.    
  665.     void execute()
  666.     {
  667.        
  668.         TraceNL(); TraceS("Stack Trace");
  669.         for ( int q = SizeOfArray(stack)-1; q >= 0; --q )
  670.         {
  671.             TraceNL(); Trace(stack[q]);
  672.         }
  673.        
  674.        
  675.         TraceNL(); TraceS("Running execute()"); TraceNL();
  676.         int reg_ptr = 0; //read the stack starting here, until we reach TOP.
  677.         int args[MAX_ARGS];
  678.         //evaluate the instruction:
  679.         int instr = stack[reg_ptr];
  680.         ++reg_ptr;
  681.         int current_arg = 0;
  682.         int num_of_params = num_instruction_params(instr);
  683.         TraceNL(); TraceS("execute() expects number of args to be: "); Trace(num_of_params); TraceNL();
  684.         for ( ; num_of_params > 0; --num_of_params )
  685.         {
  686.             args[current_arg] = stack[reg_ptr];
  687.             TraceNL(); TraceS("Putting an arg on the heap. Arg value: "); Trace(args[current_arg]); TraceNL();
  688.             ++current_arg;
  689.             ++reg_ptr;
  690.            
  691.         }
  692.        
  693.         TraceNL(); TraceS("execute believes that the present instruction is: "); Trace(instr); TraceNL();
  694.         TraceNL(); TraceS("args[0] is: "); Trace(args[0]); TraceNL();
  695.         TraceNL(); TraceS("args[1] is: "); Trace(args[1]); TraceNL();
  696.        
  697.         switch(instr)
  698.         {
  699.             case NONE:
  700.                 TraceError("STACK INSTRUCTION IS INVALID: ", instr);
  701.                 Game->TypingMode = false;
  702.                 clearbuffer();
  703.                 break;
  704.             case WARP:
  705.             {
  706.                 Link->Warp(args[0],args[1]);
  707.                 if ( log_actions ) TraceError("Cheat System Warped Link to dmap,screen:",args[0],args[1]);
  708.                 break;
  709.             }
  710.             case POS:
  711.             {
  712.                 Link->X = args[0];
  713.                 Link->Y = args[1];
  714.                 if ( log_actions ) TraceError("Cheat System repositioned Link to X,Y:",args[0],args[1]);
  715.                 break;
  716.             }
  717.            
  718.             case MOVEX:
  719.             {
  720.                 Link->X += args[0];
  721.                 if ( log_actions ) TraceError("Cheat system moved Link on his X axis by: ", args[0]);
  722.                 break;
  723.             }
  724.             case MOVEY:
  725.             {
  726.                 Link->Y += args[0];
  727.                 if ( log_actions ) TraceError("Cheat system moved Link on his Y axis by", args[0]);
  728.                 break;
  729.             }
  730.             case REFILLHP:
  731.             {
  732.                 Link->HP =  Link->MaxHP;
  733.                 if ( log_actions ) TraceError("Cheat system refilled Link's HP to", Link->MaxHP);
  734.                 break;
  735.             }
  736.             case REFILLMP:
  737.             {
  738.                 Link->MP =  Link->MaxMP;
  739.                 if ( log_actions ) TraceError("Cheat system refilled Link's MP to", Link->MaxHP);
  740.                 break;
  741.             }
  742.             case REFILLCTR:
  743.             {
  744.                 Game->Counter[args[0]] =  Game->MCounter[args[0]];
  745.                 if ( log_actions ) TraceError("Cheat system refilled Counter", args[0]);
  746.                 break;
  747.             }
  748.             case MAXHP:
  749.             {
  750.                 Game->MCounter[CR_LIFE] = args[0];
  751.                 if ( log_actions ) TraceError("Cheat system set Link's Max HP to:",args[0]);
  752.                 break;
  753.             }
  754.             case MAXMP:
  755.             {
  756.                 Game->MCounter[CR_MAGIC] = args[0];
  757.                 if ( log_actions ) TraceError("Cheat system set Link's Max MP to:",args[0]);
  758.                 break;
  759.             }
  760.             case MAXCTR:
  761.             {
  762.                 Game->Counter[args[0]] = args[1];
  763.                 if ( log_actions ) TraceError("Cheat system refilled Counter (id, amount):",args[0],args[1]);
  764.                 break;
  765.             }
  766.            
  767.             case INVINCIBLE:
  768.             {
  769.                 if ( args[0] )
  770.                 {
  771.                     Link->Invisible = true;
  772.                     if ( log_actions ) TraceErrorS("Cheat system set Link's Invisibility state to ","true");
  773.                     break;
  774.                 }
  775.                 else
  776.                 {
  777.                     Link->Invisible = false;
  778.                     if ( log_actions ) TraceErrorS("Cheat system set Link's Invisibility state to ","false");
  779.                     break;
  780.                    
  781.                 }
  782.                
  783.             }
  784.             case LINKITEM:
  785.             {
  786.                 itemdata id = Game->LoadItemData(args[0]);
  787.                 if ( id->Keep )
  788.                 {
  789.                     if ( args[1] )
  790.                     {
  791.                        
  792.                         Link->Item[args[0]] = true;
  793.                         if ( log_actions ) TraceErrorS("Cheat system set Link's Inventory Item to (item, state)","true");
  794.                         break;
  795.                     }
  796.                     else
  797.                     {
  798.                         Link->Item[args[0]] = false;
  799.                         if ( log_actions ) TraceErrorS("Cheat system set Link's Inventory Item to (item, state)","false");
  800.                         break;
  801.                        
  802.                     }
  803.                 }
  804.                 else break;
  805.             }
  806.             case SAVE:
  807.             {
  808.                 TraceNL(); TraceS("Cheat system is saving the game.");
  809.                 clearbuffer();
  810.                 Game->Save();
  811.                 break;
  812.             }
  813.             case CREATEITEM:
  814.             {
  815.                 if ( log_actions ) TraceError("Cheat system is creating item ID: ", args[0]);
  816.                 if ( log_actions ) TraceError("Cheat system is creating item at X Position: ", args[1]);
  817.                 if ( log_actions ) TraceError("Cheat system is creating item at Y Position: ", args[2]);
  818.                 item cci = Screen->CreateItem(args[0]);
  819.                 cci->X = args[1];
  820.                 cci->Y = args[2];
  821.                 break;
  822.             }
  823.             case CREATENPC:
  824.             {
  825.                 if ( log_actions ) TraceError("Cheat system is creating npc ID: ", args[0]);
  826.                 if ( log_actions ) TraceError("Cheat system is creating npc at X Position: ", args[1]);
  827.                 if ( log_actions ) TraceError("Cheat system is creating npc at Y Position: ", args[2]);
  828.                 npc ccn = Screen->CreateNPC(args[0]);
  829.                 ccn->X = args[1];
  830.                 ccn->Y = args[2];
  831.                 break;
  832.             }
  833.             case PALETTE:
  834.             {
  835.                 if ( args[0] < 0 )
  836.                 {
  837.                     Game->DMapPalette[Game->GetCurDMap()] = args[1];
  838.                 }
  839.                 else Game->DMapPalette[args[0]] = args[1];
  840.                 break;
  841.             }
  842.             case MONOCHROME:
  843.             {
  844.                 Graphics->Monochrome(args[0]);
  845.             }
  846.             default:
  847.             {
  848.                
  849.                 TraceError("Invalid instruction passed to stack",instr);
  850.                 break;
  851.             }
  852.            
  853.            
  854.         }
  855.         ///-----later, we'll add this: //pop everything off of the stack
  856.         //just wipe the stack for now, as we only support one command at this time
  857.         for ( int q = 0; q < 3; ++q ) stack[q] = 0;
  858.         SP = 0;
  859.        
  860.         //clear the main buffer, too!
  861.         for ( int cl = 0; cl < BUFFER_LENGTH; ++cl ) debug_buffer[cl] = 0;
  862.         Game->TypingMode = false; //insurance clear
  863.         Link->PressStart = false;
  864.         Link->InputStart = false;
  865.        
  866.        
  867.     }
  868.        
  869.     void run()
  870.     {
  871.    
  872.        
  873.        
  874.     }
  875. }
  876.  
  877. global script test
  878. {
  879.     void run()
  880.     {
  881.         debugshell.SP = 0;
  882.         debugshell.clearbuffer();
  883.         while(1)
  884.         {
  885.             debugshell.process();
  886.             Waitdraw();
  887.             Waitframe();
  888.         }
  889.        
  890.     }
  891. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement