Janilabo

RSCR Text development #5

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