-
Notifications
You must be signed in to change notification settings - Fork 1
/
tanks.pas
372 lines (305 loc) · 6.5 KB
/
tanks.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
unit tanks;
interface
uses
geom,
math,
bullets,
Classes,Graphics,
SysUtils;
const
TANK_RADIUS=11;
TANK_MAX_HP=50;
MAX_ROTATION_ANGLE=2*Pi/256;
MAX_ACCELERATION=0.6;
MIN_ACCELERATION=-0.4;
MAX_SPEED=12;
MIN_SPEED=-5;
MAX_FIRE_DELAY_DEFAULT=36;
MAX_FIRE_DELAY_CHEAT=10;
SPEED_EPSILON=0.08;
type
TTank=class
private
max_fire_delay:integer;
fire_delay:integer;
hitpoints:integer;
speed,
new_alfa,alfa:extended;
x,y,
acceleration:extended;
tank_name:string;
hits:integer;
frags:integer;
procedure apply_accelerate;
procedure apply_move;
procedure apply_rotate;
procedure apply_fire;
procedure apply_actions;
public
function isAssigned:boolean;
function distanceTo(other:TTank):double;
procedure setAlfa(a:double);
function getName:String;
procedure setHP(hp:integer);
function getHP:integer;
procedure decHP(hit_hp:integer);
procedure setMaxFireDelay(delay:integer);
procedure Init;virtual;
procedure setAcceleration(acc:double);
constructor Create(x0,y0,alfa0:double; name:string);
procedure rotate(newAlfa:double);
procedure use_breaks;
function getCurrentNewAlfa():double;
function getAngleToObject(x1,y1:double):double;
procedure turnTo(other_tank:TTank);overload;
destructor Destroy; override;
procedure DoMove;
//user routines:
procedure fire;
function getX:double;
function getY:double;
function getAlfa:double;
function getDAlfa:double;
function getHits:integer;
function getFrags:integer;
procedure incHits;
procedure incFrags;
procedure get_actions;virtual;
function getTankName:string;virtual;
function getTankColor:TColor;virtual;
end;
TTankList=class(TList)
function at(i:integer):TTank;
end;
implementation
uses
globals;
{ TTank }
procedure TTank.apply_accelerate;
begin
if acceleration>MAX_ACCELERATION then
acceleration:=MAX_ACCELERATION;
if acceleration<MIN_ACCELERATION then
acceleration:=MIN_ACCELERATION;
speed:=speed+acceleration;
if speed>MAX_SPEED then speed:=MAX_SPEED;
if speed<MIN_SPEED then speed:=MIN_SPEED;
end;
procedure TTank.apply_actions;
begin
apply_accelerate;
apply_move;
apply_rotate;
apply_fire;
end;
procedure TTank.apply_fire;
begin
end;
procedure TTank.apply_move;
var dr,xnew,ynew:double;
tank_collision:boolean;
world_collision:boolean;
i:integer;
tank:TTank;
dx,dy:extended;
begin
dr:=speed/8;
xnew:=x+dr*cos(alfa);
ynew:=y+dr*sin(alfa);
world_collision:=false;
if (xnew<0)then
begin xnew:=0; world_collision:=true; end;
if (xnew>getWorld.width)then
begin xnew:=getWorld.width; world_collision:=true; end;
if (ynew<0)then
begin ynew:=0; world_collision:=true; end;
if (ynew>getWorld.height) then
begin ynew:=getWorld.height; world_collision:=true; end;
if world_collision then speed:=-speed;
tank_collision:=false;
for i:=0 to getWorld.tanks.count-1 do
begin
tank:=getWorld.tanks.at(i);
dx:=(tank.getX-xnew);
dy:=(tank.getY-ynew);
if tank<>Self then
if dx*dx+dy*dy<sqr(2*TANK_RADIUS) then
begin
tank_collision:=true;
break
end;
end;
if tank_collision then speed:=-speed
else
begin
x:=xnew;
y:=ynew;
end;
end;
procedure TTank.apply_rotate;
var
da:extended;
begin
da:=normalize_angle_rot(new_alfa-alfa);
alfa:=alfa+sign(da)*min(MAX_ROTATION_ANGLE,abs(da));
end;
constructor TTank.Create;
begin
hits:=0;
frags:=0;
max_fire_delay:=MAX_FIRE_DELAY_DEFAULT;
fire_delay:=0;
tank_name:=name;
hitpoints:=TANK_MAX_HP;
acceleration:=0;
speed:=0;
x:=x0;
y:=y0;
alfa:=alfa0;
new_alfa:=alfa;
Init;
end;
procedure TTank.decHP(hit_hp: integer);
begin
dec(hitpoints,hit_hp);
if hitpoints<0 then hitpoints:=0;
end;
destructor TTank.Destroy;
begin
inherited;
end;
procedure TTank.DoMove;
begin
get_actions;
apply_actions;
end;
procedure TTank.fire;
begin
if fire_delay>0 then
begin
dec(fire_delay);
exit;
end;
fire_delay:=max_fire_delay+Random(max_fire_delay);
getWorld.addBullet(TBullet.Create(x,y,alfa,Self))
end;
function TTank.getAlfa: double;
begin
Result:=alfa;
end;
function TTank.getHP: integer;
begin
Result:=hitpoints;
end;
function TTank.getDAlfa: double;
begin
Result:=normalize_angle_rot(new_alfa-alfa);
end;
function TTank.getTankColor: TColor;
begin
Result:=clBlack;
end;
function TTank.getTankName: string;
begin
Result:='no name';
end;
function TTank.getX: double;
begin
Result:=x;
end;
function TTank.getY: double;
begin
Result:=y;
end;
procedure TTank.get_actions;
begin
end;
procedure TTank.Init;
begin
end;
procedure TTank.rotate(newAlfa: double);
begin
new_alfa:=normalize_angle(newAlfa);
end;
procedure TTank.setAcceleration(acc: double);
begin
acceleration:=acc;
if acceleration>0 then acceleration:=MAX_ACCELERATION;
if acceleration<0 then acceleration:=MIN_ACCELERATION;
end;
function TTank.getName: String;
begin
Result:=tank_name
end;
function TTank.getAngleToObject(x1, y1: double): double;
var a,b:double;
begin
a:=x1-getX;
b:=y1-getY;
Result:=0;
if (a<>0) then
begin
Result:=ArcTan2(b,a)
end
end;
procedure TTank.turnTo(other_tank: TTank);
begin
rotate(getAngleToObject(other_tank.getX,
other_tank.getY))
end;
procedure TTank.use_breaks;
begin
if speed>0 then acceleration:=MIN_ACCELERATION;
if speed<0 then acceleration:=MAX_ACCELERATION;
if abs(speed)<SPEED_EPSILON then
begin
speed:=0;
acceleration:=0;
end;
end;
function TTank.getCurrentNewAlfa: double;
begin
Result:=new_alfa;
end;
procedure TTank.setHP(hp: integer);
begin
hitpoints:=hp
end;
procedure TTank.setMaxFireDelay(delay: integer);
begin
max_fire_delay:=delay;
end;
procedure TTank.setAlfa(a: double);
begin
alfa:=a;
end;
function TTank.getFrags: integer;
begin
result:=frags;
end;
function TTank.getHits: integer;
begin
result:=hits;
end;
procedure TTank.incHits;
begin
inc(hits);
end;
procedure TTank.incFrags;
begin
inc(frags)
end;
function TTank.distanceTo(other: TTank): double;
begin
Result:=distance(getX,getY,other.getX,other.getY)
end;
function TTank.isAssigned: boolean;
begin
Result:=Assigned(Self);
end;
{ TTankList }
function TTankList.at(i: integer): TTank;
begin
Result:=TTank(items[i])
end;
end.