Advertisement
ZoriaRPG

Debug Cheat Shell (v1.4, + Instruction QUEUE, WIP)

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