-
Notifications
You must be signed in to change notification settings - Fork 1
/
TFKBot_lib.pas
522 lines (457 loc) · 11.7 KB
/
TFKBot_lib.pas
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
unit TFKBot_lib;
interface
uses
Engine_Reg,
Func_Lib,
Math_Lib,
Type_Lib,
MapObj_Lib,
ItemObj_Lib,
NFKBrick_Lib,
PlayersUtils_Lib,
Player_Lib;
const
NO_BOT_FIRE = true;
type
TTFKBot = class
constructor Create;
private
timer, thinktime: integer;
f_player : TPlayer;
public
//ðàçëè÷íûå ïàðàìåòðû óãëà ïðèöåëèâàíèÿ
//ïðèöåëèâàíèå âåä¸òñÿ ïîñòåïåííî :)
ang: single;
ang_delta, d_ang: single;//äåëüòà óãîë è ñêîðîñòü íàðàùèâàíèÿ óãëà
procedure ang_update;
public
wp_next: word;
procedure bot_restart;virtual; //ïðè ðåñïàóíå, ðåñòàðòå è.ò.ï. ;)
procedure bot_ondead;virtual; //ïðè ñìåðòè
procedure bot_onhit;virtual; //ïðè ðàíåíèè
procedure bot_think;virtual; //êîãäà ïîäóìàòü íàäî :)
procedure Update;virtual; //îáû÷íûé Update
function GetAngle(Player: TPlayer): single;
function GetDist(Player: TPLayer): single;
function GetNearest: TPlayer;virtual;
property pl : TPlayer read f_player write f_player;
end;
TAlienShaftBot = class(TTFKBot)
constructor Create;
private
reload: integer;
dist : single;
public
procedure Update;override;
procedure bot_ondead;override;
procedure bot_think;override;
function GetNearest: TPlayer;override;
end;
TBotMap = class(TTFKPlayerMap)
private
function GetAIs(ind: integer): TTFKBot;
public
procedure bot_update;
procedure bot_changemap;
procedure bot_restart;
function bot_count: integer;
property AIs[ind: integer]: TTFKBot read GetAIs;
end;
var
bot_default: boolean; //àêòèâåí ëè äåôîëòíûé áîò
//ïîêà ÷òî íå ðàáîòàåò, ò.ê. ñìûñëà íåò :)
procedure TFKBot_Init;
function TFKBot_CMD(Cmd: ShortString): boolean;
implementation
uses
Math,
Constants_Lib, SysUtils_, Map_Lib, Log_Lib,
Weapon_Lib;
procedure TFKBot_Init;
begin
bot_default:=false;
Console_CmdRegEx('bot_default', @bot_default, VT_BYTE, 0, 1, true);
Console_CmdReg('bot_add', @TFKBot_Cmd);
Console_CmdReg('bot_addmax', @TFKBot_Cmd);
Console_CmdReg('bot_remove', @TFKBot_Cmd);
end;
function TFKBot_CMD(Cmd: ShortString): boolean;
var
par : array [1..3] of string;
i : integer;
str, str_ : string;
function RandomName: string;
begin
case random(5) of
0: Result:='Òèìóð';
1: Result:='Âàí¸ê';
2: Result:='Æåí¸ê';
3: Result:='Ñàí¸ê';
4: Result:='Äåíèñ';
end;
end;
begin
Result := false;
str := Func_Lib.LowerCase(cmd);
str_ := str;
for i := 1 to 3 do
par[i] := StrSpace(str);
//äîáàâëåíèå íîâîãî áîòà
if par[1] = 'bot_add' then
begin
Result := true;
with Map do
Log_AddPlayer(pl_add(C_PLAYER_TFKBot, RandomName, 'sarge+default', true));
end;
if par[1] = 'bot_addmax' then
begin
Result := true;
with Map do
while Players < sv_maxplayers do
Log_AddPlayer(pl_add(C_PLAYER_TFKBot, RandomName, 'sarge+default', true));
end;
//óäàëåíèå ñòàðîãî áîòà ;)
if par[1] = 'bot_remove' then
begin
Result := true;
with Map do
if pl_find(-1, C_PLAYER_TFKBot) then
begin
Log_RemovePlayer(pl_current);
pl_delete_current;
end;
end;
end;
{ TBotMap }
procedure TBotMap.bot_changemap;
begin
//ñìåíèëàñü êàðòà, íàäî èíèöèàëèçèðîâàòü çàíîâî ïåðåìåííûå áîòà :)
end;
function TBotMap.bot_count: integer;
begin
Result := pl_count_ptype(C_PLAYER_TFKBOT);
end;
procedure TBotMap.bot_restart;
begin
if pl_find(-1, C_PLAYER_TFKBOT) then
repeat
AIs[pl_cur_ind].bot_restart;
until not pl_findnext(-1, C_PLAYER_TFKBOT);
end;
procedure TBotMap.bot_update;
begin
if pl_find(-1, C_PLAYER_TFKBOT) then
repeat
AIs[pl_cur_ind].Update;
until not pl_findnext(-1, C_PLAYER_TFKBOT);
end;
function TBotMap.GetAIs(ind: integer): TTFKBot;
begin
Result := TTFKBot(Player[ind].AI);
end;
{ TTFKBot }
procedure TTFKBot.ang_update;
var
a: single;
begin
a:=ang_norm2(ang-pl.absangle);
if signf(a-ang_delta)<=0 then
begin
//óãîë â äîïóñòèìûõ ïðåäåëàõ, ìîæíî êà÷àòüñÿ
d_ang:=signf(d_ang)*min(3, ang_delta/4.5); //äëÿ ðåçîíàíñà
end else
begin
//îïðåäåëÿåì ñêîðîñòü ïîâîðîòà
if a>32 then d_ang:=8
else if a>12 then d_ang:=4
else if a>2 then d_ang:=2
else d_ang:=a;
//îïðåäåëÿåì â êàêóþ ñòîðîíó ïîâîðà÷èâàòü óãîë
a:=ang_norm(ang-pl.absangle);
if a>180 then d_ang:=-d_ang
end;
pl.AbsAngle:=pl.sangle+d_ang;
end;
procedure TTFKBot.bot_restart;
begin
thinktime := 5;
wp_next:=0;
end;
procedure TTFKBot.bot_think;
var
target : TPlayer;
dist : single;
i, j, x, y: integer;
wp_temp: word;
// wp1, wp2: TWPObj;
begin
if pl.dead then
begin
pl.Key[KEY_FIRE].Down := true;
Exit;
end;
// Èùåì èãðîêà
target := GetNearest;
if not Map.player[0].dead
then target:=Map.player[0];
if target <> nil then
ang := GetAngle(target)*180/pi;
// Òîïàåì
pl.Key[KEY_UP].Down := false;
pl.Key[KEY_LEFT].Down := false;
pl.Key[KEY_RIGHT].Down := false;
pl.Key[KEY_FIRE].Down := false;
pl.Key[KEY_STRAFELEFT].Down := false;
pl.Key[KEY_STRAFERIGHT].Down := false;
if (target <> nil) and (Map.WP <> nil) then
with target, Map.WP do
begin
x:=trunc(pl.Pos.X-pl.dpos.X*3);
y:=trunc(pl.Pos.Y-pl.dpos.Y*3);
// x:=trunc(pl.Pos.X);
// y:=trunc(pl.Pos.Y);
wp_temp:=GetNearest(x, y);
// wp1:=WP[wp_next];
// wp2:=WP[wp_temp];
i:=Dist(x, y, wp_temp);
j:=Dist(x, y, wp_next);
if ((j>=i*2) or (i<15) )
// ( (x<=wp1.x)*(x<=wp2.x)>0 ) and
// ( (y<=wp1.y)*(y<=wp2.y)>0 ) )
and (wp_next<>wp_temp) then
wp_next:=wp_temp;
if FindWay(wp_next,
GetNearest(trunc(Pos.X), trunc(Pos.Y))) then
begin
with pl, WP[Way[WayLen - 2]] do
begin
if X < Pos.X then Key[KEY_STRAFELEFT].Down := true;
if X > Pos.X then Key[KEY_STRAFERIGHT].Down := true;
if Y < Pos.Y then Key[KEY_UP].Down := true;
//if Y > Pos.Y then Key[KEY_DOWN].Down := true;
end;
end;
end;
{
// âîò îíà! ïðîöåäóðà ïîèñêà ïóòè - random !!! Æ)
case random(200) of
1 : pl.Key[KEY_DOWN].Down := true;
2 : pl.Key[KEY_DOWN].Down := false;
3..13 : begin
pl.Key[KEY_STRAFELEFT].Down := true;
pl.Key[KEY_STRAFERIGHT].Down := false;
end;
14..24 : begin
pl.Key[KEY_STRAFERIGHT].Down := true;
pl.Key[KEY_STRAFELEFT].Down := false;
end;
25..35 : pl.Key[KEY_UP].Down := true;
end;
if (Map.block_s(pl.Pos.X - 32, pl.Pos.Y)) and
pl.Key[KEY_STRAFELEFT].Down then
begin
pl.Key[KEY_STRAFELEFT].Down := false;
pl.Key[KEY_STRAFERIGHT].Down := true;
end;
if (Map.block_s(pl.Pos.X + 32, pl.Pos.Y)) and
pl.Key[KEY_STRAFERIGHT].Down then
begin
pl.Key[KEY_STRAFERIGHT].Down := false;
pl.Key[KEY_STRAFELEFT].Down := true;
end;
end; }
// ñòðåëÿòü èëè íå ñòðåëÿòü? Âîò â ÷¸ì âîïðîñ...
if target <> nil then
begin
dist := Map.TraceVector(pl.shotpos.X, pl.shotpos.Y, pl.AbsAngle*Pi/180);
if dist < GetDist(target) then
with Map do
for i := 0 to Map.Players - 1 do
with player[i] do
if (playertype and C_PLAYER_ACTIVE > 0) and
(UID <> pl.UID) and
not player[i].dead then
begin
ang := self.GetAngle(player[i])*180/pi;
dist := TraceVector(pl.shotpos.X, pl.shotpos.Y, ang*Pi/180);
if dist > GetDist(player[i]) then
break;
end;
end;
//òåïåðü åñëè ìû ïîäóìàëè êàê íàâîäèòü, ïðîâåðèì äîñòàëè ëè ìû äî òîãî èãðîêà
with pl, Map do
begin
dist := TraceVector(shotpos.X, shotpos.Y, AbsAngle*Pi/180);
if TracePlayers(shotpos.X, shotpos.Y, AbsAngle*Pi/180, dist, UID)<> nil then
pl.Key[KEY_FIRE].Down := not NO_BOT_FIRE;
end;
end;
procedure TTFKBot.Update;
begin
ang_update; //íàâîäèì óãîë
if timer = 0 then
bot_think;
if thinktime > 0 then
timer := (timer + 1) mod thinktime
else
timer := 0;
end;
constructor TTFKBot.Create;
begin
ang_delta:=10;//îøèáêà
bot_restart;
end;
// äëÿ ñëåäóþùèõ äâóõ îáðàáîòîê èñïîëüçóåòñÿ
// ïåðåìåííàÿ pl.hit_UID; êîíñòàíòû äëÿ âîäû è.ò.ï.
// ñìîòðåòü constants_lib.
procedure TTFKBot.bot_ondead;
begin
end;
procedure TTFKBot.bot_onhit;
begin
end;
function TTFKBot.GetAngle(Player: TPlayer): single;
var
x, y : single;
begin
x := Player.shotpos.X - pl.shotpos.X;
y := Player.shotpos.Y - pl.shotpos.Y;
Result := arctan2(y, x + 0.000001);
end;
function TTFKBot.GetDist(Player: TPLayer): single;
begin
Result := sqrt(sqr(pl.shotpos.X - Player.Pos.X) +
sqr(pl.shotpos.Y - Player.Pos.Y));
end;
function TTFKBot.GetNearest: TPlayer;
var
i, l : integer;
min : integer;
begin
Result := nil;
min := 48000;
if Map.Players < 0 then Exit;
with Map do
for i := 0 to Players - 1 do
with player[i] do
if (playertype and C_PLAYER_ACTIVE > 0) and
(UID <> pl.UID) and
not player[i].dead then
begin
l := trunc(abs(pl.Pos.X - Pos.X) + abs(pl.Pos.Y - Pos.Y));
if l < min then
begin
min := l;
Result := player[i];
end;
end;
end;
{ TAlienShaftBot }
procedure TAlienShaftBot.bot_ondead;
begin
pl.cur_weapon:=WPN_GAUNTLET;
pl.cur_wpn:=WPN_GAUNTLET;
pl.next_weapon:=WPN_GAUNTLET;
end;
procedure TAlienShaftBot.bot_think;
const
dist1 : single = 300.0;
dist2 : single = 250.0;
dist3 : single = 100.0;
var
target, targ : TPlayer;
begin
// Èùåì èãðîêà
pl.Key[KEY_UP].Down := false;
pl.Key[KEY_LEFT].Down := false;
pl.Key[KEY_RIGHT].Down := false;
pl.Key[KEY_DOWN].Down := false;
pl.Key[KEY_FIRE].Down := false;
target := GetNearest;
if target <> nil then
begin
ang := GetAngle(target)*180/pi;
dist:=Sqr(target.Pos.x-pl.Pos.x)+Sqr(target.Pos.y-pl.Pos.y);
if dist<sqr(dist1) then
begin
if target.pos.x>pl.pos.x+dist3 then
pl.Key[KEY_RIGHT].Down := true;
if target.pos.x<pl.pos.x-dist3 then
pl.Key[KEY_LEFT].Down := true
end;
end else dist:=3000000;
dist:=Map.TraceVector(pl.Pos.X, pl.Pos.Y, ang*Pi/180);
if reload=0 then
begin
targ:=Map.TracePlayers(pl.Pos.X, pl.Pos.Y, ang*Pi/180, dist, pl.UID);
if (targ<>nil) then
begin
dist:=Sqr(targ.Pos.x-pl.Pos.x)+Sqr(targ.Pos.y-pl.Pos.y);
if (dist<sqr(dist2))then
begin
reload:=21;
ang_delta:=5;//îøèáêà
end;
end;
end else
begin
if reload>1 then
begin
pl.next_weapon:=WPN_GAUNTLET;
pl.KEY[KEY_FIRE].Down:=true;
dec(reload);
if reload=1 then
pl.TakeWpn(WPN_SHAFT, 30, 0);
end else
begin
pl.next_weapon:=WPN_SHAFT;
if pl.ammo[WPN_SHAFT]>0 then
pl.KEY[KEY_FIRE].Down:=true
else
begin
reload:=0;
ang_delta:=0;
end;
end;
end;
end;
constructor TAlienShaftBot.Create;
begin
inherited;
thinktime:=5;
ang_delta:=0;
end;
function TAlienShaftBot.GetNearest: TPlayer;
var
i, l : integer;
min : integer;
begin
Result := nil;
min := 48000;
if Map.Players < 0 then Exit;
with Map do
for i := 0 to Players - 1 do
with player[i] do
if (playertype and C_PLAYER_LOCAL > 0) and
(UID <> pl.UID) and
not player[i].dead then
begin
l := trunc(abs(pl.Pos.X - Pos.X) + abs(pl.Pos.Y - Pos.Y));
if l < min then
begin
min := l;
Result := player[i];
end;
end;
end;
procedure TAlienShaftBot.Update;
begin
ang_update; //íàâîäèì óãîë
if timer = 0 then
bot_think;
if thinktime > 0 then
timer := (timer + 1) mod thinktime
else
timer := 0;
end;
end.