source: tags/1.3.2/Brain.pas

Last change on this file was 468, checked in by chronos, 6 months ago
  • Added: High DPI support integrated into trunk branch. It can be enabled by adding DPI define to compiler parameters for main project and packages.
File size: 5.1 KB
Line 
1unit Brain;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, Protocol, LazFileUtils,
7 dynlibs, Types,
8 {$IFDEF DPI}Dpi.Graphics{$ELSE}Graphics{$ENDIF};
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 LoadPicture;
38 procedure LoadFromFile(AIFileName: string);
39 constructor Create;
40 destructor Destroy; override;
41 end;
42
43 { TBrains }
44
45 TBrains = class(TObjectList<TBrain>)
46 function AddNew: TBrain;
47 function GetKindCount(Kind: TBrainType): Integer;
48 procedure GetByKind(Kind: TBrainType; Brains: TBrains);
49 function GetBeginner: TBrain;
50 procedure LoadPictures;
51 end;
52
53
54implementation
55
56uses
57 ScreenTools, Directories;
58
59{ TBrain }
60
61procedure TBrain.LoadPicture;
62var
63 TextSize: TSize;
64begin
65 if not LoadGraphicFile(Picture, GetAiDir + DirectorySeparator +
66 FileName + DirectorySeparator + FileName + '.png', [gfNoError]) then begin
67 with Picture.Canvas do begin
68 Brush.Color := $904830;
69 FillRect(Rect(0, 0, 64, 64));
70 Font.Assign(UniFont[ftTiny]);
71 Font.Style := [];
72 Font.Color := $5FDBFF;
73 TextSize := TextExtent(FileName);
74 TextOut(32 - TextSize.Width div 2, 32 - TextSize.Height div 2, FileName);
75 end;
76 end;
77end;
78
79procedure TBrain.LoadFromFile(AIFileName: string);
80var
81 T: Text;
82 Key: string;
83 Value: string;
84 S: string;
85 BasePath: string;
86 I: Integer;
87begin
88 BasePath := ExtractFileDir(AIFileName);
89 FileName := ExtractFileName(ExtractFileNameWithoutExt(ExtractFileNameWithoutExt(AIFileName)));
90 Name := FileName;
91 DLLName := BasePath + DirectorySeparator + Name + '.dll';
92 Credits := '';
93 Flags := fMultiple;
94 Client := nil;
95 Initialized := false;
96 ServerVersion := 0;
97 if not FileExists(AIFileName) then
98 raise Exception.Create(Format('AI specification file %s not found', [AIFileName]));
99 AssignFile(T, AIFileName);
100 Reset(T);
101 while not EOF(T) do
102 begin
103 ReadLn(T, s);
104 s := trim(s);
105 if Pos(' ', S) > 0 then begin
106 Key := Copy(S, 1, Pos(' ', S) - 1);
107 Value := Trim(Copy(S, Pos(' ', S) + 1, Length(S)));
108 end else begin
109 Key := S;
110 Value := '';
111 end;
112 if Key = '#NAME' then
113 Name := Value
114 else if Key = '#.NET' then
115 Flags := Flags or fDotNet
116 else if Key = '#BEGINNER' then
117 Beginner := True
118 else if Key = '#PATH' then
119 DLLName := BasePath + DirectorySeparator + Value
120 {$IFDEF WINDOWS}{$IFDEF CPU32}
121 else if Key = '#PATH_WIN32' then
122 DLLName := BasePath + DirectorySeparator + Value
123 {$ENDIF}{$ENDIF}
124 {$IFDEF WINDOWS}{$IFDEF CPU64}
125 else if Key = '#PATH_WIN64' then
126 DLLName := BasePath + DirectorySeparator + Value
127 {$ENDIF}{$ENDIF}
128 {$IFDEF UNIX}{$IFDEF CPUI386}
129 else if Key = '#PATH_LINUX_I386' then
130 DLLName := BasePath + DirectorySeparator + Value
131 {$ENDIF}{$ENDIF}
132 {$IFDEF UNIX}{$IFDEF CPUAMD64}
133 else if Key = '#PATH_LINUX_AMD64' then
134 DLLName := BasePath + DirectorySeparator + Value
135 {$ENDIF}{$ENDIF}
136 {$IFDEF UNIX}{$IFDEF CPUARM}
137 else if Key = '#PATH_LINUX_ARM32' then
138 DLLName := BasePath + DirectorySeparator + Value
139 {$ENDIF}{$ENDIF}
140 {$IFDEF UNIX}{$IFDEF CPUAARCH64}
141 else if Key = '#PATH_LINUX_ARM64' then
142 DLLName := BasePath + DirectorySeparator + Value
143 {$ENDIF}{$ENDIF}
144 else if Key = '#GAMEVERSION' then
145 for i := 1 to Length(Value) do
146 case Value[i] of
147 '0' .. '9':
148 ServerVersion := ServerVersion and $FFFF00 + ServerVersion and
149 $FF * 10 + ord(Value[i]) - 48;
150 '.':
151 ServerVersion := ServerVersion shl 8;
152 end
153 else if Key = '#CREDITS' then
154 Credits := Value;
155 end;
156 CloseFile(T);
157end;
158
159constructor TBrain.Create;
160begin
161 Picture := TBitmap.Create;
162 Picture.SetSize(64, 64);
163end;
164
165destructor TBrain.Destroy;
166begin
167 FreeAndNil(Picture);
168 inherited;
169end;
170
171{ TBrains }
172
173function TBrains.AddNew: TBrain;
174begin
175 Result := TBrain.Create;
176 Add(Result);
177end;
178
179function TBrains.GetKindCount(Kind: TBrainType): Integer;
180var
181 I: Integer;
182begin
183 Result := 0;
184 for I := 0 to Count - 1 do
185 if Items[I].Kind = Kind then Inc(Result);
186end;
187
188procedure TBrains.GetByKind(Kind: TBrainType; Brains: TBrains);
189var
190 I: Integer;
191begin
192 Brains.Clear;
193 for I := 0 to Count - 1 do
194 if Items[I].Kind = Kind then Brains.Add(Items[I]);
195end;
196
197function TBrains.GetBeginner: TBrain;
198var
199 I: Integer;
200begin
201 I := 0;
202 while (I < Count) and not Items[I].Beginner do Inc(I);
203 if I < Count then Result := Items[I]
204 else Result := nil;
205end;
206
207procedure TBrains.LoadPictures;
208var
209 I: Integer;
210begin
211 for I := 0 to Count - 1 do
212 with Items[I] do LoadPicture;
213end;
214
215end.
216
217
Note: See TracBrowser for help on using the repository browser.