source: tags/1.3.0/UBrain.pas

Last change on this file was 375, checked in by chronos, 3 years ago
File size: 4.0 KB
Line 
1unit UBrain;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, fgl, Graphics, Protocol, LazFileUtils, dynlibs;
9
10const
11 // module flags
12 fMultiple = $10000000;
13 fDotNet = $20000000;
14 fUsed = $40000000;
15
16type
17 TBrainType = (btNoTerm, btSuperVirtual, btTerm, btRandom, btAI, btNetworkServer,
18 btNetworkClient);
19
20 { TBrain }
21
22 TBrain = class
23 FileName: string;
24 DLLName: string;
25 Name: string;
26 Credits: string; { filename and full name }
27 hm: TLibHandle; { module handle }
28 Flags: Integer;
29 ServerVersion: Integer;
30 DataVersion: Integer;
31 DataSize: Integer;
32 Client: TClientCall; { client function address }
33 Initialized: Boolean;
34 Kind: TBrainType;
35 Picture: TBitmap;
36 Beginner: Boolean;
37 procedure LoadFromFile(AIFileName: string);
38 constructor Create;
39 destructor Destroy; override;
40 end;
41
42 { TBrains }
43
44 TBrains = class(TFPGObjectList<TBrain>)
45 function AddNew: TBrain;
46 function GetKindCount(Kind: TBrainType): Integer;
47 procedure GetByKind(Kind: TBrainType; Brains: TBrains);
48 function GetBeginner: TBrain;
49 end;
50
51
52implementation
53
54uses
55 ScreenTools;
56
57{ TBrain }
58
59procedure TBrain.LoadFromFile(AIFileName: string);
60var
61 T: Text;
62 Key: string;
63 Value: string;
64 S: string;
65 BasePath: string;
66 I: Integer;
67begin
68 BasePath := ExtractFileDir(AIFileName);
69 FileName := ExtractFileName(ExtractFileNameWithoutExt(ExtractFileNameWithoutExt(AIFileName)));
70 Name := FileName;
71 DLLName := BasePath + DirectorySeparator + Name + '.dll';
72 Credits := '';
73 Flags := fMultiple;
74 Client := nil;
75 Initialized := false;
76 ServerVersion := 0;
77 if not FileExists(AIFileName) then
78 raise Exception.Create(Format('AI specification file %s not found', [AIFileName]));
79 AssignFile(T, AIFileName);
80 Reset(T);
81 while not EOF(T) do
82 begin
83 ReadLn(T, s);
84 s := trim(s);
85 if Pos(' ', S) > 0 then begin
86 Key := Copy(S, 1, Pos(' ', S) - 1);
87 Value := Trim(Copy(S, Pos(' ', S) + 1, Length(S)));
88 end else begin
89 Key := S;
90 Value := '';
91 end;
92 if Key = '#NAME' then
93 Name := Value
94 else if Key = '#.NET' then
95 Flags := Flags or fDotNet
96 else if Key = '#BEGINNER' then
97 Beginner := True
98 else if Key = '#PATH' then
99 DLLName := BasePath + DirectorySeparator + Value
100 {$IFDEF WINDOWS}{$IFDEF CPU32}
101 else if Key = '#PATH_WIN32' then
102 DLLName := BasePath + DirectorySeparator + Value
103 {$ENDIF}{$ENDIF}
104 {$IFDEF WINDOWS}{$IFDEF CPU64}
105 else if Key = '#PATH_WIN64' then
106 DLLName := BasePath + DirectorySeparator + Value
107 {$ENDIF}{$ENDIF}
108 {$IFDEF LINUX}{$IFDEF CPU32}
109 else if Key = '#PATH_LINUX32' then
110 DLLName := BasePath + DirectorySeparator + Value
111 {$ENDIF}{$ENDIF}
112 {$IFDEF LINUX}{$IFDEF CPU64}
113 else if Key = '#PATH_LINUX64' then
114 DLLName := BasePath + DirectorySeparator + Value
115 {$ENDIF}{$ENDIF}
116 else if Key = '#GAMEVERSION' then
117 for i := 1 to Length(Value) do
118 case Value[i] of
119 '0' .. '9':
120 ServerVersion := ServerVersion and $FFFF00 + ServerVersion and
121 $FF * 10 + ord(Value[i]) - 48;
122 '.':
123 ServerVersion := ServerVersion shl 8;
124 end
125 else if Key = '#CREDITS' then
126 Credits := Value;
127 end;
128 CloseFile(T);
129end;
130
131constructor TBrain.Create;
132begin
133 Picture := TBitmap.Create;
134 Picture.SetSize(64, 64);
135end;
136
137destructor TBrain.Destroy;
138begin
139 FreeAndNil(Picture);
140 inherited;
141end;
142
143{ TBrains }
144
145function TBrains.AddNew: TBrain;
146begin
147 Result := TBrain.Create;
148 Add(Result);
149end;
150
151function TBrains.GetKindCount(Kind: TBrainType): Integer;
152var
153 I: Integer;
154begin
155 Result := 0;
156 for I := 0 to Count - 1 do
157 if Items[I].Kind = Kind then Inc(Result);
158end;
159
160procedure TBrains.GetByKind(Kind: TBrainType; Brains: TBrains);
161var
162 I: Integer;
163begin
164 Brains.Clear;
165 for I := 0 to Count - 1 do
166 if Items[I].Kind = Kind then Brains.Add(Items[I]);
167end;
168
169function TBrains.GetBeginner: TBrain;
170var
171 I: Integer;
172begin
173 I := 0;
174 while (I < Count) and not Items[I].Beginner do Inc(I);
175 if I < Count then Result := Items[I]
176 else Result := nil;
177end;
178
179end.
180
Note: See TracBrowser for help on using the repository browser.