Results 1 to 1 of 1

Thread: Can someone help me optimize this?

  1. #1
    Join Date
    Feb 2008
    Posts
    517
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default Can someone help me optimize this?

    SCAR Code:
    var keyboard, shiftboard, current: array [0..3] of string;
        mistypeprobability:integer;
        shift:string;
       
        { How to use:
                      1. Create a new integer constant in your code called typespeed, lower the
                      number faster the typing, althought it will generate more mistakes.
                      2. Initialize keyboard variables by running the KeyboardVars() procedure.
                      3. You're done!
                                                                                                  }


        { Things that are not finished:

                      ~ Optimizing code for best performance.

                                                                                                  }


    procedure KeyboardVars;
    begin
         keyboard[0] := '`1234567890-= ';   shiftboard[0] := '~!@#$%^&*()_+' ;
         keyboard[1] := ' qwertyuiop[]\';   shiftboard[1] := ' QWERTYUIOP{}|';
         keyboard[2] := ' asdfghjkl;''' ;   shiftboard[2] := ' ASDFGHJKL:"'  ;
         keyboard[3] := ' zxcvbnm,./'   ;   shiftboard[3] := ' ZXCVBNM<>?'   ;
         shift := '~!@#$%^&*()_+QWERTYUIOP}{|ASDFGHJKL:"ZXCVBNM<>?' ;
         writeln('Initialized Feroc1ty Keyboard.scar');
    end;

    procedure Letter(character:char);
    var key:byte;
    begin
         key := GetKeyCode(character);
         if (Pos(character, shift) <> 0) then
         begin
             Wait(random(5));
             KeyDown(VK_SHIFT);
             Wait(40+Random(30));
             KeyDown(key);
             Wait(10+Random(30));
             KeyUp(key);
             Wait(40+Random(30));
             KeyUp(VK_SHIFT);
             Wait(random(5));
             Exit;
         end;
         Wait(random(5));
         KeyDown(key);
         Wait(10+Random(30));
         KeyUp(key);
         Wait(random(5));
    end;

    procedure PressEnter();
    begin
         Letter(Chr(13));
    end;

    procedure PressBack();
    begin
         Letter(Chr(8));
    end;

    function TypeLetter(character:char; mistake:boolean):integer;
    var i, position, positioni:integer;
        key:char;
    begin
         result := 0;
         if character = ' ' then
         begin
              Letter(' ');
              Exit;
         end;
         current := keyboard;
         if Pos(character, shift) <> 0 then
            current := shiftboard;
         for i := 0 to 3 do
         begin
              if Pos(character, current[i]) <> 0 then
                 positioni := i;
         end;
         position := Pos(character, current[positioni]);
         key := current[positioni][position];
         if mistake then
         begin
              case Random(120 + mistypeprobability) of
                0..2: // Longer wait, ex. forget key location or something
                begin
                     Wait(20+Random(100));
                     mistypeprobability := mistypeprobability + 10 + typespeed;
                end;
                3..10: // Shorter wait, ex. small brainfart
                begin
                     Wait(Random(30));
                     mistypeprobability := mistypeprobability + 5 + typespeed;
                end;
                16: // No letter typed
                begin
                     result := 1;
                     Exit;
                end;
                17..19: // Chance to mistype letter on the same row
                begin
                     position := position - 1 + Random(3);
                     if position < 1 then
                        position := 1;
                     if (positioni > 0) and (position < 2) then
                        position := 2;
                     if position > Length(current[positioni]) then
                        position := Length(current[positioni]);
                     if key <> current[positioni][position] then
                     begin
                        key := current[positioni][position];
                        result := 2;
                        mistypeprobability := mistypeprobability + 25 + typespeed;
                     end;
                end;
                21: // Mistype the letter upward
                begin
                     positioni := positioni + 1;
                     if positioni > 3 then
                        positioni := 3;
                     if key <> current[positioni][position] then
                     begin
                        key := current[positioni][position];
                        result := 2;
                        mistypeprobability := mistypeprobability + 25 + typespeed;
                     end;
                end;
                22: // Mistype the letter downward
                begin
                     positioni := positioni - 1;
                     if positioni < 0 then
                        positioni := 0;
                     if key <> current[positioni][position] then
                     begin
                        key := current[positioni][position];
                        result := 2;
                        mistypeprobability := mistypeprobability + 5 + typespeed;
                     end;
                end;
                23..300: // No mistype, increase the chance to mistype for the next letter
                begin
                     mistypeprobability := mistypeprobability - 1;
                end;
              end;
         end;
         Letter(key);
    end;

    procedure TypeText(text:string;mistake,fix:boolean);
    var i,n,t,b:integer;
    begin

       i := 1;
       n := 0;
       b := 0;
       mistypeprobability := 70 + typespeed*5;
       
       while (i <= Length(text)) do
       begin
          if fix then
          begin
             if b = 0 then
             begin
                b := TypeLetter(text[i],mistake);
                if b <> 0 then
                   n := i;
             end else
                TypeLetter(text[i],mistake);
               
             if ((random(6) = 5) and (b <> 0)) or ((Length(text) - i < 2) and (b <> 0)) then
             begin
                Wait(99+Random(300));
                if b = 2 then
                begin
                   Wait(20+Random(50));
                   PressBack;
                end;
                for t := 1 to (i - n) do
                begin
                   Wait(20+Random(50));
                   PressBack;
                end;
                i := n - 1;
                b := 0;
             end;
             
          end else
          begin
             TypeLetter(text[i],mistake);
          end;
          i := i + 1;
          Wait(5+typespeed+Random(10+6*typespeed));
       end;
    end;

    I cooked up this piece of code a long while ago as an include for myself, and now need to use it again, can anyone help me optimize this code?

    As I was looking through it I found a bunch of crap that was needed to be optimized and fixed it, but optimizing your own code doesn't do much as you're usually blind to simple things that could have been done much easier.
    Last edited by Feroc1ty; 10-12-2010 at 09:20 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •