Advertisement
ZoriaRPG

Debug Cheat Shell (Fix Compile)

Oct 30th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.35 KB | None | 0 0
  1. /////////////////////////////////
  2. /// Debug Shell for ZC Quests ///
  3. /// Alpha Version 1.1         ///
  4. /// 15th October, 2018        ///
  5. /// By: ZoriaRPG              ///
  6. /// Requires: ZC Necromancer  ///
  7. /////////////////////////////////
  8. /*
  9. DEFINED INSTRUCTION VALUES
  10.     w WARP: return 2;   //dmap,screen
  11.     p POS: return 2;        //x,y
  12.     mx MOVEX: return 1;     //pixels (+/-)
  13.     my MOVEY: return 1;     //pixels (+/-)
  14.     rh REFILLHP: return 0;  //aNONE
  15.     rm REFILLMP: return 0;  //NONE
  16.     rc REFILLCTR: return 1; //counter
  17.     mh MAXHP: return 1; //amount
  18.     mm MAXMP: return 1; //amount
  19.     mc MAXCTR: return 2;    //counter, amount
  20.    
  21.     inv INVINCIBLE: return 1;   //(BOOL) on / off
  22.     itm LINKITEM: return 2; //item, (BOOL), on / off
  23.  
  24. //COMMAND LIST
  25.     w: Warp Link to a specific dmap and screen
  26.     p: Reposition Link on the screen.
  27.     mx: Move link by +/-n pixels on the X axis.
  28.     my: Move link by +/-n pixels on the Y axis.
  29.     rh: Refill Link's HP to Max.
  30.     rm: Refill Link's HP to Max.
  31.     rc: Refill a specific counter to Max.
  32.     mh: Set Link's Max HP.
  33.     mm: Set Link's Max MP
  34.     mc: Set the maximum value of a specific counter.
  35.     inv: Set Link's Invisible state.
  36.     itm: Set the state of a specific item in Link's inventory.
  37. //SYNTAX
  38. //command,args
  39.     w,1,2
  40.     p,1,2
  41.     mx,1
  42.     mx,-1
  43.     my,1
  44.     my,-1
  45.     rh
  46.     rm
  47.     rc,1
  48.     mh,1
  49.     mm,1
  50.     mc,1,2
  51.     inv,true
  52.     inv,false
  53.     itm,1,true
  54.     itm,1,false
  55.        
  56.        
  57.  
  58. */
  59.  
  60. script typedef ffc namespace;
  61. typedef const int define;
  62. typedef const int CFG;
  63.  
  64.  
  65.  
  66. namespace script debugshell
  67. {
  68.     define INSTRUCTION_SIZE = 1; //The number of stack registers that any given *instruction* requires.
  69.     define MAX_INSTR_QUEUE = 1; //The number of instructions that can be enqueued.
  70.     define MAX_ARGS     = 2; //The maximum number of args that any instruction can use/require.
  71.     define STACK_SIZE   = 1 + ((INSTRUCTION_SIZE+MAX_ARGS)*MAX_INSTR_QUEUE);
  72.     define MAX_TOKEN_LENGTH = 16;
  73.     define BUFFER_LENGTH    = 42;
  74.     int stack[STACK_SIZE];
  75.     int SP;
  76.    
  77.     int debug_buffer[BUFFER_LENGTH];
  78.    
  79.     define YES = 1;
  80.     define NO = 0;
  81.    
  82.     CFG log_actions = YES;
  83.     CFG WINDOW_F_KEY = 7; //We use F7 to open the debug window.
  84.    
  85.    
  86.     define FONT = FONT_APPLE2; //Apple II
  87.     define F_COLOUR = 0x01; //font colour, white
  88.     define F_BCOLOUR = 0x00; //font background colour, translucent
  89.     define W_COLOUR = 0x0F; //window colour (background), black
  90.     define CHAR_WIDTH = 6; //5 + one space
  91.     define CHAR_HEIGHT = 9; //8 + one space
  92.     define WINDOW_X = 12; //window indent over screen
  93.     define WINDOW_Y = 16; //window indent over screen
  94.     define WINDOW_H = CHAR_WIDTH * BUFFER_LENGTH;
  95.     define WINDOW_W = CHAR_HEIGHT * 3;
  96.     define CHAR_X = 4; //Initial x indent
  97.     define CHAR_Y = 12; //Initial y indent
  98.     define W_OPACITY = OP_OPAQUE; //Window translucency.
  99.     define F_OPACITY = OP_OPAQUE; //Font translucency.
  100.     define W_LAYER = 6; //window draw layer
  101.     define F_LAYER = 6; //font draw layer
  102.    
  103.     CFG KEY_DELAY = 6; //frames between keystrokes
  104.    
  105.     define TYPESFX = 63;
  106.    
  107.     void process()
  108.     {
  109.         if ( FKey(WINDOW_F_KEY) )
  110.         {
  111.             if ( type() )
  112.             {
  113.                 read(debug_buffer);
  114.                 execute();
  115.             }
  116.         }
  117.     }
  118.    
  119.     //if ( type() execute() )
  120.     //returns true if the user presses enter
  121.     bool type()
  122.     {
  123.         Game->TypingMode = true;
  124.         int key_timer; int buffer_pos = 0;
  125.         while(!Input->ReadKey[KEY_ENTER] || Input->ReadKey[KEY_ENTER_PAD])
  126.         {
  127.             if ( key_timer <= 0 )
  128.             {
  129.                 if ( KeyToChar(CHAR_BACKSPC) ) //backspace
  130.                 {
  131.                    
  132.                     if ( buffer_pos > 0 )
  133.                     {
  134.                         debug_buffer[buffer_pos] = 0;
  135.                         --buffer_pos;
  136.                     }
  137.                     key_timer = KEY_DELAY;
  138.                     continue;
  139.                 }
  140.                 else if ( Input->Key[KEY_ENTER] || Input->Key[KEY_ENTER_PAD] )
  141.                 {
  142.                     Game->TypingMode = false;
  143.                     if ( !buffer_pos ) return false; //do not execute if there are no commands
  144.                     return true;
  145.                 }
  146.                 else if ( EscKey() )
  147.                 {
  148.                     Game->TypingMode = false;
  149.                     return false; //exit and do not process.
  150.                 }
  151.                
  152.                 else
  153.                 {
  154.                     //else normal key
  155.                     int k;
  156.                     int LegalKeys[]=
  157.                     {
  158.                         KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H,
  159.                         KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P,
  160.                         KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U, KEY_V, KEY_W, KEY_X,
  161.                         KEY_Y, KEY_Z, KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5,
  162.                         KEY_6, KEY_7, KEY_8, KEY_9, KEY_0_PAD, KEY_1_PAD, KEY_2_PAD,
  163.                         KEY_3_PAD, KEY_4_PAD, KEY_5_PAD,
  164.                         KEY_6_PAD, KEY_7_PAD, KEY_8_PAD, KEY_9_PAD,
  165.                         //KEY_TILDE,
  166.                         KEY_MINUS,
  167.                         //KEY_EQUALS, KEY_OPENBRACE, KEY_CLOSEBRACE,
  168.                         //KEY_COLON, KEY_QUOTE, KEY_BACKSLASH, KEY_BACKSLASH2,
  169.                         KEY_COMMA,
  170.                         //KEY_SEMICOLON, KEY_SLASH, KEY_SPACE, KEY_SLASH_PAD,
  171.                         //KEY_ASTERISK,
  172.                         KEY_MINUS_PAD
  173.                         //KEY_PLUS_PAD, KEY_CIRCUMFLEX, KEY_COLON2, KEY_EQUALS_PAD, KEY_STOP
  174.                     };
  175.  
  176.                    
  177.                     for ( int kk = SizeOfArray(LegalKeys); kk >= 0; --kk )
  178.                     {
  179.                         k = LegalKeys[kk];
  180.                         if ( Input->Key[k] )
  181.                         {
  182.                             debug_buffer[buffer_pos] = KeyToChar(k); //Warning!: Some masking may occur. :P
  183.                         }
  184.                     }
  185.                     ++buffer_pos;
  186.                     key_timer = KEY_DELAY;
  187.                     continue;
  188.                 }
  189.             }
  190.             --key_timer;
  191.             draw();
  192.             Waitframe();
  193.         }
  194.        
  195.     }
  196.    
  197.     void draw()
  198.     {
  199.         Screen->Rectangle(W_LAYER, WINDOW_X, WINDOW_Y, WINDOW_X+WINDOW_W, WINDOW_Y+WINDOW_H, W_COLOUR, 100, 0,0,0,true,W_OPACITY);
  200.         Screen->DrawString(F_LAYER,CHAR_X,CHAR_Y,FONT,F_COLOUR,F_BCOLOUR,0,debug_buffer,F_OPACITY);
  201.     }
  202.    
  203.     void TraceErrorS(int s, int s2)
  204.     {
  205.         TraceS(s); TraceS(": "); TraceS(s2); TraceNL();
  206.     }
  207.    
  208.     void TraceError(int s, float v, float v2)
  209.     {
  210.         int buf[12]; int buf2[12];
  211.         ftoa(buf,v);
  212.         ftoa(buf2,v2);
  213.         TraceS(s); TraceS(": "); TraceS(buf); TraceS(", "); TraceS(buf2); TraceNL();
  214.     }
  215.    
  216.     void TraceErrorVS(int s, float v, int s2)
  217.     {
  218.         int buf[12];
  219.         ftoa(buf,v);
  220.         TraceS(s); TraceS(": "); TraceS(buf); TraceS(", "); TraceS(s2); TraceNL();
  221.     }
  222.    
  223.     //instruction       //variables
  224.     define NONE = 1;    //NONE
  225.     define WARP     = 1;    //dmap,screen
  226.     define POS  = 2;    //x,y
  227.     define MOVEX    = 3;    //pixels (+/-)
  228.     define MOVEY    = 4;    //pixels (+/-)
  229.     define REFILLHP = 5;    //aNONE
  230.     define REFILLMP = 6;    //NONE
  231.     define REFILLCTR = 7;   //counter
  232.     define MAXHP    = 8;    //amount
  233.     define MAXMP    = 9;    //amount
  234.     define MAXCTR   = 10;   //counter, amount
  235.    
  236.     define INVINCIBLE = 11; //(BOOL) on / off
  237.     define LINKITEM = 12;   //item, (BOOL), on / off
  238.    
  239.    
  240.    
  241.    
  242.    
  243.     int num_instruction_params(int instr)
  244.     {
  245.         switch(instr)
  246.         {
  247.             //instruction       //variables
  248.             case NONE: return 0;
  249.             case WARP: return 2;    //dmap,screen
  250.             case POS: return 2;     //x,y
  251.             case MOVEX: return 1;   //pixels (+/-)
  252.             case MOVEY: return 1;   //pixels (+/-)
  253.             case REFILLHP: return 0;    //aNONE
  254.             case REFILLMP: return 0;    //NONE
  255.             case REFILLCTR: return 1;   //counter
  256.             case MAXHP: return 1;   //amount
  257.             case MAXMP: return 1;   //amount
  258.             case MAXCTR: return 2;  //counter, amount
  259.            
  260.             case INVINCIBLE: return 1;  //(BOOL) on / off
  261.             case LINKITEM: return 2;    //item, (BOOL), on / off
  262.             default:
  263.             {
  264.                
  265.                 TraceError("Invalid instruction passed to stack",instr);
  266.                 return 0;
  267.             }
  268.         }
  269.     }
  270.    
  271.    
  272.    
  273.     int match_instruction(int token)
  274.     {
  275.         if ( strcmp(token,"w")) return WARP;
  276.         if ( strcmp(token,"P")) return POS;
  277.         if ( strcmp(token,"mx")) return MOVEX;
  278.         if ( strcmp(token,"my")) return MOVEY;
  279.         if ( strcmp(token,"rh")) return REFILLHP;
  280.         if ( strcmp(token,"rm")) return REFILLMP;
  281.         if ( strcmp(token,"rc")) return REFILLCTR;
  282.         if ( strcmp(token,"mh")) return MAXHP;
  283.         if ( strcmp(token,"mm")) return MAXMP;
  284.         if ( strcmp(token,"mc")) return MAXCTR;
  285.         if ( strcmp(token,"inv")) return INVINCIBLE;
  286.         if ( strcmp(token,"itm")) return LINKITEM;
  287.         TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token);
  288.         return 0;
  289.     }
  290.    
  291.     int read(int str)
  292.     {
  293.         int token[16]; int input_string_pos;
  294.         int e; int token_pos; int current_param;
  295.         for ( input_string_pos = 0; input_string_pos < MAX_TOKEN_LENGTH; ++input_string_pos )
  296.         {
  297.             while(str[input_string_pos] != ',') //was buffer[]
  298.             {
  299.                 if ( token[input_string_pos] == NULL ) break;
  300.                 token[input_string_pos] = str[input_string_pos];
  301.             }
  302.             ++input_string_pos; //skip the comma now. If there are no params, we'll be on NULL.
  303.         }
  304.        
  305.         //put the instruction onto the stack.
  306.         //Right now, we are only allowing one instruction at a time.
  307.         //This allows for future expansion.
  308.         stack[SP] = match_instruction(token);
  309.         int num_params = num_instruction_params(stack[SP]);
  310.        
  311.         if ( num_params )
  312.         {
  313.             if ( str[input_string_pos] == NULL )
  314.             {
  315.                 //no params.
  316.                 TraceErrorS("Input string is missing params. Token was:", "token");
  317.             }
  318.         }
  319.        
  320.         ++SP; //get the stack ready for the next instruction.
  321.         //push the variables onto the stack.
  322.         while ( current_param <= num_params )  //repeat this until we are out of params
  323.             //NOT a Do loop, because some instructions have no params!
  324.         {
  325.             for ( token_pos = MAX_TOKEN_LENGTH-1; token_pos >= 0; --token_pos ) token[token_pos] = 0; //clear the token
  326.            
  327.             //copy over new token
  328.             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
  329.             {
  330.                 token[token_pos] = str[input_string_pos]; //store the variable into a new token
  331.                 ++token_pos;
  332.             }
  333.             int tval; //value of the param
  334.             //first check the boolean types:
  335.             if ( strcmp(token,"true") ) tval = 1;
  336.             else if ( strcmp(token,"T") ) tval = 1;
  337.             else if ( strcmp(token,"false") ) tval = 0;
  338.             else if ( strcmp(token,"F") ) tval = 0;
  339.             else //literals
  340.             {
  341.                 tval = atof(token);
  342.                
  343.             }
  344.             //push the token value onto the stack
  345.             stack[SP] = tval;
  346.        
  347.             //now out stack looks like:
  348.            
  349.             //: PARAMn where n is the loop iteration
  350.             //: PARAMn where n is the loop iteration
  351.             //: PARAMn where n is the loop iteration
  352.             //: INSTRUCTION
  353.            
  354.             ++SP; //this is why the stack size must be +1 larger than the3 total number of instructions and
  355.             //params that it can hold.
  356.             ++current_param;
  357.            
  358.         } //repeat this until we are out of params
  359.        
  360.     }
  361.    
  362.     //void getVarValue(int str)
  363.     //{
  364.     //  variables[VP] = atof(str);
  365.     //  ++VP;
  366.     //}
  367.    
  368.     void execute()
  369.     {
  370.         int reg_ptr = 0; //read the stack starting here, until we reach TOP.
  371.         int args[MAX_ARGS];
  372.         //evaluate the instruction:
  373.         int instr = stack[reg_ptr];
  374.         int current_arg = 0;
  375.         int num_of_params = num_instruction_params(instr);
  376.         while( num_of_params > 0 )
  377.         {
  378.             ++reg_ptr;
  379.             args[current_arg] = stack[reg_ptr];
  380.         }
  381.         switch(instr)
  382.         {
  383.             case NONE:
  384.                 TraceError("STACK INSTRUCTION IS INVALID: ", instr); break;
  385.             case WARP:
  386.             {
  387.                 Link->Warp(args[0],args[1]);
  388.                 if ( log_actions ) TraceError("Cheat System Warped Link to dmap,screen:",args[0],args[1]);
  389.                 break;
  390.             }
  391.             case POS:
  392.             {
  393.                 Link->X = args[0];
  394.                 Link->X = args[1];
  395.                 if ( log_actions ) TraceError("Cheat System repositioned Link to X,Y:",args[0],args[1]);
  396.                 break;
  397.             }
  398.            
  399.             case MOVEX:
  400.             {
  401.                 Link->X += args[0];
  402.                 if ( log_actions ) TraceError("Cheat system moved Link on his X axis by: ", args[0]);
  403.                 break;
  404.             }
  405.             case MOVEY:
  406.             {
  407.                 Link->Y += args[0];
  408.                 if ( log_actions ) TraceError("Cheat system moved Link on his Y axis by", args[0]);
  409.                 break;
  410.             }
  411.             case REFILLHP:
  412.             {
  413.                 Link->HP =  Link->MaxHP;
  414.                 if ( log_actions ) TraceError("Cheat system refilled Link's HP to", Link->MaxHP);
  415.                 break;
  416.             }
  417.             case REFILLMP:
  418.             {
  419.                 Link->MP =  Link->MaxMP;
  420.                 if ( log_actions ) TraceError("Cheat system refilled Link's MP to", Link->MaxHP);
  421.                 break;
  422.             }
  423.             case REFILLCTR:
  424.             {
  425.                 Game->Counter[args[0]] =  Game->MCounter[args[0]];
  426.                 if ( log_actions ) TraceError("Cheat system refilled Counter", args[0]);
  427.                 break;
  428.             }
  429.             case MAXHP:
  430.             {
  431.                 Game->MCounter[CR_LIFE] = args[0];
  432.                 if ( log_actions ) TraceError("Cheat system set Link's Max HP to:",args[0]);
  433.                 break;
  434.             }
  435.             case MAXMP:
  436.             {
  437.                 Game->MCounter[CR_MAGIC] = args[0];
  438.                 if ( log_actions ) TraceError("Cheat system set Link's Max MP to:",args[0]);
  439.                 break;
  440.             }
  441.             case MAXCTR:
  442.             {
  443.                 Game->Counter[args[0]] = args[1];
  444.                 if ( log_actions ) TraceError("Cheat system refilled Counter (id, amount):",args[0],args[1]);
  445.                 break;
  446.             }
  447.            
  448.             case INVINCIBLE:
  449.             {
  450.                 if ( args[0] )
  451.                 {
  452.                     Link->Invisible = true;
  453.                     if ( log_actions ) TraceErrorS("Cheat system set Link's Invisibility state to ","true");
  454.                     break;
  455.                 }
  456.                 else
  457.                 {
  458.                     Link->Invisible = false;
  459.                     if ( log_actions ) TraceErrorS("Cheat system set Link's Invisibility state to ","false");
  460.                     break;
  461.                    
  462.                 }
  463.                
  464.             }
  465.             case LINKITEM:
  466.             {
  467.                 if ( args[1] )
  468.                 {
  469.                     Link->Item[args[0]] = true;
  470.                     if ( log_actions ) TraceErrorS("Cheat system set Link's Inventory Item to (item, state)","true");
  471.                     break;
  472.                 }
  473.                 else
  474.                 {
  475.                     Link->Item[args[0]] = false;
  476.                     if ( log_actions ) TraceErrorS("Cheat system set Link's Inventory Item to (item, state)","false");
  477.                     break;
  478.                    
  479.                 }
  480.             }
  481.             default:
  482.             {
  483.                
  484.                 TraceError("Invalid instruction passed to stack",instr);
  485.                 break;
  486.             }
  487.            
  488.            
  489.         }
  490.         ///-----later, we'll add this: //pop everything off of the stack
  491.         //just wipe the stack for now, as we only support one command at this time
  492.         for ( int q = 0; q < 3; ++q ) stack[q] = 0;
  493.         SP = 0;
  494.        
  495.        
  496.     }
  497.        
  498.     void run()
  499.     {
  500.    
  501.        
  502.        
  503.     }
  504. }
  505.  
  506. global script test
  507. {
  508.     void run()
  509.     {
  510.         while(1)
  511.         {
  512.             debugshell.process();
  513.             Waitdraw();
  514.             Waitframe();
  515.         }
  516.        
  517.     }
  518. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement