Advertisement
ZoriaRPG

ZScript: Quest Debugging Shell (Public Version, Alpha 1.1)

Oct 15th, 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->KeyPress[KEY_ENTER] || Input->Key[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.         int buf[12];
  206.         ftoa(buf,v);
  207.         TraceS(s); TraceS(": "); TraceS(s2); TraceNL();
  208.     }
  209.    
  210.     void TraceError(int s, float v, float v2)
  211.     {
  212.         int buf[12]; int buf2[12];
  213.         ftoa(buf,v);
  214.         ftoa(buf2,v2);
  215.         TraceS(s); TraceS(": "); TraceS(buf); TraceS(", "); TraceS(buf2); TraceNL();
  216.     }
  217.    
  218.     void TraceErrorVS(int s, float v, int s2)
  219.     {
  220.         int buf[12];
  221.         ftoa(buf,v);
  222.         TraceS(s); TraceS(": "); TraceS(buf); TraceS(", "); TraceS(s2); TraceNL();
  223.     }
  224.    
  225.     //instruction       //variables
  226.     define NONE = 1;    //NONE
  227.     define WARP     = 1;    //dmap,screen
  228.     define POS  = 2;    //x,y
  229.     define MOVEX    = 3;    //pixels (+/-)
  230.     define MOVEY    = 4;    //pixels (+/-)
  231.     define REFILLHP = 5;    //aNONE
  232.     define REFILLMP = 6;    //NONE
  233.     define REFILLCTR = 7;   //counter
  234.     define MAXHP    = 8;    //amount
  235.     define MAXMP    = 9;    //amount
  236.     define MAXCTR   = 10;   //counter, amount
  237.    
  238.     define INVINCIBLE = 11; //(BOOL) on / off
  239.     define LINKITEM = 12;   //item, (BOOL), on / off
  240.    
  241.    
  242.    
  243.    
  244.    
  245.     int num_instruction_params(int instr)
  246.     {
  247.         switch(instr)
  248.         {
  249.             //instruction       //variables
  250.             case NONE: return 0;
  251.             case WARP: return 2;    //dmap,screen
  252.             case POS: return 2;     //x,y
  253.             case MOVEX: return 1;   //pixels (+/-)
  254.             case MOVEY: return 1;   //pixels (+/-)
  255.             case REFILLHP: return 0;    //aNONE
  256.             case REFILLMP: return 0;    //NONE
  257.             case REFILLCTR: return 1;   //counter
  258.             case MAXHP: return 1;   //amount
  259.             case MAXMP: return 1;   //amount
  260.             case MAXCTR: return 2;  //counter, amount
  261.            
  262.             case INVINCIBLE: return 1;  //(BOOL) on / off
  263.             case LINKITEM: return 2;    //item, (BOOL), on / off
  264.             default:
  265.             {
  266.                
  267.                 TraceError("Invalid instruction passed to stack",instr);
  268.                 return 0;
  269.             }
  270.         }
  271.     }
  272.    
  273.    
  274.    
  275.     int match_instruction(int token)
  276.     {
  277.         if ( strcmp(token,"w")) return WARP;
  278.         if ( strcmp(token,"P")) return POS;
  279.         if ( strcmp(token,"mx")) return MOVEX;
  280.         if ( strcmp(token,"my")) return MOVEY;
  281.         if ( strcmp(token,"rh")) return REFILLHP;
  282.         if ( strcmp(token,"rm")) return REFILLMP;
  283.         if ( strcmp(token,"rc")) return REFILLCTR;
  284.         if ( strcmp(token,"mh")) return MAXHP;
  285.         if ( strcmp(token,"mm")) return MAXMP;
  286.         if ( strcmp(token,"mc")) return MAXCTR;
  287.         if ( strcmp(token,"inv")) return INVINCIBLE;
  288.         if ( strcmp(token,"itm")) return LINKITEM;
  289.         TraceErrorS("match_instruction(TOKEN) could not evaluate the instruction:",token);
  290.         return 0;
  291.     }
  292.    
  293.     int read(int str)
  294.     {
  295.         int token[16]; int input_string_pos;
  296.         int e; int token_pos; int current_param;
  297.         for ( input_string_pos = 0; input_string_pos < MAX_TOKEN_LENGTH; ++input_string_pos )
  298.         {
  299.             while(buffer[input_string_pos] != ',')
  300.             {
  301.                 if ( token[input_string_pos] == NULL ) break;
  302.                 token[input_string_pos] = str[input_string_pos];
  303.             }
  304.             ++input_string_pos; //skip the comma now. If there are no params, we'll be on NULL.
  305.         }
  306.        
  307.         //put the instruction onto the stack.
  308.         //Right now, we are only allowing one instruction at a time.
  309.         //This allows for future expansion.
  310.         stack[SP] = match_instruction(token);
  311.         int num_params = num_instruction_params(stack[SP]);
  312.        
  313.         if ( num_params )
  314.         {
  315.             if ( str[input_string_pos] == NULL )
  316.             {
  317.                 //no params.
  318.                 TraceErrorS("Input string is missing params. Token was:", "token");
  319.             }
  320.         }
  321.        
  322.         ++SP; //get the stack ready for the next instruction.
  323.         //push the variables onto the stack.
  324.         while ( current_param <= num_params )  //repeat this until we are out of params
  325.             //NOT a Do loop, because some instructions have no params!
  326.         {
  327.             for ( token_pos = MAX_TOKEN_LENGTH-1; token_pos >= 0; --token_pos ) token[token_pos] = 0; //clear the token
  328.            
  329.             //copy over new token
  330.             while( buffer[input_string_pos] != ',' || buffer[input_string_pos] != NULL || param >= num_params ) //token terminates on a comma, or the end of the string
  331.             {
  332.                 token[token_pos] = str[input_string_pos]; //store the variable into a new token
  333.                 ++token_pos;
  334.             }
  335.             int tval; //value of the param
  336.             //first check the boolean types:
  337.             if ( strcmp(token,"true") ) tval = 1;
  338.             else if ( strcmp(token,"T") ) tval = 1;
  339.             else if ( strcmp(token,"false") ) tval = 0;
  340.             else if ( strcmp(token,"F") ) tval = 0;
  341.             else //literals
  342.             {
  343.                 tval = atof(token);
  344.                
  345.             }
  346.             //push the token value onto the stack
  347.             stack[SP] = tval;
  348.        
  349.             //now out stack looks like:
  350.            
  351.             //: PARAMn where n is the loop iteration
  352.             //: PARAMn where n is the loop iteration
  353.             //: PARAMn where n is the loop iteration
  354.             //: INSTRUCTION
  355.            
  356.             ++SP; //this is why the stack size must be +1 larger than the3 total number of instructions and
  357.             //params that it can hold.
  358.             ++current_param;
  359.            
  360.         } //repeat this until we are out of params
  361.        
  362.     }
  363.    
  364.     void getVarValue(int str)
  365.     {
  366.         variables[VP] = atof(str);
  367.         ++VP;
  368.     }
  369.    
  370.     void execute()
  371.     {
  372.         int reg_ptr = 0; //read the stack starting here, until we reach TOP.
  373.         int args[MAX_ARGS];
  374.         //evaluate the instruction:
  375.         int instr = stack[reg_ptr];
  376.         int current_arg = 0;
  377.         int num_of_params = num_instruction_params(instr);
  378.         while( num_of_params > 0 )
  379.         {
  380.             ++reg_ptr;
  381.             args[current_arg] = stack[reg_ptr];
  382.         }
  383.         switch(instr)
  384.         {
  385.             case NONE:
  386.                 TraceError("STACK INSTRUCTION IS INVALID: ", instr); break;
  387.             case WARP:
  388.             {
  389.                 Link->Warp(args[0],args[1]);
  390.                 if ( log_actions ) TraceError("Cheat System Warped Link to dmap,screen:",args[0],args[1]);
  391.                 break;
  392.             }
  393.             case POS:
  394.             {
  395.                 Link->X = args[0];
  396.                 Link->X = args[1];
  397.                 if ( log_actions ) TraceError("Cheat System repositioned Link to X,Y:",args[0],args[1]);
  398.                 break;
  399.             }
  400.            
  401.             case MOVEX:
  402.             {
  403.                 Link->X += args[0];
  404.                 if ( log_actions ) TraceError("Cheat system moved Link on his X axis by: ", args[0]);
  405.                 break,
  406.             }
  407.             case MOVEY:
  408.             {
  409.                 Link->Y += args[0];
  410.                 if ( log_actions ) TraceError("Cheat system moved Link on his Y axis by", args[0]);
  411.                 break,
  412.             }
  413.             case REFILLHP:
  414.             {
  415.                 Link->HP =  Link->MaxHP;
  416.                 if ( log_actions ) TraceError("Cheat system refilled Link's HP to", Link->MaxHP);
  417.                 break,
  418.             }
  419.             case REFILLMP:
  420.             {
  421.                 Link->MP =  Link->MaxMP;
  422.                 if ( log_actions ) TraceError("Cheat system refilled Link's MP to", Link->MaxHP);
  423.                 break,
  424.             }
  425.             case REFILLCTR:
  426.             {
  427.                 Game->Counter[args[0]] =  Game->MCounter[args[0]];
  428.                 if ( log_actions ) TraceError("Cheat system refilled Counter", args[0]);
  429.                 break,
  430.             }
  431.             case MAXHP:
  432.             {
  433.                 Game->MCounter[CR_HP] = args[0];
  434.                 if ( log_actions ) TraceError("Cheat system set Link's Max HP to:",args[0]);
  435.                 break,
  436.             }
  437.             case MAXMP:
  438.             {
  439.                 Game->MCounter[CR_MP] = args[0];
  440.                 if ( log_actions ) TraceError("Cheat system set Link's Max MP to:",args[0]);
  441.                 break,
  442.             }
  443.             case MAXCTR:
  444.             {
  445.                 Game->Counter[args[0]] = args[1];
  446.                 if ( log_actions ) TraceError("Cheat system refilled Counter (id, amount):",args[0],args[1]);
  447.                 break,
  448.             }
  449.            
  450.             case INVINCIBLE
  451.             {
  452.                 if ( args[0] )
  453.                 {
  454.                     Link->Invisible = true;
  455.                     if ( log_actions ) TraceErrorS("Cheat system set Link's Invisibility state to ","true");
  456.                     break,
  457.                 }
  458.                 else
  459.                 {
  460.                     Link->Invisible = false;
  461.                     if ( log_actions ) TraceErrorS("Cheat system set Link's Invisibility state to ","false");
  462.                     break,
  463.                    
  464.                 }
  465.                
  466.             }
  467.             case LINKITEM:
  468.             {
  469.                 if ( args[1] )
  470.                 {
  471.                     Link->Item[args[0]] = true;
  472.                     if ( log_actions ) TraceErrorVS("Cheat system set Link's Inventory Item to (item, state)","true");
  473.                     break,
  474.                 }
  475.                 else
  476.                 {
  477.                     Link->Item[args[0]] = false;
  478.                     if ( log_actions ) TraceErrorVS("Cheat system set Link's Inventory Item to (item, state)","false");
  479.                     break,
  480.                    
  481.                 }
  482.             }
  483.             default:
  484.             {
  485.                
  486.                 TraceError("Invalid instruction passed to stack",instr);
  487.                 break;
  488.             }
  489.            
  490.            
  491.         }
  492.         ///-----later, we'll add this: //pop everything off of the stack
  493.         //just wipe the stack for now, as we only support one command at this time
  494.         for ( int q = 0; q < 3; ++q ) stack[q] = 0;
  495.         SP = 0;
  496.        
  497.        
  498.     }
  499.        
  500.     void run()
  501.     {
  502.    
  503.        
  504.        
  505.     }
  506. }
  507.  
  508. global script test
  509. {
  510.     void run()
  511.     {
  512.         while(1)
  513.         {
  514.             debugshell.process();
  515.             Waitdraw();
  516.             Waitframe();
  517.         }
  518.        
  519.     }
  520. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement