Advertisement
ZoriaRPG

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

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