Advertisement
Janilabo

RSCR Text development #4

Jul 30th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 18.05 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.   area, bounds: TBox;
  127.   b, o, g, q, d, m, e, f, t, s, r, w, h, i, l, j, k, x, y, z: Integer;
  128.   offset: TPoint;
  129.   matrix: array[0..1] of array of TBoolArray;
  130.   required: TIntegerArray;
  131.   found: Integer;
  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.     if (l > -1) then
  146.       found := 1
  147.     else
  148.       found := 0;
  149.     z := (w - 1);
  150.     s := (Length(charset.loaded) - 1);
  151.     q := 0;
  152.     o := 0;
  153.     b := Max((buffer - 1), 0);
  154.     case found of
  155.       0:
  156.       for x := 0 to z do
  157.       begin
  158.         t := -1;
  159.         m := 0;
  160.         f := -1;
  161.         for e := 0 to b do
  162.           if ((f > -1) or (e = 0)) then
  163.           for i := 0 to s do
  164.           begin
  165.             g := charset.loaded[i];
  166.             k := charset.chars[g].size;
  167.             if (k > m) then
  168.               if not ((q + (x + e) + charset.chars[g].width) > w) then
  169.               begin
  170.                 offset := Point(((x + e) + q), 0);
  171.                 if MatrixMatch(matrix[0], charset.chars[g].pixels, offset) then
  172.                 begin
  173.                   f := e;
  174.                   t := g;
  175.                   m := k;
  176.                 end;
  177.               end;
  178.           end;
  179.         if (t > -1) then
  180.         begin
  181.           Result := (Result + StringOfChar(' ', (((((x + f) + q) - o) + 1) div d)));
  182.           q := ((q + charset.chars[t].width) - 1);
  183.           o := ((x + f) + q);
  184.           Result := (Result + Chr(t));
  185.           if (Length(Result) >= len) then
  186.           begin
  187.             SetLength(Result, len);
  188.             Exit;
  189.           end;
  190.         end;
  191.       end;
  192.       1:
  193.       begin
  194.         SetLength(matrix[1], w, h);
  195.         for i := 0 to l do
  196.           if ((shadow[i].X < w) and (shadow[i].Y < h)) then
  197.             matrix[1][shadow[i].X][shadow[i].Y] := True;
  198.         SetLength(required, (s + 1));
  199.         for i := 0 to s do
  200.         begin
  201.           g := charset.loaded[i];
  202.           required[i] := Round(Percentage(75.0, charset.chars[g].s_size));
  203.         end;
  204.         for x := 0 to z do
  205.         begin
  206.           t := -1;
  207.           m := 0;
  208.           f := -1;
  209.           for e := 0 to b do
  210.             if ((f > -1) or (e = 0)) then
  211.             for i := 0 to s do
  212.             begin
  213.               g := charset.loaded[i];
  214.               k := charset.chars[g].size;
  215.               if (k > m) then
  216.                 if not ((q + (x + e) + charset.chars[g].width) > w) then
  217.                 begin
  218.                   offset := Point(((x + e) + q), 0);
  219.                   if MatrixMatches(matrix[1], charset.chars[g].shadow, offset, required[i]) then
  220.                     if MatrixMatch(matrix[0], charset.chars[g].pixels, offset) then
  221.                     begin
  222.                       f := e;
  223.                       t := g;
  224.                       m := k;
  225.                     end;
  226.                 end;
  227.             end;
  228.           if (t > -1) then
  229.           begin
  230.             Result := (Result + StringOfChar(' ', (((((x + f) + q) - o) + 1) div d)));
  231.             q := ((q + charset.chars[t].width) - 1);
  232.             o := ((x + f) + q);
  233.             Result := (Result + Chr(t));
  234.             if (Length(Result) >= len) then
  235.             begin
  236.               SetLength(Result, len);
  237.               Exit;
  238.             end;
  239.           end;
  240.         end;
  241.       end;
  242.     end;
  243.   end;
  244. end;
  245.  
  246. function GetTextEx(position: TPoint; colors: TIntegerArray; len: Integer; buffer: Integer; charset: TCharset): string;
  247. var
  248.   area, bounds: TBox;
  249.   b, o, g, q, d, m, e, f, t, s, r, w, h, i, l, j, k, x, y, z: Integer;
  250.   p, c: TPoint;
  251.   a, v: TPointArray;
  252.   image: Integer;
  253.   matrix: array of TBoolArray;
  254.   u: TIntegerArray;
  255. begin
  256.   Result := '';
  257.   if (High(colors) > -1) then
  258.   begin
  259.     GetClientDimensions(w, h);
  260.     if PointInBox(position, IntToBox(0, 0, (w - 1), (h - 1))) then
  261.     begin
  262.       area := IntToBox(position.X, position.Y, (w - 1), (position.Y + charset.max_height));
  263.       if (area.Y2 < h) then
  264.         if FindColorEx(a, colors, area.X1, area.Y1, area.X2, area.Y2) then
  265.         begin
  266.           l := (Length(a) - 1);
  267.           OffsetTPA(a, Point(-position.X, -position.Y));
  268.           bounds := GetTPABounds(a);
  269.           d := charset.chars[32].width;
  270.           w := ((((bounds.X2 - 0) + charset.max_width) + 3));
  271.           h := ((area.Y2 - area.Y1) + 1);
  272.           SetLength(matrix, w, h);
  273.           for i := 0 to l do
  274.             matrix[a[i].X][a[i].Y] := True;
  275.           z := (w - 1);
  276.           s := (Length(charset.loaded) - 1);
  277.           q := 0;
  278.           o := 0;
  279.           b := Max((buffer - 1), 0);
  280.           for x := 0 to z do
  281.           begin
  282.             t := -1;
  283.             m := 0;
  284.             f := -1;
  285.             for e := 0 to b do
  286.               if ((f > -1) or (e = 0)) then
  287.               for i := 0 to s do
  288.               begin
  289.                 g := charset.loaded[i];
  290.                 if (charset.chars[g].size > 0) then
  291.                   if not ((q + (x + e) + charset.chars[g].width) > w) then
  292.                   begin
  293.                     k := (Length(charset.chars[g].pixels) - 1);
  294.                     r := 0;
  295.                     for j := 0 to k do
  296.                     begin
  297.                       c.X := ((charset.chars[g].pixels[j].X + (x + e)) + q);
  298.                       c.Y := charset.chars[g].pixels[j].Y;
  299.                       if not matrix[c.X][c.Y] then
  300.                         Break
  301.                       else
  302.                         r := (r + 1);
  303.                     end;
  304.                     if (r > k) then
  305.                       if (r > m) then
  306.                       begin
  307.                         f := e;
  308.                         t := g;
  309.                         m := r;
  310.                       end;
  311.                   end;
  312.               end;
  313.             if (t > -1) then
  314.             begin
  315.               Result := (Result + StringOfChar(' ', (((((x + f) + q) - o) + 1) div d)));
  316.               q := ((q + charset.chars[t].width) - 1);
  317.               o := ((x + f) + q);
  318.               Result := (Result + Chr(t));
  319.               if (Length(Result) >= len) then
  320.               begin
  321.                 SetLength(Result, len);
  322.                 SetLength(matrix, 0);
  323.                 Exit;
  324.               end;
  325.             end;
  326.           end;
  327.           SetLength(matrix, 0);
  328.         end;
  329.     end;
  330.   end;
  331. end;
  332.  
  333. function GetText(position: TPoint; colors: TIntegerArray; len: Integer; buffer: Integer; charset: TCharset): string;
  334.   function MatrixMatch(matrix: array of TBoolArray; TPA: TPointArray; offset: TPoint): Boolean;
  335.   var
  336.     i, l: Integer;
  337.   begin
  338.     l := (Length(TPA) - 1);
  339.     for i := 0 to l do
  340.       if not matrix[(TPA[i].X + offset.X)][(TPA[i].Y + offset.Y)] then
  341.         Exit(False);
  342.     Result := True;
  343.   end;
  344. var
  345.   area, bounds: TBox;
  346.   b, o, g, q, d, m, e, f, t, s, r, w, h, i, l, j, k, x, y, z: Integer;
  347.   p, c, offset: TPoint;
  348.   a, v: TPointArray;
  349.   image: Integer;
  350.   matrix: array of TBoolArray;
  351.   u: TIntegerArray;
  352. begin
  353.   Result := '';
  354.   if (High(colors) > -1) then
  355.   begin
  356.     GetClientDimensions(w, h);
  357.     if PointInBox(position, IntToBox(0, 0, (w - 1), (h - 1))) then
  358.     begin
  359.       area := IntToBox(position.X, position.Y, (w - 1), (position.Y + charset.max_height));
  360.       if (area.Y2 < h) then
  361.         if FindColorEx(a, colors, area.X1, area.Y1, area.X2, area.Y2) then
  362.         begin
  363.           l := (Length(a) - 1);
  364.           OffsetTPA(a, Point(-position.X, -position.Y));
  365.           bounds := GetTPABounds(a);
  366.           d := charset.chars[32].width;
  367.           w := ((((bounds.X2 - 0) + charset.max_width) + 3));
  368.           h := ((area.Y2 - area.Y1) + 1);
  369.           SetLength(matrix, w, h);
  370.           for i := 0 to l do
  371.             matrix[a[i].X][a[i].Y] := True;
  372.           z := (w - 1);
  373.           s := (Length(charset.loaded) - 1);
  374.           q := 0;
  375.           o := 0;
  376.           b := Max((buffer - 1), 0);
  377.           for x := 0 to z do
  378.           begin
  379.             t := -1;
  380.             m := 0;
  381.             f := -1;
  382.             for e := 0 to b do
  383.               if ((f > -1) or (e = 0)) then
  384.               for i := 0 to s do
  385.               begin
  386.                 g := charset.loaded[i];
  387.                 k := charset.chars[g].size;
  388.                 if (k > m) then
  389.                   if not ((q + (x + e) + charset.chars[g].width) > w) then
  390.                   begin
  391.                     offset := Point(((x + e) + q), 0);
  392.                     if MatrixMatch(matrix, charset.chars[g].pixels, offset) then
  393.                     begin
  394.                       f := e;
  395.                       t := g;
  396.                       m := k;
  397.                     end;
  398.                   end;
  399.               end;
  400.             if (t > -1) then
  401.             begin
  402.               Result := (Result + StringOfChar(' ', (((((x + f) + q) - o) + 1) div d)));
  403.               q := ((q + charset.chars[t].width) - 1);
  404.               o := ((x + f) + q);
  405.               Result := (Result + Chr(t));
  406.               if (Length(Result) >= len) then
  407.               begin
  408.                 SetLength(Result, len);
  409.                 SetLength(matrix, 0);
  410.                 Exit;
  411.               end;
  412.             end;
  413.           end;
  414.           SetLength(matrix, 0);
  415.         end;
  416.     end;
  417.   end;
  418. end;
  419.  
  420. function GetTextEx2(position: TPoint; colors: TIntegerArray; shadow, len: Integer; buffer: Integer; charset: TCharset): string;
  421.   function MatrixMatches(matrix: array of TBoolArray; TPA: TPointArray; offset: TPoint; needed: Integer): Boolean;
  422.   var
  423.     i, l, r: Integer;
  424.   begin
  425.     l := (Length(TPA) - 1);
  426.     r := 0;
  427.     for i := 0 to l do
  428.       if matrix[(TPA[i].X + offset.X)][(TPA[i].Y + offset.Y)] then
  429.       begin
  430.         r := (r + 1);
  431.         if (r >= needed) then
  432.           Break;
  433.       end;
  434.     Result := (r >= needed);
  435.   end;
  436.   function MatrixMatch(matrix: array of TBoolArray; TPA: TPointArray; offset: TPoint): Boolean;
  437.   var
  438.     i, l: Integer;
  439.   begin
  440.     l := (Length(TPA) - 1);
  441.     for i := 0 to l do
  442.       if not matrix[(TPA[i].X + offset.X)][(TPA[i].Y + offset.Y)] then
  443.         Exit(False);
  444.     Result := True;
  445.   end;
  446. var
  447.   area, bounds: TBox;
  448.   sk, sr, sm, b, o, g, q, d, m, e, f, t, s, r, w, h, i, l, j, k, x, y, z: Integer;
  449.   p, c, offset: TPoint;
  450.   sa, a, v: TPointArray;
  451.   matrix: array[0..1] of array of TBoolArray;
  452.   required: TIntegerArray;
  453.   u: TIntegerArray;
  454.   found: Boolean;
  455. begin
  456.   Result := '';
  457.   if (High(colors) > -1) then
  458.   begin
  459.     GetClientDimensions(w, h);
  460.     if PointInBox(position, IntToBox(0, 0, (w - 1), (h - 1))) then
  461.     begin
  462.       area := IntToBox(position.X, position.Y, (w - 1), (position.Y + charset.max_height));
  463.       if (area.Y2 < h) then
  464.         if FindColors(sa, shadow, area.X1, area.Y1, area.X2, area.Y2) then
  465.           if FindColorEx(a, colors, area.X1, area.Y1, area.X2, area.Y2) then
  466.           begin
  467.             l := (Length(a) - 1);
  468.             OffsetTPA(a, Point(-position.X, -position.Y));
  469.             OffsetTPA(sa, Point(-position.X, -position.Y));
  470.             bounds := GetTPABounds(a);
  471.             d := charset.chars[32].width;
  472.             w := ((((bounds.X2 - 0) + charset.max_width) + 3));
  473.             h := ((area.Y2 - area.Y1) + 1);
  474.             SetLength(matrix[0], w, h);
  475.             for i := 0 to l do
  476.               matrix[0][a[i].X][a[i].Y] := True;
  477.             l := (Length(sa) - 1);
  478.             SetLength(matrix[1], w, h);
  479.             for i := 0 to l do
  480.               if ((sa[i].X < w) and (sa[i].Y < h)) then
  481.                 matrix[1][sa[i].X][sa[i].Y] := True;
  482.             z := (w - 1);
  483.             s := (Length(charset.loaded) - 1);
  484.             q := 0;
  485.             o := 0;
  486.             b := Max((buffer - 1), 0);
  487.             SetLength(required, (s + 1));
  488.             for i := 0 to s do
  489.             begin
  490.               g := charset.loaded[i];
  491.               required[i] := Round(Percentage(75.0, charset.chars[g].s_size));
  492.             end;
  493.             for x := 0 to z do
  494.             begin
  495.               t := -1;
  496.               m := 0;
  497.               f := -1;
  498.               for e := 0 to b do
  499.                 if ((f > -1) or (e = 0)) then
  500.                 for i := 0 to s do
  501.                 begin
  502.                   g := charset.loaded[i];
  503.                   k := charset.chars[g].size;
  504.                   if (k > m) then
  505.                     if not ((q + (x + e) + charset.chars[g].width) > w) then
  506.                     begin
  507.                       offset := Point(((x + e) + q), 0);
  508.                       if MatrixMatches(matrix[1], charset.chars[g].shadow, offset, required[i]) then
  509.                         if MatrixMatch(matrix[0], charset.chars[g].pixels, offset) then
  510.                         begin
  511.                           f := e;
  512.                           t := g;
  513.                           m := k;
  514.                         end;
  515.                     end;
  516.                 end;
  517.               if (t > -1) then
  518.               begin
  519.                 Result := (Result + StringOfChar(' ', (((((x + f) + q) - o) + 1) div d)));
  520.                 q := ((q + charset.chars[t].width) - 1);
  521.                 o := ((x + f) + q);
  522.                 Result := (Result + Chr(t));
  523.                 if (Length(Result) >= len) then
  524.                 begin
  525.                   SetLength(Result, len);
  526.                   Exit;
  527.                 end;
  528.               end;
  529.             end;
  530.           end;
  531.     end;
  532.   end;
  533. end;
  534.  
  535. var
  536.   chars: TCharset;
  537.   new, old: string;
  538.   pixels, shadow: TPointArray;
  539.  
  540. function PickTextTPA(var TPA: TPointArray; position: TPoint; colors: TIntegerArray; charset: TCharset): Boolean;
  541. var
  542.   a: TBox;
  543.   w, h: Integer;
  544. begin
  545.   SetLength(TPA, 0);
  546.   Result := False;
  547.   GetClientDimensions(w, h);
  548.   if PointInBox(position, IntToBox(0, 0, (w - 1), (h - 1))) then
  549.   begin
  550.     a := IntToBox(position.X, position.Y, (w - 1), (position.Y + charset.max_height));
  551.     if (a.Y2 < h) then
  552.       Result := FindColorEx(TPA, colors, a.X1, a.Y1, a.X2, a.Y2);
  553.   end;
  554.   if Result then
  555.     OffsetTPA(TPA, Point(-position.X, -position.Y));
  556. end;
  557.  
  558. begin
  559.   chars := LoadCharset(ScriptPath + 'RSCR_Main2\');
  560.   ActivateClient;
  561.   Wait(1000);
  562.   SetLength(shadow, 0);
  563.   repeat
  564.     if PickTextTPA(shadow, Point(6, 5), [0], chars) then
  565.       if PickTextTPA(pixels, Point(6, 5), [65535, 16777215, 16776960, 4231423], chars) then
  566.       begin
  567.         new := GetTextTPA(pixels, shadow, 50, 1, chars);
  568.         if (new <> '') then
  569.           if (new <> old) then
  570.           begin
  571.             ClearDebug;
  572.             WriteLn(new);
  573.             old := new;
  574.           end;
  575.       end;
  576.   until IsKeyDown(VK_F12);
  577. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement