Advertisement
mixster

mixster

Jan 5th, 2009
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 13.75 KB | None | 0 0
  1. program Master;
  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.   mnuMain: TMainMenu;
  43.   popNewUnit, popSetFact: TPopupMenu;
  44.   mnuFactOpts, mnuUnits, mnuFactions: array of TMenuItem;
  45.   board: TBoard;
  46.   pieceset: TPieceset;
  47.   unitTypes: TUnitSkel;
  48.   units: TUnits;
  49.   rp: TPoint;
  50.  
  51. procedure Write(v: TVariantArray);
  52. var
  53.   i: Integer;
  54.   s: string;
  55. begin
  56.   if False then
  57.     exit;
  58.   if High(v) < 0 then
  59.     exit;
  60.   for i := 0 to High(v) do
  61.     s := s + v[i];
  62.   Writeln(s);
  63. end;
  64.  
  65. procedure SetupBoard(var brd: TBoard; pce: TPieceSet; w, h: Integer);
  66. var
  67.   x, y: Integer;
  68. begin
  69.   brd.w := w - 1;
  70.   brd.h := h - 1;
  71.   SetLength(brd.t, h);
  72.   for y := 0 to brd.h do
  73.   begin
  74.     SetLength(brd.t[y], w);
  75.     for x := 0 to brd.w do
  76.     begin
  77.       brd.t[y][x].t.x := 0;
  78.       brd.t[y][x].t.y := 0;
  79.       brd.t[y][x].o := False;
  80.     end;
  81.   end;
  82.   x := (w * ((pce.s.x + pce.sL) * 2)) - pce.sL;
  83.   y := h * (pce.s.y * 2);
  84.   brd.ground := BitmapFromString(x, y, '');
  85.   FastDrawClear(brd.ground, clBlack);
  86.   brd.sprite := BitmapFromString(x, y, '');
  87.   FastDrawClear(brd.sprite, clWhite);
  88. end;
  89.  
  90. procedure RandomBoard(var brd: TBoard; pce: TPieceset);
  91. var
  92.   i, ii: Integer;
  93. begin
  94.   for i := 0 to brd.h do
  95.     for ii := 0 to brd.w do
  96.       with brd.t[i][ii].t do
  97.       begin
  98.         y := Random(pce.h + 1);
  99.         x := Random(pce.p[y].h + 1);
  100.       end;
  101. end;
  102.  
  103. procedure SetupPieceset(var pce: TPieceSet; bmp, tw, th: Integer);
  104. var
  105.   l, wl, hl, m, x, y, tb, ii, i: Integer;
  106.   c, ts: TCanvas;
  107.   p: TPointArray;
  108. begin
  109.   l := 20;
  110.   pce.sL := l;
  111.   pce.s.x := Round(Sin(Radians(30)) * l);
  112.   pce.s.y := Round(Cos(Radians(30)) * l);
  113.   wl := (2 * pce.s.x) + l;
  114.   hl := 2 * pce.s.y;
  115.   m := BitmapFromString(wl, hl, '');
  116.   FastDrawClear(m, clBlack);
  117.   c := GetBitmapCanvas(m);
  118.   c.Pen.Color := RGBtoColor(1, 1, 1);
  119.   c.Brush.Color := clWhite;
  120.   with pce.s do
  121.     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)]);
  122.   SetTransparentColor(m, clWhite);
  123.   GetBitmapSize(bmp, x, y);
  124.   x := x / tw;
  125.   y := y / th;
  126.   tb := BitmapFromString(x, y, '');
  127.   FastDrawClear(tb, 0);
  128.   SetTargetBitmap(bmp);
  129.   ts := GetBitmapCanvas(bmp);
  130.   pce.h := th - 1;
  131.   SetLength(pce.p, th);
  132.   for ii := 0 to pce.h do
  133.     with pce.p[ii] do
  134.     begin
  135.       h := -1;
  136.       for i := 0 to tw - 1 do
  137.       begin
  138.         SetLength(p, 0);
  139.         FindColorsTolerance(p, clBlack, x * i, y * ii, (x * i) + wl, (y * ii) + hl, 0);
  140.         if Length(p) >= ((wl + 1) * (hl + 1)) then
  141.           Break;
  142.         Inc(h);
  143.         SetLength(t, h + 1);
  144.         t[h] := BitmapFromString(wl, hl, '');
  145.         SafeCopyCanvas(ts, GetBitmapCanvas(t[h]), x * i, y * ii, x * i + wl, y * ii + hl, 0, 0, wl, hl);
  146.         FastDrawTransparent(0, 0, m, t[h]);
  147.         SetTransparentColor(t[h], clBlack);
  148.       end;
  149.     end;
  150.   FreeBitmap(m);
  151.   ResetDc;
  152. end;
  153.  
  154. procedure SetupUnitTypes(var unTy: TUnitSkel; pce: TPieceSet; bmp, l: Integer);
  155. var
  156.   i, w: Integer;
  157.   c, t: TCanvas;
  158. begin
  159.   w := pce.s.y * 2;
  160.   unTy.h := l - 1;
  161.   GetBitmapSize(bmp, unTy.s.x, unTy.s.y);
  162.   unTy.s.x := unTy.s.x / l;
  163.   SetLength(unTy.t, l);
  164.   c := GetBitmapCanvas(bmp);
  165.   for i := 0 to unTy.h do
  166.   begin
  167.     unTy.t[i] := BitmapFromString(w, w, '');
  168.     t := GetBitmapCanvas(unTy.t[i]);
  169.     SafeCopyCanvas(c, t, i * unTy.s.x, 0, i * unTy.s.x + unTy.s.x, unTy.s.y, 0, 0, w, w);
  170.     SetTransparentColor(unTy.t[i], clWhite);
  171.   end;
  172. end;
  173.  
  174. procedure RandomUnits(var uns: TUnits; var brd: TBoard; unTy: TUnitSkel; num: Integer);
  175. var
  176.   i: Integer;
  177. begin
  178.   uns.h := 0;
  179.   SetLength(uns.t, 1);
  180.   uns.t[0].h := num - 1;
  181.   SetLength(uns.t[0].u, num);
  182.   uns.t[0].c := clRed;
  183.   for i := 0 to num - 1 do
  184.     with uns.t[0].u[i] do
  185.     begin
  186.       t := Random(unTy.h + 1);
  187.       p.x := Random(brd.w + 1);
  188.       p.y := Random(brd.h + 1);
  189.       brd.t[p.y][p.x].o := True;
  190.     end;
  191. end;
  192.  
  193. procedure DrawBoard(var img: TImage; brd: TBoard; pce: TPieceSet);
  194. var
  195.   t, i, ii, w, h, l, o: Integer;
  196. begin
  197.   FastDrawClear(t, clBlack);
  198.   l := pce.sL;
  199.   w := (pce.s.x + l) * 2;
  200.   h := pce.s.y;
  201.   img.Width := w * brd.w + w + 1 - l;
  202.   img.Height := h * brd.h + h + h + 1;
  203.   t := BitmapFromString(img.Width, img.Height, '');
  204.  
  205.   for ii := 0 to brd.h do
  206.   begin
  207.     if ii mod 2 = 0 then
  208.       o := 0
  209.     else
  210.       o := pce.s.x + l;
  211.     for i := 0 to brd.w do
  212.     begin
  213.       if o > 0 then
  214.         if i = brd.w then
  215.           Break;
  216.       with brd.t[ii][i].t do
  217.       begin
  218.         if (y > pce.h) then
  219.           Continue;
  220.         if (x > pce.p[y].h) then
  221.           Continue;
  222.         FastDrawTransparent((i * w) + o, (ii * h), pce.p[y].t[x], t);
  223.       end;
  224.     end;
  225.   end;
  226.  
  227.   SafeDrawBitmap(t, img.Canvas, 0, 0);
  228.   FastDrawTransparent(0, 0, t, brd.ground);
  229.   FreeBitmap(t);
  230. end;
  231.  
  232. procedure DrawUnits(var img: TImage; uni: TUnits; unTy: TUnitSkel; brd: TBoard; pce: TPieceSet);
  233. var
  234.   i, ii, w, h, o, l, b: Integer;
  235. begin
  236.   FastDrawClear(brd.sprite, clWhite);
  237.   w := (pce.s.x + pce.sL) * 2;
  238.   h := pce.s.y;
  239.   b := BitmapFromString(w - pce.sL, h * 2, '');
  240.   l := ((w - (2 * h)) - pce.sL) / 2;
  241.   FastDrawClear(b, clWhite);
  242.   for i := 0 to uni.h do
  243.   begin
  244.     if uni.h < 0 then
  245.       Break;
  246.     for ii := 0 to uni.t[i].h do
  247.     begin
  248.       if uni.t[i].h < 0 then
  249.         Break;
  250.       with uni.t[i].u[ii] do
  251.       begin
  252.         if p.y > brd.h then
  253.           Continue;
  254.         if p.x > brd.w then
  255.           Continue;
  256.         if p.y mod 2 = 0 then
  257.           o := 0
  258.         else
  259.           o := pce.s.x + pce.sL;
  260.  
  261.         FastDrawTransparent((p.x * w) + o + l, p.y * h, unTy.t[t], brd.sprite);
  262.       end;
  263.       FastReplaceColor(brd.sprite, clBlack, uni.t[i].c);
  264.     end;
  265.   end;
  266.   GetBitmapSize(brd.ground, w, h);
  267.   i := BitmapFromString(w, h, '');
  268.   FastDrawTransparent(0, 0, brd.ground, i);
  269.   SetTransparentColor(brd.sprite, clWhite);
  270.   FastDrawTransparent(0, 0, brd.sprite, i);
  271.   SafeDrawBitmap(i, img.Canvas, 0, 0);
  272.   FreeBitmap(i);
  273. end;
  274.  
  275.  
  276. procedure popupMenuClick(Sender: TObject); forward;
  277. procedure mnuMainClick(Sender: TObject);
  278. var
  279.   i, c, f: Integer;
  280. begin
  281.   for i := 0 to High(mnuFactOpts) do
  282.     if Sender = mnuFactOpts[i] then
  283.       Break;
  284.   with units do
  285.   begin
  286.     case i of
  287.       1: begin
  288.         h := h + 1;
  289.         SetLength(t, h + 1);
  290.         t[h].h := -1;
  291.         SetLength(mnuFactions, h + 2);
  292.         t[h].c := Random(clWhite) + 1;
  293.         mnuFactions[h + 1] := TMenuItem.Create(frmMain);
  294.         mnuFactions[h + 1].Caption := 'Faction ' + IntToStr(h + 1);
  295.         mnuFactions[h + 1].OnClick := @popupMenuClick;
  296.         popSetFact.Items.Add(mnuFactions[h + 1]);
  297.       end;
  298.       2: begin
  299.         c := -1;
  300.         case StrToIntDef(Readln('New colour (1-8)'), 1) of
  301.           2: c := clGreen;
  302.           3: c := clBlue;
  303.           4: c := clYellow;
  304.           5: c := clBronze;
  305.           6: c := clWhite;
  306.           7: c := clGray;
  307.           8: c := clBlack;
  308.         end;
  309.         if c < 0 then
  310.           c := clRed;
  311.          
  312.         f := StrToIntDef(Readln('Which faction (number)'), 1) - 1;
  313.         if f > h then
  314.           f := h;
  315.         t[f].c := c;
  316.       end;
  317.       3: begin
  318.         c := StrToIntDef(Readln('Which faction (number)'), h);
  319.         if c > h then
  320.           c := h;
  321.         Swap(t[h], t[c]);
  322.         SetLength(t, h);
  323.         h := h - 1;
  324.       end;
  325.     end;
  326.   end;
  327.  
  328.   DrawUnits(imgBoard, units, unitTypes, board, pieceset);
  329. end;
  330.  
  331. procedure imgBoardMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; x, y: Integer);
  332. var
  333.   px, py: Integer;
  334. begin
  335.   if not (Button = mbRight) then
  336.     Exit;
  337.  
  338.   rp.x := Trunc(Abs(x - pieceset.s.x / 2)) div (pieceset.s.x + pieceset.sL);
  339.   if rp.x mod 2 = 0 then
  340.     rp.y := ((y div (pieceset.s.y * 2)) * 2)
  341.   else
  342.     rp.y := (((y - pieceset.s.y) div (pieceset.s.y * 2)) * 2) + 1;
  343.    
  344.   if rp.y mod 2 = 0 then
  345.     rp.x := x div ((pieceset.s.x + pieceset.sL) * 2)
  346.   else
  347.     rp.x := (x - (pieceset.s.x + pieceset.sL)) div ((pieceset.s.x + pieceset.sL) * 2);
  348.    
  349.  
  350.   GetMousePos(px, py);
  351.  
  352.   if not board.t[rp.y][rp.x].o then
  353.     popNewUnit.Popup(px, py)
  354.   else
  355.     popSetFact.Popup(px, py);
  356.  
  357. end;
  358.  
  359. procedure popupMenuClick(Sender: TObject);
  360. var
  361.   i, a, b, h, hh: Integer;
  362.   nu: Boolean;
  363.   up: TPoint;
  364. begin
  365.   nu := False;
  366.   for i := 0 to High(mnuUnits) do
  367.     if Sender = mnuUnits[i] then
  368.     begin
  369.       nu := True;
  370.       Break;
  371.     end;
  372.   if not nu then
  373.     for i := 0 to High(mnuFactions) do
  374.       if Sender = mnuFactions[i] then
  375.         Break;
  376.  
  377.   if (not nu) then
  378.   begin
  379.     for a := 0 to units.h do
  380.       for b := 0 to units.t[a].h do
  381.         if (units.t[a].u[b].p.x = rp.x) and (units.t[a].u[b].p.y = rp.y) then
  382.         begin
  383.           up := Point(b, a);
  384.           break;
  385.         end;
  386.     if (up.y = i - 1) then
  387.       exit;
  388.     h := High(units.t[up.y].u);
  389.     Swap(units.t[up.y].u[up.x], units.t[up.y].u[h]);
  390.    
  391.     if (i > 0) then
  392.     begin
  393.       hh := units.t[i - 1].h;
  394.       units.t[i - 1].h := hh + 1;
  395.       SetLength(units.t[i - 1].u, hh + 2);
  396.       Swap(units.t[i - 1].u[hh + 1], units.t[up.y].u[h]);
  397.     end;
  398.    
  399.     SetLength(units.t[up.y].u, h);
  400.     units.t[up.y].h := h - 1;
  401.   end
  402.   else
  403.   begin
  404.     with units.t[0] do
  405.     begin
  406.       h := h + 1;
  407.       SetLength(u, h + 1);
  408.       u[h].p := rp;
  409.       u[h].t := i;
  410.       board.t[rp.y][rp.x].o := True;
  411.     end;
  412.   end;
  413.  
  414.   DrawUnits(imgBoard, units, unitTypes, board, pieceset);
  415. end;
  416.  
  417. procedure SetupForm;
  418. var
  419.   i, h: Integer;
  420. begin
  421.   frmMain := CreateForm;
  422.   with frmMain do
  423.   begin
  424.     ClientWidth := 600;
  425.     ClientHeight := 500;
  426.     Position:= poScreenCenter;
  427.     Caption := 'Strategy Game by mixster';
  428.   end;
  429.  
  430.   sbxBoard := TScrollBox.Create(frmMain);
  431.   with sbxBoard do
  432.   begin
  433.     Parent := frmMain;
  434.     Align := alClient;
  435.   end;
  436.  
  437.   imgBoard := TImage.Create(frmMain);
  438.   with imgBoard do
  439.   begin
  440.     Parent := sbxBoard;
  441.     Width := 800;
  442.     Height := 600
  443.     Left := 0;
  444.     Top := 0;
  445.     DrawBoard(imgBoard, board, pieceset);
  446.     DrawUnits(imgBoard, units, unitTypes, board, pieceset);
  447.     OnMouseUp := @imgBoardMouseUp;
  448.   end;
  449.  
  450.   mnuMain := TMainMenu.Create(frmMain);
  451.   SetLength(mnuFactOpts, 4);
  452.   for i := 0 to High(mnuFactOpts) do
  453.   begin
  454.     mnuFactOpts[i] := TMenuItem.Create(frmMain);
  455.     case i of
  456.       0: mnuFactOpts[i].Caption := 'Factions';
  457.       1: mnuFactOpts[i].Caption := 'New';
  458.       2: mnuFactOpts[i].Caption := 'Set color';
  459.       3: mnuFactOpts[i].Caption := 'Delete';
  460.     end;
  461.     if i > 0 then
  462.     begin
  463.       mnuFactOpts[i].OnClick := @mnuMainClick;
  464.       mnuMain.Items.Items[0].Add(mnuFactOpts[i]);
  465.     end
  466.     else
  467.       mnuMain.Items.Add(mnuFactOpts[0]);
  468.   end;
  469.  
  470.   popNewUnit := TPopupMenu.Create(frmMain);
  471.   SetLength(mnuUnits, unitTypes.h + 1);
  472.  
  473.   popSetFact := TPopupMenu.Create(frmMain);
  474.   SetLength(mnuFactions, units.h + 2);
  475.  
  476.   mnuFactions[0] := TMenuItem.Create(frmMain);
  477.   mnuFactions[0].Caption := 'Delete unit';
  478.   mnuFactions[0].OnClick := @popupMenuClick;
  479.   popSetFact.Items.Add(mnuFactions[0]);
  480.  
  481.   for i := 0 to unitTypes.h do
  482.   begin
  483.     mnuUnits[i] := TMenuItem.Create(frmMain);
  484.     mnuUnits[i].Caption := 'Unit ' + IntToStr(i + 1);
  485.     mnuUnits[i].OnClick := @popupMenuClick;
  486.     popNewUnit.Items.Add(mnuUnits[i]);
  487.     if i > units.h then
  488.       continue;
  489.     mnuFactions[i + 1] := TMenuItem.Create(frmMain);
  490.     mnuFactions[i + 1].Caption := 'Faction ' + IntToStr(i + 1);
  491.     mnuFactions[i + 1].OnClick := @popupMenuClick;
  492.     popSetFact.Items.Add(mnuFactions[i + 1])
  493.   end;
  494.  
  495.   for i := unitTypes.h to units.h do
  496.   begin
  497.     if i > units.h then
  498.       break;
  499.     mnuFactions[i + 1] := TMenuItem.Create(frmMain);
  500.     mnuFactions[i + 1].Caption := 'Faction ' + IntToStr(i + 1);
  501.     mnuFactions[i + 1].OnClick := @popupMenuClick;
  502.     popSetFact.Items.Add(mnuFactions[i + 1])
  503.   end;
  504.  
  505.   frmMain.ShowModal;
  506. end;
  507.  
  508. procedure LaunchForm;
  509. var
  510.   v: TVariantArray;
  511. begin
  512.   ThreadSafeCall('SetupForm', v);
  513. end;
  514.  
  515. procedure ScriptTerminate;
  516. var
  517.   i: Integer;
  518. begin
  519.   i := 0;
  520.   try
  521.     repeat
  522.       FreeBitmap(i);
  523.       Inc(i);
  524.     until false;
  525.   except
  526.   end;
  527.   try
  528.     FreeForm(frmMain);
  529.   except
  530.   end;
  531.   Write(['Freed: ', i, ' bitmap(s) and the form']);
  532. end;
  533.  
  534. var
  535.   t, i, ii: Integer;
  536. begin
  537.   Write(['Begin']);
  538.   DisplayDebugImgWindow(200, 400);
  539.   t := BitmapFromString(200, 400, '');
  540.  
  541.   for i := 0 to 7 do
  542.     for ii := 0 to 3 do
  543.     begin
  544.       if (ii > 0) then
  545.         if Random(3) = 0 then
  546.           Break;
  547.  
  548.       GetBitmapCanvas(t).Brush.Color := Random(clWhite) + 1;
  549.       GetBitmapCanvas(t).Pen.Color := GetBitmapCanvas(t).Brush.Color;
  550.       GetBitmapCanvas(t).Rectangle(ii * 50, i * 50, ii * 50 + 50, i * 50 + 50);
  551.     end;
  552.   try
  553.     SafeDrawBitmap(t, GetDebugCanvas, 0, 0);
  554.   except
  555.     Write(['Debug canvas failed again']);
  556.   end;
  557.   SetupPieceset(pieceset, t, 4, 8);
  558.   FreeBitmap(t);
  559.   SetupBoard(board, pieceset, 13, 33);
  560.   RandomBoard(board, pieceset);
  561.   i := LoadBitmap(ScriptPath + 'units.bmp');
  562.   SetupUnitTypes(unitTypes, pieceset, i, 6);
  563.   FreeBitmap(i);
  564.   RandomUnits(units, board, unitTypes, 10);
  565.   LaunchForm;
  566.   HideDebugImgWindow;
  567.   Write(['End']);
  568.   TerminateScript;
  569. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement