Janilabo

RSCR Text development #6

Jul 31st, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 8.88 KB | None | 0 0
  1. const
  2.   BUFFER = 2; // 1-3!
  3.   TEXT_LENGTH = 70; // Maximum length to use for GetText..
  4.   SCAN_SHADOW = True; // Look for shadow pixels in GetText matching?
  5.  
  6. type
  7.   TCharset = record
  8.     chars: array[32..126] of record
  9.       shape: record
  10.         pixels: TPointArray;
  11.         size, width, height: Integer;
  12.         bounds: TBox;
  13.       end;
  14.       shadow: record
  15.         pixels: TPointArray;
  16.         size: Integer;
  17.       end;
  18.       width, height: Integer;
  19.     end;
  20.     max_width, max_height: Integer;
  21.     loaded: TIntegerArray;
  22.   end;
  23.  
  24. procedure InsertTIA(var arr: TIntegerArray; index: Integer; item: Integer);
  25. var
  26.   i, l, j, k: Integer;
  27. begin
  28.   l := (Length(arr) - 1);
  29.   SetLength(arr, (l + 2));
  30.   j := index;
  31.   k := (l + 1);
  32.   if (j < 0) then
  33.     j := 0
  34.   else
  35.     if (j > k) then
  36.       j := k;
  37.   if (k > j) then
  38.   for i := l downto j do
  39.     arr[(i + 1)] := arr[i];
  40.   arr[j] := item;
  41. end;
  42.  
  43. procedure AppendTIA(var arr: TIntegerArray; item: Integer);
  44. var
  45.   l: Integer;
  46. begin
  47.   l := (Length(arr) + 1);
  48.   SetLength(arr, l);
  49.   arr[(l - 1)] := item;
  50. end;
  51.  
  52. function LoadCharset(path: string): TCharset;
  53. var
  54.   h, i, j, k, g, v, l, w, s, p: Integer;
  55.   b: TStringArray;
  56.   n: string;
  57.   d: Integer;
  58. begin
  59.   b := GetFiles(path, 'bmp');
  60.   h := High(b);
  61.   l := 0;
  62.   Result.max_width := 0;
  63.   Result.max_height := 0;
  64.   SetLength(Result.loaded, 0);
  65.   for i := 0 to h do
  66.   begin
  67.     n := Copy(b[i], 1, (Length(b[i]) - 4));
  68.     if (n = ExtractFromStr(b[i], Numbers)) then
  69.     begin
  70.       v := StrToInt(n);
  71.       if InRange(v, 32, 126) then
  72.       begin
  73.         d := LoadBitmap(path + b[i]);
  74.         GetBitmapSize(d, Result.chars[v].width, Result.chars[v].height);
  75.         if (Result.chars[v].width > Result.max_width) then
  76.           Result.max_width := Result.chars[v].width;
  77.         if (Result.chars[v].height > Result.max_height) then
  78.           Result.max_height := Result.chars[v].height;
  79.         if (v <> 32) then
  80.         begin
  81.           FindColorsBitmap(d, Result.chars[v].shape.pixels, 16777215);
  82.           FindColorsBitmap(d, Result.chars[v].shadow.pixels, 255);
  83.           Result.chars[v].shape.bounds := GetTPABounds(Result.chars[v].shape.pixels);
  84.           Result.chars[v].shape.width := ((Result.chars[v].shape.bounds.X2 - Result.chars[v].shape.bounds.X1) + 1);
  85.           Result.chars[v].shape.height := ((Result.chars[v].shape.bounds.Y2 - Result.chars[v].shape.bounds.Y1) + 1);
  86.           Result.chars[v].shape.size := Length(Result.chars[v].shape.pixels);
  87.           Result.chars[v].shadow.size := Length(Result.chars[v].shadow.pixels);
  88.           w := Result.chars[v].shape.width;
  89.           s := Result.chars[v].shape.size;
  90.           p := -1;
  91.           k := (l - 1);
  92.           for j := 0 to k do
  93.           begin
  94.             g := Result.loaded[j];
  95.             if ((s > Result.chars[g].shape.size) or ((s = Result.chars[g].shape.size) and (w > Result.chars[g].shape.width))) then
  96.             begin
  97.               p := j;
  98.               Break;
  99.             end;
  100.           end;
  101.           if (p = -1) then
  102.             AppendTIA(Result.loaded, v)
  103.           else
  104.             InsertTIA(Result.loaded, p, v);
  105.         end else
  106.         begin
  107.           Result.chars[v].shape.size := 0;
  108.           AppendTIA(Result.loaded, v);
  109.         end;
  110.         l := (l + 1);
  111.         FreeBitmap(d);
  112.       end;
  113.     end;
  114.   end;
  115.   SetLength(b, 0);
  116. end;
  117.  
  118. function Percentage(percent, source: Extended): Extended;
  119. begin
  120.   case (percent = 0) of
  121.     False: Result := ((percent / 100.0) * source);
  122.     True: Result := 0.0;
  123.   end;
  124. end;
  125.  
  126. function FindColorEx(var TPA: TPointArray; colors: TIntegerArray; XS, YS, XE, YE: Integer): Boolean;
  127. var
  128.   c: TIntegerArray;
  129.   t: TPointArray;
  130.   h, i, j, l, r: Integer;
  131. begin
  132.   h := High(colors);
  133.   if not (h = 0) then
  134.   begin
  135.     r := 0;
  136.     if (h > -1) then
  137.     begin
  138.       t := TPAFromBox(IntToBox(XS, YS, XE, YE));
  139.       l := (Length(t) - 1);
  140.       if (l > -1) then
  141.       begin
  142.         c := GetColors(t);
  143.         SetLength(TPA, (l + 1));
  144.         for i := 0 to l do
  145.           for j := 0 to h do
  146.             if (c[i] = colors[j]) then
  147.             begin
  148.               TPA[r] := t[i];
  149.               r := (r + 1);
  150.               Break;
  151.             end;
  152.       end;
  153.     end;
  154.     SetLength(TPA, r);
  155.     Result := (r > 0);
  156.   end else
  157.     Result := FindColors(TPA, colors[0], XS, YS, XE, YE);
  158. end;
  159.  
  160. function GetTextTPA(pixels, shadow: TPointArray; len: Integer; buffer: Integer; charset: TCharset): string;
  161.   function MatrixMatches(matrix: array of TBoolArray; TPA: TPointArray; offset: TPoint; needed: Integer): Boolean;
  162.   var
  163.     i, l, r: Integer;
  164.   begin
  165.     l := (Length(TPA) - 1);
  166.     r := 0;
  167.     for i := 0 to l do
  168.       if matrix[(TPA[i].X + offset.X)][(TPA[i].Y + offset.Y)] then
  169.       begin
  170.         r := (r + 1);
  171.         if (r >= needed) then
  172.           Break;
  173.       end;
  174.     Result := (r >= needed);
  175.   end;
  176.   function MatrixMatch(matrix: array of TBoolArray; TPA: TPointArray; offset: TPoint): Boolean;
  177.   var
  178.     i, l: Integer;
  179.   begin
  180.     l := (Length(TPA) - 1);
  181.     for i := 0 to l do
  182.       if not matrix[(TPA[i].X + offset.X)][(TPA[i].Y + offset.Y)] then
  183.         Exit(False);
  184.     Result := True;
  185.   end;
  186. var
  187.   b, o, g, q, m, j, d, e, f, t, s, w, h, i, l, k, x, z: Integer;
  188.   matrix: array[0..1] of array of TBoolArray;
  189.   required: TIntegerArray;
  190.   scan, found: Boolean;
  191.   offset: TPoint;
  192.   bounds: TBox;
  193. begin
  194.   Result := '';
  195.   if (High(pixels) > -1) then
  196.   begin
  197.     bounds := GetTPABounds(pixels);
  198.     d := charset.chars[32].width;
  199.     w := (((bounds.X2 + charset.max_width) + 3));
  200.     h := (charset.max_height + 1);
  201.     SetLength(matrix[0], w, h);
  202.     l := (Length(pixels) - 1);
  203.     for i := 0 to l do
  204.       matrix[0][pixels[i].X][pixels[i].Y] := True;
  205.     l := (Length(shadow) - 1);
  206.     scan := (l > -1);
  207.     z := (w - 1);
  208.     s := (Length(charset.loaded) - 1);
  209.     q := 0;
  210.     o := 0;
  211.     b := Max((buffer - 1), 0);
  212.     if scan then
  213.     begin
  214.       SetLength(matrix[1], w, h);
  215.       for i := 0 to l do
  216.         if ((shadow[i].X < w) and (shadow[i].Y < h)) then
  217.           matrix[1][shadow[i].X][shadow[i].Y] := True;
  218.       SetLength(required, (s + 1));
  219.       for i := 0 to s do
  220.       begin
  221.         g := charset.loaded[i];
  222.         required[i] := Round(Percentage(75.0, charset.chars[g].shadow.size));
  223.       end;
  224.     end;
  225.     for x := 0 to z do
  226.     begin
  227.       t := -1;
  228.       f := -1;
  229.       j := s;
  230.       for e := 0 to b do
  231.       begin
  232.         i := -1;
  233.         if ((f > -1) or (e = 0)) then
  234.         while (i < j) do
  235.         begin
  236.           i := (i + 1);
  237.           g := charset.loaded[i];
  238.           if not ((q + (x + e) + charset.chars[g].width) > w) then
  239.           begin
  240.             offset := Point(((x + e) + q), 0);
  241.             found := not scan;
  242.             if not found then
  243.               found := MatrixMatches(matrix[1], charset.chars[g].shadow.pixels, offset, required[i]);
  244.             if found then
  245.               if MatrixMatch(matrix[0], charset.chars[g].shape.pixels, offset) then
  246.               begin
  247.                 f := e;
  248.                 t := g;
  249.                 j := i;
  250.                 Break;
  251.               end;
  252.           end;
  253.         end;
  254.       end;
  255.       if (t > -1) then
  256.       begin
  257.         if (((((x + f) + q) - o) + 1) > d) then
  258.           Result := (Result + StringOfChar(' ', (((((x + f) + q) - o) + 1) div d)));
  259.         q := ((q + charset.chars[t].width) - 1);
  260.         o := ((x + f) + q);
  261.         Result := (Result + Chr(t));
  262.         if (Length(Result) >= len) then
  263.         begin
  264.           SetLength(Result, len);
  265.           Exit;
  266.         end;
  267.       end;
  268.     end;
  269.   end;
  270. end;
  271.  
  272. var
  273.   chars: TCharset;
  274.   new, old: string;
  275.   pixels, shadow: TPointArray;
  276.   found: Boolean;
  277.  
  278. function PickTextTPA(var TPA: TPointArray; position: TPoint; colors: TIntegerArray; charset: TCharset): Boolean;
  279. var
  280.   a: TBox;
  281.   w, h: Integer;
  282. begin
  283.   SetLength(TPA, 0);
  284.   Result := False;
  285.   GetClientDimensions(w, h);
  286.   if PointInBox(position, IntToBox(0, 0, (w - 1), (h - 1))) then
  287.   begin
  288.     a := IntToBox(position.X, position.Y, (w - 1), (position.Y + charset.max_height));
  289.     if (a.Y2 < h) then
  290.       Result := FindColorEx(TPA, colors, a.X1, a.Y1, a.X2, a.Y2);
  291.   end;
  292.   if Result then
  293.     OffsetTPA(TPA, Point(-position.X, -position.Y));
  294. end;
  295.  
  296. begin
  297.   chars := LoadCharset(ScriptPath + 'RSCR_Main\');
  298.   ActivateClient;
  299.   Wait(1000);
  300.   SetLength(shadow, 0);
  301.   repeat
  302.     found := not SCAN_SHADOW;
  303.     if not found then
  304.       found := PickTextTPA(shadow, Point(6, 5), [0], chars);
  305.     if found then
  306.       if PickTextTPA(pixels, Point(6, 5), [65535, 16777215, 16776960, 4231423, 255, 65408], chars) then
  307.       begin
  308.         new := GetTextTPA(pixels, shadow, TEXT_LENGTH, BUFFER, chars);
  309.         if (new <> '') then
  310.           if (new <> old) then
  311.           begin
  312.             ClearDebug;
  313.             WriteLn(new);
  314.             old := new;
  315.           end;
  316.       end;
  317.   until IsKeyDown(VK_F12);
  318. end.
Advertisement
Add Comment
Please, Sign In to add comment