Subversion Repositories spacemission

Rev

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