Advertisement
mixster

mixster

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