author | unc0rr |
Sat, 01 Nov 2014 22:51:07 +0300 | |
branch | qmlfrontend |
changeset 10450 | bf9e30b4ef9b |
parent 10448 | 4cb727e029fa |
child 10452 | 03519fd9f98d |
permissions | -rw-r--r-- |
10406 | 1 |
unit uFLGameConfig; |
2 |
interface |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
3 |
uses uFLTypes; |
10406 | 4 |
|
10430 | 5 |
procedure resetGameConfig; cdecl; |
6 |
procedure runQuickGame; cdecl; |
|
10448 | 7 |
procedure runLocalGame; cdecl; |
10430 | 8 |
procedure getPreview; cdecl; |
10406 | 9 |
|
10430 | 10 |
procedure registerGUIMessagesCallback(p: pointer; f: TGUICallback); cdecl; |
11 |
||
12 |
procedure setSeed(seed: PChar); cdecl; |
|
13 |
function getSeed: PChar; cdecl; |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
14 |
|
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
15 |
procedure tryAddTeam(teamName: PChar); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
16 |
procedure tryRemoveTeam(teamName: PChar); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
17 |
|
10406 | 18 |
implementation |
10448 | 19 |
uses uFLIPC, hwengine, uFLUtils, uFLTeams, uFLData; |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
20 |
|
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
21 |
var guiCallbackPointer: pointer; |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
22 |
guiCallbackFunction: TGUICallback; |
10406 | 23 |
|
10426 | 24 |
const |
25 |
MAXCONFIGS = 5; |
|
26 |
MAXARGS = 32; |
|
27 |
||
28 |
type |
|
29 |
TGameConfig = record |
|
30 |
seed: shortstring; |
|
31 |
theme: shortstring; |
|
32 |
script: shortstring; |
|
10432 | 33 |
mapgen: Longint; |
10426 | 34 |
gameType: TGameType; |
35 |
teams: array[0..7] of TTeam; |
|
36 |
arguments: array[0..Pred(MAXARGS)] of shortstring; |
|
37 |
argv: array[0..Pred(MAXARGS)] of PChar; |
|
38 |
argumentsNumber: Longword; |
|
39 |
end; |
|
40 |
PGameConfig = ^TGameConfig; |
|
41 |
||
10432 | 42 |
var |
43 |
currentConfig: TGameConfig; |
|
44 |
||
10430 | 45 |
|
10432 | 46 |
procedure sendConfig(config: PGameConfig); |
47 |
var i: Longword; |
|
48 |
begin |
|
49 |
with config^ do |
|
10430 | 50 |
begin |
10432 | 51 |
case gameType of |
52 |
gtPreview: begin |
|
53 |
ipcToEngine('eseed ' + seed); |
|
54 |
ipcToEngine('e$mapgen ' + intToStr(mapgen)); |
|
55 |
end; |
|
56 |
gtLocal: begin |
|
57 |
ipcToEngine('eseed ' + seed); |
|
58 |
ipcToEngine('e$mapgen ' + intToStr(mapgen)); |
|
59 |
i:= 0; |
|
60 |
while (i < 8) and (teams[i].hogsNumber > 0) do |
|
61 |
begin |
|
62 |
ipcToEngine('eammloadt 93919294221991210322351110012000000002111001010111110001'); |
|
63 |
ipcToEngine('eammprob 04050405416006555465544647765766666661555101011154111111'); |
|
64 |
ipcToEngine('eammdelay 00000000000002055000000400070040000000002200000006000200'); |
|
65 |
ipcToEngine('eammreinf 13111103121111111231141111111111111112111111011111111111'); |
|
66 |
ipcToEngine('eammstore'); |
|
67 |
sendTeamConfig(teams[i]); |
|
68 |
inc(i) |
|
69 |
end; |
|
70 |
end; |
|
71 |
end; |
|
72 |
||
73 |
ipcToEngine('!'); |
|
74 |
end; |
|
75 |
end; |
|
10426 | 76 |
|
77 |
procedure queueExecution; |
|
78 |
var pConfig: PGameConfig; |
|
79 |
i: Longword; |
|
80 |
begin |
|
81 |
new(pConfig); |
|
82 |
pConfig^:= currentConfig; |
|
83 |
||
84 |
with pConfig^ do |
|
85 |
for i:= 0 to Pred(MAXARGS) do |
|
86 |
begin |
|
87 |
if arguments[i][0] = #255 then |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
88 |
arguments[i][255]:= #0 |
10426 | 89 |
else |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
90 |
arguments[i][byte(arguments[i][0]) + 1]:= #0; |
10426 | 91 |
argv[i]:= @arguments[i][1] |
92 |
end; |
|
93 |
||
94 |
RunEngine(pConfig^.argumentsNumber, @pConfig^.argv); |
|
10432 | 95 |
|
96 |
sendConfig(pConfig) |
|
10426 | 97 |
end; |
98 |
||
99 |
procedure resetGameConfig; cdecl; |
|
10450 | 100 |
var i: Longword; |
10406 | 101 |
begin |
10450 | 102 |
with currentConfig do |
103 |
begin |
|
104 |
for i:= 0 to 7 do |
|
105 |
teams[i].hogsNumber:= 0 |
|
106 |
end |
|
10406 | 107 |
end; |
108 |
||
10430 | 109 |
procedure setSeed(seed: PChar); cdecl; |
110 |
begin |
|
111 |
currentConfig.seed:= seed |
|
112 |
end; |
|
113 |
||
10448 | 114 |
|
10430 | 115 |
function getSeed: PChar; cdecl; |
116 |
begin |
|
117 |
getSeed:= str2PChar(currentConfig.seed) |
|
118 |
end; |
|
119 |
||
10450 | 120 |
function getUnusedColor: Longword; |
10448 | 121 |
var i, c: Longword; |
122 |
fColorMatched: boolean; |
|
123 |
begin |
|
124 |
c:= 0; |
|
125 |
i:= 0; |
|
126 |
repeat |
|
127 |
repeat |
|
10450 | 128 |
fColorMatched:= (currentConfig.teams[i].hogsNumber > 0) and (currentConfig.teams[i].color = c); |
10448 | 129 |
inc(i) |
130 |
until (i >= 8) or (currentConfig.teams[i].hogsNumber = 0) or fColorMatched; |
|
131 |
||
132 |
if fColorMatched then |
|
133 |
begin |
|
134 |
i:= 0; |
|
135 |
inc(c) |
|
136 |
end; |
|
137 |
until not fColorMatched; |
|
138 |
||
10450 | 139 |
getUnusedColor:= c |
10448 | 140 |
end; |
141 |
||
10430 | 142 |
procedure runQuickGame; cdecl; |
10426 | 143 |
begin |
10432 | 144 |
with currentConfig do |
145 |
begin |
|
146 |
gameType:= gtLocal; |
|
147 |
arguments[0]:= ''; |
|
148 |
arguments[1]:= '--internal'; |
|
10448 | 149 |
arguments[2]:= '--nomusic'; |
10432 | 150 |
argumentsNumber:= 3; |
10426 | 151 |
|
10432 | 152 |
teams[0]:= createRandomTeam; |
10450 | 153 |
teams[0].color:= 0; |
10432 | 154 |
teams[1]:= createRandomTeam; |
10450 | 155 |
teams[1].color:= 1; |
10432 | 156 |
teams[1].botLevel:= 1; |
157 |
||
158 |
queueExecution; |
|
159 |
end; |
|
10426 | 160 |
end; |
161 |
||
10448 | 162 |
|
10430 | 163 |
procedure getPreview; cdecl; |
10426 | 164 |
begin |
165 |
with currentConfig do |
|
166 |
begin |
|
167 |
gameType:= gtPreview; |
|
168 |
arguments[0]:= ''; |
|
169 |
arguments[1]:= '--internal'; |
|
170 |
arguments[2]:= '--landpreview'; |
|
171 |
argumentsNumber:= 3; |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
172 |
|
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
173 |
queueExecution; |
10426 | 174 |
end; |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
175 |
end; |
10426 | 176 |
|
10448 | 177 |
procedure runLocalGame; cdecl; |
178 |
begin |
|
179 |
with currentConfig do |
|
180 |
begin |
|
181 |
gameType:= gtLocal; |
|
182 |
arguments[0]:= ''; |
|
183 |
arguments[1]:= '--internal'; |
|
184 |
arguments[2]:= '--nomusic'; |
|
185 |
argumentsNumber:= 3; |
|
186 |
||
187 |
queueExecution; |
|
188 |
end; |
|
189 |
end; |
|
190 |
||
191 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
192 |
procedure engineMessageCallback(p: pointer; msg: PChar; len: Longword); |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
193 |
begin |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
194 |
if len = 128 * 256 then guiCallbackFunction(guiCallbackPointer, mtPreview, msg, len) |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
195 |
end; |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
196 |
|
10430 | 197 |
procedure registerGUIMessagesCallback(p: pointer; f: TGUICallback); cdecl; |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
198 |
begin |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
199 |
guiCallbackPointer:= p; |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
200 |
guiCallbackFunction:= f; |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
201 |
|
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
202 |
registerIPCCallback(nil, @engineMessageCallback) |
10426 | 203 |
end; |
204 |
||
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
205 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
206 |
procedure tryAddTeam(teamName: PChar); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
207 |
var msg: ansistring; |
10446 | 208 |
i, hn, hedgehogsNumber: Longword; |
209 |
team: PTeam; |
|
10450 | 210 |
c: Longword; |
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
211 |
begin |
10446 | 212 |
with currentConfig do |
213 |
begin |
|
214 |
hedgehogsNumber:= 0; |
|
215 |
i:= 0; |
|
216 |
||
217 |
while (i < 8) and (teams[i].hogsNumber > 0) do |
|
218 |
begin |
|
219 |
inc(i); |
|
220 |
inc(hedgehogsNumber, teams[i].hogsNumber) |
|
221 |
end; |
|
222 |
||
223 |
// no free space for a team or reached hogs number maximum |
|
224 |
if (i > 7) or (hedgehogsNumber >= 48) then exit; |
|
225 |
||
226 |
team:= teamByName(teamName); |
|
227 |
if team = nil then exit; |
|
228 |
||
10448 | 229 |
c:= getUnusedColor; |
230 |
||
10446 | 231 |
teams[i]:= team^; |
232 |
||
233 |
if i = 0 then hn:= 4 else hn:= teams[i - 1].hogsNumber; |
|
234 |
if hn > 48 - hedgehogsNumber then hn:= 48 - hedgehogsNumber; |
|
235 |
teams[i].hogsNumber:= hn; |
|
10448 | 236 |
|
237 |
teams[i].color:= c; |
|
10446 | 238 |
end; |
239 |
||
240 |
||
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
241 |
msg:= '0' + #10 + teamName; |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
242 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
243 |
guiCallbackFunction(guiCallbackPointer, mtAddPlayingTeam, @msg[1], length(msg)); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
244 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
245 |
msg:= teamName; |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
246 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
247 |
guiCallbackFunction(guiCallbackPointer, mtRemoveTeam, @msg[1], length(msg)) |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
248 |
end; |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
249 |
|
10448 | 250 |
|
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
251 |
procedure tryRemoveTeam(teamName: PChar); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
252 |
var msg: ansistring; |
10448 | 253 |
i: Longword; |
254 |
tn: shortstring; |
|
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
255 |
begin |
10448 | 256 |
with currentConfig do |
257 |
begin |
|
258 |
i:= 0; |
|
259 |
tn:= teamName; |
|
260 |
while (i < 8) and (teams[i].teamName <> tn) do |
|
261 |
inc(i); |
|
262 |
||
263 |
// team not found??? |
|
264 |
if (i > 7) then exit; |
|
265 |
||
266 |
while (i < 7) and (teams[i + 1].hogsNumber > 0) do |
|
267 |
begin |
|
268 |
teams[i]:= teams[i + 1]; |
|
269 |
inc(i) |
|
270 |
end; |
|
271 |
||
272 |
teams[i].hogsNumber:= 0 |
|
273 |
end; |
|
274 |
||
10444
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
275 |
msg:= teamName; |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
276 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
277 |
guiCallbackFunction(guiCallbackPointer, mtRemovePlayingTeam, @msg[1], length(msg)); |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
278 |
guiCallbackFunction(guiCallbackPointer, mtAddTeam, @msg[1], length(msg)) |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
279 |
end; |
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
280 |
|
10406 | 281 |
end. |