Subversion Repositories spacemission

Rev

Rev 4 | Rev 14 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. unit LevSpeicherung;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ExtCtrls, Spin, ComCtrls{$IF CompilerVersion >= 23.0}, System.UITypes{$IFEND};
  8.  
  9. type
  10.   TSpeicherungForm = class(TForm)
  11.     Bevel1: TBevel;
  12.     LadenBtn: TButton;                      
  13.     LoeschenBtn: TButton;
  14.     AktualisierenBtn: TButton;
  15.     SpeichernBtn: TButton;
  16.     AbbrechenBtn: TButton;
  17.     LevelListBox: TListBox;
  18.     Label2: TLabel;
  19.     Label3: TLabel;
  20.     Label1: TLabel;
  21.     ElPanel1: TPanel;
  22.     li3a: TLabel;
  23.     li1b: TLabel;
  24.     li1a: TLabel;
  25.     li2b: TLabel;
  26.     li2a: TLabel;
  27.     li3b: TLabel;
  28.     liu: TLabel;
  29.     liw: TLabel;
  30.     SpinEditEdt: TEdit;
  31.     SpinEdit: TUpDown;
  32.     procedure LoeschenBtnClick(Sender: TObject);
  33.     procedure LadenBtnClick(Sender: TObject);
  34.     procedure SpeichernBtnClick(Sender: TObject);
  35.     procedure LevelListBoxClick(Sender: TObject);
  36.     procedure Button4Click(Sender: TObject);
  37.     procedure FormShow(Sender: TObject);
  38.     procedure DsFancyButton2Click(Sender: TObject);
  39.     procedure AbbrechenBtnClick(Sender: TObject);
  40.     procedure LevelNameChange(Sender: TObject);
  41.     procedure LevelListBoxDblClick(Sender: TObject);
  42.     procedure FormHide(Sender: TObject);
  43.     procedure SpinEditClick(Sender: TObject; Button: TUDBtnType);
  44.     procedure SpinEditEdtKeyPress(Sender: TObject; var Key: Char);
  45.     procedure SpinEditEdtChange(Sender: TObject);
  46.   public
  47.     procedure SearchLevels;
  48.     function RightStr(str: string; count: integer): string;
  49.     function Filter(n: integer; s: string): string;
  50.   end;
  51.  
  52. var
  53.   SpeicherungForm: TSpeicherungForm;
  54.  
  55. implementation
  56.  
  57. uses
  58.   Global, LevMain;
  59.  
  60. {$R *.DFM}
  61.  
  62. procedure TSpeicherungForm.SearchLevels;
  63. var
  64.   {sr: TSearchRec;
  65.   res: integer;}
  66.   i: integer;
  67. begin
  68.   //SpinEdit.Value := 1;
  69.   LevelListBox.items.clear;
  70.   li1a.visible := false;
  71.   li2a.visible := false;
  72.   li3a.visible := false;
  73.   li1b.visible := false;
  74.   li2b.visible := false;
  75.   li3b.visible := false;
  76.   liw.visible := true;
  77.   LadenBtn.enabled := false;
  78.   LoeschenBtn.enabled := false;
  79.   {res := FindFirst(mainform.fdirectory+'Levels\*.lev', 0, sr);
  80.   try
  81.     while (res = 0) do
  82.     begin
  83.       if (sr.name <> '.') and (sr.name <> '..') then
  84.         LevelListBox.items.Add(copy(sr.Name, 0, length(sr.name)-4));
  85.       res := FindNext(sr);
  86.     end;
  87.   finally
  88.     FindClose(sr);
  89.   end;}
  90.   for i := 1 to 9999 do
  91.   begin
  92.     if fileexists(fdirectory+'Levels\Level '+inttostr(i)+'.lev') then
  93.       LevelListBox.items.Add('Level ' + inttostr(i));
  94.   end;
  95. end;
  96.  
  97. procedure TSpeicherungForm.LoeschenBtnClick(Sender: TObject);
  98. var
  99.   Markiert: boolean;
  100.   i: integer;
  101. begin
  102.   Markiert := false;
  103.   for i := 0 to LevelListBox.items.Count-1 do
  104.   begin
  105.     if LevelListBox.Selected[i] then Markiert := true;
  106.   end;
  107.   if not Markiert then exit;
  108.   if MessageDlg('Dieses Level wirklich löschen?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  109.   begin
  110.     li1a.visible := false;
  111.     li2a.visible := false;
  112.     li3a.visible := false;
  113.     li1b.visible := false;
  114.     li2b.visible := false;
  115.     li3b.visible := false;
  116.     liu.visible := false;
  117.     liw.visible := true;
  118.     LadenBtn.enabled := false;
  119.     LoeschenBtn.enabled := false;
  120.     deletefile(FDirectory+'Levels\'+
  121.       LevelListBox.Items.strings[LevelListBox.itemindex]+'.lev');
  122.     SearchLevels;
  123.   end;
  124. end;
  125.  
  126. procedure TSpeicherungForm.LadenBtnClick(Sender: TObject);
  127. var
  128.   Markiert: boolean;
  129.   i, TempArtMain, TempLiveMain: integer;
  130.   SavGame: textfile;
  131.   Ergebnis: array[1..5] of string;
  132. begin
  133.   Markiert := false;
  134.   for i := 0 to LevelListBox.items.Count-1 do
  135.   begin
  136.     if LevelListBox.Selected[i] then Markiert := true;
  137.   end;
  138.   if not Markiert then exit;
  139.   if LevelListBox.items.count = 0 then
  140.   begin
  141.     li1a.visible := false;
  142.     li2a.visible := false;
  143.     li3a.visible := false;
  144.     li1b.visible := false;
  145.     li2b.visible := false;
  146.     li3b.visible := false;
  147.     liw.visible := true;
  148.     LadenBtn.enabled := false;
  149.     LoeschenBtn.enabled := false;
  150.   end;
  151.   // Da Button bei ungültigen Level deaktiviert wird, ist das nicht mehr nötig.
  152.   {if liu.visible or (LevelListBox.items.count=0) then
  153.     exit;}
  154.   // Vorbereiten
  155.   MainForm.DestroyLevel;
  156.   MainForm.LevChanged := false;
  157.   // Öffnen
  158.   AssignFile(SavGame, FDirectory+'Levels\'+
  159.     LevelListBox.Items.strings[LevelListBox.itemindex]+'.lev');
  160.   Reset(SavGame);
  161.   // Laden
  162.   ReadLN(SavGame); // --> Copyrightinfo
  163.   ReadLN(SavGame); // --> Copyrightinfo
  164.   ReadLN(SavGame, Ergebnis[5]); // --> Länge der Karte
  165.   MainForm.ScrollBar.Max := strtoint(Ergebnis[5]);
  166.   MainForm.Enemys.Clear;
  167.   TempArtMain := MainForm.ArtChecked;
  168.   TempLiveMain := MainForm.LiveEdit;
  169.   while not seekEoF(SavGame) do
  170.   begin
  171.     ReadLN(SavGame, Ergebnis[3]);
  172.     ReadLN(SavGame, Ergebnis[1]);
  173.     ReadLN(SavGame, Ergebnis[2]);
  174.     ReadLN(SavGame, Ergebnis[4]);
  175.     MainForm.EnemyAdd(strtoint(Ergebnis[1]), strtoint(Ergebnis[2]), strtoint(Ergebnis[3]), strtoint(Ergebnis[4]));
  176.     MainForm.ArtChecked := strtoint(Ergebnis[3]);
  177.     MainForm.LiveEdit := strtoint(Ergebnis[4]);
  178.     MainForm.EnemyCreate(strtoint(Ergebnis[1]), strtoint(Ergebnis[2]));
  179.     inc(MainForm.NumEnemys);
  180.     if Ergebnis[3] = '7' then MainForm.Boss := true;
  181.   end;
  182.   MainForm.LiveEdit := TempLiveMain;
  183.   MainForm.ArtChecked := TempArtMain;
  184.   // Schließen
  185.   CloseFile(SavGame);
  186.   // Nacharbeiten
  187.   MainForm.AnzeigeAct;
  188.   close;
  189. end;
  190.  
  191. function TSpeicherungForm.RightStr(str: string; count: integer): string;
  192. begin
  193.   result := copy(str, length(str)-(count-1), count);
  194. end;
  195.  
  196. function TSpeicherungForm.Filter(n: integer; s: string): string;
  197. var
  198.   i, last: integer;
  199.   start, start2: boolean;
  200.   temp: string;
  201. begin
  202.   last := 0;
  203.   start := false;
  204.   start2 := false;
  205.   temp := '';
  206.   if n = 1 then
  207.   begin
  208.     for i := 1 to length(s)+1 do
  209.     begin
  210.       if copy(s, i, 1) = '-' then
  211.       begin
  212.         last := i;
  213.         break;
  214.       end;
  215.     end;
  216.     temp := copy(s, 1, last-1);
  217.   end;
  218.   if n = 2 then
  219.   begin
  220.     for i := 1 to length(s)+1 do
  221.     begin
  222.       if start2 then start := true;
  223.       if copy(s, i, 1) = '-' then start2 := true;
  224.       if (copy(s, i, 1) = ':') and start then break;
  225.       if start then temp := temp + copy(s, i, 1)
  226.     end;
  227.   end;
  228.   if n = 3 then
  229.   begin
  230.     for i := 1 to length(s)+1 do
  231.     begin
  232.       if start2 then start := true;
  233.       if copy(s, i, 1) = ':' then start2 := true;
  234.       if (copy(s, i, 1) = '(') and start then break;
  235.       if start then temp := temp + copy(s, i, 1)
  236.     end;
  237.   end;
  238.   if n = 4 then
  239.   begin
  240.     for i := 1 to length(s)+1 do
  241.     begin
  242.       if start2 then start := true;
  243.       if copy(s, i, 1) = '(' then start2 := true;
  244.       if (copy(s, i, 1) = ')') and start then break;
  245.       if start then temp := temp + copy(s, i, 1)
  246.     end;
  247.   end;
  248.   result := temp;
  249. end;
  250.  
  251. procedure TSpeicherungForm.SpeichernBtnClick(Sender: TObject);
  252. var
  253.   SavGame: textfile;
  254.   i, j: integer;
  255.   puffer: string;
  256. begin
  257.   if mainform.Enemys.count = 0 then
  258.   begin
  259.     MessageDlg('Das Level ist leer!', mtError, [mbOK], 0);
  260.     SpinEdit.SetFocus;
  261.     exit;
  262.   end;
  263.   {for i := 0 to length(LevelName.text) do
  264.   begin
  265.     if (copy(LevelName.text, i, 1) = '\') or
  266.       (copy(LevelName.text, i, 1) = '/') or
  267.       (copy(LevelName.text, i, 1) = ':') or
  268.       (copy(LevelName.text, i, 1) = '*') or
  269.       (copy(LevelName.text, i, 1) = '?') or
  270.       (copy(LevelName.text, i, 1) = '"') or
  271.       (copy(LevelName.text, i, 1) = '<') or
  272.       (copy(LevelName.text, i, 1) = '>') or
  273.       (copy(LevelName.text, i, 1) = '|') then
  274.     begin
  275.       MessageDlg('Dies ist kein gültiger Levelname!', mtError, [mbOK], 0);
  276.       LevelName.SetFocus;
  277.       exit;
  278.     end;
  279.   end;}
  280.   if LevelListBox.items.IndexOf('Level ' + inttostr(SpinEdit.Position)) > -1 then
  281.   begin
  282.     if MessageDlg('Level ist bereits vorhanden. Ersetzen?', mtConfirmation, [mbYes, mbNo], 0) = mrNo then
  283.       exit;
  284.   end;
  285.   // Öffnen oder erstellen
  286.   AssignFile(SavGame, FDirectory+'Levels\Level '+inttostr(SpinEdit.Position)+'.lev');
  287.   Rewrite(SavGame);
  288.   // Sortierung
  289.   for j := 0 to mainform.enemys.Count - 2 do
  290.   begin
  291.     for i := 0 to mainform.enemys.Count - 2 do
  292.     begin
  293.       if strtoint(filter(1, mainform.enemys.Strings[i])) > strtoint(filter(1, mainform.enemys.Strings[i + 1])) then
  294.       begin
  295.         puffer := mainform.enemys.Strings[i];
  296.         mainform.enemys.Strings[i] := mainform.enemys.Strings[i + 1];
  297.         mainform.enemys.Strings[i + 1] := puffer;
  298.       end;
  299.     end;
  300.   end;
  301.   // Speichern
  302.   WriteLN(SavGame, '; SpaceMission ' + FCompVersion);
  303.   WriteLN(SavGame, '; LEV-File');
  304.   WriteLN(SavGame, inttostr(MainForm.ScrollBar.Max)); // --> Länge der Karte
  305.   for i := 0 to mainform.enemys.count-1 do
  306.   begin
  307.     WriteLN(SavGame, filter(3, mainform.enemys.Strings[i]));
  308.     WriteLN(SavGame, filter(1, mainform.enemys.Strings[i]));
  309.     WriteLN(SavGame, filter(2, mainform.enemys.Strings[i]));
  310.     WriteLN(SavGame, filter(4, mainform.enemys.Strings[i]));
  311.   end;
  312.   // Schließen
  313.   CloseFile(SavGame);
  314.   // Nacharbeiten
  315.   MainForm.LevChanged := false;
  316.   MainForm.AnzeigeAct;
  317.   SearchLevels;
  318. end;
  319.  
  320. procedure TSpeicherungForm.LevelListBoxClick(Sender: TObject);
  321. var
  322.   Ergebnis, boss, l, temp: string;
  323.   SavGame: textfile;
  324.   x, a: integer;
  325. begin
  326.   li1a.visible := false;
  327.   li2a.visible := false;
  328.   li3a.visible := false;
  329.   li1b.visible := false;
  330.   li2b.visible := false;
  331.   li3b.visible := false;
  332.   liu.visible := false;
  333.   liw.visible := false;
  334.   if (LevelListBox.items.count=0) or (LevelListBox.itemindex = -1) then
  335.   begin
  336.     ladenbtn.enabled := false;
  337.     loeschenbtn.enabled := false;
  338.     liw.visible := true;
  339.     exit;
  340.   end;
  341.   temp := LevelListBox.Items.strings[LevelListBox.itemindex];
  342.   SpinEdit.Position := strtoint(RightStr(temp, length(temp)-6));
  343.   AssignFile(SavGame, FDirectory+'Levels\'+
  344.     LevelListBox.Items.strings[LevelListBox.itemindex]+'.lev');
  345.   Reset(SavGame);
  346.   ReadLN(SavGame, Ergebnis);
  347.   if Ergebnis <> '; SpaceMission '+FCompVersion then
  348.   begin
  349.     liu.visible := true;
  350.     LadenBtn.enabled := false;
  351.     CloseFile(SavGame);
  352.     exit;
  353.   end;
  354.   ReadLN(SavGame, Ergebnis);
  355.   if Ergebnis <> '; LEV-File' then
  356.   begin
  357.     liu.visible := true;
  358.     LadenBtn.enabled := false;
  359.     CloseFile(SavGame);
  360.     exit;
  361.   end;
  362.   ReadLN(SavGame, l);
  363.   x := 0;
  364.   a := 0;
  365.   boss := 'Nein';
  366.   while not SeekEOF(SavGame) do
  367.   begin
  368.     ReadLN(SavGame, Ergebnis);
  369.     inc(a);
  370.     if a = 5 then a := 1;
  371.     if (a = 1) and (Ergebnis = '7') then boss := 'Ja';
  372.     inc(x);
  373.   end;
  374.   CloseFile(SavGame);
  375.   if a <> 4 then
  376.   begin
  377.     liu.visible := true;
  378.     LadenBtn.enabled := false;
  379.   end
  380.   else
  381.   begin
  382.     li1a.visible := true;
  383.     li2a.visible := true;
  384.     li3a.visible := true;
  385.     li1b.visible := true;
  386.     li2b.visible := true;
  387.     li3b.visible := true;
  388.     LadenBtn.enabled := true;
  389.     LoeschenBtn.enabled := true;
  390.     li1b.caption := inttostr(trunc(x / 4));
  391.     li2b.caption := boss;
  392.     li3b.caption := l + ' Felder';
  393.   end;
  394. end;
  395.  
  396. procedure TSpeicherungForm.Button4Click(Sender: TObject);
  397. begin
  398.   close;
  399. end;
  400.  
  401. procedure TSpeicherungForm.FormShow(Sender: TObject);
  402. begin
  403.   mainform.dxtimer.enabled := false;
  404.   SearchLevels;
  405. end;
  406.  
  407. procedure TSpeicherungForm.DsFancyButton2Click(Sender: TObject);
  408. begin
  409.   SearchLevels;
  410. end;
  411.  
  412. procedure TSpeicherungForm.AbbrechenBtnClick(Sender: TObject);
  413. begin
  414.   close;
  415. end;
  416.  
  417. procedure TSpeicherungForm.LevelNameChange(Sender: TObject);
  418. begin
  419.   {...}
  420. end;
  421.  
  422. procedure TSpeicherungForm.LevelListBoxDblClick(Sender: TObject);
  423. begin
  424.   LadenBtn.click;
  425. end;
  426.  
  427. procedure TSpeicherungForm.FormHide(Sender: TObject);
  428. begin
  429.   mainform.dxtimer.enabled := true;
  430. end;
  431.  
  432. procedure TSpeicherungForm.SpinEditClick(Sender: TObject;
  433.   Button: TUDBtnType);
  434. begin
  435.   SpinEditEdt.Text := inttostr(SpinEdit.position);
  436. end;
  437.  
  438. procedure TSpeicherungForm.SpinEditEdtKeyPress(Sender: TObject;
  439.   var Key: Char);
  440. begin
  441.   if not (Key in [#13, #08, '0'..'9']) then
  442.     Key := #0;
  443. end;
  444.  
  445. procedure TSpeicherungForm.SpinEditEdtChange(Sender: TObject);
  446. begin
  447.   SpinEdit.Position := strtoint(SpinEditEdt.text);
  448. end;
  449.  
  450. end.
  451.  
  452.