author | unc0rr |
Sat, 01 Nov 2014 00:23:22 +0300 | |
branch | qmlfrontend |
changeset 10448 | 4cb727e029fa |
parent 10446 | 7ae44f42a689 |
child 10450 | bf9e30b4ef9b |
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; |
|
10406 | 100 |
begin |
101 |
end; |
|
102 |
||
10430 | 103 |
procedure setSeed(seed: PChar); cdecl; |
104 |
begin |
|
105 |
currentConfig.seed:= seed |
|
106 |
end; |
|
107 |
||
10448 | 108 |
|
10430 | 109 |
function getSeed: PChar; cdecl; |
110 |
begin |
|
111 |
getSeed:= str2PChar(currentConfig.seed) |
|
112 |
end; |
|
113 |
||
10448 | 114 |
function getUnusedColor: shortstring; |
115 |
var i, c: Longword; |
|
116 |
fColorMatched: boolean; |
|
117 |
begin |
|
118 |
c:= 0; |
|
119 |
i:= 0; |
|
120 |
repeat |
|
121 |
repeat |
|
122 |
fColorMatched:= (currentConfig.teams[i].hogsNumber > 0) and (currentConfig.teams[i].color = colorsSet[c]); |
|
123 |
inc(i) |
|
124 |
until (i >= 8) or (currentConfig.teams[i].hogsNumber = 0) or fColorMatched; |
|
125 |
||
126 |
if fColorMatched then |
|
127 |
begin |
|
128 |
i:= 0; |
|
129 |
inc(c) |
|
130 |
end; |
|
131 |
until not fColorMatched; |
|
132 |
||
133 |
getUnusedColor:= colorsSet[c] |
|
134 |
end; |
|
135 |
||
10430 | 136 |
procedure runQuickGame; cdecl; |
10426 | 137 |
begin |
10432 | 138 |
with currentConfig do |
139 |
begin |
|
140 |
gameType:= gtLocal; |
|
141 |
arguments[0]:= ''; |
|
142 |
arguments[1]:= '--internal'; |
|
10448 | 143 |
arguments[2]:= '--nomusic'; |
10432 | 144 |
argumentsNumber:= 3; |
10426 | 145 |
|
10432 | 146 |
teams[0]:= createRandomTeam; |
10448 | 147 |
teams[0].color:= colorsSet[0]; |
10432 | 148 |
teams[1]:= createRandomTeam; |
10448 | 149 |
teams[1].color:= colorsSet[1]; |
10432 | 150 |
teams[1].botLevel:= 1; |
151 |
||
152 |
queueExecution; |
|
153 |
end; |
|
10426 | 154 |
end; |
155 |
||
10448 | 156 |
|
10430 | 157 |
procedure getPreview; cdecl; |
10426 | 158 |
begin |
159 |
with currentConfig do |
|
160 |
begin |
|
161 |
gameType:= gtPreview; |
|
162 |
arguments[0]:= ''; |
|
163 |
arguments[1]:= '--internal'; |
|
164 |
arguments[2]:= '--landpreview'; |
|
165 |
argumentsNumber:= 3; |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
166 |
|
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
167 |
queueExecution; |
10426 | 168 |
end; |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
169 |
end; |
10426 | 170 |
|
10448 | 171 |
procedure runLocalGame; cdecl; |
172 |
begin |
|
173 |
with currentConfig do |
|
174 |
begin |
|
175 |
gameType:= gtLocal; |
|
176 |
arguments[0]:= ''; |
|
177 |
arguments[1]:= '--internal'; |
|
178 |
arguments[2]:= '--nomusic'; |
|
179 |
argumentsNumber:= 3; |
|
180 |
||
181 |
queueExecution; |
|
182 |
end; |
|
183 |
end; |
|
184 |
||
185 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
186 |
procedure engineMessageCallback(p: pointer; msg: PChar; len: Longword); |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
187 |
begin |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
188 |
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
|
189 |
end; |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
190 |
|
10430 | 191 |
procedure registerGUIMessagesCallback(p: pointer; f: TGUICallback); cdecl; |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
192 |
begin |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
193 |
guiCallbackPointer:= p; |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
194 |
guiCallbackFunction:= f; |
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
195 |
|
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
196 |
registerIPCCallback(nil, @engineMessageCallback) |
10426 | 197 |
end; |
198 |
||
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
|
199 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
200 |
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
|
201 |
var msg: ansistring; |
10446 | 202 |
i, hn, hedgehogsNumber: Longword; |
203 |
team: PTeam; |
|
10448 | 204 |
c: 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
|
205 |
begin |
10446 | 206 |
with currentConfig do |
207 |
begin |
|
208 |
hedgehogsNumber:= 0; |
|
209 |
i:= 0; |
|
210 |
||
211 |
while (i < 8) and (teams[i].hogsNumber > 0) do |
|
212 |
begin |
|
213 |
inc(i); |
|
214 |
inc(hedgehogsNumber, teams[i].hogsNumber) |
|
215 |
end; |
|
216 |
||
217 |
// no free space for a team or reached hogs number maximum |
|
218 |
if (i > 7) or (hedgehogsNumber >= 48) then exit; |
|
219 |
||
220 |
team:= teamByName(teamName); |
|
221 |
if team = nil then exit; |
|
222 |
||
10448 | 223 |
c:= getUnusedColor; |
224 |
||
10446 | 225 |
teams[i]:= team^; |
226 |
||
227 |
if i = 0 then hn:= 4 else hn:= teams[i - 1].hogsNumber; |
|
228 |
if hn > 48 - hedgehogsNumber then hn:= 48 - hedgehogsNumber; |
|
229 |
teams[i].hogsNumber:= hn; |
|
10448 | 230 |
|
231 |
teams[i].color:= c; |
|
10446 | 232 |
end; |
233 |
||
234 |
||
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
|
235 |
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
|
236 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
237 |
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
|
238 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
239 |
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
|
240 |
|
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 |
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
|
242 |
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
|
243 |
|
10448 | 244 |
|
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
|
245 |
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
|
246 |
var msg: ansistring; |
10448 | 247 |
i: Longword; |
248 |
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
|
249 |
begin |
10448 | 250 |
with currentConfig do |
251 |
begin |
|
252 |
i:= 0; |
|
253 |
tn:= teamName; |
|
254 |
while (i < 8) and (teams[i].teamName <> tn) do |
|
255 |
inc(i); |
|
256 |
||
257 |
// team not found??? |
|
258 |
if (i > 7) then exit; |
|
259 |
||
260 |
while (i < 7) and (teams[i + 1].hogsNumber > 0) do |
|
261 |
begin |
|
262 |
teams[i]:= teams[i + 1]; |
|
263 |
inc(i) |
|
264 |
end; |
|
265 |
||
266 |
teams[i].hogsNumber:= 0 |
|
267 |
end; |
|
268 |
||
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
|
269 |
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
|
270 |
|
47a6231f1fc1
Teams widget now allows to add and remove teams (basic implementation, no checks performed, no colors, no hedgehogs)
unc0rr
parents:
10432
diff
changeset
|
271 |
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
|
272 |
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
|
273 |
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
|
274 |
|
10406 | 275 |
end. |