Advertisement
mixster

mixster

Jan 3rd, 2009
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.05 KB | None | 0 0
  1. program Meh;
  2. type
  3.   TTile = record
  4.     t: TPoint;
  5.     o: Boolean;
  6.   end;
  7.   TBoard = record
  8.     t: array of array of TTile;
  9.     h, w, ground, sprite: Integer;
  10.   end;
  11.   TPiece = record
  12.     t: TIntegerArray;
  13.     h: Integer;
  14.   end;
  15.   TPieceSet = record
  16.     p: array of TPiece;
  17.     s: TPoint;
  18.     h, sl: Integer;
  19.   end;
  20.   TUnitSkel = record
  21.     t: array of Integer;
  22.     s: TPoint;
  23.     h: Integer;
  24.   end;
  25.   TUnit = record
  26.     p: TPoint;
  27.     t: Integer;
  28.   end;
  29.   TFaction = record
  30.     u: array of TUnit;
  31.     h, c: Integer;
  32.   end;
  33.   TUnits = record
  34.     t: array of TFaction;
  35.     h: Integer;
  36.   end;
  37.  
  38. var
  39.   frmMain: TForm;
  40.   imgBoard: TImage;
  41.   sbxBoard: TScrollBox;
  42.   board: TBoard;
  43.   pieceset: TPieceset;
  44.   unitTypes: TUnitSkel;
  45.   units: TUnits;
  46.  
  47. procedure Write(v: TVariantArray);
  48. var
  49.   i: Integer;
  50.   s: string;
  51. begin
  52.   if False then
  53.     exit;
  54.   if High(v) < 0 then
  55.     exit;
  56.   for i := 0 to High(v) do
  57.     s := s + v[i];
  58.   Writeln(s);
  59. end;
  60.  
  61. procedure SetupBoard(var brd: TBoard; pce: TPieceSet; w, h: Integer);
  62. var
  63.   x, y: Integer;
  64. begin
  65.   brd.w := w - 1;
  66.   brd.h := h - 1;
  67.   SetLength(brd.t, h);
  68.   for y := 0 to brd.h do
  69.   begin
  70.     SetLength(brd.t[y], w);
  71.     for x := 0 to brd.w do
  72.     begin
  73.       brd.t[y][x].t.x := 0;
  74.       brd.t[y][x].t.y := 0;
  75.       brd.t[y][x].o := False;
  76.     end;
  77.   end;
  78.   x := (w * ((pce.s.x + pce.sL) * 2)) - pce.sL;
  79.   y := h * (pce.s.y * 2);
  80.   brd.ground := BitmapFromString(x, y, '');
  81.   FastDrawClear(brd.ground, clBlack);
  82.   brd.sprite := BitmapFromString(x, y, '');
  83.   FastDrawClear(brd.sprite, clWhite);
  84. end;
  85.  
  86. procedure RandomBoard(var brd: TBoard; pce: TPieceset);
  87. var
  88.   i, ii: Integer;
  89. begin
  90.   for i := 0 to brd.h do
  91.     for ii := 0 to brd.w do
  92.       with brd.t[i][ii].t do
  93.       begin
  94.         y := Random(pce.h + 1);
  95.         x := Random(pce.p[y].h + 1);
  96.       end;
  97. end;
  98.  
  99. procedure SetupPieceset(var pce: TPieceSet; bmp, tw, th: Integer);
  100. var
  101.   l, wl, hl, m, x, y, tb, ii, i: Integer;
  102.   c, ts: TCanvas;
  103.   p: TPointArray;
  104. begin
  105.   l := 20;
  106.   pce.sL := l;
  107.   pce.s.x := Round(Sin(Radians(30)) * l);
  108.   pce.s.y := Round(Cos(Radians(30)) * l);
  109.   wl := (2 * pce.s.x) + l;
  110.   hl := 2 * pce.s.y;
  111.   m := BitmapFromString(wl, hl, '');
  112.   FastDrawClear(m, clBlack);
  113.   c := GetBitmapCanvas(m);
  114.   c.Pen.Color := RGBtoColor(1, 1, 1);
  115.   c.Brush.Color := clWhite;
  116.   with pce.s do
  117.     c.Polygon([Point(0, y), Point(x, y + y), Point(x + l, y + y), Point(x + l + x, y), Point(x + l, 0), Point(x, 0)]);
  118.   SetTransparentColor(m, clWhite);
  119.   GetBitmapSize(bmp, x, y);
  120.   x := x / tw;
  121.   y := y / th;
  122.   tb := BitmapFromString(x, y, '');
  123.   FastDrawClear(tb, 0);
  124.   SetTargetBitmap(bmp);
  125.   ts := GetBitmapCanvas(bmp);
  126.   pce.h := th - 1;
  127.   SetLength(pce.p, th);
  128.   for ii := 0 to pce.h do
  129.     with pce.p[ii] do
  130.     begin
  131.       h := -1;
  132.       for i := 0 to tw - 1 do
  133.       begin
  134.         SetLength(p, 0);
  135.         FindColorsTolerance(p, clBlack, x * i, y * ii, (x * i) + wl, (y * ii) + hl, 0);
  136.         if Length(p) >= ((wl + 1) * (hl + 1)) then
  137.           Break;
  138.         Inc(h);
  139.         SetLength(t, h + 1);
  140.         t[h] := BitmapFromString(wl, hl, '');
  141.         SafeCopyCanvas(ts, GetBitmapCanvas(t[h]), x * i, y * ii, x * i + wl, y * ii + hl, 0, 0, wl, hl);
  142.         FastDrawTransparent(0, 0, m, t[h]);
  143.         SetTransparentColor(t[h], clBlack);
  144.       end;
  145.     end;
  146.   FreeBitmap(m);
  147.   ResetDc;
  148. end;
  149.  
  150. procedure SetupUnitTypes(var unTy: TUnitSkel; pce: TPieceSet; bmp, l: Integer);
  151. var
  152.   i, w: Integer;
  153.   c, t: TCanvas;
  154. begin
  155.   w := pce.s.y * 2;
  156.   unTy.h := l - 1;
  157.   GetBitmapSize(bmp, unTy.s.x, unTy.s.y);
  158.   unTy.s.x := unTy.s.x / l;
  159.   SetLength(unTy.t, l);
  160.   c := GetBitmapCanvas(bmp);
  161.   for i := 0 to unTy.h do
  162.   begin
  163.     unTy.t[i] := BitmapFromString(w, w, '');
  164.     t := GetBitmapCanvas(unTy.t[i]);
  165.     SafeCopyCanvas(c, t, i * unTy.s.x, 0, i * unTy.s.x + unTy.s.x, unTy.s.y, 0, 0, w, w);
  166.     SetTransparentColor(unTy.t[i], clWhite);
  167.   end;
  168. end;
  169.  
  170. procedure RandomUnits(var uns: TUnits; unTy: TUnitSkel; brd: TBoard; num: Integer);
  171. var
  172.   i: Integer;
  173. begin
  174.   uns.h := 0;
  175.   SetLength(uns.t, 1);
  176.   uns.t[0].h := num - 1;
  177.   SetLength(uns.t[0].u, num);
  178.   for i := 0 to num - 1 do
  179.     with uns.t[0].u[i] do
  180.     begin
  181.       t := Random(unTy.h + 1);
  182.       p.x := Random(brd.w + 1);
  183.       p.y := Random(brd.h + 1);
  184.       Write([i, ' of ', num, ' - ', p.x, ',', p.y, ',', t]);
  185.     end;
  186. end;
  187.  
  188. procedure DrawBoard(var img: TImage; brd: TBoard; pce: TPieceSet);
  189. var
  190.   t, i, ii, w, h, l, o: Integer;
  191. begin
  192.   FastDrawClear(t, clBlack);
  193.   l := pce.sL;
  194.   w := (pce.s.x + l) * 2;
  195.   h := pce.s.y;
  196.   img.Width := w * brd.w + w + 1 - l;
  197.   img.Height := h * brd.h + h + h + 1;
  198.   t := BitmapFromString(img.Width, img.Height, '');
  199.  
  200.   for ii := 0 to brd.h do
  201.   begin
  202.     if ii mod 2 = 0 then
  203.       o := 0
  204.     else
  205.       o := pce.s.x + l;
  206.     for i := 0 to brd.w do
  207.     begin
  208.       if o > 0 then
  209.         if i = brd.w then
  210.           Break;
  211.       with brd.t[ii][i].t do
  212.       begin
  213.         if (y > pce.h) then
  214.           Continue;
  215.         if (x > pce.p[y].h) then
  216.           Continue;
  217.         FastDrawTransparent((i * w) + o, (ii * h), pce.p[y].t[x], t);
  218.       end;
  219.     end;
  220.   end;
  221.  
  222.   SafeDrawBitmap(t, img.Canvas, 0, 0);
  223.   FastDrawTransparent(0, 0, t, brd.ground);
  224.   FreeBitmap(t);
  225. end;
  226.  
  227. procedure DrawUnits(var img: TImage; uni: TUnits; unTy: TUnitSkel; brd: TBoard; pce: TPieceSet);
  228. var
  229.   i, ii, w, h, o, l: Integer;
  230. begin
  231.   FastDrawClear(brd.sprite, clWhite);
  232.   w := (pce.s.x + pce.sL) * 2;
  233.   h := pce.s.y;
  234.   l := ((w - (2 * h)) - pce.sL) / 2;
  235.   for i := 0 to uni.h do
  236.     for ii := 0 to uni.t[i].h do
  237.       with uni.t[i].u[ii] do
  238.       begin
  239.         if p.y > brd.h then
  240.           Continue;
  241.         if p.x > brd.w then
  242.           Continue;
  243.         if p.y mod 2 = 0 then
  244.           o := 0
  245.         else
  246.           o := pce.s.x + pce.sL;
  247.  
  248.         Write([i, ',', ii, ' - ', p.x, ',', p.y, ' - ', (p.x * w) + o + l, ',', p.y * h, ',', unTy.t[t]]);
  249.         FastDrawTransparent((p.x * w) + o + l, p.y * h, unTy.t[t], brd.sprite);
  250.       end;
  251.  
  252.   GetBitmapSize(brd.ground, w, h);
  253.   i := BitmapFromString(w, h, '');
  254.   FastDrawTransparent(0, 0, brd.ground, i);
  255.   SetTransparentColor(brd.sprite, clWhite);
  256.   FastDrawTransparent(0, 0, brd.sprite, i);
  257.   SafeDrawBitmap(i, img.Canvas, 0, 0);
  258.   FreeBitmap(i);
  259. end;
  260.  
  261. procedure SetupForm;
  262. begin
  263.   frmMain := CreateForm;
  264.   with frmMain do
  265.   begin
  266.     ClientWidth := 600;
  267.     ClientHeight := 500;
  268.     Position:= poScreenCenter;
  269.     Caption := 'Strategy Game by mixster';
  270.   end;
  271.  
  272.   sbxBoard := TScrollBox.Create(frmMain);
  273.   with sbxBoard do
  274.   begin
  275.     Parent := frmMain;
  276.     Width := 500;
  277.     Height := 400;
  278.     Left := 50;
  279.     Top := 50;
  280.   end;
  281.  
  282.   imgBoard := TImage.Create(frmMain);
  283.   with imgBoard do
  284.   begin
  285.     Parent := sbxBoard;
  286.     Width := 800;
  287.     Height := 600
  288.     Left := 0;
  289.     Top := 0;
  290.     DrawBoard(imgBoard, board, pieceset);
  291.     DrawUnits(imgBoard, units, unitTypes, board, pieceset);
  292.   end;
  293.  
  294.   frmMain.ShowModal;
  295. end;
  296.  
  297. procedure LaunchForm;
  298. var
  299.   v: TVariantArray;
  300. begin
  301.   ThreadSafeCall('SetupForm', v);
  302. end;
  303.  
  304. procedure ScriptTerminate;
  305. var
  306.   i: Integer;
  307. begin
  308.   i := 0;
  309.   try
  310.     FreeBitmap(i);
  311.     Inc(i);
  312.   except
  313.   end;
  314. end;
  315.  
  316. var
  317.   t, i, ii: Integer;
  318. begin
  319.   Write(['Begin']);
  320.  
  321.   t := BitmapFromString(200, 400, '');
  322.   for i := 0 to 7 do
  323.     for ii := 0 to 3 do
  324.     begin
  325.       if (ii > 0) then
  326.         if Random(3) = 0 then
  327.           Break;
  328.  
  329.       GetBitmapCanvas(t).Brush.Color := Random(clWhite);
  330.       GetBitmapCanvas(t).Pen.Color := GetBitmapCanvas(t).Brush.Color;
  331.       GetBitmapCanvas(t).Rectangle(ii * 50, i * 50, ii * 50 + 50, i * 50 + 50);
  332.     end;
  333.   DisplayDebugImgWindow(200, 400);
  334.   SafeDrawBitmap(t, GetDebugCanvas, 0, 0);
  335.   SetupPieceset(pieceset, t, 4, 8);
  336.   SetupBoard(board, pieceset, 13, 33);
  337.   RandomBoard(board, pieceset);
  338.   i := LoadBitmap(ScriptPath + 'units.bmp');
  339.   SetupUnitTypes(unitTypes, pieceset, i, 6);
  340.   FreeBitmap(i);
  341.   RandomUnits(units, unitTypes, board, 10);
  342.   LaunchForm;
  343.   HideDebugImgWindow;
  344.   Write(['End']);
  345. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement