-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot_util.pas
210 lines (189 loc) · 7.28 KB
/
bot_util.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
{
BOT.DLL for Need For Kill
(c) 3d[Power]
http://www.3dpower.org
unit: bot_util
purpose: useful procedures & functions
}
unit bot_util;
interface
uses bot_register, bot_defs, math;
Const _PiDiv180 = PI / 180;
_180DivPi = 180 / PI;
// ============================
{function Deg2Rad(Degrees: Extended): Extended;
function Rad2Deg(Radians: Extended): Extended;
}function strpar(s:string; pos : word):string;{
function PlayersCount:byte;
function CurrentAmmo(i:byte):byte;
function GetDist(x1,y1,x2,y2: real): word;
procedure LookAt(i : byte; x, y :real);
function TraceVector(x1,y1,x2,y2:single):boolean;
function TouchRegion(I, X1,Y1, width, height : integer):boolean;
// ============================
}
implementation
{
// ============================
// Áîëåå áûñòðûå ôóíêöèè äëÿ êîíâåðòàöèè çíà÷åíèé óãëîâ
// ============================
function Deg2Rad(Degrees: Extended): Extended; begin Result := Degrees * _PiDiv180; end; // Àíàëîã ôóíêöèè DegToRad
function Rad2Deg(Radians: Extended): Extended; begin Result := Radians * _180DivPi; end; // Àíàëîã ôóíêöèè RadToDeg
}
// ============================
// return string between spaces
// ============================
function strpar(s:string; pos : word):string;
var counter, del1 : byte;
len, i : word;
const delimeter : char = ' ';
begin
result := ''; len := length(s); del1 := 1;
if len = 0 then exit; counter := 0;
for i := 1 to len do
if (s[i]=delimeter) or (i=len) then begin
if counter = pos then begin
if (pos=0) and (s[i]<>delimeter) then result := copy(s, del1, i-del1+1) else
if (pos=0) then result := copy(s, del1, i-del1) else
if (i=len) and (s[i]<>delimeter) then result := copy(s, del1+1, i-del1+2) else
result := copy(s, del1+1, i-del1);
exit;
end;
del1 := i;
inc(counter);
end;
end;
{
// ==================
// gets player count
// ==================
function PlayersCount:byte;
var i : byte;
begin
result :=0;
for i := 0 to 7 do if players[i]<> nil then inc(result);
end;
// âñïîìîãàòåëüíàÿ ïðîöåäóðà äëÿ TraceVector
Function InIs(I,m1,m2:single):Boolean;
begin
If m1<m2 then Result:=(m1<=I)and(I<=m2)
else Result:=(m2<=I)and(I<=m1);
end;
// ==================
// Ïðîâåðÿåò ñòîëêíîâåíèå ñ áðèêàìè
// true åñëè âåêòîð ïðîâîäèì ñ x1,y1 äî x2,y2
// ==================
function TraceVector(x1,y1,x2,y2:single):boolean;
var bounds,start : array[0..3] of word; // Left, Up, Right, Down
I,J:integer;
zA,zB,nX,nY:single; // Y:=zA*X+zB;
begin
if (x2=x1) and (y2=y1) then begin
result := true;
exit;
end;
result := false;
if(x1=x2) then zA:=0 else zA:=(y2-y1)/(x2-x1); zB:=y2-zA*x2;
if x1 <= x2 then begin start[0] := trunc(x1) div 32; start[2] := trunc(x2) div 32; end
else begin start[0] := trunc(x2) div 32; start[2] := trunc(x1) div 32; end;
if y1 <= y2 then begin start[1] := trunc(y1) div 16; start[3] := trunc(y2) div 16; end
else begin start[1] := trunc(y2) div 16; start[3] := trunc(y1) div 16; end;
For I := start[0] to start[2] do
For J := start[1] to start[3] do
If GetBrickStruct(I, J).block then begin
bounds[0] := I*32;
bounds[1] := J*16;
bounds[2] := bounds[0]+32;
bounds[3] := bounds[1]+16;
// calculating X
If not(x2=x1) then begin
If x1<x2 then nX:=bounds[0] else nX := bounds[2];
If InIs(nX, x1, x2) then begin
nY:=zA*nX+zB;
If InIs(nY, bounds[1], bounds[3]) then Exit;
end;
end;
// calculating Y
If y2<>y1 then begin
If y2>y1 then nY:=bounds[1] else nY:=bounds[3];
If InIs(nY, y1, y2) then begin
If zA=0 then nX:=x1 else nX:=( (nY-zB)/zA );
If InIs(nX, bounds[0], bounds[2]) then Exit;
end;
end;
end;
Result:=True;
end;
// ==================
// Íàõîäèòñÿ ëè èãðîê íà çåìëå (ïðèìåð ïðîöåäóðû)
// ==================
function IsOnground(sender : TPlayer) : boolean; // this procedure checkz if the player onground
var z : integer;
begin // compare current coordinates via brick matrix;
with sender as TPlayer do begin
z := 9;
if x <= 0 then x := 100; // HACK: crash fix.
if y <= 0 then y := 100;
result := true;
if (GetBrickStruct( trunc(x-z) div 32, trunc(y+25) div 16).block = true) and
(GetBrickStruct( trunc(x-z) div 32, trunc(y+23) div 16).block = false) then exit;
if (GetBrickStruct( trunc(x+z) div 32, trunc(y+25) div 16).block = true) and
(GetBrickStruct( trunc(x+z) div 32, trunc(y+23) div 16).block = false) then exit;
if (GetBrickStruct( trunc(x-z) div 32, trunc(y+24) div 16).block = true) and
(GetBrickStruct( trunc(x-z) div 32, trunc(y+8) div 16).block = false) then exit;
if (GetBrickStruct( trunc(x+z) div 32, trunc(y+24) div 16).block = true) and
(GetBrickStruct( trunc(x+z) div 32, trunc(y+8) div 16).block = false) then exit;
result := false;
end;
end;
// ============================
// Âîçâðàùàåò êîë-âî ïàòðîíîâ òåêóùåãî îðóæèÿ
// ============================
function CurrentAmmo(i:byte):byte;
begin
result := 200;
if players[i]=nil then exit;
case players[i].weapon of
C_WPN_MACHINE : result := players[i].ammo_mg;
C_WPN_SHOTGUN : result := players[i].ammo_sg;
C_WPN_GRENADE : result := players[i].ammo_gl;
C_WPN_ROCKET : result := players[i].ammo_rl;
C_WPN_SHAFT : result := players[i].ammo_sh;
C_WPN_RAIL : result := players[i].ammo_rl;
C_WPN_PLASMA : result := players[i].ammo_pl;
C_WPN_BFG : result := players[i].ammo_bfg;
end;
end;
// ============================
// Ðàññòîÿíèå ìåæäó 2ìÿ òî÷êàìè
// ============================
function GetDist(x1,y1,x2,y2: real): word;
begin
result:=round(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
end;
// ============================
// Ïîâîðà÷èâàåò îðóæèå áîòà ê êîîðäèíàòàì x, y
// ============================
procedure LookAt(i : byte; x, y :real);
var angle : integer;
begin
if players[i]=nil then exit;
if players[i].bot = false then exit;
angle := (round(Rad2Deg(ArcTan2(players[i].y - y + 5,players[i].x-x))-90) mod 360);
if angle < 0 then angle := 360+angle;
players[i].fangle := angle;
end;
// ============================
// Ïðîâåðÿåò äîòðàãèâàåòñÿ ëè èãðîê (áîò) äî ðåãèîíà ðàçìåðîì width íà height, ðàñïîëîæåííîì â ïîçèöèè x1, y1
// Ïàðàìåòðû X1,Y1, width, height èìåðÿþòñÿ â áðèêàõ, à íå â ïèêñåëÿõ.
// ============================
function TouchRegion(I, X1,Y1, width, height : integer):boolean;
begin
result := false;
if players[i] = nil then exit;
if (players[i].x + 9 >= x1*32) and (players[i].x - 8 <= x1*32+width*32) then
if (players[i].y + 23 >= y1*16) and (players[i].y - 23 <= y1*16+height*16) then
result := true;
end;
}
end.