-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuai.pas
More file actions
156 lines (131 loc) · 4.29 KB
/
uai.pas
File metadata and controls
156 lines (131 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
(******************************************************************************)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* This file is part of FPC_Atomic *)
(* *)
(* See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(******************************************************************************)
Unit uai;
{$MODE ObjFPC}{$H+}
Interface
Uses
Classes, SysUtils
, uai_types
, ctypes
;
Type
(*
* Initialize the Lib with the Playercount (normally 10, only in Debuggmode different)
*)
TAiInit = Function(): cBool; cdecl;
(*
* Will be called on lib unloading
*)
TAiDeInit = Procedure(); cdecl;
(*
* Tells the Lib to start a new "round" = Reset all old values if needed
* Strenght in [0% .. 100%] -> 100% means "Best / Strongest"
*)
TAiNewRound = Procedure(Strength: cuint8); cdecl;
(*
* Will be Called every 10ms, this is the main routine where the Ai should be implemented
*)
TAiHandlePlayer = Function(PlayerIndex: cuint32; Var AiInfo: TAiInfo): TAiCommand; Cdecl;
(*
* Callback to get Interface version
*)
TAiInterfaceVersion = Function(): cuint32; Cdecl;
(*
* Callback for versionstring of ai lib
*)
TAiVersion = Function(): pchar; cdecl;
Var
AiInit: TAiInit = Nil;
AiDeInit: TAiDeInit = Nil;
AiNewRound: TAiNewRound = Nil;
AiHandlePlayer: TAiHandlePlayer = Nil;
AiInterfaceVersion: TAiInterfaceVersion = Nil;
AiVersion: TAiVersion = Nil;
Function LoadAiLib(): Boolean;
Procedure UnLoadAiLib;
Implementation
Uses dynlibs, uatomic_common;
Var
Lib: TLibHandle = 0;
Function LoadAiLib(): Boolean;
Var
Filename: String;
Begin
{$IFDEF Windows}
Filename := 'ai.dll';
{$ELSE}
Filename := 'libai.so';
{$ENDIF}
Filename := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + Filename;
result := false;
If lib <> 0 Then UnloadLibrary(lib);
If Not FileExists(Filename) Then exit;
lib := LoadLibrary(Filename);
If lib = 0 Then exit;
AiInterfaceVersion := TAiInterfaceVersion(GetProcAddress(lib, 'AiInterfaceVersion'));
If Not assigned(AiInterfaceVersion) Then Begin
UnLoadAiLib;
exit;
End;
If AiInterfaceVersion() <> AiLibInterfaceVersion Then Begin
logshow(format('Error, invalid ai interface version. Got %d need %d', [AiInterfaceVersion(), AiLibInterfaceVersion]), llError);
UnLoadAiLib;
exit;
End;
AiInit := TAiInit(GetProcAddress(lib, 'AiInit'));
If Not assigned(AiInit) Then Begin
UnLoadAiLib;
exit;
End;
AiNewRound := TAiNewRound(GetProcAddress(lib, 'AiNewRound'));
If Not assigned(AiNewRound) Then Begin
UnLoadAiLib;
exit;
End;
AiHandlePlayer := TAiHandlePlayer(GetProcAddress(lib, 'AiHandlePlayer'));
If Not assigned(AiHandlePlayer) Then Begin
UnLoadAiLib;
exit;
End;
AiDeInit := TAiDeInit(GetProcAddress(lib, 'AiDeInit'));
If Not assigned(AiDeInit) Then Begin
UnLoadAiLib;
exit;
End;
AiVersion := TAiVersion(GetProcAddress(lib, 'AiVersion'));
If Not assigned(AiVersion) Then Begin
UnLoadAiLib;
exit;
End;
result := true;
End;
Procedure UnLoadAiLib;
Begin
(*
* Beim Entladen vorher immer ein Deinit !
*)
If assigned(AiDeInit) Then Begin
AiDeInit();
End;
If lib <> 0 Then UnloadLibrary(Lib);
Lib := 0;
AiInit := Nil;
AiNewRound := Nil;
AiDeInit := Nil;
AiHandlePlayer := Nil;
AiInterfaceVersion := Nil;
AiVersion := Nil;
End;
End.